Project

General

Profile

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

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

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