Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 99dd8d12

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
      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
RubikPreRender pre = view.getPreRender();
271
pre.destroyNewObject();
272
RubikDebug.addDebug("RubikActivity: onResume");
273

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

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

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

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

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

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

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

    
320
      editor.putBoolean("policyAccepted", mPolicyAccepted);
321

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

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

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

    
336
      editor.apply();
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

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

    
345
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
346

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

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

    
357
      StateList.restorePreferences(preferences);
358

    
359
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
360
      view.getPreRender().restorePreferences(preferences);
361

    
362
      RubikScores scores = RubikScores.getInstance();
363

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

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

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

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

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388
// PUBLIC API
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

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

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

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

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
    public int getScreenWidthInPixels()
408
      {
409
      return mScreenWidth;
410
      }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
    public int getScreenHeightInPixels()
415
      {
416
      return mScreenHeight;
417
      }
418

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

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

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

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

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

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

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

    
469
      pre.changeObject(newObject,newSize);
470
      }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

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

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

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

    
498
      return 3;
499
      }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

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

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

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
    public void acceptPrivacy()
519
      {
520
      mPolicyAccepted = true;
521
      }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
    public void declinePrivacy()
526
      {
527
      finish();
528
      }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
    public static boolean localeIsChinese()
533
      {
534
      String language;
535

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

    
545
      return language.equals("zh");
546
      }
547

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

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

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
    public void toggleLock()
559
      {
560
      mIsLocked = !mIsLocked;
561
      }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

    
565
    public boolean isLocked()
566
      {
567
      StateList state = StateList.getCurrentState();
568

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

    
574
      return false;
575
      }
576

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

    
579
    public boolean retLocked()
580
      {
581
      return mIsLocked;
582
      }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

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

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

    
597
  public void change()
598
    {
599
    mNumTests++;
600

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

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

    
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

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

    
624
    int angle = 360 / object.getBasicAngle();
625

    
626
    object.addNewRotation(1,1,angle,1000,this);
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630

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