Project

General

Profile

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

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

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.external;
21

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

    
32
import android.content.pm.PackageInfo;
33
import android.content.pm.PackageManager;
34
import android.graphics.Bitmap;
35
import android.graphics.BitmapFactory;
36

    
37
import androidx.fragment.app.FragmentActivity;
38

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
56
  public interface IconReceiver
57
    {
58
    void iconDownloaded(int ordinal, Bitmap bitmap);
59
    }
60

    
61
  public interface Updatee
62
    {
63
    void receiveUpdate(RubikUpdates update);
64
    void errorUpdate();
65
    }
66

    
67
  public interface Downloadee
68
    {
69
    void jsonDownloaded();
70
    }
71

    
72
  public static final int MAX_PLACES = 10;
73

    
74
  private static final int REND_ADRENO= 0;
75
  private static final int REND_MALI  = 1;
76
  private static final int REND_POWER = 2;
77
  private static final int REND_OTHER = 3;
78

    
79
  private static final int DEBUG_RUNNING = 1;
80
  private static final int DEBUG_SUCCESS = 2;
81
  private static final int DEBUG_FAILURE = 3;
82

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

    
118
  private static String[][][] mCountry;
119
  private static String[][][] mName;
120
  private static float[][][] mTime;
121
  private static int[][] mPlaces;
122

    
123
  private static RubikNetwork mThis;
124
  private static String mScores = "";
125
  private static boolean mRunning = false;
126
  private static Updatee mUpdatee;
127
  private static String mVersion;
128
  private static int mNumObjects;
129
  private static RubikUpdates mUpdates;
130
  private static int mDebugState;
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

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

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

    
145
    mNumObjects = newNum;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

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

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

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

    
173
    return generatedPassword;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

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

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

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

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

    
215
      begin = end;
216
      }
