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) { |
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); |