Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 400ff34d

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
        }
258

    
259
      int object = RubikObjectList.getCurrObject();
260
      changeIfDifferent(object,view.getObjectControl());
261

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

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

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

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

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

    
296
      editor.putBoolean("policyAccepted", mPolicyAccepted);
297
      editor.putString("appVersion", getAppVers() );
298

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

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

    
309
      RubikObjectList.savePreferences(editor);
310
      ScreenList.savePreferences(editor);
311
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
312
      view.getObjectControl().savePreferences(editor);
313

    
314
      editor.apply();
315
      }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
    private void restorePreferences()
320
      {
321
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
322

    
323
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
324
      String oldVersion = preferences.getString("appVersion","");
325

    
326
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
327
        {
328
        BaseEffect.Type.getType(i).restorePreferences(preferences);
329
        }
330

    
331
      for (int i = 0; i< ScreenList.LENGTH; i++)
332
        {
333
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
334
        }
335

    
336
      RubikObjectList.restorePreferences(preferences);
337
      ScreenList.restorePreferences(preferences);
338

    
339
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
340

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

    
350
      if( !oldVersion.equals("") )
351
        {
352
        view.getObjectControl().restorePreferences(preferences);
353
        }
354

    
355
      RubikScores scores = RubikScores.getInstance();
356

    
357
      if( scores.isVerified() )
358
        {
359
        FirebaseAnalytics analytics = getAnalytics();
360
        analytics.setUserId(scores.getName());
361
        }
362
      }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
    private void PrivacyPolicy()
367
      {
368
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
369
      priDiag.show(getSupportFragmentManager(), null);
370
      }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
    void OpenGLError()
375
      {
376
      RubikDialogError errDiag = new RubikDialogError();
377
      errDiag.show(getSupportFragmentManager(), null);
378
      }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
    void setScreenSize(int width, int height)
383
      {
384
      mScreenWidth = width;
385
      mScreenHeight= height;
386
      }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
// PUBLIC API
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
    public FirebaseAnalytics getAnalytics()
393
      {
394
      return mFirebaseAnalytics;
395
      }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
    public int getHeightUpperBar()
400
      {
401
      return mHeightUpperBar;
402
      }
403

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

    
406
    public int getHeightLowerBar()
407
      {
408
      return mHeightLowerBar;
409
      }
410

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

    
413
    public TwistyObject getObject()
414
      {
415
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
416
      return view.getObjectControl().getObject();
417
      }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
    public ObjectControl getControl()
422
      {
423
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
424
      return view.getObjectControl();
425
      }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
    public int getScreenWidthInPixels()
430
      {
431
      return mScreenWidth;
432
      }
433

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

    
436
    public int getScreenHeightInPixels()
437
      {
438
      return mScreenHeight;
439
      }
440

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

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

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

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

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

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

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

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

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

    
507
      return 3;
508
      }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
    public static int getDrawable(int small, int medium, int big, int huge)
513
      {
514
      int size = getDrawableSize();
515

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

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

    
527
    public void acceptPrivacy()
528
      {
529
      mPolicyAccepted = true;
530
      }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

    
534
    public void declinePrivacy()
535
      {
536
      finish();
537
      }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
    public static boolean localeIsChinese()
542
      {
543
      String language;
544

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

    
554
      return language.equals("zh");
555
      }
556

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

    
559
    public boolean isVertical()
560
      {
561
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
562
      return view.isVertical();
563
      }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
    public void blockEverything(int place)
568
      {
569
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
570
      ObjectControl control = view.getObjectControl();
571
      control.blockEverything(place);
572

    
573
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
574
      play.setLockState(this);
575
      }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
    public void unblockEverything()
580
      {
581
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
582
      ObjectControl control = view.getObjectControl();
583
      control.unblockEverything();
584

    
585
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
586
      play.setLockState(this);
587
      }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

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

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

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