1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.external;
|
21
|
|
22
|
import java.io.BufferedReader;
|
23
|
import java.io.IOException;
|
24
|
import java.io.InputStream;
|
25
|
import java.io.InputStreamReader;
|
26
|
import java.net.HttpURLConnection;
|
27
|
import java.net.URL;
|
28
|
import java.net.UnknownHostException;
|
29
|
import java.security.MessageDigest;
|
30
|
import java.security.NoSuchAlgorithmException;
|
31
|
|
32
|
import android.app.Activity;
|
33
|
import android.content.Context;
|
34
|
import android.content.pm.PackageInfo;
|
35
|
import android.content.pm.PackageManager;
|
36
|
import android.graphics.Bitmap;
|
37
|
import android.graphics.BitmapFactory;
|
38
|
|
39
|
import org.distorted.library.main.DistortedLibrary;
|
40
|
import org.distorted.objectlib.json.JsonWriter;
|
41
|
import org.distorted.objects.RubikObjectList;
|
42
|
|
43
|
import static org.distorted.objects.RubikObjectList.MAX_LEVEL;
|
44
|
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
public class RubikNetwork
|
49
|
{
|
50
|
public interface ScoresReceiver
|
51
|
{
|
52
|
void receive(String[][][] country, String[][][] name, float[][][] time);
|
53
|
void message(String mess);
|
54
|
void error(String error);
|
55
|
}
|
56
|
|
57
|
public interface IconReceiver
|
58
|
{
|
59
|
void iconDownloaded(int ordinal, Bitmap bitmap, boolean downloaded);
|
60
|
}
|
61
|
|
62
|
public interface Updatee
|
63
|
{
|
64
|
void receiveUpdate(RubikUpdates update);
|
65
|
void errorUpdate();
|
66
|
}
|
67
|
|
68
|
public interface Downloadee
|
69
|
{
|
70
|
void jsonDownloaded();
|
71
|
}
|
72
|
|
73
|
public static final int MAX_PLACES = 10;
|
74
|
|
75
|
private static final int REND_ADRENO= 0;
|
76
|
private static final int REND_MALI = 1;
|
77
|
private static final int REND_POWER = 2;
|
78
|
private static final int REND_OTHER = 3;
|
79
|
|
80
|
private static final int DEBUG_RUNNING = 1;
|
81
|
private static final int DEBUG_SUCCESS = 2;
|
82
|
private static final int DEBUG_FAILURE = 3;
|
83
|
|
84
|
private final String[] hex = {
|
85
|
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
|
86
|
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
|
87
|
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
|
88
|
"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
|
89
|
"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
|
90
|
"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
|
91
|
"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
|
92
|
"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
|
93
|
"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
|
94
|
"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
|
95
|
"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
|
96
|
"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
|
97
|
"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
|
98
|
"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
|
99
|
"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
|
100
|
"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
|
101
|
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
|
102
|
"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
|
103
|
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
|
104
|
"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
|
105
|
"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
|
106
|
"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
|
107
|
"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
|
108
|
"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
|
109
|
"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
|
110
|
"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
|
111
|
"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
|
112
|
"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
|
113
|
"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
|
114
|
"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
|
115
|
"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
|
116
|
"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
|
117
|
};
|
118
|
|
119
|
private static String[][][] mCountry;
|
120
|
private static String[][][] mName;
|
121
|
private static float[][][] mTime;
|
122
|
private static int[][] mPlaces;
|
123
|
|
124
|
private static RubikNetwork mThis;
|
125
|
private static String mScores = "";
|
126
|
private static boolean mRunning = false;
|
127
|
private static Updatee mUpdatee;
|
128
|
private static String mVersion;
|
129
|
private static int mNumObjects;
|
130
|
private static RubikUpdates mUpdates;
|
131
|
private static int mDebugState;
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
private static void initializeStatics()
|
136
|
{
|
137
|
int newNum = RubikObjectList.getNumObjects();
|
138
|
|
139
|
if( mCountry==null || newNum!=mNumObjects ) mCountry = new String[newNum][MAX_LEVEL][MAX_PLACES];
|
140
|
if( mName==null || newNum!=mNumObjects ) mName = new String[newNum][MAX_LEVEL][MAX_PLACES];
|
141
|
if( mTime==null || newNum!=mNumObjects ) mTime = new float[newNum][MAX_LEVEL][MAX_PLACES];
|
142
|
if( mPlaces==null || newNum!=mNumObjects ) mPlaces = new int[newNum][MAX_LEVEL];
|
143
|
|
144
|
if( mUpdates==null ) mUpdates = new RubikUpdates();
|
145
|
|
146
|
mNumObjects = newNum;
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
private static String computeHash(String stringToHash, byte[] salt)
|
152
|
{
|
153
|
String generatedPassword;
|
154
|
|
155
|
try
|
156
|
{
|
157
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
158
|
md.update(salt);
|
159
|
byte[] bytes = md.digest(stringToHash.getBytes());
|
160
|
StringBuilder sb = new StringBuilder();
|
161
|
|
162
|
for (byte aByte : bytes)
|
163
|
{
|
164
|
sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
|
165
|
}
|
166
|
|
167
|
generatedPassword = sb.toString();
|
168
|
}
|
169
|
catch (NoSuchAlgorithmException e)
|
170
|
{
|
171
|
return "NoSuchAlgorithm";
|
172
|
}
|
173
|
|
174
|
return generatedPassword;
|
175
|
}
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
private boolean fillValuesNormal(ScoresReceiver receiver)
|
180
|
{
|
181
|
int begin=-1 ,end, len = mScores.length();
|
182
|
String row;
|
183
|
|
184
|
if( len==0 )
|
185
|
{
|
186
|
receiver.error("1");
|
187
|
return false;
|
188
|
}
|
189
|
else if( len<=2 )
|
190
|
{
|
191
|
receiver.error(mScores);
|
192
|
return false;
|
193
|
}
|
194
|
|
195
|
for(int i=0; i<mNumObjects; i++)
|
196
|
for(int j=0; j<MAX_LEVEL; j++)
|
197
|
{
|
198
|
mPlaces[i][j] = 0;
|
199
|
}
|
200
|
|
201
|
while( begin<len )
|
202
|
{
|
203
|
end = mScores.indexOf('\n', begin+1);
|
204
|
if( end<0 ) end = len;
|
205
|
|
206
|
try
|
207
|
{
|
208
|
row = mScores.substring(begin+1,end);
|
209
|
fillRow(row);
|
210
|
}
|
211
|
catch(Exception ex)
|
212
|
{
|
213
|
// faulty row - ignore
|
214
|
}
|
215
|
|
216
|
begin = end;
|
217
|
}
|
218
|
|
219
|
return true;
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
private void fillRow(String row)
|
225
|
{
|
226
|
int s1 = row.indexOf(' ');
|
227
|
int s2 = row.indexOf(' ',s1+1);
|
228
|
int s3 = row.indexOf(' ',s2+1);
|
229
|
int s4 = row.indexOf(' ',s3+1);
|
230
|
int s5 = row.length();
|
231
|
|
232
|
if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
|
233
|
{
|
234
|
int object = RubikObjectList.getOrdinal( row.substring(0,s1) );
|
235
|
|
236
|
if( object>=0 && object<mNumObjects )
|
237
|
{
|
238
|
int level = Integer.parseInt( row.substring(s1+1,s2) );
|
239
|
String name = row.substring(s2+1, s3);
|
240
|
int time = Integer.parseInt( row.substring(s3+1,s4) );
|
241
|
String country = row.substring(s4+1, s5);
|
242
|
|
243
|
if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
|
244
|
|
245
|
if(level>=0 && level<MAX_LEVEL)
|
246
|
{
|
247
|
int p = mPlaces[object][level];
|
248
|
mPlaces[object][level]++;
|
249
|
|
250
|
mCountry[object][level][p] = country;
|
251
|
mName [object][level][p] = name;
|
252
|
mTime [object][level][p] = ((float)(time/10))/100.0f;
|
253
|
}
|
254
|
}
|
255
|
}
|
256
|
else
|
257
|
{
|
258
|
tryDoCommand(row);
|
259
|
}
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
|
264
|
private void tryDoCommand(String row)
|
265
|
{
|
266
|
if( row.startsWith("comm") )
|
267
|
{
|
268
|
int colon = row.indexOf(':');
|
269
|
|
270
|
if( colon>0 )
|
271
|
{
|
272
|
String commandNumber = row.substring(4,colon);
|
273
|
int number;
|
274
|
|
275
|
try
|
276
|
{
|
277
|
number = Integer.parseInt(commandNumber);
|
278
|
}
|
279
|
catch(NumberFormatException ex)
|
280
|
{
|
281
|
number=0;
|
282
|
}
|
283
|
|
284
|
if(number==1)
|
285
|
{
|
286
|
String country = row.substring(colon+1);
|
287
|
RubikScores scores = RubikScores.getInstance();
|
288
|
scores.setCountry(country);
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
private int getRendererType(String renderer)
|
297
|
{
|
298
|
if( renderer.contains("Adreno") ) return REND_ADRENO;
|
299
|
if( renderer.contains("Mali") ) return REND_MALI;
|
300
|
if( renderer.contains("PowerVR") ) return REND_POWER;
|
301
|
|
302
|
return REND_OTHER;
|
303
|
}
|
304
|
|
305
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306
|
|
307
|
private String parseRenderer(final int type, String renderer)
|
308
|
{
|
309
|
if( type==REND_ADRENO || type==REND_POWER )
|
310
|
{
|
311
|
int lastSpace = renderer.lastIndexOf(' ');
|
312
|
String ret = renderer.substring(lastSpace+1);
|
313
|
return URLencode(ret);
|
314
|
}
|
315
|
|
316
|
if( type==REND_MALI )
|
317
|
{
|
318
|
int firstHyphen = renderer.indexOf('-');
|
319
|
String ret = renderer.substring(firstHyphen+1);
|
320
|
return URLencode(ret);
|
321
|
}
|
322
|
|
323
|
return "other";
|
324
|
}
|
325
|
|
326
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
327
|
|
328
|
private String parseVersion(final int type, String version)
|
329
|
{
|
330
|
switch(type)
|
331
|
{
|
332
|
case REND_ADRENO: int aMonkey = version.indexOf('@');
|
333
|
int aDot = version.indexOf('.', aMonkey);
|
334
|
String ret1 = aDot>=3 ? version.substring(aDot-3,aDot) : "";
|
335
|
return URLencode(ret1);
|
336
|
case REND_MALI : int mV1 = version.indexOf("v1");
|
337
|
int mHyphen = version.indexOf('-', mV1);
|
338
|
String ret2 = mHyphen>mV1+3 && mV1>=0 ? version.substring(mV1+3,mHyphen) : "";
|
339
|
return URLencode(ret2);
|
340
|
case REND_POWER : int pMonkey = version.indexOf('@');
|
341
|
int pSpace = version.lastIndexOf(' ');
|
342
|
String ret3 = pSpace>=0 && pMonkey>pSpace+1 ? version.substring(pSpace+1,pMonkey) : "";
|
343
|
return URLencode(ret3);
|
344
|
default : return "";
|
345
|
}
|
346
|
}
|
347
|
|
348
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
349
|
|
350
|
private String URLencode(String s)
|
351
|
{
|
352
|
StringBuilder sbuf = new StringBuilder();
|
353
|
int len = s.length();
|
354
|
|
355
|
for (int i = 0; i < len; i++)
|
356
|
{
|
357
|
int ch = s.charAt(i);
|
358
|
|
359
|
if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
|
360
|
else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
|
361
|
else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
|
362
|
else if (ch == ' ' ) sbuf.append('+');
|
363
|
else if (ch == '-' || ch == '_'
|
364
|
|| ch == '.' || ch == '!'
|
365
|
|| ch == '~' || ch == '*'
|
366
|
|| ch == '\'' || ch == '('
|
367
|
|| ch == ')' ) sbuf.append((char)ch);
|
368
|
else if (ch <= 0x007f) sbuf.append(hex[ch]);
|
369
|
else if (ch <= 0x07FF)
|
370
|
{
|
371
|
sbuf.append(hex[0xc0 | (ch >> 6)]);
|
372
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
373
|
}
|
374
|
else
|
375
|
{
|
376
|
sbuf.append(hex[0xe0 | (ch >> 12)]);
|
377
|
sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
|
378
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
379
|
}
|
380
|
}
|
381
|
|
382
|
return sbuf.toString();
|
383
|
}
|
384
|
|
385
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
386
|
|
387
|
private boolean network(String url, ScoresReceiver receiver)
|
388
|
{
|
389
|
try
|
390
|
{
|
391
|
java.net.URL connectURL = new URL(url);
|
392
|
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
|
393
|
|
394
|
conn.setDoInput(true);
|
395
|
conn.setDoOutput(true);
|
396
|
conn.setUseCaches(false);
|
397
|
conn.setRequestMethod("GET");
|
398
|
conn.connect();
|
399
|
conn.getOutputStream().flush();
|
400
|
|
401
|
InputStream is = conn.getInputStream();
|
402
|
BufferedReader r = new BufferedReader(new InputStreamReader(is));
|
403
|
StringBuilder total = new StringBuilder();
|
404
|
|
405
|
for (String line; (line = r.readLine()) != null; )
|
406
|
{
|
407
|
total.append(line).append('\n');
|
408
|
}
|
409
|
|
410
|
mScores = total.toString();
|
411
|
conn.disconnect();
|
412
|
}
|
413
|
catch( final UnknownHostException e )
|
414
|
{
|
415
|
receiver.message("No access to Internet");
|
416
|
return false;
|
417
|
}
|
418
|
catch( final SecurityException e )
|
419
|
{
|
420
|
receiver.message("Application not authorized to connect to the Internet");
|
421
|
return false;
|
422
|
}
|
423
|
catch( final Exception e )
|
424
|
{
|
425
|
receiver.message(e.getMessage());
|
426
|
return false;
|
427
|
}
|
428
|
|
429
|
if( mScores.length()==0 )
|
430
|
{
|
431
|
receiver.message("Failed to download scores");
|
432
|
return false;
|
433
|
}
|
434
|
|
435
|
return true;
|
436
|
}
|
437
|
|
438
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439
|
|
440
|
private String constructSuspiciousURL(String suspURL)
|
441
|
{
|
442
|
RubikScores scores = RubikScores.getInstance();
|
443
|
int deviceID= scores.getDeviceID();
|
444
|
String suspicious = URLencode(suspURL);
|
445
|
|
446
|
String url="https://distorted.org/magic/cgi-bin/suspicious.cgi";
|
447
|
url += "?i="+deviceID+"&d="+suspicious;
|
448
|
|
449
|
return url;
|
450
|
}
|
451
|
|
452
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
453
|
|
454
|
private String constructDebugURL()
|
455
|
{
|
456
|
RubikScores scores = RubikScores.getInstance();
|
457
|
String name = URLencode(scores.getName());
|
458
|
int numRuns = scores.getNumRuns();
|
459
|
int numPlay = scores.getNumPlays();
|
460
|
String country = scores.getCountry();
|
461
|
String renderer = DistortedLibrary.getDriverRenderer();
|
462
|
String version = DistortedLibrary.getDriverVersion();
|
463
|
int objectAPI = JsonWriter.VERSION_OBJECT_MAJOR;
|
464
|
int tutorialAPI = JsonWriter.VERSION_EXTRAS_MAJOR;
|
465
|
|
466
|
renderer = URLencode(renderer);
|
467
|
version = URLencode(version);
|
468
|
|
469
|
String url="https://distorted.org/magic/cgi-bin/debugs.cgi";
|
470
|
url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
|
471
|
url += "&d="+renderer+"&v="+version+"&a="+objectAPI+"&b="+tutorialAPI;
|
472
|
|
473
|
return url;
|
474
|
}
|
475
|
|
476
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
477
|
|
478
|
private String constructDownloadURL()
|
479
|
{
|
480
|
RubikScores scores = RubikScores.getInstance();
|
481
|
String name = URLencode(scores.getName());
|
482
|
int numRuns = scores.getNumRuns();
|
483
|
int numPlay = scores.getNumPlays();
|
484
|
String country = scores.getCountry();
|
485
|
|
486
|
String url="https://distorted.org/magic/cgi-bin/download.cgi";
|
487
|
url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion;
|
488
|
|
489
|
return url;
|
490
|
}
|
491
|
|
492
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
493
|
|
494
|
private String constructSubmitURL()
|
495
|
{
|
496
|
RubikScores scores = RubikScores.getInstance();
|
497
|
String name = URLencode(scores.getName());
|
498
|
String veri = scores.isVerified() ? "1" : "";
|
499
|
int numRuns = scores.getNumRuns();
|
500
|
int numPlay = scores.getNumPlays();
|
501
|
int deviceID= scores.getDeviceID();
|
502
|
String reclist = scores.getRecordList("&o=","&l=","&t=");
|
503
|
String country = scores.getCountry();
|
504
|
long epoch = System.currentTimeMillis();
|
505
|
String salt = "cuboid";
|
506
|
|
507
|
String renderer = DistortedLibrary.getDriverRenderer();
|
508
|
String version = DistortedLibrary.getDriverVersion();
|
509
|
|
510
|
int type = getRendererType(renderer);
|
511
|
renderer = parseRenderer(type,renderer);
|
512
|
version = parseVersion(type,version);
|
513
|
|
514
|
String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
|
515
|
String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion;
|
516
|
url2 += "&d="+renderer+"&s="+version+reclist+"&c="+country+"&f="+epoch;
|
517
|
String hash = computeHash( url2, salt.getBytes() );
|
518
|
|
519
|
return url1 + "?" + url2 + "&h=" + hash;
|
520
|
}
|
521
|
|
522
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
523
|
|
524
|
private boolean gottaDownload()
|
525
|
{
|
526
|
return ((mScores.length()==0) && !mRunning);
|
527
|
}
|
528
|
|
529
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
530
|
|
531
|
private void figureOutVersion(Activity act)
|
532
|
{
|
533
|
if( mVersion==null )
|
534
|
{
|
535
|
try
|
536
|
{
|
537
|
PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
|
538
|
mVersion = pInfo.versionName;
|
539
|
}
|
540
|
catch (PackageManager.NameNotFoundException e)
|
541
|
{
|
542
|
mVersion = "0.9.2";
|
543
|
}
|
544
|
}
|
545
|
}
|
546
|
|
547
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
548
|
|
549
|
private void downloadThread(ScoresReceiver receiver)
|
550
|
{
|
551
|
boolean receiveValues=true;
|
552
|
|
553
|
try
|
554
|
{
|
555
|
if( gottaDownload() )
|
556
|
{
|
557
|
mRunning = true;
|
558
|
receiveValues = network(constructDownloadURL(),receiver);
|
559
|
|
560
|
if( mRunning )
|
561
|
{
|
562
|
receiveValues = fillValuesNormal(receiver);
|
563
|
mRunning = false;
|
564
|
}
|
565
|
}
|
566
|
if( receiveValues ) receiver.receive(mCountry, mName, mTime);
|
567
|
}
|
568
|
catch( Exception e )
|
569
|
{
|
570
|
receiver.message("Exception downloading records: "+e.getMessage() );
|
571
|
}
|
572
|
}
|
573
|
|
574
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
575
|
|
576
|
private void submitThread(ScoresReceiver receiver)
|
577
|
{
|
578
|
try
|
579
|
{
|
580
|
mRunning = true;
|
581
|
RubikScores scores = RubikScores.getInstance();
|
582
|
|
583
|
if( scores.thereAreUnsubmittedRecords() )
|
584
|
{
|
585
|
boolean receiveValues = network(constructSubmitURL(),receiver);
|
586
|
|
587
|
if( mRunning )
|
588
|
{
|
589
|
receiveValues = fillValuesNormal(receiver);
|
590
|
mRunning = false;
|
591
|
}
|
592
|
|
593
|
if( receiveValues )
|
594
|
{
|
595
|
RubikScores.getInstance().successfulSubmit();
|
596
|
receiver.receive(mCountry, mName, mTime);
|
597
|
}
|
598
|
}
|
599
|
}
|
600
|
catch( Exception e )
|
601
|
{
|
602
|
receiver.message("Exception submitting records: "+e.getMessage() );
|
603
|
}
|
604
|
}
|
605
|
|
606
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
607
|
|
608
|
private void debugThread()
|
609
|
{
|
610
|
String url = constructDebugURL();
|
611
|
/*
|
612
|
try { Thread.sleep(5000); }
|
613
|
catch( InterruptedException ignored) {}
|
614
|
*/
|
615
|
try
|
616
|
{
|
617
|
java.net.URL connectURL = new URL(url);
|
618
|
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
|
619
|
|
620
|
conn.setDoInput(true);
|
621
|
conn.setDoOutput(true);
|
622
|
conn.setUseCaches(false);
|
623
|
conn.setRequestMethod("GET");
|
624
|
conn.connect();
|
625
|
conn.getOutputStream().flush();
|
626
|
|
627
|
InputStream is = conn.getInputStream();
|
628
|
BufferedReader r = new BufferedReader(new InputStreamReader(is));
|
629
|
StringBuilder answer = new StringBuilder();
|
630
|
|
631
|
for (String line; (line = r.readLine()) != null; )
|
632
|
{
|
633
|
answer.append(line).append('\n');
|
634
|
}
|
635
|
|
636
|
String updates = answer.toString();
|
637
|
conn.disconnect();
|
638
|
mUpdates.parse(updates);
|
639
|
|
640
|
if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
|
641
|
mDebugState = DEBUG_SUCCESS;
|
642
|
}
|
643
|
catch( final Exception e )
|
644
|
{
|
645
|
if( mUpdatee!=null ) mUpdatee.errorUpdate();
|
646
|
mDebugState = DEBUG_FAILURE;
|
647
|
}
|
648
|
}
|
649
|
|
650
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
651
|
|
652
|
private void suspiciousThread(String suspURL)
|
653
|
{
|
654
|
String url = constructSuspiciousURL(suspURL);
|
655
|
|
656
|
try
|
657
|
{
|
658
|
java.net.URL connectURL = new URL(url);
|
659
|
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
|
660
|
|
661
|
conn.setDoInput(true);
|
662
|
conn.setDoOutput(true);
|
663
|
conn.setUseCaches(false);
|
664
|
conn.setRequestMethod("GET");
|
665
|
conn.connect();
|
666
|
conn.getOutputStream().flush();
|
667
|
conn.getInputStream();
|
668
|
conn.disconnect();
|
669
|
}
|
670
|
catch( final Exception e )
|
671
|
{
|
672
|
// ignore
|
673
|
}
|
674
|
}
|
675
|
|
676
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
677
|
|
678
|
private Bitmap downloadIcon(String url)
|
679
|
{
|
680
|
try
|
681
|
{
|
682
|
java.net.URL connectURL = new URL(url);
|
683
|
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
|
684
|
conn.setDoInput(true);
|
685
|
conn.connect();
|
686
|
InputStream input = conn.getInputStream();
|
687
|
Bitmap icon = BitmapFactory.decodeStream(input);
|
688
|
conn.disconnect();
|
689
|
return icon;
|
690
|
}
|
691
|
catch (IOException e)
|
692
|
{
|
693
|
android.util.Log.e("D", "Failed to download "+url);
|
694
|
android.util.Log.e("D", e.getMessage() );
|
695
|
return null;
|
696
|
}
|
697
|
}
|
698
|
|
699
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
700
|
|
701
|
private void iconThread(Context context, IconReceiver receiver)
|
702
|
{
|
703
|
int numC = mUpdates.getCompletedNumber();
|
704
|
int numS = mUpdates.getStartedNumber();
|
705
|
|
706
|
for(int c=0; c<numC; c++)
|
707
|
{
|
708
|
int iconPresent = mUpdates.getCompletedIconPresent(c);
|
709
|
|
710
|
if( iconPresent!=0 )
|
711
|
{
|
712
|
boolean downloaded = false;
|
713
|
Bitmap icon = mUpdates.getCompletedIcon(context,c);
|
714
|
|
715
|
if( icon==null )
|
716
|
{
|
717
|
String url = mUpdates.getCompletedURL(c);
|
718
|
icon = downloadIcon(url);
|
719
|
downloaded = true;
|
720
|
|
721
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading icon "+url);
|
722
|
}
|
723
|
if( icon!=null )
|
724
|
{
|
725
|
mUpdates.setCompletedIcon(c,icon);
|
726
|
receiver.iconDownloaded(c,icon,downloaded);
|
727
|
}
|
728
|
}
|
729
|
}
|
730
|
|
731
|
for(int s=0; s<numS; s++)
|
732
|
{
|
733
|
int iconPresent = mUpdates.getStartedIconPresent(s);
|
734
|
|
735
|
if( iconPresent!=0 )
|
736
|
{
|
737
|
boolean downloaded = false;
|
738
|
Bitmap icon = mUpdates.getStartedIcon(context,s);
|
739
|
|
740
|
if( icon==null )
|
741
|
{
|
742
|
String url = mUpdates.getStartedURL(s);
|
743
|
icon = downloadIcon(url);
|
744
|
downloaded = true;
|
745
|
|
746
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading icon "+url);
|
747
|
}
|
748
|
if( icon!=null )
|
749
|
{
|
750
|
mUpdates.setStartedIcon(s,icon);
|
751
|
receiver.iconDownloaded(numC+s,icon,downloaded);
|
752
|
}
|
753
|
}
|
754
|
}
|
755
|
}
|
756
|
|
757
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
758
|
|
759
|
private InputStream downloadJSON(String name)
|
760
|
{
|
761
|
String url = mUpdates.getURL() + name;
|
762
|
|
763
|
try
|
764
|
{
|
765
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading JSON "+url);
|
766
|
|
767
|
java.net.URL connectURL = new URL(url);
|
768
|
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
|
769
|
conn.setDoInput(true);
|
770
|
conn.connect();
|
771
|
return conn.getInputStream();
|
772
|
}
|
773
|
catch (IOException e)
|
774
|
{
|
775
|
android.util.Log.e("D", "Failed to download "+url);
|
776
|
android.util.Log.e("D", e.getMessage() );
|
777
|
return null;
|
778
|
}
|
779
|
}
|
780
|
|
781
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
782
|
|
783
|
private void jsonThread(final RubikUpdates.UpdateInfo info, Downloadee downloadee)
|
784
|
{
|
785
|
if(info.mUpdateObject) info.mObjectStream = downloadJSON(info.mObjectShortName+"_object.json");
|
786
|
if(info.mUpdateExtras) info.mExtrasStream = downloadJSON(info.mObjectShortName+"_extras.json");
|
787
|
|
788
|
downloadee.jsonDownloaded();
|
789
|
|
790
|
try
|
791
|
{
|
792
|
if( info.mObjectStream!=null ) info.mObjectStream.close();
|
793
|
}
|
794
|
catch(IOException ioe)
|
795
|
{
|
796
|
android.util.Log.e("D", "failed to close object input stream");
|
797
|
}
|
798
|
|
799
|
try
|
800
|
{
|
801
|
if( info.mExtrasStream!=null ) info.mExtrasStream.close();
|
802
|
}
|
803
|
catch(IOException ioe)
|
804
|
{
|
805
|
android.util.Log.e("D", "failed to close extras input stream");
|
806
|
}
|
807
|
}
|
808
|
|
809
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
810
|
|
811
|
private RubikNetwork()
|
812
|
{
|
813
|
|
814
|
}
|
815
|
|
816
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
817
|
// PUBLIC API
|
818
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
819
|
|
820
|
public static void onPause()
|
821
|
{
|
822
|
mRunning = false;
|
823
|
}
|
824
|
|
825
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
826
|
|
827
|
public static RubikNetwork getInstance()
|
828
|
{
|
829
|
if( mThis==null ) mThis = new RubikNetwork();
|
830
|
return mThis;
|
831
|
}
|
832
|
|
833
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
834
|
|
835
|
public void download(final ScoresReceiver receiver, final Activity act)
|
836
|
{
|
837
|
initializeStatics();
|
838
|
figureOutVersion(act);
|
839
|
|
840
|
Thread thread = new Thread()
|
841
|
{
|
842
|
public void run()
|
843
|
{
|
844
|
downloadThread(receiver);
|
845
|
}
|
846
|
};
|
847
|
|
848
|
thread.start();
|
849
|
}
|
850
|
|
851
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
852
|
|
853
|
public void submit(ScoresReceiver receiver, final Activity act)
|
854
|
{
|
855
|
initializeStatics();
|
856
|
figureOutVersion(act);
|
857
|
|
858
|
Thread thread = new Thread()
|
859
|
{
|
860
|
public void run()
|
861
|
{
|
862
|
submitThread(receiver);
|
863
|
}
|
864
|
};
|
865
|
|
866
|
thread.start();
|
867
|
}
|
868
|
|
869
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
870
|
|
871
|
public void debug(final Activity act)
|
872
|
{
|
873
|
initializeStatics();
|
874
|
figureOutVersion(act);
|
875
|
mDebugState = DEBUG_RUNNING;
|
876
|
|
877
|
Thread thread = new Thread()
|
878
|
{
|
879
|
public void run()
|
880
|
{
|
881
|
debugThread();
|
882
|
}
|
883
|
};
|
884
|
|
885
|
thread.start();
|
886
|
}
|
887
|
|
888
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
889
|
|
890
|
public void suspicious(final String suspicious, final Activity act)
|
891
|
{
|
892
|
initializeStatics();
|
893
|
figureOutVersion(act);
|
894
|
|
895
|
Thread thread = new Thread()
|
896
|
{
|
897
|
public void run()
|
898
|
{
|
899
|
suspiciousThread(suspicious);
|
900
|
}
|
901
|
};
|
902
|
|
903
|
thread.start();
|
904
|
}
|
905
|
|
906
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
907
|
// Yes it can happen that the second Updatee registers before we sent an update to the first one
|
908
|
// and, as a result, the update never gets sent to the first one. This is not a problem (now, when
|
909
|
// there are only two updatees - the RubikStatePlay and the UpdateDialog)
|
910
|
//
|
911
|
// Yes, there is also a remote possibility that the two threads executing this function and executing
|
912
|
// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee.
|
913
|
// We don't care about such remote possibility, then the app simply would signal that there are no
|
914
|
// updates available.
|
915
|
|
916
|
public void signUpForUpdates(Updatee updatee)
|
917
|
{
|
918
|
if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates);
|
919
|
else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate();
|
920
|
else mUpdatee = updatee;
|
921
|
}
|
922
|
|
923
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
924
|
|
925
|
public void downloadIcons(final Context context, final IconReceiver receiver)
|
926
|
{
|
927
|
initializeStatics();
|
928
|
|
929
|
Thread thread = new Thread()
|
930
|
{
|
931
|
public void run()
|
932
|
{
|
933
|
iconThread(context,receiver);
|
934
|
}
|
935
|
};
|
936
|
|
937
|
thread.start();
|
938
|
}
|
939
|
|
940
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
941
|
|
942
|
public void downloadJSON(final RubikUpdates.UpdateInfo info, final Downloadee downloadee)
|
943
|
{
|
944
|
initializeStatics();
|
945
|
|
946
|
Thread thread = new Thread()
|
947
|
{
|
948
|
public void run()
|
949
|
{
|
950
|
jsonThread(info,downloadee);
|
951
|
}
|
952
|
};
|
953
|
|
954
|
thread.start();
|
955
|
}
|
956
|
|
957
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
958
|
|
959
|
public void updateDone(String shortName)
|
960
|
{
|
961
|
mUpdates.updateDone(shortName);
|
962
|
mScores = "";
|
963
|
}
|
964
|
}
|