Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 09784afd

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 android.content.Intent;
23
import android.content.SharedPreferences;
24
import android.os.Build;
25
import android.os.Bundle;
26
import android.os.LocaleList;
27
import android.preference.PreferenceManager;
28
import androidx.appcompat.app.AppCompatActivity;
29

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

    
37
import com.google.firebase.analytics.FirebaseAnalytics;
38

    
39
import org.distorted.dialogs.RubikDialogError;
40
import org.distorted.dialogs.RubikDialogPrivacy;
41
import org.distorted.effects.BaseEffect;
42
import org.distorted.library.main.DistortedLibrary;
43

    
44
import org.distorted.library.message.EffectListener;
45
import org.distorted.objects.TwistyObject;
46
import org.distorted.scores.RubikScores;
47
import org.distorted.scores.RubikScoresDownloader;
48
import org.distorted.objects.ObjectList;
49
import org.distorted.states.StateList;
50
import org.distorted.states.RubikStatePlay;
51
import org.distorted.tutorial.TutorialActivity;
52
import org.distorted.tutorial.TutorialList;
53

    
54
import java.util.Locale;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

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

    
84
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
85
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
86

    
87
    private boolean mJustStarted;
88
    private FirebaseAnalytics mFirebaseAnalytics;
89
    private static int mScreenWidth, mScreenHeight;
90
    private boolean mPolicyAccepted, mIsChinese;
91
    private int mCurrentApiVersion;
92
    private boolean mIsLocked;
93

    
94
    private static int mNumTests=0;
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
    @Override
99
    protected void onCreate(Bundle savedState)
100
      {
101
      super.onCreate(savedState);
102
      DistortedLibrary.onCreate(0);
103

    
104
      setTheme(R.style.CustomActivityThemeNoActionBar);
105
      setContentView(R.layout.main);
106

    
107
      mJustStarted = true;
108
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
109

    
110
      DisplayMetrics displaymetrics = new DisplayMetrics();
111
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
112
      mScreenWidth =displaymetrics.widthPixels;
113
      mScreenHeight=displaymetrics.heightPixels;
114

    
115
      mIsChinese = localeIsChinese();
116
      mIsLocked = false;
117

    
118
      hideNavigationBar();
119
      cutoutHack();
120
      // askForPermissions();
121

    
122
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
123
      downloader.error("TEST_BEGIN");
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    private void hideNavigationBar()
129
      {
130
      mCurrentApiVersion = Build.VERSION.SDK_INT;
131

    
132
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
133
        {
134
        final View decorView = getWindow().getDecorView();
135

    
136
        decorView.setSystemUiVisibility(FLAGS);
137

    
138
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
139
          {
140
          @Override
141
          public void onSystemUiVisibilityChange(int visibility)
142
            {
143
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
144
              {
145
              decorView.setSystemUiVisibility(FLAGS);
146
              }
147
            }
148
          });
149
        }
150
      }
151

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

    
154
    @Override
155
    public void onAttachedToWindow()
