Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikNetwork.java @ 3f7a4363

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 6a083c6a Leszek Koltunski
package org.distorted.network;
21 fdec60a3 Leszek Koltunski
22 3f7a4363 Leszek Koltunski
import java.io.InputStream;
23
import java.net.HttpURLConnection;
24
import java.net.URL;
25
import java.net.UnknownHostException;
26
import java.security.MessageDigest;
27
import java.security.NoSuchAlgorithmException;
28
29 6a083c6a Leszek Koltunski
import android.app.Activity;
30 1780dcb0 Leszek Koltunski
import android.content.pm.PackageInfo;
31
import android.content.pm.PackageManager;
32
33 6a083c6a Leszek Koltunski
import androidx.appcompat.app.AppCompatActivity;
34 66e777b0 Leszek Koltunski
import androidx.fragment.app.FragmentActivity;
35 4895fff6 Leszek Koltunski
36 6a083c6a Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 3f7a4363 Leszek Koltunski
import org.distorted.objectlib.main.ObjectList;
38 36e2cbdd Leszek Koltunski
39 3f7a4363 Leszek Koltunski
import static org.distorted.objectlib.main.ObjectList.MAX_LEVEL;
40 211b48f2 Leszek Koltunski
41 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 6a083c6a Leszek Koltunski
public class RubikNetwork implements Runnable
44 fdec60a3 Leszek Koltunski
  {
45 211b48f2 Leszek Koltunski
  public interface Receiver
46 b8b38548 Leszek Koltunski
    {
47 1c90c64a Leszek Koltunski
    void receive(String[][][] country, String[][][] name, float[][][] time);
48 4895fff6 Leszek Koltunski
    void message(String mess);
49 4c0cd600 Leszek Koltunski
    void error(String error);
50 b8b38548 Leszek Koltunski
    }
51
52 286d73ae Leszek Koltunski
  public static final int MAX_PLACES = 10;
53 d8aa4ba8 Leszek Koltunski
54 b8b38548 Leszek Koltunski
  private static final int DOWNLOAD   = 0;
55
  private static final int SUBMIT     = 1;
56 6a083c6a Leszek Koltunski
  private static final int DEBUG      = 2;
57
  private static final int IDLE       = 3;
58 b8b38548 Leszek Koltunski
59 14f14138 Leszek Koltunski
  private static final int REND_ADRENO= 0;
60
  private static final int REND_MALI  = 1;
61
  private static final int REND_POWER = 2;
62
  private static final int REND_OTHER = 3;
63 9d4c38eb Leszek Koltunski
64 36e2cbdd Leszek Koltunski
  private final String[] hex = {
65
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
66
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
67
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
68
    "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
69
    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
70
    "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
71
    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
72
    "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
73
    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
74
    "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
75
    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
76
    "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
77
    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
78
    "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
79
    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
80
    "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
81
    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
82
    "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
83
    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
84
    "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
85
    "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
86
    "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
87
    "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
88
    "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
89
    "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
90
    "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
91
    "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
92
    "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
93
    "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
94
    "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
95
    "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
96
    "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
97
    };
98
99 a36b9dca Leszek Koltunski
  private static int mTotal = 0;
100
  private static String[][][] mCountry;
101
  private static String[][][] mName;
102
  private static float[][][] mTime;
103
  private static int[][] mPlaces;
104 688f7712 Leszek Koltunski
105 6a083c6a Leszek Koltunski
  private static RubikNetwork mThis;
106 688f7712 Leszek Koltunski
  private static String mScores = "";
107 b8b38548 Leszek Koltunski
  private static boolean mRunning = false;
108
  private static int mMode = IDLE;
109
  private static Receiver mReceiver;
110 4895fff6 Leszek Koltunski
  private static String mVersion;
111 b8b38548 Leszek Koltunski
112 a36b9dca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
  private static void initializeStatics()
115
    {
116
    if( mTotal==0      ) mTotal   = ObjectList.getTotal();
117
    if( mCountry==null ) mCountry = new String[mTotal][MAX_LEVEL][MAX_PLACES];
118
    if( mName==null    ) mName    = new String[mTotal][MAX_LEVEL][MAX_PLACES];
119
    if( mTime==null    ) mTime    = new  float[mTotal][MAX_LEVEL][MAX_PLACES];
120
    if( mPlaces==null  ) mPlaces  = new int[mTotal][MAX_LEVEL];
121
    }
122
123 4895fff6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125 86cbdab1 Leszek Koltunski
  private static String computeHash(String stringToHash, byte[] salt)
126 4895fff6 Leszek Koltunski
    {
127 86cbdab1 Leszek Koltunski
    String generatedPassword;
128
129
    try
130
      {
131
      MessageDigest md = MessageDigest.getInstance("MD5");
132
      md.update(salt);
133
      byte[] bytes = md.digest(stringToHash.getBytes());
134
      StringBuilder sb = new StringBuilder();
135
136
      for (byte aByte : bytes)
137
        {
138
        sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
139
        }
140
141
      generatedPassword = sb.toString();
142
      }
143
    catch (NoSuchAlgorithmException e)
144
      {
145
      return "NoSuchAlgorithm";
146
      }
147
148
    return generatedPassword;
149 4895fff6 Leszek Koltunski
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153 17f9a695 Leszek Koltunski
  private boolean fillValues()
154 f3e12931 Leszek Koltunski
    {
155 d8aa4ba8 Leszek Koltunski
    int begin=-1 ,end, len = mScores.length();
156 17f9a695 Leszek Koltunski
    String row;
157 f3e12931 Leszek Koltunski
158 4c0cd600 Leszek Koltunski
    if( len==0 )
159
      {
160
      mReceiver.error("1");
161
      return false;
162
      }
163 90fd47b0 Leszek Koltunski
    else if( len<=2 )
164 4c0cd600 Leszek Koltunski
      {
165
      mReceiver.error(mScores);
166
      return false;
167
      }
168
169 4888e97c Leszek Koltunski
    for(int i=0; i<mTotal; i++)
170 85b09df4 Leszek Koltunski
      for(int j=0; j<MAX_LEVEL; j++)
171 329c0aeb Leszek Koltunski
        {
172
        mPlaces[i][j] = 0;
173
        }
174
175 d8aa4ba8 Leszek Koltunski
    while( begin<len )
176
      {
177
      end = mScores.indexOf('\n', begin+1);
178
      if( end<0 ) end = len;
179 286d73ae Leszek Koltunski
180
      try
181
        {
182 17f9a695 Leszek Koltunski
        row = mScores.substring(begin+1,end);
183 4c0cd600 Leszek Koltunski
        fillRow(row);
184 286d73ae Leszek Koltunski
        }
185
      catch(Exception ex)
186
        {
187
        // faulty row - ignore
188
        }
189
190 d8aa4ba8 Leszek Koltunski
      begin = end;
191
      }
192 17f9a695 Leszek Koltunski
193
    return true;
194 f3e12931 Leszek Koltunski
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 d8aa4ba8 Leszek Koltunski
  private void fillRow(String row)
199 f3e12931 Leszek Koltunski
    {
200 d8aa4ba8 Leszek Koltunski
    int s1 = row.indexOf(' ');
201
    int s2 = row.indexOf(' ',s1+1);
202
    int s3 = row.indexOf(' ',s2+1);
203
    int s4 = row.indexOf(' ',s3+1);
204 286d73ae Leszek Koltunski
    int s5 = row.length();
205 d8aa4ba8 Leszek Koltunski
206
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
207
      {
208 9c2f0c91 Leszek Koltunski
      int object = ObjectList.unpackObjectFromString( row.substring(0,s1) );
209 b8b38548 Leszek Koltunski
210 286d73ae Leszek Koltunski
      if( object>=0 && object<mTotal )
211 d8aa4ba8 Leszek Koltunski
        {
212 8e3898c8 Leszek Koltunski
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
213 286d73ae Leszek Koltunski
        String name    = row.substring(s2+1, s3);
214
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
215
        String country = row.substring(s4+1, s5);
216 d8aa4ba8 Leszek Koltunski
217 c8249cf6 Leszek Koltunski
        if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
218
219 8e3898c8 Leszek Koltunski
        if(level>=0 && level<MAX_LEVEL)
220 d8aa4ba8 Leszek Koltunski
          {
221 8e3898c8 Leszek Koltunski
          int p = mPlaces[object][level];
222
          mPlaces[object][level]++;
223 329c0aeb Leszek Koltunski
224 8e3898c8 Leszek Koltunski
          mCountry[object][level][p] = country;
225
          mName   [object][level][p] = name;
226 79e752b3 Leszek Koltunski
          mTime   [object][level][p] = ((float)(time/10))/100.0f;
227 d8aa4ba8 Leszek Koltunski
          }
228
        }
229
      }
230 874c37b1 Leszek Koltunski
    else
231
      {
232 5d50e7a7 Leszek Koltunski
      tryDoCommand(row);
233
      }
234
    }
235
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238
  private void tryDoCommand(String row)
239
    {
240
    if( row.startsWith("comm") )
241
      {
242
      int colon = row.indexOf(':');
243
244
      if( colon>0 )
245 874c37b1 Leszek Koltunski
        {
246 5d50e7a7 Leszek Koltunski
        String commandNumber = row.substring(4,colon);
247
        int number;
248
249
        try
250
          {
251
          number = Integer.parseInt(commandNumber);
252
          }
253
        catch(NumberFormatException ex)
254
          {
255
          number=0;
256
          }
257 874c37b1 Leszek Koltunski
258 5d50e7a7 Leszek Koltunski
        if(number==1)
259 874c37b1 Leszek Koltunski
          {
260
          String country = row.substring(colon+1);
261
          RubikScores scores = RubikScores.getInstance();
262
          scores.setCountry(country);
263
          }
264
        }
265
      }
266 f3e12931 Leszek Koltunski
    }
267
268 9d4c38eb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
  private int getRendererType(String renderer)
271
    {
272
    if( renderer.contains("Adreno")  ) return REND_ADRENO;
273
    if( renderer.contains("Mali")    ) return REND_MALI;
274
    if( renderer.contains("PowerVR") ) return REND_POWER;
275
276
    return REND_OTHER;
277
    }
278
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280
281
  private String parseRenderer(final int type, String renderer)
282
    {
283
    if( type==REND_ADRENO || type==REND_POWER )
284
      {
285
      int lastSpace = renderer.lastIndexOf(' ');
286
      String ret = renderer.substring(lastSpace+1);
287
      return URLencode(ret);
288
      }
289
290
    if( type==REND_MALI )
291
      {
292
      int firstHyphen = renderer.indexOf('-');
293
      String ret = renderer.substring(firstHyphen+1);
294
      return URLencode(ret);
295
      }
296
297
    return "other";
298
    }
299
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
302
  private String parseVersion(final int type, String version)
303
    {
304
    switch(type)
305
      {
306
      case REND_ADRENO: int aMonkey = version.indexOf('@');
307
                        int aDot = version.indexOf('.', aMonkey);
308
                        String ret1 = aDot>=3 ? version.substring(aDot-3,aDot) : "";
309
                        return URLencode(ret1);
310
      case REND_MALI  : int mV1 = version.indexOf("v1");
311
                        int mHyphen = version.indexOf('-', mV1);
312
                        String ret2 = mHyphen>mV1+3 && mV1>=0 ? version.substring(mV1+3,mHyphen) : "";
313
                        return URLencode(ret2);
314
      case REND_POWER : int pMonkey = version.indexOf('@');
315
                        int pSpace  = version.lastIndexOf(' ');
316
                        String ret3 = pSpace>=0 && pMonkey>pSpace+1 ? version.substring(pSpace+1,pMonkey) : "";
317
                        return URLencode(ret3);
318
      default         : return "";
319
      }
320
    }
321
322 36e2cbdd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324
  private String URLencode(String s)
325
    {
326
    StringBuilder sbuf = new StringBuilder();
327
    int len = s.length();
328
329
    for (int i = 0; i < len; i++)
330
      {
331
      int ch = s.charAt(i);
332
333
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
334
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
335
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
336
      else if (ch == ' '             ) sbuf.append('+');
337
      else if (ch == '-' || ch == '_'
338
            || ch == '.' || ch == '!'
339
            || ch == '~' || ch == '*'
340
            || ch == '\'' || ch == '('
341
            || ch == ')'             ) sbuf.append((char)ch);
342
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
343
      else if (ch <= 0x07FF)
344
        {
345
        sbuf.append(hex[0xc0 | (ch >> 6)]);
346
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
347
        }
348
      else
349
        {
350
        sbuf.append(hex[0xe0 | (ch >> 12)]);
351
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
352
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
353
        }
354
      }
355
356
    return sbuf.toString();
357
    }
358
359 6a083c6a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
360
361
  private void sendDebug()
362
    {
363
    String url = constructDebugURL();
364
365
    try
366
      {
367
      java.net.URL connectURL = new URL(url);
368
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
369
370
      conn.setDoInput(true);
371
      conn.setDoOutput(true);
372
      conn.setUseCaches(false);
373
      conn.setRequestMethod("GET");
374
      conn.connect();
375
      conn.getOutputStream().flush();
376
      conn.getInputStream();
377
      }
378
    catch( final Exception e )
379
      {
380
      // ignore
381
      }
382
    }
383
384 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
385
386 4895fff6 Leszek Koltunski
  private boolean network(String url)
387 f3e12931 Leszek Koltunski
    {
388 36e2cbdd Leszek Koltunski
    try
389
      {
390 4895fff6 Leszek Koltunski
      java.net.URL connectURL = new URL(url);
391 36e2cbdd Leszek Koltunski
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
392
393
      conn.setDoInput(true);
394
      conn.setDoOutput(true);
395
      conn.setUseCaches(false);
396
      conn.setRequestMethod("GET");
397
      conn.connect();
398
      conn.getOutputStream().flush();
399
400 a675474f Leszek Koltunski
      try( InputStream is = conn.getInputStream() )
401 36e2cbdd Leszek Koltunski
        {
402
        int ch;
403
        StringBuilder sb = new StringBuilder();
404
        while( ( ch = is.read() ) != -1 )
405
          {
406
          sb.append( (char)ch );
407
          }
408
        mScores = sb.toString();
409
        }
410 cc5ec229 Leszek Koltunski
      catch( final Exception e)
411 36e2cbdd Leszek Koltunski
        {
412 4895fff6 Leszek Koltunski
        mReceiver.message("Failed to get an answer from the High Scores server");
413 a675474f Leszek Koltunski
        return false;
414 36e2cbdd Leszek Koltunski
        }
415
      }
416 cc5ec229 Leszek Koltunski
    catch( final UnknownHostException e )
417
      {
418 4895fff6 Leszek Koltunski
      mReceiver.message("No access to Internet");
419 cc5ec229 Leszek Koltunski
      return false;
420
      }
421
    catch( final SecurityException e )
422
      {
423 4895fff6 Leszek Koltunski
      mReceiver.message("Application not authorized to connect to the Internet");
424 cc5ec229 Leszek Koltunski
      return false;
425
      }
426
    catch( final Exception e )
427 36e2cbdd Leszek Koltunski
      {
428 4895fff6 Leszek Koltunski
      mReceiver.message(e.getMessage());
429 a675474f Leszek Koltunski
      return false;
430 36e2cbdd Leszek Koltunski
      }
431
432 329c0aeb Leszek Koltunski
    if( mScores.length()==0 )
433
      {
434 4895fff6 Leszek Koltunski
      mReceiver.message("Failed to download scores");
435 329c0aeb Leszek Koltunski
      return false;
436
      }
437
438 a675474f Leszek Koltunski
    return true;
439 b8b38548 Leszek Koltunski
    }
440 d8aa4ba8 Leszek Koltunski
441 6a083c6a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
442
443
  private String constructDebugURL()
444
    {
445
    RubikScores scores = RubikScores.getInstance();
446
    String name = URLencode(scores.getName());
447
    int numRuns = scores.getNumRuns();
448
    int numPlay = scores.getNumPlays();
449
    String country = scores.getCountry();
450
    String renderer = DistortedLibrary.getDriverRenderer();
451
    String version  = DistortedLibrary.getDriverVersion();
452
453
    renderer = URLencode(renderer);
454
    version  = URLencode(version);
455
456
    String url="https://distorted.org/magic/cgi-bin/debugs.cgi";
457
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d"+"&d="+renderer+"&v="+version;
458
459
    return url;
460
    }
461
462 17f9a695 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
463
464
  private String constructDownloadURL()
465
    {
466
    RubikScores scores = RubikScores.getInstance();
467
    String name = URLencode(scores.getName());
468
    String veri = scores.isVerified() ? name : "";
469
    int numRuns = scores.getNumRuns();
470
    int numPlay = scores.getNumPlays();
471 874c37b1 Leszek Koltunski
    String country = scores.getCountry();
472 17f9a695 Leszek Koltunski
473
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
474 874c37b1 Leszek Koltunski
    url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
475 9c2f0c91 Leszek Koltunski
    url += "&o="+ ObjectList.getObjectList()+"&min=0&max="+MAX_LEVEL+"&l="+MAX_PLACES;
476 17f9a695 Leszek Koltunski
477
    return url;
478
    }
479
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481
482
  private String constructSubmitURL()
483
    {
484
    RubikScores scores = RubikScores.getInstance();
485
    String name = URLencode(scores.getName());
486
    String veri = scores.isVerified() ? name : "";
487
    int numRuns = scores.getNumRuns();
488
    int numPlay = scores.getNumPlays();
489
    int deviceID= scores.getDeviceID();
490 6570171b Leszek Koltunski
    String reclist = scores.getRecordList("&o=","&l=","&t=");
491 17f9a695 Leszek Koltunski
    String country = scores.getCountry();
492 86cbdab1 Leszek Koltunski
    long epoch = System.currentTimeMillis();
493 57b4f567 Leszek Koltunski
    String salt = "cuboid";
494 17f9a695 Leszek Koltunski
495 9d4c38eb Leszek Koltunski
    String renderer = DistortedLibrary.getDriverRenderer();
496
    String version  = DistortedLibrary.getDriverVersion();
497
498
    int type = getRendererType(renderer);
499
    renderer = parseRenderer(type,renderer);
500
    version  = parseVersion(type,version);
501
502 86cbdab1 Leszek Koltunski
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
503
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d";
504 9d4c38eb Leszek Koltunski
    url2 += "&d="+renderer+"&s="+version;
505 6570171b Leszek Koltunski
    url2 += reclist+"&c="+country+"&f="+epoch+"&oo="+ ObjectList.getObjectList();
506 b3ee0e78 Leszek Koltunski
    url2 += "&min=0&max="+MAX_LEVEL+"&lo="+MAX_PLACES;
507 57b4f567 Leszek Koltunski
    String hash = computeHash( url2, salt.getBytes() );
508 17f9a695 Leszek Koltunski
509 b3ee0e78 Leszek Koltunski
    return url1 + "?" + url2 + "&h=" + hash;
510 17f9a695 Leszek Koltunski
    }
511
512 d8aa4ba8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
513
514
  private boolean gottaDownload()
515 b8b38548 Leszek Koltunski
    {
516 d8aa4ba8 Leszek Koltunski
    return ((mScores.length()==0) && !mRunning);
517 f3e12931 Leszek Koltunski
    }
518 b8b38548 Leszek Koltunski
519 d8aa4ba8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
520
521 4f9f99a2 Leszek Koltunski
  @Override
522 d8aa4ba8 Leszek Koltunski
  public void run()
523
    {
524 6a083c6a Leszek Koltunski
    boolean receiveValues=true;
525 a675474f Leszek Koltunski
526 a36b9dca Leszek Koltunski
    initializeStatics();
527
528 17f9a695 Leszek Koltunski
    try
529 d8aa4ba8 Leszek Koltunski
      {
530 17f9a695 Leszek Koltunski
      if( mMode==DOWNLOAD && gottaDownload() )
531 d8aa4ba8 Leszek Koltunski
        {
532 17f9a695 Leszek Koltunski
        mRunning = true;
533 6a083c6a Leszek Koltunski
        receiveValues = network(constructDownloadURL());
534 d8aa4ba8 Leszek Koltunski
        }
535 17f9a695 Leszek Koltunski
      if( mMode==SUBMIT )
536 4895fff6 Leszek Koltunski
        {
537 17f9a695 Leszek Koltunski
        mRunning = true;
538 4895fff6 Leszek Koltunski
539 17f9a695 Leszek Koltunski
        if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
540
          {
541 6a083c6a Leszek Koltunski
          receiveValues = network(constructSubmitURL());
542 17f9a695 Leszek Koltunski
          }
543
        }
544 6a083c6a Leszek Koltunski
      if( mMode==DEBUG )
545
        {
546
        sendDebug();
547
        receiveValues = false;
548
        mRunning = false;
549
        }
550 d8aa4ba8 Leszek Koltunski
      }
551 17f9a695 Leszek Koltunski
    catch( Exception e )
552 d8aa4ba8 Leszek Koltunski
      {
553 6a083c6a Leszek Koltunski
      if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
554 4895fff6 Leszek Koltunski
      }
555 286d73ae Leszek Koltunski
556 17f9a695 Leszek Koltunski
    if( mRunning )
557
      {
558 6a083c6a Leszek Koltunski
      receiveValues = fillValues();
559 17f9a695 Leszek Koltunski
      mRunning = false;
560
      }
561 a675474f Leszek Koltunski
562 6a083c6a Leszek Koltunski
    if( receiveValues )
563 a675474f Leszek Koltunski
      {
564 6a083c6a Leszek Koltunski
      if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
565 17f9a695 Leszek Koltunski
566
      if( mMode==SUBMIT )
567
        {
568
        RubikScores.getInstance().successfulSubmit();
569
        }
570 a675474f Leszek Koltunski
      }
571 d8aa4ba8 Leszek Koltunski
    }
572 4f9f99a2 Leszek Koltunski
573 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
574
575 6a083c6a Leszek Koltunski
  private RubikNetwork()
576 bc511ea5 Leszek Koltunski
    {
577
578
    }
579
580 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
581
// PUBLIC API
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583
584
  public static void onPause()
585
    {
586
    mRunning = false;
587
    }
588
589 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
590
591 6a083c6a Leszek Koltunski
  public static RubikNetwork getInstance()
592 bc511ea5 Leszek Koltunski
    {
593
    if( mThis==null )
594
      {
595 6a083c6a Leszek Koltunski
      mThis = new RubikNetwork();
596 bc511ea5 Leszek Koltunski
      }
597
598
    return mThis;
599
    }
600
601 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
602
603 6a083c6a Leszek Koltunski
  private void start(Receiver receiver, Activity act, int mode)
604 4f9f99a2 Leszek Koltunski
    {
605
    mReceiver = receiver;
606 1780dcb0 Leszek Koltunski
    mMode     = mode;
607
608
    try
609
      {
610
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
611
      mVersion = pInfo.versionName;
612
      }
613
    catch (PackageManager.NameNotFoundException e)
614
      {
615 6570171b Leszek Koltunski
      mVersion = "0.9.2";
616 1780dcb0 Leszek Koltunski
      }
617 6f8e5dfc Leszek Koltunski
618 4f9f99a2 Leszek Koltunski
    Thread networkThrd = new Thread(this);
619
    networkThrd.start();
620
    }
621 bc511ea5 Leszek Koltunski
622
///////////////////////////////////////////////////////////////////////////////////////////////////
623
624 1780dcb0 Leszek Koltunski
  public void download(Receiver receiver, FragmentActivity act)
625 bc511ea5 Leszek Koltunski
    {
626 1780dcb0 Leszek Koltunski
    start(receiver, act, DOWNLOAD);
627
    }
628 bc511ea5 Leszek Koltunski
629 1780dcb0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
630
631
  public void submit(Receiver receiver, FragmentActivity act)
632
    {
633
    start(receiver, act, SUBMIT);
634 bc511ea5 Leszek Koltunski
    }
635 6a083c6a Leszek Koltunski
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637
638
  public void debug(AppCompatActivity act)
639
    {
640
    start(null, act, DEBUG);
641
    }
642 b8b38548 Leszek Koltunski
}