Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 8badfe2a

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
import androidx.appcompat.app.AppCompatActivity;
29

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

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

    
39
import org.distorted.dialogs.RubikDialogError;
40
import org.distorted.dialogs.RubikDialogPrivacy;
41
import org.distorted.effects.BaseEffect;
42
import org.distorted.library.main.DistortedLibrary;
43

    
44
import org.distorted.library.main.DistortedScreen;
45
import org.distorted.objects.TwistyObject;
46
import org.distorted.network.RubikScores;
47
import org.distorted.network.RubikNetwork;
48
import org.distorted.objects.ObjectList;
49
import org.distorted.screens.ScreenList;
50
import org.distorted.screens.RubikScreenPlay;
51
import org.distorted.tutorials.TutorialActivity;
52

    
53
import java.util.Locale;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
77
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
78
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
79
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
80
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
81
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
82

    
83
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
84
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
85

    
86
    private boolean mJustStarted;
87
    private FirebaseAnalytics mFirebaseAnalytics;
88
    private static int mScreenWidth, mScreenHeight;
89
    private boolean mPolicyAccepted, mIsChinese;
90
    private int mCurrentApiVersion;
91
    private boolean mIsLocked, mRemLocked;
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

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

    
101
      setTheme(R.style.CustomActivityThemeNoActionBar);
102
      setContentView(R.layout.main);
103

    
104
      mJustStarted = true;
105
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
106

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

    
112
      mIsChinese = localeIsChinese();
113
      mIsLocked = false;
114

    
115
      hideNavigationBar();
116
      cutoutHack();
