Project

General

Profile

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

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

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

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

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

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

    
43
import org.distorted.objects.TwistyObject;
44
import org.distorted.scores.RubikScores;
45
import org.distorted.scores.RubikScoresDownloader;
46
import org.distorted.objects.ObjectList;
47
import org.distorted.states.StateList;
48
import org.distorted.states.RubikStatePlay;
49

    
50
import java.util.Locale;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

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

    
79
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
80
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
81

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

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
    @Override
92
    protected void onCreate(Bundle savedState)
93
      {
94
      super.onCreate(savedState);
95
      setTheme(R.style.CustomActivityThemeNoActionBar);
96
      setContentView(R.layout.main);
97

    
98
      mJustStarted = true;
99
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
100

    
101
      DisplayMetrics displaymetrics = new DisplayMetrics();
102
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
103
      mScreenWidth =displaymetrics.widthPixels;
104
      mScreenHeight=displaymetrics.heightPixels;
105

    
106
      mIsChinese = localeIsChinese();
107
      mIsLocked = false;
108

    
109
      hideNavigationBar();
110

    
111
      // huaweiHack();
112
      // askForPermissions();
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
    private void hideNavigationBar()
118
      {
119
      mCurrentApiVersion = Build.VERSION.SDK_INT;
120

    
121
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
122
        {
123
        final View decorView = getWindow().getDecorView();
124

    
125
        decorView.setSystemUiVisibility(FLAGS);
126

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

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
    @Override
144
    public void onAttachedToWindow()
145
      {
146
      super.onAttachedToWindow();
147

    
148
      final float RATIO = 0.10f;
149
      float height = getScreenHeightInPixels();
150

    
151
      LinearLayout layoutTop = findViewById(R.id.upperBar);
152
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
153

    
154
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
155
      paramsTop.height = (int)(height*RATIO);
156
      layoutTop.setLayoutParams(paramsTop);
157
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
158
      paramsBot.height = (int)(height*RATIO);
159
      layoutBot.setLayoutParams(paramsBot);
160

    
161
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
162
        {
163
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
164
        int inset = cutout!=null ? cutout.getSafeInsetTop() : 0;
165
        RubikStatePlay.setSafeInsetTop(inset);
166
        }
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
// on some Huawei devices ( P40, P40 Pro, Mate 30 Pro, Mate 30, Nova 5T, P30 ) this has to be called
171
// so that the App enters fullscreen mode. Otherwise there's a black bar at the top. See
172
// https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201361898979080302&fid=0101246461018590361
173

    
174
    private void huaweiHack()
175
      {
176
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
177
        {
178
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
179
        }
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    @Override
185
    public void onWindowFocusChanged(boolean hasFocus)
186
      {
187
      super.onWindowFocusChanged(hasFocus);
188

    
189
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
190
        {
191
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
192
        }
193
      }
194

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

    
197
    private void askForPermissions()
198
      {
199
      final int REQUEST_EXTERNAL_STORAGE = 1;
200

    
201
      String[] PERMISSIONS_STORAGE =
202
        {
203
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
204
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
205
        };
206

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

    
209
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
210
        {
211
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
212
        }
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
    
217
    @Override
218
    protected void onPause() 
219
      {
220
      super.onPause();
221
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
222
      view.onPause();
223
      DistortedLibrary.onPause();
224
      RubikScoresDownloader.onPause();
225
      savePreferences();
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
    
230
    @Override
231
    protected void onResume() 
232
      {
233
      super.onResume();
234
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
235
      view.onResume();
236
      view.initialize();
237
      restorePreferences();
238
      StateList.setState(this);
239

    
240
      if( mJustStarted )
241
        {
242
        mJustStarted = false;
243
        RubikScores scores = RubikScores.getInstance();
244
        scores.incrementNumRuns();
245
        scores.setCountry(this);
246
        }
247

    
248
      boolean success = false;
249
      RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
250
      int object = play.getObject();
251
      int size   = play.getSize();
252

    
253
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
254
        {
255
        ObjectList obj = ObjectList.getObject(object);
256
        int[] sizes = obj.getSizes();
257
        int sizeIndex = ObjectList.getSizeIndex(object,size);
258

    
259
        if( sizeIndex>=0 && sizeIndex<sizes.length )
260
          {
261
          success = true;
262
          view.getPreRender().changeObject(obj,size);
263
          }
264
        }
265

    
266
      if( !success )
267
        {
268
        ObjectList obj = ObjectList.getObject(RubikStatePlay.DEF_OBJECT);
269
        int s = RubikStatePlay.DEF_SIZE;
270

    
271
        play.setObjectAndSize(this,obj,s);
272
        view.getPreRender().changeObject(obj,s);
273
        }
274

    
275
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
276
      }
277
    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279
    
280
    @Override
281
    protected void onDestroy() 
282
      {
283
      DistortedLibrary.onDestroy();
284
      super.onDestroy();
285
      }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
    private void savePreferences()
290
      {
291
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
292
      SharedPreferences.Editor editor = preferences.edit();
293

    
294
      editor.putBoolean("policyAccepted", mPolicyAccepted);
295

    
296
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
297
        {
298
        BaseEffect.Type.getType(i).savePreferences(editor);
299
        }
300

    
301
      for (int i = 0; i< StateList.LENGTH; i++)
302
        {
303
        StateList.getState(i).getStateClass().savePreferences(editor);
304
        }
305

    
306
      StateList.savePreferences(editor);
307
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
308
      view.getPreRender().savePreferences(editor);
309

    
310
      editor.apply();
311
      }
312

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

    
315
    private void restorePreferences()
316
      {
317
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
318

    
319
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
320

    
321
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
322
        {
323
        BaseEffect.Type.getType(i).restorePreferences(preferences);
324
        }
325

    
326
      for (int i = 0; i< StateList.LENGTH; i++)
327
        {
328
        StateList.getState(i).getStateClass().restorePreferences(preferences);
329
        }
330

    
331
      StateList.restorePreferences(preferences);
332

    
333
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
334
      view.getPreRender().restorePreferences(preferences);
335
      }
336

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

    
339
    private void PrivacyPolicy()
340
      {
341
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
342
      priDiag.show(getSupportFragmentManager(), null);
343
      }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
    void OpenGLError()
348
      {
349
      RubikDialogError errDiag = new RubikDialogError();
350
      errDiag.show(getSupportFragmentManager(), null);
351
      }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
// PUBLIC API
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
    public FirebaseAnalytics getAnalytics()
358
      {
359
      return mFirebaseAnalytics;
360
      }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
    public TwistyObject getObject()
365
      {
366
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
367
      RubikPreRender pre = view.getPreRender();
368
      return pre.getObject();
369
      }
370

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

    
373
    public int getScreenWidthInPixels()
374
      {
375
      return mScreenWidth;
376
      }
377

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

    
380
    public int getScreenHeightInPixels()
381
      {
382
      return mScreenHeight;
383
      }
384

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

    
387
    public RubikPreRender getPreRender()
388
      {
389
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
390
      return view.getPreRender();
391
      }
392

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

    
395
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
396
      {
397
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
398
      RubikPreRender pre = view.getPreRender();
399

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

    
418
        if( BuildConfig.DEBUG )
419
          {
420
          android.util.Log.e("rubik", name.toString());
421
          }
422
        else
423
          {
424
          FirebaseAnalytics analytics = getAnalytics();
425

    
426
          if( analytics!=null )
427
            {
428
            Bundle bundle = new Bundle();
429
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
430
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
431
            }
432
          }
433
        }
434

    
435
      pre.changeObject(newObject,newSize);
436
      }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
    public void setupObject(ObjectList object, int size, int[][] moves)
441
      {
442
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
443
      RubikPreRender pre = view.getPreRender();
444
      pre.setupObject(object,size,moves);
445
      }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
    public static int getDrawableSize()
450
      {
451
      if( mScreenHeight<1000 )
452
        {
453
        return 0;
454
        }
455
      if( mScreenHeight<1600 )
456
        {
457
        return 1;
458
        }
459
      if( mScreenHeight<1900 )
460
        {
461
        return 2;
462
        }
463

    
464
      return 3;
465
      }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
    public static int getDrawable(int small, int medium, int big, int huge)
470
      {
471
      int size = getDrawableSize();
472

    
473
      switch(size)
474
        {
475
        case 0 : return small;
476
        case 1 : return medium;
477
        case 2 : return big;
478
        default: return huge;
479
        }
480
      }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
    public void acceptPrivacy()
485
      {
486
      mPolicyAccepted = true;
487
      }
488

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

    
491
    public void declinePrivacy()
492
      {
493
      finish();
494
      }
495

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

    
498
    public static boolean localeIsChinese()
499
      {
500
      String language;
501

    
502
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
503
        {
504
        language= LocaleList.getDefault().get(0).getLanguage();
505
        }
506
      else
507
        {
508
        language= Locale.getDefault().getLanguage();
509
        }
510

    
511
      return language.equals("zh");
512
      }
513

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

    
516
    public boolean isVertical()
517
      {
518
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
519
      return view.isVertical();
520
      }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

    
524
    public void toggleLock()
525
      {
526
      mIsLocked = !mIsLocked;
527
      }
528

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

    
531
    public boolean isLocked()
532
      {
533
      StateList state = StateList.getCurrentState();
534

    
535
      if( state== StateList.PLAY || state== StateList.READ || state== StateList.SOLV )
536
        {
537
        return mIsLocked;
538
        }
539

    
540
      return false;
541
      }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
    public boolean retLocked()
546
      {
547
      return mIsLocked;
548
      }
549
}
(1-1/4)