Revision cc5ec229
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikScoresDownloader.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.magic; |
21 | 21 |
|
22 |
import android.content.res.Resources; |
|
23 |
|
|
22 | 24 |
import java.io.InputStream; |
23 | 25 |
import java.net.HttpURLConnection; |
24 | 26 |
import java.net.URL; |
27 |
import java.net.UnknownHostException; |
|
25 | 28 |
|
26 | 29 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
27 | 30 |
|
... | ... | |
29 | 32 |
{ |
30 | 33 |
interface Receiver |
31 | 34 |
{ |
32 |
void receive(String[][][][] scores);
|
|
35 |
void receive(int[][][] country, String[][][] name, String[][][] time);
|
|
33 | 36 |
void exception(String exception); |
34 | 37 |
} |
35 | 38 |
|
... | ... | |
79 | 82 |
private static boolean mRunning = false; |
80 | 83 |
private static int mMode = IDLE; |
81 | 84 |
private static Receiver mReceiver; |
85 |
private static Resources mResources; |
|
86 |
private static String mPackageName; |
|
82 | 87 |
|
83 | 88 |
private static String mScores = ""; |
84 |
private static String[][][][] mValues = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES][3]; |
|
89 |
private static int[][][] mCountry = new int [RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES]; |
|
90 |
private static String[][][] mName = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES]; |
|
91 |
private static String[][][] mTime = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES]; |
|
85 | 92 |
|
86 | 93 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
87 | 94 |
|
... | ... | |
124 | 131 |
|
125 | 132 |
if(level>=0 && level<RubikActivity.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES) |
126 | 133 |
{ |
127 |
mValues[size][level][place][0] = country; |
|
128 |
mValues[size][level][place][1] = name; |
|
129 |
mValues[size][level][place][2] = realTime; |
|
134 |
int resID = mResources.getIdentifier( country, "drawable", mPackageName); |
|
135 |
mCountry[size][level][place] = resID!=0 ? resID:R.drawable.unk; |
|
136 |
mName[size][level][place] = name; |
|
137 |
mTime[size][level][place] = realTime; |
|
130 | 138 |
} |
131 | 139 |
} |
132 | 140 |
} |
... | ... | |
201 | 209 |
} |
202 | 210 |
mScores = sb.toString(); |
203 | 211 |
} |
204 |
catch(Exception e) |
|
212 |
catch( final Exception e)
|
|
205 | 213 |
{ |
206 | 214 |
mReceiver.exception("biffed it getting HTTPResponse"); |
207 | 215 |
return false; |
208 | 216 |
} |
209 | 217 |
} |
210 |
catch( Exception e ) |
|
218 |
catch( final UnknownHostException e ) |
|
219 |
{ |
|
220 |
mReceiver.exception("No access to Internet"); |
|
221 |
return false; |
|
222 |
} |
|
223 |
catch( final SecurityException e ) |
|
224 |
{ |
|
225 |
mReceiver.exception("Application not authorized to connect to the Internet"); |
|
226 |
return false; |
|
227 |
} |
|
228 |
catch( final Exception e ) |
|
211 | 229 |
{ |
212 | 230 |
mReceiver.exception(e.getMessage()); |
213 | 231 |
return false; |
... | ... | |
228 | 246 |
static void onPause() |
229 | 247 |
{ |
230 | 248 |
mRunning = false; |
231 |
mScores = ""; |
|
232 | 249 |
} |
233 | 250 |
|
234 | 251 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
235 | 252 |
|
236 |
void download(Receiver receiver) |
|
253 |
void download(Receiver receiver, Resources resources, String packageName)
|
|
237 | 254 |
{ |
238 | 255 |
mReceiver = receiver; |
256 |
mResources= resources; |
|
257 |
mPackageName = packageName; |
|
239 | 258 |
mMode = DOWNLOAD; |
259 |
|
|
240 | 260 |
Thread networkThrd = new Thread(this); |
241 | 261 |
networkThrd.start(); |
242 | 262 |
} |
... | ... | |
258 | 278 |
} |
259 | 279 |
catch( Exception e ) |
260 | 280 |
{ |
261 |
android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
|
|
281 |
mReceiver.exception("Exception downloading records: "+e.getMessage() );
|
|
262 | 282 |
} |
263 | 283 |
|
264 | 284 |
mRunning = false; |
265 | 285 |
|
266 | 286 |
if( success ) |
267 | 287 |
{ |
268 |
mReceiver.receive(mValues);
|
|
288 |
mReceiver.receive(mCountry, mName, mTime);
|
|
269 | 289 |
} |
270 | 290 |
} |
271 | 291 |
} |
Also available in: Unified diff
Downloading High Scores: actually downloading scores!