commit 82ce8e645dc67c025ca03fbc3313743220cc73f3
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Mar 20 15:40:24 2020 +0000

    Progress with getting/setting Country and DeviceID.

diff --git a/src/main/java/org/distorted/dialog/RubikDialogScoresView.java b/src/main/java/org/distorted/dialog/RubikDialogScoresView.java
index 8e15ad0d..d3f83ba9 100644
--- a/src/main/java/org/distorted/dialog/RubikDialogScoresView.java
+++ b/src/main/java/org/distorted/dialog/RubikDialogScoresView.java
@@ -85,7 +85,7 @@ public class RubikDialogScoresView extends FrameLayout
     float myRecordInSeconds = (myRecordInMillis/100)/10.0f;
     boolean mySubmitted = scores.isSubmitted(object, size, scramble);
     String myName = scores.getName();
-    int myCountryID = scores.getCountryID();
+    int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
     String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? Float.toString(myRecordInSeconds) : "??";
     String theirTime;
     int theirCountryID;
@@ -106,7 +106,7 @@ public class RubikDialogScoresView extends FrameLayout
 
         theirCountryID = res.getIdentifier( country[j], "drawable", packageName);
         theirTime = Float.toString(time[j]);
-        View row = createRow(act, theirCountryID, name[j], theirTime, white);
+        View row = createRow(act, theirCountryID, name[j], theirTime, name[j].equals(myName) ? red:white);
         level.addView(row);
         }
       }
diff --git a/src/main/java/org/distorted/magic/RubikActivity.java b/src/main/java/org/distorted/magic/RubikActivity.java
index e2a463db..b8393018 100644
--- a/src/main/java/org/distorted/magic/RubikActivity.java
+++ b/src/main/java/org/distorted/magic/RubikActivity.java
@@ -84,7 +84,9 @@ public class RubikActivity extends AppCompatActivity implements View.OnClickList
       if( mJustStarted )
         {
         mJustStarted = false;
-        RubikScores.getInstance().incrementNumRuns();
+        RubikScores scores = RubikScores.getInstance();
+        scores.incrementNumRuns();
+        scores.setCountry(this);
         }
 
       int object = play.getObject();
diff --git a/src/main/java/org/distorted/scores/RubikScores.java b/src/main/java/org/distorted/scores/RubikScores.java
index 44f6a68f..6d7d4edb 100644
--- a/src/main/java/org/distorted/scores/RubikScores.java
+++ b/src/main/java/org/distorted/scores/RubikScores.java
@@ -19,17 +19,20 @@
 
 package org.distorted.scores;
 
+import android.content.Context;
 import android.content.SharedPreferences;
+import android.telephony.TelephonyManager;
 
-import org.distorted.magic.R;
 import org.distorted.object.RubikObjectList;
 
+import java.util.UUID;
+
 import static org.distorted.object.RubikObjectList.MAX_SIZE;
 import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
 import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// hold my own scores, some other statistics.
+// hold my own scores, and some other statistics.
 
 public class RubikScores
   {
@@ -39,11 +42,11 @@ public class RubikScores
   private long[][][] mRecords;
   private int [][][] mSubmitted;
 
-  private String mName;
+  private String mName, mCountry;
   private boolean mNameIsVerified;
   private int mNumRuns;
   private int mNumPlays;
-  private int mCountryID;
+  private int mDeviceID;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -61,12 +64,37 @@ public class RubikScores
           }
 
     mName = "YOU";
+    mCountry = "un";
+
     mNameIsVerified = false;
+
     mNumPlays= -1;
     mNumRuns = -1;
-    mCountryID = R.drawable.un;
+    mDeviceID= -1;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/*
+  private static int computeHash()
+    {
+    final int MODULO = 227;
+    int ret = 0;
+
+    for(int i=0; i<MAX_CUBE-1; i++)
+      ret += (2*i+3)*passedlevel[i];
+
+    for(int i=0; i<MAX_CUBE-1; i++)
+      for(int j=0; j<MAX_LEVELS; j++)
+        ret += (i*i+3*j)*records[i][j];
+
+    int length = veriname==null ? 0 : veriname.length();
+
+    for(int i=0;i<length;i++)
+      ret += i*veriname.charAt(i);
+
+    return (ret%=MODULO);
+    }
+*/
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   public static RubikScores getInstance()
@@ -118,6 +146,7 @@ public class RubikScores
     editor.putBoolean("scores_isVerified", mNameIsVerified);
     editor.putInt("scores_numPlays", mNumPlays);
     editor.putInt("scores_numRuns" , mNumRuns);
+    editor.putInt("scores_deviceid", mDeviceID);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -181,6 +210,9 @@ public class RubikScores
     mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
     mNumPlays       = preferences.getInt("scores_numPlays", 0);
     mNumRuns        = preferences.getInt("scores_numRuns" , 0);
+    mDeviceID       = preferences.getInt("scores_deviceid",-1);
+
+    if( mDeviceID==-1 ) mDeviceID = getDeviceID();
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -232,24 +264,60 @@ public class RubikScores
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // TODO
 
-  public void setCountry(int country)
+  public void setSubmitted(int object, int size, int scramble)
     {
-    mCountryID = country;
+    int maxsize = RubikObjectList.getObject(object).getSizes().length;
+
+    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
+      {
+      mSubmitted[object][size][scramble] = 1;
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// TODO
 
-  public void setSubmitted(int object, int size, int scramble)
+  public void setCountry(Context context)
     {
-    int maxsize = RubikObjectList.getObject(object).getSizes().length;
+    TelephonyManager tM =((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
 
-    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
+    if( tM!=null )
       {
-      mSubmitted[object][size][scramble] = 1;
+      mCountry = tM.getSimCountryIso();
+
+      if( mCountry==null || mCountry.length()<=1 )
+        {
+        mCountry = tM.getNetworkCountryIso();
+        }
+      }
+
+    // Special case: Dominicana. Its ISO-3166-alpha-2 country code is 'do' which we can't have here
+    // because we later on map this to a resource name (the flag) and 'do' is a resevred Java keyword
+    // and can't be a resource name.
+
+    if( mCountry.equals("do") ) mCountry = "dm";
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int getDeviceID()
+    {
+    int id;
+
+    try
+      {
+      String s = UUID.randomUUID().toString();
+      id = (s!=null ? s.hashCode():0);
+      }
+    catch(Exception ex)
+      {
+      id = 0;
+      android.util.Log.e("scores", "Exception in getDeviceID()");
       }
+
+    return id<0 ? -id : id;
     }
 
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   public long getRecord(int object, int size, int scramble)
@@ -310,8 +378,8 @@ public class RubikScores
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  public int getCountryID()
+  public String getCountry()
     {
-    return mCountryID;
+    return mCountry;
     }
   }
