Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikNetwork.java @ e4854e54

1 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 acabdd83 Leszek Koltunski
package org.distorted.external;
21 fdec60a3 Leszek Koltunski
22 1d643a0e Leszek Koltunski
import java.io.BufferedReader;
23 b88cdd91 Leszek Koltunski
import java.io.IOException;
24 3f7a4363 Leszek Koltunski
import java.io.InputStream;
25 1d643a0e Leszek Koltunski
import java.io.InputStreamReader;
26 3f7a4363 Leszek Koltunski
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 e4854e54 Leszek Koltunski
import android.app.Activity;
33 7fe62d1f Leszek Koltunski
import android.content.Context;
34 1780dcb0 Leszek Koltunski
import android.content.pm.PackageInfo;
35
import android.content.pm.PackageManager;
36 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
37
import android.graphics.BitmapFactory;
38 1780dcb0 Leszek Koltunski
39 6a083c6a Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
40 fcf7320f Leszek Koltunski
import org.distorted.objectlib.json.JsonWriter;
41 a7d8c3cd Leszek Koltunski
import org.distorted.objects.RubikObjectList;
42 36e2cbdd Leszek Koltunski
43 d433b50e Leszek Koltunski
import static org.distorted.objects.RubikObjectList.MAX_LEVEL;
44 e847c553 Leszek Koltunski
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
45 211b48f2 Leszek Koltunski
46 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 ffe7e55d Leszek Koltunski
public class RubikNetwork
49 fdec60a3 Leszek Koltunski
  {
50 46be3ddf Leszek Koltunski
  public interface ScoresReceiver
51 b8b38548 Leszek Koltunski
    {
52 1c90c64a Leszek Koltunski
    void receive(String[][][] country, String[][][] name, float[][][] time);
53 4895fff6 Leszek Koltunski
    void message(String mess);
54 4c0cd600 Leszek Koltunski
    void error(String error);
55 b8b38548 Leszek Koltunski
    }
56
57 46be3ddf Leszek Koltunski
  public interface IconReceiver
58 63dd19c4 Leszek Koltunski
    {
59 7fe62d1f Leszek Koltunski
    void iconDownloaded(int ordinal, Bitmap bitmap, boolean downloaded);
60 46be3ddf Leszek Koltunski
    }
61
62
  public interface Updatee
63
    {
64 63dd19c4 Leszek Koltunski
    void receiveUpdate(RubikUpdates update);
65
    void errorUpdate();
66
    }
67
68 46be3ddf Leszek Koltunski
  public interface Downloadee
69
    {
70
    void jsonDownloaded();
71
    }
72
73 286d73ae Leszek Koltunski
  public static final int MAX_PLACES = 10;
74 d8aa4ba8 Leszek Koltunski
75 14f14138 Leszek Koltunski
  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 9d4c38eb Leszek Koltunski
80 63dd19c4 Leszek Koltunski
  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 36e2cbdd Leszek Koltunski
  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 a36b9dca Leszek Koltunski
  private static String[][][] mCountry;
120
  private static String[][][] mName;
121
  private static float[][][] mTime;
122
  private static int[][] mPlaces;
123 688f7712 Leszek Koltunski
124 6a083c6a Leszek Koltunski
  private static RubikNetwork mThis;
125 688f7712 Leszek Koltunski
  private static String mScores = "";
126 b8b38548 Leszek Koltunski
  private static boolean mRunning = false;
127 63dd19c4 Leszek Koltunski
  private static Updatee mUpdatee;
128 4895fff6 Leszek Koltunski
  private static String mVersion;
129 a7d8c3cd Leszek Koltunski
  private static int mNumObjects;
130 fcf7320f Leszek Koltunski
  private static RubikUpdates mUpdates;
131 63dd19c4 Leszek Koltunski
  private static int mDebugState;
132 b8b38548 Leszek Koltunski
133 a36b9dca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135
  private static void initializeStatics()
136
    {
137 a7d8c3cd Leszek Koltunski
    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 fcf7320f Leszek Koltunski
    if( mPlaces==null  || newNum!=mNumObjects ) mPlaces  = new    int[newNum][MAX_LEVEL];
143
144
    if( mUpdates==null ) mUpdates = new RubikUpdates();
145 a7d8c3cd Leszek Koltunski
146
    mNumObjects = newNum;
147 a36b9dca Leszek Koltunski
    }
148
149 4895fff6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 86cbdab1 Leszek Koltunski
  private static String computeHash(String stringToHash, byte[] salt)
152 4895fff6 Leszek Koltunski
    {
153 86cbdab1 Leszek Koltunski
    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 4895fff6 Leszek Koltunski
    }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179 46be3ddf Leszek Koltunski
  private boolean fillValuesNormal(ScoresReceiver receiver)
180 f3e12931 Leszek Koltunski
    {
181 d8aa4ba8 Leszek Koltunski
    int begin=-1 ,end, len = mScores.length();
182 17f9a695 Leszek Koltunski
    String row;
183 f3e12931 Leszek Koltunski
184 4c0cd600 Leszek Koltunski
    if( len==0 )
185
      {
186 46be3ddf Leszek Koltunski
      receiver.error("1");
187 4c0cd600 Leszek Koltunski
      return false;
188
      }
189 90fd47b0 Leszek Koltunski
    else if( len<=2 )
190 4c0cd600 Leszek Koltunski
      {
191 46be3ddf Leszek Koltunski
      receiver.error(mScores);
192 4c0cd600 Leszek Koltunski
      return false;
193
      }
194
195 a7d8c3cd Leszek Koltunski
    for(int i=0; i<mNumObjects; i++)
196 85b09df4 Leszek Koltunski
      for(int j=0; j<MAX_LEVEL; j++)
197 329c0aeb Leszek Koltunski
        {
198
        mPlaces[i][j] = 0;
199
        }
200
201 d8aa4ba8 Leszek Koltunski
    while( begin<len )
202
      {
203
      end = mScores.indexOf('\n', begin+1);
204
      if( end<0 ) end = len;
205 286d73ae Leszek Koltunski
206
      try
207
        {
208 17f9a695 Leszek Koltunski
        row = mScores.substring(begin+1,end);
209 4c0cd600 Leszek Koltunski
        fillRow(row);
210 286d73ae Leszek Koltunski
        }
211
      catch(Exception ex)
212
        {
213
        // faulty row - ignore
214
        }
215
216 d8aa4ba8 Leszek Koltunski
      begin = end;
217
      }
218 17f9a695 Leszek Koltunski
219
    return true;
220 f3e12931 Leszek Koltunski
    }
221
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224 d8aa4ba8 Leszek Koltunski
  private void fillRow(String row)
225 f3e12931 Leszek Koltunski
    {
226 d8aa4ba8 Leszek Koltunski
    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 286d73ae Leszek Koltunski
    int s5 = row.length();
231 d8aa4ba8 Leszek Koltunski
232
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
233
      {
234 a7d8c3cd Leszek Koltunski
      int object = RubikObjectList.getOrdinal( row.substring(0,s1) );
235 b8b38548 Leszek Koltunski
236 a7d8c3cd Leszek Koltunski
      if( object>=0 && object<mNumObjects )
237 d8aa4ba8 Leszek Koltunski
        {
238 8e3898c8 Leszek Koltunski
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
239 286d73ae Leszek Koltunski
        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 d8aa4ba8 Leszek Koltunski
243 c8249cf6 Leszek Koltunski
        if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
244
245 8e3898c8 Leszek Koltunski
        if(level>=0 && level<MAX_LEVEL)
246 d8aa4ba8 Leszek Koltunski
          {
247 8e3898c8 Leszek Koltunski
          int p = mPlaces[object][level];
248
          mPlaces[object][level]++;
249 329c0aeb Leszek Koltunski
250 8e3898c8 Leszek Koltunski
          mCountry[object][level][p] = country;
251
          mName   [object][level][p] = name;
252 79e752b3 Leszek Koltunski
          mTime   [object][level][p] = ((float)(time/10))/100.0f;
253 d8aa4ba8 Leszek Koltunski
          }
254
        }
255
      }
256 874c37b1 Leszek Koltunski
    else
257
      {
258 5d50e7a7 Leszek Koltunski
      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 874c37b1 Leszek Koltunski
        {
272 5d50e7a7 Leszek Koltunski
        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 874c37b1 Leszek Koltunski
284 5d50e7a7 Leszek Koltunski
        if(number==1)
285 874c37b1 Leszek Koltunski
          {
286
          String country = row.substring(colon+1);
287
          RubikScores scores = RubikScores.getInstance();
288
          scores.setCountry(country);
289
          }
290
        }
291
      }
292 f3e12931 Leszek Koltunski
    }
293
294 9d4c38eb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 36e2cbdd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
386
387 46be3ddf Leszek Koltunski
  private boolean network(String url, ScoresReceiver receiver)
388 f3e12931 Leszek Koltunski
    {
389 36e2cbdd Leszek Koltunski
    try
390
      {
391 4895fff6 Leszek Koltunski
      java.net.URL connectURL = new URL(url);
392 36e2cbdd Leszek Koltunski
      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 1d643a0e Leszek Koltunski
      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 36e2cbdd Leszek Koltunski
        {
407 1d643a0e Leszek Koltunski
        total.append(line).append('\n');
408 36e2cbdd Leszek Koltunski
        }
409 1d643a0e Leszek Koltunski
410
      mScores = total.toString();
411 46be3ddf Leszek Koltunski
      conn.disconnect();
412 36e2cbdd Leszek Koltunski
      }
413 cc5ec229 Leszek Koltunski
    catch( final UnknownHostException e )
414
      {
415 46be3ddf Leszek Koltunski
      receiver.message("No access to Internet");
416 cc5ec229 Leszek Koltunski
      return false;
417
      }
418
    catch( final SecurityException e )
419
      {
420 46be3ddf Leszek Koltunski
      receiver.message("Application not authorized to connect to the Internet");
421 cc5ec229 Leszek Koltunski
      return false;
422
      }
423
    catch( final Exception e )
424 36e2cbdd Leszek Koltunski
      {
425 46be3ddf Leszek Koltunski
      receiver.message(e.getMessage());
426 a675474f Leszek Koltunski
      return false;
427 36e2cbdd Leszek Koltunski
      }
428
429 329c0aeb Leszek Koltunski
    if( mScores.length()==0 )
430
      {
431 46be3ddf Leszek Koltunski
      receiver.message("Failed to download scores");
432 329c0aeb Leszek Koltunski
      return false;
433
      }
434
435 a675474f Leszek Koltunski
    return true;
436 b8b38548 Leszek Koltunski
    }
437 d8aa4ba8 Leszek Koltunski
438 1fa125c2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
439
440 46be3ddf Leszek Koltunski
  private String constructSuspiciousURL(String suspURL)
441 1fa125c2 Leszek Koltunski
    {
442
    RubikScores scores = RubikScores.getInstance();
443
    int deviceID= scores.getDeviceID();
444 46be3ddf Leszek Koltunski
    String suspicious = URLencode(suspURL);
445 1fa125c2 Leszek Koltunski
446
    String url="https://distorted.org/magic/cgi-bin/suspicious.cgi";
447 63dd19c4 Leszek Koltunski
    url += "?i="+deviceID+"&d="+suspicious;
448 1fa125c2 Leszek Koltunski
449
    return url;
450
    }
451
452 6a083c6a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fcf7320f Leszek Koltunski
    int objectAPI   = JsonWriter.VERSION_OBJECT_MAJOR;
464
    int tutorialAPI = JsonWriter.VERSION_EXTRAS_MAJOR;
465 6a083c6a Leszek Koltunski
466
    renderer = URLencode(renderer);
467
    version  = URLencode(version);
468
469 66935886 Leszek Koltunski
    String url="https://distorted.org/magic/cgi-bin/debugs.cgi";
470 fcf7320f Leszek Koltunski
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
471
    url += "&d="+renderer+"&v="+version+"&a="+objectAPI+"&b="+tutorialAPI;
472 6a083c6a Leszek Koltunski
473
    return url;
474
    }
