Project

General

Profile

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

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

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