project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAsyncTask.java
branchhedgeroid
changeset 6343 9df5a486f41e
parent 6047 10011f051f9c
child 6350 41b0a9955c47
equal deleted inserted replaced
6340:9dd921c0c7e7 6343:9df5a486f41e
    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 android.os.AsyncTask;
    34 import android.os.AsyncTask;
    35 import android.util.Log;
       
    36 /**
    35 /**
    37  * This is an AsyncTask which will download a zip from an URL and unzip it to a specified path
    36  * This is an AsyncTask which will download a zip from an URL and unzip it to a specified path
    38  * 
    37  * 
    39  *  a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip");
    38  *  a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip");
    40  * @author Xeli
    39  * @author Xeli
    41  *
    40  *
    42  */
    41  */
    43 public class DownloadAsyncTask extends AsyncTask<String, Object, Long> {
    42 public class DownloadAsyncTask extends AsyncTask<DownloadTask, Object, Long> {
    44 
    43 
    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.";
    44 	//private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton.";
    47 	private final static String URL_ZIP_SUFFIX = "zip";
    45 	private final static String URL_ZIP_SUFFIX = ".zip";
    48 	private final static String URL_HASH_SUFFIX = "hash";
    46 	private final static String URL_HASH_SUFFIX = ".hash";
    49 
    47 	
    50 	private DownloadService service;
    48 	private DownloadService service;
    51 	private long lastUpdateMillis = 0;
    49 	private long lastUpdateMillis = 0;
    52 
    50 
    53 	public DownloadAsyncTask(DownloadService _service){
    51 	public DownloadAsyncTask(DownloadService _service){
    54 		service = _service;
    52 		service = _service;
    55 	}
    53 	}
    56 
    54 
    57 	/**
    55 	/**
    58 	 * 
    56 	 * 
    59 	 * @param params - 2 Strings, first is the path where the unzipped files will be stored, second is the URL to download from
    57 	 * @param params - A {@link}DownloadTask which gives information about where to download from and store the files to 
    60 	 */
    58 	 */
    61 	protected Long doInBackground(String... params) {
    59 	protected Long doInBackground(DownloadTask...tasks) {
       
    60 		DownloadTask task = tasks[0];//just use one task per execute call for now
       
    61 		
    62 		HttpURLConnection conn = null;
    62 		HttpURLConnection conn = null;
    63 		MessageDigest digester = null;
    63 		MessageDigest digester = null;
    64 		String rootZipDest = params[0];
    64 		String rootZipDest = task.getPathToStore();
    65 
    65 
    66 		File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
    66 		File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
    67 		rootDest.mkdir();
    67 		rootDest.mkdir();
    68 
    68 
    69 		try {
    69 		try {
    70 			URL url = new URL(URL_WITHOUT_SUFFIX + URL_ZIP_SUFFIX);
    70 			URL url = new URL(task.getURL() + URL_ZIP_SUFFIX);
    71 			conn = (HttpURLConnection)url.openConnection();
    71 			conn = (HttpURLConnection)url.openConnection();
    72 		} catch (IOException e) {
    72 		} catch (IOException e) {
    73 			e.printStackTrace();
    73 			e.printStackTrace();
    74 			return -1l;
    74 			return -1l;
    75 		}
    75 		}
   159 			} catch (IOException e) {}
   159 			} catch (IOException e) {}
   160 		}//end if contentType == "zip"
   160 		}//end if contentType == "zip"
   161 
   161 
   162 		if(conn != null) conn.disconnect();
   162 		if(conn != null) conn.disconnect();
   163 
   163 
   164 		if(checkMD5(digester))return 0l;
   164 		if(checkMD5(digester, task))return 0l;
   165 		else return -1l;
   165 		else return -1l;
   166 	}
   166 	}
   167 
   167 
   168 	//TODO proper result handling
   168 	//TODO proper result handling
   169 	protected void onPostExecute(Long result){
   169 	protected void onPostExecute(Long result){
   172 
   172 
   173 	protected void onProgressUpdate(Object...objects){
   173 	protected void onProgressUpdate(Object...objects){
   174 		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
   174 		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
   175 	}
   175 	}
   176 
   176 
   177 	private boolean checkMD5(MessageDigest digester){
   177 	private boolean checkMD5(MessageDigest digester, DownloadTask task){
   178 		if(digester != null) {
   178 		if(digester != null) {
   179 			byte[] messageDigest = digester.digest();
   179 			byte[] messageDigest = digester.digest();
   180 
   180 
   181 			try {
   181 			try {
   182 				URL url = new URL(URL_WITHOUT_SUFFIX + URL_HASH_SUFFIX);
   182 				URL url = new URL(task.getURL() + URL_HASH_SUFFIX);
   183 				HttpURLConnection conn = (HttpURLConnection)url.openConnection();
   183 				HttpURLConnection conn = (HttpURLConnection)url.openConnection();
   184 
   184 
   185 				byte[] buffer = new byte[1024];//size is large enough to hold the entire hash
   185 				byte[] buffer = new byte[1024];//size is large enough to hold the entire hash
   186 				BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
   186 				BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
   187 				int bytesRead = bis.read(buffer);
   187 				int bytesRead = bis.read(buffer);