Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ b1629e16

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.RubikDialogUpdates;
34
import org.distorted.external.RubikNetwork;
35
import org.distorted.external.RubikScores;
36
import org.distorted.external.RubikUpdates;
37

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

    
51
import static android.view.View.inflate;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  void leaveScreen(RubikActivity act)
78
    {
79

    
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  void enterScreen(final RubikActivity act)
85
    {
86
    mWeakAct = new WeakReference<>(act);
87
    mScreenWidth = act.getScreenWidthInPixels();
88
    mUpperBarHeight = act.getHeightUpperBar();
89

    
90
    mMenuButtonHeight = (int)(mScreenWidth*RubikActivity.MENU_BUTTON_HEIGHT);
91
    mMenuTextSize     = (int)(mScreenWidth*RubikActivity.MENU_MAIN_TEXT_SIZE);
92

    
93
    mObjectPopup = null;
94
    mOldNumScramble = 1000; // used to remember which 'level' buttons are visible; initially all visible
95

    
96
    // TOP ////////////////////////////
97
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
98
    layoutTop.removeAllViews();
99

    
100
    setupSolveButton(act);
101
    layoutTop.addView(mSolveButton);
102
    setupMenuButton(act,mScreenWidth);
103
    layoutTop.addView(mMenuButton);
104
    setupScrambleButton(act);
105
    layoutTop.addView(mScrambleButton);
106

    
107
    // BOTTOM /////////////////////////
108
    setupObjectButton(act,mScreenWidth);
109
    createBottomPane(act,mObjButton,null);
110
    }
111

    
112
//////////////////////////////////////////////////////////////////////////////////////////////////
113

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

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

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

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

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

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

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

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

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

    
188
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
189
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
190
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
191
    setupBottomLayout(act,bottomLayout);
192

    
193
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
194

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

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

    
222
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
223
          }
224
        });
225
      }
226

    
227
    mObjectPopup = new PopupWindow(act);
228
    mObjectPopup.setFocusable(true);
229
    mObjectPopup.setContentView(view);
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
240
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
241
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
242
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
243

    
244
    buttonTut.setImageResource(iconT);
245
    buttonDow.setImageResource(iconD);
246
    buttonInf.setImageResource(iconI);
247

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

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

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

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

    
287
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
288
    mBubbleUpdates.setVisibility(View.INVISIBLE);
289

    
290
    RubikNetwork network = RubikNetwork.getInstance();
291
    network.signUpForUpdates(this);
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  void setupSolveButton(final RubikActivity act)
297
    {
298
    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);
299
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
300
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
301

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

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  private void setupScrambleButton(final RubikActivity act)
316
    {
317
    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);
318
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
319
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
320

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

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

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

    
340
    mMenuPopup = new PopupWindow(act);
341
    mMenuPopup.setContentView(layout);
342
    mMenuPopup.setFocusable(true);
343
    int padding = (int)(width*RubikActivity.MEDIUM_MARGIN);
344

    
345
    mMenuLayoutWidth = (int)(width*0.65f);
346
    mMenuLayoutHeight= padding + NUM_BUTTONS*(mMenuButtonHeight+padding) + 4*mMenuButtonHeight+6*padding;
347

    
348
    layout.setPadding(padding,0,padding,0);
349

    
350
    Button highScores = layout.findViewById(R.id.menuHighScores);
351
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
352
    highScores.setOnClickListener( new View.OnClickListener()
353
        {
354
        @Override
355
        public void onClick(View v)
356
          {
357
          mMenuPopup.dismiss();
358
          Bundle sBundle = new Bundle();
359
          int currObject = RubikObjectList.getCurrObject();
360
          sBundle.putInt("tab", currObject );
361
          sBundle.putBoolean("submitting", false);
362
          RubikDialogScores scores = new RubikDialogScores();
363
          scores.setArguments(sBundle);
364
          scores.show(act.getSupportFragmentManager(), null);
365
          }
366
        });
