Project

General

Profile

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

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

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 DIALOG_BUTTON_SIZE  = 0.06f;
71
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
72
    public static final float MENU_MED_TEXT_SIZE  = 0.04f;
73
    public static final float MENU_SMALL_TEXT_SIZE= 0.035f;
74

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

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

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

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

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

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

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

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

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

    
128
        decorView.setSystemUiVisibility(FLAGS);
129

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

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

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

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

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

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

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

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
// do not avoid cutouts
178

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

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

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
300
      editor.putBoolean("policyAccepted", mPolicyAccepted);
301

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

    
307
      for (int i = 0; i< StateList.LENGTH; i++)
308
        {
309
        StateList.getState(i).getStateClass().savePreferences(editor);
310
        }
311

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

    
316
      editor.apply();
317
      }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

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

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

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

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

    
337
      StateList.restorePreferences(preferences);
338

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

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

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

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

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

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
// PUBLIC API
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

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

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

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

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

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

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

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

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

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

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

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

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

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

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

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

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

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

    
470
      return 3;
471
      }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

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

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

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

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

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

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

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

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

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

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

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

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

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

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

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

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

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

    
546
      return false;
547
      }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

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

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

    
558
    public void switchTutorial()
559
      {
560
      Intent myIntent = new Intent(this, TutorialActivity.class);
561
    //myIntent.putExtra("", value); //Optional parameters
562
      startActivity(myIntent);
563
      }
564
}
(1-1/4)