|
1 package org.hedgewars.mobile.Downloader; |
|
2 |
|
3 import java.io.File; |
|
4 import java.io.FileNotFoundException; |
|
5 import java.io.FileOutputStream; |
|
6 import java.io.IOException; |
|
7 import java.net.HttpURLConnection; |
|
8 import java.net.URL; |
|
9 import java.util.zip.ZipEntry; |
|
10 import java.util.zip.ZipInputStream; |
|
11 |
|
12 import android.os.AsyncTask; |
|
13 import android.util.Log; |
|
14 /** |
|
15 * This is an AsyncTask which will download a zip from an URL and unzip it to a specified path |
|
16 * |
|
17 * a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip"); |
|
18 * @author Xeli |
|
19 * |
|
20 */ |
|
21 public class DownloadAsyncTask extends AsyncTask<String, Object, Long> { |
|
22 |
|
23 private DownloadService service; |
|
24 private long lastUpdateMillis = 0; |
|
25 |
|
26 public DownloadAsyncTask(DownloadService _service){ |
|
27 service = _service; |
|
28 } |
|
29 |
|
30 /** |
|
31 * |
|
32 * @param params - 2 Strings, first is the path where the unzipped files will be stored, second is the URL to download from |
|
33 */ |
|
34 protected Long doInBackground(String... params) { |
|
35 HttpURLConnection conn = null; |
|
36 try { |
|
37 String rootZipDest = params[0]; |
|
38 |
|
39 File rootDest = new File(rootZipDest); |
|
40 rootDest.mkdir(); |
|
41 |
|
42 URL url = new URL(params[1]); |
|
43 conn = (HttpURLConnection)url.openConnection(); |
|
44 String contentType = conn.getContentType(); |
|
45 |
|
46 if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips |
|
47 ZipInputStream input = new ZipInputStream(conn.getInputStream()); |
|
48 int bytesDecompressed = 0; |
|
49 final int kbytesToProcess = conn.getContentLength()/1024; |
|
50 |
|
51 service.start(kbytesToProcess); |
|
52 |
|
53 ZipEntry entry = null; |
|
54 while((entry = input.getNextEntry()) != null){ |
|
55 String fileName = entry.getName(); |
|
56 |
|
57 if(isCancelled()) break; |
|
58 else if(System.currentTimeMillis() - lastUpdateMillis > 1000){ |
|
59 lastUpdateMillis = System.currentTimeMillis(); |
|
60 publishProgress(bytesDecompressed, kbytesToProcess, fileName); |
|
61 } |
|
62 |
|
63 Log.e("bla", fileName); |
|
64 bytesDecompressed += entry.getCompressedSize(); |
|
65 |
|
66 File f = new File(rootZipDest + fileName); |
|
67 |
|
68 if(entry.isDirectory()){ |
|
69 f.mkdir(); |
|
70 }else{ |
|
71 if(f.exists()){ |
|
72 f.delete(); |
|
73 } |
|
74 |
|
75 try { |
|
76 f.createNewFile(); |
|
77 FileOutputStream out = new FileOutputStream(f); |
|
78 |
|
79 byte[] buffer = new byte[1024]; |
|
80 int count = 0; |
|
81 while((count = input.read(buffer)) != -1){ |
|
82 out.write(buffer, 0, count); |
|
83 } |
|
84 out.flush(); |
|
85 out.close(); |
|
86 input.closeEntry(); |
|
87 } catch (FileNotFoundException e) { |
|
88 e.printStackTrace(); |
|
89 } catch (IOException e) { |
|
90 e.printStackTrace(); |
|
91 } |
|
92 } |
|
93 } |
|
94 input.close(); |
|
95 }else{ |
|
96 Log.e("bla", "contenttype = " + contentType); |
|
97 } |
|
98 } catch (IOException e) { |
|
99 e.printStackTrace(); |
|
100 }finally{ |
|
101 if(conn != null) conn.disconnect(); |
|
102 } |
|
103 return null; |
|
104 } |
|
105 |
|
106 //TODO propper result handling |
|
107 protected void onPostExecute(Long result){ |
|
108 service.done(true); |
|
109 } |
|
110 |
|
111 protected void onProgressUpdate(Object...objects){ |
|
112 service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]); |
|
113 } |
|
114 |
|
115 } |