project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Downloader/DownloadAsyncTask.java
branchhedgeroid
changeset 5671 ba4c3a4c8b09
parent 5621 ea796c83ea47
equal deleted inserted replaced
5669:a806dbe25288 5671:ba4c3a4c8b09
    17  */
    17  */
    18 
    18 
    19 
    19 
    20 package org.hedgewars.mobile.Downloader;
    20 package org.hedgewars.mobile.Downloader;
    21 
    21 
       
    22 import java.io.BufferedInputStream;
    22 import java.io.File;
    23 import java.io.File;
    23 import java.io.FileNotFoundException;
    24 import java.io.FileNotFoundException;
    24 import java.io.FileOutputStream;
    25 import java.io.FileOutputStream;
    25 import java.io.IOException;
    26 import java.io.IOException;
    26 import java.net.HttpURLConnection;
    27 import java.net.HttpURLConnection;
    27 import java.net.URL;
    28 import java.net.URL;
       
    29 import java.security.MessageDigest;
       
    30 import java.security.NoSuchAlgorithmException;
    28 import java.util.zip.ZipEntry;
    31 import java.util.zip.ZipEntry;
    29 import java.util.zip.ZipInputStream;
    32 import java.util.zip.ZipInputStream;
    30 
    33 
    31 import android.os.AsyncTask;
    34 import android.os.AsyncTask;
    32 import android.util.Log;
    35 import android.util.Log;
    37  * @author Xeli
    40  * @author Xeli
    38  *
    41  *
    39  */
    42  */
    40 public class DownloadAsyncTask extends AsyncTask<String, Object, Long> {
    43 public class DownloadAsyncTask extends AsyncTask<String, Object, Long> {
    41 
    44 
       
    45 	private final static String URL_WITHOUT_SUFFIX = "http://hedgewars.googlecode.com/files/data_5631.";
       
    46 	//private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton.";
       
    47 	private final static String URL_ZIP_SUFFIX = "zip";
       
    48 	private final static String URL_HASH_SUFFIX = "hash";
       
    49 
    42 	private DownloadService service;
    50 	private DownloadService service;
    43 	private long lastUpdateMillis = 0;
    51 	private long lastUpdateMillis = 0;
    44 	
    52 
    45 	public DownloadAsyncTask(DownloadService _service){
    53 	public DownloadAsyncTask(DownloadService _service){
    46 		service = _service;
    54 		service = _service;
    47 	}
    55 	}
    48 	
    56 
    49 	/**
    57 	/**
    50 	 * 
    58 	 * 
    51 	 * @param params - 2 Strings, first is the path where the unzipped files will be stored, second is the URL to download from
    59 	 * @param params - 2 Strings, first is the path where the unzipped files will be stored, second is the URL to download from
    52 	 */
    60 	 */
    53 	protected Long doInBackground(String... params) {
    61 	protected Long doInBackground(String... params) {
    54 		HttpURLConnection conn = null;
    62 		HttpURLConnection conn = null;
       
    63 		MessageDigest digester = null;
       
    64 		String rootZipDest = params[0];
       
    65 
       
    66 		File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
       
    67 		rootDest.mkdir();
       
    68 
    55 		try {
    69 		try {
    56 			String rootZipDest = params[0];
    70 			URL url = new URL(URL_WITHOUT_SUFFIX + URL_ZIP_SUFFIX);
    57 
       
    58 			File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
       
    59 			rootDest.mkdir();
       
    60 			
       
    61 			URL url = new URL(params[1]);
       
    62 			conn = (HttpURLConnection)url.openConnection();
    71 			conn = (HttpURLConnection)url.openConnection();
    63 			String contentType = conn.getContentType();
    72 		} catch (IOException e) {
    64 
    73 			e.printStackTrace();
    65 			if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips
    74 			return -1l;
    66 				ZipInputStream input = new ZipInputStream(conn.getInputStream());
    75 		}
    67 				int bytesDecompressed = 0;
    76 
    68 				final int kbytesToProcess = conn.getContentLength()/1024;
    77 		String contentType = conn.getContentType();
    69 				
    78 
    70 				service.start(kbytesToProcess);
    79 		if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips
    71 				
    80 			int bytesDecompressed = 0;
    72 				ZipEntry entry = null;
    81 			ZipEntry entry = null;
    73 				while((entry = input.getNextEntry()) != null){
    82 			ZipInputStream input = null;
    74 					String fileName = entry.getName();
    83 			int kbytesToProcess = conn.getContentLength()/1024;
    75 					
    84 
    76 					if(isCancelled()) break;
    85 			byte[] buffer = new byte[1024];
    77 					else if(System.currentTimeMillis() - lastUpdateMillis > 1000){
    86 			service.start(kbytesToProcess);
    78 						lastUpdateMillis = System.currentTimeMillis();
    87 
    79 						publishProgress(bytesDecompressed, kbytesToProcess, fileName);
    88 			try {
       
    89 				digester = MessageDigest.getInstance("MD5");
       
    90 
       
    91 			} catch (NoSuchAlgorithmException e1) {
       
    92 				e1.printStackTrace();
       
    93 			}
       
    94 
       
    95 			try{
       
    96 				input = new ZipInputStream(conn.getInputStream());
       
    97 				entry = input.getNextEntry();	
       
    98 			}catch(IOException e){
       
    99 				e.printStackTrace();
       
   100 				if(conn != null) conn.disconnect();
       
   101 				return -1l;
       
   102 			}
       
   103 
       
   104 			while(entry != null){
       
   105 				if(isCancelled()) break;
       
   106 
       
   107 				String fileName = entry.getName();
       
   108 				File f = new File(rootZipDest + fileName);
       
   109 				bytesDecompressed += entry.getCompressedSize();
       
   110 
       
   111 				if(entry.isDirectory()){
       
   112 					f.mkdir();
       
   113 				}else{
       
   114 					if(f.exists()){
       
   115 						f.delete();
    80 					}
   116 					}
    81 					
   117 
    82 					Log.e("bla", fileName);
   118 					FileOutputStream output = null;
    83 					bytesDecompressed += entry.getCompressedSize();
   119 					try {
    84 					
   120 						f.createNewFile();
    85 					File f = new File(rootZipDest + fileName);
   121 						output = new FileOutputStream(f);
    86 
   122 
    87 					if(entry.isDirectory()){
   123 						int count = 0;
    88 						f.mkdir();
   124 						while((count = input.read(buffer)) != -1){
    89 					}else{
   125 							output.write(buffer, 0, count);
    90 						if(f.exists()){
   126 							digester.update(buffer, 0, count);
    91 							f.delete();
   127 							if(System.currentTimeMillis() - lastUpdateMillis > 1000){
       
   128 								lastUpdateMillis = System.currentTimeMillis();
       
   129 								publishProgress(bytesDecompressed, kbytesToProcess, fileName);
       
   130 							}
    92 						}
   131 						}
    93 
   132 						output.flush();
       
   133 						input.closeEntry();
       
   134 					} catch (FileNotFoundException e) {
       
   135 						e.printStackTrace();
       
   136 						if(conn != null) conn.disconnect();
       
   137 						return -1l;
       
   138 					} catch (IOException e) {
       
   139 						e.printStackTrace();
       
   140 						if(conn != null) conn.disconnect();
       
   141 						return -1l;
       
   142 					}finally{
    94 						try {
   143 						try {
    95 							f.createNewFile();
   144 							if( output != null) output.close();
    96 							FileOutputStream out = new FileOutputStream(f);
   145 						} catch (IOException e) {}
    97 
       
    98 							byte[] buffer = new byte[1024];
       
    99 							int count = 0;
       
   100 							while((count = input.read(buffer)) != -1){
       
   101 								out.write(buffer, 0, count);
       
   102 							}
       
   103 							out.flush();
       
   104 							out.close();
       
   105 							input.closeEntry();
       
   106 						} catch (FileNotFoundException e) {
       
   107 							e.printStackTrace();
       
   108 						} catch (IOException e) {
       
   109 							e.printStackTrace();
       
   110 						}
       
   111 					}
   146 					}
   112 				}
   147 				}
       
   148 				try{
       
   149 					entry = input.getNextEntry();
       
   150 				}catch(IOException e){
       
   151 					e.printStackTrace();
       
   152 					if(conn != null) conn.disconnect();
       
   153 					return -1l;
       
   154 				}
       
   155 			}//end while(entry != null)
       
   156 
       
   157 			try {
   113 				input.close();
   158 				input.close();
   114 			}else{
   159 			} catch (IOException e) {}
   115 				Log.e("bla", "contenttype = " + contentType);
   160 		}//end if contentType == "zip"
   116 			}
   161 
   117 		} catch (IOException e) {
   162 		if(conn != null) conn.disconnect();
   118 			e.printStackTrace();
   163 
   119 		}finally{
   164 		if(checkMD5(digester))return 0l;
   120 			if(conn != null) conn.disconnect();
   165 		else return -1l;
   121 		}
   166 	}
   122 		return null;
   167 
   123 	}
   168 	//TODO proper result handling
   124 	
       
   125 	//TODO propper result handling
       
   126 	protected void onPostExecute(Long result){
   169 	protected void onPostExecute(Long result){
   127 		service.done(true);
   170 		service.done(result > -1l);
   128 	}
   171 	}
   129 	
   172 
   130 	protected void onProgressUpdate(Object...objects){
   173 	protected void onProgressUpdate(Object...objects){
   131 		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
   174 		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
   132 	}
   175 	}
   133 
   176 
       
   177 	private boolean checkMD5(MessageDigest digester){
       
   178 		if(digester != null) {
       
   179 			byte[] messageDigest = digester.digest();
       
   180 
       
   181 			try {
       
   182 				URL url = new URL(URL_WITHOUT_SUFFIX + URL_HASH_SUFFIX);
       
   183 				HttpURLConnection conn = (HttpURLConnection)url.openConnection();
       
   184 
       
   185 				byte[] buffer = new byte[1024];//size is large enough to hold the entire hash
       
   186 				BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
       
   187 				int bytesRead = bis.read(buffer);
       
   188 				if(bytesRead > -1){
       
   189 					String hash = new String(buffer, 0, bytesRead);
       
   190 					StringBuffer sb = new StringBuffer();
       
   191 					Integer tmp = 0;
       
   192 					for(int i = 0; i < messageDigest.length; i++){
       
   193 						tmp = 0xFF & messageDigest[i];
       
   194 						if(tmp < 0xF) sb.append('0');
       
   195 						sb.append(Integer.toHexString(tmp));
       
   196 					}
       
   197 					sb.append('\n');//add newline to become identical with the hash file
       
   198 					
       
   199 					return hash.equals(sb.toString());
       
   200 				}
       
   201 				return false;
       
   202 			} catch (IOException e) {
       
   203 				e.printStackTrace();
       
   204 				return false;
       
   205 			}
       
   206 		}else{
       
   207 			return false;	
       
   208 		}
       
   209 
       
   210 	}
       
   211 
   134 }
   212 }