Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 7cb8d4b0

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.bandaged.BandagedCreatorActivity;
47
import org.distorted.library.main.DistortedLibrary;
48

    
49
import org.distorted.objectlib.main.ObjectControl;
50
import org.distorted.objectlib.main.TwistyObject;
51
import org.distorted.objectlib.effects.BaseEffect;
52

    
53
import org.distorted.dialogs.RubikDialogError;
54
import org.distorted.dialogs.RubikDialogPrivacy;
55
import org.distorted.external.RubikScores;
56
import org.distorted.external.RubikNetwork;
57
import org.distorted.objects.RubikObject;
58
import org.distorted.objects.RubikObjectList;
59
import org.distorted.screens.ScreenList;
60
import org.distorted.screens.RubikScreenPlay;
61
import org.distorted.tutorials.TutorialActivity;
62

    
63
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
public class RubikActivity extends AppCompatActivity
68
{
69
    public static final boolean SHOW_DOWNLOADED_DEBUG = false;
70

    
71
    public static final float PADDING             = 0.01f;
72
    public static final float SMALL_MARGIN        = 0.004f;
73
    public static final float MEDIUM_MARGIN       = 0.015f;
74
    public static final float LARGE_MARGIN        = 0.025f;
75
    public static final float BUTTON_TEXT_SIZE    = 0.05f;
76
    public static final float TITLE_TEXT_SIZE     = 0.06f;
77
    public static final float SOLVER_BMP_H_SIZE   = 0.11f;
78
    public static final float SOLVER_BMP_V_SIZE   = 0.06f;
79
    public static final float MENU_ITEM_SIZE      = 0.11f;
80
    public static final float PATTERN_GROUP_TEXT  = 0.03f;
81
    public static final float PATTERN_CHILD_TEXT  = 0.02f;
82
    public static final float SCORES_LEVEL_TEXT   = 0.035f;
83
    public static final float SCORES_ITEM_TEXT    = 0.030f;
84
    public static final float TUTORIAL_ITEM_TEXT  = 0.100f;
85
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
86
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
87
    public static final float MENU_MED_TEXT_SIZE  = 0.04f;
88
    public static final float MENU_SMALL_TEXT_SIZE= 0.035f;
89
    public static final float TAB_WIDTH           = 0.100f;
90
    public static final float TAB_HEIGHT          = 0.100f;
91

    
92
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
93
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
94
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
95
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
96
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
97

    
98
    private static final int ACTIVITY_NUMBER = 0;
99
    private static final float RATIO_BAR  = 0.10f;
100
    private static final float RATIO_INSET= 0.09f;
101

    
102
    private boolean mJustStarted;
103
    private FirebaseAnalytics mFirebaseAnalytics;
104
    private static int mScreenWidth, mScreenHeight;
105
    private boolean mPolicyAccepted, mIsChinese;
106
    private int mCurrentApiVersion;
107
    private int mHeightUpperBar, mHeightLowerBar;
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
    @Override
112
    protected void onCreate(Bundle savedState)
113
      {
114
      super.onCreate(savedState);
115
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
116

    
117
      setTheme(R.style.MaterialThemeNoActionBar);
118
      setContentView(R.layout.main);
119

    
120
      mJustStarted = true;
121
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
122
      mIsChinese = localeIsChinese();
123

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// this does not include possible insets
138

    
139
    private void computeBarHeights()
140
      {
141
      float height = mScreenHeight;
142
      int barHeight = (int)(height*RATIO_BAR);
143
      mHeightLowerBar = barHeight;
144
      mHeightUpperBar = barHeight;
145

    
146
      LinearLayout layoutTop = findViewById(R.id.upperBar);
147
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
148

    
149
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
150
      paramsTop.height = mHeightUpperBar;
151
      layoutTop.setLayoutParams(paramsTop);
152
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
153
      paramsBot.height = mHeightLowerBar;
154
      layoutBot.setLayoutParams(paramsBot);
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    private void hideNavigationBar()
160
      {
161
      mCurrentApiVersion = Build.VERSION.SDK_INT;
162

    
163
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
164
        {
165
        final View decorView = getWindow().getDecorView();
166

    
167
        decorView.setSystemUiVisibility(FLAGS);
168

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    @Override
186
    public void onAttachedToWindow()
187
      {
188
      super.onAttachedToWindow();
189

    
190
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
191
        {
192
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
193
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
194

    
195
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
196
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
197
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
198
        layoutHid.setLayoutParams(paramsHid);
199
        mHeightUpperBar += paramsHid.height;
200
        }
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// do not avoid cutouts
205

    
206
    private void cutoutHack()
207
      {
208
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
209
        {
210
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
211
        }
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    @Override
217
    public void onWindowFocusChanged(boolean hasFocus)
218
      {
219
      super.onWindowFocusChanged(hasFocus);
220

    
221
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
222
        {
223
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
224
        }
225
      }
226

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
    
242
    @Override
243
    protected void onResume() 
244
      {
245
      super.onResume();
246
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
247
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
248
      view.onResume();
249
      restorePreferences();
250

    
251
      ScreenList.setScreen(this);
252
      unblockEverything();
253

    
254
      if( mJustStarted )
255
        {
256
        mJustStarted = false;
257
        RubikScores scores = RubikScores.getInstance();
258
        scores.incrementNumRuns();
259
        scores.setCountry(this);
260
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
261
        RubikObjectList.restoreMeshState(preferences);
262
        }
263

    
264
      int object = RubikObjectList.getCurrObject();
265
      changeIfDifferent(object,view.getObjectControl());
266

    
267
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
268
      }
269
    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
    
272
    @Override
273
    protected void onDestroy() 
274
      {
275
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
276
      super.onDestroy();
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    private String getAppVers()
282
      {
283
      try
284
        {
285
        PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
286
        return pInfo.versionName;
287
        }
288
      catch (PackageManager.NameNotFoundException e)
289
        {
290
        return "unknown";
291
        }
292
      }
293

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

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

    
301
      editor.putBoolean("policyAccepted", mPolicyAccepted);
302
      editor.putString("appVersion", getAppVers() );
303

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

    
309
      for (int i = 0; i< ScreenList.LENGTH; i++)
310
        {
311
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
312
        }
313

    
314
      RubikObjectList.savePreferences(editor);
315
      RubikObjectList.saveMeshState(editor);
316
      ScreenList.savePreferences(editor);
317
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
318
      view.getObjectControl().savePreferences(editor);
319

    
320
      editor.apply();
321
      }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
    private void restorePreferences()
326
      {
327
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
328

    
329
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
330
      String oldVersion = preferences.getString("appVersion","");
331

    
332
      RubikObjectList.restorePreferences(preferences);
333

    
334
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
335
        {
336
        BaseEffect.Type.getType(i).restorePreferences(preferences);
337
        }
338

    
339
      for (int i = 0; i< ScreenList.LENGTH; i++)
340
        {
341
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
342
        }
343

    
344
      ScreenList.restorePreferences(preferences);
345
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
346

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

    
356
      if( !oldVersion.equals("") )
357
        {
358
        view.getObjectControl().restorePreferences(preferences);
359
        }
360

    
361
      RubikScores scores = RubikScores.getInstance();
362

    
363
      if( scores.isVerified() )
364
        {
365
        FirebaseAnalytics analytics = getAnalytics();
366
        analytics.setUserId(scores.getName());
367
        }
368
      }
369

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

    
372
    private void PrivacyPolicy()
373
      {
374
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
375
      priDiag.show(getSupportFragmentManager(), null);
376
      }
377

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

    
380
    void OpenGLError()
381
      {
382
      RubikDialogError errDiag = new RubikDialogError();
383
      errDiag.show(getSupportFragmentManager(), null);
384
      }
385

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

    
388
    void setScreenSize(int width, int height)
389
      {
390
      mScreenWidth = width;
391
      mScreenHeight= height;
392
      }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
// PUBLIC API
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
    public FirebaseAnalytics getAnalytics()
399
      {
400
      return mFirebaseAnalytics;
401
      }
402

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

    
405
    public int getHeightUpperBar()
406
      {
407
      return mHeightUpperBar;
408
      }
409

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

    
412
    public int getHeightLowerBar()
413
      {
414
      return mHeightLowerBar;
415
      }
416

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

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

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

    
427
    public ObjectControl getControl()
428
      {
429
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
430
      return view.getObjectControl();
431
      }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
    public int getScreenWidthInPixels()
436
      {
437
      return mScreenWidth;
438
      }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
    public int getScreenHeightInPixels()
443
      {
444
      return mScreenHeight;
445
      }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
    public void changeObject(int newObject, boolean reportChange)
450
      {
451
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
452
      ObjectControl control = view.getObjectControl();
453
      TwistyObject oldObject = control.getObject();
454
      changeIfDifferent(newObject,control);
455

    
456
      if( reportChange && oldObject!=null )
457
        {
458
        RubikObject robject = RubikObjectList.getObject(newObject);
459
        String newName = robject==null ? "NULL" : robject.getUpperName();
460
        float fps = view.getRenderer().getFPS();
461
        fps = (int)(fps+0.5f);
462
        StringBuilder name = new StringBuilder();
463
        name.append(oldObject.getShortName());
464
        name.append(' ');
465
        name.append(fps);
466
        name.append(" --> ");
467
        name.append(newName);
468

    
469
        if( BuildConfig.DEBUG )
470
          {
471
          android.util.Log.e("rubik", name.toString());
472
          }
473
        else
474
          {
475
          FirebaseAnalytics analytics = getAnalytics();
476

    
477
          if( analytics!=null )
478
            {
479
            Bundle bundle = new Bundle();
480
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
481
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
482
            }
483
          }
484
        }
485
      }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
    public void changeIfDifferent(int ordinal, ObjectControl control)
490
      {
491
      RubikObject object = RubikObjectList.getObject(ordinal);
492
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
493
      int iconMode  = TwistyObject.MODE_NORM;
494
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
495
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
496
      String name = object==null ? "NULL" : object.getUpperName();
497

    
498
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
499
      }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
    public static int getDrawableSize()
504
      {
505
      if( mScreenHeight<1000 )
506
        {
507
        return 0;
508
        }
509
      if( mScreenHeight<1600 )
510
        {
511
        return 1;
512
        }
513
      if( mScreenHeight<1900 )
514
        {
515
        return 2;
516
        }
517

    
518
      return 3;
519
      }
520

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

    
523
    public static int getDrawable(int small, int medium, int big, int huge)
524
      {
525
      int size = getDrawableSize();
526

    
527
      switch(size)
528
        {
529
        case 0 : return small;
530
        case 1 : return medium;
531
        case 2 : return big;
532
        default: return huge;
533
        }
534
      }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
    public void acceptPrivacy()
539
      {
540
      mPolicyAccepted = true;
541
      }
542

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

    
545
    public void declinePrivacy()
546
      {
547
      finish();
548
      }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
    public static boolean localeIsChinese()
553
      {
554
      String language;
555

    
556
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
557
        {
558
        language= LocaleList.getDefault().get(0).getLanguage();
559
        }
560
      else
561
        {
562
        language= Locale.getDefault().getLanguage();
563
        }
564

    
565
      return language.equals("zh");
566
      }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

    
570
    public boolean isVertical()
571
      {
572
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
573
      return view.isVertical();
574
      }
575

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577

    
578
    public void blockEverything(int place)
579
      {
580
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
581
      ObjectControl control = view.getObjectControl();
582
      control.blockEverything(place);
583

    
584
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
585
      play.setLockState(this);
586
      }
587

    
588
///////////////////////////////////////////////////////////////////////////////////////////////////
589

    
590
    public void unblockEverything()
591
      {
592
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
593
      ObjectControl control = view.getObjectControl();
594
      control.unblockEverything();
595

    
596
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
597
      play.setLockState(this);
598
      }
599

    
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601

    
602
    public void switchTutorial(String url, int objectOrdinal)
603
      {
604
      Intent intent = new Intent(this, TutorialActivity.class);
605
      intent.putExtra("url", url);
606
      intent.putExtra("obj", objectOrdinal);
607
      startActivity(intent);
608
      }
609

    
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611

    
612
    public void switchConfig(int objectOrdinal)
613
      {
614
      Intent intent = new Intent(this, ConfigActivity.class);
615
      intent.putExtra("obj", objectOrdinal);
616
      startActivity(intent);
617
      }
618

    
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620

    
621
    public void switchToBandagedCreator()
622
      {
623
      Intent intent = new Intent(this, BandagedCreatorActivity.class);
624
      startActivity(intent);
625
      }
626
}
(1-1/4)