Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 338babe8

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.content.pm.PackageInfo;
27
import android.content.pm.PackageManager;
28
import android.os.Build;
29
import android.os.Bundle;
30
import android.os.LocaleList;
31
import android.preference.PreferenceManager;
32

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

    
40
import androidx.appcompat.app.AppCompatActivity;
41

    
42
import com.google.firebase.analytics.FirebaseAnalytics;
43

    
44
import org.distorted.library.main.DistortedLibrary;
45

    
46
import org.distorted.objectlib.main.ObjectControl;
47
import org.distorted.objectlib.main.TwistyObject;
48
import org.distorted.objectlib.main.ObjectType;
49
import org.distorted.objectlib.effects.BaseEffect;
50

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

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

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

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

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

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

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

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

    
119
      mIsChinese = localeIsChinese();
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
      RubikNetwork.onPause();
227
      savePreferences();
228
      }
229

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

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

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
  private String getAppVers()
270
    {
271
    try
272
      {
273
      PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
274
      return pInfo.versionName;
275
      }
276
    catch (PackageManager.NameNotFoundException e)
277
      {
278
      return "unknown";
279
      }
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    private void savePreferences()
285
      {
286
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
287
      SharedPreferences.Editor editor = preferences.edit();
288

    
289
      editor.putBoolean("policyAccepted", mPolicyAccepted);
290
      editor.putString("appVersion", getAppVers() );
291

    
292
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
293
        {
294
        BaseEffect.Type.getType(i).savePreferences(editor);
295
        }
296

    
297
      for (int i = 0; i< ScreenList.LENGTH; i++)
298
        {
299
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
300
        }
301

    
302
      ScreenList.savePreferences(editor);
303
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
304
      view.getObjectControl().savePreferences(editor);
305

    
306
      editor.apply();
307
      }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
    private void restorePreferences()
312
      {
313
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
314

    
315
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
316
      String oldVersion = preferences.getString("appVersion","");
317

    
318
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
319
        {
320
        BaseEffect.Type.getType(i).restorePreferences(preferences);
321
        }
322

    
323
      for (int i = 0; i< ScreenList.LENGTH; i++)
324
        {
325
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
326
        }
327

    
328
      ScreenList.restorePreferences(preferences);
329

    
330
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
331

    
332
      // Versions <= 1.8.6 did not save their 'appVersion' to preferences, therefore in their
333
      // case the 'mOldVersion' - version of the app which saved the preferences on the last
334
      // go - is empty.
335
      // Between versions 1.8.6 and 1.9.0, the order of the cubits in TwistyCube has changed.
336
      // If someone has scrambled the cube with v. 1.8.6, then upgraded to 1.9.0 and re-started
337
      // the app, because of the different order of the cubits - his cube would be messed up.
338
      // So in such case, i.e. on fresh upgrade from version<=1.8.6 to version>=1.9.0, do not
339
      // restore the object scrambling.
340

    
341
      if( !oldVersion.equals("") )
342
        {
343
        view.getObjectControl().restorePreferences(preferences);
344
        }
345

    
346
      RubikScores scores = RubikScores.getInstance();
347

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

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

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

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

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

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
    void setScreenSize(int width, int height)
374
      {
375
      mScreenWidth = width;
376
      mScreenHeight= height;
377
      }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
// PUBLIC API
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
    public FirebaseAnalytics getAnalytics()
384
      {
385
      return mFirebaseAnalytics;
386
      }
387

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

    
390
    public int getHeightUpperBar()
391
      {
392
      return mHeightUpperBar;
393
      }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
    public int getHeightLowerBar()
398
      {
399
      return mHeightLowerBar;
400
      }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
    public TwistyObject getObject()
405
      {
406
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
407
      return view.getObjectControl().getObject();
408
      }
409

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

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

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

    
420
    public int getScreenWidthInPixels()
421
      {
422
      return mScreenWidth;
423
      }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
    public int getScreenHeightInPixels()
428
      {
429
      return mScreenHeight;
430
      }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    public void changeObject(ObjectType newObject, boolean reportChange)
435
      {
436
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
437
      ObjectControl control = view.getObjectControl();
438

    
439
      if( reportChange )
440
        {
441
        TwistyObject oldObject = control.getObject();
442

    
443
        if( oldObject!=null )
444
          {
445
          ObjectType oldType = oldObject.getObjectType();
446
          float fps = view.getRenderer().getFPS();
447
          fps = (int)(fps+0.5f);
448
          StringBuilder name = new StringBuilder();
449
          name.append(oldType.name());
450
          name.append(' ');
451
          name.append(fps);
452
          name.append(" --> ");
453
          name.append(newObject.name());
454

    
455
          if( BuildConfig.DEBUG )
456
            {
457
            android.util.Log.e("rubik", name.toString());
458
            }
459
          else
460
            {
461
            FirebaseAnalytics analytics = getAnalytics();
462

    
463
            if( analytics!=null )
464
              {
465
              Bundle bundle = new Bundle();
466
              bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
467
              analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
468
              }
469
            }
470
          }
471
        }
472

    
473
      control.changeObject(newObject);
474
      }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

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

    
493
      return 3;
494
      }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
    public static int getDrawable(int small, int medium, int big, int huge)
499
      {
500
      int size = getDrawableSize();
501

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

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

    
513
    public void acceptPrivacy()
514
      {
515
      mPolicyAccepted = true;
516
      }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

    
520
    public void declinePrivacy()
521
      {
522
      finish();
523
      }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

    
527
    public static boolean localeIsChinese()
528
      {
529
      String language;
530

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

    
540
      return language.equals("zh");
541
      }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
    public boolean isVertical()
546
      {
547
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
548
      return view.isVertical();
549
      }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552

    
553
    public void blockEverything(int place)
554
      {
555
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
556
      ObjectControl control = view.getObjectControl();
557
      control.blockEverything(place);
558

    
559
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
560
      play.setLockState(this);
561
      }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

    
565
    public void unblockEverything()
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)