author | Xeli |
Sun, 21 Aug 2011 16:25:18 +0200 | |
branch | hedgeroid |
changeset 5621 | ea796c83ea47 |
parent 5514 | 294c92eea729 |
child 5671 | ba4c3a4c8b09 |
permissions | -rw-r--r-- |
5621 | 1 |
/* |
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
||
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
20 |
package org.hedgewars.mobile.Downloader; |
5397 | 21 |
|
22 |
import java.io.File; |
|
23 |
import java.io.FileNotFoundException; |
|
24 |
import java.io.FileOutputStream; |
|
25 |
import java.io.IOException; |
|
26 |
import java.net.HttpURLConnection; |
|
27 |
import java.net.URL; |
|
28 |
import java.util.zip.ZipEntry; |
|
29 |
import java.util.zip.ZipInputStream; |
|
30 |
||
31 |
import android.os.AsyncTask; |
|
32 |
import android.util.Log; |
|
33 |
/** |
|
34 |
* This is an AsyncTask which will download a zip from an URL and unzip it to a specified path |
|
35 |
* |
|
36 |
* a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip"); |
|
37 |
* @author Xeli |
|
38 |
* |
|
39 |
*/ |
|
40 |
public class DownloadAsyncTask extends AsyncTask<String, Object, Long> { |
|
41 |
||
42 |
private DownloadService service; |
|
43 |
private long lastUpdateMillis = 0; |
|
44 |
||
45 |
public DownloadAsyncTask(DownloadService _service){ |
|
46 |
service = _service; |
|
47 |
} |
|
48 |
||
49 |
/** |
|
50 |
* |
|
51 |
* @param params - 2 Strings, first is the path where the unzipped files will be stored, second is the URL to download from |
|
52 |
*/ |
|
53 |
protected Long doInBackground(String... params) { |
|
54 |
HttpURLConnection conn = null; |
|
55 |
try { |
|
56 |
String rootZipDest = params[0]; |
|
57 |
||
5514 | 58 |
File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard |
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
59 |
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
|
60 |
|
5397 | 61 |
URL url = new URL(params[1]); |
62 |
conn = (HttpURLConnection)url.openConnection(); |
|
63 |
String contentType = conn.getContentType(); |
|
64 |
||
65 |
if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips |
|
66 |
ZipInputStream input = new ZipInputStream(conn.getInputStream()); |
|
67 |
int bytesDecompressed = 0; |
|
68 |
final int kbytesToProcess = conn.getContentLength()/1024; |
|
69 |
||
70 |
service.start(kbytesToProcess); |
|
71 |
||
72 |
ZipEntry entry = null; |
|
73 |
while((entry = input.getNextEntry()) != null){ |
|
74 |
String fileName = entry.getName(); |
|
75 |
||
76 |
if(isCancelled()) break; |
|
77 |
else if(System.currentTimeMillis() - lastUpdateMillis > 1000){ |
|
78 |
lastUpdateMillis = System.currentTimeMillis(); |
|
79 |
publishProgress(bytesDecompressed, kbytesToProcess, fileName); |
|
80 |
} |
|
81 |
||
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
82 |
Log.e("bla", fileName); |
5397 | 83 |
bytesDecompressed += entry.getCompressedSize(); |
84 |
||
85 |
File f = new File(rootZipDest + fileName); |
|
86 |
||
87 |
if(entry.isDirectory()){ |
|
88 |
f.mkdir(); |
|
89 |
}else{ |
|
90 |
if(f.exists()){ |
|
91 |
f.delete(); |
|
92 |
} |
|
93 |
||
94 |
try { |
|
95 |
f.createNewFile(); |
|
96 |
FileOutputStream out = new FileOutputStream(f); |
|
97 |
||
98 |
byte[] buffer = new byte[1024]; |
|
99 |
int count = 0; |
|
100 |
while((count = input.read(buffer)) != -1){ |
|
101 |
out.write(buffer, 0, count); |
|
102 |
} |
|
103 |
out.flush(); |
|
104 |
out.close(); |
|
105 |
input.closeEntry(); |
|
106 |
} catch (FileNotFoundException e) { |
|
107 |
e.printStackTrace(); |
|
108 |
} catch (IOException e) { |
|
109 |
e.printStackTrace(); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
input.close(); |
|
114 |
}else{ |
|
115 |
Log.e("bla", "contenttype = " + contentType); |
|
116 |
} |
|
117 |
} catch (IOException e) { |
|
118 |
e.printStackTrace(); |
|
119 |
}finally{ |
|
120 |
if(conn != null) conn.disconnect(); |
|
121 |
} |
|
122 |
return null; |
|
123 |
} |
|
124 |
||
125 |
//TODO propper result handling |
|
126 |
protected void onPostExecute(Long result){ |
|
127 |
service.done(true); |
|
128 |
} |
|
129 |
||
130 |
protected void onProgressUpdate(Object...objects){ |
|
131 |
service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]); |
|
132 |
} |
|
133 |
||
134 |
} |