Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 7aa4c349

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 boolean mJustStarted;
90
    private FirebaseAnalytics mFirebaseAnalytics;
91
    private static int mScreenWidth, mScreenHeight;
92
    private boolean mPolicyAccepted, mIsChinese;
93
    private int mCurrentApiVersion;
94

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

    
97
    @Override
98
    protected void onCreate(Bundle savedState)
99
      {
100
      super.onCreate(savedState);
101
      DistortedLibrary.onCreate(0);
102

    
103
      setTheme(R.style.CustomActivityThemeNoActionBar);
104
      setContentView(R.layout.main);
105

    
106
      mJustStarted = true;
107
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
108

    
109
      DisplayMetrics displaymetrics = new DisplayMetrics();
110
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
111
      mScreenWidth =displaymetrics.widthPixels;
112
      mScreenHeight=displaymetrics.heightPixels;
113

    
114
      mIsChinese = localeIsChinese();
115
      unlock();
116

    
117
      hideNavigationBar();
118
      cutoutHack();
119
      // askForPermissions();
120
      }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
    private void hideNavigationBar()
125
      {
126
      mCurrentApiVersion = Build.VERSION.SDK_INT;
127

    
128
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
129
        {
130
        final View decorView = getWindow().getDecorView();
131

    
132
        decorView.setSystemUiVisibility(FLAGS);
133

    
134
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
135
          {
136
          @Override
137
          public void onSystemUiVisibilityChange(int visibility)
138
            {
139
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
140
              {
141
              decorView.setSystemUiVisibility(FLAGS);
142
              }
143
            }
144
          });
145
        }
146
      }
147

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

    
150
    @Override
151
    public void onAttachedToWindow()
