Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 51853bf2

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.io.InputStream;
23
import java.util.Locale;
24

    
25
import android.content.Intent;
26
import android.content.SharedPreferences;
27
import android.content.pm.PackageInfo;
28
import android.content.pm.PackageManager;
29
import android.os.Build;
30
import android.os.Bundle;
31
import android.os.LocaleList;
32
import android.preference.PreferenceManager;
33

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

    
41
import androidx.appcompat.app.AppCompatActivity;
42

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

    
45
import org.distorted.config.ConfigActivity;
46
import org.distorted.dmesh.ObjectMesh;
47
import org.distorted.jsons.ObjectJson;
48
import org.distorted.library.main.DistortedLibrary;
49

    
50
import org.distorted.objectlib.main.ObjectControl;
51
import org.distorted.objectlib.main.TwistyObject;
52
import org.distorted.objectlib.main.ObjectType;
53
import org.distorted.objectlib.effects.BaseEffect;
54

    
55
import org.distorted.dialogs.RubikDialogError;
56
import org.distorted.dialogs.RubikDialogPrivacy;
57
import org.distorted.network.RubikScores;
58
import org.distorted.network.RubikNetwork;
59
import org.distorted.screens.ScreenList;
60
import org.distorted.screens.RubikScreenPlay;
61
import org.distorted.tutorials.TutorialActivity;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
85
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
86
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
87
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
88
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
89
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
90

    
91
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
92
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
93

    
94
    private static final int ACTIVITY_NUMBER = 0;
95
    private static final float RATIO_BAR  = 0.10f;
96
    private static final float RATIO_INSET= 0.08f;
97

    
98
    private boolean mJustStarted;
99
    private FirebaseAnalytics mFirebaseAnalytics;
100
    private static int mScreenWidth, mScreenHeight;
101
    private boolean mPolicyAccepted, mIsChinese;
102
    private int mCurrentApiVersion;
103
    private int mHeightUpperBar, mHeightLowerBar;
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
    @Override
108
    protected void onCreate(Bundle savedState)
