project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAsyncTask.java
branchhedgeroid
changeset 6350 41b0a9955c47
parent 6343 9df5a486f41e
child 6437 4ed58839b13b
equal deleted inserted replaced
6348:162fec525764 6350:41b0a9955c47
    29 import java.security.MessageDigest;
    29 import java.security.MessageDigest;
    30 import java.security.NoSuchAlgorithmException;
    30 import java.security.NoSuchAlgorithmException;
    31 import java.util.zip.ZipEntry;
    31 import java.util.zip.ZipEntry;
    32 import java.util.zip.ZipInputStream;
    32 import java.util.zip.ZipInputStream;
    33 
    33 
       
    34 import org.hedgewars.hedgeroid.Downloader.DownloadService.DownloadTask;
       
    35 
    34 import android.os.AsyncTask;
    36 import android.os.AsyncTask;
    35 /**
    37 /**
    36  * This is an AsyncTask which will download a zip from an URL and unzip it to a specified path
    38  * This is an AsyncTask which will download a zip from an URL and unzip it to a specified path
    37  * 
    39  * 
    38  *  a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip");
    40  *  a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip");
    39  * @author Xeli
    41  * @author Xeli
    40  *
    42  *
    41  */
    43  */
    42 public class DownloadAsyncTask extends AsyncTask<DownloadTask, Object, Long> {
    44 public class DownloadAsyncTask extends AsyncTask<DownloadPackage, Object, Long> {
    43 
    45 
    44 	//private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton.";
    46 	//private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton.";
    45 	private final static String URL_ZIP_SUFFIX = ".zip";
    47 	private final static String URL_ZIP_SUFFIX = ".zip";
    46 	private final static String URL_HASH_SUFFIX = ".hash";
    48 	private final static String URL_HASH_SUFFIX = ".hash";
    47 	
    49 	
    48 	private DownloadService service;
    50 	private DownloadTask task;
    49 	private long lastUpdateMillis = 0;
    51 	private long lastUpdateMillis = 0;
    50 
    52 
    51 	public DownloadAsyncTask(DownloadService _service){
    53 	public DownloadAsyncTask(DownloadTask _task){
    52 		service = _service;
    54 		task = _task;
    53 	}
    55 	}
    54 
    56 
    55 	/**
    57 	/**
    56 	 * 
    58 	 * 
    57 	 * @param params - A {@link}DownloadTask which gives information about where to download from and store the files to 
    59 	 * @param params - A {@link}DownloadTask which gives information about where to download from and store the files to 
    58 	 */
    60 	 */
    59 	protected Long doInBackground(DownloadTask...tasks) {
    61 	protected Long doInBackground(DownloadPackage...packages) {
    60 		DownloadTask task = tasks[0];//just use one task per execute call for now
    62 		DownloadPackage pack = packages[0];//just use one task per execute call for now
    61 		
    63 		
    62 		HttpURLConnection conn = null;
    64 		HttpURLConnection conn = null;
    63 		MessageDigest digester = null;
    65 		MessageDigest digester = null;
    64 		String rootZipDest = task.getPathToStore();
    66 		String rootZipDest = pack.getPathToStore();
    65 
    67 
    66 		File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
    68 		File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
    67 		rootDest.mkdir();
    69 		rootDest.mkdir();
    68 
    70 
    69 		try {
    71 		try {
    70 			URL url = new URL(task.getURL() + URL_ZIP_SUFFIX);
    72 			URL url = new URL(pack.getURL() + URL_ZIP_SUFFIX);
    71 			conn = (HttpURLConnection)url.openConnection();
    73 			conn = (HttpURLConnection)url.openConnection();
    72 		} catch (IOException e) {
    74 		} catch (IOException e) {
    73 			e.printStackTrace();
    75 			e.printStackTrace();
    74 			return -1l;
    76 			return -1l;
    75 		}
    77 		}
    81 			ZipEntry entry = null;
    83 			ZipEntry entry = null;
    82 			ZipInputStream input = null;
    84 			ZipInputStream input = null;
    83 			int kbytesToProcess = conn.getContentLength()/1024;
    85 			int kbytesToProcess = conn.getContentLength()/1024;
    84 
    86 
    85 			byte[] buffer = new byte[1024];
    87 			byte[] buffer = new byte[1024];
    86 			service.start(kbytesToProcess);
    88 			task.start(kbytesToProcess);
    87 
    89 
    88 			try {
    90 			try {
    89 				digester = MessageDigest.getInstance("MD5");
    91 				digester = MessageDigest.getInstance("MD5");
    90 
    92 
    91 			} catch (NoSuchAlgorithmException e1) {
    93 			} catch (NoSuchAlgorithmException e1) {
    96 				input = new ZipInputStream(conn.getInputStream());
    98 				input = new ZipInputStream(conn.getInputStream());
    97 				entry = input.getNextEntry();	
    99 				entry = input.getNextEntry();	
    98 			}catch(IOException e){
   100 			}catch(IOException e){
    99 				e.printStackTrace();
   101 				e.printStackTrace();
   100 				if(conn != null) conn.disconnect();
   102 				if(conn != null) conn.disconnect();
   101 				return -1l;
   103 				return -2l;
   102 			}
   104 			}
   103 
   105 
   104 			while(entry != null){
   106 			while(entry != null){
   105 				if(isCancelled()) break;
   107 				if(isCancelled()) break;
   106 
   108 
   132 						output.flush();
   134 						output.flush();
   133 						input.closeEntry();
   135 						input.closeEntry();
   134 					} catch (FileNotFoundException e) {
   136 					} catch (FileNotFoundException e) {
   135 						e.printStackTrace();
   137 						e.printStackTrace();
   136 						if(conn != null) conn.disconnect();
   138 						if(conn != null) conn.disconnect();
   137 						return -1l;
   139 						return -3l;
   138 					} catch (IOException e) {
   140 					} catch (IOException e) {
   139 						e.printStackTrace();
   141 						e.printStackTrace();
   140 						if(conn != null) conn.disconnect();
   142 						if(conn != null) conn.disconnect();
   141 						return -1l;
   143 						return -4l;
   142 					}finally{
   144 					}finally{
   143 						try {
   145 						try {
   144 							if( output != null) output.close();
   146 							if( output != null) output.close();
   145 						} catch (IOException e) {}
   147 						} catch (IOException e) {}
   146 					}
   148 					}
   159 			} catch (IOException e) {}
   161 			} catch (IOException e) {}
   160 		}//end if contentType == "zip"
   162 		}//end if contentType == "zip"
   161 
   163 
   162 		if(conn != null) conn.disconnect();
   164 		if(conn != null) conn.disconnect();
   163 
   165 
   164 		if(checkMD5(digester, task))return 0l;
   166 		if(checkMD5(digester, pack))return 0l;
   165 		else return -1l;
   167 		else return -1l;
   166 	}
   168 	}
   167 
   169 
   168 	//TODO proper result handling
   170 	//TODO proper result handling
   169 	protected void onPostExecute(Long result){
   171 	protected void onPostExecute(Long result){
   170 		service.done(result > -1l);
   172 		task.done(result > -1l);
   171 	}
   173 	}
   172 
   174 
   173 	protected void onProgressUpdate(Object...objects){
   175 	protected void onProgressUpdate(Object...objects){
   174 		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
   176 		task.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
   175 	}
   177 	}
   176 
   178 
   177 	private boolean checkMD5(MessageDigest digester, DownloadTask task){
   179 	private boolean checkMD5(MessageDigest digester, DownloadPackage task){
   178 		if(digester != null) {
   180 		if(digester != null) {
   179 			byte[] messageDigest = digester.digest();
   181 			byte[] messageDigest = digester.digest();
   180 
   182 
   181 			try {
   183 			try {
   182 				URL url = new URL(task.getURL() + URL_HASH_SUFFIX);
   184 				URL url = new URL(task.getURL() + URL_HASH_SUFFIX);