Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikNetwork.java @ 9d4c38eb

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