Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 598de3ee

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.main;
21

    
22
import android.content.Intent;
23
import android.content.SharedPreferences;
24
import android.os.Build;
25
import android.os.Bundle;
26
import android.os.LocaleList;
27
import android.preference.PreferenceManager;
28

    
29
import android.util.DisplayMetrics;
30
import android.view.DisplayCutout;
31
import android.view.View;
32
import android.view.ViewGroup;
33
import android.view.WindowManager;
34
import android.widget.LinearLayout;
35

    
36
import com.google.firebase.analytics.FirebaseAnalytics;
37

    
38
import org.distorted.dialogs.RubikDialogError;
39
import org.distorted.dialogs.RubikDialogPrivacy;
40
import org.distorted.effects.BaseEffect;
41
import org.distorted.helpers.BlockController;
42
import org.distorted.helpers.TwistyActivity;
43
import org.distorted.helpers.TwistyPreRender;
44
import org.distorted.library.main.DistortedLibrary;
45

    
46
import org.distorted.library.main.DistortedScreen;
47
import org.distorted.library.type.Static4D;
48
import org.distorted.objects.TwistyObject;
49
import org.distorted.network.RubikScores;
50
import org.distorted.network.RubikNetwork;
51
import org.distorted.objects.ObjectList;
52
import org.distorted.screens.ScreenList;
53
import org.distorted.screens.RubikScreenPlay;
54
import org.distorted.tutorials.TutorialActivity;
55

    
56
import java.util.Locale;
57

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

    
60
public class RubikActivity extends TwistyActivity
61
{
62
    public static final float PADDING             = 0.01f;
63
    public static final float MARGIN              = 0.004f;
64
    public static final float LARGE_MARGIN        = 0.025f;
65
    public static final float BUTTON_TEXT_SIZE    = 0.05f;
66
    public static final float TITLE_TEXT_SIZE     = 0.06f;
67
    public static final float SOLVER_BMP_H_SIZE   = 0.11f;
68
    public static final float SOLVER_BMP_V_SIZE   = 0.06f;
69
    public static final float MENU_ITEM_SIZE      = 0.11f;
70
    public static final float PATTERN_GROUP_TEXT  = 0.03f;
71
    public static final float PATTERN_CHILD_TEXT  = 0.02f;
72
    public static final float SCORES_LEVEL_TEXT   = 0.035f;
73
    public static final float SCORES_ITEM_TEXT    = 0.030f;
74
    public static final float TUTORIAL_ITEM_TEXT  = 0.100f;
75
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
76
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
77
    public static final float MENU_MED_TEXT_SIZE  = 0.04f;
78
    public static final float MENU_SMALL_TEXT_SIZE= 0.035f;
79

    
80
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
81
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
82
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
83
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
84
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
85

    
86
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
87
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
88

    
89
    private static final float RATIO_BAR  = 0.10f;
90
    private static final float RATIO_INSET= 0.08f;
91

    
92
    private boolean mJustStarted;
93
    private FirebaseAnalytics mFirebaseAnalytics;
94
    private static int mScreenWidth, mScreenHeight;
95
    private boolean mPolicyAccepted, mIsChinese;
96
    private int mCurrentApiVersion;
97
    private int mHeightUpperBar, mHeightLowerBar;
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    @Override
102
    protected void onCreate(Bundle savedState)
103
      {
104
      super.onCreate(savedState);
105
      DistortedLibrary.onCreate(0);
106

    
107
      setTheme(R.style.CustomActivityThemeNoActionBar);
108
      setContentView(R.layout.main);
109

    
110
      mJustStarted = true;
111
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
112

    
113
      DisplayMetrics displaymetrics = new DisplayMetrics();
114
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
115
      mScreenWidth =displaymetrics.widthPixels;
116
      mScreenHeight=displaymetrics.heightPixels;
117

    
118
      mIsChinese = localeIsChinese();
119
      unlock();
120

    
121
      hideNavigationBar();
122
      cutoutHack();
123
      computeBarHeights();
124
      // askForPermissions();
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
// this does not include possible insets
129

    
130
    private void computeBarHeights()
131
      {
132
      float height = getScreenHeightInPixels();
133
      int barHeight = (int)(height*RATIO_BAR);
134
      mHeightLowerBar = barHeight;
135
      mHeightUpperBar = barHeight;
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
    private void hideNavigationBar()
141
      {
142
      mCurrentApiVersion = Build.VERSION.SDK_INT;
143

    
144
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
145
        {
146
        final View decorView = getWindow().getDecorView();
147

    
148
        decorView.setSystemUiVisibility(FLAGS);
149

    
150
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
151
          {
152
          @Override
153
          public void onSystemUiVisibilityChange(int visibility)
154
            {
155
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
156
              {
157
              decorView.setSystemUiVisibility(FLAGS);
158
              }
159
            }
160
          });
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    @Override
167
    public void onAttachedToWindow()
168
      {
169
      super.onAttachedToWindow();
170

    
171
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
172
        {
173
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
174
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
175

    
176
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
177
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
178
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
179
        layoutHid.setLayoutParams(paramsHid);
180
        mHeightUpperBar += paramsHid.height;
181
        }
182

    
183
      float height = getScreenHeightInPixels();
184

    
185
      LinearLayout layoutTop = findViewById(R.id.upperBar);
186
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
187

    
188
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
189
      paramsTop.height = (int)(height*RATIO_BAR);
190
      layoutTop.setLayoutParams(paramsTop);
191
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
192
      paramsBot.height = (int)(height*RATIO_BAR);
193
      layoutBot.setLayoutParams(paramsBot);
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// do not avoid cutouts
198

    
199
    private void cutoutHack()
200
      {
201
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
202
        {
203
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
204
        }
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
    @Override
210
    public void onWindowFocusChanged(boolean hasFocus)
211
      {
212
      super.onWindowFocusChanged(hasFocus);
213

    
214
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
215
        {
216
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
217
        }
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
    private void askForPermissions()
223
      {
224
      final int REQUEST_EXTERNAL_STORAGE = 1;
225

    
226
      String[] PERMISSIONS_STORAGE =
227
        {
228
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
229
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
230
        };
231

    
232
      int permission = androidx.core.app.ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
233

    
234
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
235
        {
236
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
237
        }
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
    
242
    @Override
243
    protected void onPause() 
244
      {
245
      super.onPause();
246
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
247
      view.onPause();
248
      DistortedLibrary.onPause(0);
249
      BlockController.onPause();
250
      RubikNetwork.onPause();
251
      savePreferences();
252
      }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
    
256
    @Override
257
    protected void onResume() 
258
      {
259
      super.onResume();
260
      DistortedLibrary.onResume(0);
261
      BlockController.onResume();
262
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
263
      view.onResume();
264
      view.initialize();
265
      restorePreferences();
266
      ScreenList.setScreen(this);
267
      unblockEverything();
268

    
269
      if( mJustStarted )
270
        {
271
        mJustStarted = false;
272
        RubikScores scores = RubikScores.getInstance();
273
        scores.incrementNumRuns();
274
        scores.setCountry(this);
275
        }
276

    
277
      boolean success = false;
278
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
279
      int object = play.getObject();
280
      int size   = play.getSize();
281

    
282
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
283
        {
284
        ObjectList obj = ObjectList.getObject(object);
285
        int[] sizes = obj.getSizes();
286
        int sizeIndex = ObjectList.getSizeIndex(object,size);
287

    
288
        if( sizeIndex>=0 && sizeIndex<sizes.length )
289
          {
290
          success = true;
291
          view.getPreRender().changeObject(obj,size);
292
          }
293
        }
294

    
295
      if( !success )
296
        {
297
        ObjectList obj = ObjectList.getObject(RubikScreenPlay.DEF_OBJECT);
298
        int s = RubikScreenPlay.DEF_SIZE;
299

    
300
        play.setObjectAndSize(this,obj,s);
301
        view.getPreRender().changeObject(obj,s);
302
        }
303

    
304
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
305
      }
306
    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308
    
309
    @Override
310
    protected void onDestroy() 
311
      {
312
      DistortedLibrary.onDestroy(0);
313
      super.onDestroy();
314
      }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
    private void savePreferences()
319
      {
320
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
321
      SharedPreferences.Editor editor = preferences.edit();
322

    
323
      editor.putBoolean("policyAccepted", mPolicyAccepted);
324

    
325
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
326
        {
327
        BaseEffect.Type.getType(i).savePreferences(editor);
328
        }
329

    
330
      for (int i = 0; i< ScreenList.LENGTH; i++)
331
        {
332
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
333
        }
334

    
335
      ScreenList.savePreferences(editor);
336
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
337
      view.getPreRender().savePreferences(editor);
338

    
339
      editor.apply();
340
      }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
    private void restorePreferences()
345
      {
346
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
347

    
348
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
349

    
350
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
351
        {
352
        BaseEffect.Type.getType(i).restorePreferences(preferences);
353
        }
354

    
355
      for (int i = 0; i< ScreenList.LENGTH; i++)
356
        {
357
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
358
        }
359

    
360
      ScreenList.restorePreferences(preferences);
361

    
362
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
363
      view.getPreRender().restorePreferences(preferences);
364

    
365
      RubikScores scores = RubikScores.getInstance();
366

    
367
      if( scores.isVerified() )
368
        {
369
        FirebaseAnalytics analytics = getAnalytics();
370
        analytics.setUserId(scores.getName());
371
        }
372
      }
373

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

    
376
    private void PrivacyPolicy()
377
      {
378
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
379
      priDiag.show(getSupportFragmentManager(), null);
380
      }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
    void OpenGLError()
385
      {
386
      RubikDialogError errDiag = new RubikDialogError();
387
      errDiag.show(getSupportFragmentManager(), null);
388
      }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
// PUBLIC API
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
    public FirebaseAnalytics getAnalytics()
395
      {
396
      return mFirebaseAnalytics;
397
      }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
    public int getHeightUpperBar()
402
      {
403
      return mHeightUpperBar;
404
      }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
    public int getHeightLowerBar()
409
      {
410
      return mHeightLowerBar;
411
      }
412

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

    
415
    public TwistyObject getObject()
416
      {
417
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
418
      RubikPreRender pre = view.getPreRender();
419
      return pre.getObject();
420
      }
421

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

    
424
    public DistortedScreen getScreen()
425
      {
426
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
427
      RubikRenderer renderer = view.getRenderer();
428
      return renderer.getScreen();
429
      }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
    public RubikPreRender getPreRender()
434
      {
435
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
436
      return view.getPreRender();
437
      }
438

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

    
441
    public TwistyPreRender getTwistyPreRender()
442
      {
443
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
444
      return view.getPreRender();
445
      }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
    public RubikSurfaceView getSurfaceView()
450
      {
451
      return findViewById(R.id.rubikSurfaceView);
452
      }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
    public int getScreenWidthInPixels()
457
      {
458
      return mScreenWidth;
459
      }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
    public int getScreenHeightInPixels()
464
      {
465
      return mScreenHeight;
466
      }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
471
      {
472
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
473
      RubikPreRender pre = view.getPreRender();
474

    
475
      if( reportChange )
476
        {
477
        TwistyObject oldObject = pre.getObject();
478

    
479
        if( oldObject!=null )
480
          {
481
          ObjectList oldList = oldObject.getObjectList();
482
          int oldNum = oldObject.getNumLayers();
483
          float fps = view.getRenderer().getFPS();
484
          fps = (int)(fps+0.5f);
485
          StringBuilder name = new StringBuilder();
486
          name.append(oldList.name());
487
          name.append('_');
488
          name.append(oldNum);
489
          name.append(' ');
490
          name.append(fps);
491
          name.append(" --> ");
492
          name.append(newObject.name());
493
          name.append('_');
494
          name.append(newSize);
495

    
496
          if( BuildConfig.DEBUG )
497
            {
498
            android.util.Log.e("rubik", name.toString());
499
            }
500
          else
501
            {
502
            FirebaseAnalytics analytics = getAnalytics();
503

    
504
            if( analytics!=null )
505
              {
506
              Bundle bundle = new Bundle();
507
              bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
508
              analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
509
              }
510
            }
511
          }
512
        }
513

    
514
      pre.changeObject(newObject,newSize);
515
      }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
    public void setupObject(ObjectList object, int size, int[][] moves)
520
      {
521
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
522
      RubikPreRender pre = view.getPreRender();
523
      pre.setupObject(object,size,moves);
524
      }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
    public static int getDrawableSize()
529
      {
530
      if( mScreenHeight<1000 )
531
        {
532
        return 0;
533
        }
534
      if( mScreenHeight<1600 )
535
        {
536
        return 1;
537
        }
538
      if( mScreenHeight<1900 )
539
        {
540
        return 2;
541
        }
542

    
543
      return 3;
544
      }
545

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547

    
548
    public static int getDrawable(int small, int medium, int big, int huge)
549
      {
550
      int size = getDrawableSize();
551

    
552
      switch(size)
553
        {
554
        case 0 : return small;
555
        case 1 : return medium;
556
        case 2 : return big;
557
        default: return huge;
558
        }
559
      }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
    public void setControlState(boolean on)
564
      {
565
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
566
      RubikRenderer renderer = view.getRenderer();
567
      renderer.setControlState(on);
568
      }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

    
572
    public void acceptPrivacy()
573
      {
574
      mPolicyAccepted = true;
575
      }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
    public void declinePrivacy()
580
      {
581
      finish();
582
      }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

    
586
    public static boolean localeIsChinese()
587
      {
588
      String language;
589

    
590
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
591
        {
592
        language= LocaleList.getDefault().get(0).getLanguage();
593
        }
594
      else
595
        {
596
        language= Locale.getDefault().getLanguage();
597
        }
598

    
599
      return language.equals("zh");
600
      }
601

    
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603

    
604
    public boolean isVertical()
605
      {
606
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
607
      return view.isVertical();
608
      }
609

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

    
612
    public void blockEverything(int place)
613
      {
614
      setLock();
615

    
616
      TwistyPreRender pre = getPreRender();
617
      pre.blockEverything(place);
618

    
619
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
620
      play.setLockState(this);
621
      }
622

    
623
///////////////////////////////////////////////////////////////////////////////////////////////////
624

    
625
    public void unblockEverything()
626
      {
627
      unsetLock();
628

    
629
      TwistyPreRender pre = getPreRender();
630
      pre.unblockEverything();
631

    
632
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
633
      play.setLockState(this);
634
      }
635

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637

    
638
    public Static4D getCurrQuat()
639
      {
640
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
641
      return view.getQuat();
642
      }
643

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645

    
646
    public void switchTutorial(String url, ObjectList object, int size)
647
      {
648
      Intent myIntent = new Intent(this, TutorialActivity.class);
649
      myIntent.putExtra("url", url);
650
      myIntent.putExtra("obj", object.ordinal());
651
      myIntent.putExtra("siz", size);
652
      startActivity(myIntent);
653
      }
654
}
(1-1/4)