Project

General

Profile

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

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

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.dmesh.ObjectMesh;
46
import org.distorted.jsons.ObjectJson;
47
import org.distorted.library.main.DistortedLibrary;
48

    
49
import org.distorted.objectlib.main.ObjectControl;
50
import org.distorted.objectlib.main.TwistyObject;
51
import org.distorted.objectlib.main.ObjectType;
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.screens.ScreenList;
59
import org.distorted.screens.RubikScreenPlay;
60
import org.distorted.tutorials.TutorialActivity;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

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

    
93
    private static final float RATIO_BAR  = 0.10f;
94
    private static final float RATIO_INSET= 0.08f;
95

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
    @Override
106
    protected void onCreate(Bundle savedState)
107
      {
108
      super.onCreate(savedState);
109
      DistortedLibrary.onCreate(0);
110

    
111
      setTheme(R.style.MaterialThemeNoActionBar);
112
      setContentView(R.layout.main);
113

    
114
      mJustStarted = true;
115
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
116

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

    
122
      mIsChinese = localeIsChinese();
123

    
124
      hideNavigationBar();
125
      cutoutHack();
126
      computeBarHeights();
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// this does not include possible insets
131

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

    
139
      LinearLayout layoutTop = findViewById(R.id.upperBar);
140
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
141

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    private void hideNavigationBar()
153
      {
154
      mCurrentApiVersion = Build.VERSION.SDK_INT;
155

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

    
160
        decorView.setSystemUiVisibility(FLAGS);
161

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
    @Override
179
    public void onAttachedToWindow()
180
      {
181
      super.onAttachedToWindow();
182

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

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

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// do not avoid cutouts
198

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

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

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

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

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

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

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

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

    
292
      editor.putBoolean("policyAccepted", mPolicyAccepted);
293
      editor.putString("appVersion", getAppVers() );
294

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

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

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

    
309
      editor.apply();
310
      }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

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

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

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

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

    
331
      ScreenList.restorePreferences(preferences);
332

    
333
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
334

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

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

    
349
      RubikScores scores = RubikScores.getInstance();
350

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

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

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

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

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

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

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

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
// PUBLIC API
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
    public FirebaseAnalytics getAnalytics()
387
      {
388
      return mFirebaseAnalytics;
389
      }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
    public int getHeightUpperBar()
394
      {
395
      return mHeightUpperBar;
396
      }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
    public int getHeightLowerBar()
401
      {
402
      return mHeightLowerBar;
403
      }
404

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

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

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

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

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
    public int getScreenWidthInPixels()
424
      {
425
      return mScreenWidth;
426
      }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
    public int getScreenHeightInPixels()
431
      {
432
      return mScreenHeight;
433
      }
434

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

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

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

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

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

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

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

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

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

    
499
      return 3;
500
      }
501

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

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

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

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
    public void acceptPrivacy()
520
      {
521
      mPolicyAccepted = true;
522
      }
523

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

    
526
    public void declinePrivacy()
527
      {
528
      finish();
529
      }
530

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

    
533
    public static boolean localeIsChinese()
534
      {
535
      String language;
536

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

    
546
      return language.equals("zh");
547
      }
548

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

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

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

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

    
565
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
566
      play.setLockState(this);
567
      }
568

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

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

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

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582

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