Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 03822c33

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.objects.TwistyObject;
48
import org.distorted.network.RubikScores;
49
import org.distorted.network.RubikNetwork;
50
import org.distorted.objects.ObjectList;
51
import org.distorted.screens.ScreenList;
52
import org.distorted.screens.RubikScreenPlay;
53
import org.distorted.tutorials.TutorialActivity;
54

    
55
import java.util.Locale;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

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

    
88
    private boolean mJustStarted;
89
    private FirebaseAnalytics mFirebaseAnalytics;
90
    private static int mScreenWidth, mScreenHeight;
91
    private boolean mPolicyAccepted, mIsChinese;
92
    private int mCurrentApiVersion;
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

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

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

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

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

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

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

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

    
131
        decorView.setSystemUiVisibility(FLAGS);
132

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

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

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

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

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

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
// do not avoid cutouts
181

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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

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

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

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

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

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

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

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

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

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

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

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

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

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

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

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

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

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

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

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

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

    
322
      editor.apply();
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

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

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

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

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

    
343
      ScreenList.restorePreferences(preferences);
344

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

    
348
      RubikScores scores = RubikScores.getInstance();
349

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

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

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

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

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

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374
// PUBLIC API
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

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

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

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

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

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

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

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

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

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

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

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

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

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

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

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

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

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

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

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

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

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

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

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

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

    
512
      return 3;
513
      }
514

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

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

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

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

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

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

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

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

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

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554

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

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

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

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

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

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580

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

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

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

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

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

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

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

    
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606

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