Project

General

Profile

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

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

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
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
99
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
100

    
101
    private static final int ACTIVITY_NUMBER = 0;
102
    private static final float RATIO_BAR  = 0.10f;
103
    private static final float RATIO_INSET= 0.09f;
104

    
105
    private boolean mJustStarted;
106
    private FirebaseAnalytics mFirebaseAnalytics;
107
    private static int mScreenWidth, mScreenHeight;
108
    private boolean mPolicyAccepted, mIsChinese;
109
    private int mCurrentApiVersion;
110
    private int mHeightUpperBar, mHeightLowerBar;
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    @Override
115
    protected void onCreate(Bundle savedState)
116
      {
117
      super.onCreate(savedState);
118
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
119

    
120
      setTheme(R.style.MaterialThemeNoActionBar);
121
      setContentView(R.layout.main);
122

    
123
      mJustStarted = true;
124
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
125
      mIsChinese = localeIsChinese();
126

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
// this does not include possible insets
141

    
142
    private void computeBarHeights()
143
      {
144
      float height = mScreenHeight;
145
      int barHeight = (int)(height*RATIO_BAR);
146
      mHeightLowerBar = barHeight;
147
      mHeightUpperBar = barHeight;
148

    
149
      LinearLayout layoutTop = findViewById(R.id.upperBar);
150
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
151

    
152
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
153
      paramsTop.height = mHeightUpperBar;
154
      layoutTop.setLayoutParams(paramsTop);
155
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
156
      paramsBot.height = mHeightLowerBar;
157
      layoutBot.setLayoutParams(paramsBot);
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
    private void hideNavigationBar()
163
      {
164
      mCurrentApiVersion = Build.VERSION.SDK_INT;
165

    
166
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
167
        {
168
        final View decorView = getWindow().getDecorView();
169

    
170
        decorView.setSystemUiVisibility(FLAGS);
171

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
    @Override
189
    public void onAttachedToWindow()
190
      {
191
      super.onAttachedToWindow();
192

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

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
// do not avoid cutouts
208

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

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    @Override
220
    public void onWindowFocusChanged(boolean hasFocus)
221
      {
222
      super.onWindowFocusChanged(hasFocus);
223

    
224
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
225
        {
226
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
227
        }
228
      }
229

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

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

    
254
      ScreenList.setScreen(this);
255
      unblockEverything();
256

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

    
267
      int object = RubikObjectList.getCurrObject();
268
      changeIfDifferent(object,view.getObjectControl());
269

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

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

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

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
    private void savePreferences()
300
      {
301
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
302
      SharedPreferences.Editor editor = preferences.edit();
303

    
304
      editor.putBoolean("policyAccepted", mPolicyAccepted);
305
      editor.putString("appVersion", getAppVers() );
306

    
307
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
308
        {
309
        BaseEffect.Type.getType(i).savePreferences(editor);
310
        }
311

    
312
      for (int i = 0; i< ScreenList.LENGTH; i++)
313
        {
314
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
315
        }
316

    
317
      RubikObjectList.savePreferences(editor);
318
      RubikObjectList.saveMeshState(editor);
319
      ScreenList.savePreferences(editor);
320
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
321
      view.getObjectControl().savePreferences(editor);
322

    
323
      editor.apply();
324
      }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
    private void restorePreferences()
329
      {
330
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
331

    
332
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
333
      String oldVersion = preferences.getString("appVersion","");
334

    
335
      RubikObjectList.restorePreferences(preferences);
336

    
337
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
338
        {
339
        BaseEffect.Type.getType(i).restorePreferences(preferences);
340
        }
341

    
342
      for (int i = 0; i< ScreenList.LENGTH; i++)
343
        {
344
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
345
        }
346

    
347
      ScreenList.restorePreferences(preferences);
348
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
349

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

    
359
      if( !oldVersion.equals("") )
360
        {
361
        view.getObjectControl().restorePreferences(preferences);
362
        }
363

    
364
      RubikScores scores = RubikScores.getInstance();
365

    
366
      if( scores.isVerified() )
367
        {
368
        FirebaseAnalytics analytics = getAnalytics();
369
        analytics.setUserId(scores.getName());
370
        }
371
      }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
    private void PrivacyPolicy()
376
      {
377
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
378
      priDiag.show(getSupportFragmentManager(), null);
379
      }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
    void OpenGLError()
384
      {
385
      RubikDialogError errDiag = new RubikDialogError();
386
      errDiag.show(getSupportFragmentManager(), null);
387
      }
388

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

    
391
    void setScreenSize(int width, int height)
392
      {
393
      mScreenWidth = width;
394
      mScreenHeight= height;
395
      }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
// PUBLIC API
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
    public FirebaseAnalytics getAnalytics()
402
      {
403
      return mFirebaseAnalytics;
404
      }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
    public int getHeightUpperBar()
409
      {
410
      return mHeightUpperBar;
411
      }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
    public int getHeightLowerBar()
416
      {
417
      return mHeightLowerBar;
418
      }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
    public TwistyObject getObject()
423
      {
424
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
425
      return view.getObjectControl().getObject();
426
      }
427

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

    
430
    public ObjectControl getControl()
431
      {
432
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
433
      return view.getObjectControl();
434
      }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

    
438
    public int getScreenWidthInPixels()
439
      {
440
      return mScreenWidth;
441
      }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
    public int getScreenHeightInPixels()
446
      {
447
      return mScreenHeight;
448
      }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

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

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

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

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

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

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

    
500
      control.changeIfDifferent(ordinal,name,meshState,jsonStream,meshStream);
501
      }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

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

    
520
      return 3;
521
      }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

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

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

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

    
540
    public void acceptPrivacy()
541
      {
542
      mPolicyAccepted = true;
543
      }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
    public void declinePrivacy()
548
      {
549
      finish();
550
      }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553

    
554
    public static boolean localeIsChinese()
555
      {
556
      String language;
557

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

    
567
      return language.equals("zh");
568
      }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

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

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579

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

    
586
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
587
      play.setLockState(this);
588
      }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591

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

    
598
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
599
      play.setLockState(this);
600
      }
601

    
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603

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

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613

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

    
621
///////////////////////////////////////////////////////////////////////////////////////////////////
622

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