Project

General

Profile

Download (29.4 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / external / RubikNetwork.java @ 9fe350fe

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