Project

General

Profile

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

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

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 java.util.Locale;
23

    
24
import android.content.Intent;
25
import android.content.SharedPreferences;
26
import android.os.Build;
27
import android.os.Bundle;
28
import android.os.LocaleList;
29
import android.preference.PreferenceManager;
30

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

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

    
40
import org.distorted.library.main.DistortedLibrary;
41
import org.distorted.library.main.DistortedScreen;
42

    
43
import org.distorted.objectlib.main.ObjectControl;
44
import org.distorted.objectlib.main.TwistyObject;
45
import org.distorted.objectlib.main.ObjectType;
46
import org.distorted.objectlib.helpers.BlockController;
47
import org.distorted.objectlib.effects.BaseEffect;
48
import org.distorted.objectlib.helpers.TwistyActivity;
49

    
50
import org.distorted.dialogs.RubikDialogError;
51
import org.distorted.dialogs.RubikDialogPrivacy;
52
import org.distorted.network.RubikScores;
53
import org.distorted.network.RubikNetwork;
54
import org.distorted.screens.ScreenList;
55
import org.distorted.screens.RubikScreenPlay;
56
import org.distorted.tutorials.TutorialActivity;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

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

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

    
89
    private static final float RATIO_BAR  = 0.10f;
90
    private static final float RATIO_INSET= 0.08f;
91

    
92
    private boolean mJustStarted;
93
    private FirebaseAnalytics mFirebaseAnalytics;
94
    private static int mScreenWidth, mScreenHeight;
95
    private boolean mPolicyAccepted, mIsChinese;
96
    private int mCurrentApiVersion;
97
    private int mHeightUpperBar, mHeightLowerBar;
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    @Override
102
    protected void onCreate(Bundle savedState)
