Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 1c04d054

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.view.ViewGroup;
26
import android.widget.Button;
27
import android.widget.GridLayout;
28
import android.widget.ImageButton;
29
import android.widget.LinearLayout;
30
import android.widget.PopupWindow;
31
import android.widget.RelativeLayout;
32
import android.widget.TextView;
33

    
34
import org.distorted.dialogs.RubikDialogCreators;
35
import org.distorted.dialogs.RubikDialogSolvers;
36
import org.distorted.dialogs.RubikDialogStarsStatus;
37
import org.distorted.dialogs.RubikDialogUpdates;
38
import org.distorted.dialogs.RubikDialogAbout;
39
import org.distorted.external.RubikNetwork;
40
import org.distorted.external.RubikScores;
41
import org.distorted.external.RubikUpdates;
42

    
43
import org.distorted.helpers.PopupCreator;
44
import org.distorted.main.R;
45
import org.distorted.main_old.RubikActivity;
46
import org.distorted.dialogs.RubikDialogPattern;
47
import org.distorted.dialogs.RubikDialogScores;
48
import org.distorted.dialogs.RubikDialogTutorial;
49
import org.distorted.helpers.TransparentImageButton;
50
import org.distorted.objectlib.effects.BaseEffect;
51
import org.distorted.objectlib.main.ObjectControl;
52
import org.distorted.objects.RubikObject;
53
import org.distorted.objects.RubikObjectList;
54

    
55
import static android.view.View.GONE;
56
import static android.view.View.inflate;
57
import static org.distorted.main_old.RubikActivity.USE_IAP;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
62
  {
63
  private static final int MP = LinearLayout.LayoutParams.MATCH_PARENT;
64

    
65
  private static final float MENU_BUTTON_HEIGHT  = 0.068f;
66
  private static final float MENU_TEXT_SIZE      = 0.024f;
67
  private static final float MENU_MARGIN         = 0.008f;
68
  private static final float MENU_WIDTH          = 0.650f;
69
  private static final float LEVEL_BUTTON_HEIGHT = 0.089f;
70

    
71
  public static final int NUM_COLUMNS  = 5;
72
  public static final int LEVELS_SHOWN = 8;
73
  private static final int[] mLocation = new int[2];
74

    
75
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
76
  private PopupWindow mObjectPopup, mMenuPopup;
77
  private WeakReference<RubikActivity> mWeakAct;
78
  private TextView mBubbleUpdates;
79
  private Button[] mLevel;
80
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mMenuButtonHeight, mMenuTextSize;
81
  private int mLevelValue;
82
  private int mColCount, mRowCount, mMaxRowCount;
83
  private int mUpperBarHeight;
84
  private boolean mShouldReactToEndOfScrambling;
85
  private float mScreenWidth, mScreenHeight;
86
  private int mLevelHeight, mLevelWidth;
87
  private int mOldNumScramble;
88

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

    
91
  void leaveScreen(RubikActivity act)
92
    {
93

    
94
    }
95

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

    
98
  void enterScreen(final RubikActivity act)
99
    {
100
    mWeakAct = new WeakReference<>(act);
101
    mScreenWidth  = act.getScreenWidthInPixels();
102
    mScreenHeight = act.getScreenHeightInPixels();
103
    mUpperBarHeight = act.getHeightUpperBar();
104
    mMenuButtonHeight = (int)(mScreenHeight*MENU_BUTTON_HEIGHT);
105
    mMenuTextSize     = (int)(mScreenHeight*MENU_TEXT_SIZE);
106

    
107
    mObjectPopup = null;
108

    
109
    // TOP ////////////////////////////
110
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
111
    layoutTop.removeAllViews();
112

    
113
    setupSolveButton(act);
114
    layoutTop.addView(mSolveButton);
115
    setupMenuButton(act,mScreenWidth, mScreenHeight);
116
    layoutTop.addView(mMenuButton);
117
    setupScrambleButton(act);
118
    layoutTop.addView(mScrambleButton);
119

    
120
    // BOTTOM /////////////////////////
121
    setupObjectButton(act,mScreenWidth);
122
    createBottomPane(act,mObjButton);
123
    }
124

    
125
//////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void setupObjectButton(final RubikActivity act, final float width)
128
    {
129
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
130
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MP,MP,1.0f);
131
    mObjButton = new TransparentImageButton(act, R.drawable.ui_cube_menu,params);
132

    
133
    mObjButton.setOnClickListener( new View.OnClickListener()
134
      {
135
      @Override
136
      public void onClick(View view)
137
        {
138
        if( act.getControl().isScramblingAndSolvingNotBlocked() )
139
          {
140
          if( mObjectPopup==null )
141
            {
142
            float width = act.getScreenWidthInPixels();
143
            float height= act.getScreenHeightInPixels();
144
            setupObjectWindow(act,width,height);
145
            }
146

    
147
          int rowCount = Math.min(mMaxRowCount,mRowCount);
148
          View popupView = mObjectPopup.getContentView();
149
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
150
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
151
          }
152
        }
153
      });
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private void setupMenuButton(final RubikActivity act, final float width, final float height)
159
    {
160
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MP,MP,1.0f);
161
    mMenuButton = new TransparentImageButton(act, R.drawable.ui_menu,params);
162

    
163
    mMenuButton.setOnClickListener( new View.OnClickListener()
164
      {
165
      @Override
166
      public void onClick(View view)
167
        {
168
        if( act.getControl().isScramblingAndSolvingNotBlocked() )
169
          {
170
          if( mMenuPopup==null )
171
            {
172
            float width = act.getScreenWidthInPixels();
173
            setupMenuWindow(act,width,height);
174
            }
175

    
176
          View popupView = mMenuPopup.getContentView();
177
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
178
          setupLevelButtonVisibilityAndColor(act);
179
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-mMenuLayoutWidth/2 + width/6),0);
180
          }
181
        }
182
      });
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
188
    {
189
    int numObjects = RubikObjectList.getNumObjects();
190
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
191
    mColCount = NUM_COLUMNS;
192

    
193
    int cubeSize = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
194
    int margin   = (int)(height*RubikActivity.POPUP_MARGIN);
195
    int padding  = (int)(height*RubikActivity.POPUP_PADDING);
196
    int botHeight= (int)(height*RubikActivity.POPUP_BOTTOM);
197
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
198
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
199

    
200
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
201
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
202
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
203

    
204
    ViewGroup.LayoutParams params = bottomLayout.getLayoutParams();
205
    params.height = botHeight;
206

    
207
    setupBottomLayout(act,bottomLayout);
208

    
209
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
210

    
211
    for(int child=0; child<numObjects; child++)
212
      {
213
      final RubikObject obj = RubikObjectList.getObject(child);
214
      View v = objectGrid.getChildAt(child);
215
      ImageButton button = PopupCreator.getButton(obj,v);
216
      final int ordinal = child;
217

    
218
      button.setOnClickListener( new View.OnClickListener()
219
        {
220
        @Override
221
        public void onClick(View v)
222
          {
223
          if( obj!=null && act.getControl().isScramblingAndSolvingNotBlocked() && ScreenList.getCurrentScreen()==ScreenList.PLAY )
224
            {
225
            if( obj.isFree() )
226
              {
227
              RubikObjectList.setCurrObject(ordinal);
228
              act.changeObject(ordinal,true);
229
              if( mMenuPopup!=null ) setupLevelButtonVisibilityAndColor(act);
230
              mMovesController.clearMoves(act);
231
              }
232
            else
233
              {
234
              act.switchToPurchase(ordinal);
235
              }
236
            }
237

    
238
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
239
          }
240
        });
241
      }
242

    
243
    mObjectPopup = new PopupWindow(act);
244
    mObjectPopup.setFocusable(true);
245
    mObjectPopup.setContentView(view);
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
251
    {
252
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
253
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
254
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
255

    
256
    buttonTut.setImageResource(R.drawable.ui_tutorial);
257
    buttonDow.setImageResource(R.drawable.ui_download);
258
    buttonInf.setImageResource(R.drawable.ui_info);
259

    
260
    TypedValue outValue = new TypedValue();
261
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
262
    buttonTut.setBackgroundResource(outValue.resourceId);
263
    buttonDow.setBackgroundResource(outValue.resourceId);
264
    buttonInf.setBackgroundResource(outValue.resourceId);
265

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

    
277
    buttonDow.setOnClickListener( new View.OnClickListener()
278
      {
279
      @Override
280
      public void onClick(View v)
281
        {
282
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
283
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
284
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
285
        }
286
      });
287

    
288
    buttonInf.setOnClickListener( new View.OnClickListener()
289
      {
290
      @Override
291
      public void onClick(View v)
292
        {
293
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
294
        int currObject = RubikObjectList.getCurrObject();
295
        act.switchToConfig(currObject);
296
        }
297
      });
298

    
299
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
300
    mBubbleUpdates.setVisibility(View.INVISIBLE);
301

    
302
    RubikNetwork network = RubikNetwork.getInstance();
303
    network.signUpForUpdates(this);
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  void setupSolveButton(final RubikActivity act)
309
    {
310
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MP,MP,1.0f);
311
    mSolveButton = new TransparentImageButton(act, R.drawable.ui_cube_solve,params);
312

    
313
    mSolveButton.setOnClickListener( new View.OnClickListener()
314
      {
315
      @Override
316
      public void onClick(View v)
317
        {
318
        act.getControl().solveObject();
319
        mMovesController.clearMoves(act);
320
        }
321
      });
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  private void setupScrambleButton(final RubikActivity act)
327
    {
328
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MP,MP,1.0f);
329
    mScrambleButton = new TransparentImageButton(act, R.drawable.ui_cube_scramble,params);
330

    
331
    mScrambleButton.setOnClickListener( new View.OnClickListener()
332
      {
333
      @Override
334
      public void onClick(View v)
335
        {
336
        int duration = BaseEffect.Type.FAST_SCRAMBLE.getDuration();
337
        boolean success = act.getControl().fastScrambleObject(duration,RubikObject.FAST_SCRAMBLES);
338
        if( success ) mShouldReactToEndOfScrambling = false;
339
        }
340
      });
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  private void setupMenuWindow(final RubikActivity act, final float width, final float height)
346
    {
347
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
348
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
349

    
350
    mMenuPopup = new PopupWindow(act);
351
    mMenuPopup.setContentView(layout);
352
    mMenuPopup.setFocusable(true);
353
    int padding = (int)(height*MENU_MARGIN);
354
    int numButtons = USE_IAP ? 7 : 6;
355
    mLevelHeight = (int)(height*LEVEL_BUTTON_HEIGHT);
356
    mMenuLayoutHeight= padding + numButtons*(mMenuButtonHeight+padding) + mLevelHeight/3 + 3*(mLevelHeight+padding);
357
    mMenuLayoutWidth = Math.min((int)(width*MENU_WIDTH),mMenuLayoutHeight/2);
358
    mLevelWidth = (mMenuLayoutWidth-4*padding)/3;
359

    
360
    layout.setPadding(padding,0,padding,0);
361

    
362
    if( USE_IAP )
363
      {
364
      Button stars = layout.findViewById(R.id.menuStars);
365
      stars.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
366
      stars.setOnClickListener( new View.OnClickListener()
367
          {
368
          @Override
369
          public void onClick(View v)
370
            {
371
            mMenuPopup.dismiss();
372
            RubikDialogStarsStatus d = new RubikDialogStarsStatus();
373
            d.show(act.getSupportFragmentManager(), null);
374
            }
375
          });
376
      }
377
    else
378
      {
379
      Button stars = layout.findViewById(R.id.menuStars);
380
      stars.setVisibility(GONE);
381
      }
382

    
383
    Button solver = layout.findViewById(R.id.menuSolver);
384
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
385
    solver.setOnClickListener( new View.OnClickListener()
386
        {
387
        @Override
388
        public void onClick(View v)
389
          {
390
          mMenuPopup.dismiss();
391
          RubikDialogSolvers solvers = new RubikDialogSolvers();
392
          solvers.show(act.getSupportFragmentManager(), RubikDialogSolvers.getDialogTag() );
393
          }
394
        });
395

    
396
    Button highScores = layout.findViewById(R.id.menuHighScores);
397
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
398
    highScores.setOnClickListener( new View.OnClickListener()
399
        {
400
        @Override
401
        public void onClick(View v)
402
          {
403
          mMenuPopup.dismiss();
404
          Bundle sBundle = new Bundle();
405
          sBundle.putString("argument", "false");
406
          RubikDialogScores scores = new RubikDialogScores();
407
          scores.setArguments(sBundle);
408
          scores.show(act.getSupportFragmentManager(), null);
409
          }
410
        });
411

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

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

    
438
    Button bandaged = layout.findViewById(R.id.menuBandaged);
439
    bandaged.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
440
    bandaged.setOnClickListener( new View.OnClickListener()
441
        {
442
        @Override
443
        public void onClick(View v)
444
          {
445
          mMenuPopup.dismiss();
446
          RubikDialogCreators creators = new RubikDialogCreators();
447
          creators.show(act.getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
448
          }
449
        });
450

    
451
    Button whatsNew = layout.findViewById(R.id.menuNew);
452
    whatsNew.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
453
    whatsNew.setOnClickListener( new View.OnClickListener()
454
        {
455
        @Override
456
        public void onClick(View v)
457
          {
458
          mMenuPopup.dismiss();
459
          RubikDialogAbout aDiag = new RubikDialogAbout();
460
          aDiag.show(act.getSupportFragmentManager(), null);
461
          }
462
        });
463

    
464
    TextView levels = layout.findViewById(R.id.menuLevels);
465
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
466

    
467
    setupLevelButtons(act,layout,padding/2);
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  private void setupLevelButtons(RubikActivity act, View layout, int margin)
473
    {
474
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mLevelWidth,mLevelHeight);
475
    params.setMargins(margin,margin,margin,margin);
476

    
477
    mOldNumScramble = LEVELS_SHOWN+1;
478

    
479
    mLevel = new Button[LEVELS_SHOWN+1];
480

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

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

    
503
          if( control.isScramblingAndSolvingNotBlocked() )
504
            {
505
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
506

    
507
            int currObject = RubikObjectList.getCurrObject();
508
            RubikObject object = RubikObjectList.getObject(currObject);
509
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
510

    
511
            boolean success = control.scrambleObject(scrambles);
512
            if( success )
513
              {
514
              mLevelValue = ii+1;
515
              mShouldReactToEndOfScrambling = true;
516
              }
517
            }
518
          }
519
        });
520
      }
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  private void setupLevelButtonVisibilityAndColor(RubikActivity act)
526
    {
527
    int currObject = RubikObjectList.getCurrObject();
528
    RubikObject object = RubikObjectList.getObject(currObject);
529
    int numScramble = object==null ? 1 : object.getNumScramble();
530
    RubikScores scores = RubikScores.getInstance();
531
    Resources res = act.getResources();
532
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
533
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
534

    
535
    for(int level=0; level<=LEVELS_SHOWN; level++)
536
      {
537
      boolean isSolved = scores.isSolved(currObject,level);
538
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
539
      }
540

    
541
    if( numScramble<=LEVELS_SHOWN || mOldNumScramble<=LEVELS_SHOWN )
542
      {
543
      if( numScramble<mOldNumScramble )
544
        {
545
        int max = Math.min(LEVELS_SHOWN,mOldNumScramble-1);
546
        for(int level=numScramble; level<=max; level++) mLevel[level].setVisibility(View.INVISIBLE);
547
        mLevel[numScramble-1].setText(R.string.levelM);
548
        }
549
      if( numScramble>mOldNumScramble )
550
        {
551
        int max = Math.min(LEVELS_SHOWN,numScramble-1);
552
        mLevel[mOldNumScramble-1].setText( String.valueOf(mOldNumScramble) );
553

    
554
        for(int level=mOldNumScramble; level<=max; level++)
555
          {
556
          mLevel[level].setVisibility(View.VISIBLE);
557
          if( level<max ) mLevel[level].setText( String.valueOf(level+1) );
558
          else            mLevel[level].setText( R.string.levelM );
559
          }
560
        }
561
      }
562

    
563
    mOldNumScramble = numScramble;
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
568
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
569
// going to be called)
570
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
571

    
572
  public void savePreferences(SharedPreferences.Editor editor)
573
    {
574
    editor.putInt("play_LevelValue", mLevelValue );
575

    
576
    if( mObjectPopup!=null )
577
      {
578
      mObjectPopup.dismiss();
579
      mObjectPopup = null;
580
      }
581

    
582
    if( mMenuPopup!=null )
583
      {
584
      mMenuPopup.dismiss();
585
      mMenuPopup = null;
586
      }
587
    }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

    
591
  public void restorePreferences(SharedPreferences preferences)
592
    {
593
    mLevelValue = preferences.getInt("play_LevelValue", 0);
594
    }
595

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
598

    
599
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
600
    {
601
    View topLayout = act.findViewById(R.id.relativeLayout);
602
    boolean isFullScreen;
603

    
604
    if( topLayout!=null )
605
      {
606
      topLayout.getLocationOnScreen(mLocation);
607
      isFullScreen = (mLocation[1]==0);
608
      }
609
    else
610
      {
611
      isFullScreen = true;
612
      }
613

    
614
    try
615
      {
616
      // if on Android 11 or we are fullscreen
617
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
618
        {
619
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
620
        window.update(view, w, h);
621
        }
622
      else  // Android 10 or below in pop-up mode or split-screen mode
623
        {
624
        view.getLocationOnScreen(mLocation);
625
        int width  = view.getWidth();
626
        int height = view.getHeight();
627
        int x = mLocation[0]+(width-w)/2;
628
        int y = mLocation[1]+height+yoff;
629

    
630
        window.showAsDropDown(view);
631
        window.update(x,y,w,h);
632
        }
633
      }
634
    catch( IllegalArgumentException iae )
635
      {
636
      // ignore, this means window is 'not attached to window manager' -
637
      // which most probably is because we are already exiting the app.
638
      }
639
    }
640

    
641
///////////////////////////////////////////////////////////////////////////////////////////////////
642

    
643
  public int getLevel()
644
    {
645
    return mLevelValue;
646
    }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649

    
650
  public void recreatePopup()
651
    {
652
    mObjectPopup = null;
653
    }
654

    
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656

    
657
  public boolean shouldReactToEndOfScrambling()
658
    {
659
    return mShouldReactToEndOfScrambling;
660
    }
661

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663

    
664
  public void receiveUpdate(RubikUpdates updates)
665
    {
666
    Activity act = mWeakAct.get();
667

    
668
    if( act!=null )
669
      {
670
      act.runOnUiThread(new Runnable()
671
        {
672
        @Override
673
        public void run()
674
          {
675
          int num = updates.getCompletedNumber();
676

    
677
          if( num>0 )
678
            {
679
            String shownNum = String.valueOf(num);
680
            mBubbleUpdates.setText(shownNum);
681
            mBubbleUpdates.setVisibility(View.VISIBLE);
682
            int height = (int)(0.05f*mScreenWidth);
683
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
684
            }
685
         else
686
            {
687
            mBubbleUpdates.setVisibility(View.INVISIBLE);
688
            }
689
          }
690
        });
691
      }
692
    }
693

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

    
696
  public void errorUpdate()
697
    {
698
    android.util.Log.e("D", "RubikScreenPlay: Error receiving downloaded objects update");
699
    }
700

    
701
///////////////////////////////////////////////////////////////////////////////////////////////////
702

    
703
  public int getType()
704
    {
705
    return 0;
706
    }
707

    
708
///////////////////////////////////////////////////////////////////////////////////////////////////
709

    
710
  public void objectDownloaded(String shortName)
711
    {
712
    recreatePopup();
713
    }
714
  }
(5-5/10)