475
476 17f9a695 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 874c37b1 Leszek Koltunski
    String country = scores.getCountry();
485 17f9a695 Leszek Koltunski
486
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
487 11d68e9c Leszek Koltunski
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion;
488 17f9a695 Leszek Koltunski
489
    return url;
490
    }
491
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493
494
  private String constructSubmitURL()
495
    {
496
    RubikScores scores = RubikScores.getInstance();
497
    String name = URLencode(scores.getName());
498 11d68e9c Leszek Koltunski
    String veri = scores.isVerified() ? "1" : "";
499 17f9a695 Leszek Koltunski
    int numRuns = scores.getNumRuns();
500
    int numPlay = scores.getNumPlays();
501
    int deviceID= scores.getDeviceID();
502 6570171b Leszek Koltunski
    String reclist = scores.getRecordList("&o=","&l=","&t=");
503 17f9a695 Leszek Koltunski
    String country = scores.getCountry();
504 86cbdab1 Leszek Koltunski
    long epoch = System.currentTimeMillis();
505 9e171f43 Leszek Koltunski
    String salt = "cuboid";
506 17f9a695 Leszek Koltunski
507 9d4c38eb Leszek Koltunski
    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 86cbdab1 Leszek Koltunski
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
515 11d68e9c Leszek Koltunski
    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 9e171f43 Leszek Koltunski
    String hash = computeHash( url2, salt.getBytes() );
518 17f9a695 Leszek Koltunski
519 b3ee0e78 Leszek Koltunski
    return url1 + "?" + url2 + "&h=" + hash;
520 17f9a695 Leszek Koltunski
    }
