Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikNetwork.java @ a83fae26

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.network;
21

    
22
import java.io.BufferedReader;
23
import java.io.InputStream;
24
import java.io.InputStreamReader;
25
import java.net.HttpURLConnection;
26
import java.net.URL;
27
import java.net.UnknownHostException;
28
import java.security.MessageDigest;
29
import java.security.NoSuchAlgorithmException;
30

    
31
import android.app.Activity;
32
import android.content.pm.PackageInfo;
33
import android.content.pm.PackageManager;
34

    
35
import androidx.appcompat.app.AppCompatActivity;
36
import androidx.fragment.app.FragmentActivity;
37

    
38
import org.distorted.library.main.DistortedLibrary;
39
import org.distorted.objectlib.json.JsonWriter;
40
import org.distorted.objects.RubikObjectList;
41

    
42
import static org.distorted.objects.RubikObjectList.MAX_LEVEL;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class RubikNetwork implements Runnable
47
  {
48
  public interface Receiver
49
    {
50
    void receive(String[][][] country, String[][][] name, float[][][] time);
51
    void message(String mess);
52
    void error(String error);
53
    }
54

    
55
  public interface Updatee
56
    {
57
    void receiveUpdate(RubikUpdates update);
58
    void errorUpdate();
59
    }
60

    
61
  public static final int MAX_PLACES = 10;
62

    
63
  private static final int DOWNLOAD   = 0;
64
  private static final int SUBMIT     = 1;
65
  private static final int DEBUG      = 2;
66
  private static final int SUSPICIOUS = 3;
67
  private static final int IDLE       = 4;
68

    
69
  private static final int REND_ADRENO= 0;
70
  private static final int REND_MALI  = 1;
71
  private static final int REND_POWER = 2;
72
  private static final int REND_OTHER = 3;
73

    
74
  private static final int DEBUG_NOT_YET = 0;
75
  private static final int DEBUG_RUNNING = 1;
76
  private static final int DEBUG_SUCCESS = 2;
77
  private static final int DEBUG_FAILURE = 3;
78

    
79
  private final String[] hex = {
80
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
81
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
82
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
83
    "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
84
    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
85
    "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
86
    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
87
    "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
88
    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
89
    "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
90
    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
91
    "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
92
    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
93
    "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
94
    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
95
    "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
96
    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
97
    "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
98
    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
99
    "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
100
    "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
101
    "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
102
    "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
103
    "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
104
    "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
105
    "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
106
    "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
107
    "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
108
    "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
109
    "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
110
    "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
111
    "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
112
    };
113

    
114
  private static String[][][] mCountry;
115
  private static String[][][] mName;
116
  private static float[][][] mTime;
117
  private static int[][] mPlaces;
118

    
119
  private static RubikNetwork mThis;
120
  private static String mScores = "";
121
  private static boolean mRunning = false;
122
  private static int mMode = IDLE;
123
  private static Receiver mReceiver;
124
  private static Updatee mUpdatee;
125
  private static String mVersion;
126
  private static String mSuspicious;
127
  private static int mNumObjects;
128
  private static RubikUpdates mUpdates;
129
  private static int mDebugState;
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  private static void initializeStatics()
134
    {
135
    int newNum = RubikObjectList.getNumObjects();
136

    
137
    if( mCountry==null || newNum!=mNumObjects ) mCountry = new String[newNum][MAX_LEVEL][MAX_PLACES];
138
    if( mName==null    || newNum!=mNumObjects ) mName    = new String[newNum][MAX_LEVEL][MAX_PLACES];
139
    if( mTime==null    || newNum!=mNumObjects ) mTime    = new  float[newNum][MAX_LEVEL][MAX_PLACES];
140
    if( mPlaces==null  || newNum!=mNumObjects ) mPlaces  = new    int[newNum][MAX_LEVEL];
141

    
142
    if( mUpdates==null ) mUpdates = new RubikUpdates();
143

    
144
    mNumObjects = newNum;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private static String computeHash(String stringToHash, byte[] salt)
150
    {
151
    String generatedPassword;
152

    
153
    try
154
      {
155
      MessageDigest md = MessageDigest.getInstance("MD5");
156
      md.update(salt);
157
      byte[] bytes = md.digest(stringToHash.getBytes());
158
      StringBuilder sb = new StringBuilder();
159

    
160
      for (byte aByte : bytes)
161
        {
162
        sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
163
        }
164

    
165
      generatedPassword = sb.toString();
166
      }
167
    catch (NoSuchAlgorithmException e)
168
      {
169
      return "NoSuchAlgorithm";
170
      }
171

    
172
    return generatedPassword;
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  private boolean fillValuesNormal()
178
    {
179
    int begin=-1 ,end, len = mScores.length();
180
    String row;
181

    
182
    if( len==0 )
183
      {
184
      if( mReceiver!=null ) mReceiver.error("1");
185
      return false;
186
      }
187
    else if( len<=2 )
188
      {
189
      if( mReceiver!=null ) mReceiver.error(mScores);
190
      return false;
191
      }
192

    
193
    for(int i=0; i<mNumObjects; i++)
194
      for(int j=0; j<MAX_LEVEL; j++)
195
        {
196
        mPlaces[i][j] = 0;
197
        }
198

    
199
    while( begin<len )
200
      {
201
      end = mScores.indexOf('\n', begin+1);
202
      if( end<0 ) end = len;
203

    
204
      try
205
        {
206
        row = mScores.substring(begin+1,end);
207
        fillRow(row);
208
        }
209
      catch(Exception ex)
210
        {
211
        // faulty row - ignore
212
        }
213

    
214
      begin = end;
215
      }
216

    
217
    return true;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  private void fillRow(String row)
223
    {
224
    int s1 = row.indexOf(' ');
225
    int s2 = row.indexOf(' ',s1+1);
226
    int s3 = row.indexOf(' ',s2+1);
227
    int s4 = row.indexOf(' ',s3+1);
228
    int s5 = row.length();
229

    
230
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
231
      {
232
      int object = RubikObjectList.getOrdinal( row.substring(0,s1) );
233

    
234
      if( object>=0 && object<mNumObjects )
235
        {
236
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
237
        String name    = row.substring(s2+1, s3);
238
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
239
        String country = row.substring(s4+1, s5);
240

    
241
        if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
242

    
243
        if(level>=0 && level<MAX_LEVEL)
244
          {
245
          int p = mPlaces[object][level];
246
          mPlaces[object][level]++;
247

    
248
          mCountry[object][level][p] = country;
249
          mName   [object][level][p] = name;
250
          mTime   [object][level][p] = ((float)(time/10))/100.0f;
251
          }
252
        }
253
      }
254
    else
255
      {
256
      tryDoCommand(row);
257
      }
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  private void tryDoCommand(String row)
263
    {
264
    if( row.startsWith("comm") )
265
      {
266
      int colon = row.indexOf(':');
267

    
268
      if( colon>0 )
269
        {
270
        String commandNumber = row.substring(4,colon);
271
        int number;
272

    
273
        try
274
          {
275
          number = Integer.parseInt(commandNumber);
276
          }
277
        catch(NumberFormatException ex)
278
          {
279
          number=0;
280
          }
281

    
282
        if(number==1)
283
          {
284
          String country = row.substring(colon+1);
285
          RubikScores scores = RubikScores.getInstance();
286
          scores.setCountry(country);
287
          }
288
        }
289
      }
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  private int getRendererType(String renderer)
295
    {
296
    if( renderer.contains("Adreno")  ) return REND_ADRENO;
297
    if( renderer.contains("Mali")    ) return REND_MALI;
298
    if( renderer.contains("PowerVR") ) return REND_POWER;
299

    
300
    return REND_OTHER;
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  private String parseRenderer(final int type, String renderer)
306
    {
307
    if( type==REND_ADRENO || type==REND_POWER )
308
      {
309
      int lastSpace = renderer.lastIndexOf(' ');
310
      String ret = renderer.substring(lastSpace+1);
311
      return URLencode(ret);
312
      }
313

    
314
    if( type==REND_MALI )
315
      {
316
      int firstHyphen = renderer.indexOf('-');
317
      String ret = renderer.substring(firstHyphen+1);
318
      return URLencode(ret);
319
      }
320

    
321
    return "other";
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  private String parseVersion(final int type, String version)
327
    {
328
    switch(type)
329
      {
330
      case REND_ADRENO: int aMonkey = version.indexOf('@');
331
                        int aDot = version.indexOf('.', aMonkey);
332
                        String ret1 = aDot>=3 ? version.substring(aDot-3,aDot) : "";
333
                        return URLencode(ret1);
334
      case REND_MALI  : int mV1 = version.indexOf("v1");
335
                        int mHyphen = version.indexOf('-', mV1);
336
                        String ret2 = mHyphen>mV1+3 && mV1>=0 ? version.substring(mV1+3,mHyphen) : "";
337
                        return URLencode(ret2);
338
      case REND_POWER : int pMonkey = version.indexOf('@');
339
                        int pSpace  = version.lastIndexOf(' ');
340
                        String ret3 = pSpace>=0 && pMonkey>pSpace+1 ? version.substring(pSpace+1,pMonkey) : "";
341
                        return URLencode(ret3);
342
      default         : return "";
343
      }
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  private String URLencode(String s)
349
    {
350
    StringBuilder sbuf = new StringBuilder();
351
    int len = s.length();
352

    
353
    for (int i = 0; i < len; i++)
354
      {
355
      int ch = s.charAt(i);
356

    
357
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
358
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
359
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
360
      else if (ch == ' '             ) sbuf.append('+');
361
      else if (ch == '-' || ch == '_'
362
            || ch == '.' || ch == '!'
363
            || ch == '~' || ch == '*'
364
            || ch == '\'' || ch == '('
365
            || ch == ')'             ) sbuf.append((char)ch);
366
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
367
      else if (ch <= 0x07FF)
368
        {
369
        sbuf.append(hex[0xc0 | (ch >> 6)]);
370
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
371
        }
372
      else
373
        {
374
        sbuf.append(hex[0xe0 | (ch >> 12)]);
375
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
376
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
377
        }
378
      }
379

    
380
    return sbuf.toString();
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
  private void sendDebug()
386
    {
387
    String url = constructDebugURL();
388
/*
389
    try { Thread.sleep(5000); }
390
    catch( InterruptedException ignored) {}
391
*/
392
    try
393
      {
394
      java.net.URL connectURL = new URL(url);
395
      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
      InputStream is = conn.getInputStream();
405
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
406
      StringBuilder answer = new StringBuilder();
407

    
408
      for (String line; (line = r.readLine()) != null; )
409
        {
410
        answer.append(line).append('\n');
411
        }
412

    
413
      String updates = answer.toString();
414
      mUpdates.parse(updates);
415
      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
416
      mDebugState = DEBUG_SUCCESS;
417
      }
418
    catch( final Exception e )
419
      {
420
      if( mUpdatee!=null ) mUpdatee.errorUpdate();
421
      mDebugState = DEBUG_FAILURE;
422
      }
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
  private void sendSuspicious()
428
    {
429
    String url = constructSuspiciousURL();
430

    
431
    try
432
      {
433
      java.net.URL connectURL = new URL(url);
434
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
435

    
436
      conn.setDoInput(true);
437
      conn.setDoOutput(true);
438
      conn.setUseCaches(false);
439
      conn.setRequestMethod("GET");
440
      conn.connect();
441
      conn.getOutputStream().flush();
442
      conn.getInputStream();
443
      }
444
    catch( final Exception e )
445
      {
446
      // ignore
447
      }
448
    }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
  private boolean network(String url)
453
    {
454
    try
455
      {
456
      java.net.URL connectURL = new URL(url);
457
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
458

    
459
      conn.setDoInput(true);
460
      conn.setDoOutput(true);
461
      conn.setUseCaches(false);
462
      conn.setRequestMethod("GET");
463
      conn.connect();
464
      conn.getOutputStream().flush();
465

    
466
      InputStream is = conn.getInputStream();
467
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
468
      StringBuilder total = new StringBuilder();
469

    
470
      for (String line; (line = r.readLine()) != null; )
471
        {
472
        total.append(line).append('\n');
473
        }
474

    
475
      mScores = total.toString();
476
      }
477
    catch( final UnknownHostException e )
478
      {
479
      if( mReceiver!=null ) mReceiver.message("No access to Internet");
480
      return false;
481
      }
482
    catch( final SecurityException e )
483
      {
484
      if( mReceiver!=null ) mReceiver.message("Application not authorized to connect to the Internet");
485
      return false;
486
      }
487
    catch( final Exception e )
488
      {
489
      if( mReceiver!=null ) mReceiver.message(e.getMessage());
490
      return false;
491
      }
492

    
493
    if( mScores.length()==0 )
494
      {
495
      if( mReceiver!=null ) mReceiver.message("Failed to download scores");
496
      return false;
497
      }
498

    
499
    return true;
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
  private String constructSuspiciousURL()
505
    {
506
    RubikScores scores = RubikScores.getInstance();
507
    int deviceID= scores.getDeviceID();
508
    String suspicious = URLencode(mSuspicious);
509

    
510
    String url="https://distorted.org/magic/cgi-bin/suspicious.cgi";
511
    url += "?i="+deviceID+"&d="+suspicious;
512

    
513
    return url;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  private String constructDebugURL()
519
    {
520
    RubikScores scores = RubikScores.getInstance();
521
    String name = URLencode(scores.getName());
522
    int numRuns = scores.getNumRuns();
523
    int numPlay = scores.getNumPlays();
524
    String country = scores.getCountry();
525
    String renderer = DistortedLibrary.getDriverRenderer();
526
    String version  = DistortedLibrary.getDriverVersion();
527
    int objectAPI   = JsonWriter.VERSION_OBJECT_MAJOR;
528
    int tutorialAPI = JsonWriter.VERSION_EXTRAS_MAJOR;
529

    
530
    renderer = URLencode(renderer);
531
    version  = URLencode(version);
532

    
533
    String url="https://distorted.org/magic/cgi-bin/debugs-new.cgi";
534
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
535
    url += "&d="+renderer+"&v="+version+"&a="+objectAPI+"&b="+tutorialAPI;
536

    
537
    return url;
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
  private String constructDownloadURL()
543
    {
544
    RubikScores scores = RubikScores.getInstance();
545
    String name = URLencode(scores.getName());
546
    int numRuns = scores.getNumRuns();
547
    int numPlay = scores.getNumPlays();
548
    String country = scores.getCountry();
549

    
550
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
551
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion;
552

    
553
    return url;
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
  private String constructSubmitURL()
559
    {
560
    RubikScores scores = RubikScores.getInstance();
561
    String name = URLencode(scores.getName());
562
    String veri = scores.isVerified() ? "1" : "";
563
    int numRuns = scores.getNumRuns();
564
    int numPlay = scores.getNumPlays();
565
    int deviceID= scores.getDeviceID();
566
    String reclist = scores.getRecordList("&o=","&l=","&t=");
567
    String country = scores.getCountry();
568
    long epoch = System.currentTimeMillis();
569
    String salt = "cuboid";
570

    
571
    String renderer = DistortedLibrary.getDriverRenderer();
572
    String version  = DistortedLibrary.getDriverVersion();
573

    
574
    int type = getRendererType(renderer);
575
    renderer = parseRenderer(type,renderer);
576
    version  = parseVersion(type,version);
577

    
578
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
579
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion;
580
    url2 += "&d="+renderer+"&s="+version+reclist+"&c="+country+"&f="+epoch;
581
    String hash = computeHash( url2, salt.getBytes() );
582

    
583
    return url1 + "?" + url2 + "&h=" + hash;
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587

    
588
  private boolean gottaDownload()
589
    {
590
    return ((mScores.length()==0) && !mRunning);
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
  @Override
596
  public void run()
597
    {
598
    boolean receiveValues=true;
599

    
600
    initializeStatics();
601

    
602
    try
603
      {
604
      if( mMode==DOWNLOAD && gottaDownload() )
605
        {
606
        mRunning = true;
607
        receiveValues = network(constructDownloadURL());
608
        }
609
      if( mMode==SUBMIT )
610
        {
611
        mRunning = true;
612
        RubikScores scores = RubikScores.getInstance();
613

    
614
        if( scores.thereAreUnsubmittedRecords() )
615
          {
616
          receiveValues = network(constructSubmitURL());
617
          }
618
        }
619
      if( mMode==DEBUG )
620
        {
621
        sendDebug();
622
        receiveValues = false;
623
        mRunning = false;
624
        }
625
      if( mMode==SUSPICIOUS )
626
        {
627
        sendSuspicious();
628
        receiveValues = false;
629
        mRunning = false;
630
        }
631
      }
632
    catch( Exception e )
633
      {
634
      android.util.Log.e("D", "Exception in RUN");
635

    
636
      if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
637
      }
638

    
639
    if( mRunning )
640
      {
641
      receiveValues = fillValuesNormal();
642
      mRunning = false;
643
      }
644

    
645
    if( receiveValues )
646
      {
647
      if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
648

    
649
      if( mMode==SUBMIT )
650
        {
651
        RubikScores.getInstance().successfulSubmit();
652
        }
653
      }
654
    }
655

    
656
///////////////////////////////////////////////////////////////////////////////////////////////////
657

    
658
  private RubikNetwork()
659
    {
660

    
661
    }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664
// PUBLIC API
665
///////////////////////////////////////////////////////////////////////////////////////////////////
666

    
667
  public static void onPause()
668
    {
669
    mRunning = false;
670
    }
671

    
672
///////////////////////////////////////////////////////////////////////////////////////////////////
673

    
674
  public static RubikNetwork getInstance()
675
    {
676
    if( mThis==null )
677
      {
678
      mThis = new RubikNetwork();
679
      }
680

    
681
    return mThis;
682
    }
683

    
684
///////////////////////////////////////////////////////////////////////////////////////////////////
685

    
686
  private void start(Receiver receiver, Activity act, int mode)
687
    {
688
    mReceiver = receiver;
689
    mMode     = mode;
690

    
691
    try
692
      {
693
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
694
      mVersion = pInfo.versionName;
695
      }
696
    catch (PackageManager.NameNotFoundException e)
697
      {
698
      mVersion = "0.9.2";
699
      }
700

    
701
    Thread networkThrd = new Thread(this);
702
    networkThrd.start();
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706

    
707
  public void download(Receiver receiver, FragmentActivity act)
708
    {
709
    start(receiver, act, DOWNLOAD);
710
    }
711

    
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713

    
714
  public void submit(Receiver receiver, FragmentActivity act)
715
    {
716
    start(receiver, act, SUBMIT);
717
    }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
  public void debug(AppCompatActivity act)
722
    {
723
    mDebugState = DEBUG_RUNNING;
724
    start(null, act, DEBUG);
725
    }
726

    
727
///////////////////////////////////////////////////////////////////////////////////////////////////
728

    
729
  public void suspicious(AppCompatActivity act, String suspicious)
730
    {
731
    mSuspicious = suspicious;
732
    start(null, act, SUSPICIOUS);
733
    }
734

    
735
///////////////////////////////////////////////////////////////////////////////////////////////////
736
// Yes it can happen that the second Updatee registers before we sent an update to the first one
737
// and, as a result, the update never gets sent to the first one. This is not a problem (now, when
738
// there are only two updatees - the RubikStatePlay and the UpdateDialog)
739
//
740
// Yes, there is also a remote possibility that the two threads executing this function and executing
741
// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee.
742
// We don't care about such remote possibility, then the app simply would signal that there are no
743
// updates available.
744

    
745
  public void signUpForUpdates(Updatee updatee)
746
    {
747
         if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates);
748
    else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate();
749
    else mUpdatee = updatee;
750
    }
751

    
752
///////////////////////////////////////////////////////////////////////////////////////////////////
753

    
754
  public void updateDone(String shortName)
755
    {
756
    mUpdates.updateDone(shortName);
757
    }
758
}
(1-1/3)