Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 214e301a

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
      LinearLayout layoutTop = findViewById(R.id.upperBar);
138
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
139

    
140
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
141
      paramsTop.height = mHeightUpperBar;
142
      layoutTop.setLayoutParams(paramsTop);
143
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
144
      paramsBot.height = mHeightLowerBar;
145
      layoutBot.setLayoutParams(paramsBot);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    private void hideNavigationBar()
151
      {
152
      mCurrentApiVersion = Build.VERSION.SDK_INT;
153

    
154
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
155
        {
156
        final View decorView = getWindow().getDecorView();
157

    
158
        decorView.setSystemUiVisibility(FLAGS);
159

    
160
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
161
          {
162
          @Override
163
          public void onSystemUiVisibilityChange(int visibility)
164
            {
165
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
166
              {
167
              decorView.setSystemUiVisibility(FLAGS);
168
              }
169
            }
170
          });
171
        }
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    @Override
177
    public void onAttachedToWindow()
178
      {
179
      super.onAttachedToWindow();
180

    
181
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
182
        {
183
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
184
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
185

    
186
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
187
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
188
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
189
        layoutHid.setLayoutParams(paramsHid);
190
        mHeightUpperBar += paramsHid.height;
191
        }
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
// do not avoid cutouts
196

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

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

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

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    private void askForPermissions()
221
      {
222
      final int REQUEST_EXTERNAL_STORAGE = 1;
223

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

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

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

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

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

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

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

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

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

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

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

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

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

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

    
321
      editor.putBoolean("policyAccepted", mPolicyAccepted);
322

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

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

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

    
337
      editor.apply();
338
      }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

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

    
346
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
347

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

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

    
358
      ScreenList.restorePreferences(preferences);
359

    
360
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
361
      view.getPreRender().restorePreferences(preferences);
362

    
363
      RubikScores scores = RubikScores.getInstance();
364

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

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

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

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

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

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
// PUBLIC API
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
    public FirebaseAnalytics getAnalytics()
393
      {
394
      return mFirebaseAnalytics;
395
      }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
    public int getHeightUpperBar()
400
      {
401
      return mHeightUpperBar;
402
      }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
    public int getHeightLowerBar()
407
      {
408
      return mHeightLowerBar;
409
      }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

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

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

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

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

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

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

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

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

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

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
    public int getScreenWidthInPixels()
455
      {
456
      return mScreenWidth;
457
      }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
    public int getScreenHeightInPixels()
462
      {
463
      return mScreenHeight;
464
      }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

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

    
473
      if( reportChange )
474
        {
475
        TwistyObject oldObject = pre.getObject();
476

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

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

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

    
512
      pre.changeObject(newObject,newSize);
513
      }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

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

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

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

    
541
      return 3;
542
      }
543

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

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

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

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

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

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

    
570
    public void acceptPrivacy()
571
      {
572
      mPolicyAccepted = true;
573
      }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

    
577
    public void declinePrivacy()
578
      {
579
      finish();
580
      }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
    public static boolean localeIsChinese()
585
      {
586
      String language;
587

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

    
597
      return language.equals("zh");
598
      }
599

    
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601

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

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

    
610
    public void blockEverything(int place)
611
      {
612
      setLock();
613

    
614
      TwistyPreRender pre = getPreRender();
615
      pre.blockEverything(place);
616

    
617
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
618
      play.setLockState(this);
619
      }
620

    
621
///////////////////////////////////////////////////////////////////////////////////////////////////
622

    
623
    public void unblockEverything()
624
      {
625
      unsetLock();
626

    
627
      TwistyPreRender pre = getPreRender();
628
      pre.unblockEverything();
629

    
630
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
631
      play.setLockState(this);
632
      }
633

    
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635

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

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643

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