Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 33f6abfd

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.util.Locale;
23

    
24
import android.content.Intent;
25
import android.content.SharedPreferences;
26
import android.os.Build;
27
import android.os.Bundle;
28
import android.os.LocaleList;
29
import android.preference.PreferenceManager;
30

    
31
import android.util.DisplayMetrics;
32
import android.view.DisplayCutout;
33
import android.view.View;
34
import android.view.ViewGroup;
35
import android.view.WindowManager;
36
import android.widget.LinearLayout;
37

    
38
import androidx.appcompat.app.AppCompatActivity;
39

    
40
import com.google.firebase.analytics.FirebaseAnalytics;
41

    
42
import org.distorted.library.main.DistortedLibrary;
43
import org.distorted.library.main.DistortedScreen;
44

    
45
import org.distorted.objectlib.main.ObjectControl;
46
import org.distorted.objectlib.main.TwistyObject;
47
import org.distorted.objectlib.main.ObjectType;
48
import org.distorted.objectlib.effects.BaseEffect;
49

    
50
import org.distorted.dialogs.RubikDialogError;
51
import org.distorted.dialogs.RubikDialogPrivacy;
52
import org.distorted.network.RubikScores;
53
import org.distorted.network.RubikNetwork;
54
import org.distorted.screens.ScreenList;
55
import org.distorted.screens.RubikScreenPlay;
56
import org.distorted.tutorials.TutorialActivity;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
80
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
81
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
82
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
83
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
84
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
85

    
86
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
87
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
88

    
89
    private static final float RATIO_BAR  = 0.10f;
90
    private static final float RATIO_INSET= 0.08f;
91

    
92
    private boolean mJustStarted;
93
    private FirebaseAnalytics mFirebaseAnalytics;
94
    private static int mScreenWidth, mScreenHeight;
95
    private boolean mPolicyAccepted, mIsChinese;
96
    private int mCurrentApiVersion;
97
    private int mHeightUpperBar, mHeightLowerBar;
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    @Override
102
    protected void onCreate(Bundle savedState)
