Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ ef1f3e34

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.objects.TwistyObject;
45
import org.distorted.scores.RubikScores;
46
import org.distorted.scores.RubikScoresDownloader;
47
import org.distorted.objects.ObjectList;
48
import org.distorted.states.StateList;
49
import org.distorted.states.RubikStatePlay;
50
import org.distorted.tutorial.TutorialActivity;
51

    
52
import java.util.Locale;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

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

    
82
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
83
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
84

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
    @Override
95
    protected void onCreate(Bundle savedState)
96
      {
97
      super.onCreate(savedState);
98
      DistortedLibrary.onCreate(0);
99

    
100
      setTheme(R.style.CustomActivityThemeNoActionBar);
101
      setContentView(R.layout.main);
102

    
103
      mJustStarted = true;
104
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
105

    
106
      DisplayMetrics displaymetrics = new DisplayMetrics();
107
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
108
      mScreenWidth =displaymetrics.widthPixels;
109
      mScreenHeight=displaymetrics.heightPixels;
110

    
111
      mIsChinese = localeIsChinese();
112
      mIsLocked = false;
113

    
114
      hideNavigationBar();
115
      cutoutHack();
116
      // askForPermissions();
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    private void hideNavigationBar()
122
      {
123
      mCurrentApiVersion = Build.VERSION.SDK_INT;
124

    
125
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
126
        {
127
        final View decorView = getWindow().getDecorView();
128

    
129
        decorView.setSystemUiVisibility(FLAGS);
130

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

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
    @Override
148
    public void onAttachedToWindow()
149
      {
150
      super.onAttachedToWindow();
151

    
152
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
153
        {
154
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
155
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
156

    
157
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
158
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
159
        paramsHid.height = (int)(0.8f*insetHeight);
160
        layoutHid.setLayoutParams(paramsHid);
161
        }
162

    
163
      final float RATIO = 0.10f;
164
      float height = getScreenHeightInPixels();
165

    
166
      LinearLayout layoutTop = findViewById(R.id.upperBar);
167
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
168

    
169
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
170
      paramsTop.height = (int)(height*RATIO);
171
      layoutTop.setLayoutParams(paramsTop);
172
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
173
      paramsBot.height = (int)(height*RATIO);
174
      layoutBot.setLayoutParams(paramsBot);
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// do not avoid cutouts
179

    
180
    private void cutoutHack()
181
      {
182
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
183
        {
184
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
185
        }
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    @Override
191
    public void onWindowFocusChanged(boolean hasFocus)
192
      {
193
      super.onWindowFocusChanged(hasFocus);
194

    
195
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
196
        {
197
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
198
        }
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    private void askForPermissions()
204
      {
205
      final int REQUEST_EXTERNAL_STORAGE = 1;
206

    
207
      String[] PERMISSIONS_STORAGE =
208
        {
209
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
210
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
211
        };
212

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

    
215
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
216
        {
217
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
218
        }
219
      }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
    
223
    @Override
224
    protected void onPause() 
225
      {
226
      super.onPause();
227
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
228
      view.onPause();
229
      DistortedLibrary.onPause(0);
230
      RubikScoresDownloader.onPause();
231
      savePreferences();
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
    
236
    @Override
237
    protected void onResume() 
238
      {
239
      super.onResume();
240
      DistortedLibrary.onResume(0);
241
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
242
      view.onResume();
243
      view.initialize();
244
      restorePreferences();
245
      StateList.setState(this);
246

    
247
      if( mJustStarted )
248
        {
249
        mJustStarted = false;
250
        RubikScores scores = RubikScores.getInstance();
251
        scores.incrementNumRuns();
252
        scores.setCountry(this);
253
        }
254

    
255
      boolean success = false;
256
      RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
257
      int object = play.getObject();
258
      int size   = play.getSize();
259

    
260
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
261
        {
262
        ObjectList obj = ObjectList.getObject(object);
263
        int[] sizes = obj.getSizes();
264
        int sizeIndex = ObjectList.getSizeIndex(object,size);
265

    
266
        if( sizeIndex>=0 && sizeIndex<sizes.length )
267
          {
268
          success = true;
269
          view.getPreRender().changeObject(obj,size);
270
          }
271
        }
272

    
273
      if( !success )
274
        {
275
        ObjectList obj = ObjectList.getObject(RubikStatePlay.DEF_OBJECT);
276
        int s = RubikStatePlay.DEF_SIZE;
277

    
278
        play.setObjectAndSize(this,obj,s);
279
        view.getPreRender().changeObject(obj,s);
280
        }
281

    
282
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
283
      }
284
    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
    
287
    @Override
288
    protected void onDestroy() 
289
      {
290
      DistortedLibrary.onDestroy(0);
291
      super.onDestroy();
292
      }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

    
301
      editor.putBoolean("policyAccepted", mPolicyAccepted);
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< StateList.LENGTH; i++)
309
        {
310
        StateList.getState(i).getStateClass().savePreferences(editor);
311
        }
312

    
313
      StateList.savePreferences(editor);
314
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
315
      view.getPreRender().savePreferences(editor);
316

    
317
      editor.apply();
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

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

    
326
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
327

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

    
333
      for (int i = 0; i< StateList.LENGTH; i++)
334
        {
335
        StateList.getState(i).getStateClass().restorePreferences(preferences);
336
        }
337

    
338
      StateList.restorePreferences(preferences);
339

    
340
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
341
      view.getPreRender().restorePreferences(preferences);
342
      }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
    private void PrivacyPolicy()
347
      {
348
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
349
      priDiag.show(getSupportFragmentManager(), null);
350
      }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
    void OpenGLError()
355
      {
356
      RubikDialogError errDiag = new RubikDialogError();
357
      errDiag.show(getSupportFragmentManager(), null);
358
      }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
// PUBLIC API
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
    public FirebaseAnalytics getAnalytics()
365
      {
366
      return mFirebaseAnalytics;
367
      }
368

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

    
371
    public TwistyObject getObject()
372
      {
373
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
374
      RubikPreRender pre = view.getPreRender();
375
      return pre.getObject();
376
      }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
    public int getScreenWidthInPixels()
381
      {
382
      return mScreenWidth;
383
      }
384

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

    
387
    public int getScreenHeightInPixels()
388
      {
389
      return mScreenHeight;
390
      }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
    public RubikPreRender getPreRender()
395
      {
396
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
397
      return view.getPreRender();
398
      }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
403
      {
404
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
405
      RubikPreRender pre = view.getPreRender();
406

    
407
      if( reportChange )
408
        {
409
        TwistyObject oldObject = pre.getObject();
410
        ObjectList oldList = oldObject.getObjectList();
411
        int oldNum = oldObject.getNumLayers();
412
        float fps = view.getRenderer().getFPS();
413
        fps = (int)(fps+0.5f);
414
        StringBuilder name = new StringBuilder();
415
        name.append(oldList.name());
416
        name.append('_');
417
        name.append(oldNum);
418
        name.append(' ');
419
        name.append(fps);
420
        name.append(" --> ");
421
        name.append(newObject.name());
422
        name.append('_');
423
        name.append(newSize);
424

    
425
        if( BuildConfig.DEBUG )
426
          {
427
          android.util.Log.e("rubik", name.toString());
428
          }
429
        else
430
          {
431
          FirebaseAnalytics analytics = getAnalytics();
432

    
433
          if( analytics!=null )
434
            {
435
            Bundle bundle = new Bundle();
436
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
437
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
438
            }
439
          }
440
        }
441

    
442
      pre.changeObject(newObject,newSize);
443
      }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

    
447
    public void setupObject(ObjectList object, int size, int[][] moves)
448
      {
449
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
450
      RubikPreRender pre = view.getPreRender();
451
      pre.setupObject(object,size,moves);
452
      }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
    public static int getDrawableSize()
457
      {
458
      if( mScreenHeight<1000 )
459
        {
460
        return 0;
461
        }
462
      if( mScreenHeight<1600 )
463
        {
464
        return 1;
465
        }
466
      if( mScreenHeight<1900 )
467
        {
468
        return 2;
469
        }
470

    
471
      return 3;
472
      }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475

    
476
    public static int getDrawable(int small, int medium, int big, int huge)
477
      {
478
      int size = getDrawableSize();
479

    
480
      switch(size)
481
        {
482
        case 0 : return small;
483
        case 1 : return medium;
484
        case 2 : return big;
485
        default: return huge;
486
        }
487
      }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
    public void acceptPrivacy()
492
      {
493
      mPolicyAccepted = true;
494
      }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
    public void declinePrivacy()
499
      {
500
      finish();
501
      }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
    public static boolean localeIsChinese()
506
      {
507
      String language;
508

    
509
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
510
        {
511
        language= LocaleList.getDefault().get(0).getLanguage();
512
        }
513
      else
514
        {
515
        language= Locale.getDefault().getLanguage();
516
        }
517

    
518
      return language.equals("zh");
519
      }
520

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

    
523
    public boolean isVertical()
524
      {
525
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
526
      return view.isVertical();
527
      }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
    public void toggleLock()
532
      {
533
      mIsLocked = !mIsLocked;
534
      }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
    public boolean isLocked()
539
      {
540
      StateList state = StateList.getCurrentState();
541

    
542
      if( state== StateList.PLAY || state== StateList.READ || state== StateList.SOLV )
543
        {
544
        return mIsLocked;
545
        }
546

    
547
      return false;
548
      }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
    public boolean retLocked()
553
      {
554
      return mIsLocked;
555
      }
556

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

    
559
    public void switchTutorial(String url, ObjectList object, int size)
560
      {
561
      Intent myIntent = new Intent(this, TutorialActivity.class);
562
      myIntent.putExtra("url", url);
563
      myIntent.putExtra("obj", object.ordinal());
564
      myIntent.putExtra("siz", size);
565
      startActivity(myIntent);
566
      }
567
}
(1-1/4)