Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 09cf2a36

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
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
88
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
89
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
90
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
91
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
92
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
93

    
94
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
95
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
96

    
97
    private static final int ACTIVITY_NUMBER = 0;
98
    private static final float RATIO_BAR  = 0.10f;
99
    private static final float RATIO_INSET= 0.08f;
100

    
101
    private boolean mJustStarted;
102
    private FirebaseAnalytics mFirebaseAnalytics;
103
    private static int mScreenWidth, mScreenHeight;
104
    private boolean mPolicyAccepted, mIsChinese;
105
    private int mCurrentApiVersion;
106
    private int mHeightUpperBar, mHeightLowerBar;
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
    @Override
111
    protected void onCreate(Bundle savedState)
112
      {
113
      super.onCreate(savedState);
114
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
115

    
116
      setTheme(R.style.MaterialThemeNoActionBar);
117
      setContentView(R.layout.main);
118

    
119
      mJustStarted = true;
120
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
121
      mIsChinese = localeIsChinese();
122

    
123
      DisplayMetrics displaymetrics = new DisplayMetrics();
124
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
125
      mScreenWidth =displaymetrics.widthPixels;
126
      mScreenHeight=displaymetrics.heightPixels;
127
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
128
                                                  // which is not yet hidden.
129
                                                  // TODO: figure this out exactly.
130
      hideNavigationBar();
131
      cutoutHack();
132
      computeBarHeights();
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// this does not include possible insets
137

    
138
    private void computeBarHeights()
139
      {
140
      float height = getScreenHeightInPixels();
141
      int barHeight = (int)(height*RATIO_BAR);
142
      mHeightLowerBar = barHeight;
143
      mHeightUpperBar = barHeight;
144

    
145
      LinearLayout layoutTop = findViewById(R.id.upperBar);
146
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
147

    
148
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
149
      paramsTop.height = mHeightUpperBar;
150
      layoutTop.setLayoutParams(paramsTop);
151
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
152
      paramsBot.height = mHeightLowerBar;
153
      layoutBot.setLayoutParams(paramsBot);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    private void hideNavigationBar()
159
      {
160
      mCurrentApiVersion = Build.VERSION.SDK_INT;
161

    
162
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
163
        {
164
        final View decorView = getWindow().getDecorView();
165

    
166
        decorView.setSystemUiVisibility(FLAGS);
167

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    @Override
185
    public void onAttachedToWindow()
186
      {
187
      super.onAttachedToWindow();
188

    
189
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
190
        {
191
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
192
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
193

    
194
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
195
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
196
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
197
        layoutHid.setLayoutParams(paramsHid);
198
        mHeightUpperBar += paramsHid.height;
199
        }
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
// do not avoid cutouts
204

    
205
    private void cutoutHack()
206
      {
207
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
208
        {
209
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
210
        }
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
    @Override
216
    public void onWindowFocusChanged(boolean hasFocus)
217
      {
218
      super.onWindowFocusChanged(hasFocus);
219

    
220
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
221
        {
222
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
223
        }
224
      }
225

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
    
241
    @Override
242
    protected void onResume() 
243
      {
244
      super.onResume();
245
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
246
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
247
      view.onResume();
248
      restorePreferences();
249

    
250
      ScreenList.setScreen(this);
251
      unblockEverything();
252

    
253
      if( mJustStarted )
254
        {
255
        mJustStarted = false;
256
        RubikScores scores = RubikScores.getInstance();
257
        scores.incrementNumRuns();
258
        scores.setCountry(this);
259
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
260
        RubikObjectList.restoreMeshState(preferences);
261
        }
262

    
263
      int object = RubikObjectList.getCurrObject();
264
      changeIfDifferent(object,view.getObjectControl());
265

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

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

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

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

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

    
300
      editor.putBoolean("policyAccepted", mPolicyAccepted);
301
      editor.putString("appVersion", getAppVers() );
302

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

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

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

    
319
      editor.apply();
320
      }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

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

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

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

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

    
341
      RubikObjectList.restorePreferences(preferences);
342
      ScreenList.restorePreferences(preferences);
343

    
344
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
345

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

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

    
360
      RubikScores scores = RubikScores.getInstance();
361

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

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

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

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

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

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
// PUBLIC API
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
    public FirebaseAnalytics getAnalytics()
398
      {
399
      return mFirebaseAnalytics;
400
      }
401

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

    
404
    public int getHeightUpperBar()
405
      {
406
      return mHeightUpperBar;
407
      }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
    public int getHeightLowerBar()
412
      {
413
      return mHeightLowerBar;
414
      }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

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

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

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

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    public int getScreenWidthInPixels()
435
      {
436
      return mScreenWidth;
437
      }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
    public int getScreenHeightInPixels()
442
      {
443
      return mScreenHeight;
444
      }
445

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

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

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

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

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

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
    public void changeIfDifferent(int ordinal, ObjectControl control)
489
      {
490
      RubikObject object = RubikObjectList.getObject(ordinal);
491
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
492

    
493
      InputStream jsonStream = ObjectJson.getStream(this,ordinal);
494
      InputStream meshStream = ObjectMesh.getStream(this,ordinal);
495
      control.changeIfDifferent(ordinal,meshState,jsonStream,meshStream);
496
      }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
    public static int getDrawableSize()
501
      {
502
      if( mScreenHeight<1000 )
503
        {
504
        return 0;
505
        }
506
      if( mScreenHeight<1600 )
507
        {
508
        return 1;
509
        }
510
      if( mScreenHeight<1900 )
511
        {
512
        return 2;
513
        }
514

    
515
      return 3;
516
      }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

    
520
    public static int getDrawable(int small, int medium, int big, int huge)
521
      {
522
      int size = getDrawableSize();
523

    
524
      switch(size)
525
        {
526
        case 0 : return small;
527
        case 1 : return medium;
528
        case 2 : return big;
529
        default: return huge;
530
        }
531
      }
532

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

    
535
    public void acceptPrivacy()
536
      {
537
      mPolicyAccepted = true;
538
      }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
    public void declinePrivacy()
543
      {
544
      finish();
545
      }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
    public static boolean localeIsChinese()
550
      {
551
      String language;
552

    
553
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
554
        {
555
        language= LocaleList.getDefault().get(0).getLanguage();
556
        }
557
      else
558
        {
559
        language= Locale.getDefault().getLanguage();
560
        }
561

    
562
      return language.equals("zh");
563
      }
564

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

    
567
    public boolean isVertical()
568
      {
569
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
570
      return view.isVertical();
571
      }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
    public void blockEverything(int place)
576
      {
577
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
578
      ObjectControl control = view.getObjectControl();
579
      control.blockEverything(place);
580

    
581
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
582
      play.setLockState(this);
583
      }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
    public void unblockEverything()
588
      {
589
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
590
      ObjectControl control = view.getObjectControl();
591
      control.unblockEverything();
592

    
593
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
594
      play.setLockState(this);
595
      }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598

    
599
    public void switchTutorial(String url, int objectOrdinal)
600
      {
601
      Intent myIntent = new Intent(this, TutorialActivity.class);
602
      myIntent.putExtra("url", url);
603
      myIntent.putExtra("obj", objectOrdinal);
604
      startActivity(myIntent);
605
      }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608

    
609
    public void switchConfig(int objectOrdinal)
610
      {
611
      Intent myIntent = new Intent(this, ConfigActivity.class);
612
      myIntent.putExtra("obj", objectOrdinal);
613
      startActivity(myIntent);
614
      }
615
}
(1-1/4)