Project

General

Profile

« Previous | Next » 

Revision 1c04d054

Added by Leszek Koltunski 7 months ago

Major progress to version 2.0.0.

View differences:

src/main/java/org/distorted/external/RubikNetwork.java
18 18
import java.net.UnknownHostException;
19 19
import java.security.MessageDigest;
20 20
import java.security.NoSuchAlgorithmException;
21
import java.util.ArrayList;
21 22

  
22 23
import android.app.Activity;
23 24
import android.content.Context;
......
26 27
import android.graphics.Bitmap;
27 28
import android.graphics.BitmapFactory;
28 29

  
29
import org.distorted.library.main.DistortedLibrary;
30 30
import org.distorted.objectlib.json.JsonWriter;
31 31
import org.distorted.objects.RubikObjectList;
32 32

  
33
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
33
import static org.distorted.main_old.RubikActivity.SHOW_DOWNLOADED_DEBUG;
34 34
import static org.distorted.screens.RubikScreenPlay.LEVELS_SHOWN;
35 35

  
36 36
///////////////////////////////////////////////////////////////////////////////////////////////////
......
51 51

  
52 52
  public interface Updatee
53 53
    {
54
    int getType();
54 55
    void receiveUpdate(RubikUpdates update);
56
    void objectDownloaded(String shortName);
55 57
    void errorUpdate();
56 58
    }
57 59

  
......
62 64

  
63 65
  public static final int MAX_PLACES = 10;
64 66

  
65
  private static final int REND_ADRENO= 0;
66
  private static final int REND_MALI  = 1;
67
  private static final int REND_POWER = 2;
68
  private static final int REND_OTHER = 3;
69

  
70
  private static final int DEBUG_RUNNING = 1;
71
  private static final int DEBUG_SUCCESS = 2;
72
  private static final int DEBUG_FAILURE = 3;
67
  private static final int UPDATES_RUNNING = 1;
68
  private static final int UPDATES_SUCCESS = 2;
69
  private static final int UPDATES_FAILURE = 3;
73 70

  
74 71
  private final String[] hex = {
75 72
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
......
116 113
  private static RubikNetwork mThis;
117 114
  private static String mScores = "";
118 115
  private static boolean mRunning = false;
119
  private static Updatee mUpdatee;
116
  private static ArrayList<Updatee> mUpdateeList;
120 117
  private static String mVersion;
121 118
  private static int mNumObjects;
122 119
  private static RubikUpdates mUpdates;
123
  private static int mDebugState;
120
  private static int mUpdatesState;
124 121

  
125 122
///////////////////////////////////////////////////////////////////////////////////////////////////
126 123

  
......
285 282
      }
286 283
    }
287 284

  
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

  
290
  private int getRendererType(String renderer)
291
    {
292
    if( renderer.contains("Adreno")  ) return REND_ADRENO;
293
    if( renderer.contains("Mali")    ) return REND_MALI;
294
    if( renderer.contains("PowerVR") ) return REND_POWER;
295

  
296
    return REND_OTHER;
297
    }
298

  
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

  
310
    if( type==REND_MALI )
311
      {
312
      int firstHyphen = renderer.indexOf('-');
313
      String ret = renderer.substring(firstHyphen+1);
314
      return URLencode(ret);
315
      }
316

  
317
    return "other";
318
    }
319

  
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

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

  
342 285
///////////////////////////////////////////////////////////////////////////////////////////////////
343 286

  
344 287
  private String URLencode(String s)
......
456 399

  
457 400
///////////////////////////////////////////////////////////////////////////////////////////////////
458 401

  
459
  private String constructDebugURL()
402
  private String constructUpdatesURL()
460 403
    {
461
    RubikScores scores = RubikScores.getInstance();
462
    String name = URLencode(scores.getName());
463
    int numRuns = scores.getNumRuns();
464
    int numPlay = scores.getNumPlays();
465
    String country = scores.getCountry();
466
    String renderer = DistortedLibrary.getDriverRenderer();
467
    String version  = DistortedLibrary.getDriverVersion();
404
    RubikScores sco = RubikScores.getInstance();
405
    String name     = URLencode(sco.getName());
406
    int numRuns     = sco.getNumRuns();
407
    int numPlay     = sco.getNumPlays();
408
    String country  = sco.getCountry();
468 409
    int objectAPI   = JsonWriter.VERSION_OBJECT_APP;
469 410
    int tutorialAPI = JsonWriter.VERSION_EXTRAS_APP;
470
    int numStars    = scores.getNumStars();
471

  
472
    renderer = URLencode(renderer);
473
    version  = URLencode(version);
474 411

  
475
    String url=SERVER+"debugs.cgi";
476
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
477
    url += "&d="+renderer+"&v="+version+"&a="+objectAPI+"&b="+tutorialAPI+"&s="+numStars;
412
    String url=SERVER+"updates.cgi";
413
    url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d"+"&a="+objectAPI+"&b="+tutorialAPI;
478 414

  
479 415
    return url;
480 416
    }
......
507 443
    long epoch = System.currentTimeMillis();
508 444
    String salt = "cuboid";
509 445

  
510
    String renderer = DistortedLibrary.getDriverRenderer();
511
    String version  = DistortedLibrary.getDriverVersion();
512

  
513
    int type = getRendererType(renderer);
514
    renderer = parseRenderer(type,renderer);
