Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 3f7a4363

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.library.main.DistortedLibrary;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.type.Static4D;
41

    
42
import org.distorted.objectlib.main.TwistyObject;
43
import org.distorted.objectlib.main.ObjectList;
44

    
45
import org.distorted.dialogs.RubikDialogError;
46
import org.distorted.dialogs.RubikDialogPrivacy;
47
import org.distorted.effects.BaseEffect;
48
import org.distorted.helpers.BlockController;
49
import org.distorted.helpers.TwistyActivity;
50
import org.distorted.helpers.TwistyPreRender;
51
import org.distorted.network.RubikScores;
52
import org.distorted.network.RubikNetwork;
53
import org.distorted.screens.ScreenList;
54
import org.distorted.screens.RubikScreenPlay;
55
import org.distorted.tutorials.TutorialActivity;
56

    
57
import java.util.Locale;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

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

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

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

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

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

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

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

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

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

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

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

    
138
      LinearLayout layoutTop = findViewById(R.id.upperBar);
139
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
140

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

    
159
        decorView.setSystemUiVisibility(FLAGS);
160

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

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

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

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
// do not avoid cutouts
197

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

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

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
338
      editor.apply();
339
      }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

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

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

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

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

    
359
      ScreenList.restorePreferences(preferences);
360

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

    
364
      RubikScores scores = RubikScores.getInstance();
365

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

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

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

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

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

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
// PUBLIC API
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

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

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

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

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

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

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

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

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

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

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

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

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

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

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

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

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

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

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

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

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

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

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

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

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

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

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

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

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

    
542
      return 3;
543
      }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

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

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

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561

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

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

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

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577

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

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

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

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

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

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

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

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610

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

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

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

    
622
///////////////////////////////////////////////////////////////////////////////////////////////////
623

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

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

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

    
635
///////////////////////////////////////////////////////////////////////////////////////////////////
636

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

    
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644

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