Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 4c9947bd

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
import org.distorted.library.main.DistortedScreen;
46

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

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

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

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

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

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

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

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

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

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

    
120
      mIsChinese = localeIsChinese();
121

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

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

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

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

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

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

    
158
        decorView.setSystemUiVisibility(FLAGS);
159

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

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

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

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

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
// do not avoid cutouts
196

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

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

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

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

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

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

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

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

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

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

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

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

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

    
307
      editor.apply();
308
      }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

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

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

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

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

    
329
      ScreenList.restorePreferences(preferences);
330

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

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

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

    
347
      RubikScores scores = RubikScores.getInstance();
348

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

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

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

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

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

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

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

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
// PUBLIC API
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

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

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

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

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

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

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

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

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

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

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
    public DistortedScreen getScreen()
422
      {
423
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
424
      RubikRenderer renderer = view.getRenderer();
425
      return renderer.getScreen();
426
      }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
    public int getScreenWidthInPixels()
431
      {
432
      return mScreenWidth;
433
      }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
    public int getScreenHeightInPixels()
438
      {
439
      return mScreenHeight;
440
      }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
    public void changeObject(ObjectType newObject, boolean reportChange)
445
      {
446
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
447
      ObjectControl control = view.getObjectControl();
448

    
449
      if( reportChange )
450
        {
451
        TwistyObject oldObject = control.getObject();
452

    
453
        if( oldObject!=null )
454
          {
455
          ObjectType oldType = oldObject.getObjectType();
456
          float fps = view.getRenderer().getFPS();
457
          fps = (int)(fps+0.5f);
458
          StringBuilder name = new StringBuilder();
459
          name.append(oldType.name());
460
          name.append(' ');
461
          name.append(fps);
462
          name.append(" --> ");
463
          name.append(newObject.name());
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
      control.changeObject(newObject);
484
      }
485

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

    
488
    public static int getDrawableSize()
489
      {
490
      if( mScreenHeight<1000 )
491
        {
492
        return 0;
493
        }
494
      if( mScreenHeight<1600 )
495
        {
496
        return 1;
497
        }
498
      if( mScreenHeight<1900 )
499
        {
500
        return 2;
501
        }
502

    
503
      return 3;
504
      }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
    public static int getDrawable(int small, int medium, int big, int huge)
509
      {
510
      int size = getDrawableSize();
511

    
512
      switch(size)
513
        {
514
        case 0 : return small;
515
        case 1 : return medium;
516
        case 2 : return big;
517
        default: return huge;
518
        }
519
      }
520

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

    
523
    public void acceptPrivacy()
524
      {
525
      mPolicyAccepted = true;
526
      }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
    public void declinePrivacy()
531
      {
532
      finish();
533
      }
534

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
    public static boolean localeIsChinese()
538
      {
539
      String language;
540

    
541
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
542
        {
543
        language= LocaleList.getDefault().get(0).getLanguage();
544
        }
545
      else
546
        {
547
        language= Locale.getDefault().getLanguage();
548
        }
549

    
550
      return language.equals("zh");
551
      }
552

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

    
555
    public boolean isVertical()
556
      {
557
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
558
      return view.isVertical();
559
      }
560

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

    
563
    public void blockEverything(int place)
564
      {
565
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
566
      ObjectControl control = view.getObjectControl();
567
      control.blockEverything(place);
568

    
569
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
570
      play.setLockState(this);
571
      }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
    public void unblockEverything()
576
      {
577
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
578
      ObjectControl control = view.getObjectControl();
579
      control.unblockEverything();
580

    
581
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
582
      play.setLockState(this);
583
      }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
    public void switchTutorial(String url, ObjectType object)
588
      {
589
      Intent myIntent = new Intent(this, TutorialActivity.class);
590
      myIntent.putExtra("url", url);
591
      myIntent.putExtra("obj", object.ordinal());
592
      startActivity(myIntent);
593
      }
594
}
(1-1/4)