Project

General

Profile

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

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

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

    
43
import org.distorted.dialogs.RubikDialogUpdates;
44
import org.distorted.external.RubikNetwork;
45
import org.distorted.external.RubikUpdates;
46
import org.distorted.objectlib.main.ObjectControl;
47

    
48
import org.distorted.main.R;
49
import org.distorted.main.RubikActivity;
50
import org.distorted.dialogs.RubikDialogAbout;
51
import org.distorted.dialogs.RubikDialogPattern;
52
import org.distorted.dialogs.RubikDialogScores;
53
import org.distorted.dialogs.RubikDialogTutorial;
54
import org.distorted.helpers.TransparentButton;
55
import org.distorted.helpers.TransparentImageButton;
56
import org.distorted.external.RubikScores;
57
import org.distorted.objects.RubikObject;
58
import org.distorted.objects.RubikObjectList;
59

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
65
  {
66
  public static final int NUM_COLUMNS  = 5;
67
  public static final int LEVELS_SHOWN = 10;
68

    
69
  private static final int[] BUTTON_LABELS = { R.string.scores,
70
                                               R.string.patterns,
71
                                               R.string.solver,
72
                                               R.string.tutorials,
73
                                               R.string.about };
74

    
75
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
76
  private static final float LAST_BUTTON = 1.5f;
77
  private static final int[] mLocation = new int[2];
78

    
79
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
80
  private TransparentButton mPlayButton;
81
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
82
  private LinearLayout mPlayLayout;
83
  private TextView mBubbleUpdates;
84
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
85
  private int mLevelValue;
86
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
87
  private int mColCount, mRowCount, mMaxRowCount;
88
  private int mUpperBarHeight;
89
  private boolean mShouldReactToEndOfScrambling;
90
  private int mBottomHeight;
91
  private float mScreenWidth;
92
  private WeakReference<RubikActivity> mWeakAct;
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  void leaveScreen(RubikActivity act)
97
    {
98

    
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  void enterScreen(final RubikActivity act)
104
    {
105
    mWeakAct = new WeakReference<>(act);
106
    int numObjects = RubikObjectList.getNumObjects();
107
    mScreenWidth = act.getScreenWidthInPixels();
108
    mUpperBarHeight = act.getHeightUpperBar();
109

    
110
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
111
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
112
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
113

    
114
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
115
    mColCount = NUM_COLUMNS;
116

    
117
    // TOP ////////////////////////////
118
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
119
    layoutTop.removeAllViews();
120

    
121
    setupObjectButton(act,mScreenWidth);
122
    layoutTop.addView(mObjButton);
123

    
124
    setupMenuButton(act,mScreenWidth);
125
    layoutTop.addView(mMenuButton);
126

    
127
    setupPlayButton(act,mScreenWidth);
128
    layoutTop.addView(mPlayButton);
129

    
130
    setupSolveButton(act);
131
    setupScrambleButton(act);
132
    createBottomPane(act,mSolveButton,mScrambleButton);
133
    }
134

    
135
//////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private void setupObjectButton(final RubikActivity act, final float width)
138
    {
139
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
140
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
141
    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);
142
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
143
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
144

    
145
    mObjButton.setOnClickListener( new View.OnClickListener()
146
      {
147
      @Override
148
      public void onClick(View view)
149
        {
150
        if( mObjectPopup==null )
151
          {
152
          float width = act.getScreenWidthInPixels();
153
          float height= act.getScreenHeightInPixels();
154
          setupObjectWindow(act,width,height);
155
          }
156

    
157
        if( act.getControl().isUINotBlocked())
158
          {
159
          int rowCount = Math.min(mMaxRowCount,mRowCount);
160
          View popupView = mObjectPopup.getContentView();
161
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
162
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
163
          }
164
        }
165
      });
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void setupPlayButton(final RubikActivity act, final float width)
171
    {
172
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
173

    
174
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
175

    
176
    mPlayButton.setOnClickListener( new View.OnClickListener()
177
      {
178
      @Override
179
      public void onClick(View view)
180
        {
181
         if( mPlayPopup==null )
182
          {
183
          float width = act.getScreenWidthInPixels();
184
          setupPlayWindow(act,width);
185
          }
186

    
187
        if( act.getControl().isUINotBlocked())
188
          {
189
          adjustSolvedIcons();
190
          float height= act.getScreenHeightInPixels();
191
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
192
          View popupView = mPlayPopup.getContentView();
193
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
194
          final int object  = RubikObjectList.getCurrObject();
195
          final int dbLevel = RubikObjectList.getDBLevel(object);
196
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
197
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
198
          final int realHeight = Math.min(popupHeight,maxHeight);
199
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
200
          }
201
        }
202
      });
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private void setupMenuButton(final RubikActivity act, final float width)
208
    {
209
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
210
    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);
211
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
212
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
213

    
214
    mMenuButton.setOnClickListener( new View.OnClickListener()
215
      {
216
      @Override
217
      public void onClick(View view)
218
        {
219
        if( mMenuPopup==null )
220
          {
221
          float width = act.getScreenWidthInPixels();
222
          setupMenuWindow(act,width);
223
          }
224

    
225
        if( act.getControl().isUINotBlocked())
226
          {
227
          View popupView = mMenuPopup.getContentView();
228
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
229
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
230
          }
231
        }
232
      });
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
238
    {
239
    int cubeWidth = (int)(width/9);
240
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
241
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
242
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
243

    
244
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
245
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
246

    
247
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
248
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
249

    
250
    objectGrid.setColumnCount(mColCount);
251
    objectGrid.setRowCount(mRowCount);
252

    
253
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
254
    setupBottomLayout(act,bottomLayout);
255

    
256
    mObjectPopup = new PopupWindow(act);
257
    mObjectPopup.setFocusable(true);
258
    mObjectPopup.setContentView(view);
259

    
260
    int[] nextInRow = new int[mRowCount];
261

    
262
    for(int row=0; row<mRowCount; row++)
263
      {
264
      rowSpecs[row] = GridLayout.spec(row);
265
      nextInRow[row]= 0;
266
      }
267
    for(int col=0; col<mColCount; col++)
268
      {
269
      colSpecs[col] = GridLayout.spec(col);
270
      }
271

    
272
    int numObjects = RubikObjectList.getNumObjects();
273

    
274
    for(int object=0; object<numObjects; object++)
275
      {
276
      final RubikObject robject = RubikObjectList.getObject(object);
277
      int icons = robject==null ? 0 : robject.getIconID();
278
      int row = object/NUM_COLUMNS;
279
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
280

    
281
      ImageButton button = new ImageButton(act);
282
      button.setBackgroundResource(icons);
283
      button.setOnClickListener( new View.OnClickListener()
284
        {
285
        @Override
286
        public void onClick(View v)
287
          {
288
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
289
            {
290
            RubikObjectList.setCurrObject(act,ordinal);
291
            act.changeObject(ordinal,true);
292
            if( mPlayLayout!=null ) adjustLevels(act);
293
            mMovesController.clearMoves(act);
294
            }
295

    
296
          mObjectPopup.dismiss();
297
          }
298
        });
299

    
300
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
301
      params.bottomMargin = margin;
302
      params.topMargin    = margin;
303
      params.leftMargin   = margin;
304
      params.rightMargin  = margin;
305

    
306
      params.width = cubeWidth;
307
      params.height= cubeWidth;
308

    
309
      nextInRow[row]++;
310

    
311
      objectGrid.addView(button, params);
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
318
    {
319
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
320
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
321
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
322

    
323
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
324
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
325
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
326

    
327
    buttonTut.setImageResource(iconT);
328
    buttonDow.setImageResource(iconD);
329
    buttonInf.setImageResource(iconI);
330

    
331
    Resources res = act.getResources();
332
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
333
    mBottomHeight = bd.getIntrinsicHeight();
334

    
335
    TypedValue outValue = new TypedValue();
336
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
337
    buttonTut.setBackgroundResource(outValue.resourceId);
338
    buttonDow.setBackgroundResource(outValue.resourceId);
339
    buttonInf.setBackgroundResource(outValue.resourceId);
340

    
341
    buttonTut.setOnClickListener( new View.OnClickListener()
342
      {
343
      @Override
344
      public void onClick(View v)
345
        {
346
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
347
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
348
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
349
        }
350
      });
351

    
352
    buttonDow.setOnClickListener( new View.OnClickListener()
353
      {
354
      @Override
355
      public void onClick(View v)
356
        {
357
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
358
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
359
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
360
        }
361
      });
362

    
363
    buttonInf.setOnClickListener( new View.OnClickListener()
364
      {
365
      @Override
366
      public void onClick(View v)
367
        {
368
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
369
        int currObject = RubikObjectList.getCurrObject();
370
        act.switchConfig(currObject);
371
        }
372
      });
373

    
374
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
375
    mBubbleUpdates.setVisibility(View.INVISIBLE);
376

    
377
    RubikNetwork network = RubikNetwork.getInstance();
378
    network.signUpForUpdates(this);
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  private void setupMenuWindow(final RubikActivity act, final float width)
384
    {
385
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
386
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
387
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
388

    
389
    mMenuPopup = new PopupWindow(act);
390
    mMenuPopup.setContentView(layout);
391
    mMenuPopup.setFocusable(true);
392
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
393
    int padding = (int)(width*RubikActivity.PADDING);
394

    
395
    mMenuLayoutWidth = (int)(width/2);
396
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
397

    
398
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
399

    
400
    for(int i=0; i<NUM_BUTTONS; i++)
401
      {
402
      final int but = i;
403
      Button button = new Button(act);
404
      button.setLayoutParams(p);
405
      button.setText(BUTTON_LABELS[i]);
406
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
407

    
408
      button.setOnClickListener( new View.OnClickListener()
409
        {
410
        @Override
411
        public void onClick(View v)
412
          {
413
          mMenuPopup.dismiss();
414
          MenuAction(act,but);
415
          }
416
        });
417

    
418
      menuLayout.addView(button);
419
      }
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  private void setupPlayWindow(final RubikActivity act, final float width)
425
    {
426
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
427
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
428
    mPlayLayout = layout.findViewById(R.id.playGrid);
429

    
430
    mPlayLayoutWidth = (int)(width*0.4f);
431

    
432
    mPlayPopup = new PopupWindow(act);
433
    mPlayPopup.setContentView(layout);
434
    mPlayPopup.setFocusable(true);
435

    
436
    adjustLevels(act);
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  private void MenuAction(RubikActivity act, int button)
442
    {
443
    switch(button)
444
      {
445
      case 0: Bundle sBundle = new Bundle();
446
              int currObject = RubikObjectList.getCurrObject();
447
              sBundle.putInt("tab", currObject );
448
              sBundle.putBoolean("submitting", false);
449
              RubikDialogScores scores = new RubikDialogScores();
450
              scores.setArguments(sBundle);
451
              scores.show(act.getSupportFragmentManager(), null);
452
              break;
453
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
454
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
455
              break;
456
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
457
              break;
458
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
459
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
460
              break;
461
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
462
              aDiag.show(act.getSupportFragmentManager(), null);
463
              break;
464
      }
465
    }
466

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

    
469
  void setupSolveButton(final RubikActivity act)
470
    {
471
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve_new,R.drawable.ui_medium_cube_solve_new, R.drawable.ui_big_cube_solve_new, R.drawable.ui_huge_cube_solve_new);
472
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
473
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
474

    
475
    mSolveButton.setOnClickListener( new View.OnClickListener()
476
      {
477
      @Override
478
      public void onClick(View v)
479
        {
480
        act.getControl().solveObject();
481
        mMovesController.clearMoves(act);
482
        }
483
      });
484
    }
485

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

    
488
  private void setupScrambleButton(final RubikActivity act)
489
    {
490
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble_new,R.drawable.ui_medium_cube_scramble_new, R.drawable.ui_big_cube_scramble_new, R.drawable.ui_huge_cube_scramble_new);
491
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
492
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
493

    
494
    mScrambleButton.setOnClickListener( new View.OnClickListener()
495
      {
496
      @Override
497
      public void onClick(View v)
498
        {
499
        int currObject = RubikObjectList.getCurrObject();
500
        RubikObject object = RubikObjectList.getObject(currObject);
501
        int numScrambles = object==null ? 0 : object.getNumScramble();
502
        mShouldReactToEndOfScrambling = false;
503
        act.getControl().scrambleObject(numScrambles);
504
        }
505
      });
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
510
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
511
// going to be called)
512
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
513

    
514
  public void savePreferences(SharedPreferences.Editor editor)
515
    {
516
    editor.putInt("play_LevelValue", mLevelValue );
517

    
518
    if( mObjectPopup!=null )
519
      {
520
      mObjectPopup.dismiss();
521
      mObjectPopup = null;
522
      }
523

    
524
    if( mMenuPopup!=null )
525
      {
526
      mMenuPopup.dismiss();
527
      mMenuPopup = null;
528
      }
529

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

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

    
539
  public void restorePreferences(SharedPreferences preferences)
540
    {
541
    mLevelValue = preferences.getInt("play_LevelValue", 0);
542
    }
543

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

    
546
  public void setCurrObject(RubikActivity act)
547
    {
548
    if( mPlayLayout!=null ) adjustLevels(act);
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
  private void adjustSolvedIcons()
599
    {
600
    if( mPlayLayout!=null )
601
      {
602
      int currObject = RubikObjectList.getCurrObject();
603
      int dbLevel = RubikObjectList.getDBLevel(currObject);
604
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
605
      RubikScores scores = RubikScores.getInstance();
606

    
607
      for(int i=0; i<numLevel; i++)
608
        {
609
        int level = i<numLevel-1 ? i+1 : dbLevel;
610
        Button button = (Button)mPlayLayout.getChildAt(i);
611
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
612
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
613
        }
614
      }
615
    }
616

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

    
619
  private void adjustLevels(final RubikActivity act)
620
    {
621
    int currObject = RubikObjectList.getCurrObject();
622
    int dbLevel = RubikObjectList.getDBLevel(currObject);
623
    RubikObject object = RubikObjectList.getObject(currObject);
624
    int numScrambles = object==null ? 0 : object.getNumScramble();
625
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
626
    String[] levels = new String[numLevel];
627

    
628
    for(int i=0; i<numLevel-1; i++)
629
      {
630
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
631
      }
632

    
633
    if( numLevel>0 )
634
      {
635
      levels[numLevel-1] = act.getString(R.string.level_full);
636
      }
637

    
638
    if( mLevelValue>dbLevel || mLevelValue<1 ||
639
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
640
      {
641
      mLevelValue=1;
642
      }
643

    
644
    float width  = act.getScreenWidthInPixels();
645
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
646
    int padding  = (int)(width*RubikActivity.PADDING);
647
    int butWidth = mPlayLayoutWidth - 2*padding;
648
    int butHeight= (int)mMenuItemSize;
649
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
650

    
651
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
652
    pM.setMargins(margin, 0, margin, margin);
653
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
654
    pT.setMargins(margin, margin, margin, margin);
655
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
656
    pB.setMargins(margin, margin, margin, 2*margin);
657

    
658
    mPlayLayout.removeAllViews();
659

    
660
    RubikScores scores = RubikScores.getInstance();
661

    
662
    for(int i=0; i<numLevel; i++)
663
      {
664
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
665
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
666
      Button button = new Button(act);
667
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
668
      button.setText(levels[i]);
669
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
670

    
671
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
672
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
673

    
674
      button.setOnClickListener( new View.OnClickListener()
675
        {
676
        @Override
677
        public void onClick(View v)
678
          {
679
          ObjectControl control = act.getControl();
680

    
681
          if(control.isUINotBlocked())
682
            {
683
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
684
            mLevelValue = level;
685
            mShouldReactToEndOfScrambling = true;
686
            control.scrambleObject(scrambles);
687
            }
688
          }
689
        });
690

    
691
      mPlayLayout.addView(button);
692
      }
693
    }
694

    
695
///////////////////////////////////////////////////////////////////////////////////////////////////
696

    
697
  public int getLevel()
698
    {
699
    return mLevelValue;
700
    }
701

    
702
///////////////////////////////////////////////////////////////////////////////////////////////////
703

    
704
  public boolean shouldReactToEndOfScrambling()
705
    {
706
    return mShouldReactToEndOfScrambling;
707
    }
708

    
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710

    
711
  public void receiveUpdate(RubikUpdates updates)
712
    {
713
    Activity act = mWeakAct.get();
714

    
715
    if( act!=null )
716
      {
717
      act.runOnUiThread(new Runnable()
718
        {
719
        @Override
720
        public void run()
721
          {
722
          int num = updates.getCompletedNumber();
723

    
724
          if( num>0 )
725
            {
726
            String shownNum = String.valueOf(num);
727
            mBubbleUpdates.setText(shownNum);
728
            mBubbleUpdates.setVisibility(View.VISIBLE);
729
            int height = (int)(0.05f*mScreenWidth);
730
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
731
            }
732
         else
733
            {
734
            mBubbleUpdates.setVisibility(View.INVISIBLE);
735
            }
736
          }
737
        });
738
      }
739
    }
740

    
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742

    
743
  public void errorUpdate()
744
    {
745
    android.util.Log.e("D", "Screen: Error receiving update");
746
    }
747
  }
(5-5/10)