515
    version  = parseVersion(type,version);
516

  
517 446
    String url1=SERVER+"submit.cgi";
518 447
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion;
519
    url2 += "&d="+renderer+"&s="+version+reclist+"&c="+country+"&f="+epoch;
448
    url2 += reclist+"&c="+country+"&f="+epoch;
520 449
    String hash = computeHash( url2, salt.getBytes() );
521 450

  
522 451
    return url1 + "?" + url2 + "&h=" + hash;
......
608 537

  
609 538
///////////////////////////////////////////////////////////////////////////////////////////////////
610 539

  
611
  private void debugThread()
540
  private void updatesThread()
612 541
    {
613
    String url = constructDebugURL();
542
    String url = constructUpdatesURL();
614 543

  
615 544
    try
616 545
      {
......
628 557
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
629 558
      StringBuilder answer = new StringBuilder();
630 559

  
631
      for (String line; (line = r.readLine()) != null; )
560
      for( String line; (line = r.readLine()) != null; )
632 561
        {
633 562
        answer.append(line).append('\n');
634 563
        }
......
637 566
      conn.disconnect();
638 567
      mUpdates.parse(updates);
639 568

  
640
      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
641
      mDebugState = DEBUG_SUCCESS;
569
      if( mUpdateeList!=null )
570
        {
571
        int numUpdatees = mUpdateeList.size();
572

  
573
        for(int u=0; u<numUpdatees; u++)
574
          {
575
          Updatee upd = mUpdateeList.get(u);
576
          upd.receiveUpdate(mUpdates);
577
          }
578
        }
579

  
580
      mUpdatesState = UPDATES_SUCCESS;
642 581
      }
643 582
    catch( final Exception e )
644 583
      {
645
      if( mUpdatee!=null ) mUpdatee.errorUpdate();
646
      mDebugState = DEBUG_FAILURE;
584
      if( mUpdateeList!=null )
585
        {
586
        int numUpdatees = mUpdateeList.size();
587

  
588
        for(int u=0; u<numUpdatees; u++)
589
          {
590
          Updatee upd = mUpdateeList.get(u);
591
          upd.errorUpdate();
592
          }
593
        }
594

  
595
      mUpdatesState = UPDATES_FAILURE;
647 596
      }
648 597
    }
649 598

  
......
846 795
  public static void onPause()
847 796
    {
848 797
    mRunning = false;
798
    mUpdateeList.clear();
799
    mUpdatesState = UPDATES_RUNNING;
849 800
    }
850 801

  
851 802
///////////////////////////////////////////////////////////////////////////////////////////////////
......
894 845

  
895 846
///////////////////////////////////////////////////////////////////////////////////////////////////
896 847

  
897
  public void debug(final Activity act)
848
  public void downloadUpdates(final Activity act)
898 849
    {
899 850
    initializeStatics();
900 851
    figureOutVersion(act);
901
    mDebugState = DEBUG_RUNNING;
852
    mUpdatesState = UPDATES_RUNNING;
902 853

  
903 854
    Thread thread = new Thread()
904 855
      {
905 856
      public void run()
906 857
        {
907
        debugThread();
858
        updatesThread();
908 859
        }
909 860
      };
910 861

  
......
947 898
    }
948 899

  
949 900
///////////////////////////////////////////////////////////////////////////////////////////////////
950
// Yes it can happen that the second Updatee registers before we sent an update to the first one
951
// and, as a result, the update never gets sent to the first one. This is not a problem (now, when
952
// there are only two updatees - the RubikStatePlay and the UpdateDialog)
953
//
954
// Yes, there is also a remote possibility that the two threads executing this function and executing
955
// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee.
956
// We don't care about such remote possibility, then the app simply would signal that there are no
957
// updates available.
958 901

  
959 902
  public void signUpForUpdates(Updatee updatee)
960 903
    {
961
         if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates);
962
    else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate();
963
    else mUpdatee = updatee;
904
    if( mUpdateeList==null ) mUpdateeList = new ArrayList<>();
905

  
906
    int numUpdatees = mUpdateeList.size();
907
    int type = updatee.getType();
908

  
909
    for(int u=0; u<numUpdatees; u++)
910
      {
911
      Updatee upd = mUpdateeList.get(u);
912

  
913
      if( upd.getType()==type )
914
        {
915
        mUpdateeList.remove(u);
916
        break;
917
        }
918
      }
919

  
920
    mUpdateeList.add(updatee);
921

  
922
    if( mUpdatesState==UPDATES_SUCCESS )
923
      {
924
      updatee.receiveUpdate(mUpdates);
925
      }
926
    else if( mUpdatesState==UPDATES_FAILURE )
927
      {
928
      updatee.errorUpdate();
929
      }
964 930
    }
965 931

  
966 932
///////////////////////////////////////////////////////////////////////////////////////////////////
......
1003 969
    {
1004 970
    mUpdates.updateDone(shortName);
1005 971
    mScores = "";
972

  
973
    if( mUpdateeList!=null )
974
      {
975
      int numUpdatees = mUpdateeList.size();
976

  
977
      for(int u=0; u<numUpdatees; u++)
978
        {
979
        Updatee upd = mUpdateeList.get(u);
980
        upd.objectDownloaded(shortName);
981
        }
982
      }
1006 983
    }
1007 984
}

Also available in: Unified diff