Project

General

Profile

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

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

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
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
258
        RubikObjectList.restoreMeshState(preferences);
259
        }
260

    
261
      int object = RubikObjectList.getCurrObject();
262
      changeIfDifferent(object,view.getObjectControl());
263

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

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

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

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

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

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

    
311
      RubikObjectList.savePreferences(editor);
312
      RubikObjectList.saveMeshState(editor);
313
      ScreenList.savePreferences(editor);
314
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
315
      view.getObjectControl().savePreferences(editor);
316

    
317
      editor.apply();
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    private void restorePreferences()
323
      {
324
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
325

    
326
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
327
      String oldVersion = preferences.getString("appVersion","");
328

    
329
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
330
        {
331
        BaseEffect.Type.getType(i).restorePreferences(preferences);
332
        }
333

    
334
      for (int i = 0; i< ScreenList.LENGTH; i++)
335
        {
336
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
337
        }
338

    
339
      RubikObjectList.restorePreferences(preferences);
340
      ScreenList.restorePreferences(preferences);
341

    
342
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
343

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

    
353
      if( !oldVersion.equals("") )
354
        {
355
        view.getObjectControl().restorePreferences(preferences);
356
        }
357

    
358
      RubikScores scores = RubikScores.getInstance();
359

    
360
      if( scores.isVerified() )
361
        {
362
        FirebaseAnalytics analytics = getAnalytics();
363
        analytics.setUserId(scores.getName());
364
        }
365
      }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
    private void PrivacyPolicy()
370
      {
371
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
372
      priDiag.show(getSupportFragmentManager(), null);
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    void OpenGLError()
378
      {
379
      RubikDialogError errDiag = new RubikDialogError();
380
      errDiag.show(getSupportFragmentManager(), null);
381
      }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
    void setScreenSize(int width, int height)
386
      {
387
      mScreenWidth = width;
388
      mScreenHeight= height;
389
      }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392
// PUBLIC API
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
    public FirebaseAnalytics getAnalytics()
396
      {
397
      return mFirebaseAnalytics;
398
      }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
    public int getHeightUpperBar()
403
      {
404
      return mHeightUpperBar;
405
      }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
    public int getHeightLowerBar()
410
      {
411
      return mHeightLowerBar;
412
      }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
    public TwistyObject getObject()
417
      {
418
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
419
      return view.getObjectControl().getObject();
420
      }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
    public ObjectControl getControl()
425
      {
426
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
427
      return view.getObjectControl();
428
      }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
    public int getScreenWidthInPixels()
433
      {
434
      return mScreenWidth;
435
      }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
    public int getScreenHeightInPixels()
440
      {
441
      return mScreenHeight;
442
      }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

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

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

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

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

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
    public void changeIfDifferent(int ordinal, ObjectControl control)
487
      {
488
      InputStream jsonStream = ObjectJson.getStream(this,ordinal);
489
      InputStream meshStream = ObjectMesh.getStream(this,ordinal);
490
      control.changeIfDifferent(ordinal,jsonStream,meshStream);
491
      }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
    public static int getDrawableSize()
496
      {
497
      if( mScreenHeight<1000 )
498
        {
499
        return 0;
500
        }
501
      if( mScreenHeight<1600 )
502
        {
503
        return 1;
504
        }
505
      if( mScreenHeight<1900 )
506
        {
507
        return 2;
508
        }
509

    
510
      return 3;
511
      }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
    public static int getDrawable(int small, int medium, int big, int huge)
516
      {
517
      int size = getDrawableSize();
518

    
519
      switch(size)
520
        {
521
        case 0 : return small;
522
        case 1 : return medium;
523
        case 2 : return big;
524
        default: return huge;
525
        }
526
      }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
    public void acceptPrivacy()
531
      {
532
      mPolicyAccepted = true;
533
      }
534

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

    
537
    public void declinePrivacy()
538
      {
539
      finish();
540
      }
541

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

    
544
    public static boolean localeIsChinese()
545
      {
546
      String language;
547

    
548
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
549
        {
550
        language= LocaleList.getDefault().get(0).getLanguage();
551
        }
552
      else
553
        {
554
        language= Locale.getDefault().getLanguage();
555
        }
556

    
557
      return language.equals("zh");
558
      }
559

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561

    
562
    public boolean isVertical()
563
      {
564
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
565
      return view.isVertical();
566
      }
567

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

    
570
    public void blockEverything(int place)
571
      {
572
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
573
      ObjectControl control = view.getObjectControl();
574
      control.blockEverything(place);
575

    
576
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
577
      play.setLockState(this);
578
      }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581

    
582
    public void unblockEverything()
583
      {
584
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
585
      ObjectControl control = view.getObjectControl();
586
      control.unblockEverything();
587

    
588
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
589
      play.setLockState(this);
590
      }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

    
594
    public void switchTutorial(String url, int objectOrdinal)
595
      {
596
      Intent myIntent = new Intent(this, TutorialActivity.class);
597
      myIntent.putExtra("url", url);
598
      myIntent.putExtra("obj", objectOrdinal);
599
      startActivity(myIntent);
600
      }
601

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

    
604
    public void switchConfig(int objectOrdinal)
605
      {
606
      Intent myIntent = new Intent(this, ConfigActivity.class);
607
      myIntent.putExtra("obj", objectOrdinal);
608
      startActivity(myIntent);
609
      }
610
}
(1-1/4)