103
      {
104
      super.onCreate(savedState);
105
      DistortedLibrary.onCreate(0);
106

    
107
      setTheme(R.style.CustomActivityThemeNoActionBar);
108
      setContentView(R.layout.main);
109

    
110
      mJustStarted = true;
111
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
112

    
113
      DisplayMetrics displaymetrics = new DisplayMetrics();
114
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
115
      mScreenWidth =displaymetrics.widthPixels;
116
      mScreenHeight=displaymetrics.heightPixels;
117

    
118
      mIsChinese = localeIsChinese();
119
      unlock();
120

    
121
      hideNavigationBar();
122
      cutoutHack();
123
      computeBarHeights();
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
// this does not include possible insets
128

    
129
    private void computeBarHeights()
130
      {
131
      float height = getScreenHeightInPixels();
132
      int barHeight = (int)(height*RATIO_BAR);
133
      mHeightLowerBar = barHeight;
134
      mHeightUpperBar = barHeight;
135

    
136
      LinearLayout layoutTop = findViewById(R.id.upperBar);
137
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
138

    
139
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
140
      paramsTop.height = mHeightUpperBar;
141
      layoutTop.setLayoutParams(paramsTop);
142
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
143
      paramsBot.height = mHeightLowerBar;
144
      layoutBot.setLayoutParams(paramsBot);
145
      }
146

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

    
149
    private void hideNavigationBar()
150
      {
151
      mCurrentApiVersion = Build.VERSION.SDK_INT;
152

    
153
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
154
        {
155
        final View decorView = getWindow().getDecorView();
156

    
157
        decorView.setSystemUiVisibility(FLAGS);
158

    
159
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
160
          {
161
          @Override
162
          public void onSystemUiVisibilityChange(int visibility)
163
            {
164
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
165
              {
166
              decorView.setSystemUiVisibility(FLAGS);
167
              }
168
            }
169
          });
170
        }
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    @Override
176
    public void onAttachedToWindow()
177
      {
178
      super.onAttachedToWindow();
179

    
180
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
181
        {
182
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
183
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
184

    
185
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
186
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
187
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
188
        layoutHid.setLayoutParams(paramsHid);
189
        mHeightUpperBar += paramsHid.height;
190
        }
191
      }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
// do not avoid cutouts
195

    
196
    private void cutoutHack()
197
      {
198
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
199
        {
200
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
201
        }
202
      }
203

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

    
206
    @Override
207
    public void onWindowFocusChanged(boolean hasFocus)
208
      {
209
      super.onWindowFocusChanged(hasFocus);
210

    
211
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
212
        {
213
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
214
        }
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
    
219
    @Override
220
    protected void onPause() 
221
      {
222
      super.onPause();
223
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
224
      view.onPause();
225
      DistortedLibrary.onPause(0);
226
      BlockController.onPause();
227
      RubikNetwork.onPause();
228
      savePreferences();
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
    
233
    @Override
234
    protected void onResume() 
235
      {
236
      super.onResume();
237
      DistortedLibrary.onResume(0);
238
      BlockController.onResume();
239
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
240
      view.onResume();
241
      restorePreferences();
242
      ScreenList.setScreen(this);
243
      unblockEverything();
244

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

    
253
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
254
      ObjectType object = play.getObject();
255
      view.getObjectControl().changeObject(object);
256
      
257
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
258
      }
259
    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
    
262
    @Override
263
    protected void onDestroy() 
264
      {
265
      DistortedLibrary.onDestroy(0);
266
      super.onDestroy();
267
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
    private void savePreferences()
272
      {
273
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
274
      SharedPreferences.Editor editor = preferences.edit();
275

    
276
      editor.putBoolean("policyAccepted", mPolicyAccepted);
277

    
278
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
279
        {
280
        BaseEffect.Type.getType(i).savePreferences(editor);
281
        }
282

    
283
      for (int i = 0; i< ScreenList.LENGTH; i++)
284
        {
285
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
286
        }
287

    
288
      ScreenList.savePreferences(editor);
289
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
290
      view.getObjectControl().savePreferences(editor);
291

    
292
      editor.apply();
293
      }
294

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

    
297
    private void restorePreferences()
298
      {
299
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
300

    
301
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
302

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

    
308
      for (int i = 0; i< ScreenList.LENGTH; i++)
309
        {
310
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
311
        }
312

    
313
      ScreenList.restorePreferences(preferences);
314

    
315
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
316
      view.getObjectControl().restorePreferences(preferences);
317

    
318
      RubikScores scores = RubikScores.getInstance();
319

    
320
      if( scores.isVerified() )
321
        {
322
        FirebaseAnalytics analytics = getAnalytics();
323
        analytics.setUserId(scores.getName());
324
        }
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    private void PrivacyPolicy()
330
      {
331
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
332
      priDiag.show(getSupportFragmentManager(), null);
333
      }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
    void OpenGLError()
338
      {
339
      RubikDialogError errDiag = new RubikDialogError();
340
      errDiag.show(getSupportFragmentManager(), null);
341
      }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344
// PUBLIC API
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
    public FirebaseAnalytics getAnalytics()
348
      {
349
      return mFirebaseAnalytics;
350
      }
351

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

    
354
    public int getHeightUpperBar()
355
      {
356
      return mHeightUpperBar;
357
      }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
    public int getHeightLowerBar()
362
      {
363
      return mHeightLowerBar;
364
      }
365

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

    
368
    public TwistyObject getObject()
369
      {
370
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
371
      return view.getObjectControl().getObject();
372
      }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
    public ObjectControl getControl()
377
      {
378
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
379
      return view.getObjectControl();
380
      }
381

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

    
384
    public DistortedScreen getScreen()
385
      {
386
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
387
      RubikRenderer renderer = view.getRenderer();
388
      return renderer.getScreen();
389
      }
390

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

    
393
    public int getScreenWidthInPixels()
394
      {
395
      return mScreenWidth;
396
      }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
    public int getScreenHeightInPixels()
401
      {
402
      return mScreenHeight;
403
      }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
    public void changeObject(ObjectType newObject, boolean reportChange)
408
      {
409
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
410
      ObjectControl control = view.getObjectControl();
411

    
412
      if( reportChange )
413
        {
414
        TwistyObject oldObject = control.getObject();
415

    
416
        if( oldObject!=null )
417
          {
418
          ObjectType oldType = oldObject.getObjectType();
419
          float fps = view.getRenderer().getFPS();
420
          fps = (int)(fps+0.5f);
421
          StringBuilder name = new StringBuilder();
422
          name.append(oldType.name());
423
          name.append(' ');
424
          name.append(fps);
425
          name.append(" --> ");
426
          name.append(newObject.name());
427

    
428
          if( BuildConfig.DEBUG )
429
            {
430
            android.util.Log.e("rubik", name.toString());
431
            }
432
          else
433
            {
434
            FirebaseAnalytics analytics = getAnalytics();
435

    
436
            if( analytics!=null )
437
              {
438
              Bundle bundle = new Bundle();
439
              bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
440
              analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
441
              }
442
            }
443
          }
444
        }
445

    
446
      control.changeObject(newObject);
447
      }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public boolean isLocked()
452
    {
453
    ScreenList state = ScreenList.getCurrentScreen();
454

    
455
    if( state== ScreenList.PLAY || state== ScreenList.READ || state== ScreenList.SOLV )
456
      {
457
      return retLocked();
458
      }
459

    
460
    return false;
461
    }
462

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

    
465
    public void setupObject(ObjectType object, int[][] moves)
466
      {
467
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
468
      ObjectControl control = view.getObjectControl();
469
      control.setupObject(object,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 blockEverything(int place)
550
      {
551
      setLock();
552

    
553
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
554
      ObjectControl control = view.getObjectControl();
555
      control.blockEverything(place);
556

    
557
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
558
      play.setLockState(this);
559
      }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
    public void unblockEverything()
564
      {
565
      unsetLock();
566

    
567
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
568
      ObjectControl control = view.getObjectControl();
569
      control.unblockEverything();
570

    
571
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
572
      play.setLockState(this);
573
      }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

    
577
    public void switchTutorial(String url, ObjectType object)
578
      {
579
      Intent myIntent = new Intent(this, TutorialActivity.class);
580
      myIntent.putExtra("url", url);
581
      myIntent.putExtra("obj", object.ordinal());
582
      startActivity(myIntent);
583
      }
584
}
(1-1/4)