521
522 d8aa4ba8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
523
524
  private boolean gottaDownload()
525 b8b38548 Leszek Koltunski
    {
526 d8aa4ba8 Leszek Koltunski
    return ((mScores.length()==0) && !mRunning);
527 f3e12931 Leszek Koltunski
    }
528 b8b38548 Leszek Koltunski
529 d8aa4ba8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
530
531 e4854e54 Leszek Koltunski
  private void figureOutVersion(Activity act)
532 d8aa4ba8 Leszek Koltunski
    {
533 e4854e54 Leszek Koltunski
    if( mVersion==null )
534 ffe7e55d Leszek Koltunski
      {
535 e4854e54 Leszek Koltunski
      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 ffe7e55d Leszek Koltunski
      }
545
    }
546 a675474f Leszek Koltunski
547 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
548 a36b9dca Leszek Koltunski
549 46be3ddf Leszek Koltunski
  private void downloadThread(ScoresReceiver receiver)
550 ffe7e55d Leszek Koltunski
    {
551 17f9a695 Leszek Koltunski
    try
552 d8aa4ba8 Leszek Koltunski
      {
553 ffe7e55d Leszek Koltunski
      if( gottaDownload() )
554 4895fff6 Leszek Koltunski
        {
555 17f9a695 Leszek Koltunski
        mRunning = true;
556 46be3ddf Leszek Koltunski
        boolean receiveValues = network(constructDownloadURL(),receiver);
557 4895fff6 Leszek Koltunski
558 ffe7e55d Leszek Koltunski
        if( mRunning )
559 17f9a695 Leszek Koltunski
          {
560 46be3ddf Leszek Koltunski
          receiveValues = fillValuesNormal(receiver);
561 ffe7e55d Leszek Koltunski
          mRunning = false;
562 17f9a695 Leszek Koltunski
          }
563 ffe7e55d Leszek Koltunski
564 46be3ddf Leszek Koltunski
        if( receiveValues ) receiver.receive(mCountry, mName, mTime);
565 1fa125c2 Leszek Koltunski
        }
566 d8aa4ba8 Leszek Koltunski
      }
567 17f9a695 Leszek Koltunski
    catch( Exception e )
568 d8aa4ba8 Leszek Koltunski
      {
569 46be3ddf Leszek Koltunski
      receiver.message("Exception downloading records: "+e.getMessage() );
570 4895fff6 Leszek Koltunski
      }
571 ffe7e55d Leszek Koltunski
    }