117
      // askForPermissions();
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    private void hideNavigationBar()
123
      {
124
      mCurrentApiVersion = Build.VERSION.SDK_INT;
125

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

    
130
        decorView.setSystemUiVisibility(FLAGS);
131

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    @Override
149
    public void onAttachedToWindow()
150
      {
151
      super.onAttachedToWindow();
152

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

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

    
164
      final float RATIO = 0.10f;
165
      float height = getScreenHeightInPixels();
166

    
167
      LinearLayout layoutTop = findViewById(R.id.upperBar);
168
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
169

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
// do not avoid cutouts
180

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

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

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
    private void askForPermissions()
205
      {
206
      final int REQUEST_EXTERNAL_STORAGE = 1;
207

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

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

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

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

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

    
249
      if( mJustStarted )
250
        {
251
        mJustStarted = false;
252
        RubikScores scores = RubikScores.getInstance();
253
        scores.incrementNumRuns();
254
        scores.setCountry(this);
255
        }
256

    
257
      boolean success = false;
258
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
259
      int object = play.getObject();
260
      int size   = play.getSize();
261

    
262
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
263
        {
264
        ObjectList obj = ObjectList.getObject(object);
265
        int[] sizes = obj.getSizes();
266
        int sizeIndex = ObjectList.getSizeIndex(object,size);
267

    
268
        if( sizeIndex>=0 && sizeIndex<sizes.length )
269
          {
270
          success = true;
271
          view.getPreRender().changeObject(obj,size);
272
          }
273
        }
274

    
275
      if( !success )
276
        {
277
        ObjectList obj = ObjectList.getObject(RubikScreenPlay.DEF_OBJECT);
278
        int s = RubikScreenPlay.DEF_SIZE;
279

    
280
        play.setObjectAndSize(this,obj,s);
281
        view.getPreRender().changeObject(obj,s);
282
        }
283

    
284
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
285
      }
286
    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
    
289
    @Override
290
    protected void onDestroy() 
291
      {
292
      DistortedLibrary.onDestroy(0);
293
      super.onDestroy();
294
      }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
    private void savePreferences()
299
      {
300
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
301
      SharedPreferences.Editor editor = preferences.edit();
302

    
303
      editor.putBoolean("policyAccepted", mPolicyAccepted);
304

    
305
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
306
        {
307
        BaseEffect.Type.getType(i).savePreferences(editor);
308
        }
309

    
310
      for (int i = 0; i< ScreenList.LENGTH; i++)
311
        {
312
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
313
        }
314

    
315
      ScreenList.savePreferences(editor);
316
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
317
      view.getPreRender().savePreferences(editor);
318

    
319
      editor.apply();
320
      }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
    private void restorePreferences()
325
      {
326
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
327

    
328
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
329

    
330
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
331
        {
332
        BaseEffect.Type.getType(i).restorePreferences(preferences);
333
        }
334

    
335
      for (int i = 0; i< ScreenList.LENGTH; i++)
336
        {
337
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
338
        }
339

    
340
      ScreenList.restorePreferences(preferences);
341

    
342
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
343
      view.getPreRender().restorePreferences(preferences);
344

    
345
      RubikScores scores = RubikScores.getInstance();
346

    
347
      if( scores.isVerified() )
348
        {
349
        FirebaseAnalytics analytics = getAnalytics();
350
        analytics.setUserId(scores.getName());
351
        }
352
      }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
    private void PrivacyPolicy()
357
      {
358
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
359
      priDiag.show(getSupportFragmentManager(), null);
360
      }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
    void OpenGLError()
365
      {
366
      RubikDialogError errDiag = new RubikDialogError();
367
      errDiag.show(getSupportFragmentManager(), null);
368
      }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
// PUBLIC API
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
    public FirebaseAnalytics getAnalytics()
375
      {
376
      return mFirebaseAnalytics;
377
      }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
    public TwistyObject getObject()
382
      {
383
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
384
      RubikPreRender pre = view.getPreRender();
385
      return pre.getObject();
386
      }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
    public DistortedScreen getScreen()
391
      {
392
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
393
      RubikRenderer renderer = view.getRenderer();
394
      return renderer.getScreen();
395
      }
396

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

    
399
    public int getScreenWidthInPixels()
400
      {
401
      return mScreenWidth;
402
      }
403

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

    
406
    public int getScreenHeightInPixels()
407
      {
408
      return mScreenHeight;
409
      }
410

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

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

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
422
      {
423
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
424
      RubikPreRender pre = view.getPreRender();
425

    
426
      if( reportChange )
427
        {
428
        TwistyObject oldObject = pre.getObject();
429
        ObjectList oldList = oldObject.getObjectList();
430
        int oldNum = oldObject.getNumLayers();
431
        float fps = view.getRenderer().getFPS();
432
        fps = (int)(fps+0.5f);
433
        StringBuilder name = new StringBuilder();
434
        name.append(oldList.name());
435
        name.append('_');
436
        name.append(oldNum);
437
        name.append(' ');
438
        name.append(fps);
439
        name.append(" --> ");
440
        name.append(newObject.name());
441
        name.append('_');
442
        name.append(newSize);
443

    
444
        if( BuildConfig.DEBUG )
445
          {
446
          android.util.Log.e("rubik", name.toString());
447
          }
448
        else
449
          {
450
          FirebaseAnalytics analytics = getAnalytics();
451

    
452
          if( analytics!=null )
453
            {
454
            Bundle bundle = new Bundle();
455
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
456
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
457
            }
458
          }
459
        }
460

    
461
      pre.changeObject(newObject,newSize);
462
      }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
    public void setupObject(ObjectList object, int size, int[][] moves)
467
      {
468
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
469
      RubikPreRender pre = view.getPreRender();
470
      pre.setupObject(object,size,moves);
471
      }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
    public static int getDrawableSize()
476
      {
477
      if( mScreenHeight<1000 )
478
        {
479
        return 0;
480
        }
481
      if( mScreenHeight<1600 )
482
        {
483
        return 1;
484
        }
485
      if( mScreenHeight<1900 )
486
        {
487
        return 2;
488
        }
489

    
490
      return 3;
491
      }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
    public static int getDrawable(int small, int medium, int big, int huge)
496
      {
497
      int size = getDrawableSize();
498

    
499
      switch(size)
500
        {
501
        case 0 : return small;
502
        case 1 : return medium;
503
        case 2 : return big;
504
        default: return huge;
505
        }
506
      }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

    
510
    public void acceptPrivacy()
511
      {
512
      mPolicyAccepted = true;
513
      }
514

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

    
517
    public void declinePrivacy()
518
      {
519
      finish();
520
      }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

    
524
    public static boolean localeIsChinese()
525
      {
526
      String language;
527

    
528
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
529
        {
530
        language= LocaleList.getDefault().get(0).getLanguage();
531
        }
532
      else
533
        {
534
        language= Locale.getDefault().getLanguage();
535
        }
536

    
537
      return language.equals("zh");
538
      }
539

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

    
542
    public boolean isVertical()
543
      {
544
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
545
      return view.isVertical();
546
      }
547

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

    
550
    public void toggleLock()
551
      {
552
      mIsLocked = !mIsLocked;
553
      }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556

    
557
    public boolean isLocked()
558
      {
559
      ScreenList state = ScreenList.getCurrentScreen();
560

    
561
      if( state== ScreenList.PLAY || state== ScreenList.READ || state== ScreenList.SOLV )
562
        {
563
        return mIsLocked;
564
        }
565

    
566
      return false;
567
      }
568

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

    
571
    public void blockEverything()
572
      {
573
      mRemLocked = mIsLocked;
574
      mIsLocked = true;
575

    
576
      RubikPreRender pre = getPreRender();
577
      pre.blockEverything();
578

    
579
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
580
      play.setLockState(this);
581
      }
582

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

    
585
    public void unblockEverything()
586
      {
587
      mIsLocked = mRemLocked;
588

    
589
      RubikPreRender pre = getPreRender();
590
      pre.unblockEverything();
591

    
592
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
593
      play.setLockState(this);
594
      }
595

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597

    
598
    public boolean retLocked()
599
      {
600
      return mIsLocked;
601
      }
602

    
603
///////////////////////////////////////////////////////////////////////////////////////////////////
604

    
605
    public void switchTutorial(String url, ObjectList object, int size)
606
      {
607
      Intent myIntent = new Intent(this, TutorialActivity.class);
608
      myIntent.putExtra("url", url);
609
      myIntent.putExtra("obj", object.ordinal());
610
      myIntent.putExtra("siz", size);
611
      startActivity(myIntent);
612
      }
613
}
(1-1/4)