Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 1237d25d

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.main.ObjectType;
53
import org.distorted.objectlib.effects.BaseEffect;
54

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

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

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

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

    
116
      mJustStarted = true;
117
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
118

    
119
      DisplayMetrics displaymetrics = new DisplayMetrics();
120
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
121
      mScreenWidth =displaymetrics.widthPixels;
122
      mScreenHeight=displaymetrics.heightPixels;
123

    
124
      mIsChinese = localeIsChinese();
125

    
126
      hideNavigationBar();
127
      cutoutHack();
128
      computeBarHeights();
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// this does not include possible insets
133

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

    
141
      LinearLayout layoutTop = findViewById(R.id.upperBar);
142
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
143

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
    private void hideNavigationBar()
155
      {
156
      mCurrentApiVersion = Build.VERSION.SDK_INT;
157

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

    
162
        decorView.setSystemUiVisibility(FLAGS);
163

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    @Override
181
    public void onAttachedToWindow()
182
      {
183
      super.onAttachedToWindow();
184

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

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
// do not avoid cutouts
200

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

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

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

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

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

    
248
      if( mJustStarted )
249
        {
250
        mJustStarted = false;
251
        RubikScores scores = RubikScores.getInstance();
252
        scores.incrementNumRuns();
253
        scores.setCountry(this);
254
        }
255

    
256
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
257
      ObjectType object = play.getObject();
258
      changeIfDifferent(object,view.getObjectControl());
259
      
260
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
261
      }
262
    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
    
265
    @Override
266
    protected void onDestroy() 
267
      {
268
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
269
      super.onDestroy();
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

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

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
    private void savePreferences()
290
      {
291
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
292
      SharedPreferences.Editor editor = preferences.edit();
293

    
294
      editor.putBoolean("policyAccepted", mPolicyAccepted);
295
      editor.putString("appVersion", getAppVers() );
296

    
297
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
298
        {
299
        BaseEffect.Type.getType(i).savePreferences(editor);
300
        }
301

    
302
      for (int i = 0; i< ScreenList.LENGTH; i++)
303
        {
304
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
305
        }
306

    
307
      ScreenList.savePreferences(editor);
308
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
309
      view.getObjectControl().savePreferences(editor);
310

    
311
      editor.apply();
312
      }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
    private void restorePreferences()
317
      {
318
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
319

    
320
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
321
      String oldVersion = preferences.getString("appVersion","");
322

    
323
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
324
        {
325
        BaseEffect.Type.getType(i).restorePreferences(preferences);
326
        }
327

    
328
      for (int i = 0; i< ScreenList.LENGTH; i++)
329
        {
330
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
331
        }
332

    
333
      ScreenList.restorePreferences(preferences);
334

    
335
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
336

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

    
346
      if( !oldVersion.equals("") )
347
        {
348
        view.getObjectControl().restorePreferences(preferences);
349
        }
350

    
351
      RubikScores scores = RubikScores.getInstance();
352

    
353
      if( scores.isVerified() )
354
        {
355
        FirebaseAnalytics analytics = getAnalytics();
356
        analytics.setUserId(scores.getName());
357
        }
358
      }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
    private void PrivacyPolicy()
363
      {
364
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
365
      priDiag.show(getSupportFragmentManager(), null);
366
      }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
    void OpenGLError()
371
      {
372
      RubikDialogError errDiag = new RubikDialogError();
373
      errDiag.show(getSupportFragmentManager(), null);
374
      }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
    void setScreenSize(int width, int height)
379
      {
380
      mScreenWidth = width;
381
      mScreenHeight= height;
382
      }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
// PUBLIC API
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
    public FirebaseAnalytics getAnalytics()
389
      {
390
      return mFirebaseAnalytics;
391
      }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
    public int getHeightUpperBar()
396
      {
397
      return mHeightUpperBar;
398
      }
399

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

    
402
    public int getHeightLowerBar()
403
      {
404
      return mHeightLowerBar;
405
      }
406

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

    
409
    public TwistyObject getObject()
410
      {
411
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
412
      return view.getObjectControl().getObject();
413
      }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
    public ObjectControl getControl()
418
      {
419
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
420
      return view.getObjectControl();
421
      }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
    public int getScreenWidthInPixels()
426
      {
427
      return mScreenWidth;
428
      }
429

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

    
432
    public int getScreenHeightInPixels()
433
      {
434
      return mScreenHeight;
435
      }
436

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

    
439
    public void changeObject(ObjectType newObject, boolean reportChange)
440
      {
441
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
442
      ObjectControl control = view.getObjectControl();
443
      TwistyObject oldObject = control.getObject();
444
      changeIfDifferent(newObject,control);
445

    
446
      if( reportChange && oldObject!=null )
447
        {
448
        float fps = view.getRenderer().getFPS();
449
        fps = (int)(fps+0.5f);
450
        StringBuilder name = new StringBuilder();
451
        name.append(oldObject.getShortName());
452
        name.append(' ');
453
        name.append(fps);
454
        name.append(" --> ");
455
        name.append(newObject.name());
456

    
457
        if( BuildConfig.DEBUG )
458
          {
459
          android.util.Log.e("rubik", name.toString());
460
          }
461
        else
462
          {
463
          FirebaseAnalytics analytics = getAnalytics();
464

    
465
          if( analytics!=null )
466
            {
467
            Bundle bundle = new Bundle();
468
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
469
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
470
            }
471
          }
472
        }
473
      }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
    public void changeIfDifferent(ObjectType type,ObjectControl control)
478
      {
479
      InputStream jsonStream = ObjectJson.getStream(type,this);
480
      InputStream meshStream = ObjectMesh.getStream(type,this);
481
      control.changeIfDifferent(type.ordinal(),jsonStream,meshStream);
482
      }
483

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

    
486
    public static int getDrawableSize()
487
      {
488
      if( mScreenHeight<1000 )
489
        {
490
        return 0;
491
        }
492
      if( mScreenHeight<1600 )
493
        {
494
        return 1;
495
        }
496
      if( mScreenHeight<1900 )
497
        {
498
        return 2;
499
        }
500

    
501
      return 3;
502
      }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
    public static int getDrawable(int small, int medium, int big, int huge)
507
      {
508
      int size = getDrawableSize();
509

    
510
      switch(size)
511
        {
512
        case 0 : return small;
513
        case 1 : return medium;
514
        case 2 : return big;
515
        default: return huge;
516
        }
517
      }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
    public void acceptPrivacy()
522
      {
523
      mPolicyAccepted = true;
524
      }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
    public void declinePrivacy()
529
      {
530
      finish();
531
      }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534

    
535
    public static boolean localeIsChinese()
536
      {
537
      String language;
538

    
539
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
540
        {
541
        language= LocaleList.getDefault().get(0).getLanguage();
542
        }
543
      else
544
        {
545
        language= Locale.getDefault().getLanguage();
546
        }
547

    
548
      return language.equals("zh");
549
      }
550

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

    
553
    public boolean isVertical()
554
      {
555
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
556
      return view.isVertical();
557
      }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

    
561
    public void blockEverything(int place)
562
      {
563
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
564
      ObjectControl control = view.getObjectControl();
565
      control.blockEverything(place);
566

    
567
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
568
      play.setLockState(this);
569
      }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
    public void unblockEverything()
574
      {
575
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
576
      ObjectControl control = view.getObjectControl();
577
      control.unblockEverything();
578

    
579
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
580
      play.setLockState(this);
581
      }
582

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

    
585
    public void switchTutorial(String url, ObjectType object)
586
      {
587
      Intent myIntent = new Intent(this, TutorialActivity.class);
588
      myIntent.putExtra("url", url);
589
      myIntent.putExtra("obj", object.ordinal());
590
      startActivity(myIntent);
591
      }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
    public void switchConfig(ObjectType object)
596
      {
597
      Intent myIntent = new Intent(this, ConfigActivity.class);
598
      myIntent.putExtra("obj", object.ordinal());
599
      startActivity(myIntent);
600
      }
601
}
(1-1/4)