Project

General

Profile

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

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

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 acabdd83 Leszek Koltunski
package org.distorted.external;
21 fdec60a3 Leszek Koltunski
22 1d643a0e Leszek Koltunski
import java.io.BufferedReader;
23 b88cdd91 Leszek Koltunski
import java.io.IOException;
24 3f7a4363 Leszek Koltunski
import java.io.InputStream;
25 1d643a0e Leszek Koltunski
import java.io.InputStreamReader;
26 3f7a4363 Leszek Koltunski
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 7fe62d1f Leszek Koltunski
import android.content.Context;
33 1780dcb0 Leszek Koltunski
import android.content.pm.PackageInfo;
34
import android.content.pm.PackageManager;
35 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
36
import android.graphics.BitmapFactory;
37 1780dcb0 Leszek Koltunski
38 66e777b0 Leszek Koltunski
import androidx.fragment.app.FragmentActivity;
39 4895fff6 Leszek Koltunski
40 6a083c6a Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
41 fcf7320f Leszek Koltunski
import org.distorted.objectlib.json.JsonWriter;
42 a7d8c3cd Leszek Koltunski
import org.distorted.objects.RubikObjectList;
43 36e2cbdd Leszek Koltunski
44 d433b50e Leszek Koltunski
import static org.distorted.objects.RubikObjectList.MAX_LEVEL;
45 e847c553 Leszek Koltunski
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
46 211b48f2 Leszek Koltunski
47 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 ffe7e55d Leszek Koltunski
public class RubikNetwork
50 fdec60a3 Leszek Koltunski
  {
51 46be3ddf Leszek Koltunski
  public interface ScoresReceiver
52 b8b38548 Leszek Koltunski
    {
53 1c90c64a Leszek Koltunski
    void receive(String[][][] country, String[][][] name, float[][][] time);
54 4895fff6 Leszek Koltunski
    void message(String mess);
55 4c0cd600 Leszek Koltunski
    void error(String error);
56 b8b38548 Leszek Koltunski
    }
57
58 46be3ddf Leszek Koltunski
  public interface IconReceiver
59 63dd19c4 Leszek Koltunski
    {
60 7fe62d1f Leszek Koltunski
    void iconDownloaded(int ordinal, Bitmap bitmap, boolean downloaded);
61 46be3ddf Leszek Koltunski
    }
62
63
  public interface Updatee
64
    {
65 63dd19c4 Leszek Koltunski
    void receiveUpdate(RubikUpdates update);
66
    void errorUpdate();
67
    }
68
69 46be3ddf Leszek Koltunski
  public interface Downloadee
70
    {
71
    void jsonDownloaded();
72
    }
73
74 286d73ae Leszek Koltunski
  public static final int MAX_PLACES = 10;
75 d8aa4ba8 Leszek Koltunski
76 14f14138 Leszek Koltunski
  private static final int REND_ADRENO= 0;
77
  private static final int REND_MALI  = 1;
78
  private static final int REND_POWER = 2;
79
  private static final int REND_OTHER = 3;
80 9d4c38eb Leszek Koltunski
81 63dd19c4 Leszek Koltunski
  private static final int DEBUG_RUNNING = 1;
82
  private static final int DEBUG_SUCCESS = 2;
83
  private static final int DEBUG_FAILURE = 3;
84
85 36e2cbdd Leszek Koltunski
  private final String[] hex = {
86
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
87
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
88
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
89
    "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
90
    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
91
    "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
92
    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
93
    "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
94
    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
95
    "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
96
    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
97
    "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
98
    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
99
    "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
100
    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
101
    "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
102
    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
103
    "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
104
    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
105
    "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
106
    "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
107
    "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
108
    "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
109
    "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
110
    "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
111
    "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
112
    "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
113
    "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
114
    "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
115
    "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
116
    "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
117
    "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
118
    };
119
120 a36b9dca Leszek Koltunski
  private static String[][][] mCountry;
121
  private static String[][][] mName;
122
  private static float[][][] mTime;
123
  private static int[][] mPlaces;
124 688f7712 Leszek Koltunski
125 6a083c6a Leszek Koltunski
  private static RubikNetwork mThis;
126 688f7712 Leszek Koltunski
  private static String mScores = "";
127 b8b38548 Leszek Koltunski
  private static boolean mRunning = false;
128 63dd19c4 Leszek Koltunski
  private static Updatee mUpdatee;
129 4895fff6 Leszek Koltunski
  private static String mVersion;
130 a7d8c3cd Leszek Koltunski
  private static int mNumObjects;
131 fcf7320f Leszek Koltunski
  private static RubikUpdates mUpdates;
132 63dd19c4 Leszek Koltunski
  private static int mDebugState;
133 b8b38548 Leszek Koltunski
134 a36b9dca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
  private static void initializeStatics()
137
    {
138 a7d8c3cd Leszek Koltunski
    int newNum = RubikObjectList.getNumObjects();
139
140
    if( mCountry==null || newNum!=mNumObjects ) mCountry = new String[newNum][MAX_LEVEL][MAX_PLACES];
141
    if( mName==null    || newNum!=mNumObjects ) mName    = new String[newNum][MAX_LEVEL][MAX_PLACES];
142
    if( mTime==null    || newNum!=mNumObjects ) mTime    = new  float[newNum][MAX_LEVEL][MAX_PLACES];
143 fcf7320f Leszek Koltunski
    if( mPlaces==null  || newNum!=mNumObjects ) mPlaces  = new    int[newNum][MAX_LEVEL];
144
145
    if( mUpdates==null ) mUpdates = new RubikUpdates();
146 a7d8c3cd Leszek Koltunski
147
    mNumObjects = newNum;
148 a36b9dca Leszek Koltunski
    }
149
150 4895fff6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152 86cbdab1 Leszek Koltunski
  private static String computeHash(String stringToHash, byte[] salt)
153 4895fff6 Leszek Koltunski
    {
154 86cbdab1 Leszek Koltunski
    String generatedPassword;
155
156
    try
157
      {
158
      MessageDigest md = MessageDigest.getInstance("MD5");
159
      md.update(salt);
160
      byte[] bytes = md.digest(stringToHash.getBytes());
161
      StringBuilder sb = new StringBuilder();
162
163
      for (byte aByte : bytes)
164
        {
165
        sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
166
        }
167
168
      generatedPassword = sb.toString();
169
      }
170
    catch (NoSuchAlgorithmException e)
171
      {
172
      return "NoSuchAlgorithm";
173
      }
174
175
    return generatedPassword;
176 4895fff6 Leszek Koltunski
    }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180 46be3ddf Leszek Koltunski
  private boolean fillValuesNormal(ScoresReceiver receiver)
181 f3e12931 Leszek Koltunski
    {
182 d8aa4ba8 Leszek Koltunski
    int begin=-1 ,end, len = mScores.length();
183 17f9a695 Leszek Koltunski
    String row;
184 f3e12931 Leszek Koltunski
185 4c0cd600 Leszek Koltunski
    if( len==0 )
186
      {
187 46be3ddf Leszek Koltunski
      receiver.error("1");
188 4c0cd600 Leszek Koltunski
      return false;
189
      }
190 90fd47b0 Leszek Koltunski
    else if( len<=2 )
191 4c0cd600 Leszek Koltunski
      {
192 46be3ddf Leszek Koltunski
      receiver.error(mScores);
193 4c0cd600 Leszek Koltunski
      return false;
194
      }
195
196 a7d8c3cd Leszek Koltunski
    for(int i=0; i<mNumObjects; i++)
197 85b09df4 Leszek Koltunski
      for(int j=0; j<MAX_LEVEL; j++)
198 329c0aeb Leszek Koltunski
        {
199
        mPlaces[i][j] = 0;
200
        }
201
202 d8aa4ba8 Leszek Koltunski
    while( begin<len )
203
      {
204
      end = mScores.indexOf('\n', begin+1);
205
      if( end<0 ) end = len;
206 286d73ae Leszek Koltunski
207
      try
208
        {
209 17f9a695 Leszek Koltunski
        row = mScores.substring(begin+1,end);
210 4c0cd600 Leszek Koltunski
        fillRow(row);
211 286d73ae Leszek Koltunski
        }
212
      catch(Exception ex)
213
        {
214
        // faulty row - ignore
215
        }
216
217 d8aa4ba8 Leszek Koltunski
      begin = end;
218
      }
219 17f9a695 Leszek Koltunski
220
    return true;
221 f3e12931 Leszek Koltunski
    }
222
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225 d8aa4ba8 Leszek Koltunski
  private void fillRow(String row)
226 f3e12931 Leszek Koltunski
    {
227 d8aa4ba8 Leszek Koltunski
    int s1 = row.indexOf(' ');
228
    int s2 = row.indexOf(' ',s1+1);
229
    int s3 = row.indexOf(' ',s2+1);
230
    int s4 = row.indexOf(' ',s3+1);
231 286d73ae Leszek Koltunski
    int s5 = row.length();
232 d8aa4ba8 Leszek Koltunski
233
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
234
      {
235 a7d8c3cd Leszek Koltunski
      int object = RubikObjectList.getOrdinal( row.substring(0,s1) );
236 b8b38548 Leszek Koltunski
237 a7d8c3cd Leszek Koltunski
      if( object>=0 && object<mNumObjects )
238 d8aa4ba8 Leszek Koltunski
        {
239 8e3898c8 Leszek Koltunski
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
240 286d73ae Leszek Koltunski
        String name    = row.substring(s2+1, s3);
241
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
242
        String country = row.substring(s4+1, s5);
243 d8aa4ba8 Leszek Koltunski
244 c8249cf6 Leszek Koltunski
        if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
245
246 8e3898c8 Leszek Koltunski
        if(level>=0 && level<MAX_LEVEL)
247 d8aa4ba8 Leszek Koltunski
          {
248 8e3898c8 Leszek Koltunski
          int p = mPlaces[object][level];
249
          mPlaces[object][level]++;
250 329c0aeb Leszek Koltunski
251 8e3898c8 Leszek Koltunski
          mCountry[object][level][p] = country;
252
          mName   [object][level][p] = name;
253 79e752b3 Leszek Koltunski
          mTime   [object][level][p] = ((float)(time/10))/100.0f;
254 d8aa4ba8 Leszek Koltunski
          }
255
        }
256
      }
257 874c37b1 Leszek Koltunski
    else
258
      {
259 5d50e7a7 Leszek Koltunski
      tryDoCommand(row);
260
      }
261
    }
262
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
265
  private void tryDoCommand(String row)
266
    {
267
    if( row.startsWith("comm") )
268
      {
269
      int colon = row.indexOf(':');
270
271
      if( colon>0 )
272 874c37b1 Leszek Koltunski
        {
273 5d50e7a7 Leszek Koltunski
        String commandNumber = row.substring(4,colon);
274
        int number;
275
276
        try
277
          {
278
          number = Integer.parseInt(commandNumber);
279
          }
280
        catch(NumberFormatException ex)
281
          {
282
          number=0;
283
          }
284 874c37b1 Leszek Koltunski
285 5d50e7a7 Leszek Koltunski
        if(number==1)
286 874c37b1 Leszek Koltunski
          {
287
          String country = row.substring(colon+1);
288
          RubikScores scores = RubikScores.getInstance();
289
          scores.setCountry(country);
290
          }
291
        }
292
      }
293 f3e12931 Leszek Koltunski
    }
294
295 9d4c38eb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
296
297
  private int getRendererType(String renderer)
298
    {
299
    if( renderer.contains("Adreno")  ) return REND_ADRENO;
300
    if( renderer.contains("Mali")    ) return REND_MALI;
301
    if( renderer.contains("PowerVR") ) return REND_POWER;
302
303
    return REND_OTHER;
304
    }
305
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307
308
  private String parseRenderer(final int type, String renderer)
309
    {
310
    if( type==REND_ADRENO || type==REND_POWER )
311
      {
312
      int lastSpace = renderer.lastIndexOf(' ');
313
      String ret = renderer.substring(lastSpace+1);
314
      return URLencode(ret);
315
      }
316
317
    if( type==REND_MALI )
318
      {
319
      int firstHyphen = renderer.indexOf('-');
320
      String ret = renderer.substring(firstHyphen+1);
321
      return URLencode(ret);
322
      }
323
324
    return "other";
325
    }
326
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
329
  private String parseVersion(final int type, String version)
330
    {
331
    switch(type)
332
      {
333
      case REND_ADRENO: int aMonkey = version.indexOf('@');
334
                        int aDot = version.indexOf('.', aMonkey);
335
                        String ret1 = aDot>=3 ? version.substring(aDot-3,aDot) : "";
336
                        return URLencode(ret1);
337
      case REND_MALI  : int mV1 = version.indexOf("v1");
338
                        int mHyphen = version.indexOf('-', mV1);
339
                        String ret2 = mHyphen>mV1+3 && mV1>=0 ? version.substring(mV1+3,mHyphen) : "";
340
                        return URLencode(ret2);
341
      case REND_POWER : int pMonkey = version.indexOf('@');
342
                        int pSpace  = version.lastIndexOf(' ');
343
                        String ret3 = pSpace>=0 && pMonkey>pSpace+1 ? version.substring(pSpace+1,pMonkey) : "";
344
                        return URLencode(ret3);
345
      default         : return "";
346
      }
347
    }
348
349 36e2cbdd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
350
351
  private String URLencode(String s)
352
    {
353
    StringBuilder sbuf = new StringBuilder();
354
    int len = s.length();
355
356
    for (int i = 0; i < len; i++)
357
      {
358
      int ch = s.charAt(i);
359
360
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
361
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
362
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
363
      else if (ch == ' '             ) sbuf.append('+');
364
      else if (ch == '-' || ch == '_'
365
            || ch == '.' || ch == '!'
366
            || ch == '~' || ch == '*'
367
            || ch == '\'' || ch == '('
368
            || ch == ')'             ) sbuf.append((char)ch);
369
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
370
      else if (ch <= 0x07FF)
371
        {
372
        sbuf.append(hex[0xc0 | (ch >> 6)]);
373
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
374
        }
375
      else
376
        {
377
        sbuf.append(hex[0xe0 | (ch >> 12)]);
378
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
379
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
380
        }
381
      }
382
383
    return sbuf.toString();
384
    }
385
386 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
387
388 46be3ddf Leszek Koltunski
  private boolean network(String url, ScoresReceiver receiver)
389 f3e12931 Leszek Koltunski
    {
390 36e2cbdd Leszek Koltunski
    try
391
      {
392 4895fff6 Leszek Koltunski
      java.net.URL connectURL = new URL(url);
393 36e2cbdd Leszek Koltunski
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
394
395
      conn.setDoInput(true);
396
      conn.setDoOutput(true);
397
      conn.setUseCaches(false);
398
      conn.setRequestMethod("GET");
399
      conn.connect();
400
      conn.getOutputStream().flush();
401
402 1d643a0e Leszek Koltunski
      InputStream is = conn.getInputStream();
403
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
404
      StringBuilder total = new StringBuilder();
405
406
      for (String line; (line = r.readLine()) != null; )
407 36e2cbdd Leszek Koltunski
        {
408 1d643a0e Leszek Koltunski
        total.append(line).append('\n');
409 36e2cbdd Leszek Koltunski
        }
410 1d643a0e Leszek Koltunski
411
      mScores = total.toString();
412 46be3ddf Leszek Koltunski
      conn.disconnect();
413 36e2cbdd Leszek Koltunski
      }
414 cc5ec229 Leszek Koltunski
    catch( final UnknownHostException e )
415
      {
416 46be3ddf Leszek Koltunski
      receiver.message("No access to Internet");
417 cc5ec229 Leszek Koltunski
      return false;
418
      }
419
    catch( final SecurityException e )
420
      {
421 46be3ddf Leszek Koltunski
      receiver.message("Application not authorized to connect to the Internet");
422 cc5ec229 Leszek Koltunski
      return false;
423
      }
424
    catch( final Exception e )
425 36e2cbdd Leszek Koltunski
      {
426 46be3ddf Leszek Koltunski
      receiver.message(e.getMessage());
427 a675474f Leszek Koltunski
      return false;
428 36e2cbdd Leszek Koltunski
      }
429
430 329c0aeb Leszek Koltunski
    if( mScores.length()==0 )
431
      {
432 46be3ddf Leszek Koltunski
      receiver.message("Failed to download scores");
433 329c0aeb Leszek Koltunski
      return false;
434
      }
435
436 a675474f Leszek Koltunski
    return true;
437 b8b38548 Leszek Koltunski
    }
438 d8aa4ba8 Leszek Koltunski
439 1fa125c2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
440
441 46be3ddf Leszek Koltunski
  private String constructSuspiciousURL(String suspURL)
442 1fa125c2 Leszek Koltunski
    {
443
    RubikScores scores = RubikScores.getInstance();
444
    int deviceID= scores.getDeviceID();
445 46be3ddf Leszek Koltunski
    String suspicious = URLencode(suspURL);
446 1fa125c2 Leszek Koltunski
447
    String url="https://distorted.org/magic/cgi-bin/suspicious.cgi";
448 63dd19c4 Leszek Koltunski
    url += "?i="+deviceID+"&d="+suspicious;
449 1fa125c2 Leszek Koltunski
450
    return url;
451
    }
452
453 6a083c6a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
454
455
  private String constructDebugURL()
456
    {
457
    RubikScores scores = RubikScores.getInstance();
458
    String name = URLencode(scores.getName());
459
    int numRuns = scores.getNumRuns();
460
    int numPlay = scores.getNumPlays();
461
    String country = scores.getCountry();
462
    String renderer = DistortedLibrary.getDriverRenderer();
463
    String version  = DistortedLibrary.getDriverVersion();
464 fcf7320f Leszek Koltunski
    int objectAPI   = JsonWriter.VERSION_OBJECT_MAJOR;
465
    int tutorialAPI = JsonWriter.VERSION_EXTRAS_MAJOR;
466 6a083c6a Leszek Koltunski
467
    renderer = URLencode(renderer);
468
    version  = URLencode(version);
469
470 66935886 Leszek Koltunski
    String url="https://distorted.org/magic/cgi-bin/debugs.cgi";
471 fcf7320f Leszek Koltunski
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
472
    url += "&d="+renderer+"&v="+version+"&a="+objectAPI+"&b="+tutorialAPI;
473 6a083c6a Leszek Koltunski
474
    return url;
475
    }
476
477 17f9a695 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
478
479
  private String constructDownloadURL()
480
    {
481
    RubikScores scores = RubikScores.getInstance();
482
    String name = URLencode(scores.getName());
483
    int numRuns = scores.getNumRuns();
484
    int numPlay = scores.getNumPlays();
485 874c37b1 Leszek Koltunski
    String country = scores.getCountry();
486 17f9a695 Leszek Koltunski
487
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
488 11d68e9c Leszek Koltunski
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion;
489 17f9a695 Leszek Koltunski
490
    return url;
491
    }
492
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494
495
  private String constructSubmitURL()
496
    {
497
    RubikScores scores = RubikScores.getInstance();
498
    String name = URLencode(scores.getName());
499 11d68e9c Leszek Koltunski
    String veri = scores.isVerified() ? "1" : "";
500 17f9a695 Leszek Koltunski
    int numRuns = scores.getNumRuns();
501
    int numPlay = scores.getNumPlays();
502
    int deviceID= scores.getDeviceID();
503 6570171b Leszek Koltunski
    String reclist = scores.getRecordList("&o=","&l=","&t=");
504 17f9a695 Leszek Koltunski
    String country = scores.getCountry();
505 86cbdab1 Leszek Koltunski
    long epoch = System.currentTimeMillis();
506 9e171f43 Leszek Koltunski
    String salt = "cuboid";
507 17f9a695 Leszek Koltunski
508 9d4c38eb Leszek Koltunski
    String renderer = DistortedLibrary.getDriverRenderer();
509
    String version  = DistortedLibrary.getDriverVersion();
510
511
    int type = getRendererType(renderer);
512
    renderer = parseRenderer(type,renderer);
513
    version  = parseVersion(type,version);
514
515 86cbdab1 Leszek Koltunski
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
516 11d68e9c Leszek Koltunski
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion;
517
    url2 += "&d="+renderer+"&s="+version+reclist+"&c="+country+"&f="+epoch;
518 9e171f43 Leszek Koltunski
    String hash = computeHash( url2, salt.getBytes() );
519 17f9a695 Leszek Koltunski
520 b3ee0e78 Leszek Koltunski
    return url1 + "?" + url2 + "&h=" + hash;
521 17f9a695 Leszek Koltunski
    }
522
523 d8aa4ba8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
524
525
  private boolean gottaDownload()
526 b8b38548 Leszek Koltunski
    {
527 d8aa4ba8 Leszek Koltunski
    return ((mScores.length()==0) && !mRunning);
528 f3e12931 Leszek Koltunski
    }
529 b8b38548 Leszek Koltunski
530 d8aa4ba8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
531
532 ffe7e55d Leszek Koltunski
  private void figureOutVersion(FragmentActivity act)
533 d8aa4ba8 Leszek Koltunski
    {
534 ffe7e55d Leszek Koltunski
    try
535
      {
536
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
537
      mVersion = pInfo.versionName;
538
      }
539
    catch (PackageManager.NameNotFoundException e)
540
      {
541
      mVersion = "0.9.2";
542
      }
543
    }
544 a675474f Leszek Koltunski
545 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
546 a36b9dca Leszek Koltunski
547 46be3ddf Leszek Koltunski
  private void downloadThread(ScoresReceiver receiver)
548 ffe7e55d Leszek Koltunski
    {
549 17f9a695 Leszek Koltunski
    try
550 d8aa4ba8 Leszek Koltunski
      {
551 ffe7e55d Leszek Koltunski
      if( gottaDownload() )
552 4895fff6 Leszek Koltunski
        {
553 17f9a695 Leszek Koltunski
        mRunning = true;
554 46be3ddf Leszek Koltunski
        boolean receiveValues = network(constructDownloadURL(),receiver);
555 4895fff6 Leszek Koltunski
556 ffe7e55d Leszek Koltunski
        if( mRunning )
557 17f9a695 Leszek Koltunski
          {
558 46be3ddf Leszek Koltunski
          receiveValues = fillValuesNormal(receiver);
559 ffe7e55d Leszek Koltunski
          mRunning = false;
560 17f9a695 Leszek Koltunski
          }
561 ffe7e55d Leszek Koltunski
562 46be3ddf Leszek Koltunski
        if( receiveValues ) receiver.receive(mCountry, mName, mTime);
563 1fa125c2 Leszek Koltunski
        }
564 d8aa4ba8 Leszek Koltunski
      }
565 17f9a695 Leszek Koltunski
    catch( Exception e )
566 d8aa4ba8 Leszek Koltunski
      {
567 46be3ddf Leszek Koltunski
      receiver.message("Exception downloading records: "+e.getMessage() );
568 4895fff6 Leszek Koltunski
      }
569 ffe7e55d Leszek Koltunski
    }
570
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572 286d73ae Leszek Koltunski
573 46be3ddf Leszek Koltunski
  private void submitThread(ScoresReceiver receiver)
574 ffe7e55d Leszek Koltunski
    {
575
    try
576
      {
577
      mRunning = true;
578
      RubikScores scores = RubikScores.getInstance();
579
580
      if( scores.thereAreUnsubmittedRecords() )
581
        {
582 46be3ddf Leszek Koltunski
        boolean receiveValues = network(constructSubmitURL(),receiver);
583 ffe7e55d Leszek Koltunski
584
        if( mRunning )
585
          {
586 46be3ddf Leszek Koltunski
          receiveValues = fillValuesNormal(receiver);
587 ffe7e55d Leszek Koltunski
          mRunning = false;
588
          }
589
590
        if( receiveValues )
591
          {
592
          RubikScores.getInstance().successfulSubmit();
593 46be3ddf Leszek Koltunski
          receiver.receive(mCountry, mName, mTime);
594 ffe7e55d Leszek Koltunski
          }
595
        }
596
      }
597
    catch( Exception e )
598 17f9a695 Leszek Koltunski
      {
599 46be3ddf Leszek Koltunski
      receiver.message("Exception submitting records: "+e.getMessage() );
600 17f9a695 Leszek Koltunski
      }
601 ffe7e55d Leszek Koltunski
    }
602 a675474f Leszek Koltunski
603 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
604
605
  private void debugThread()
606
    {
607
    String url = constructDebugURL();
608
/*
609
    try { Thread.sleep(5000); }
610
    catch( InterruptedException ignored) {}
611
*/
612
    try
613 a675474f Leszek Koltunski
      {
614 ffe7e55d Leszek Koltunski
      java.net.URL connectURL = new URL(url);
615
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
616 17f9a695 Leszek Koltunski
617 ffe7e55d Leszek Koltunski
      conn.setDoInput(true);
618
      conn.setDoOutput(true);
619
      conn.setUseCaches(false);
620
      conn.setRequestMethod("GET");
621
      conn.connect();
622
      conn.getOutputStream().flush();
623
624
      InputStream is = conn.getInputStream();
625
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
626
      StringBuilder answer = new StringBuilder();
627
628
      for (String line; (line = r.readLine()) != null; )
629 17f9a695 Leszek Koltunski
        {
630 ffe7e55d Leszek Koltunski
        answer.append(line).append('\n');
631 17f9a695 Leszek Koltunski
        }
632 ffe7e55d Leszek Koltunski
633
      String updates = answer.toString();
634 46be3ddf Leszek Koltunski
      conn.disconnect();
635 ffe7e55d Leszek Koltunski
      mUpdates.parse(updates);
636
637
      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
638
      mDebugState = DEBUG_SUCCESS;
639
      }
640
    catch( final Exception e )
641
      {
642
      if( mUpdatee!=null ) mUpdatee.errorUpdate();
643
      mDebugState = DEBUG_FAILURE;
644
      }
645
    }
646
647
///////////////////////////////////////////////////////////////////////////////////////////////////
648
649 46be3ddf Leszek Koltunski
  private void suspiciousThread(String suspURL)
650 ffe7e55d Leszek Koltunski
    {
651 46be3ddf Leszek Koltunski
    String url = constructSuspiciousURL(suspURL);
652 ffe7e55d Leszek Koltunski
653
    try
654
      {
655
      java.net.URL connectURL = new URL(url);
656
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
657
658
      conn.setDoInput(true);
659
      conn.setDoOutput(true);
660
      conn.setUseCaches(false);
661
      conn.setRequestMethod("GET");
662
      conn.connect();
663
      conn.getOutputStream().flush();
664
      conn.getInputStream();
665 46be3ddf Leszek Koltunski
      conn.disconnect();
666 ffe7e55d Leszek Koltunski
      }
667
    catch( final Exception e )
668
      {
669
      // ignore
670 a675474f Leszek Koltunski
      }
671 d8aa4ba8 Leszek Koltunski
    }
672 4f9f99a2 Leszek Koltunski
673 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
674
675
  private Bitmap downloadIcon(String url)
676
    {
677
    try
678
      {
679
      java.net.URL connectURL = new URL(url);
680
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
681
      conn.setDoInput(true);
682
      conn.connect();
683
      InputStream input = conn.getInputStream();
684 46be3ddf Leszek Koltunski
      Bitmap icon = BitmapFactory.decodeStream(input);
685
      conn.disconnect();
686
      return icon;
687 b88cdd91 Leszek Koltunski
      }
688
    catch (IOException e)
689
      {
690
      android.util.Log.e("D", "Failed to download "+url);
691
      android.util.Log.e("D", e.getMessage() );
692
      return null;
693
      }
694
    }
695
696
///////////////////////////////////////////////////////////////////////////////////////////////////
697
698 7fe62d1f Leszek Koltunski
  private void iconThread(Context context, IconReceiver receiver)
699 b88cdd91 Leszek Koltunski
    {
700
    int numC = mUpdates.getCompletedNumber();
701
    int numS = mUpdates.getStartedNumber();
702
703
    for(int c=0; c<numC; c++)
704
      {
705 b92ad5cd Leszek Koltunski
      int iconPresent = mUpdates.getCompletedIconPresent(c);
706 b88cdd91 Leszek Koltunski
707 b92ad5cd Leszek Koltunski
      if( iconPresent!=0 )
708 b88cdd91 Leszek Koltunski
        {
709 7fe62d1f Leszek Koltunski
        boolean downloaded = false;
710
        Bitmap icon = mUpdates.getCompletedIcon(context,c);
711 b92ad5cd Leszek Koltunski
712
        if( icon==null )
713
          {
714
          String url = mUpdates.getCompletedURL(c);
715
          icon = downloadIcon(url);
716 7fe62d1f Leszek Koltunski
          downloaded = true;
717 84d746d7 Leszek Koltunski
718 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading icon "+url);
719 b92ad5cd Leszek Koltunski
          }
720
        if( icon!=null )
721
          {
722
          mUpdates.setCompletedIcon(c,icon);
723 7fe62d1f Leszek Koltunski
          receiver.iconDownloaded(c,icon,downloaded);
724 b92ad5cd Leszek Koltunski
          }
725 b88cdd91 Leszek Koltunski
        }
726
      }
727
728
    for(int s=0; s<numS; s++)
729
      {
730 b92ad5cd Leszek Koltunski
      int iconPresent = mUpdates.getStartedIconPresent(s);
731 b88cdd91 Leszek Koltunski
732 b92ad5cd Leszek Koltunski
      if( iconPresent!=0 )
733 b88cdd91 Leszek Koltunski
        {
734 7fe62d1f Leszek Koltunski
        boolean downloaded = false;
735
        Bitmap icon = mUpdates.getStartedIcon(context,s);
736 b92ad5cd Leszek Koltunski
737
        if( icon==null )
738
          {
739
          String url = mUpdates.getStartedURL(s);
740
          icon = downloadIcon(url);
741 7fe62d1f Leszek Koltunski
          downloaded = true;
742 84d746d7 Leszek Koltunski
743 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading icon "+url);
744 b92ad5cd Leszek Koltunski
          }
745
        if( icon!=null )
746
          {
747
          mUpdates.setStartedIcon(s,icon);
748 7fe62d1f Leszek Koltunski
          receiver.iconDownloaded(numC+s,icon,downloaded);
749 b92ad5cd Leszek Koltunski
          }
750 b88cdd91 Leszek Koltunski
        }
751
      }
752
    }
753
754 46be3ddf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
755
756
  private InputStream downloadJSON(String name)
757
    {
758
    String url = mUpdates.getURL() + name;
759
760
    try
761
      {
762 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Downloading JSON "+url);
763 84d746d7 Leszek Koltunski
764 46be3ddf Leszek Koltunski
      java.net.URL connectURL = new URL(url);
765
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
766
      conn.setDoInput(true);
767
      conn.connect();
768 806329e3 Leszek Koltunski
      return conn.getInputStream();
769 46be3ddf Leszek Koltunski
      }
770
    catch (IOException e)
771
      {
772
      android.util.Log.e("D", "Failed to download "+url);
773
      android.util.Log.e("D", e.getMessage() );
774
      return null;
775
      }
776
    }
777
778
///////////////////////////////////////////////////////////////////////////////////////////////////
779
780
  private void jsonThread(final RubikUpdates.UpdateInfo info, Downloadee downloadee)
781
    {
782
    if(info.mUpdateObject) info.mObjectStream = downloadJSON(info.mObjectShortName+"_object.json");
783
    if(info.mUpdateExtras) info.mExtrasStream = downloadJSON(info.mObjectShortName+"_extras.json");
784
785
    downloadee.jsonDownloaded();
786 806329e3 Leszek Koltunski
787
    try
788
      {
789
      if( info.mObjectStream!=null ) info.mObjectStream.close();
790
      }
791
    catch(IOException ioe)
792
      {
793
      android.util.Log.e("D", "failed to close object input stream");
794
      }
795
796
    try
797
      {
798
      if( info.mExtrasStream!=null ) info.mExtrasStream.close();
799
      }
800
    catch(IOException ioe)
801
      {
802
      android.util.Log.e("D", "failed to close extras input stream");
803
      }
804 46be3ddf Leszek Koltunski
    }
805
806 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
807
808 6a083c6a Leszek Koltunski
  private RubikNetwork()
809 bc511ea5 Leszek Koltunski
    {
810
811
    }
812
813 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
814
// PUBLIC API
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816
817
  public static void onPause()
818
    {
819
    mRunning = false;
820
    }
821
822 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
823
824 6a083c6a Leszek Koltunski
  public static RubikNetwork getInstance()
825 bc511ea5 Leszek Koltunski
    {
826 35161021 Leszek Koltunski
    if( mThis==null ) mThis = new RubikNetwork();
827 bc511ea5 Leszek Koltunski
    return mThis;
828
    }
829
830 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
831
832 46be3ddf Leszek Koltunski
  public void download(final ScoresReceiver receiver, final FragmentActivity act)
833 4f9f99a2 Leszek Koltunski
    {
834 ffe7e55d Leszek Koltunski
    initializeStatics();
835
    figureOutVersion(act);
836 1780dcb0 Leszek Koltunski
837 ffe7e55d Leszek Koltunski
    Thread thread = new Thread()
838 1780dcb0 Leszek Koltunski
      {
839 ffe7e55d Leszek Koltunski
      public void run()
840
        {
841 46be3ddf Leszek Koltunski
        downloadThread(receiver);
842 ffe7e55d Leszek Koltunski
        }
843
      };
844 6f8e5dfc Leszek Koltunski
845 ffe7e55d Leszek Koltunski
    thread.start();
846 4f9f99a2 Leszek Koltunski
    }
847 bc511ea5 Leszek Koltunski
848
///////////////////////////////////////////////////////////////////////////////////////////////////
849
850 46be3ddf Leszek Koltunski
  public void submit(ScoresReceiver receiver, final FragmentActivity act)
851 bc511ea5 Leszek Koltunski
    {
852 ffe7e55d Leszek Koltunski
    initializeStatics();
853
    figureOutVersion(act);
854 bc511ea5 Leszek Koltunski
855 ffe7e55d Leszek Koltunski
    Thread thread = new Thread()
856
      {
857
      public void run()
858
        {
859 46be3ddf Leszek Koltunski
        submitThread(receiver);
860 ffe7e55d Leszek Koltunski
        }
861
      };
862 1780dcb0 Leszek Koltunski
863 ffe7e55d Leszek Koltunski
    thread.start();
864 bc511ea5 Leszek Koltunski
    }
865 6a083c6a Leszek Koltunski
866
///////////////////////////////////////////////////////////////////////////////////////////////////
867
868 ffe7e55d Leszek Koltunski
  public void debug()
869 6a083c6a Leszek Koltunski
    {
870 ffe7e55d Leszek Koltunski
    initializeStatics();
871 63dd19c4 Leszek Koltunski
    mDebugState = DEBUG_RUNNING;
872 ffe7e55d Leszek Koltunski
873
    Thread thread = new Thread()
874
      {
875
      public void run()
876
        {
877
        debugThread();
878
        }
879
      };
880
881
    thread.start();
882 6a083c6a Leszek Koltunski
    }
883 1fa125c2 Leszek Koltunski
884
///////////////////////////////////////////////////////////////////////////////////////////////////
885
886 46be3ddf Leszek Koltunski
  public void suspicious(final String suspicious)
887 1fa125c2 Leszek Koltunski
    {
888 ffe7e55d Leszek Koltunski
    initializeStatics();
889
890
    Thread thread = new Thread()
891
      {
892
      public void run()
893
        {
894 46be3ddf Leszek Koltunski
        suspiciousThread(suspicious);
895 ffe7e55d Leszek Koltunski
        }
896
      };
897
898
    thread.start();
899 1fa125c2 Leszek Koltunski
    }
900 63dd19c4 Leszek Koltunski
901
///////////////////////////////////////////////////////////////////////////////////////////////////
902
// Yes it can happen that the second Updatee registers before we sent an update to the first one
903
// and, as a result, the update never gets sent to the first one. This is not a problem (now, when
904
// there are only two updatees - the RubikStatePlay and the UpdateDialog)
905
//
906
// Yes, there is also a remote possibility that the two threads executing this function and executing
907
// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee.
908
// We don't care about such remote possibility, then the app simply would signal that there are no
909
// updates available.
910
911
  public void signUpForUpdates(Updatee updatee)
912
    {
913
         if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates);
914
    else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate();
915
    else mUpdatee = updatee;
916
    }
