Project

General

Profile

Download (27.3 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 6b64f9f8

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.screens;
21

    
22
import java.lang.ref.WeakReference;
23

    
24
import android.app.Activity;
25
import android.content.Context;
26
import android.content.SharedPreferences;
27
import android.content.res.Resources;
28
import android.graphics.drawable.BitmapDrawable;
29
import android.os.Build;
30
import android.os.Bundle;
31
import android.util.TypedValue;
32
import android.view.Gravity;
33
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.widget.Button;
36
import android.widget.GridLayout;
37
import android.widget.ImageButton;
38
import android.widget.LinearLayout;
39
import android.widget.PopupWindow;
40
import android.widget.RelativeLayout;
41
import android.widget.TextView;
42

    
43
import org.distorted.dialogs.RubikDialogUpdates;
44
import org.distorted.external.RubikNetwork;
45
import org.distorted.external.RubikUpdates;
46
import org.distorted.objectlib.main.ObjectControl;
47

    
48
import org.distorted.main.R;
49
import org.distorted.main.RubikActivity;
50
import org.distorted.dialogs.RubikDialogAbout;
51
import org.distorted.dialogs.RubikDialogPattern;
52
import org.distorted.dialogs.RubikDialogScores;
53
import org.distorted.dialogs.RubikDialogTutorial;
54
import org.distorted.helpers.TransparentButton;
55
import org.distorted.helpers.TransparentImageButton;
56
import org.distorted.external.RubikScores;
57
import org.distorted.objects.RubikObject;
58
import org.distorted.objects.RubikObjectList;
59

    
60
import static android.view.View.inflate;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
65
  {
66
  public static final int NUM_COLUMNS  = 5;
67
  public static final int LEVELS_SHOWN = 8;
68

    
69
  private static final int[] BUTTON_LABELS = { R.string.scores,
70
                                               R.string.patterns,
71
                                               R.string.solver,
72
                                               R.string.tutorials,
73
                                               R.string.bandaged,
74
                                               R.string.about };
75
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
76

    
77
  private static final float BIG_BUTTON = 1.5f;
78
  private static final int[] mLocation = new int[2];
79

    
80
  private TransparentImageButton mObjButton, mMenuButton, mExitButton;
81
  private TransparentButton mPlayButton;
82
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
83
  private LinearLayout mPlayLayout;
84
  private TextView mBubbleUpdates;
85
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
86
  private int mLevelValue;
87
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
88
  private int mColCount, mRowCount, mMaxRowCount;
89
  private int mUpperBarHeight;
90
  private boolean mShouldReactToEndOfScrambling;
91
  private int mBottomHeight;
92
  private float mScreenWidth;
93
  private WeakReference<RubikActivity> mWeakAct;
94
  private String mObjectsPlayed;
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  void leaveScreen(RubikActivity act)
99
    {
100

    
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  void enterScreen(final RubikActivity act)
106
    {
107
    mWeakAct = new WeakReference<>(act);
108
    int numObjects = RubikObjectList.getNumObjects();
109
    mScreenWidth = act.getScreenWidthInPixels();
110
    mUpperBarHeight = act.getHeightUpperBar();
111

    
112
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
113
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
114
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
115

    
116
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
117
    mColCount = NUM_COLUMNS;
118

    
119
    // TOP ////////////////////////////
120
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
121
    layoutTop.removeAllViews();
122

    
123
    setupObjectButton(act,mScreenWidth);
124
    layoutTop.addView(mObjButton);
125

    
126
    setupMenuButton(act,mScreenWidth);
127
    layoutTop.addView(mMenuButton);
128

    
129
    setupPlayButton(act,mScreenWidth);
130
    layoutTop.addView(mPlayButton);
131

    
132
    setupExitButton(act);
133
    createBottomPane(act,mExitButton,null);
134
    }
135

    
136
//////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private void setupObjectButton(final RubikActivity act, final float width)
139
    {
140
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
141
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
142
    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);
143
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
144
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
145

    
146
    mObjButton.setOnClickListener( new View.OnClickListener()
147
      {
148
      @Override
149
      public void onClick(View view)
150
        {
151
        if( mObjectPopup==null )
152
          {
153
          float width = act.getScreenWidthInPixels();
154
          float height= act.getScreenHeightInPixels();
155
          setupObjectWindow(act,width,height);
156
          }
157

    
158
        if( act.getControl().isUINotBlocked())
159
          {
160
          int rowCount = Math.min(mMaxRowCount,mRowCount);
161
          View popupView = mObjectPopup.getContentView();
162
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
163
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
164
          }
165
        }
166
      });
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  private void setupPlayButton(final RubikActivity act, final float width)
172
    {
173
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
174

    
175
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
176

    
177
    mPlayButton.setOnClickListener( new View.OnClickListener()
178
      {
179
      @Override
180
      public void onClick(View view)
181
        {
182
        if( mPlayPopup==null )
183
          {
184
          float width = act.getScreenWidthInPixels();
185
          setupPlayWindow(act,width);
186
          }
187

    
188
        if( act.getControl().isUINotBlocked())
189
          {
190
          adjustSolvedIcons();
191
          float height= act.getScreenHeightInPixels();
192
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
193
          View popupView = mPlayPopup.getContentView();
194
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
195
          final int object  = RubikObjectList.getCurrObject();
196
          final int dbLevel = RubikObjectList.getDBLevel(object);
197
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN)+1;
198
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+4*margin+2*mMenuItemSize*(BIG_BUTTON-1.0f));
199
          final int realHeight = Math.min(popupHeight,maxHeight);
200
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
201
          }
202
        }
203
      });
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  private void setupMenuButton(final RubikActivity act, final float width)
209
    {
210
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
211
    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);
212
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
213
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
214

    
215
    mMenuButton.setOnClickListener( new View.OnClickListener()
216
      {
217
      @Override
218
      public void onClick(View view)
219
        {
220
        if( mMenuPopup==null )
221
          {
222
          float width = act.getScreenWidthInPixels();
223
          setupMenuWindow(act,width);
224
          }
225

    
226
        if( act.getControl().isUINotBlocked())
227
          {
228
          View popupView = mMenuPopup.getContentView();
229
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
230
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
231
          }
232
        }
233
      });
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
239
    {
240
    int cubeSize = (int)(width/9);
241
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
242
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
243
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
244

    
245
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
246
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
247

    
248
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
249
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
250

    
251
    objectGrid.setColumnCount(mColCount);
252
    objectGrid.setRowCount(mRowCount);
253

    
254
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
255
    setupBottomLayout(act,bottomLayout);
256

    
257
    mObjectPopup = new PopupWindow(act);
258
    mObjectPopup.setFocusable(true);
259
    mObjectPopup.setContentView(view);
260

    
261
    int[] nextInRow = new int[mRowCount];
262

    
263
    for(int row=0; row<mRowCount; row++)
264
      {
265
      rowSpecs[row] = GridLayout.spec(row);
266
      nextInRow[row]= 0;
267
      }
268
    for(int col=0; col<mColCount; col++)
269
      {
270
      colSpecs[col] = GridLayout.spec(col);
271
      }
272

    
273
    int numObjects = RubikObjectList.getNumObjects();
274

    
275
    for(int object=0; object<numObjects; object++)
276
      {
277
      final int ordinal = object;
278
      final RubikObject rObject = RubikObjectList.getObject(object);
279
      int row = object/NUM_COLUMNS;
280
      ImageButton button = new ImageButton(act);
281
      if( rObject!=null ) rObject.setIconTo(act,button);
282

    
283
      button.setOnClickListener( new View.OnClickListener()
284
        {
285
        @Override
286
        public void onClick(View v)
287
          {
288
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
289
            {
290
            RubikObjectList.setCurrObject(act,ordinal);
291
            act.changeObject(ordinal,true);
292
            if( mPlayLayout!=null ) adjustLevels(act);
293
            mMovesController.clearMoves(act);
294
            }
295

    
296
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
297
          }
298
        });
299

    
300
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
301
      params.bottomMargin = margin;
302
      params.topMargin    = margin;
303
      params.leftMargin   = margin;
304
      params.rightMargin  = margin;
305

    
306
      params.width = cubeSize;
307
      params.height= cubeSize;
308

    
309
      nextInRow[row]++;
310

    
311
      objectGrid.addView(button, params);
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
318
    {
319
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
320
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
321
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
322

    
323
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
324
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
325
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
326

    
327
    buttonTut.setImageResource(iconT);
328
    buttonDow.setImageResource(iconD);
329
    buttonInf.setImageResource(iconI);
330

    
331
    Resources res = act.getResources();
332
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
333
    mBottomHeight = bd.getIntrinsicHeight();
334

    
335
    TypedValue outValue = new TypedValue();
336
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
337
    buttonTut.setBackgroundResource(outValue.resourceId);
338
    buttonDow.setBackgroundResource(outValue.resourceId);
339
    buttonInf.setBackgroundResource(outValue.resourceId);
340

    
341
    buttonTut.setOnClickListener( new View.OnClickListener()
342
      {
343
      @Override
344
      public void onClick(View v)
345
        {
346
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
347
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
348
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
349
        }
350
      });
351

    
352
    buttonDow.setOnClickListener( new View.OnClickListener()
353
      {
354
      @Override
355
      public void onClick(View v)
356
        {
357
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
358
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
359
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
360
        }
361
      });
362

    
363
    buttonInf.setOnClickListener( new View.OnClickListener()
364
      {
365
      @Override
366
      public void onClick(View v)
367
        {
368
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
369
        int currObject = RubikObjectList.getCurrObject();
370
        act.switchConfig(currObject);
371
        }
372
      });
373

    
374
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
375
    mBubbleUpdates.setVisibility(View.INVISIBLE);
376

    
377
    RubikNetwork network = RubikNetwork.getInstance();
378
    network.signUpForUpdates(this);
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  private void setupMenuWindow(final RubikActivity act, final float width)
384
    {
385
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
386
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
387
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
388

    
389
    mMenuPopup = new PopupWindow(act);
390
    mMenuPopup.setContentView(layout);
391
    mMenuPopup.setFocusable(true);
392
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
393
    int padding = (int)(width*RubikActivity.PADDING);
394

    
395
    mMenuLayoutWidth = (int)(width/2);
396
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
397

    
398
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
399

    
400
    for(int i=0; i<NUM_BUTTONS; i++)
401
      {
402
      final int but = i;
403
      Button button = new Button(act);
404
      button.setLayoutParams(p);
405
      button.setText(BUTTON_LABELS[i]);
406
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
407

    
408
      button.setOnClickListener( new View.OnClickListener()
409
        {
410
        @Override
411
        public void onClick(View v)
412
          {
413
          mMenuPopup.dismiss();
414
          MenuAction(act,but);
415
          }
416
        });
417

    
418
      menuLayout.addView(button);
419
      }
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  private void setupPlayWindow(final RubikActivity act, final float width)
425
    {
426
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
427
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
428
    mPlayLayout = layout.findViewById(R.id.playGrid);
429

    
430
    mPlayLayoutWidth = (int)(width*0.4f);
431

    
432
    mPlayPopup = new PopupWindow(act);
433
    mPlayPopup.setContentView(layout);
434
    mPlayPopup.setFocusable(true);
435

    
436
    adjustLevels(act);
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  private void MenuAction(RubikActivity act, int button)
442
    {
443
    switch(button)
444
      {
445
      case 0: Bundle sBundle = new Bundle();
446
              int currObject = RubikObjectList.getCurrObject();
447
              sBundle.putInt("tab", currObject );
448
              sBundle.putBoolean("submitting", false);
449
              RubikDialogScores scores = new RubikDialogScores();
450
              scores.setArguments(sBundle);
451
              scores.show(act.getSupportFragmentManager(), null);
452
              break;
453
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
454
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
455
              break;
456
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
457
              break;
458
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
459
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
460
              break;
461
      case 4: act.switchToBandagedCreator();
462
              break;
463
      case 5: RubikDialogAbout aDiag = new RubikDialogAbout();
464
              aDiag.show(act.getSupportFragmentManager(), null);
465
              break;
466
      }
467
    }
468

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

    
471
  void setupExitButton(final RubikActivity act)
472
    {
473
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_exit,R.drawable.ui_medium_exit, R.drawable.ui_big_exit, R.drawable.ui_huge_exit);
474
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
475
    mExitButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
476

    
477
    mExitButton.setOnClickListener( new View.OnClickListener()
478
      {
479
      @Override
480
      public void onClick(View v)
481
        {
482
        act.finish();
483
        }
484
      });
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
489
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
490
// going to be called)
491
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
492

    
493
  public void savePreferences(SharedPreferences.Editor editor)
494
    {
495
    editor.putInt("play_LevelValue", mLevelValue );
496
    editor.putString("play_objectsPlayed", mObjectsPlayed );
497

    
498
    if( mObjectPopup!=null )
499
      {
500
      mObjectPopup.dismiss();
501
      mObjectPopup = null;
502
      }
503

    
504
    if( mMenuPopup!=null )
505
      {
506
      mMenuPopup.dismiss();
507
      mMenuPopup = null;
508
      }
509

    
510
    if( mPlayPopup!=null )
511
      {
512
      mPlayPopup.dismiss();
513
      mPlayPopup = null;
514
      }
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
  private void addToPlayedObjects()
520
    {
521
    String name = RubikObjectList.getCurrentName();
522
    if( !mObjectsPlayed.contains(name) ) mObjectsPlayed += (" "+name);
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

    
527
  private boolean hasBeenPlayed()
528
    {
529
    String name = RubikObjectList.getCurrentName();
530
    return mObjectsPlayed.contains(name);
531
    }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534

    
535
  public void restorePreferences(SharedPreferences preferences)
536
    {
537
    mLevelValue    = preferences.getInt("play_LevelValue", 0);
538
    mObjectsPlayed = preferences.getString("play_objectsPlayed", "");
539
    }
540

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

    
543
  public void setCurrObject(RubikActivity act)
544
    {
545
    if( mPlayLayout!=null ) adjustLevels(act);
546
    }
547

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
550

    
551
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
552
    {
553
    View topLayout = act.findViewById(R.id.relativeLayout);
554
    boolean isFullScreen;
555

    
556
    if( topLayout!=null )
557
      {
558
      topLayout.getLocationOnScreen(mLocation);
559
      isFullScreen = (mLocation[1]==0);
560
      }
561
    else
562
      {
563
      isFullScreen = true;
564
      }
565

    
566
    try
567
      {
568
      // if on Android 11 or we are fullscreen
569
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
570
        {
571
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
572
        window.update(view, w, h);
573
        }
574
      else  // Android 10 or below in pop-up mode or split-screen mode
575
        {
576
        view.getLocationOnScreen(mLocation);
577
        int width  = view.getWidth();
578
        int height = view.getHeight();
579
        int x = mLocation[0]+(width-w)/2;
580
        int y = mLocation[1]+height+yoff;
581

    
582
        window.showAsDropDown(view);
583
        window.update(x,y,w,h);
584
        }
585
      }
586
    catch( IllegalArgumentException iae )
587
      {
588
      // ignore, this means window is 'not attached to window manager' -
589
      // which most probably is because we are already exiting the app.
590
      }
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
  private void adjustSolvedIcons()
596
    {
597
    if( mPlayLayout!=null )
598
      {
599
      int currObject = RubikObjectList.getCurrObject();
600
      int dbLevel = RubikObjectList.getDBLevel(currObject);
601
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN)+1;
602
      RubikScores scores = RubikScores.getInstance();
603

    
604
      for(int i=0; i<numLevel; i++)
605
        {
606
        int level = i<numLevel-1 ? i : dbLevel;
607
        Button button = (Button)mPlayLayout.getChildAt(i);
608
        boolean isSolved = i>0 ? scores.isSolved(currObject, level-1) : hasBeenPlayed();
609
        int icon = isSolved ? R.drawable.ui_solved : R.drawable.ui_notsolved;
610
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
611
        }
612
      }
613
    }
614

    
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616

    
617
  private void adjustLevels(final RubikActivity act)
618
    {
619
    int currObject = RubikObjectList.getCurrObject();
620
    int dbLevel = RubikObjectList.getDBLevel(currObject);
621
    RubikObject object = RubikObjectList.getObject(currObject);
622
    int numScrambles = object==null ? 0 : object.getNumScramble();
623
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN)+1;
624
    String[] levels = new String[numLevel];
625

    
626
    levels[0] = act.getString(R.string.free_play);
627
    for(int i=1; i<numLevel-1; i++) levels[i] = act.getString(R.string.lv_placeholder,i);
628
    levels[numLevel-1] = act.getString(R.string.level_full);
629

    
630
    if( mLevelValue>dbLevel || mLevelValue<1 ||
631
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) ) mLevelValue=1;
632

    
633
    float width  = act.getScreenWidthInPixels();
634
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
635
    int padding  = (int)(width*RubikActivity.PADDING);
636
    int butWidth = mPlayLayoutWidth - 2*padding;
637
    int butHeight= (int)mMenuItemSize;
638
    int bigButH  = (int)(mMenuItemSize*BIG_BUTTON) ;
639

    
640
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
641
    pM.setMargins(margin, 0, margin, margin);
642
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, bigButH   );
643
    pB.setMargins(margin, margin, margin, 2*margin);
644

    
645
    mPlayLayout.removeAllViews();
646

    
647
    for(int i=0; i<numLevel; i++)
648
      {
649
      final int level     = i<numLevel-1 ? i : dbLevel;
650
      final int scrambles = i<numLevel-1 ? i : numScrambles;
651
      Button button = new Button(act);
652
      button.setLayoutParams(i==0 ? pB : (i==numLevel-1 ? pB : pM));
653
      button.setText(levels[i]);
654
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
655

    
656
      button.setOnClickListener( new View.OnClickListener()
657
        {
658
        @Override
659
        public void onClick(View v)
660
          {
661
          ObjectControl control = act.getControl();
662

    
663
          if(control.isUINotBlocked())
664
            {
665
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
666

    
667
            if( level>0 )
668
              {
669
              mLevelValue = level;
670
              mShouldReactToEndOfScrambling = true;
671
              control.scrambleObject(scrambles);
672
              }
673
            else
674
              {
675
              addToPlayedObjects();
676
              mShouldReactToEndOfScrambling = false;
677
              ScreenList.switchScreen(act, ScreenList.FREE);
678
              }
679
            }
680
          }
681
        });
682

    
683
      mPlayLayout.addView(button);
684
      }
685
    }
686

    
687
///////////////////////////////////////////////////////////////////////////////////////////////////
688

    
689
  public int getLevel()
690
    {
691
    return mLevelValue;
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695

    
696
  public void recreatePopup()
697
    {
698
    mObjectPopup = null;
699

    
700
    int numObjects = RubikObjectList.getNumObjects();
701
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
702
    mColCount = NUM_COLUMNS;
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706

    
707
  public boolean shouldReactToEndOfScrambling()
708
    {
709
    return mShouldReactToEndOfScrambling;
710
    }
711

    
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713

    
714
  public void receiveUpdate(RubikUpdates updates)
715
    {
716
    Activity act = mWeakAct.get();
717

    
718
    if( act!=null )
719
      {
720
      act.runOnUiThread(new Runnable()
721
        {
722
        @Override
723
        public void run()
724
          {
725
          int num = updates.getCompletedNumber();
726

    
727
          if( num>0 )
728
            {
729
            String shownNum = String.valueOf(num);
730
            mBubbleUpdates.setText(shownNum);
731
            mBubbleUpdates.setVisibility(View.VISIBLE);
732
            int height = (int)(0.05f*mScreenWidth);
733
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
734
            }
735
         else
736
            {
737
            mBubbleUpdates.setVisibility(View.INVISIBLE);
738
            }
739
          }
740
        });
741
      }
742
    }
743

    
744
///////////////////////////////////////////////////////////////////////////////////////////////////
745

    
746
  public void errorUpdate()
747
    {
748
    android.util.Log.e("D", "Screen: Error receiving update");
749
    }
750
  }
(6-6/11)