152
      {
153
      super.onAttachedToWindow();
154

    
155
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
156
        {
157
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
158
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
159

    
160
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
161
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
162
        paramsHid.height = (int)(0.8f*insetHeight);
163
        layoutHid.setLayoutParams(paramsHid);
164
        }
165

    
166
      final float RATIO = 0.10f;
167
      float height = getScreenHeightInPixels();
168

    
169
      LinearLayout layoutTop = findViewById(R.id.upperBar);
170
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
171

    
172
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
173
      paramsTop.height = (int)(height*RATIO);
174
      layoutTop.setLayoutParams(paramsTop);
175
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
176
      paramsBot.height = (int)(height*RATIO);
177
      layoutBot.setLayoutParams(paramsBot);
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
// do not avoid cutouts
182

    
183
    private void cutoutHack()
184
      {
185
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
186
        {
187
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
188
        }
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    @Override
194
    public void onWindowFocusChanged(boolean hasFocus)
195
      {
196
      super.onWindowFocusChanged(hasFocus);
197

    
198
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
199
        {
200
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
201
        }
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    private void askForPermissions()
207
      {
208
      final int REQUEST_EXTERNAL_STORAGE = 1;
209

    
210
      String[] PERMISSIONS_STORAGE =
211
        {
212
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
213
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
214
        };
215

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

    
218
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
219
        {
220
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
221
        }
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
    
226
    @Override
227
    protected void onPause() 
228
      {
229
      super.onPause();
230
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
231
      view.onPause();
232
      DistortedLibrary.onPause(0);
233
      BlockController.onPause();
234
      RubikNetwork.onPause();
235
      savePreferences();
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
    
240
    @Override
241
    protected void onResume() 
242
      {
243
      super.onResume();
244
      DistortedLibrary.onResume(0);
245
      BlockController.onResume();
246
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
247
      view.onResume();
248
      view.initialize();
249
      restorePreferences();
250
      ScreenList.setScreen(this);
251
      unblockEverything();
252

    
253
      if( mJustStarted )
254
        {
255
        mJustStarted = false;
256
        RubikScores scores = RubikScores.getInstance();
257
        scores.incrementNumRuns();
258
        scores.setCountry(this);
259
        }
260

    
261
      boolean success = false;
262
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
263
      int object = play.getObject();
264
      int size   = play.getSize();
265

    
266
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
267
        {
268
        ObjectList obj = ObjectList.getObject(object);
269
        int[] sizes = obj.getSizes();
270
        int sizeIndex = ObjectList.getSizeIndex(object,size);
271

    
272
        if( sizeIndex>=0 && sizeIndex<sizes.length )
273
          {
274
          success = true;
275
          view.getPreRender().changeObject(obj,size);
276
          }
277
        }
278

    
279
      if( !success )
280
        {
281
        ObjectList obj = ObjectList.getObject(RubikScreenPlay.DEF_OBJECT);
282
        int s = RubikScreenPlay.DEF_SIZE;
283

    
284
        play.setObjectAndSize(this,obj,s);
285
        view.getPreRender().changeObject(obj,s);
286
        }
287

    
288
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
289
      }
290
    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
    
293
    @Override
294
    protected void onDestroy() 
295
      {
296
      DistortedLibrary.onDestroy(0);
297
      super.onDestroy();
298
      }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    private void savePreferences()
303
      {
304
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
305
      SharedPreferences.Editor editor = preferences.edit();
306

    
307
      editor.putBoolean("policyAccepted", mPolicyAccepted);
308

    
309
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
310
        {
311
        BaseEffect.Type.getType(i).savePreferences(editor);
312
        }
313

    
314
      for (int i = 0; i< ScreenList.LENGTH; i++)
315
        {
316
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
317
        }
318

    
319
      ScreenList.savePreferences(editor);
320
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
321
      view.getPreRender().savePreferences(editor);
322

    
323
      editor.apply();
324
      }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
    private void restorePreferences()
329
      {
330
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
331

    
332
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
333

    
334
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
335
        {
336
        BaseEffect.Type.getType(i).restorePreferences(preferences);
337
        }
338

    
339
      for (int i = 0; i< ScreenList.LENGTH; i++)
340
        {
341
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
342
        }
343

    
344
      ScreenList.restorePreferences(preferences);
345

    
346
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
347
      view.getPreRender().restorePreferences(preferences);
348

    
349
      RubikScores scores = RubikScores.getInstance();
350

    
351
      if( scores.isVerified() )
352
        {
353
        FirebaseAnalytics analytics = getAnalytics();
354
        analytics.setUserId(scores.getName());
355
        }
356
      }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
    private void PrivacyPolicy()
361
      {
362
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
363
      priDiag.show(getSupportFragmentManager(), null);
364
      }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
    void OpenGLError()
369
      {
370
      RubikDialogError errDiag = new RubikDialogError();
371
      errDiag.show(getSupportFragmentManager(), null);
372
      }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
// PUBLIC API
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
    public FirebaseAnalytics getAnalytics()
379
      {
380
      return mFirebaseAnalytics;
381
      }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
    public TwistyObject getObject()
386
      {
387
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
388
      RubikPreRender pre = view.getPreRender();
389
      return pre.getObject();
390
      }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
    public DistortedScreen getScreen()
395
      {
396
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
397
      RubikRenderer renderer = view.getRenderer();
398
      return renderer.getScreen();
399
      }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
    public RubikPreRender getPreRender()
404
      {
405
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
406
      return view.getPreRender();
407
      }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
    public TwistyPreRender getTwistyPreRender()
412
      {
413
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
414
      return view.getPreRender();
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
    public RubikSurfaceView getSurfaceView()
420
      {
421
      return findViewById(R.id.rubikSurfaceView);
422
      }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
    public int getScreenWidthInPixels()
427
      {
428
      return mScreenWidth;
429
      }
430

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

    
433
    public int getScreenHeightInPixels()
434
      {
435
      return mScreenHeight;
436
      }
437

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

    
440
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
441
      {
442
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
443
      RubikPreRender pre = view.getPreRender();
444

    
445
      if( reportChange )
446
        {
447
        TwistyObject oldObject = pre.getObject();
448

    
449
        if( oldObject!=null )
450
          {
451
          ObjectList oldList = oldObject.getObjectList();
452
          int oldNum = oldObject.getNumLayers();
453
          float fps = view.getRenderer().getFPS();
454
          fps = (int)(fps+0.5f);
455
          StringBuilder name = new StringBuilder();
456
          name.append(oldList.name());
457
          name.append('_');
458
          name.append(oldNum);
459
          name.append(' ');
460
          name.append(fps);
461
          name.append(" --> ");
462
          name.append(newObject.name());
463
          name.append('_');
464
          name.append(newSize);
465

    
466
          if( BuildConfig.DEBUG )
467
            {
468
            android.util.Log.e("rubik", name.toString());
469
            }
470
          else
471
            {
472
            FirebaseAnalytics analytics = getAnalytics();
473

    
474
            if( analytics!=null )
475
              {
476
              Bundle bundle = new Bundle();
477
              bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
478
              analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
479
              }
480
            }
481
          }
482
        }
483

    
484
      pre.changeObject(newObject,newSize);
485
      }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
    public void setupObject(ObjectList object, int size, int[][] moves)
490
      {
491
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
492
      RubikPreRender pre = view.getPreRender();
493
      pre.setupObject(object,size,moves);
494
      }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
    public static int getDrawableSize()
499
      {
500
      if( mScreenHeight<1000 )
501
        {
502
        return 0;
503
        }
504
      if( mScreenHeight<1600 )
505
        {
506
        return 1;
507
        }
508
      if( mScreenHeight<1900 )
509
        {
510
        return 2;
511
        }
512

    
513
      return 3;
514
      }
515

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

    
518
    public static int getDrawable(int small, int medium, int big, int huge)
519
      {
520
      int size = getDrawableSize();
521

    
522
      switch(size)
523
        {
524
        case 0 : return small;
525
        case 1 : return medium;
526
        case 2 : return big;
527
        default: return huge;
528
        }
529
      }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
    public void setControlState(boolean on)
534
      {
535
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
536
      RubikRenderer renderer = view.getRenderer();
537
      renderer.setControlState(on);
538
      }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
    public void acceptPrivacy()
543
      {
544
      mPolicyAccepted = true;
545
      }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
    public void declinePrivacy()
550
      {
551
      finish();
552
      }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
    public static boolean localeIsChinese()
557
      {
558
      String language;
559

    
560
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
561
        {
562
        language= LocaleList.getDefault().get(0).getLanguage();
563
        }
564
      else
565
        {
566
        language= Locale.getDefault().getLanguage();
567
        }
568

    
569
      return language.equals("zh");
570
      }
571

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573

    
574
    public boolean isVertical()
575
      {
576
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
577
      return view.isVertical();
578
      }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581

    
582
    public void blockEverything(int place)
583
      {
584
      setLock();
585

    
586
      TwistyPreRender pre = getPreRender();
587
      pre.blockEverything(place);
588

    
589
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
590
      play.setLockState(this);
591
      }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
    public void unblockEverything()
596
      {
597
      unsetLock();
598

    
599
      TwistyPreRender pre = getPreRender();
600
      pre.unblockEverything();
601

    
602
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
603
      play.setLockState(this);
604
      }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607

    
608
    public Static4D getCurrQuat()
609
      {
610
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
611
      return view.getQuat();
612
      }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615

    
616
    public void switchTutorial(String url, ObjectList object, int size)
617
      {
618
      Intent myIntent = new Intent(this, TutorialActivity.class);
619
      myIntent.putExtra("url", url);
620
      myIntent.putExtra("obj", object.ordinal());
621
      myIntent.putExtra("siz", size);
622
      startActivity(myIntent);
623
      }
624
}
(1-1/4)