Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 6e3fcb91

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
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

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

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

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

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

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

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

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

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

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

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

    
168
        decorView.setSystemUiVisibility(FLAGS);
169

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

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

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// do not avoid cutouts
206

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

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

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

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

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

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

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

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

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

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

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

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

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

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

    
321
      editor.apply();
322
      }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

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

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

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

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

    
343
      RubikObjectList.restorePreferences(preferences);
344
      ScreenList.restorePreferences(preferences);
345

    
346
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
347

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

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

    
362
      RubikScores scores = RubikScores.getInstance();
363

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

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

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

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

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

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

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
// PUBLIC API
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

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

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

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

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

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

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

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

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

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

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

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

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

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

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

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

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

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

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

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
    public void changeIfDifferent(int ordinal, ObjectControl control)
491
      {
492
      RubikObject object = RubikObjectList.getObject(ordinal);
493
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
494

    
495
      InputStream jsonStream = ObjectJson.getStream(this,ordinal);
496
      InputStream meshStream = ObjectMesh.getStream(this,ordinal);
497
      control.changeIfDifferent(ordinal,meshState,jsonStream,meshStream);
498
      }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

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

    
517
      return 3;
518
      }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

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

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

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

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

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543

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

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

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

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

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

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568

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

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

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

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

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588

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

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

    
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600

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

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610

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