917 903c7bbc Leszek Koltunski
918 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
919
920 7fe62d1f Leszek Koltunski
  public void downloadIcons(final Context context, final IconReceiver receiver)
921 46be3ddf Leszek Koltunski
    {
922
    initializeStatics();
923
924
    Thread thread = new Thread()
925
      {
926
      public void run()
927
        {
928 7fe62d1f Leszek Koltunski
        iconThread(context,receiver);
929 46be3ddf Leszek Koltunski
        }
930
      };
931
932
    thread.start();
933
    }
934
935
///////////////////////////////////////////////////////////////////////////////////////////////////
936
937
  public void downloadJSON(final RubikUpdates.UpdateInfo info, final Downloadee downloadee)
938 ffe7e55d Leszek Koltunski
    {
939 b88cdd91 Leszek Koltunski
    initializeStatics();
940
941
    Thread thread = new Thread()
942
      {
943
      public void run()
944
        {
945 46be3ddf Leszek Koltunski
        jsonThread(info,downloadee);
946 b88cdd91 Leszek Koltunski
        }
947
      };
948 ffe7e55d Leszek Koltunski
949 b88cdd91 Leszek Koltunski
    thread.start();
950 ffe7e55d Leszek Koltunski
    }
951
952 903c7bbc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
953
954
  public void updateDone(String shortName)
955
    {
956
    mUpdates.updateDone(shortName);
957
    }
958 b8b38548 Leszek Koltunski
}