Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 83018ac4

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.main.R;
39
import org.distorted.main.RubikActivity;
40
import org.distorted.dialogs.RubikDialogAbout;
41
import org.distorted.dialogs.RubikDialogPattern;
42
import org.distorted.dialogs.RubikDialogScores;
43
import org.distorted.dialogs.RubikDialogTutorial;
44
import org.distorted.helpers.TransparentImageButton;
45
import org.distorted.objectlib.main.ObjectControl;
46
import org.distorted.objects.MainEntry;
47
import org.distorted.objects.MainEntryList;
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 = 5;
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

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  void leaveScreen(RubikActivity act)
77
    {
78

    
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

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

    
92
    mObjectPopup = null;
93

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

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

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

    
110
//////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
175
    {
176
    MainEntryList list = MainEntryList.getInstance();
177
    int numEntries = list.getNumOfEntries();
178
    mRowCount = (numEntries + NUM_COLUMNS-1) / NUM_COLUMNS;
179
    mColCount = NUM_COLUMNS;
180

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

    
186
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
187
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
188

    
189
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
190
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
191

    
192
    objectGrid.setColumnCount(mColCount);
193
    objectGrid.setRowCount(mRowCount);
194

    
195
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
196
    setupBottomLayout(act,bottomLayout);
197

    
198
    mObjectPopup = new PopupWindow(act);
199
    mObjectPopup.setFocusable(true);
200
    mObjectPopup.setContentView(view);
201

    
202
    int[] nextInRow = new int[mRowCount];
203

    
204
    for(int row=0; row<mRowCount; row++)
205
      {
206
      rowSpecs[row] = GridLayout.spec(row);
207
      nextInRow[row]= 0;
208
      }
209
    for(int col=0; col<mColCount; col++)
210
      {
211
      colSpecs[col] = GridLayout.spec(col);
212
      }
213

    
214
    for(int entry=0; entry<numEntries; entry++)
215
      {
216
      final MainEntry mainEntry = list.getEntry(entry);
217
      int row = entry/NUM_COLUMNS;
218
      ImageButton button = new ImageButton(act);
219
      if( mainEntry!=null ) mainEntry.setIconTo(act,button);
220

    
221
      button.setOnClickListener( new View.OnClickListener()
222
        {
223
        @Override
224
        public void onClick(View v)
225
          {
226
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
227
            {
228
            int type = mainEntry!=null ? mainEntry.getType() : -1;
229

    
230
            if( type==MainEntry.TYPE_OBJECT )
231
              {
232
              int ordinal = mainEntry.getOrdinal();
233
              RubikObjectList.setCurrObject(ordinal);
234
              act.changeObject(ordinal,true);
235
              if( mMenuPopup!=null ) setupLevelColors(act);
236
              mMovesController.clearMoves(act);
237
              }
238
            else if( type==MainEntry.TYPE_CREATOR )
239
              {
240
              act.switchToBandagedCreator();
241
              }
242
            }
243

    
244
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
245
          }
246
        });
247

    
248
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
249
      params.bottomMargin = margin;
250
      params.topMargin    = margin;
251
      params.leftMargin   = margin;
252
      params.rightMargin  = margin;
253

    
254
      params.width = cubeSize;
255
      params.height= cubeSize;
256

    
257
      nextInRow[row]++;
258

    
259
      objectGrid.addView(button, params);
260
      }
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
266
    {
267
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
268
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
269
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
270

    
271
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
272
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
273
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
274

    
275
    buttonTut.setImageResource(iconT);
276
    buttonDow.setImageResource(iconD);
277
    buttonInf.setImageResource(iconI);
278

    
279
    TypedValue outValue = new TypedValue();
280
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
281
    buttonTut.setBackgroundResource(outValue.resourceId);
282
    buttonDow.setBackgroundResource(outValue.resourceId);
283
    buttonInf.setBackgroundResource(outValue.resourceId);
284

    
285
    buttonTut.setOnClickListener( new View.OnClickListener()
286
      {
287
      @Override
288
      public void onClick(View v)
289
        {
290
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
291
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
292
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
293
        }
294
      });
295

    
296
    buttonDow.setOnClickListener( new View.OnClickListener()
297
      {
298
      @Override
299
      public void onClick(View v)
300
        {
301
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
302
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
303
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
304
        }
305
      });
306

    
307
    buttonInf.setOnClickListener( new View.OnClickListener()
308
      {
309
      @Override
310
      public void onClick(View v)
311
        {
312
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
313
        int currObject = RubikObjectList.getCurrObject();
314
        act.switchConfig(currObject);
315
        }
316
      });
317

    
318
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
319
    mBubbleUpdates.setVisibility(View.INVISIBLE);
320

    
321
    RubikNetwork network = RubikNetwork.getInstance();
322
    network.signUpForUpdates(this);
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  void setupSolveButton(final RubikActivity act)
328
    {
329
    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);
330
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
331
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
332

    
333
    mSolveButton.setOnClickListener( new View.OnClickListener()
334
      {
335
      @Override
336
      public void onClick(View v)
337
        {
338
        act.getControl().solveObject();
339
        mMovesController.clearMoves(act);
340
        }
341
      });
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  private void setupScrambleButton(final RubikActivity act)
347
    {
348
    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);
349
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
350
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
351

    
352
    mScrambleButton.setOnClickListener( new View.OnClickListener()
353
      {
354
      @Override
355
      public void onClick(View v)
356
        {
357
        int currObject = RubikObjectList.getCurrObject();
358
        RubikObject object = RubikObjectList.getObject(currObject);
359
        int numScrambles = object==null ? 0 : 2*object.getNumScramble();
360
        mShouldReactToEndOfScrambling = false;
361
        act.getControl().fastScrambleObject(numScrambles);
362
        }
363
      });
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  private void setupMenuWindow(final RubikActivity act, final float width)
369
    {
370
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
371
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
372

    
373
    mMenuPopup = new PopupWindow(act);
374
    mMenuPopup.setContentView(layout);
375
    mMenuPopup.setFocusable(true);
376
    int padding = (int)(width*RubikActivity.MEDIUM_MARGIN);
377

    
378
    mMenuLayoutWidth = (int)(width*0.6f);
379
    mMenuLayoutHeight= padding + NUM_BUTTONS*(mMenuButtonHeight+padding) + 4*mMenuButtonHeight+6*padding;
380

    
381
    layout.setPadding(padding,0,padding,0);
382

    
383
    Button highScores = layout.findViewById(R.id.menuHighScores);
384
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
385
    highScores.setOnClickListener( new View.OnClickListener()
386
        {
387
        @Override
388
        public void onClick(View v)
389
          {
390
          mMenuPopup.dismiss();
391
          Bundle sBundle = new Bundle();
392
          int currObject = RubikObjectList.getCurrObject();
393
          sBundle.putInt("tab", currObject );
394
          sBundle.putBoolean("submitting", false);
395
          RubikDialogScores scores = new RubikDialogScores();
396
          scores.setArguments(sBundle);
397
          scores.show(act.getSupportFragmentManager(), null);
398
          }
399
        });
400

    
401
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
402
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
403
    prettyPatterns.setOnClickListener( new View.OnClickListener()
404
        {
405
        @Override
406
        public void onClick(View v)
407
          {
408
          mMenuPopup.dismiss();
409
          RubikDialogPattern pDiag = new RubikDialogPattern();
410
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
411
          }
412
        });
413

    
414
    Button solver = layout.findViewById(R.id.menuSolver);
415
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
416
    solver.setOnClickListener( new View.OnClickListener()
417
        {
418
        @Override
419
        public void onClick(View v)
420
          {
421
          mMenuPopup.dismiss();
422
          ScreenList.switchScreen(act, ScreenList.SVER);
423
          }
424
        });
425

    
426
    Button tutorials = layout.findViewById(R.id.menuTutorials);
427
    tutorials.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
428
    tutorials.setOnClickListener( new View.OnClickListener()
429
        {
430
        @Override
431
        public void onClick(View v)
432
          {
433
          mMenuPopup.dismiss();
434
          RubikDialogTutorial tDiag = new RubikDialogTutorial();
435
          tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
436
          }
437
        });
438

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

    
452
    TextView levels = layout.findViewById(R.id.menuLevels);
453
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
454

    
455
    setupLevelButtons(act,layout,padding);
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

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

    
467
    mLevel = new Button[LEVELS_SHOWN+1];
468

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

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

    
491
          if(control.isUINotBlocked())
492
            {
493
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
494

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

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
  private void setupLevelColors(RubikActivity act)
510
    {
511
    int currObject = RubikObjectList.getCurrObject();
512
    RubikScores scores = RubikScores.getInstance();
513
    Resources res = act.getResources();
514
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
515
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
516

    
517
    for(int level=0; level<=LEVELS_SHOWN; level++)
518
      {
519
      boolean isSolved = scores.isSolved(currObject,level);
520
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
521
      }
522
    }
523

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

    
530
  public void savePreferences(SharedPreferences.Editor editor)
531
    {
532
    editor.putInt("play_LevelValue", mLevelValue );
533

    
534
    if( mObjectPopup!=null )
535
      {
536
      mObjectPopup.dismiss();
537
      mObjectPopup = null;
538
      }
539

    
540
    if( mMenuPopup!=null )
541
      {
542
      mMenuPopup.dismiss();
543
      mMenuPopup = null;
544
      }
545
    }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
  public void restorePreferences(SharedPreferences preferences)
550
    {
551
    mLevelValue = preferences.getInt("play_LevelValue", 0);
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
556

    
557
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
558
    {
559
    View topLayout = act.findViewById(R.id.relativeLayout);
560
    boolean isFullScreen;
561

    
562
    if( topLayout!=null )
563
      {
564
      topLayout.getLocationOnScreen(mLocation);
565
      isFullScreen = (mLocation[1]==0);
566
      }
567
    else
568
      {
569
      isFullScreen = true;
570
      }
571

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

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

    
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600

    
601
  public int getLevel()
602
    {
603
    return mLevelValue;
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607

    
608
  public void recreatePopup()
609
    {
610
    mObjectPopup = null;
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614

    
615
  public boolean shouldReactToEndOfScrambling()
616
    {
617
    return mShouldReactToEndOfScrambling;
618
    }
619

    
620
///////////////////////////////////////////////////////////////////////////////////////////////////
621

    
622
  public void receiveUpdate(RubikUpdates updates)
623
    {
624
    Activity act = mWeakAct.get();
625

    
626
    if( act!=null )
627
      {
628
      act.runOnUiThread(new Runnable()
629
        {
630
        @Override
631
        public void run()
632
          {
633
          int num = updates.getCompletedNumber();
634

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

    
652
///////////////////////////////////////////////////////////////////////////////////////////////////
653

    
654
  public void errorUpdate()
655
    {
656
    android.util.Log.e("D", "Screen: Error receiving update");
657
    }
658
  }
(5-5/10)