Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 9dfb553f

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.screens;
11

    
12
import java.lang.ref.WeakReference;
13

    
14
import android.app.Activity;
15
import android.content.Context;
16
import android.content.SharedPreferences;
17
import android.content.res.ColorStateList;
18
import android.content.res.Resources;
19
import android.os.Build;
20
import android.os.Bundle;
21
import android.util.TypedValue;
22
import android.view.Gravity;
23
import android.view.LayoutInflater;
24
import android.view.View;
25
import android.widget.Button;
26
import android.widget.GridLayout;
27
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29
import android.widget.PopupWindow;
30
import android.widget.RelativeLayout;
31
import android.widget.TextView;
32

    
33
import org.distorted.dialogs.RubikDialogStarsStatus;
34
import org.distorted.dialogs.RubikDialogUpdates;
35
import org.distorted.external.RubikNetwork;
36
import org.distorted.external.RubikScores;
37
import org.distorted.external.RubikUpdates;
38

    
39
import org.distorted.helpers.PopupCreator;
40
import org.distorted.main.R;
41
import org.distorted.main.RubikActivity;
42
import org.distorted.dialogs.RubikDialogAbout;
43
import org.distorted.dialogs.RubikDialogPattern;
44
import org.distorted.dialogs.RubikDialogScores;
45
import org.distorted.dialogs.RubikDialogTutorial;
46
import org.distorted.helpers.TransparentImageButton;
47
import org.distorted.objectlib.effects.BaseEffect;
48
import org.distorted.objectlib.main.ObjectControl;
49
import org.distorted.objects.RubikObject;
50
import org.distorted.objects.RubikObjectList;
51

    
52
import static android.view.View.GONE;
53
import static android.view.View.inflate;
54
import static org.distorted.main.RubikActivity.USE_IAP;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
59
  {
60
  public static final int NUM_COLUMNS  = 5;
61
  public static final int LEVELS_SHOWN = 8;
62
  private static final int[] mLocation = new int[2];
63

    
64
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
65
  private PopupWindow mObjectPopup, mMenuPopup;
66
  private WeakReference<RubikActivity> mWeakAct;
67
  private TextView mBubbleUpdates;
68
  private Button[] mLevel;
69
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mMenuButtonHeight, mMenuTextSize;
70
  private int mLevelValue;
71
  private int mColCount, mRowCount, mMaxRowCount;
72
  private int mUpperBarHeight;
73
  private boolean mShouldReactToEndOfScrambling;
74
  private float mScreenWidth, mScreenHeight;
75
  private int mOldNumScramble;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  void leaveScreen(RubikActivity act)
80
    {
81

    
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  void enterScreen(final RubikActivity act)
87
    {
88
    mWeakAct = new WeakReference<>(act);
89
    mScreenWidth  = act.getScreenWidthInPixels();
90
    mScreenHeight = act.getScreenHeightInPixels();
91
    mUpperBarHeight = act.getHeightUpperBar();
92

    
93
    mMenuButtonHeight = (int)(mScreenHeight*RubikActivity.MENU_BUTTON_HEIGHT);
94
    mMenuTextSize     = (int)(mScreenHeight*RubikActivity.MENU_TEXT_SIZE);
95

    
96
    mObjectPopup = null;
97
    mOldNumScramble = 1000; // used to remember which 'level' buttons are visible; initially all visible
98

    
99
    // TOP ////////////////////////////
100
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
101
    layoutTop.removeAllViews();
102

    
103
    setupSolveButton(act);
104
    layoutTop.addView(mSolveButton);
105
    setupMenuButton(act,mScreenWidth);
106
    layoutTop.addView(mMenuButton);
107
    setupScrambleButton(act);
108
    layoutTop.addView(mScrambleButton);
109

    
110
    // BOTTOM /////////////////////////
111
    setupObjectButton(act,mScreenWidth);
112
    createBottomPane(act,mObjButton,null);
113
    }
114

    
115
//////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  private void setupObjectButton(final RubikActivity act, final float width)
118
    {
119
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
120
    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);
121
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
122
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
123

    
124
    mObjButton.setOnClickListener( new View.OnClickListener()
125
      {
126
      @Override
127
      public void onClick(View view)
128
        {
129
        if( mObjectPopup==null )
130
          {
131
          float width = act.getScreenWidthInPixels();
132
          float height= act.getScreenHeightInPixels();
133
          setupObjectWindow(act,width,height);
134
          }
135

    
136
        if( act.getControl().isUINotBlocked())
137
          {
138
          int rowCount = Math.min(mMaxRowCount,mRowCount);
139
          View popupView = mObjectPopup.getContentView();
140
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
141
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
142
          }
143
        }
144
      });
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private void setupMenuButton(final RubikActivity act, final float width)
150
    {
151
    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);
152
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
153
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
154

    
155
    mMenuButton.setOnClickListener( new View.OnClickListener()
156
      {
157
      @Override
158
      public void onClick(View view)
159
        {
160
        if( mMenuPopup==null )
161
          {
162
          float width = act.getScreenWidthInPixels();
163
          setupMenuWindow(act,width);
164
          }
165

    
166
        if( act.getControl().isUINotBlocked())
167
          {
168
          View popupView = mMenuPopup.getContentView();
169
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
170
          setupLevelButtonVisibilityAndColor(act);
171
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-mMenuLayoutWidth/2 + width/6),0);
172
          }
173
        }
174
      });
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
180
    {
181
    int numObjects = RubikObjectList.getNumObjects();
182
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
183
    mColCount = NUM_COLUMNS;
184

    
185
    int cubeSize = (int)(width/9);
186
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
187
    int padding  = (int)(width*RubikActivity.POPUP_PADDING);
188
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
189
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
190

    
191
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
192
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
193
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
194
    setupBottomLayout(act,bottomLayout);
195

    
196
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
197

    
198
    for(int child=0; child<numObjects; child++)
199
      {
200
      final RubikObject obj = RubikObjectList.getObject(child);
201
      View v = objectGrid.getChildAt(child);
202
      ImageButton button = PopupCreator.getButton(obj,v);
203
      final int ordinal = child;
204

    
205
      button.setOnClickListener( new View.OnClickListener()
206
        {
207
        @Override
208
        public void onClick(View v)
209
          {
210
          if( obj!=null && act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()==ScreenList.PLAY )
211
            {
212
            if( obj.isFree() )
213
              {
214
              RubikObjectList.setCurrObject(ordinal);
215
              act.changeObject(ordinal,true);
216
              if( mMenuPopup!=null ) setupLevelButtonVisibilityAndColor(act);
217
              mMovesController.clearMoves(act);
218
              }
219
            else
220
              {
221
              act.switchToPurchase(ordinal);
222
              }
223
            }
224

    
225
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
226
          }
227
        });
228
      }
229

    
230
    mObjectPopup = new PopupWindow(act);
231
    mObjectPopup.setFocusable(true);
232
    mObjectPopup.setContentView(view);
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
238
    {
239
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
240
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
241
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
242

    
243
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
244
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
245
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
246

    
247
    buttonTut.setImageResource(iconT);
248
    buttonDow.setImageResource(iconD);
249
    buttonInf.setImageResource(iconI);
250

    
251
    TypedValue outValue = new TypedValue();
252
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
253
    buttonTut.setBackgroundResource(outValue.resourceId);
254
    buttonDow.setBackgroundResource(outValue.resourceId);
255
    buttonInf.setBackgroundResource(outValue.resourceId);
256

    
257
    buttonTut.setOnClickListener( new View.OnClickListener()
258
      {
259
      @Override
260
      public void onClick(View v)
261
        {
262
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
263
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
264
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
265
        }
266
      });
267

    
268
    buttonDow.setOnClickListener( new View.OnClickListener()
269
      {
270
      @Override
271
      public void onClick(View v)
272
        {
273
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
274
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
275
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
276
        }
277
      });
278

    
279
    buttonInf.setOnClickListener( new View.OnClickListener()
280
      {
281
      @Override
282
      public void onClick(View v)
283
        {
284
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
285
        int currObject = RubikObjectList.getCurrObject();
286
        act.switchConfig(currObject);
287
        }
288
      });
289

    
290
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
291
    mBubbleUpdates.setVisibility(View.INVISIBLE);
292

    
293
    RubikNetwork network = RubikNetwork.getInstance();
294
    network.signUpForUpdates(this);
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  void setupSolveButton(final RubikActivity act)
300
    {
301
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
302
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
303
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
304

    
305
    mSolveButton.setOnClickListener( new View.OnClickListener()
306
      {
307
      @Override
308
      public void onClick(View v)
309
        {
310
        act.getControl().solveObject();
311
        mMovesController.clearMoves(act);
312
        }
313
      });
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  private void setupScrambleButton(final RubikActivity act)
319
    {
320
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble,R.drawable.ui_medium_cube_scramble, R.drawable.ui_big_cube_scramble, R.drawable.ui_huge_cube_scramble);
321
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
322
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
323

    
324
    mScrambleButton.setOnClickListener( new View.OnClickListener()
325
      {
326
      @Override
327
      public void onClick(View v)
328
        {
329
        mShouldReactToEndOfScrambling = false;
330
        int duration = BaseEffect.Type.FAST_SCRAMBLE.getDuration();
331
        act.getControl().fastScrambleObject(duration,RubikObject.FAST_SCRAMBLES);
332
        }
333
      });
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
  private void setupMenuWindow(final RubikActivity act, final float width)
339
    {
340
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
341
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
342

    
343
    mMenuPopup = new PopupWindow(act);
344
    mMenuPopup.setContentView(layout);
345
    mMenuPopup.setFocusable(true);
346
    int padding = (int)(width*RubikActivity.MEDIUM_MARGIN);
347
    int numButtons = USE_IAP ? 7 : 6;
348
    mMenuLayoutWidth = (int)(width*0.65f);
349
    mMenuLayoutHeight= padding + numButtons*(mMenuButtonHeight+padding) + 4*mMenuButtonHeight+6*padding;
350

    
351
    layout.setPadding(padding,0,padding,0);
352

    
353
    if( USE_IAP )
354
      {
355
      Button stars = layout.findViewById(R.id.menuStars);
356
      stars.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
357
      stars.setOnClickListener( new View.OnClickListener()
358
          {
359
          @Override
360
          public void onClick(View v)
361
            {
362
            mMenuPopup.dismiss();
363
            RubikDialogStarsStatus d = new RubikDialogStarsStatus();
364
            d.show(act.getSupportFragmentManager(), null);
365
            }
366
          });
367
      }
368
    else
369
      {
370
      Button stars = layout.findViewById(R.id.menuStars);
371
      stars.setVisibility(GONE);
372
      }
373

    
374
    Button highScores = layout.findViewById(R.id.menuHighScores);
375
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
376
    highScores.setOnClickListener( new View.OnClickListener()
377
        {
378
        @Override
379
        public void onClick(View v)
380
          {
381
          mMenuPopup.dismiss();
382
          Bundle sBundle = new Bundle();
383
          int currObject = RubikObjectList.getCurrObject();
384
          sBundle.putInt("tab", currObject );
385
          sBundle.putBoolean("submitting", false);
386
          RubikDialogScores scores = new RubikDialogScores();
387
          scores.setArguments(sBundle);
388
          scores.show(act.getSupportFragmentManager(), null);
389
          }
390
        });
391

    
392
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
393
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
394
    prettyPatterns.setOnClickListener( new View.OnClickListener()
395
        {
396
        @Override
397
        public void onClick(View v)
398
          {
399
          mMenuPopup.dismiss();
400
          RubikDialogPattern pDiag = new RubikDialogPattern();
401
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
402
          }
403
        });
404

    
405
    Button solver = layout.findViewById(R.id.menuSolver);
406
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
407
    solver.setOnClickListener( new View.OnClickListener()
408
        {
409
        @Override
410
        public void onClick(View v)
411
          {
412
          mMenuPopup.dismiss();
413
          ScreenList.switchScreen(act, ScreenList.SVER);
414
          }
415
        });
416

    
417
    Button tutorials = layout.findViewById(R.id.menuTutorials);
418
    tutorials.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
419
    tutorials.setOnClickListener( new View.OnClickListener()
420
        {
421
        @Override
422
        public void onClick(View v)
423
          {
424
          mMenuPopup.dismiss();
425
          RubikDialogTutorial tDiag = new RubikDialogTutorial();
426
          tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
427
          }
428
        });
429

    
430
    Button bandaged = layout.findViewById(R.id.menuBandaged);
431
    bandaged.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
432
    bandaged.setOnClickListener( new View.OnClickListener()
433
        {
434
        @Override
435
        public void onClick(View v)
436
          {
437
          mMenuPopup.dismiss();
438
          act.switchToBandagedCreator();
439
          }
440
        });
441

    
442
    Button about = layout.findViewById(R.id.menuAbout);
443
    about.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
444
    about.setOnClickListener( new View.OnClickListener()
445
        {
446
        @Override
447
        public void onClick(View v)
448
          {
449
          mMenuPopup.dismiss();
450
          RubikDialogAbout aDiag = new RubikDialogAbout();
451
          aDiag.show(act.getSupportFragmentManager(), null);
452
          }
453
        });
454

    
455
    TextView levels = layout.findViewById(R.id.menuLevels);
456
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
457

    
458
    setupLevelButtons(act,layout,padding);
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  private void setupLevelButtons(RubikActivity act, View layout, int padding)
464
    {
465
    int sizeW = (mMenuLayoutWidth-4*padding)/3;
466
    int sizeH = (int)(sizeW*0.8f);
467
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(sizeW,sizeH);
468
    params.setMargins(padding/2,0,padding/2,0);
469

    
470
    mLevel = new Button[LEVELS_SHOWN+1];
471

    
472
    mLevel[0] = layout.findViewById(R.id.level1);
473
    mLevel[1] = layout.findViewById(R.id.level2);
474
    mLevel[2] = layout.findViewById(R.id.level3);
475
    mLevel[3] = layout.findViewById(R.id.level4);
476
    mLevel[4] = layout.findViewById(R.id.level5);
477
    mLevel[5] = layout.findViewById(R.id.level6);
478
    mLevel[6] = layout.findViewById(R.id.level7);
479
    mLevel[7] = layout.findViewById(R.id.level8);
480
    mLevel[8] = layout.findViewById(R.id.levelM);
481

    
482
    for(int i=0; i<=LEVELS_SHOWN; i++)
483
      {
484
      final int ii = i;
485
      mLevel[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
486
      mLevel[i].setLayoutParams(params);
487
      mLevel[i].setOnClickListener( new View.OnClickListener()
488
        {
489
        @Override
490
        public void onClick(View v)
491
          {
492
          ObjectControl control = act.getControl();
493

    
494
          if(control.isUINotBlocked())
495
            {
496
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
497

    
498
            int currObject = RubikObjectList.getCurrObject();
499
            RubikObject object = RubikObjectList.getObject(currObject);
500
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
501
            mLevelValue = ii+1;
502
            mShouldReactToEndOfScrambling = true;
503
            control.scrambleObject(scrambles);
504
            }
505
          }
506
        });
507
      }
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
  private void setupLevelButtonVisibilityAndColor(RubikActivity act)
513
    {
514
    int currObject = RubikObjectList.getCurrObject();
515
    RubikObject object = RubikObjectList.getObject(currObject);
516
    int numScramble = object==null ? 1 : object.getNumScramble();
517
    RubikScores scores = RubikScores.getInstance();
518
    Resources res = act.getResources();
519
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
520
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
521

    
522
    for(int level=0; level<=LEVELS_SHOWN; level++)
523
      {
524
      boolean isSolved = scores.isSolved(currObject,level);
525
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
526
      }
527

    
528
    if( numScramble<=LEVELS_SHOWN || mOldNumScramble<=LEVELS_SHOWN )
529
      {
530
      if( numScramble<mOldNumScramble )
531
        {
532
        int max = Math.min(LEVELS_SHOWN,mOldNumScramble-1);
533
        for(int level=numScramble; level<=max; level++) mLevel[level].setVisibility(View.INVISIBLE);
534
        mLevel[numScramble-1].setText(R.string.levelM);
535
        }
536
      if( numScramble>mOldNumScramble )
537
        {
538
        int max = Math.min(LEVELS_SHOWN,numScramble-1);
539
        mLevel[mOldNumScramble-1].setText( String.valueOf(mOldNumScramble) );
540

    
541
        for(int level=mOldNumScramble; level<=max; level++)
542
          {
543
          mLevel[level].setVisibility(View.VISIBLE);
544
          if( level<max ) mLevel[level].setText( String.valueOf(level+1) );
545
          else            mLevel[level].setText( R.string.levelM );
546
          }
547
        }
548
      }
549

    
550
    mOldNumScramble = numScramble;
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
555
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
556
// going to be called)
557
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
558

    
559
  public void savePreferences(SharedPreferences.Editor editor)
560
    {
561
    editor.putInt("play_LevelValue", mLevelValue );
562

    
563
    if( mObjectPopup!=null )
564
      {
565
      mObjectPopup.dismiss();
566
      mObjectPopup = null;
567
      }
568

    
569
    if( mMenuPopup!=null )
570
      {
571
      mMenuPopup.dismiss();
572
      mMenuPopup = null;
573
      }
574
    }
575

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577

    
578
  public void restorePreferences(SharedPreferences preferences)
579
    {
580
    mLevelValue = preferences.getInt("play_LevelValue", 0);
581
    }
582

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
585

    
586
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
587
    {
588
    View topLayout = act.findViewById(R.id.relativeLayout);
589
    boolean isFullScreen;
590

    
591
    if( topLayout!=null )
592
      {
593
      topLayout.getLocationOnScreen(mLocation);
594
      isFullScreen = (mLocation[1]==0);
595
      }
596
    else
597
      {
598
      isFullScreen = true;
599
      }
600

    
601
    try
602
      {
603
      // if on Android 11 or we are fullscreen
604
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
605
        {
606
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
607
        window.update(view, w, h);
608
        }
609
      else  // Android 10 or below in pop-up mode or split-screen mode
610
        {
611
        view.getLocationOnScreen(mLocation);
612
        int width  = view.getWidth();
613
        int height = view.getHeight();
614
        int x = mLocation[0]+(width-w)/2;
615
        int y = mLocation[1]+height+yoff;
616

    
617
        window.showAsDropDown(view);
618
        window.update(x,y,w,h);
619
        }
620
      }
621
    catch( IllegalArgumentException iae )
622
      {
623
      // ignore, this means window is 'not attached to window manager' -
624
      // which most probably is because we are already exiting the app.
625
      }
626
    }
627

    
628
///////////////////////////////////////////////////////////////////////////////////////////////////
629

    
630
  public int getLevel()
631
    {
632
    return mLevelValue;
633
    }
634

    
635
///////////////////////////////////////////////////////////////////////////////////////////////////
636

    
637
  public void recreatePopup()
638
    {
639
    mObjectPopup = null;
640
    }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643

    
644
  public boolean shouldReactToEndOfScrambling()
645
    {
646
    return mShouldReactToEndOfScrambling;
647
    }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650

    
651
  public void receiveUpdate(RubikUpdates updates)
652
    {
653
    Activity act = mWeakAct.get();
654

    
655
    if( act!=null )
656
      {
657
      act.runOnUiThread(new Runnable()
658
        {
659
        @Override
660
        public void run()
661
          {
662
          int num = updates.getCompletedNumber();
663

    
664
          if( num>0 )
665
            {
666
            String shownNum = String.valueOf(num);
667
            mBubbleUpdates.setText(shownNum);
668
            mBubbleUpdates.setVisibility(View.VISIBLE);
669
            int height = (int)(0.05f*mScreenWidth);
670
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
671
            }
672
         else
673
            {
674
            mBubbleUpdates.setVisibility(View.INVISIBLE);
675
            }
676
          }
677
        });
678
      }
679
    }
680

    
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682

    
683
  public void errorUpdate()
684
    {
685
    android.util.Log.e("D", "RubikScreenPlay: Error receiving downloaded objects update");
686
    }
687
  }
(5-5/10)