Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 1088a219

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.os.Build;
28
import android.os.Bundle;
29
import android.util.TypedValue;
30
import android.view.Gravity;
31
import android.view.LayoutInflater;
32
import android.view.View;
33
import android.widget.Button;
34
import android.widget.GridLayout;
35
import android.widget.ImageButton;
36
import android.widget.LinearLayout;
37
import android.widget.PopupWindow;
38
import android.widget.RelativeLayout;
39
import android.widget.TextView;
40

    
41
import org.distorted.dialogs.RubikDialogUpdates;
42
import org.distorted.external.RubikNetwork;
43
import org.distorted.external.RubikScores;
44
import org.distorted.external.RubikUpdates;
45

    
46
import org.distorted.main.R;
47
import org.distorted.main.RubikActivity;
48
import org.distorted.dialogs.RubikDialogAbout;
49
import org.distorted.dialogs.RubikDialogPattern;
50
import org.distorted.dialogs.RubikDialogScores;
51
import org.distorted.dialogs.RubikDialogTutorial;
52
import org.distorted.helpers.TransparentImageButton;
53
import org.distorted.objectlib.main.ObjectControl;
54
import org.distorted.objects.MainEntry;
55
import org.distorted.objects.MainEntryList;
56
import org.distorted.objects.RubikObject;
57
import org.distorted.objects.RubikObjectList;
58

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
64
  {
65
  public static final int NUM_COLUMNS  = 5;
66
  public static final int LEVELS_SHOWN = 8;
67
  private static final int NUM_BUTTONS = 5;
68
  private static final int[] mLocation = new int[2];
69

    
70
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
71
  private PopupWindow mObjectPopup, mMenuPopup;
72
  private WeakReference<RubikActivity> mWeakAct;
73
  private TextView mBubbleUpdates;
74
  private Button[] mLevel;
75
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mMenuButtonHeight, mMenuTextSize;
76
  private int mLevelValue;
77
  private int mColCount, mRowCount, mMaxRowCount;
78
  private int mUpperBarHeight;
79
  private boolean mShouldReactToEndOfScrambling;
80
  private float mScreenWidth;
81

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

    
84
  void leaveScreen(RubikActivity act)
85
    {
86

    
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  void enterScreen(final RubikActivity act)
92
    {
93
    mWeakAct = new WeakReference<>(act);
94
    int numObjects = RubikObjectList.getNumObjects();
95
    mScreenWidth = act.getScreenWidthInPixels();
96
    mUpperBarHeight = act.getHeightUpperBar();
97

    
98
    mMenuButtonHeight = (int)(mScreenWidth*RubikActivity.MENU_BUTTON_HEIGHT);
99
    mMenuTextSize     = (int)(mScreenWidth*RubikActivity.MENU_MAIN_TEXT_SIZE);
100

    
101
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
102
    mColCount = NUM_COLUMNS;
103

    
104
    // TOP ////////////////////////////
105
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
106
    layoutTop.removeAllViews();
107

    
108
    setupSolveButton(act);
109
    layoutTop.addView(mSolveButton);
110
    setupMenuButton(act,mScreenWidth);
111
    layoutTop.addView(mMenuButton);
112
    setupScrambleButton(act);
113
    layoutTop.addView(mScrambleButton);
114

    
115
    // BOTTOM /////////////////////////
116
    setupObjectButton(act,mScreenWidth);
117
    createBottomPane(act,mObjButton,null);
118
    }
119

    
120
//////////////////////////////////////////////////////////////////////////////////////////////////
121

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

    
129
    mObjButton.setOnClickListener( new View.OnClickListener()
130
      {
131
      @Override
132
      public void onClick(View view)
133
        {
134
        if( mObjectPopup==null )
135
          {
136
          float width = act.getScreenWidthInPixels();
137
          float height= act.getScreenHeightInPixels();
138
          setupObjectWindow(act,width,height);
139
          }
140

    
141
        if( act.getControl().isUINotBlocked())
142
          {
143
          int rowCount = Math.min(mMaxRowCount,mRowCount);
144
          View popupView = mObjectPopup.getContentView();
145
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
146
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
147
          }
148
        }
149
      });
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private void setupMenuButton(final RubikActivity act, final float width)
155
    {
156
    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);
157
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
158
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
159

    
160
    mMenuButton.setOnClickListener( new View.OnClickListener()
161
      {
162
      @Override
163
      public void onClick(View view)
164
        {
165
        if( mMenuPopup==null )
166
          {
167
          float width = act.getScreenWidthInPixels();
168
          setupMenuWindow(act,width);
169
          }
170

    
171
        if( act.getControl().isUINotBlocked())
172
          {
173
          View popupView = mMenuPopup.getContentView();
174
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
175
          setupLevelColors();
176
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-mMenuLayoutWidth/2 + width/6),0);
177
          }
178
        }
179
      });
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
185
    {
186
    int cubeSize = (int)(width/9);
187
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
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

    
194
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
195
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
196

    
197
    objectGrid.setColumnCount(mColCount);
198
    objectGrid.setRowCount(mRowCount);
199

    
200
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
201
    setupBottomLayout(act,bottomLayout);
202

    
203
    mObjectPopup = new PopupWindow(act);
204
    mObjectPopup.setFocusable(true);
205
    mObjectPopup.setContentView(view);
206

    
207
    int[] nextInRow = new int[mRowCount];
208

    
209
    for(int row=0; row<mRowCount; row++)
210
      {
211
      rowSpecs[row] = GridLayout.spec(row);
212
      nextInRow[row]= 0;
213
      }
214
    for(int col=0; col<mColCount; col++)
215
      {
216
      colSpecs[col] = GridLayout.spec(col);
217
      }
218

    
219
    MainEntryList list = MainEntryList.getInstance();
220
    int numEntries = list.getNumOfEntries();
221

    
222
    for(int entry=0; entry<numEntries; entry++)
223
      {
224
      final MainEntry mainEntry = list.getEntry(entry);
225
      int row = entry/NUM_COLUMNS;
226
      ImageButton button = new ImageButton(act);
227
      if( mainEntry!=null ) mainEntry.setIconTo(act,button);
228

    
229
      button.setOnClickListener( new View.OnClickListener()
230
        {
231
        @Override
232
        public void onClick(View v)
233
          {
234
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
235
            {
236
            int type = mainEntry!=null ? mainEntry.getType() : -1;
237

    
238
            if( type==MainEntry.TYPE_OBJECT )
239
              {
240
              int ordinal = mainEntry.getOrdinal();
241
              RubikObjectList.setCurrObject(ordinal);
242
              act.changeObject(ordinal,true);
243
              if( mMenuPopup!=null ) setupLevelColors();
244
              mMovesController.clearMoves(act);
245
              }
246
            else if( type==MainEntry.TYPE_CREATOR )
247
              {
248
              act.switchToBandagedCreator();
249
              }
250
            }
251

    
252
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
253
          }
254
        });
255

    
256
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
257
      params.bottomMargin = margin;
258
      params.topMargin    = margin;
259
      params.leftMargin   = margin;
260
      params.rightMargin  = margin;
261

    
262
      params.width = cubeSize;
263
      params.height= cubeSize;
264

    
265
      nextInRow[row]++;
266

    
267
      objectGrid.addView(button, params);
268
      }
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
274
    {
275
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
276
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
277
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
278

    
279
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
280
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
281
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
282

    
283
    buttonTut.setImageResource(iconT);
284
    buttonDow.setImageResource(iconD);
285
    buttonInf.setImageResource(iconI);
286

    
287
    TypedValue outValue = new TypedValue();
288
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
289
    buttonTut.setBackgroundResource(outValue.resourceId);
290
    buttonDow.setBackgroundResource(outValue.resourceId);
291
    buttonInf.setBackgroundResource(outValue.resourceId);
292

    
293
    buttonTut.setOnClickListener( new View.OnClickListener()
294
      {
295
      @Override
296
      public void onClick(View v)
297
        {
298
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
299
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
300
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
301
        }
302
      });
303

    
304
    buttonDow.setOnClickListener( new View.OnClickListener()
305
      {
306
      @Override
307
      public void onClick(View v)
308
        {
309
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
310
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
311
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
312
        }
313
      });
314

    
315
    buttonInf.setOnClickListener( new View.OnClickListener()
316
      {
317
      @Override
318
      public void onClick(View v)
319
        {
320
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
321
        int currObject = RubikObjectList.getCurrObject();
322
        act.switchConfig(currObject);
323
        }
324
      });
325

    
326
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
327
    mBubbleUpdates.setVisibility(View.INVISIBLE);
328

    
329
    RubikNetwork network = RubikNetwork.getInstance();
330
    network.signUpForUpdates(this);
331
    }
332

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

    
335
  void setupSolveButton(final RubikActivity act)
336
    {
337
    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);
338
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
339
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
340

    
341
    mSolveButton.setOnClickListener( new View.OnClickListener()
342
      {
343
      @Override
344
      public void onClick(View v)
345
        {
346
        act.getControl().solveObject();
347
        mMovesController.clearMoves(act);
348
        }
349
      });
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  private void setupScrambleButton(final RubikActivity act)
355
    {
356
    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);
357
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
358
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
359

    
360
    mScrambleButton.setOnClickListener( new View.OnClickListener()
361
      {
362
      @Override
363
      public void onClick(View v)
364
        {
365
        int currObject = RubikObjectList.getCurrObject();
366
        RubikObject object = RubikObjectList.getObject(currObject);
367
        int numScrambles = object==null ? 0 : object.getNumScramble();
368
        mShouldReactToEndOfScrambling = false;
369
        act.getControl().fastScrambleObject(numScrambles);
370
        }
371
      });
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  private void setupMenuWindow(final RubikActivity act, final float width)
377
    {
378
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
379
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
380

    
381
    mMenuPopup = new PopupWindow(act);
382
    mMenuPopup.setContentView(layout);
383
    mMenuPopup.setFocusable(true);
384
    int padding = (int)(width*RubikActivity.MEDIUM_MARGIN);
385

    
386
    mMenuLayoutWidth = (int)(width*0.6f);
387
    mMenuLayoutHeight= (int)( padding + NUM_BUTTONS*(mMenuButtonHeight+padding) + 4*mMenuButtonHeight+6*padding);
388

    
389
    layout.setPadding(padding,0,padding,0);
390

    
391
    Button highScores = layout.findViewById(R.id.menuHighScores);
392
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
393
    highScores.setOnClickListener( new View.OnClickListener()
394
        {
395
        @Override
396
        public void onClick(View v)
397
          {
398
          mMenuPopup.dismiss();
399
          Bundle sBundle = new Bundle();
400
          int currObject = RubikObjectList.getCurrObject();
401
          sBundle.putInt("tab", currObject );
402
          sBundle.putBoolean("submitting", false);
403
          RubikDialogScores scores = new RubikDialogScores();
404
          scores.setArguments(sBundle);
405
          scores.show(act.getSupportFragmentManager(), null);
406
          }
407
        });
408

    
409
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
410
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
411
    prettyPatterns.setOnClickListener( new View.OnClickListener()
412
        {
413
        @Override
414
        public void onClick(View v)
415
          {
416
          mMenuPopup.dismiss();
417
          RubikDialogPattern pDiag = new RubikDialogPattern();
418
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
419
          }
420
        });
421

    
422
    Button solver = layout.findViewById(R.id.menuSolver);
423
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
424
    solver.setOnClickListener( new View.OnClickListener()
425
        {
426
        @Override
427
        public void onClick(View v)
428
          {
429
          mMenuPopup.dismiss();
430
          ScreenList.switchScreen(act, ScreenList.SVER);
431
          }
432
        });
433

    
434
    Button tutorials = layout.findViewById(R.id.menuTutorials);
435
    tutorials.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
436
    tutorials.setOnClickListener( new View.OnClickListener()
437
        {
438
        @Override
439
        public void onClick(View v)
440
          {
441
          mMenuPopup.dismiss();
442
          RubikDialogTutorial tDiag = new RubikDialogTutorial();
443
          tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
444
          }
445
        });
446

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

    
460
    TextView levels = layout.findViewById(R.id.menuLevels);
461
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
462

    
463
    setupLevelButtons(act,layout,padding);
464
    setupLevelColors();
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  private void setupLevelButtons(RubikActivity act, View layout, int padding)
470
    {
471
    int sizeW = (mMenuLayoutWidth-4*padding)/3;
472
    int sizeH = (int)(sizeW*0.8f);
473
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(sizeW,sizeH);
474
    params.setMargins(padding/2,0,padding/2,0);
475

    
476
    mLevel = new Button[LEVELS_SHOWN+1];
477

    
478
    mLevel[0] = layout.findViewById(R.id.level1);
479
    mLevel[1] = layout.findViewById(R.id.level2);
480
    mLevel[2] = layout.findViewById(R.id.level3);
481
    mLevel[3] = layout.findViewById(R.id.level4);
482
    mLevel[4] = layout.findViewById(R.id.level5);
483
    mLevel[5] = layout.findViewById(R.id.level6);
484
    mLevel[6] = layout.findViewById(R.id.level7);
485
    mLevel[7] = layout.findViewById(R.id.level8);
486
    mLevel[8] = layout.findViewById(R.id.levelM);
487

    
488
    for(int i=0; i<=LEVELS_SHOWN; i++)
489
      {
490
      final int ii = i;
491
      mLevel[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
492
      mLevel[i].setLayoutParams(params);
493
      mLevel[i].setOnClickListener( new View.OnClickListener()
494
        {
495
        @Override
496
        public void onClick(View v)
497
          {
498
          ObjectControl control = act.getControl();
499

    
500
          if(control.isUINotBlocked())
501
            {
502
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
503

    
504
            int currObject = RubikObjectList.getCurrObject();
505
            RubikObject object = RubikObjectList.getObject(currObject);
506
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
507
            mLevelValue = ii+1;
508
            mShouldReactToEndOfScrambling = true;
509
            control.scrambleObject(scrambles);
510
            }
511
          }
512
        });
513
      }
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  private void setupLevelColors()
519
    {
520
    int currObject = RubikObjectList.getCurrObject();
521
    int dbLevel = RubikObjectList.getDBLevel(currObject);
522
    RubikScores scores = RubikScores.getInstance();
523

    
524
    for(int i=0; i<=LEVELS_SHOWN; i++)
525
      {
526
      int level = i<LEVELS_SHOWN ? i : dbLevel;
527
      boolean isSolved = scores.isSolved(currObject,level);
528
      int icon = isSolved ? R.drawable.ui_solved : R.drawable.ui_notsolved;
529
      mLevel[i].setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
530
      }
531
    }
532

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

    
539
  public void savePreferences(SharedPreferences.Editor editor)
540
    {
541
    editor.putInt("play_LevelValue", mLevelValue );
542

    
543
    if( mObjectPopup!=null )
544
      {
545
      mObjectPopup.dismiss();
546
      mObjectPopup = null;
547
      }
548

    
549
    if( mMenuPopup!=null )
550
      {
551
      mMenuPopup.dismiss();
552
      mMenuPopup = null;
553
      }
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
  public void restorePreferences(SharedPreferences preferences)
559
    {
560
    mLevelValue = preferences.getInt("play_LevelValue", 0);
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
565

    
566
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
567
    {
568
    View topLayout = act.findViewById(R.id.relativeLayout);
569
    boolean isFullScreen;
570

    
571
    if( topLayout!=null )
572
      {
573
      topLayout.getLocationOnScreen(mLocation);
574
      isFullScreen = (mLocation[1]==0);
575
      }
576
    else
577
      {
578
      isFullScreen = true;
579
      }
580

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

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

    
608
///////////////////////////////////////////////////////////////////////////////////////////////////
609

    
610
  public int getLevel()
611
    {
612
    return mLevelValue;
613
    }
614

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

    
617
  public void recreatePopup()
618
    {
619
    mObjectPopup = null;
620

    
621
    int numObjects = RubikObjectList.getNumObjects();
622
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
623
    mColCount = NUM_COLUMNS;
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627

    
628
  public boolean shouldReactToEndOfScrambling()
629
    {
630
    return mShouldReactToEndOfScrambling;
631
    }
632

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

    
635
  public void receiveUpdate(RubikUpdates updates)
636
    {
637
    Activity act = mWeakAct.get();
638

    
639
    if( act!=null )
640
      {
641
      act.runOnUiThread(new Runnable()
642
        {
643
        @Override
644
        public void run()
645
          {
646
          int num = updates.getCompletedNumber();
647

    
648
          if( num>0 )
649
            {
650
            String shownNum = String.valueOf(num);
651
            mBubbleUpdates.setText(shownNum);
652
            mBubbleUpdates.setVisibility(View.VISIBLE);
653
            int height = (int)(0.05f*mScreenWidth);
654
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
655
            }
656
         else
657
            {
658
            mBubbleUpdates.setVisibility(View.INVISIBLE);
659
            }
660
          }
661
        });
662
      }
663
    }
664

    
665
///////////////////////////////////////////////////////////////////////////////////////////////////
666

    
667
  public void errorUpdate()
668
    {
669
    android.util.Log.e("D", "Screen: Error receiving update");
670
    }
671
  }
(5-5/10)