Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 526a5906

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.library.main.DistortedLibrary;
47

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

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

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

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

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

    
97
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
98
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
99

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

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

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

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

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

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

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

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

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

    
169
        decorView.setSystemUiVisibility(FLAGS);
170

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
// do not avoid cutouts
207

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

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

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

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

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

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

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

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

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

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

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

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

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

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

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

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

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

    
322
      editor.apply();
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

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

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

    
334
      RubikObjectList.restorePreferences(preferences);
335

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

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

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

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

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

    
363
      RubikScores scores = RubikScores.getInstance();
364

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

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

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

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

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

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

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

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397
// PUBLIC API
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

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

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

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

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

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

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

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

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

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

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

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

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

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

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

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

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

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

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

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

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

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

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

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

    
519
      return 3;
520
      }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

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

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

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

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

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

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

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

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

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

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

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

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

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

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

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

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

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

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

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

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

    
611
///////////////////////////////////////////////////////////////////////////////////////////////////
612

    
613
    public void switchConfig(int objectOrdinal)
614
      {
615
      Intent myIntent = new Intent(this, ConfigActivity.class);
616
      myIntent.putExtra("obj", objectOrdinal);
617
      startActivity(myIntent);
618
      }
619
}
(1-1/4)