217

    
218
    return true;
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

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

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

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

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

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

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

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

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

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

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

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

    
301
    return REND_OTHER;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

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

    
322
    return "other";
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

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

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

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

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

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

    
381
    return sbuf.toString();
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  private boolean network(String url, ScoresReceiver receiver)
387
    {
388
    try
389
      {
390
      java.net.URL connectURL = new URL(url);
391
      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
      InputStream is = conn.getInputStream();
401
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
402
      StringBuilder total = new StringBuilder();
403

    
404
      for (String line; (line = r.readLine()) != null; )
405
        {
406
        total.append(line).append('\n');
407
        }
408

    
409
      mScores = total.toString();
410
      conn.disconnect();
411
      }
412
    catch( final UnknownHostException e )
413
      {
414
      receiver.message("No access to Internet");
415
      return false;
416
      }
417
    catch( final SecurityException e )
418
      {
419
      receiver.message("Application not authorized to connect to the Internet");
420
      return false;
421
      }
422
    catch( final Exception e )
423
      {
424
      receiver.message(e.getMessage());
425
      return false;
426
      }
427

    
428
    if( mScores.length()==0 )
429
      {
430
      receiver.message("Failed to download scores");
431
      return false;
432
      }
433

    
434
    return true;
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  private String constructSuspiciousURL(String suspURL)
440
    {
441
    RubikScores scores = RubikScores.getInstance();
442
    int deviceID= scores.getDeviceID();
443
    String suspicious = URLencode(suspURL);
444

    
445
    String url="https://distorted.org/magic/cgi-bin/suspicious.cgi";
446
    url += "?i="+deviceID+"&d="+suspicious;
447

    
448
    return url;
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  private String constructDebugURL()
454
    {
455
    RubikScores scores = RubikScores.getInstance();
456
    String name = URLencode(scores.getName());
457
    int numRuns = scores.getNumRuns();
458
    int numPlay = scores.getNumPlays();
459
    String country = scores.getCountry();
460
    String renderer = DistortedLibrary.getDriverRenderer();
461
    String version  = DistortedLibrary.getDriverVersion();
462
    int objectAPI   = JsonWriter.VERSION_OBJECT_MAJOR;
463
    int tutorialAPI = JsonWriter.VERSION_EXTRAS_MAJOR;
464

    
465
    renderer = URLencode(renderer);
466
    version  = URLencode(version);
467

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

    
472
    return url;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  private String constructDownloadURL()
478
    {
479
    RubikScores scores = RubikScores.getInstance();
480
    String name = URLencode(scores.getName());
481
    int numRuns = scores.getNumRuns();
482
    int numPlay = scores.getNumPlays();
483
    String country = scores.getCountry();
484

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

    
488
    return url;
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  private String constructSubmitURL()
494
    {
495
    RubikScores scores = RubikScores.getInstance();
496
    String name = URLencode(scores.getName());
497
    String veri = scores.isVerified() ? "1" : "";
498
    int numRuns = scores.getNumRuns();
499
    int numPlay = scores.getNumPlays();
500
    int deviceID= scores.getDeviceID();
501
    String reclist = scores.getRecordList("&o=","&l=","&t=");
502
    String country = scores.getCountry();
503
    long epoch = System.currentTimeMillis();
504
    String salt = "cuboid";
505

    
506
    String renderer = DistortedLibrary.getDriverRenderer();
507
    String version  = DistortedLibrary.getDriverVersion();
508

    
509
    int type = getRendererType(renderer);
510
    renderer = parseRenderer(type,renderer);
511
    version  = parseVersion(type,version);
512

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

    
518
    return url1 + "?" + url2 + "&h=" + hash;
519
    }
520

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

    
523
  private boolean gottaDownload()
524
    {
525
    return ((mScores.length()==0) && !mRunning);
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  private void figureOutVersion(FragmentActivity act)
531
    {
532
    try
533
      {
534
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
535
      mVersion = pInfo.versionName;
536
      }
537
    catch (PackageManager.NameNotFoundException e)
538
      {
539
      mVersion = "0.9.2";
540
      }
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  private void downloadThread(ScoresReceiver receiver)
546
    {
547
    try
548
      {
549
      if( gottaDownload() )
550
        {
551
        mRunning = true;
552
        boolean receiveValues = network(constructDownloadURL(),receiver);
553

    
554
        if( mRunning )
555
          {
556
          receiveValues = fillValuesNormal(receiver);
557
          mRunning = false;
558
          }
559

    
560
        if( receiveValues ) receiver.receive(mCountry, mName, mTime);
561
        }
562
      }
563
    catch( Exception e )
564
      {
565
      receiver.message("Exception downloading records: "+e.getMessage() );
566
      }
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

    
571
  private void submitThread(ScoresReceiver receiver)
572
    {
573
    try
574
      {
575
      mRunning = true;
576
      RubikScores scores = RubikScores.getInstance();
577

    
578
      if( scores.thereAreUnsubmittedRecords() )
579
        {
580
        boolean receiveValues = network(constructSubmitURL(),receiver);
581

    
582
        if( mRunning )
583
          {
584
          receiveValues = fillValuesNormal(receiver);
585
          mRunning = false;
586
          }
587

    
588
        if( receiveValues )
589
          {
590
          RubikScores.getInstance().successfulSubmit();
591
          receiver.receive(mCountry, mName, mTime);
592
          }
593
        }
594
      }
595
    catch( Exception e )
596
      {
597
      receiver.message("Exception submitting records: "+e.getMessage() );
598
      }
599
    }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

    
603
  private void debugThread()
604
    {
605
    String url = constructDebugURL();
606
/*
607
    try { Thread.sleep(5000); }
608
    catch( InterruptedException ignored) {}
609
*/
610
    try
611
      {
612
      java.net.URL connectURL = new URL(url);
613
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
614

    
615
      conn.setDoInput(true);
616
      conn.setDoOutput(true);
617
      conn.setUseCaches(false);
618
      conn.setRequestMethod("GET");
619
      conn.connect();
620
      conn.getOutputStream().flush();
621

    
622
      InputStream is = conn.getInputStream();
623
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
624
      StringBuilder answer = new StringBuilder();
625

    
626
      for (String line; (line = r.readLine()) != null; )
627
        {
628
        answer.append(line).append('\n');
629
        }
630

    
631
      String updates = answer.toString();
632
      conn.disconnect();
633
      mUpdates.parse(updates);
634

    
635
      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
636
      mDebugState = DEBUG_SUCCESS;
637
      }
638
    catch( final Exception e )
639
      {
640
      if( mUpdatee!=null ) mUpdatee.errorUpdate();
641
      mDebugState = DEBUG_FAILURE;
642
      }
643
    }
644

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646

    
647
  private void suspiciousThread(String suspURL)
648
    {
649
    String url = constructSuspiciousURL(suspURL);
650

    
651
    try
652
      {
653
      java.net.URL connectURL = new URL(url);
654
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
655

    
656
      conn.setDoInput(true);
657
      conn.setDoOutput(true);
658
      conn.setUseCaches(false);
659
      conn.setRequestMethod("GET");
660
      conn.connect();
661
      conn.getOutputStream().flush();
662
      conn.getInputStream();
663
      conn.disconnect();
664
      }
665
    catch( final Exception e )
666
      {
667
      // ignore
668
      }
669
    }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672

    
673
  private Bitmap downloadIcon(String url)
674
    {
675
    try
676
      {
677
      java.net.URL connectURL = new URL(url);
678
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
679
      conn.setDoInput(true);
680
      conn.connect();
681
      InputStream input = conn.getInputStream();
682
      Bitmap icon = BitmapFactory.decodeStream(input);
683
      conn.disconnect();
684
      return icon;
685
      }
686
    catch (IOException e)
687
      {
688
      android.util.Log.e("D", "Failed to download "+url);
689
      android.util.Log.e("D", e.getMessage() );
690
      return null;
691
      }
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695

    
696
  private void iconThread(IconReceiver receiver)
697
    {
698
    int numC = mUpdates.getCompletedNumber();
699
    int numS = mUpdates.getStartedNumber();
700

    
701
    for(int c=0; c<numC; c++)
702
      {
703
      int iconPresent = mUpdates.getCompletedIconPresent(c);
704

    
705
      if( iconPresent!=0 )
706
        {
707
        Bitmap icon = mUpdates.getCompletedIcon(c);
708

    
709
        if( icon==null )
710
          {
711
          String url = mUpdates.getCompletedURL(c);
712
          icon = downloadIcon(url);
713
          }
714
        if( icon!=null )
715
          {
716
          mUpdates.setCompletedIcon(c,icon);
717
          receiver.iconDownloaded(c,icon);
718
          }
719
        }
720
      }
721

    
722
    for(int s=0; s<numS; s++)
723
      {
724
      int iconPresent = mUpdates.getStartedIconPresent(s);
725

    
726
      if( iconPresent!=0 )
727
        {
728
        Bitmap icon = mUpdates.getStartedIcon(s);
729

    
730
        if( icon==null )
731
          {
732
          String url = mUpdates.getStartedURL(s);
733
          icon = downloadIcon(url);
734
          }
735
        if( icon!=null )
736
          {
737
          mUpdates.setStartedIcon(s,icon);
738
          receiver.iconDownloaded(numC+s,icon);
739
          }
740
        }
741
      }
742
    }
743

    
744
///////////////////////////////////////////////////////////////////////////////////////////////////
745

    
746
  private InputStream downloadJSON(String name)
747
    {
748
    String url = mUpdates.getURL() + name;
749

    
750
    try
751
      {
752
      java.net.URL connectURL = new URL(url);
753
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
754
      conn.setDoInput(true);
755
      conn.connect();
756
      InputStream stream = conn.getInputStream();
757
      conn.disconnect();
758
      return stream;
759
      }
760
    catch (IOException e)
761
      {
762
      android.util.Log.e("D", "Failed to download "+url);
763
      android.util.Log.e("D", e.getMessage() );
764
      return null;
765
      }
766
    }
767

    
768
///////////////////////////////////////////////////////////////////////////////////////////////////
769

    
770
  private void jsonThread(final RubikUpdates.UpdateInfo info, Downloadee downloadee)
771
    {
772
    if(info.mUpdateObject) info.mObjectStream = downloadJSON(info.mObjectShortName+"_object.json");
773
    if(info.mUpdateExtras) info.mExtrasStream = downloadJSON(info.mObjectShortName+"_extras.json");
774

    
775
    downloadee.jsonDownloaded();
776
    }
777

    
778
///////////////////////////////////////////////////////////////////////////////////////////////////
779

    
780
  private RubikNetwork()
781
    {
782

    
783
    }
784

    
785
///////////////////////////////////////////////////////////////////////////////////////////////////
786
// PUBLIC API
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788

    
789
  public static void onPause()
790
    {
791
    mRunning = false;
792
    }
793

    
794
///////////////////////////////////////////////////////////////////////////////////////////////////
795

    
796
  public static RubikNetwork getInstance()
797
    {
798
    if( mThis==null )
799
      {
800
      mThis = new RubikNetwork();
801
      }
802

    
803
    return mThis;
804
    }
805

    
806
///////////////////////////////////////////////////////////////////////////////////////////////////
807

    
808
  public void download(final ScoresReceiver receiver, final FragmentActivity act)
809
    {
810
    initializeStatics();
811
    figureOutVersion(act);
812

    
813
    Thread thread = new Thread()
814
      {
815
      public void run()
816
        {
817
        downloadThread(receiver);
818
        }
819
      };
820

    
821
    thread.start();
822
    }
823

    
824
///////////////////////////////////////////////////////////////////////////////////////////////////
825

    
826
  public void submit(ScoresReceiver receiver, final FragmentActivity act)
827
    {
828
    initializeStatics();
829
    figureOutVersion(act);
830

    
831
    Thread thread = new Thread()
832
      {
833
      public void run()
834
        {
835
        submitThread(receiver);
836
        }
837
      };
838

    
839
    thread.start();
840
    }
841

    
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843

    
844
  public void debug()
845
    {
846
    initializeStatics();
847
    mDebugState = DEBUG_RUNNING;
848

    
849
    Thread thread = new Thread()
850
      {
851
      public void run()
852
        {
853
        debugThread();
854
        }
855
      };
856

    
857
    thread.start();
858
    }
859

    
860
///////////////////////////////////////////////////////////////////////////////////////////////////
861

    
862
  public void suspicious(final String suspicious)
863
    {
864
    initializeStatics();
865

    
866
    Thread thread = new Thread()
867
      {
868
      public void run()
869
        {
870
        suspiciousThread(suspicious);
871
        }
872
      };
873

    
874
    thread.start();
875
    }
876

    
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878
// Yes it can happen that the second Updatee registers before we sent an update to the first one
879
// and, as a result, the update never gets sent to the first one. This is not a problem (now, when
880
// there are only two updatees - the RubikStatePlay and the UpdateDialog)
881
//
882
// Yes, there is also a remote possibility that the two threads executing this function and executing
883
// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee.
884
// We don't care about such remote possibility, then the app simply would signal that there are no
885
// updates available.
886

    
887
  public void signUpForUpdates(Updatee updatee)
888
    {
889
         if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates);
890
    else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate();
891
    else mUpdatee = updatee;
892
    }
893

    
894
///////////////////////////////////////////////////////////////////////////////////////////////////
895

    
896
  public void downloadIcons(final IconReceiver receiver)
897
    {
898
    initializeStatics();
899

    
900
    Thread thread = new Thread()
901
      {
902
      public void run()
903
        {
904
        iconThread(receiver);
905
        }
906
      };
907

    
908
    thread.start();
909
    }
910

    
911
///////////////////////////////////////////////////////////////////////////////////////////////////
912

    
913
  public void downloadJSON(final RubikUpdates.UpdateInfo info, final Downloadee downloadee)
914
    {
915
    initializeStatics();
916

    
917
    Thread thread = new Thread()
918
      {
919
      public void run()
920
        {
921
        jsonThread(info,downloadee);
922
        }
923
      };
924

    
925
    thread.start();
926
    }
927

    
928
///////////////////////////////////////////////////////////////////////////////////////////////////
929

    
930
  public void updateDone(String shortName)
931
    {
932
    mUpdates.updateDone(shortName);
933
    }
934
}
(1-1/3)