572
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574 286d73ae Leszek Koltunski
575 46be3ddf Leszek Koltunski
  private void submitThread(ScoresReceiver receiver)
576 ffe7e55d Leszek Koltunski
    {
577
    try
578
      {
579
      mRunning = true;
580
      RubikScores scores = RubikScores.getInstance();
581
582
      if( scores.thereAreUnsubmittedRecords() )
583
        {
584 46be3ddf Leszek Koltunski
        boolean receiveValues = network(constructSubmitURL(),receiver);
585 ffe7e55d Leszek Koltunski
586
        if( mRunning )
587
          {
588 46be3ddf Leszek Koltunski
          receiveValues = fillValuesNormal(receiver);
589 ffe7e55d Leszek Koltunski
          mRunning = false;
590
          }
591
592
        if( receiveValues )
593
          {
594
          RubikScores.getInstance().successfulSubmit();
595 46be3ddf Leszek Koltunski
          receiver.receive(mCountry, mName, mTime);
596 ffe7e55d Leszek Koltunski
          }
597
        }
598
      }
599
    catch( Exception e )
600 17f9a695 Leszek Koltunski
      {
601 46be3ddf Leszek Koltunski
      receiver.message("Exception submitting records: "+e.getMessage() );
602 17f9a695 Leszek Koltunski
      }
603 ffe7e55d Leszek Koltunski
    }
