commit d8aa4ba873d00584d8221d2eef4d5e87e67b3b65
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Mon Feb 10 23:55:07 2020 +0000

    Downloading High Scores: optimizations

diff --git a/src/main/java/org/distorted/magic/RubikScoresDownloader.java b/src/main/java/org/distorted/magic/RubikScoresDownloader.java
index bb59e256..46af703c 100644
--- a/src/main/java/org/distorted/magic/RubikScoresDownloader.java
+++ b/src/main/java/org/distorted/magic/RubikScoresDownloader.java
@@ -25,9 +25,11 @@ class RubikScoresDownloader implements Runnable
   {
   interface Receiver
     {
-    void receive(String scores);
+    void receive(String[][][][] scores);
     }
 
+  static final int MAX_PLACES = 10;
+
   private static final int DOWNLOAD   = 0;
   private static final int SUBMIT     = 1;
   private static final int IDLE       = 2;
@@ -40,51 +42,73 @@ class RubikScoresDownloader implements Runnable
   private static Receiver mReceiver;
 
   private static String mScores = "";
+  private static String[][][][] mValues = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES][3];
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  static void onPause()
+  private void fillValues()
     {
-    mRunning = false;
-    mScores = "";
-    }
+    int begin=-1 ,end, len = mScores.length();
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private boolean gottaDownload()
-    {
-    return ((mScores.length()==0) && !mRunning);
+    while( begin<len )
+      {
+      end = mScores.indexOf('\n', begin+1);
+      if( end<0 ) end = len;
+      fillRow(mScores.substring(begin+1,end));
+      begin = end;
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  void abortNetworkTransaction()
+  private void fillRow(String row)
     {
-    mRunning = false;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
+    int s1 = row.indexOf(' ');
+    int s2 = row.indexOf(' ',s1+1);
+    int s3 = row.indexOf(' ',s2+1);
+    int s4 = row.indexOf(' ',s3+1);
+    int s5 = row.indexOf(' ',s4+1);
+    int s6 = row.length();
+
+    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
+      {
+      int size = Integer.valueOf( row.substring(0,s1) );
 
-  void download(Receiver receiver)
-    {
-    mReceiver = receiver;
-    mMode = DOWNLOAD;
-    mNetworkThrd = new Thread(this);
-    mNetworkThrd.start();
+      if( size>=0 && size<RubikSize.LENGTH )
+        {
+        int level      = Integer.valueOf( row.substring(s1+1,s2) );
+        int place      = Integer.valueOf( row.substring(s2+1,s3) );
+        String name    = row.substring(s3+1, s4);
+        int time       = Integer.valueOf( row.substring(s4+1,s5) );
+        String country = row.substring(s5+1, s6);
+        String realTime= String.valueOf(time/10.0f);
+
+        if(level>=0 && level<RubikActivity.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
+          {
+          mValues[size][level][place][0] = country;
+          mValues[size][level][place][1] = name;
+          mValues[size][level][place][2] = realTime;
+          }
+        else
+          {
+          android.util.Log.e("fillRow", "row invalid: level or place invalid: "+row);
+          }
+        }
+      else
+        {
+        android.util.Log.e("fillRow", "row invalid: size invalid: "+row);
+        }
+      }
+    else
+      {
+      android.util.Log.e("fillRow", "row invalid: "+row);
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  public void run()
+  private void doDownload()
     {
-    try
-      {
-      if( gottaDownload() )
-        {
-        mRunning = true;
-
-        Thread.sleep(2000);
-
 mScores =
 
 "0 0 0 INEED7X7X7 1 rus" + "\n" +
@@ -167,14 +191,59 @@ mScores =
 "3 3 2 INEED7X7X7 24 rus" + "\n" +
 "3 3 3 RUBIK123 30 lit" + "\n" +
 "3 3 4 SKY16 31 usa";
-      }
     }
-  catch( Exception e )
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private boolean gottaDownload()
     {
-    android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
+    return ((mScores.length()==0) && !mRunning);
     }
 
-  mRunning = false;
-  mReceiver.receive(mScores);
-  }
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  static void onPause()
+    {
+    mRunning = false;
+    mScores = "";
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void abortNetworkTransaction()
+    {
+    mRunning = false;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void download(Receiver receiver)
+    {
+    mReceiver = receiver;
+    mMode = DOWNLOAD;
+    mNetworkThrd = new Thread(this);
+    mNetworkThrd.start();
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public void run()
+    {
+    try
+      {
+      if( gottaDownload() )
+        {
+        mRunning = true;
+        doDownload();
+        fillValues();
+        }
+      }
+    catch( Exception e )
+      {
+      android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
+      }
+
+    mRunning = false;
+    mReceiver.receive(mValues);
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/org/distorted/magic/RubikScoresView.java b/src/main/java/org/distorted/magic/RubikScoresView.java
index 1419bab9..457ed70c 100644
--- a/src/main/java/org/distorted/magic/RubikScoresView.java
+++ b/src/main/java/org/distorted/magic/RubikScoresView.java
@@ -67,14 +67,7 @@ public class RubikScoresView extends FrameLayout
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int getLevelLayoutID(int level)
-    {
-    return 100+level;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  public void prepareView(FragmentActivity act)
+  void prepareView(FragmentActivity act, final String[][][] values)
     {
     removeAllViews();
 
@@ -82,41 +75,35 @@ public class RubikScoresView extends FrameLayout
     LinearLayout layout = tab.findViewById(R.id.tabLayout);
     addView(tab);
 
-    for(int i=1; i<=RubikActivity.MAX_SCRAMBLE; i++)
+    for(int i=0; i<RubikActivity.MAX_SCRAMBLE; i++)
       {
-      View level = inflate(act, R.layout.level_records, null);
-      level.setId(getLevelLayoutID(i));
+      LinearLayout level = (LinearLayout)inflate(act, R.layout.level_records, null);
       TextView text = level.findViewById(R.id.levelTitle);
-      text.setText(act.getString(R.string.sc_placeholder,i));
+      text.setText(act.getString(R.string.sc_placeholder,(i+1)));
 
-      layout.addView(level);
-      }
-    }
+      for(int j=0; j<RubikScoresDownloader.MAX_PLACES; j++)
+        {
+        String coun = values[i][j][0];
+        String name = values[i][j][1];
+        String time = values[i][j][2];
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  public void fillRow(FragmentActivity act, int level, String place, int time, String name, String country)
-    {
-    if( level>=0 && level<RubikActivity.MAX_SCRAMBLE )
-      {
-      LinearLayout layout = findViewById(getLevelLayoutID(level+1));
-      View rowLay = inflate(act, R.layout.level_row, null);
+        if( name!=null && name.length()>0 )
+          {
+          View row = inflate(act, R.layout.level_row, null);
 
-      TextView textName    = rowLay.findViewById(R.id.level_row_name);
-      TextView textTime    = rowLay.findViewById(R.id.level_row_time);
-      TextView textCountry = rowLay.findViewById(R.id.level_row_country);
+          TextView textCoun = row.findViewById(R.id.level_row_country);
+          TextView textName = row.findViewById(R.id.level_row_name);
+          TextView textTime = row.findViewById(R.id.level_row_time);
 
-      float real_time = time/10.0f;
+          textCoun.setText(coun);
+          textName.setText(name);
+          textTime.setText(time);
 
-      textName.setText(name);
-      textTime.setText( String.valueOf(real_time) );
-      textCountry.setText(country);
+          level.addView(row);
+          }
+        }
 
-      layout.addView(rowLay);
-      }
-    else
-      {
-      android.util.Log.e("receive", "level invalid: "+level);
+      layout.addView(level);
       }
     }
   }
diff --git a/src/main/java/org/distorted/magic/RubikScoresViewPager.java b/src/main/java/org/distorted/magic/RubikScoresViewPager.java
index 6dd78c4d..66d5adaa 100644
--- a/src/main/java/org/distorted/magic/RubikScoresViewPager.java
+++ b/src/main/java/org/distorted/magic/RubikScoresViewPager.java
@@ -45,7 +45,7 @@ class RubikScoresViewPager extends PagerAdapter implements RubikScoresDownloader
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  public void receive(final String scores)
+  public void receive(final String[][][][] scores)
     {
     mAct.runOnUiThread(new Runnable()
       {
@@ -54,57 +54,12 @@ class RubikScoresViewPager extends PagerAdapter implements RubikScoresDownloader
         {
         for(int i=0; i<RubikSize.LENGTH; i++)
           {
-          mViews[i].prepareView(mAct);
-          }
-
-        int begin=-1 ,end, len = scores.length();
-
-        while( begin<len )
-          {
-          end = scores.indexOf('\n', begin+1);
-          if( end<0 ) end = len;
-          sendRow(scores.substring(begin+1,end));
-          begin = end;
+          mViews[i].prepareView(mAct,scores[i]);
           }
         }
       });
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private void sendRow(String row)
-    {
-    int s1 = row.indexOf(' ');
-    int s2 = row.indexOf(' ',s1+1);
-    int s3 = row.indexOf(' ',s2+1);
-    int s4 = row.indexOf(' ',s3+1);
-    int s5 = row.indexOf(' ',s4+1);
-    int s6 = row.length();
-
-    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
-      {
-      int size       = Integer.valueOf( row.substring(   0,s1) );
-      int levl       = Integer.valueOf( row.substring(s1+1,s2) );
-      String place   = row.substring(s2+1, s3);
-      String name    = row.substring(s3+1, s4);
-      int time       = Integer.valueOf( row.substring(s4+1,s5) );
-      String country = row.substring(s5+1, s6);
-
-      if( size>=0 && size< RubikSize.LENGTH )
-        {
-        mViews[size].fillRow(mAct, levl, place, time, name, country);
-        }
-      else
-        {
-        android.util.Log.e("receive", "row invalid: size invalid: "+row);
-        }
-      }
-    else
-      {
-      android.util.Log.e("receive", "row invalid: "+row);
-      }
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   @Override
diff --git a/src/main/res/layout/level_records.xml b/src/main/res/layout/level_records.xml
index 91579cbe..a95180a0 100644
--- a/src/main/res/layout/level_records.xml
+++ b/src/main/res/layout/level_records.xml
@@ -14,11 +14,11 @@
     <TextView
         android:id="@+id/levelTitle"
         android:layout_width="match_parent"
-        android:layout_height="40dp"
-        android:textSize="26sp"
+        android:layout_height="32dp"
+        android:textSize="24sp"
         android:gravity="center"
         android:background="@color/grey"
-        android:paddingBottom="5dp"
+        android:paddingBottom="0dp"
         android:paddingTop="0dp"
         android:paddingLeft="5dp"
         android:paddingRight="5dp"/>
