Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ f8eece7d

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

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

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

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

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

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

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

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

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

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

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

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

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

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

    
318
      editor.apply();
319
      }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

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

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

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

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

    
339
      ScreenList.restorePreferences(preferences);
340

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

    
344
      RubikScores scores = RubikScores.getInstance();
345

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

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

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

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

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

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// PUBLIC API
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

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

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

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

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

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

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

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

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

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

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

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

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

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

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

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

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

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

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

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

    
489
      return 3;
490
      }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

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

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

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

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

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515

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

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

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

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

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

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

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

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

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

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

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

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

    
565
      return false;
566
      }
567

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

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

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

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

    
581
    public void unblockEverything()
582
      {
583
      mIsLocked = mRemLocked;
584

    
585
      RubikPreRender pre = getPreRender();
586
      pre.unblockEverything();
587
      }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

    
591
    public boolean retLocked()
592
      {
593
      return mIsLocked;
594
      }
595

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

    
598
    public void switchTutorial(String url, ObjectList object, int size)
599
      {
600
      Intent myIntent = new Intent(this, TutorialActivity.class);
601
      myIntent.putExtra("url", url);
602
      myIntent.putExtra("obj", object.ordinal());
603
      myIntent.putExtra("siz", size);
604
      startActivity(myIntent);
605
      }
606
}
(1-1/4)