604 a675474f Leszek Koltunski
605 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
606
607
  private void debugThread()
608
    {
609
    String url = constructDebugURL();
610
/*
611
    try { Thread.sleep(5000); }
612
    catch( InterruptedException ignored) {}
613
*/
614
    try
615 a675474f Leszek Koltunski
      {
616 ffe7e55d Leszek Koltunski
      java.net.URL connectURL = new URL(url);
617
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
618 17f9a695 Leszek Koltunski
619 ffe7e55d Leszek Koltunski
      conn.setDoInput(true);
620
      conn.setDoOutput(true);
621
      conn.setUseCaches(false);
622
      conn.setRequestMethod("GET");
623
      conn.connect();
624
      conn.getOutputStream().flush();
625
626
      InputStream is = conn.getInputStream();
627
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
628
      StringBuilder answer = new StringBuilder();
629
630
      for (String line; (line = r.readLine()) != null; )
631 17f9a695 Leszek Koltunski
        {
632 ffe7e55d Leszek Koltunski
        answer.append(line).append('\n');
633 17f9a695 Leszek Koltunski
        }
634 ffe7e55d Leszek Koltunski
635
      String updates = answer.toString();
636 46be3ddf Leszek Koltunski
      conn.disconnect();
637 ffe7e55d Leszek Koltunski
      mUpdates.parse(updates);
638
639
      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
640
      mDebugState = DEBUG_SUCCESS;
641
      }
642
    catch( final Exception e )
643
      {
644
      if( mUpdatee!=null ) mUpdatee.errorUpdate();
645
      mDebugState = DEBUG_FAILURE;
646
      }
647
    }
648
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650
651 46be3ddf Leszek Koltunski
  private void suspiciousThread(String suspURL)