109
      {
110
      super.onCreate(savedState);
111
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
112

    
113
      setTheme(R.style.MaterialThemeNoActionBar);
114
      setContentView(R.layout.main);
115

    
116
      mJustStarted = true;
117
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
118
      mIsChinese = localeIsChinese();
119

    
120
      DisplayMetrics displaymetrics = new DisplayMetrics();
121
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
122
      mScreenWidth =displaymetrics.widthPixels;
123
      mScreenHeight=displaymetrics.heightPixels;
124
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
125
                                                  // which is not yet hidden.
126
                                                  // TODO: figure this out exactly.
127
      hideNavigationBar();
128
      cutoutHack();
129
      computeBarHeights();
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// this does not include possible insets
134

    
135
    private void computeBarHeights()
136
      {
137
      float height = getScreenHeightInPixels();
138
      int barHeight = (int)(height*RATIO_BAR);
139
      mHeightLowerBar = barHeight;
140
      mHeightUpperBar = barHeight;
141

    
142
      LinearLayout layoutTop = findViewById(R.id.upperBar);
143
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
144

    
145
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
146
      paramsTop.height = mHeightUpperBar;
147
      layoutTop.setLayoutParams(paramsTop);
148
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
149
      paramsBot.height = mHeightLowerBar;
150
      layoutBot.setLayoutParams(paramsBot);
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
    private void hideNavigationBar()
156
      {
157
      mCurrentApiVersion = Build.VERSION.SDK_INT;
158

    
159
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
160
        {
161
        final View decorView = getWindow().getDecorView();
162

    
163
        decorView.setSystemUiVisibility(FLAGS);
164

    
165
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
166
          {
167
          @Override
168
          public void onSystemUiVisibilityChange(int visibility)
169
            {
170
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
171
              {
172
              decorView.setSystemUiVisibility(FLAGS);
173
              }
174
            }
175
          });
176
        }
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    @Override
182
    public void onAttachedToWindow()
183
      {
184
      super.onAttachedToWindow();
185

    
186
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
187
        {
188
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
189
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
190

    
191
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
192
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
193
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
194
        layoutHid.setLayoutParams(paramsHid);
195
        mHeightUpperBar += paramsHid.height;
196
        }
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
// do not avoid cutouts
201

    
202
    private void cutoutHack()
203
      {
204
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
205
        {
206
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
207
        }
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    @Override
213
    public void onWindowFocusChanged(boolean hasFocus)
214
      {
215
      super.onWindowFocusChanged(hasFocus);
216

    
217
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
218
        {
219
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
220
        }
221
      }
222

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

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

    
247
      ScreenList.setScreen(this);
248
      unblockEverything();
249

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

    
258
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
259
      ObjectType object = play.getObject();
260
      changeIfDifferent(object,view.getObjectControl());
261

    
262
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
263
      }
264
    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
    
267
    @Override
268
    protected void onDestroy() 
269
      {
270
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
271
      super.onDestroy();
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    private String getAppVers()
277
      {
278
      try
279
        {
280
        PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
281
        return pInfo.versionName;
282
        }
283
      catch (PackageManager.NameNotFoundException e)
284
        {
285
        return "unknown";
286
        }
287
      }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    private void savePreferences()
292
      {
293
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
294
      SharedPreferences.Editor editor = preferences.edit();
295

    
296
      editor.putBoolean("policyAccepted", mPolicyAccepted);
297
      editor.putString("appVersion", getAppVers() );
298

    
299
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
300
        {
301
        BaseEffect.Type.getType(i).savePreferences(editor);
302
        }
303

    
304
      for (int i = 0; i< ScreenList.LENGTH; i++)
305
        {
306
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
307
        }
308

    
309
      ScreenList.savePreferences(editor);
310
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
311
      view.getObjectControl().savePreferences(editor);
312

    
313
      editor.apply();
314
      }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
    private void restorePreferences()
319
      {
320
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
321

    
322
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
323
      String oldVersion = preferences.getString("appVersion","");
324

    
325
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
326
        {
327
        BaseEffect.Type.getType(i).restorePreferences(preferences);
328
        }
329

    
330
      for (int i = 0; i< ScreenList.LENGTH; i++)
331
        {
332
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
333
        }
334

    
335
      ScreenList.restorePreferences(preferences);
336

    
337
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
338

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

    
348
      if( !oldVersion.equals("") )
349
        {
350
        view.getObjectControl().restorePreferences(preferences);
351
        }
352

    
353
      RubikScores scores = RubikScores.getInstance();
354

    
355
      if( scores.isVerified() )
356
        {
357
        FirebaseAnalytics analytics = getAnalytics();
358
        analytics.setUserId(scores.getName());
359
        }
360
      }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
    private void PrivacyPolicy()
365
      {
366
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
367
      priDiag.show(getSupportFragmentManager(), null);
368
      }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
    void OpenGLError()
373
      {
374
      RubikDialogError errDiag = new RubikDialogError();
375
      errDiag.show(getSupportFragmentManager(), null);
376
      }
377

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

    
380
    void setScreenSize(int width, int height)
381
      {
382
      mScreenWidth = width;
383
      mScreenHeight= height;
384
      }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387
// PUBLIC API
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
    public FirebaseAnalytics getAnalytics()
391
      {
392
      return mFirebaseAnalytics;
393
      }
394

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

    
397
    public int getHeightUpperBar()
398
      {
399
      return mHeightUpperBar;
400
      }
401

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

    
404
    public int getHeightLowerBar()
405
      {
406
      return mHeightLowerBar;
407
      }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
    public TwistyObject getObject()
412
      {
413
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
414
      return view.getObjectControl().getObject();
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
    public ObjectControl getControl()
420
      {
421
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
422
      return view.getObjectControl();
423
      }
424

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

    
427
    public int getScreenWidthInPixels()
428
      {
429
      return mScreenWidth;
430
      }
431

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

    
434
    public int getScreenHeightInPixels()
435
      {
436
      return mScreenHeight;
437
      }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
    public void changeObject(ObjectType newObject, boolean reportChange)
442
      {
443
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
444
      ObjectControl control = view.getObjectControl();
445
      TwistyObject oldObject = control.getObject();
446
      changeIfDifferent(newObject,control);
447

    
448
      if( reportChange && oldObject!=null )
449
        {
450
        float fps = view.getRenderer().getFPS();
451
        fps = (int)(fps+0.5f);
452
        StringBuilder name = new StringBuilder();
453
        name.append(oldObject.getShortName());
454
        name.append(' ');
455
        name.append(fps);
456
        name.append(" --> ");
457
        name.append(newObject.name());
458

    
459
        if( BuildConfig.DEBUG )
460
          {
461
          android.util.Log.e("rubik", name.toString());
462
          }
463
        else
464
          {
465
          FirebaseAnalytics analytics = getAnalytics();
466

    
467
          if( analytics!=null )
468
            {
469
            Bundle bundle = new Bundle();
470
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
471
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
472
            }
473
          }
474
        }
475
      }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

    
479
    public void changeIfDifferent(ObjectType type,ObjectControl control)
480
      {
481
      InputStream jsonStream = ObjectJson.getStream(type,this);
482
      InputStream meshStream = ObjectMesh.getStream(type,this);
483
      control.changeIfDifferent(type.ordinal(),jsonStream,meshStream);
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

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

    
597
    public void switchConfig(ObjectType object)
598
      {
599
      Intent myIntent = new Intent(this, ConfigActivity.class);
600
      myIntent.putExtra("obj", object.ordinal());
601
      startActivity(myIntent);
602
      }
603
}
(1-1/4)