156
      {
157
      super.onAttachedToWindow();
158

    
159
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
160
        {
161
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
162
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
163

    
164
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
165
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
166
        paramsHid.height = (int)(0.8f*insetHeight);
167
        layoutHid.setLayoutParams(paramsHid);
168
        }
169

    
170
      final float RATIO = 0.10f;
171
      float height = getScreenHeightInPixels();
172

    
173
      LinearLayout layoutTop = findViewById(R.id.upperBar);
174
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
175

    
176
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
177
      paramsTop.height = (int)(height*RATIO);
178
      layoutTop.setLayoutParams(paramsTop);
179
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
180
      paramsBot.height = (int)(height*RATIO);
181
      layoutBot.setLayoutParams(paramsBot);
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// do not avoid cutouts
186

    
187
    private void cutoutHack()
188
      {
189
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
190
        {
191
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
192
        }
193
      }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
    @Override
198
    public void onWindowFocusChanged(boolean hasFocus)
199
      {
200
      super.onWindowFocusChanged(hasFocus);
201

    
202
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
203
        {
204
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
205
        }
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    private void askForPermissions()
211
      {
212
      final int REQUEST_EXTERNAL_STORAGE = 1;
213

    
214
      String[] PERMISSIONS_STORAGE =
215
        {
216
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
217
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
218
        };
219

    
220
      int permission = androidx.core.app.ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
221

    
222
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
223
        {
224
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
225
        }
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
    
230
    @Override
231
    protected void onPause() 
232
      {
233
      super.onPause();
234

    
235
      RubikDebug debug = RubikDebug.getInstance();
236
      debug.onPause();
237

    
238
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
239
      view.onPause();
240
      DistortedLibrary.onPause(0);
241
      RubikScoresDownloader.onPause();
242
      savePreferences();
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
    
247
    @Override
248
    protected void onResume() 
249
      {
250
      super.onResume();
251
      DistortedLibrary.onResume(0);
252
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
253
      view.onResume();
254

    
255
      RubikDebug debug = RubikDebug.getInstance();
256
      debug.onResume(this);
257

    
258
      view.initialize();
259
      restorePreferences();
260
      StateList.setState(this);
261

    
262
      if( mJustStarted )
263
        {
264
        mJustStarted = false;
265
        RubikScores scores = RubikScores.getInstance();
266
        scores.incrementNumRuns();
267
        scores.setCountry(this);
268
        }
269

    
270
      RubikDebug.addDebug("RubikActivity: onResume");
271

    
272
      boolean success = false;
273
      RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
274
      int object = play.getObject();
275
      int size   = play.getSize();
276

    
277
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
278
        {
279
        ObjectList obj = ObjectList.getObject(object);
280
        int[] sizes = obj.getSizes();
281
        int sizeIndex = ObjectList.getSizeIndex(object,size);
282

    
283
        if( sizeIndex>=0 && sizeIndex<sizes.length )
284
          {
285
          success = true;
286
          view.getPreRender().changeObject(obj,size);
287
          }
288
        }
289

    
290
      if( !success )
291
        {
292
        ObjectList obj = ObjectList.getObject(RubikStatePlay.DEF_OBJECT);
293
        int s = RubikStatePlay.DEF_SIZE;
294

    
295
        play.setObjectAndSize(this,obj,s);
296
        view.getPreRender().changeObject(obj,s);
297
        }
298

    
299
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
300
      }
301
    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303
    
304
    @Override
305
    protected void onDestroy() 
306
      {
307
      DistortedLibrary.onDestroy(0);
308
      super.onDestroy();
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
    private void savePreferences()
314
      {
315
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
316
      SharedPreferences.Editor editor = preferences.edit();
317

    
318
      editor.putBoolean("policyAccepted", mPolicyAccepted);
319

    
320
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
321
        {
322
        BaseEffect.Type.getType(i).savePreferences(editor);
323
        }
324

    
325
      for (int i = 0; i< StateList.LENGTH; i++)
326
        {
327
        StateList.getState(i).getStateClass().savePreferences(editor);
328
        }
329

    
330
      StateList.savePreferences(editor);
331
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
332
      view.getPreRender().savePreferences(editor);
333

    
334
      editor.apply();
335
      }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
    private void restorePreferences()
340
      {
341
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
342

    
343
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
344

    
345
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
346
        {
347
        BaseEffect.Type.getType(i).restorePreferences(preferences);
348
        }
349

    
350
      for (int i = 0; i< StateList.LENGTH; i++)
351
        {
352
        StateList.getState(i).getStateClass().restorePreferences(preferences);
353
        }
354

    
355
      StateList.restorePreferences(preferences);
356

    
357
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
358
      view.getPreRender().restorePreferences(preferences);
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
// PUBLIC API
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

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

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

    
396
    public TwistyObject getObject()
397
      {
398
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
399
      RubikPreRender pre = view.getPreRender();
400
      return pre.getObject();
401
      }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
    public int getScreenWidthInPixels()
406
      {
407
      return mScreenWidth;
408
      }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
    public int getScreenHeightInPixels()
413
      {
414
      return mScreenHeight;
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
    public RubikPreRender getPreRender()
420
      {
421
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
422
      return view.getPreRender();
423
      }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
428
      {
429
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
430
      RubikPreRender pre = view.getPreRender();
431

    
432
      if( reportChange )
433
        {
434
        TwistyObject oldObject = pre.getObject();
435
        ObjectList oldList = oldObject.getObjectList();
436
        int oldNum = oldObject.getNumLayers();
437
        float fps = view.getRenderer().getFPS();
438
        fps = (int)(fps+0.5f);
439
        StringBuilder name = new StringBuilder();
440
        name.append(oldList.name());
441
        name.append('_');
442
        name.append(oldNum);
443
        name.append(' ');
444
        name.append(fps);
445
        name.append(" --> ");
446
        name.append(newObject.name());
447
        name.append('_');
448
        name.append(newSize);
449

    
450
        if( BuildConfig.DEBUG )
451
          {
452
          android.util.Log.e("rubik", name.toString());
453
          }
454
        else
455
          {
456
          FirebaseAnalytics analytics = getAnalytics();
457

    
458
          if( analytics!=null )
459
            {
460
            Bundle bundle = new Bundle();
461
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
462
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
463
            }
464
          }
465
        }
466

    
467
      pre.changeObject(newObject,newSize);
468
      }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
    public void setupObject(ObjectList object, int size, int[][] moves)
473
      {
474
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
475
      RubikPreRender pre = view.getPreRender();
476
      pre.setupObject(object,size,moves);
477
      }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

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

    
496
      return 3;
497
      }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
    public static int getDrawable(int small, int medium, int big, int huge)
502
      {
503
      int size = getDrawableSize();
504

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

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515

    
516
    public void acceptPrivacy()
517
      {
518
      mPolicyAccepted = true;
519
      }
520

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

    
523
    public void declinePrivacy()
524
      {
525
      finish();
526
      }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
    public static boolean localeIsChinese()
531
      {
532
      String language;
533

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

    
543
      return language.equals("zh");
544
      }
545

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547

    
548
    public boolean isVertical()
549
      {
550
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
551
      return view.isVertical();
552
      }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
    public void toggleLock()
557
      {
558
      mIsLocked = !mIsLocked;
559
      }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
    public boolean isLocked()
564
      {
565
      StateList state = StateList.getCurrentState();
566

    
567
      if( state== StateList.PLAY || state== StateList.READ || state== StateList.SOLV )
568
        {
569
        return mIsLocked;
570
        }
571

    
572
      return false;
573
      }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

    
577
    public boolean retLocked()
578
      {
579
      return mIsLocked;
580
      }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

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

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

    
595
  public void change()
596
    {
597
    mNumTests++;
598

    
599
    if( mNumTests<100 )
600
      {
601
      TutorialList list  = TutorialList.getObject(2);
602
      ObjectList objList = list.getObjectList();
603
      int size           = list.getSize();
604
      String url         = list.getTutorialURL(0);
605

    
606
      switchTutorial(url, objList, size);
607
      }
608
    else
609
      {
610
      finish();
611
      }
612
    }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615

    
616
  public void assign()
617
    {
618
    RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
619
    RubikPreRender pre = view.getPreRender();
620
    TwistyObject object = pre.getObject();
621

    
622
    int angle = 360 / object.getBasicAngle();
623

    
624
    pre.addRotation(this,1,1,angle,1000);
625
    }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628

    
629
  public void onActionFinished(final long effectID)
630
    {
631
    RubikDebug debug = RubikDebug.getInstance();
632
    debug.onReturned();
633
    }
634
}
(1-1/5)