652 ffe7e55d Leszek Koltunski
    {
653 46be3ddf Leszek Koltunski
    String url = constructSuspiciousURL(suspURL);
654 ffe7e55d Leszek Koltunski
655
    try
656
      {
657
      java.net.URL connectURL = new URL(url);
658
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
659
660
      conn.setDoInput(true);
661
      conn.setDoOutput(true);
662
      conn.setUseCaches(false);
663
      conn.setRequestMethod("GET");
664
      conn.connect();
665
      conn.getOutputStream().flush();
666
      conn.getInputStream();
667 46be3ddf Leszek Koltunski
      conn.disconnect();
668 ffe7e55d Leszek Koltunski
      }
669
    catch( final Exception e )
670
      {
671
      // ignore
672 a675474f Leszek Koltunski
      }
673 d8aa4ba8 Leszek Koltunski
    }
674 4f9f99a2 Leszek Koltunski
675 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
676
677
  private Bitmap downloadIcon(String url)
678
    {
679
    try
680
      {
681
      java.net.URL connectURL = new URL(url);
682
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
683
      conn.setDoInput(true);
684
      conn.connect();
685
      InputStream input = conn.getInputStream();
686 46be3ddf Leszek Koltunski
      Bitmap icon = BitmapFactory.decodeStream(input);
687
      conn.disconnect();
688
      return icon;
689 b88cdd91 Leszek Koltunski
      }
690
    catch (IOException e)
691
      {
692
      android.util.Log.e("D", "Failed to download "+url);
693
      android.util.Log.e("D", e.getMessage() );
694
      return null;
695
      }
696
    }
697
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699
700 7fe62d1f Leszek Koltunski
  private void iconThread(Context context, IconReceiver receiver)
701 b88cdd91 Leszek Koltunski
    {
702
    int numC = mUpdates.getCompletedNumber();
703
    int numS = mUpdates.getStartedNumber();
704
705
    for(int c=0; c<numC; c++)
706
      {
707 b92ad5cd Leszek Koltunski
      int iconPresent = mUpdates.getCompletedIconPresent(c);
708 b88cdd91 Leszek Koltunski
709 b92ad5cd Leszek Koltunski
      if( iconPresent!=0 )
710 b88cdd91 Leszek Koltunski
        {
711 7fe62d1f Leszek Koltunski
        boolean downloaded = false;
712
        Bitmap icon = mUpdates.getCompletedIcon(context,c);
713 b92ad5cd Leszek Koltunski
714
        if( icon==null )
715
          {
716
          String url = mUpdates.getCompletedURL(c);
717
          icon = downloadIcon(url);
718 7fe62d1f Leszek Koltunski
          downloaded = true;
719 84d746d7 Leszek Koltunski
720 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading icon "+url);
721 b92ad5cd Leszek Koltunski
          }
722
        if( icon!=null )
723
          {
724
          mUpdates.setCompletedIcon(c,icon);
725 7fe62d1f Leszek Koltunski
          receiver.iconDownloaded(c,icon,downloaded);
726 b92ad5cd Leszek Koltunski
          }
727 b88cdd91 Leszek Koltunski
        }
728
      }
729
730
    for(int s=0; s<numS; s++)
731
      {
732 b92ad5cd Leszek Koltunski
      int iconPresent = mUpdates.getStartedIconPresent(s);
733 b88cdd91 Leszek Koltunski
734 b92ad5cd Leszek Koltunski
      if( iconPresent!=0 )
735 b88cdd91 Leszek Koltunski
        {
736 7fe62d1f Leszek Koltunski
        boolean downloaded = false;
737
        Bitmap icon = mUpdates.getStartedIcon(context,s);
738 b92ad5cd Leszek Koltunski
739
        if( icon==null )
740
          {
741
          String url = mUpdates.getStartedURL(s);
742
          icon = downloadIcon(url);
743 7fe62d1f Leszek Koltunski
          downloaded = true;
744 84d746d7 Leszek Koltunski
745 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading icon "+url);
746 b92ad5cd Leszek Koltunski
          }
747
        if( icon!=null )
748
          {
749
          mUpdates.setStartedIcon(s,icon);
750 7fe62d1f Leszek Koltunski
          receiver.iconDownloaded(numC+s,icon,downloaded);
751 b92ad5cd Leszek Koltunski
          }
752 b88cdd91 Leszek Koltunski
        }
753
      }
754
    }
