Project

General

Profile

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

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

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.effects.BaseEffect;
53

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

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

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

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

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

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

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

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

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

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

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

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

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

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

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

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

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

    
164
        decorView.setSystemUiVisibility(FLAGS);
165

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

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

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

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

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
// do not avoid cutouts
202

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

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

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

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

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

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

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

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

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

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

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

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

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

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

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

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

    
314
      editor.apply();
315
      }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

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

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

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

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

    
336
      ScreenList.restorePreferences(preferences);
337

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

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

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

    
354
      RubikScores scores = RubikScores.getInstance();
355

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

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

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

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

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

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388
// PUBLIC API
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

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

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

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

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

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

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

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

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

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

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

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

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

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

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

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

    
449
      if( reportChange && oldObject!=null )
450
        {
451
        RubikObject robject = RubikObjectList.getObject(newObject);
452
        String newName = robject==null ? "NULL" : robject.getName();
453
        float fps = view.getRenderer().getFPS();
454
        fps = (int)(fps+0.5f);
455
        StringBuilder name = new StringBuilder();
456
        name.append(oldObject.getShortName());
457
        name.append(' ');
458
        name.append(fps);
459
        name.append(" --> ");
460
        name.append(newName);
461

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

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

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
    public void changeIfDifferent(int ordinal, ObjectControl control)
483
      {
484
      InputStream jsonStream = ObjectJson.getStream(this,ordinal);
485
      InputStream meshStream = ObjectMesh.getStream(this,ordinal);
486
      control.changeIfDifferent(ordinal,jsonStream,meshStream);
487
      }
488

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

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

    
506
      return 3;
507
      }
508

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510

    
511
    public static int getDrawable(int small, int medium, int big, int huge)
512
      {
513
      int size = getDrawableSize();
514

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

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
    public void acceptPrivacy()
527
      {
528
      mPolicyAccepted = true;
529
      }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
    public void declinePrivacy()
534
      {
535
      finish();
536
      }
537

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

    
540
    public static boolean localeIsChinese()
541
      {
542
      String language;
543

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

    
553
      return language.equals("zh");
554
      }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
    public boolean isVertical()
559
      {
560
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
561
      return view.isVertical();
562
      }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
    public void blockEverything(int place)
567
      {
568
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
569
      ObjectControl control = view.getObjectControl();
570
      control.blockEverything(place);
571

    
572
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
573
      play.setLockState(this);
574
      }
575

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

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

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

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

    
590
    public void switchTutorial(String url, int objectOrdinal)
591
      {
592
      Intent myIntent = new Intent(this, TutorialActivity.class);
593
      myIntent.putExtra("url", url);
594
      myIntent.putExtra("obj", objectOrdinal);
595
      startActivity(myIntent);
596
      }
597

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599

    
600
    public void switchConfig(int objectOrdinal)
601
      {
602
      Intent myIntent = new Intent(this, ConfigActivity.class);
603
      myIntent.putExtra("obj", objectOrdinal);
604
      startActivity(myIntent);
605
      }
606
}
(1-1/4)