|
1 package org.hedgewars.mobile; |
|
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 URL url = new URL(params[1]); |
|
40 conn = (HttpURLConnection)url.openConnection(); |
|
41 String contentType = conn.getContentType(); |
|
42 |
|
43 if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips |
|
44 ZipInputStream input = new ZipInputStream(conn.getInputStream()); |
|
45 int bytesDecompressed = 0; |
|
46 final int kbytesToProcess = conn.getContentLength()/1024; |
|
47 |
|
48 service.start(kbytesToProcess); |
|
49 |
|
50 ZipEntry entry = null; |
|
51 while((entry = input.getNextEntry()) != null){ |
|
52 String fileName = entry.getName(); |
|
53 |
|
54 if(isCancelled()) break; |
|
55 else if(System.currentTimeMillis() - lastUpdateMillis > 1000){ |
|
56 lastUpdateMillis = System.currentTimeMillis(); |
|
57 publishProgress(bytesDecompressed, kbytesToProcess, fileName); |
|
58 } |
|
59 |
|
60 bytesDecompressed += entry.getCompressedSize(); |
|
61 |
|
62 File f = new File(rootZipDest + fileName); |
|
63 |
|
64 if(entry.isDirectory()){ |
|
65 f.mkdir(); |
|
66 }else{ |
|
67 if(f.exists()){ |
|
68 f.delete(); |
|
69 } |
|
70 |
|
71 try { |
|
72 f.createNewFile(); |
|
73 FileOutputStream out = new FileOutputStream(f); |
|
74 |
|
75 byte[] buffer = new byte[1024]; |
|
76 int count = 0; |
|
77 while((count = input.read(buffer)) != -1){ |
|
78 out.write(buffer, 0, count); |
|
79 } |
|
80 out.flush(); |
|
81 out.close(); |
|
82 input.closeEntry(); |
|
83 } catch (FileNotFoundException e) { |
|
84 e.printStackTrace(); |
|
85 } catch (IOException e) { |
|
86 e.printStackTrace(); |
|
87 } |
|
88 } |
|
89 } |
|
90 input.close(); |
|
91 }else{ |
|
92 Log.e("bla", "contenttype = " + contentType); |
|
93 } |
|
94 } catch (IOException e) { |
|
95 e.printStackTrace(); |
|
96 }finally{ |
|
97 if(conn != null) conn.disconnect(); |
|
98 } |
|
99 return null; |
|
100 } |
|
101 |
|
102 //TODO propper result handling |
|
103 protected void onPostExecute(Long result){ |
|
104 service.done(true); |
|
105 } |
|
106 |
|
107 protected void onProgressUpdate(Object...objects){ |
|
108 service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]); |
|
109 } |
|
110 |
|
111 } |