Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 10373dc7

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

    
40
import org.distorted.network.RubikNetwork;
41
import org.distorted.network.RubikUpdates;
42
import org.distorted.objectlib.main.ObjectControl;
43

    
44
import org.distorted.main.R;
45
import org.distorted.main.RubikActivity;
46
import org.distorted.dialogs.RubikDialogAbout;
47
import org.distorted.dialogs.RubikDialogPattern;
48
import org.distorted.dialogs.RubikDialogScores;
49
import org.distorted.dialogs.RubikDialogTutorial;
50
import org.distorted.helpers.TransparentButton;
51
import org.distorted.helpers.TransparentImageButton;
52
import org.distorted.network.RubikScores;
53
import org.distorted.objects.RubikObject;
54
import org.distorted.objects.RubikObjectList;
55

    
56
import static android.view.View.inflate;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
61
  {
62
  public static final int NUM_COLUMNS  = 5;
63
  public static final int LEVELS_SHOWN = 10;
64

    
65
  private static final int[] BUTTON_LABELS = { R.string.scores,
66
                                               R.string.patterns,
67
                                               R.string.solver,
68
                                               R.string.tutorials,
69
                                               R.string.about };
70

    
71
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
72
  private static final float LAST_BUTTON = 1.5f;
73
  private static final int[] mLocation = new int[2];
74

    
75
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
76
  private TransparentButton mPlayButton;
77
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
78
  private LinearLayout mPlayLayout;
79
  private TextView mBubbleUpdates;
80
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
81
  private int mLevelValue;
82
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
83
  private int mColCount, mRowCount, mMaxRowCount;
84
  private int mUpperBarHeight;
85
  private boolean mShouldReactToEndOfScrambling;
86
  private int mBottomHeight;
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  void leaveScreen(RubikActivity act)
91
    {
92

    
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  void enterScreen(final RubikActivity act)
98
    {
99
    int numObjects = RubikObjectList.getNumObjects();
100
    float width = act.getScreenWidthInPixels();
101
    mUpperBarHeight = act.getHeightUpperBar();
102

    
103
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
104
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
105
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
106

    
107
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
108
    mColCount = NUM_COLUMNS;
109

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

    
114
    setupObjectButton(act,width);
115
    layoutTop.addView(mObjButton);
116

    
117
    setupMenuButton(act,width);
118
    layoutTop.addView(mMenuButton);
119

    
120
    setupPlayButton(act,width);
121
    layoutTop.addView(mPlayButton);
122

    
123
    setupSolveButton(act);
124
    setupScrambleButton(act);
125
    createBottomPane(act,mSolveButton,mScrambleButton);
126
    }
127

    
128
//////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  private void setupObjectButton(final RubikActivity act, final float width)
131
    {
132
    final int margin  = (int)(width*RubikActivity.MARGIN);
133
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
134
    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);
135
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
136
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
137

    
138
    mObjButton.setOnClickListener( new View.OnClickListener()
139
      {
140
      @Override
141
      public void onClick(View view)
142
        {
143
        if( mObjectPopup==null )
144
          {
145
          float width = act.getScreenWidthInPixels();
146
          float height= act.getScreenHeightInPixels();
147
          setupObjectWindow(act,width,height);
148
          }
149

    
150
        if( act.getControl().isUINotBlocked())
151
          {
152
          int rowCount = Math.min(mMaxRowCount,mRowCount);
153
          View popupView = mObjectPopup.getContentView();
154
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
155
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
156
          }
157
        }
158
      });
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void setupPlayButton(final RubikActivity act, final float width)
164
    {
165
    final int margin = (int)(width*RubikActivity.MARGIN);
166

    
167
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
168

    
169
    mPlayButton.setOnClickListener( new View.OnClickListener()
170
      {
171
      @Override
172
      public void onClick(View view)
173
        {
174
         if( mPlayPopup==null )
175
          {
176
          float width = act.getScreenWidthInPixels();
177
          setupPlayWindow(act,width);
178
          }
179

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private void setupMenuButton(final RubikActivity act, final float width)
201
    {
202
    final int margin = (int)(width*RubikActivity.MARGIN);
203
    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);
204
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
205
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
206

    
207
    mMenuButton.setOnClickListener( new View.OnClickListener()
208
      {
209
      @Override
210
      public void onClick(View view)
211
        {
212
        if( mMenuPopup==null )
213
          {
214
          float width = act.getScreenWidthInPixels();
215
          setupMenuWindow(act,width);
216
          }
217

    
218
        if( act.getControl().isUINotBlocked())
219
          {
220
          View popupView = mMenuPopup.getContentView();
221
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
222
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
223
          }
224
        }
225
      });
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
231
    {
232
    int cubeWidth = (int)(width/9);
233
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
234
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
235
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
236

    
237
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
238
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
239

    
240
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
241
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
242

    
243
    objectGrid.setColumnCount(mColCount);
244
    objectGrid.setRowCount(mRowCount);
245

    
246
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
247
    setupBottomLayout(act,bottomLayout);
248

    
249
    mObjectPopup = new PopupWindow(act);
250
    mObjectPopup.setFocusable(true);
251
    mObjectPopup.setContentView(view);
252

    
253
    int[] nextInRow = new int[mRowCount];
254

    
255
    for(int row=0; row<mRowCount; row++)
256
      {
257
      rowSpecs[row] = GridLayout.spec(row);
258
      nextInRow[row]= 0;
259
      }
260
    for(int col=0; col<mColCount; col++)
261
      {
262
      colSpecs[col] = GridLayout.spec(col);
263
      }
264

    
265
    int numObjects = RubikObjectList.getNumObjects();
266

    
267
    for(int object=0; object<numObjects; object++)
268
      {
269
      final RubikObject robject = RubikObjectList.getObject(object);
270
      int icons = robject==null ? 0 : robject.getIconID();
271
      int row = object/NUM_COLUMNS;
272
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
273

    
274
      ImageButton button = new ImageButton(act);
275
      button.setBackgroundResource(icons);
276
      button.setOnClickListener( new View.OnClickListener()
277
        {
278
        @Override
279
        public void onClick(View v)
280
          {
281
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
282
            {
283
            RubikObjectList.setCurrObject(act,ordinal);
284
            act.changeObject(ordinal,true);
285
            if( mPlayLayout!=null ) adjustLevels(act);
286
            mMovesController.clearMoves(act);
287
            }
288

    
289
          mObjectPopup.dismiss();
290
          }
291
        });
292

    
293
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
294
      params.bottomMargin = margin;
295
      params.topMargin    = margin;
296
      params.leftMargin   = margin;
297
      params.rightMargin  = margin;
298

    
299
      params.width = cubeWidth;
300
      params.height= cubeWidth;
301

    
302
      nextInRow[row]++;
303

    
304
      objectGrid.addView(button, params);
305
      }
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
311
    {
312
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
313
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
314
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
315

    
316
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
317
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
318
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
319

    
320
    buttonTut.setImageResource(iconT);
321
    buttonDow.setImageResource(iconD);
322
    buttonInf.setImageResource(iconI);
323

    
324
    Resources res = act.getResources();
325
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
326
    mBottomHeight = bd.getIntrinsicHeight();
327

    
328
    TypedValue outValue = new TypedValue();
329
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
330
    buttonTut.setBackgroundResource(outValue.resourceId);
331
    buttonDow.setBackgroundResource(outValue.resourceId);
332
    buttonInf.setBackgroundResource(outValue.resourceId);
333

    
334
    buttonTut.setOnClickListener( new View.OnClickListener()
335
      {
336
      @Override
337
      public void onClick(View v)
338
        {
339
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
340
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
341
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
342
        }
343
      });
344

    
345
    buttonDow.setOnClickListener( new View.OnClickListener()
346
      {
347
      @Override
348
      public void onClick(View v)
349
        {
350
        android.util.Log.e("D", "Download!!");
351
        }
352
      });
353

    
354
    buttonInf.setOnClickListener( new View.OnClickListener()
355
      {
356
      @Override
357
      public void onClick(View v)
358
        {
359
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
360
        int currObject = RubikObjectList.getCurrObject();
361
        act.switchConfig(currObject);
362
        }
363
      });
364

    
365
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
366
    mBubbleUpdates.setVisibility(View.INVISIBLE);
367

    
368
    RubikNetwork network = RubikNetwork.getInstance();
369
    network.signUpForUpdates(this);
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
  private void setupMenuWindow(final RubikActivity act, final float width)
375
    {
376
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
377
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
378
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
379

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

    
386
    mMenuLayoutWidth = (int)(width/2);
387
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
388

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

    
391
    for(int i=0; i<NUM_BUTTONS; i++)
392
      {
393
      final int but = i;
394
      Button button = new Button(act);
395
      button.setLayoutParams(p);
396
      button.setText(BUTTON_LABELS[i]);
397
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
398

    
399
      button.setOnClickListener( new View.OnClickListener()
400
        {
401
        @Override
402
        public void onClick(View v)
403
          {
404
          mMenuPopup.dismiss();
405
          MenuAction(act,but);
406
          }
407
        });
408

    
409
      menuLayout.addView(button);
410
      }
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  private void setupPlayWindow(final RubikActivity act, final float width)
416
    {
417
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
418
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
419
    mPlayLayout = layout.findViewById(R.id.playGrid);
420

    
421
    mPlayLayoutWidth = (int)(width*0.4f);
422

    
423
    mPlayPopup = new PopupWindow(act);
424
    mPlayPopup.setContentView(layout);
425
    mPlayPopup.setFocusable(true);
426

    
427
    adjustLevels(act);
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  private void MenuAction(RubikActivity act, int button)
433
    {
434
    switch(button)
435
      {
436
      case 0: Bundle sBundle = new Bundle();
437
              int currObject = RubikObjectList.getCurrObject();
438
              sBundle.putInt("tab", currObject );
439
              sBundle.putBoolean("submitting", false);
440
              RubikDialogScores scores = new RubikDialogScores();
441
              scores.setArguments(sBundle);
442
              scores.show(act.getSupportFragmentManager(), null);
443
              break;
444
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
445
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
446
              break;
447
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
448
              break;
449
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
450
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
451
              break;
452
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
453
              aDiag.show(act.getSupportFragmentManager(), null);
454
              break;
455
      }
456
    }
457

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

    
460
  void setupSolveButton(final RubikActivity act)
461
    {
462
    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);
463
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
464
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
465

    
466
    mSolveButton.setOnClickListener( new View.OnClickListener()
467
      {
468
      @Override
469
      public void onClick(View v)
470
        {
471
        act.getControl().solveObject();
472
        mMovesController.clearMoves(act);
473
        }
474
      });
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

    
479
  private void setupScrambleButton(final RubikActivity act)
480
    {
481
    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);
482
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
483
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
484

    
485
    mScrambleButton.setOnClickListener( new View.OnClickListener()
486
      {
487
      @Override
488
      public void onClick(View v)
489
        {
490
        int currObject = RubikObjectList.getCurrObject();
491
        RubikObject object = RubikObjectList.getObject(currObject);
492
        int numScrambles = object==null ? 0 : object.getNumScramble();
493
        mShouldReactToEndOfScrambling = false;
494
        act.getControl().scrambleObject(numScrambles);
495
        }
496
      });
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
501
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
502
// going to be called)
503
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
504

    
505
  public void savePreferences(SharedPreferences.Editor editor)
506
    {
507
    editor.putInt("play_LevelValue", mLevelValue );
508

    
509
    if( mObjectPopup!=null )
510
      {
511
      mObjectPopup.dismiss();
512
      mObjectPopup = null;
513
      }
514

    
515
    if( mMenuPopup!=null )
516
      {
517
      mMenuPopup.dismiss();
518
      mMenuPopup = null;
519
      }
520

    
521
    if( mPlayPopup!=null )
522
      {
523
      mPlayPopup.dismiss();
524
      mPlayPopup = null;
525
      }
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  public void restorePreferences(SharedPreferences preferences)
531
    {
532
    mLevelValue = preferences.getInt("play_LevelValue", 0);
533
    }
534

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
  public void setCurrObject(RubikActivity act)
538
    {
539
    if( mPlayLayout!=null ) adjustLevels(act);
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
544

    
545
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
546
    {
547
    View topLayout = act.findViewById(R.id.relativeLayout);
548
    boolean isFullScreen;
549

    
550
    if( topLayout!=null )
551
      {
552
      topLayout.getLocationOnScreen(mLocation);
553
      isFullScreen = (mLocation[1]==0);
554
      }
555
    else
556
      {
557
      isFullScreen = true;
558
      }
559

    
560
    try
561
      {
562
      // if on Android 11 or we are fullscreen
563
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
564
        {
565
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
566
        window.update(view, w, h);
567
        }
568
      else  // Android 10 or below in pop-up mode or split-screen mode
569
        {
570
        view.getLocationOnScreen(mLocation);
571
        int width  = view.getWidth();
572
        int height = view.getHeight();
573
        int x = mLocation[0]+(width-w)/2;
574
        int y = mLocation[1]+height+yoff;
575

    
576
        window.showAsDropDown(view);
577
        window.update(x,y,w,h);
578
        }
579
      }
580
    catch( IllegalArgumentException iae )
581
      {
582
      // ignore, this means window is 'not attached to window manager' -
583
      // which most probably is because we are already exiting the app.
584
      }
585
    }
586

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588

    
589
  private void adjustSolvedIcons()
590
    {
591
    if( mPlayLayout!=null )
592
      {
593
      int currObject = RubikObjectList.getCurrObject();
594
      int dbLevel = RubikObjectList.getDBLevel(currObject);
595
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
596
      RubikScores scores = RubikScores.getInstance();
597

    
598
      for(int i=0; i<numLevel; i++)
599
        {
600
        int level = i<numLevel-1 ? i+1 : dbLevel;
601
        Button button = (Button)mPlayLayout.getChildAt(i);
602
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
603
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
604
        }
605
      }
606
    }
607

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

    
610
  private void adjustLevels(final RubikActivity act)
611
    {
612
    int currObject = RubikObjectList.getCurrObject();
613
    int dbLevel = RubikObjectList.getDBLevel(currObject);
614
    RubikObject object = RubikObjectList.getObject(currObject);
615
    int numScrambles = object==null ? 0 : object.getNumScramble();
616
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
617
    String[] levels = new String[numLevel];
618

    
619
    for(int i=0; i<numLevel-1; i++)
620
      {
621
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
622
      }
623

    
624
    if( numLevel>0 )
625
      {
626
      levels[numLevel-1] = act.getString(R.string.level_full);
627
      }
628

    
629
    if( mLevelValue>dbLevel || mLevelValue<1 ||
630
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
631
      {
632
      mLevelValue=1;
633
      }
634

    
635
    float width  = act.getScreenWidthInPixels();
636
    int margin   = (int)(width*RubikActivity.MARGIN);
637
    int padding  = (int)(width*RubikActivity.PADDING);
638
    int butWidth = mPlayLayoutWidth - 2*padding;
639
    int butHeight= (int)mMenuItemSize;
640
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
641

    
642
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
643
    pM.setMargins(margin, 0, margin, margin);
644
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
645
    pT.setMargins(margin, margin, margin, margin);
646
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
647
    pB.setMargins(margin, margin, margin, 2*margin);
648

    
649
    mPlayLayout.removeAllViews();
650

    
651
    RubikScores scores = RubikScores.getInstance();
652

    
653
    for(int i=0; i<numLevel; i++)
654
      {
655
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
656
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
657
      Button button = new Button(act);
658
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
659
      button.setText(levels[i]);
660
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
661

    
662
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
663
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
664

    
665
      button.setOnClickListener( new View.OnClickListener()
666
        {
667
        @Override
668
        public void onClick(View v)
669
          {
670
          ObjectControl control = act.getControl();
671

    
672
          if(control.isUINotBlocked())
673
            {
674
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
675
            mLevelValue = level;
676
            mShouldReactToEndOfScrambling = true;
677
            control.scrambleObject(scrambles);
678
            }
679
          }
680
        });
681

    
682
      mPlayLayout.addView(button);
683
      }
684
    }
685

    
686
///////////////////////////////////////////////////////////////////////////////////////////////////
687

    
688
  public int getLevel()
689
    {
690
    return mLevelValue;
691
    }
692

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694

    
695
  public boolean shouldReactToEndOfScrambling()
696
    {
697
    return mShouldReactToEndOfScrambling;
698
    }
699

    
700
///////////////////////////////////////////////////////////////////////////////////////////////////
701

    
702
  public void receiveUpdate(RubikUpdates updates)
703
    {
704
    int num = updates.getNumberOfUpdates();
705

    
706
    if( num>0 )
707
      {
708
      String shownNum = String.valueOf(num);
709
      mBubbleUpdates.setText(shownNum);
710
      mBubbleUpdates.setVisibility(View.VISIBLE);
711
      }
712
    }
713

    
714
///////////////////////////////////////////////////////////////////////////////////////////////////
715

    
716
  public void errorUpdate()
717
    {
718
    android.util.Log.e("D", "Error receiving update");
719
    }
720
  }
(5-5/10)