755
756 46be3ddf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
757
758
  private InputStream downloadJSON(String name)
759
    {
760
    String url = mUpdates.getURL() + name;
761
762
    try
763
      {
764 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading JSON "+url);
765 84d746d7 Leszek Koltunski
766 46be3ddf Leszek Koltunski
      java.net.URL connectURL = new URL(url);
767
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
768
      conn.setDoInput(true);
769
      conn.connect();
770 806329e3 Leszek Koltunski
      return conn.getInputStream();
771 46be3ddf Leszek Koltunski
      }
772
    catch (IOException e)
773
      {
774
      android.util.Log.e("D", "Failed to download "+url);
775
      android.util.Log.e("D", e.getMessage() );
776
      return null;
777
      }
778
    }
779
780
///////////////////////////////////////////////////////////////////////////////////////////////////
781
782
  private void jsonThread(final RubikUpdates.UpdateInfo info, Downloadee downloadee)
783
    {
784
    if(info.mUpdateObject) info.mObjectStream = downloadJSON(info.mObjectShortName+"_object.json");
785
    if(info.mUpdateExtras) info.mExtrasStream = downloadJSON(info.mObjectShortName+"_extras.json");
786
787
    downloadee.jsonDownloaded();
788 806329e3 Leszek Koltunski
789
    try
790
      {
791
      if( info.mObjectStream!=null ) info.mObjectStream.close();
792
      }
793
    catch(IOException ioe)
794
      {
795
      android.util.Log.e("D", "failed to close object input stream");
796
      }
797
798
    try
799
      {
800
      if( info.mExtrasStream!=null ) info.mExtrasStream.close();
801
      }
802
    catch(IOException ioe)
803
      {
804
      android.util.Log.e("D", "failed to close extras input stream");
805
      }
806 46be3ddf Leszek Koltunski
    }
807
808 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
809
810 6a083c6a Leszek Koltunski
  private RubikNetwork()
811 bc511ea5 Leszek Koltunski
    {
812
813
    }
814
815 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
816
// PUBLIC API
817
///////////////////////////////////////////////////////////////////////////////////////////////////
818
819
  public static void onPause()
820
    {
821
    mRunning = false;
822
    }
823
824 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
825
826 6a083c6a Leszek Koltunski
  public static RubikNetwork getInstance()
827 bc511ea5 Leszek Koltunski
    {
828 35161021 Leszek Koltunski
    if( mThis==null ) mThis = new RubikNetwork();
829 bc511ea5 Leszek Koltunski
    return mThis;
830
    }
831
832 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
833
834 e4854e54 Leszek Koltunski
  public void download(final ScoresReceiver receiver, final Activity act)
835 4f9f99a2 Leszek Koltunski
    {
836 ffe7e55d Leszek Koltunski
    initializeStatics();
837
    figureOutVersion(act);
838 1780dcb0 Leszek Koltunski
839 ffe7e55d Leszek Koltunski
    Thread thread = new Thread()
840 1780dcb0 Leszek Koltunski
      {
841 ffe7e55d Leszek Koltunski
      public void run()
842
        {
843 46be3ddf Leszek Koltunski
        downloadThread(receiver);
844 ffe7e55d Leszek Koltunski
        }
845
      };
846 6f8e5dfc Leszek Koltunski
847 ffe7e55d Leszek Koltunski
    thread.start();
848 4f9f99a2 Leszek Koltunski
    }
849 bc511ea5 Leszek Koltunski
850
///////////////////////////////////////////////////////////////////////////////////////////////////
851
852 e4854e54 Leszek Koltunski
  public void submit(ScoresReceiver receiver, final Activity act)
