commit 63dd19c47a798a17959af1e4eefc82c3823711b4
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Jan 21 15:56:54 2022 +0100

    Progress downloading updates.

diff --git a/src/main/java/org/distorted/main/RubikRenderer.java b/src/main/java/org/distorted/main/RubikRenderer.java
index b9634d72..efd8dd0c 100644
--- a/src/main/java/org/distorted/main/RubikRenderer.java
+++ b/src/main/java/org/distorted/main/RubikRenderer.java
@@ -136,7 +136,7 @@ public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.E
 
       DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
 
-      if( !BuildConfig.DEBUG && !mDebugSent )
+      if( /*!BuildConfig.DEBUG &&*/ !mDebugSent )
         {
         mDebugSent= true;
         RubikNetwork network = RubikNetwork.getInstance();
diff --git a/src/main/java/org/distorted/network/RubikNetwork.java b/src/main/java/org/distorted/network/RubikNetwork.java
index 283f1ea6..d8c240b0 100644
--- a/src/main/java/org/distorted/network/RubikNetwork.java
+++ b/src/main/java/org/distorted/network/RubikNetwork.java
@@ -52,6 +52,12 @@ public class RubikNetwork implements Runnable
     void error(String error);
     }
 
+  public interface Updatee
+    {
+    void receiveUpdate(RubikUpdates update);
+    void errorUpdate();
+    }
+
   public static final int MAX_PLACES = 10;
 
   private static final int DOWNLOAD   = 0;
@@ -65,6 +71,11 @@ public class RubikNetwork implements Runnable
   private static final int REND_POWER = 2;
   private static final int REND_OTHER = 3;
 
+  private static final int DEBUG_NOT_YET = 0;
+  private static final int DEBUG_RUNNING = 1;
+  private static final int DEBUG_SUCCESS = 2;
+  private static final int DEBUG_FAILURE = 3;
+
   private final String[] hex = {
     "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
     "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
@@ -110,10 +121,12 @@ public class RubikNetwork implements Runnable
   private static boolean mRunning = false;
   private static int mMode = IDLE;
   private static Receiver mReceiver;
+  private static Updatee mUpdatee;
   private static String mVersion;
-  private static String mDebug;
+  private static String mSuspicious;
   private static int mNumObjects;
   private static RubikUpdates mUpdates;
+  private static int mDebugState;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -396,10 +409,13 @@ public class RubikNetwork implements Runnable
 
       String updates = answer.toString();
       mUpdates.parse(updates);
+      if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates);
+      mDebugState = DEBUG_SUCCESS;
       }
     catch( final Exception e )
       {
-      // ignore
+      if( mUpdatee!=null ) mUpdatee.errorUpdate();
+      mDebugState = DEBUG_FAILURE;
       }
     }
 
@@ -486,10 +502,10 @@ public class RubikNetwork implements Runnable
     {
     RubikScores scores = RubikScores.getInstance();
     int deviceID= scores.getDeviceID();
-    String debug = URLencode(mDebug);
+    String suspicious = URLencode(mSuspicious);
 
     String url="https://distorted.org/magic/cgi-bin/suspicious.cgi";
-    url += "?i="+deviceID+"&d="+debug;
+    url += "?i="+deviceID+"&d="+suspicious;
 
     return url;
     }
@@ -699,14 +715,32 @@ public class RubikNetwork implements Runnable
 
   public void debug(AppCompatActivity act)
     {
+    mDebugState = DEBUG_RUNNING;
     start(null, act, DEBUG);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  public void suspicious(AppCompatActivity act, String debug)
+  public void suspicious(AppCompatActivity act, String suspicious)
     {
-    mDebug = debug;
+    mSuspicious = suspicious;
     start(null, act, SUSPICIOUS);
     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Yes it can happen that the second Updatee registers before we sent an update to the first one
+// and, as a result, the update never gets sent to the first one. This is not a problem (now, when
+// there are only two updatees - the RubikStatePlay and the UpdateDialog)
+//
+// Yes, there is also a remote possibility that the two threads executing this function and executing
+// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee.
+// We don't care about such remote possibility, then the app simply would signal that there are no
+// updates available.
+
+  public void signUpForUpdates(Updatee updatee)
+    {
+         if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates);
+    else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate();
+    else mUpdatee = updatee;
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/org/distorted/network/RubikUpdates.java b/src/main/java/org/distorted/network/RubikUpdates.java
index b37c80c3..3d3b2d36 100644
--- a/src/main/java/org/distorted/network/RubikUpdates.java
+++ b/src/main/java/org/distorted/network/RubikUpdates.java
@@ -140,12 +140,16 @@ public class RubikUpdates
         if( elements.length>=3 ) parseLine(elements);
         }
       }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
 
+  public void showDebug()
+    {
     android.util.Log.e("D", "url: "+mUrl);
     android.util.Log.e("D", "new objects: "+debug(mNewObjects));
     android.util.Log.e("D", "new extras : "+debug(mNewExtras ));
     android.util.Log.e("D", "upd objects: "+debug(mUpdObjects));
     android.util.Log.e("D", "upd extras : "+debug(mUpdExtras ));
     }
-
 }
\ No newline at end of file
diff --git a/src/main/java/org/distorted/screens/RubikScreenPlay.java b/src/main/java/org/distorted/screens/RubikScreenPlay.java
index e5bd2b97..a248245e 100644
--- a/src/main/java/org/distorted/screens/RubikScreenPlay.java
+++ b/src/main/java/org/distorted/screens/RubikScreenPlay.java
@@ -35,6 +35,8 @@ import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.PopupWindow;
 
+import org.distorted.network.RubikNetwork;
+import org.distorted.network.RubikUpdates;
 import org.distorted.objectlib.main.ObjectControl;
 
 import org.distorted.main.R;
@@ -53,7 +55,7 @@ import static android.view.View.inflate;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-public class RubikScreenPlay extends RubikScreenBase
+public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
   {
   public static final int NUM_COLUMNS  = 5;
   public static final int LEVELS_SHOWN = 10;
@@ -229,6 +231,9 @@ public class RubikScreenPlay extends RubikScreenBase
     mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
     mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
 
+    RubikNetwork network = RubikNetwork.getInstance();
+    network.signUpForUpdates(this);
+
     LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
     GridLayout objectGrid = view.findViewById(R.id.objectGrid);
 
@@ -676,4 +681,18 @@ public class RubikScreenPlay extends RubikScreenBase
     {
     return mShouldReactToEndOfScrambling;
     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public void receiveUpdate(RubikUpdates updates)
+    {
+    updates.showDebug();
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public void errorUpdate()
+    {
+    android.util.Log.e("D", "Error receiving update");
+    }
   }
