commit 573c7ef7682ea5e12cf6017acaa9cde4df343c39
Author: leszek <leszek@koltunski.pl>
Date:   Sun Nov 17 13:13:54 2024 +0100

    PhasedSolver app: bugfixes.

diff --git a/src/main/java/org/distorted/phasedsolver/SolverActivity.java b/src/main/java/org/distorted/phasedsolver/SolverActivity.java
index 4b4f595..54afed8 100644
--- a/src/main/java/org/distorted/phasedsolver/SolverActivity.java
+++ b/src/main/java/org/distorted/phasedsolver/SolverActivity.java
@@ -15,6 +15,8 @@ import android.app.Activity;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.view.View;
+import android.widget.Toast;
+
 import androidx.preference.PreferenceManager;
 
 import org.distorted.library.main.DistortedLibrary;
@@ -31,103 +33,134 @@ import org.distorted.os.OSInterface;
 
 public class SolverActivity extends Activity
 {
-    private InitAssets mAsset;
-    private SharedPreferences mPreferences;
-    private SolverLowerPane mLowerPane;
+  private InitAssets mAsset;
+  private SharedPreferences mPreferences;
+  private SolverLowerPane mLowerPane;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    @Override
-    protected void onCreate(Bundle icicle) 
-      {
-      super.onCreate(icicle);
-      mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
-      DistortedLibrary.onCreate();
-      setContentView(R.layout.mainlayout);
-      SolverSurfaceView view = findViewById(R.id.solverView);
-      OSInterface os = view.getInterface();
-      mAsset = new InitAssets(null,null,os);
-      mLowerPane = new SolverLowerPane(this);
-      }
+  @Override
+  protected void onCreate(Bundle icicle)
+    {
+    super.onCreate(icicle);
+    mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+    DistortedLibrary.onCreate();
+    setContentView(R.layout.mainlayout);
+    SolverSurfaceView view = findViewById(R.id.solverView);
+    OSInterface os = view.getInterface();
+    mAsset = new InitAssets(null, null, os);
+    mLowerPane = new SolverLowerPane(this);
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-    
-    @Override
-    protected void onPause() 
-      {
-      super.onPause();
-      SolverSurfaceView view = findViewById(R.id.solverView);
-      view.onPause();
-      DistortedLibrary.onPause();
 
-      savePreferences();
-      }
+  @Override
+  protected void onPause()
+    {
+    super.onPause();
+    SolverSurfaceView view = findViewById(R.id.solverView);
+    view.onPause();
+    DistortedLibrary.onPause();
+
+    savePreferences();
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-    
-    @Override
-    protected void onResume() 
-      {
-      super.onResume();
-      SolverSurfaceView view = findViewById(R.id.solverView);
-      view.onResume();
-      DistortedLibrary.onResume();
 
-      restorePreferences();
+  @Override
+  protected void onResume()
+    {
+    super.onResume();
+    SolverSurfaceView view = findViewById(R.id.solverView);
+    view.onResume();
+    DistortedLibrary.onResume();
+
+    restorePreferences();
+
+    ObjectControl control = getControl();
+    control.changeObject(ListObjects.CUBE_3.ordinal(), MODE_NORM, mAsset);
+    }
 
-      ObjectControl control = getControl();
-      control.changeObject( ListObjects.CUBE_3.ordinal(), MODE_NORM, mAsset);
-      }
-    
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-    
-    @Override
-    protected void onDestroy() 
-      {
-      DistortedLibrary.onDestroy();
-      super.onDestroy();
-      }
+
+  @Override
+  protected void onDestroy()
+    {
+    DistortedLibrary.onDestroy();
+    super.onDestroy();
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    ObjectControl getControl()
-      {
-      SolverSurfaceView view = findViewById(R.id.solverView);
-      return view.getObjectControl();
-      }
+  ObjectControl getControl()
+    {
+    SolverSurfaceView view = findViewById(R.id.solverView);
+    return view.getObjectControl();
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    private void savePreferences()
-      {
-      SharedPreferences.Editor editor = mPreferences.edit();
-      SolverSurfaceView view = findViewById(R.id.solverView);
-      OSInterface os = view.getInterface();
-      os.setEditor(editor);
-      view.getObjectControl().savePreferences();
+  private void savePreferences()
+    {
+    SharedPreferences.Editor editor = mPreferences.edit();
+    SolverSurfaceView view = findViewById(R.id.solverView);
+    OSInterface os = view.getInterface();
+    os.setEditor(editor);
+    view.getObjectControl().savePreferences();
 
-      editor.apply();
-      }
+    editor.apply();
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private void restorePreferences()
+    {
+    SolverSurfaceView view = findViewById(R.id.solverView);
+    OSInterface os = view.getInterface();
+    os.setPreferences(mPreferences);
+    view.getObjectControl().restorePreferences();
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    private void restorePreferences()
+  private void solve(PhasedSolver ps, int[] quats, int[][] moveTable)
+    {
+    int[][] sol = ps.solution(quats);
+    int numPhases = sol.length;
+    int totalLength = 0;
+
+    for(int p=0; p<numPhases; p++)
       {
-      SolverSurfaceView view = findViewById(R.id.solverView);
-      OSInterface os = view.getInterface();
-      os.setPreferences(mPreferences);
-      view.getObjectControl().restorePreferences();
+      int numMoves = sol[p]==null ? -1 : sol[p][0];
+
+      if( numMoves<0 )
+        {
+        String message = "Phase "+p+": FAIL";
+        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
+        return;
+        }
+
+      totalLength += numMoves;
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
+    int[][] moves = new int[totalLength][];
+    int index = 0;
 
-    public void Reset(View v)
+    for( int[] s : sol )
       {
-      ObjectControl control = getControl();
-      control.solveObject();
-      mLowerPane.clearMoves(this);
+      int numMoves = s[0];
+
+      for(int m=1; m<=numMoves; m++)
+        {
+        int mi = s[m];
+        moves[index++] = moveTable[mi];
+        android.util.Log.e("D","move "+m+" : "+moveTable[mi][0]+" "+moveTable[mi][1]+" "+moveTable[mi][2]);
+        }
       }
 
+    mLowerPane.setMoves(this,moves);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     public void Solve(View v)
@@ -157,24 +190,20 @@ public class SolverActivity extends Activity
         @Override
         public void run()
           {
-          int[][] sol = ps.solution(qi);
-          int numPhases = sol.length;
-          android.util.Log.e("D", "Solution, num of phases: "+numPhases);
-
-          for(int p=0; p<numPhases; p++)
-            {
-            int numMoves = sol[p]==null ? -1 : sol[p].length;
-            android.util.Log.e("D", "Phase "+p+" numMoves="+numMoves);
-
-            for(int m=0; m<numMoves; m++)
-              {
-              android.util.Log.e("D", "move "+m+" : "+sol[p][m]);
-              }
-            }
+          solve(ps,qi,moves);
           }
         });
       }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public void Reset(View v)
+      {
+      ObjectControl control = getControl();
+      control.solveObject();
+      mLowerPane.clearMoves(this);
+      }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     public void Left(View v)
diff --git a/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java b/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
index 5b8e2e0..9dfb733 100644
--- a/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
+++ b/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
@@ -47,7 +47,7 @@ public class SolverLowerPane implements MovesFinished
       @Override
       public void run()
         {
-        mText.setText((mCurrMove+1)+"/"+mNumMoves);
+        mText.setText(mCurrMove+"/"+mNumMoves);
         }
       });
     }
@@ -74,17 +74,15 @@ public class SolverLowerPane implements MovesFinished
 
   void backMove(SolverActivity act)
     {
-    if( mCanMove && mCurrMove>0 )
+    if( mMoves!=null && mCanMove && mCurrMove>0 )
       {
       mCanMove = false;
-      mCurrMove--;
       setText(act);
-
-      int[] move = mMoves[mCurrMove];
+      int[] move = mMoves[--mCurrMove];
 
       mControl = act.getControl();
       mControl.blockTouch(MOVES_PLACE_0);
-      mControl.addRotation(this, move[0], (1<<move[1]), -move[2], MILLIS_PER_DEGREE);
+      mControl.addRotation(this, move[0], (1<<move[1]), move[2], MILLIS_PER_DEGREE);
       }
     }
 
@@ -92,17 +90,15 @@ public class SolverLowerPane implements MovesFinished
 
   void makeMove(SolverActivity act)
     {
-    if( mCanMove && mCurrMove<mNumMoves-1 )
+    if( mMoves!=null && mCanMove && mCurrMove<mNumMoves )
       {
       mCanMove = false;
-      mCurrMove++;
       setText(act);
-
-      int[] move = mMoves[mCurrMove];
+      int[] move = mMoves[mCurrMove++];
 
       mControl = act.getControl();
       mControl.blockTouch(MOVES_PLACE_1);
-      mControl.addRotation(this, move[0], (1<<move[1]), move[2], MILLIS_PER_DEGREE);
+      mControl.addRotation(this, move[0], (1<<move[1]), -move[2], MILLIS_PER_DEGREE);
       }
     }
 