853 bc511ea5 Leszek Koltunski
    {
854 ffe7e55d Leszek Koltunski
    initializeStatics();
855
    figureOutVersion(act);
856 bc511ea5 Leszek Koltunski
857 ffe7e55d Leszek Koltunski
    Thread thread = new Thread()
858
      {
859
      public void run()
860
        {
861 46be3ddf Leszek Koltunski
        submitThread(receiver);
862 ffe7e55d Leszek Koltunski
        }
863
      };
864 1780dcb0 Leszek Koltunski
865 ffe7e55d Leszek Koltunski
    thread.start();
866 bc511ea5 Leszek Koltunski
    }
867 6a083c6a Leszek Koltunski
868
///////////////////////////////////////////////////////////////////////////////////////////////////
869
870 e4854e54 Leszek Koltunski
  public void debug(final Activity act)
871 6a083c6a Leszek Koltunski
    {
872 ffe7e55d Leszek Koltunski
    initializeStatics();
873 e4854e54 Leszek Koltunski
    figureOutVersion(act);
874 63dd19c4 Leszek Koltunski
    mDebugState = DEBUG_RUNNING;
875 ffe7e55d Leszek Koltunski
876
    Thread thread = new Thread()
877
      {
878
      public void run()
879
        {
880
        debugThread();
881
        }
882
      };
883
884
    thread.start();
885 6a083c6a Leszek Koltunski
    }
886 1fa125c2 Leszek Koltunski
887
///////////////////////////////////////////////////////////////////////////////////////////////////
888
889 e4854e54 Leszek Koltunski
  public void suspicious(final String suspicious, final Activity act)
890 1fa125c2 Leszek Koltunski
    {
891 ffe7e55d Leszek Koltunski
    initializeStatics();
892 e4854e54 Leszek Koltunski
    figureOutVersion(act);
893 ffe7e55d Leszek Koltunski
894
    Thread thread = new Thread()
895
      {
896
      public void run()
897
        {
898 46be3ddf Leszek Koltunski
        suspiciousThread(suspicious);
899 ffe7e55d Leszek Koltunski
        }
900
      };
901
902
    thread.start();
903 1fa125c2 Leszek Koltunski
    }
904 63dd19c4 Leszek Koltunski
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906
// Yes it can happen that the second Updatee registers before we sent an update to the first one
907
// and, as a result, the update never gets sent to the first one. This is not a problem (now, when
908
// there are only two updatees - the RubikStatePlay and the UpdateDialog)
909
//
910
// Yes, there is also a remote possibility that the two threads executing this function and executing
911
// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee.
912
// We don't care about such remote possibility, then the app simply would signal that there are no
913
// updates available.
914
915
  public void signUpForUpdates(Updatee updatee)
916
    {
917
         if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates);
918
    else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate();
919
    else mUpdatee = updatee;
920
    }
921 903c7bbc Leszek Koltunski
922 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
923
924 7fe62d1f Leszek Koltunski
  public void downloadIcons(final Context context, final IconReceiver receiver)
925 46be3ddf Leszek Koltunski
    {
926
    initializeStatics();
927
928
    Thread thread = new Thread()
929
      {
930
      public void run()
931
        {
932 7fe62d1f Leszek Koltunski
        iconThread(context,receiver);
933 46be3ddf Leszek Koltunski
        }
934
      };
935
936
    thread.start();
937
    }
938
939
///////////////////////////////////////////////////////////////////////////////////////////////////
940
941
  public void downloadJSON(final RubikUpdates.UpdateInfo info, final Downloadee downloadee)
942 ffe7e55d Leszek Koltunski
    {
943 b88cdd91 Leszek Koltunski
    initializeStatics();
944
945
    Thread thread = new Thread()
946
      {
947
      public void run()
948
        {
949 46be3ddf Leszek Koltunski
        jsonThread(info,downloadee);
950 b88cdd91 Leszek Koltunski
        }
951
      };
952 ffe7e55d Leszek Koltunski
953 b88cdd91 Leszek Koltunski
    thread.start();
954 ffe7e55d Leszek Koltunski
    }
955
956 903c7bbc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
957
958
  public void updateDone(String shortName)
959
    {
960
    mUpdates.updateDone(shortName);
961
    }
962 b8b38548 Leszek Koltunski
}