Project

General

Profile

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

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

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.RubikObject;
47
import org.distorted.objects.RubikObjectList;
48

    
49
import static android.view.View.inflate;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  void leaveScreen(RubikActivity act)
75
    {
76

    
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

    
87
    mMenuButtonHeight = (int)(mScreenWidth*RubikActivity.MENU_BUTTON_HEIGHT);
88
    mMenuTextSize     = (int)(mScreenWidth*RubikActivity.MENU_MAIN_TEXT_SIZE);
89

    
90
    mObjectPopup = null;
91

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

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

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

    
108
//////////////////////////////////////////////////////////////////////////////////////////////////
109

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

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

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
173
    {
174
    int numObjects = RubikObjectList.getNumObjects();
175
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
176
    mColCount = NUM_COLUMNS;
177

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

    
183
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
184
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
185

    
186
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
187
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
188

    
189
    objectGrid.setColumnCount(mColCount);
190
    objectGrid.setRowCount(mRowCount);
191

    
192
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
193
    setupBottomLayout(act,bottomLayout);
194

    
195
    mObjectPopup = new PopupWindow(act);
196
    mObjectPopup.setFocusable(true);
197
    mObjectPopup.setContentView(view);
198

    
199
    int[] nextInRow = new int[mRowCount];
200

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

    
211
    for(int object=0; object<numObjects; object++)
212
      {
213
      final int ordinal = object;
214
      final RubikObject rObject = RubikObjectList.getObject(object);
215
      int row = object/NUM_COLUMNS;
216
      ImageButton button = new ImageButton(act);
217
      if( rObject!=null ) rObject.setIconTo(act,button);
218

    
219
      button.setOnClickListener( new View.OnClickListener()
220
        {
221
        @Override
222
        public void onClick(View v)
223
          {
224
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
225
            {
226
            RubikObjectList.setCurrObject(ordinal);
227
            act.changeObject(ordinal,true);
228
            if( mMenuPopup!=null ) setupLevelColors(act);
229
            mMovesController.clearMoves(act);
230
            }
231

    
232
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
233
          }
234
        });
235

    
236
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
237
      params.bottomMargin = margin;
238
      params.topMargin    = margin;
239
      params.leftMargin   = margin;
240
      params.rightMargin  = margin;
241

    
242
      params.width = cubeSize;
243
      params.height= cubeSize;
244

    
245
      nextInRow[row]++;
246

    
247
      objectGrid.addView(button, params);
248
      }
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
254
    {
255
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
256
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
257
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
258

    
259
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
260
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
261
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
262

    
263
    buttonTut.setImageResource(iconT);
264
    buttonDow.setImageResource(iconD);
265
    buttonInf.setImageResource(iconI);
266

    
267
    TypedValue outValue = new TypedValue();
268
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
269
    buttonTut.setBackgroundResource(outValue.resourceId);
270
    buttonDow.setBackgroundResource(outValue.resourceId);
271
    buttonInf.setBackgroundResource(outValue.resourceId);
272

    
273
    buttonTut.setOnClickListener( new View.OnClickListener()
274
      {
275
      @Override
276
      public void onClick(View v)
277
        {
278
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
279
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
280
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
281
        }
282
      });
283

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

    
295
    buttonInf.setOnClickListener( new View.OnClickListener()
296
      {
297
      @Override
298
      public void onClick(View v)
299
        {
300
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
301
        int currObject = RubikObjectList.getCurrObject();
302
        act.switchConfig(currObject);
303
        }
304
      });
305

    
306
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
307
    mBubbleUpdates.setVisibility(View.INVISIBLE);
308

    
309
    RubikNetwork network = RubikNetwork.getInstance();
310
    network.signUpForUpdates(this);
311
    }
312

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

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

    
321
    mSolveButton.setOnClickListener( new View.OnClickListener()
322
      {
323
      @Override
324
      public void onClick(View v)
325
        {
326
        act.getControl().solveObject();
327
        mMovesController.clearMoves(act);
328
        }
329
      });
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

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

    
340
    mScrambleButton.setOnClickListener( new View.OnClickListener()
341
      {
342
      @Override
343
      public void onClick(View v)
344
        {
345
        mShouldReactToEndOfScrambling = false;
346
        act.getControl().fastScrambleObject(RubikObject.FAST_SCRAMBLES);
347
        }
348
      });
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  private void setupMenuWindow(final RubikActivity act, final float width)
354
    {
355
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
356
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
357

    
358
    mMenuPopup = new PopupWindow(act);
359
    mMenuPopup.setContentView(layout);
360
    mMenuPopup.setFocusable(true);
361
    int padding = (int)(width*RubikActivity.MEDIUM_MARGIN);
362

    
363
    mMenuLayoutWidth = (int)(width*0.6f);
364
    mMenuLayoutHeight= padding + NUM_BUTTONS*(mMenuButtonHeight+padding) + 4*mMenuButtonHeight+6*padding;
365

    
366
    layout.setPadding(padding,0,padding,0);
367

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

    
386
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
387
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
388
    prettyPatterns.setOnClickListener( new View.OnClickListener()
389
        {
390
        @Override
391
        public void onClick(View v)
392
          {
393
          mMenuPopup.dismiss();
394
          RubikDialogPattern pDiag = new RubikDialogPattern();
395
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
396
          }
397
        });
398

    
399
    Button solver = layout.findViewById(R.id.menuSolver);
400
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
401
    solver.setOnClickListener( new View.OnClickListener()
402
        {
403
        @Override
404
        public void onClick(View v)
405
          {
406
          mMenuPopup.dismiss();
407
          ScreenList.switchScreen(act, ScreenList.SVER);
408
          }
409
        });
410

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

    
424
    Button bandaged = layout.findViewById(R.id.menuBandaged);
425
    bandaged.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
426
    bandaged.setOnClickListener( new View.OnClickListener()
427
        {
428
        @Override
429
        public void onClick(View v)
430
          {
431
          mMenuPopup.dismiss();
432
          act.switchToBandagedCreator();
433
          }
434
        });
435

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

    
449
    TextView levels = layout.findViewById(R.id.menuLevels);
450
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
451

    
452
    setupLevelButtons(act,layout,padding);
453
    }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

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

    
464
    mLevel = new Button[LEVELS_SHOWN+1];
465

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

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

    
488
          if(control.isUINotBlocked())
489
            {
490
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
491

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

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

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

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

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

    
527
  public void savePreferences(SharedPreferences.Editor editor)
528
    {
529
    editor.putInt("play_LevelValue", mLevelValue );
530

    
531
    if( mObjectPopup!=null )
532
      {
533
      mObjectPopup.dismiss();
534
      mObjectPopup = null;
535
      }
536

    
537
    if( mMenuPopup!=null )
538
      {
539
      mMenuPopup.dismiss();
540
      mMenuPopup = null;
541
      }
542
    }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
  public void restorePreferences(SharedPreferences preferences)
547
    {
548
    mLevelValue = preferences.getInt("play_LevelValue", 0);
549
    }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
553

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

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

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

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

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597

    
598
  public int getLevel()
599
    {
600
    return mLevelValue;
601
    }
602

    
603
///////////////////////////////////////////////////////////////////////////////////////////////////
604

    
605
  public void recreatePopup()
606
    {
607
    mObjectPopup = null;
608
    }
609

    
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611

    
612
  public boolean shouldReactToEndOfScrambling()
613
    {
614
    return mShouldReactToEndOfScrambling;
615
    }
616

    
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618

    
619
  public void receiveUpdate(RubikUpdates updates)
620
    {
621
    Activity act = mWeakAct.get();
622

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

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

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

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