author | Wuzzy <almikes@aol.com> |
Sat, 30 Sep 2017 23:52:08 +0200 | |
changeset 12627 | 07fdda8c13a2 |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
1 |
/* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
6700 | 3 |
* Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
4 |
* |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6842
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6842
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6842
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6842
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
9 |
* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
13 |
* GNU General Public License for more details. |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
14 |
* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6842
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
18 |
*/ |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
19 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
20 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
21 |
package org.hedgewars.hedgeroid.Downloader; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
22 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
23 |
import java.io.BufferedInputStream; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
24 |
import java.io.File; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
25 |
import java.io.FileNotFoundException; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
26 |
import java.io.FileOutputStream; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
27 |
import java.io.IOException; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
28 |
import java.net.HttpURLConnection; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
29 |
import java.net.URL; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
30 |
import java.security.MessageDigest; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
31 |
import java.security.NoSuchAlgorithmException; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
32 |
import java.util.zip.ZipEntry; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
33 |
import java.util.zip.ZipInputStream; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
34 |
|
6350 | 35 |
import org.hedgewars.hedgeroid.Downloader.DownloadService.DownloadTask; |
36 |
||
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
37 |
import android.os.AsyncTask; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
38 |
/** |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
39 |
* This is an AsyncTask which will download a zip from an URL and unzip it to a specified path |
10017 | 40 |
* |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
41 |
* a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip"); |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
42 |
* @author Xeli |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
43 |
* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
44 |
*/ |
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
45 |
public class DownloadAsyncTask extends AsyncTask<DownloadPackage, Object, Integer> { |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
46 |
|
10017 | 47 |
//private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton."; |
48 |
private final static String URL_ZIP_SUFFIX = ".zip"; |
|
49 |
private final static String URL_HASH_SUFFIX = ".hash"; |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
50 |
|
10017 | 51 |
public static final int EXIT_SUCCESS = 0; |
52 |
public static final int EXIT_URLFAIL = 1; |
|
53 |
public static final int EXIT_CONNERROR = 2; |
|
54 |
public static final int EXIT_FNF = 3; |
|
55 |
public static final int EXIT_MD5 = 4; |
|
56 |
public static final int EXIT_CANCELLED = 5; |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
57 |
|
10017 | 58 |
private DownloadTask task; |
59 |
private long lastUpdateMillis = 0; |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
60 |
|
10017 | 61 |
public DownloadAsyncTask(DownloadTask _task){ |
62 |
task = _task; |
|
63 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
64 |
|
10017 | 65 |
/** |
66 |
* |
|
67 |
* @param params - A {@link}DownloadTask which gives information about where to download from and store the files to |
|
68 |
*/ |
|
69 |
protected Integer doInBackground(DownloadPackage...packages) { |
|
70 |
DownloadPackage pack = packages[0];//just use one task per execute call for now |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
71 |
|
10017 | 72 |
HttpURLConnection conn = null; |
73 |
MessageDigest digester = null; |
|
74 |
String rootZipDest = pack.getPathToStore(); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
75 |
|
10017 | 76 |
File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard |
77 |
rootDest.mkdirs(); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
78 |
|
10017 | 79 |
try { |
80 |
URL url = new URL(pack.getURL() + URL_ZIP_SUFFIX); |
|
81 |
conn = (HttpURLConnection)url.openConnection(); |
|
82 |
} catch (IOException e) { |
|
83 |
e.printStackTrace(); |
|
84 |
return EXIT_URLFAIL; |
|
85 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
86 |
|
10017 | 87 |
String contentType = conn.getContentType(); |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
88 |
|
10017 | 89 |
if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips |
90 |
int bytesDecompressed = 0; |
|
91 |
ZipEntry entry = null; |
|
92 |
ZipInputStream input = null; |
|
93 |
FileOutputStream output = null; |
|
94 |
int kbytesToProcess = conn.getContentLength()/1024; |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
95 |
|
10017 | 96 |
byte[] buffer = new byte[1024]; |
97 |
task.start(kbytesToProcess); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
98 |
|
10017 | 99 |
try { |
100 |
digester = MessageDigest.getInstance("MD5"); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
101 |
|
10017 | 102 |
} catch (NoSuchAlgorithmException e1) { |
103 |
e1.printStackTrace(); |
|
104 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
105 |
|
10017 | 106 |
try{ |
107 |
input = new ZipInputStream(conn.getInputStream()); |
|
108 |
entry = input.getNextEntry(); |
|
109 |
}catch(IOException e){ |
|
110 |
e.printStackTrace(); |
|
111 |
conn.disconnect(); |
|
112 |
return EXIT_CONNERROR; |
|
113 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
114 |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
115 |
|
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
116 |
|
10017 | 117 |
while(entry != null){ |
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
118 |
|
10017 | 119 |
if(isCancelled()) break; |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
120 |
|
10017 | 121 |
try { |
122 |
String fileName = entry.getName(); |
|
123 |
File f = new File(rootZipDest + fileName); |
|
124 |
bytesDecompressed += entry.getCompressedSize(); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
125 |
|
10017 | 126 |
if(entry.isDirectory()){ |
127 |
f.mkdir(); |
|
128 |
}else{ |
|
129 |
if(f.exists()){ |
|
130 |
f.delete(); |
|
131 |
} |
|
132 |
f.createNewFile(); |
|
133 |
output = new FileOutputStream(f); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
134 |
|
10017 | 135 |
int count = 0; |
136 |
while((count = input.read(buffer)) != -1){ |
|
137 |
output.write(buffer, 0, count); |
|
138 |
digester.update(buffer, 0, count); |
|
139 |
if(System.currentTimeMillis() - lastUpdateMillis > 1000){ |
|
140 |
lastUpdateMillis = System.currentTimeMillis(); |
|
141 |
publishProgress(bytesDecompressed, kbytesToProcess, fileName); |
|
142 |
} |
|
143 |
} |
|
144 |
output.flush(); |
|
145 |
input.closeEntry(); |
|
146 |
}//if isDir |
|
147 |
entry = input.getNextEntry(); |
|
148 |
} catch (FileNotFoundException e) { |
|
149 |
e.printStackTrace(); |
|
150 |
if(conn != null) conn.disconnect(); |
|
151 |
return EXIT_FNF; |
|
152 |
} catch (IOException e) { |
|
153 |
e.printStackTrace(); |
|
154 |
if(conn != null) conn.disconnect(); |
|
155 |
return EXIT_CONNERROR; |
|
156 |
}finally{ |
|
157 |
try { |
|
158 |
if( output != null) output.close(); |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
159 |
|
10017 | 160 |
} catch (IOException e) {} |
161 |
} |
|
162 |
}//end while(entry != null) |
|
163 |
if( input != null) |
|
164 |
try { |
|
165 |
input.close(); |
|
166 |
} catch (IOException e) {} |
|
167 |
}else{//end if contentType == "zip" |
|
168 |
return EXIT_URLFAIL; |
|
169 |
} |
|
170 |
if(conn != null) conn.disconnect(); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
171 |
|
10017 | 172 |
if(checkMD5(digester, pack))return EXIT_SUCCESS; |
173 |
else return EXIT_MD5; |
|
174 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
175 |
|
10017 | 176 |
//TODO proper result handling |
177 |
protected void onPostExecute(Integer result){ |
|
178 |
task.done(result); |
|
179 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
180 |
|
10017 | 181 |
protected void onProgressUpdate(Object...objects){ |
182 |
task.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]); |
|
183 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
184 |
|
10017 | 185 |
protected void onCancelled(){ |
186 |
onPostExecute(EXIT_CANCELLED); |
|
187 |
} |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
188 |
|
10017 | 189 |
private boolean checkMD5(MessageDigest digester, DownloadPackage task){ |
190 |
if(digester != null) { |
|
191 |
byte[] messageDigest = digester.digest(); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
192 |
|
10017 | 193 |
try { |
194 |
URL url = new URL(task.getURL() + URL_HASH_SUFFIX); |
|
195 |
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
196 |
|
10017 | 197 |
byte[] buffer = new byte[1024];//size is large enough to hold the entire hash |
198 |
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); |
|
199 |
int bytesRead = bis.read(buffer); |
|
200 |
String hash = null; |
|
201 |
if(bytesRead > -1){ |
|
202 |
hash = new String(buffer, 0, bytesRead); |
|
203 |
} |
|
204 |
StringBuffer sb = new StringBuffer(); |
|
205 |
Integer tmp = 0; |
|
206 |
for(int i = 0; i < messageDigest.length; i++){ |
|
207 |
tmp = 0xFF & messageDigest[i]; |
|
208 |
if(tmp < 0xF) sb.append('0'); |
|
209 |
sb.append(Integer.toHexString(tmp)); |
|
210 |
} |
|
211 |
sb.append('\n');//add newline to become identical with the hash file |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6350
diff
changeset
|
212 |
|
10017 | 213 |
return hash.equals(sb.toString()); |
214 |
} catch (IOException e) { |
|
215 |
e.printStackTrace(); |
|
216 |
return true; |
|
217 |
} |
|
218 |
}else{ |
|
219 |
return true; |
|
220 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
221 |
|
10017 | 222 |
} |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
223 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
224 |
} |