Project

General

Profile

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

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

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.objects.TwistyObject;
45
import org.distorted.scores.RubikScores;
46
import org.distorted.scores.RubikScoresDownloader;
47
import org.distorted.objects.ObjectList;
48
import org.distorted.states.StateList;
49
import org.distorted.states.RubikStatePlay;
50
import org.distorted.tutorials.TutorialActivity;
51

    
52
import java.util.Locale;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

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

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

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

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

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

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

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

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

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

    
129
        decorView.setSystemUiVisibility(FLAGS);
130

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

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

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

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

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

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

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// do not avoid cutouts
179

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

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

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

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

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

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

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

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

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

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

    
255
      boolean success = false;
256
      RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
257
      int object = play.getObject();
258
      int size   = play.getSize();
259

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

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

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

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

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

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

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

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

    
308
      for (int i = 0; i< StateList.LENGTH; i++)
309
        {
310
        StateList.getState(i).getStateClass().savePreferences(editor);
311
        }
312

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

    
317
      editor.apply();
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

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

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

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

    
333
      for (int i = 0; i< StateList.LENGTH; i++)
334
        {
335
        StateList.getState(i).getStateClass().restorePreferences(preferences);
336
        }
337

    
338
      StateList.restorePreferences(preferences);
339

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

    
343
      RubikScores scores = RubikScores.getInstance();
344

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

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

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

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369
// PUBLIC API
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

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

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
    public int getScreenWidthInPixels()
389
      {
390
      return mScreenWidth;
391
      }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
    public int getScreenHeightInPixels()
396
      {
397
      return mScreenHeight;
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 void changeObject(ObjectList newObject, int newSize, boolean reportChange)
411
      {
412
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
413
      RubikPreRender pre = view.getPreRender();
414

    
415
      if( reportChange )
416
        {
417
        TwistyObject oldObject = pre.getObject();
418
        ObjectList oldList = oldObject.getObjectList();
419
        int oldNum = oldObject.getNumLayers();
420
        float fps = view.getRenderer().getFPS();
421
        fps = (int)(fps+0.5f);
422
        StringBuilder name = new StringBuilder();
423
        name.append(oldList.name());
424
        name.append('_');
425
        name.append(oldNum);
426
        name.append(' ');
427
        name.append(fps);
428
        name.append(" --> ");
429
        name.append(newObject.name());
430
        name.append('_');
431
        name.append(newSize);
432

    
433
        if( BuildConfig.DEBUG )
434
          {
435
          android.util.Log.e("rubik", name.toString());
436
          }
437
        else
438
          {
439
          FirebaseAnalytics analytics = getAnalytics();
440

    
441
          if( analytics!=null )
442
            {
443
            Bundle bundle = new Bundle();
444
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
445
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
446
            }
447
          }
448
        }
449

    
450
      pre.changeObject(newObject,newSize);
451
      }
452

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

    
455
    public void setupObject(ObjectList object, int size, int[][] moves)
456
      {
457
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
458
      RubikPreRender pre = view.getPreRender();
459
      pre.setupObject(object,size,moves);
460
      }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
    public static int getDrawableSize()
465
      {
466
      if( mScreenHeight<1000 )
467
        {
468
        return 0;
469
        }
470
      if( mScreenHeight<1600 )
471
        {
472
        return 1;
473
        }
474
      if( mScreenHeight<1900 )
475
        {
476
        return 2;
477
        }
478

    
479
      return 3;
480
      }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
    public static int getDrawable(int small, int medium, int big, int huge)
485
      {
486
      int size = getDrawableSize();
487

    
488
      switch(size)
489
        {
490
        case 0 : return small;
491
        case 1 : return medium;
492
        case 2 : return big;
493
        default: return huge;
494
        }
495
      }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

    
499
    public void acceptPrivacy()
500
      {
501
      mPolicyAccepted = true;
502
      }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
    public void declinePrivacy()
507
      {
508
      finish();
509
      }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
    public static boolean localeIsChinese()
514
      {
515
      String language;
516

    
517
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
518
        {
519
        language= LocaleList.getDefault().get(0).getLanguage();
520
        }
521
      else
522
        {
523
        language= Locale.getDefault().getLanguage();
524
        }
525

    
526
      return language.equals("zh");
527
      }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
    public boolean isVertical()
532
      {
533
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
534
      return view.isVertical();
535
      }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

    
539
    public void toggleLock()
540
      {
541
      mIsLocked = !mIsLocked;
542
      }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
    public boolean isLocked()
547
      {
548
      StateList state = StateList.getCurrentState();
549

    
550
      if( state== StateList.PLAY || state== StateList.READ || state== StateList.SOLV )
551
        {
552
        return mIsLocked;
553
        }
554

    
555
      return false;
556
      }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

    
560
    public boolean retLocked()
561
      {
562
      return mIsLocked;
563
      }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
    public void switchTutorial(String url, ObjectList object, int size)
568
      {
569
      Intent myIntent = new Intent(this, TutorialActivity.class);
570
      myIntent.putExtra("url", url);
571
      myIntent.putExtra("obj", object.ordinal());
572
      myIntent.putExtra("siz", size);
573
      startActivity(myIntent);
574
      }
575
}
(1-1/4)