author | Xeli |
Thu, 04 Aug 2011 17:40:29 +0200 | |
branch | hedgeroid |
changeset 5477 | b420afbc20d4 |
parent 5412 | ab055114c788 |
child 5514 | 294c92eea729 |
permissions | -rw-r--r-- |
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
1 |
package org.hedgewars.mobile.Downloader; |
5397 | 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 |
||
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
39 |
File rootDest = new File(rootZipDest); |
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
40 |
rootDest.mkdir(); |
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
41 |
|
5397 | 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 |
||
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
63 |
Log.e("bla", fileName); |
5397 | 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 |
} |