103
      {
104
      super.onCreate(savedState);
105
      DistortedLibrary.onCreate(0);
106

    
107
      setTheme(R.style.CustomActivityThemeNoActionBar);
108
      setContentView(R.layout.main);
109

    
110
      mJustStarted = true;
111
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
112

    
113
      DisplayMetrics displaymetrics = new DisplayMetrics();
114
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
115
      mScreenWidth =displaymetrics.widthPixels;
116
      mScreenHeight=displaymetrics.heightPixels;
117

    
118
      mIsChinese = localeIsChinese();
119

    
120
      hideNavigationBar();
121
      cutoutHack();
122
      computeBarHeights();
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// this does not include possible insets
127

    
128
    private void computeBarHeights()
129
      {
130
      float height = getScreenHeightInPixels();
131
      int barHeight = (int)(height*RATIO_BAR);
132
      mHeightLowerBar = barHeight;
133
      mHeightUpperBar = barHeight;
134

    
135
      LinearLayout layoutTop = findViewById(R.id.upperBar);
136
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
137

    
138
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
139
      paramsTop.height = mHeightUpperBar;
140
      layoutTop.setLayoutParams(paramsTop);
141
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
142
      paramsBot.height = mHeightLowerBar;
143
      layoutBot.setLayoutParams(paramsBot);
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    private void hideNavigationBar()
149
      {
150
      mCurrentApiVersion = Build.VERSION.SDK_INT;
151

    
152
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
153
        {
154
        final View decorView = getWindow().getDecorView();
155

    
156
        decorView.setSystemUiVisibility(FLAGS);
157

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    @Override
175
    public void onAttachedToWindow()
176
      {
177
      super.onAttachedToWindow();
178

    
179
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
180
        {
181
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
182
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
183

    
184
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
185
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
186
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
187
        layoutHid.setLayoutParams(paramsHid);
188
        mHeightUpperBar += paramsHid.height;
189
        }
190
      }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// do not avoid cutouts
194

    
195
    private void cutoutHack()
196
      {
197
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
198
        {
199
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
200
        }
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    @Override
206
    public void onWindowFocusChanged(boolean hasFocus)
207
      {
208
      super.onWindowFocusChanged(hasFocus);
209

    
210
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
211
        {
212
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
213
        }
214
      }
215

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

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

    
242
      if( mJustStarted )
243
        {
244
        mJustStarted = false;
245
        RubikScores scores = RubikScores.getInstance();
246
        scores.incrementNumRuns();
247
        scores.setCountry(this);
248
        }
249

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

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    private void savePreferences()
269
      {
270
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
271
      SharedPreferences.Editor editor = preferences.edit();
272

    
273
      editor.putBoolean("policyAccepted", mPolicyAccepted);
274

    
275
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
276
        {
277
        BaseEffect.Type.getType(i).savePreferences(editor);
278
        }
279

    
280
      for (int i = 0; i< ScreenList.LENGTH; i++)
281
        {
282
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
283
        }
284

    
285
      ScreenList.savePreferences(editor);
286
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
287
      view.getObjectControl().savePreferences(editor);
288

    
289
      editor.apply();
290
      }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
    private void restorePreferences()
295
      {
296
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
297

    
298
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
299

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

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

    
310
      ScreenList.restorePreferences(preferences);
311

    
312
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
313
      view.getObjectControl().restorePreferences(preferences);
314

    
315
      RubikScores scores = RubikScores.getInstance();
316

    
317
      if( scores.isVerified() )
318
        {
319
        FirebaseAnalytics analytics = getAnalytics();
320
        analytics.setUserId(scores.getName());
321
        }
322
      }
323

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

    
326
    private void PrivacyPolicy()
327
      {
328
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
329
      priDiag.show(getSupportFragmentManager(), null);
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    void OpenGLError()
335
      {
336
      RubikDialogError errDiag = new RubikDialogError();
337
      errDiag.show(getSupportFragmentManager(), null);
338
      }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
// PUBLIC API
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
    public FirebaseAnalytics getAnalytics()
345
      {
346
      return mFirebaseAnalytics;
347
      }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
    public int getHeightUpperBar()
352
      {
353
      return mHeightUpperBar;
354
      }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
    public int getHeightLowerBar()
359
      {
360
      return mHeightLowerBar;
361
      }
362

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

    
365
    public TwistyObject getObject()
366
      {
367
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
368
      return view.getObjectControl().getObject();
369
      }
370

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

    
373
    public ObjectControl getControl()
374
      {
375
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
376
      return view.getObjectControl();
377
      }
378

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

    
381
    public DistortedScreen getScreen()
382
      {
383
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
384
      RubikRenderer renderer = view.getRenderer();
385
      return renderer.getScreen();
386
      }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
    public int getScreenWidthInPixels()
391
      {
392
      return mScreenWidth;
393
      }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
    public int getScreenHeightInPixels()
398
      {
399
      return mScreenHeight;
400
      }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
    public void changeObject(ObjectType newObject, boolean reportChange)
405
      {
406
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
407
      ObjectControl control = view.getObjectControl();
408

    
409
      if( reportChange )
410
        {
411
        TwistyObject oldObject = control.getObject();
412

    
413
        if( oldObject!=null )
414
          {
415
          ObjectType oldType = oldObject.getObjectType();
416
          float fps = view.getRenderer().getFPS();
417
          fps = (int)(fps+0.5f);
418
          StringBuilder name = new StringBuilder();
419
          name.append(oldType.name());
420
          name.append(' ');
421
          name.append(fps);
422
          name.append(" --> ");
423
          name.append(newObject.name());
424

    
425
          if( BuildConfig.DEBUG )
426
            {
427
            android.util.Log.e("rubik", name.toString());
428
            }
429
          else
430
            {
431
            FirebaseAnalytics analytics = getAnalytics();
432

    
433
            if( analytics!=null )
434
              {
435
              Bundle bundle = new Bundle();
436
              bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
437
              analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
438
              }
439
            }
440
          }
441
        }
442

    
443
      control.changeObject(newObject);
444
      }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
    public static int getDrawableSize()
449
      {
450
      if( mScreenHeight<1000 )
451
        {
452
        return 0;
453
        }
454
      if( mScreenHeight<1600 )
455
        {
456
        return 1;
457
        }
458
      if( mScreenHeight<1900 )
459
        {
460
        return 2;
461
        }
462

    
463
      return 3;
464
      }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
    public static int getDrawable(int small, int medium, int big, int huge)
469
      {
470
      int size = getDrawableSize();
471

    
472
      switch(size)
473
        {
474
        case 0 : return small;
475
        case 1 : return medium;
476
        case 2 : return big;
477
        default: return huge;
478
        }
479
      }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
    public void acceptPrivacy()
484
      {
485
      mPolicyAccepted = true;
486
      }
487

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

    
490
    public void declinePrivacy()
491
      {
492
      finish();
493
      }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
    public static boolean localeIsChinese()
498
      {
499
      String language;
500

    
501
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
502
        {
503
        language= LocaleList.getDefault().get(0).getLanguage();
504
        }
505
      else
506
        {
507
        language= Locale.getDefault().getLanguage();
508
        }
509

    
510
      return language.equals("zh");
511
      }
512

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

    
515
    public boolean isVertical()
516
      {
517
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
518
      return view.isVertical();
519
      }
520

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

    
523
    public void blockEverything(int place)
524
      {
525
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
526
      ObjectControl control = view.getObjectControl();
527
      control.blockEverything(place);
528

    
529
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
530
      play.setLockState(this);
531
      }
532

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

    
535
    public void unblockEverything()
536
      {
537
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
538
      ObjectControl control = view.getObjectControl();
539
      control.unblockEverything();
540

    
541
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
542
      play.setLockState(this);
543
      }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
    public void switchTutorial(String url, ObjectType object)
548
      {
549
      Intent myIntent = new Intent(this, TutorialActivity.class);
550
      myIntent.putExtra("url", url);
551
      myIntent.putExtra("obj", object.ordinal());
552
      startActivity(myIntent);
553
      }
554
}
(1-1/4)