commit dc78f3952ff377399625993c14d7d068d506c835
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue Sep 7 14:34:24 2021 +0200

    Fix play screen popups in case the whole app is in 'multi-window' or 'pop-up window' state.

diff --git a/src/main/java/org/distorted/screens/RubikScreenPlay.java b/src/main/java/org/distorted/screens/RubikScreenPlay.java
index 0fd02602..332dc512 100644
--- a/src/main/java/org/distorted/screens/RubikScreenPlay.java
+++ b/src/main/java/org/distorted/screens/RubikScreenPlay.java
@@ -24,7 +24,6 @@ import android.content.SharedPreferences;
 import android.graphics.drawable.BitmapDrawable;
 import android.os.Bundle;
 import android.util.TypedValue;
-import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
@@ -34,7 +33,6 @@ import android.widget.LinearLayout;
 import android.widget.PopupWindow;
 import android.widget.ScrollView;
 
-import org.distorted.control.RubikControl;
 import org.distorted.dialogs.RubikDialogAbout;
 import org.distorted.dialogs.RubikDialogPattern;
 import org.distorted.dialogs.RubikDialogScores;
@@ -64,6 +62,7 @@ public class RubikScreenPlay extends RubikScreenBase
 
   private static final int NUM_BUTTONS = BUTTON_LABELS.length;
   private static final float LAST_BUTTON = 1.5f;
+  private static final int[] mLocation = new int[2];
 
   private ImageButton mObjButton, mMenuButton, mSolveButton;
   private Button mPlayButton;
@@ -122,7 +121,9 @@ public class RubikScreenPlay extends RubikScreenBase
   private void setupObjectButton(final RubikActivity act, final float width)
     {
     final int margin  = (int)(width*RubikActivity.MARGIN);
+    final int upperBarHeight = act.getHeightUpperBar();
     final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
+
     mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
 
     mObjButton.setOnClickListener( new View.OnClickListener()
@@ -133,12 +134,15 @@ public class RubikScreenPlay extends RubikScreenBase
         if( mObjectPopup!=null && act.getPreRender().isUINotBlocked())
           {
           int rowCount = Math.min(mMaxRowCount,mRowCount);
-
           View popupView = mObjectPopup.getContentView();
           popupView.setSystemUiVisibility(RubikActivity.FLAGS);
 
-          mObjectPopup.showAsDropDown(view, margin, margin);
-          mObjectPopup.update(view, mObjectSize*mColCount, mObjectSize*rowCount);
+          view.getLocationOnScreen(mLocation);
+          final int x = mLocation[0];
+          final int y = mLocation[1]+upperBarHeight+margin;
+
+          mObjectPopup.showAsDropDown(view);
+          mObjectPopup.update(x,y,mObjectSize*mColCount,mObjectSize*rowCount);
           }
         }
       });
@@ -149,7 +153,7 @@ public class RubikScreenPlay extends RubikScreenBase
   private void setupPlayButton(final RubikActivity act, final float width, final float height)
     {
     final int margin   = (int)(width*RubikActivity.MARGIN);
-    int upperBarHeight = act.getHeightUpperBar();
+    final int upperBarHeight = act.getHeightUpperBar();
     final int maxHeight= (int)(0.9f*(height-upperBarHeight) );
 
     mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
@@ -164,13 +168,19 @@ public class RubikScreenPlay extends RubikScreenBase
           View popupView = mPlayPopup.getContentView();
           popupView.setSystemUiVisibility(RubikActivity.FLAGS);
 
+          view.getLocationOnScreen(mLocation);
+          int width = view.getWidth();
+
           final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
           final int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
           final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
           final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
+          final int realHeight  = Math.min(popupHeight,maxHeight);
+          final int x = mLocation[0]+width-mPlayLayoutWidth;
+          final int y = mLocation[1]+upperBarHeight+margin;
 
-          mPlayPopup.showAsDropDown(view, margin, margin);
-          mPlayPopup.update(view, mPlayLayoutWidth, Math.min(popupHeight,maxHeight));
+          mPlayPopup.showAsDropDown(view);
+          mPlayPopup.update(x,y,mPlayLayoutWidth,realHeight);
           }
         }
       });
@@ -182,6 +192,8 @@ public class RubikScreenPlay extends RubikScreenBase
     {
     final int margin  = (int)(width*RubikActivity.MARGIN);
     final int icon = RubikActivity.getDrawable(R.drawable.ui_small_menu,R.drawable.ui_medium_menu, R.drawable.ui_big_menu, R.drawable.ui_huge_menu);
+    final int upperBarHeight = act.getHeightUpperBar();
+
     mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
 
     mMenuButton.setOnClickListener( new View.OnClickListener()
@@ -194,8 +206,13 @@ public class RubikScreenPlay extends RubikScreenBase
           View popupView = mMenuPopup.getContentView();
           popupView.setSystemUiVisibility(RubikActivity.FLAGS);
 
-          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
-          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
+          view.getLocationOnScreen(mLocation);
+          int width = view.getWidth();
+          final int x = mLocation[0]+(width-mMenuLayoutWidth)/2;
+          final int y = mLocation[1]+upperBarHeight+margin;
+
+          mMenuPopup.showAsDropDown(view);
+          mMenuPopup.update(x,y,mMenuLayoutWidth,mMenuLayoutHeight);
           }
         }
       });
@@ -212,7 +229,7 @@ public class RubikScreenPlay extends RubikScreenBase
     int margin = (int)(width*RubikActivity.LARGE_MARGIN);
     mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
     int upperBarHeight  = act.getHeightUpperBar();
-    mMaxRowCount = (int)((height-upperBarHeight)/mObjectSize);
+    mMaxRowCount = (int)(0.9f*(height-upperBarHeight)/mObjectSize);
     GridLayout objectGrid = new GridLayout(act);
     mObjectPopup = new PopupWindow(act);
     mObjectPopup.setFocusable(true);