367

    
368
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
369
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
370
    prettyPatterns.setOnClickListener( new View.OnClickListener()
371
        {
372
        @Override
373
        public void onClick(View v)
374
          {
375
          mMenuPopup.dismiss();
376
          RubikDialogPattern pDiag = new RubikDialogPattern();
377
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
378
          }
379
        });
380

    
381
    Button solver = layout.findViewById(R.id.menuSolver);
382
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
383
    solver.setOnClickListener( new View.OnClickListener()
384
        {
385
        @Override
386
        public void onClick(View v)
387
          {
388
          mMenuPopup.dismiss();
389
          ScreenList.switchScreen(act, ScreenList.SVER);
390
          }
391
        });
392

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

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

    
418
    Button about = layout.findViewById(R.id.menuAbout);
419
    about.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
420
    about.setOnClickListener( new View.OnClickListener()
421
        {
422
        @Override
423
        public void onClick(View v)
424
          {
425
          mMenuPopup.dismiss();
426
          RubikDialogAbout aDiag = new RubikDialogAbout();
427
          aDiag.show(act.getSupportFragmentManager(), null);
428
          }
429
        });
430

    
431
    TextView levels = layout.findViewById(R.id.menuLevels);
432
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
433

    
434
    setupLevelButtons(act,layout,padding);
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  private void setupLevelButtons(RubikActivity act, View layout, int padding)
440
    {
441
    int sizeW = (mMenuLayoutWidth-4*padding)/3;
442
    int sizeH = (int)(sizeW*0.8f);
443
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(sizeW,sizeH);
444
    params.setMargins(padding/2,0,padding/2,0);
445

    
446
    mLevel = new Button[LEVELS_SHOWN+1];
447

    
448
    mLevel[0] = layout.findViewById(R.id.level1);
449
    mLevel[1] = layout.findViewById(R.id.level2);
450
    mLevel[2] = layout.findViewById(R.id.level3);
451
    mLevel[3] = layout.findViewById(R.id.level4);
452
    mLevel[4] = layout.findViewById(R.id.level5);
453
    mLevel[5] = layout.findViewById(R.id.level6);
454
    mLevel[6] = layout.findViewById(R.id.level7);
455
    mLevel[7] = layout.findViewById(R.id.level8);
456
    mLevel[8] = layout.findViewById(R.id.levelM);
457

    
458
    for(int i=0; i<=LEVELS_SHOWN; i++)
459
      {
460
      final int ii = i;
461
      mLevel[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
462
      mLevel[i].setLayoutParams(params);
463
      mLevel[i].setOnClickListener( new View.OnClickListener()
464
        {
465
        @Override
466
        public void onClick(View v)
467
          {
468
          ObjectControl control = act.getControl();
469

    
470
          if(control.isUINotBlocked())
471
            {
472
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
473

    
474
            int currObject = RubikObjectList.getCurrObject();
475
            RubikObject object = RubikObjectList.getObject(currObject);
476
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
477
            mLevelValue = ii+1;
478
            mShouldReactToEndOfScrambling = true;
479
            control.scrambleObject(scrambles);
480
            }
481
          }
482
        });
483
      }
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  private void setupLevelButtonVisibilityAndColor(RubikActivity act)
489
    {
490
    int currObject = RubikObjectList.getCurrObject();
491
    RubikObject object = RubikObjectList.getObject(currObject);
492
    int numScramble = object==null ? 1 : object.getNumScramble();
493
    RubikScores scores = RubikScores.getInstance();
494
    Resources res = act.getResources();
495
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
496
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
497

    
498
    for(int level=0; level<=LEVELS_SHOWN; level++)
499
      {
500
      boolean isSolved = scores.isSolved(currObject,level);
501
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
502
      }
503

    
504
    if( numScramble<=LEVELS_SHOWN || mOldNumScramble<=LEVELS_SHOWN )
505
      {
506
      if( numScramble<mOldNumScramble )
507
        {
508
        int max = Math.min(LEVELS_SHOWN,mOldNumScramble-1);
509
        for(int level=numScramble; level<=max; level++) mLevel[level].setVisibility(View.INVISIBLE);
510
        mLevel[numScramble-1].setText(R.string.levelM);
511
        }
512
      if( numScramble>mOldNumScramble )
513
        {
514
        int max = Math.min(LEVELS_SHOWN,numScramble-1);
515
        mLevel[mOldNumScramble-1].setText( String.valueOf(mOldNumScramble) );
516

    
517
        for(int level=mOldNumScramble; level<=max; level++)
518
          {
519
          mLevel[level].setVisibility(View.VISIBLE);
520
          if( level<max ) mLevel[level].setText( String.valueOf(level+1) );
521
          else            mLevel[level].setText( R.string.levelM );
522
          }
523
        }
524
      }
525

    
526
    mOldNumScramble = numScramble;
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
531
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
532
// going to be called)
533
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
534

    
535
  public void savePreferences(SharedPreferences.Editor editor)
536
    {
537
    editor.putInt("play_LevelValue", mLevelValue );
538

    
539
    if( mObjectPopup!=null )
540
      {
541
      mObjectPopup.dismiss();
542
      mObjectPopup = null;
543
      }
544

    
545
    if( mMenuPopup!=null )
546
      {
547
      mMenuPopup.dismiss();
548
      mMenuPopup = null;
549
      }
550
    }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553

    
554
  public void restorePreferences(SharedPreferences preferences)
555
    {
556
    mLevelValue = preferences.getInt("play_LevelValue", 0);
557
    }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
561

    
562
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
563
    {
564
    View topLayout = act.findViewById(R.id.relativeLayout);
565
    boolean isFullScreen;
566

    
567
    if( topLayout!=null )
568
      {
569
      topLayout.getLocationOnScreen(mLocation);
570
      isFullScreen = (mLocation[1]==0);
571
      }
572
    else
573
      {
574
      isFullScreen = true;
575
      }
576

    
577
    try
578
      {
579
      // if on Android 11 or we are fullscreen
580
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
581
        {
582
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
583
        window.update(view, w, h);
584
        }
585
      else  // Android 10 or below in pop-up mode or split-screen mode
586
        {
587
        view.getLocationOnScreen(mLocation);
588
        int width  = view.getWidth();
589
        int height = view.getHeight();
590
        int x = mLocation[0]+(width-w)/2;
591
        int y = mLocation[1]+height+yoff;
592

    
593
        window.showAsDropDown(view);
594
        window.update(x,y,w,h);
595
        }
596
      }
597
    catch( IllegalArgumentException iae )
598
      {
599
      // ignore, this means window is 'not attached to window manager' -
600
      // which most probably is because we are already exiting the app.
601
      }
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605

    
606
  public int getLevel()
607
    {
608
    return mLevelValue;
609
    }
610

    
611
///////////////////////////////////////////////////////////////////////////////////////////////////
612

    
613
  public void recreatePopup()
614
    {
615
    mObjectPopup = null;
616
    }
617

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619

    
620
  public boolean shouldReactToEndOfScrambling()
621
    {
622
    return mShouldReactToEndOfScrambling;
623
    }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626

    
627
  public void receiveUpdate(RubikUpdates updates)
628
    {
629
    Activity act = mWeakAct.get();
630

    
631
    if( act!=null )
632
      {
633
      act.runOnUiThread(new Runnable()
634
        {
635
        @Override
636
        public void run()
637
          {
638
          int num = updates.getCompletedNumber();
639

    
640
          if( num>0 )
641
            {
642
            String shownNum = String.valueOf(num);
643
            mBubbleUpdates.setText(shownNum);
644
            mBubbleUpdates.setVisibility(View.VISIBLE);
645
            int height = (int)(0.05f*mScreenWidth);
646
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
647
            }
648
         else
649
            {
650
            mBubbleUpdates.setVisibility(View.INVISIBLE);
651
            }
652
          }
653
        });
654
      }
655
    }
656

    
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658

    
659
  public void errorUpdate()
660
    {
661
    android.util.Log.e("D", "RubikScreenPlay: Error receiving downloaded objects update");
662
    }
663
  }
(5-5/10)