Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 36b9ee93

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, EffectListener
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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
    private void hideNavigationBar()
126
      {
127
      mCurrentApiVersion = Build.VERSION.SDK_INT;
128

    
129
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
130
        {
131
        final View decorView = getWindow().getDecorView();
132

    
133
        decorView.setSystemUiVisibility(FLAGS);
134

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
    @Override
152
    public void onAttachedToWindow()
153
      {
154
      super.onAttachedToWindow();
155

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

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

    
167
      final float RATIO = 0.10f;
168
      float height = getScreenHeightInPixels();
169

    
170
      LinearLayout layoutTop = findViewById(R.id.upperBar);
171
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
172

    
173
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
174
      paramsTop.height = (int)(height*RATIO);
175
      layoutTop.setLayoutParams(paramsTop);
176
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
177
      paramsBot.height = (int)(height*RATIO);
178
      layoutBot.setLayoutParams(paramsBot);
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// do not avoid cutouts
183

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
    @Override
195
    public void onWindowFocusChanged(boolean hasFocus)
196
      {
197
      super.onWindowFocusChanged(hasFocus);
198

    
199
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
200
        {
201
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
202
        }
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    private void askForPermissions()
208
      {
209
      final int REQUEST_EXTERNAL_STORAGE = 1;
210

    
211
      String[] PERMISSIONS_STORAGE =
212
        {
213
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
214
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
215
        };
216

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

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
    
227
    @Override
228
    protected void onPause() 
229
      {
230
      super.onPause();
231

    
232
      RubikDebug debug = RubikDebug.getInstance();
233
      debug.onPause();
234

    
235
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
236
      view.onPause();
237
      DistortedLibrary.onPause(0);
238
      RubikScoresDownloader.onPause();
239
      savePreferences();
240
      }
241

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

    
252
      RubikDebug debug = RubikDebug.getInstance();
253
      debug.onResume(this);
254

    
255
      view.initialize();
256
      restorePreferences();
257
      StateList.setState(this);
258

    
259
      if( mJustStarted )
260
        {
261
        mJustStarted = false;
262
        RubikScores scores = RubikScores.getInstance();
263
        scores.incrementNumRuns();
264
        scores.setCountry(this);
265
        }
266

    
267
RubikPreRender pre = view.getPreRender();
268
pre.destroyNewObject();
269
RubikDebug.addDebug("RubikActivity: onResume");
270

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

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

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

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

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

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

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

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

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

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

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

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

    
333
      editor.apply();
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

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

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

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

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

    
354
      StateList.restorePreferences(preferences);
355

    
356
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
357
      view.getPreRender().restorePreferences(preferences);
358

    
359
      RubikScores scores = RubikScores.getInstance();
360

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

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

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

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

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

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
// PUBLIC API
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

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

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

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

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

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

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

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

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

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

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

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

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

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

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

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

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

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

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

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

    
495
      return 3;
496
      }
497

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

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

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

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

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

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

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

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528

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

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

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

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

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

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554

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

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561

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

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

    
571
      return false;
572
      }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575

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

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

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

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

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

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

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

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614

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

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

    
623
    object.addNewRotation(1,1,angle,1000,this);
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627

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