Project

General

Profile

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

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

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.View;
31
import android.view.ViewGroup;
32
import android.view.WindowManager;
33
import android.widget.LinearLayout;
34

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

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

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

    
49
import java.util.Locale;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

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

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

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

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

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

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

    
108
      hideNavigationBar();
109
      huaweiHack();
110

    
111
      // askForPermissions();
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

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

    
124
        decorView.setSystemUiVisibility(FLAGS);
125

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    @Override
143
    public void onAttachedToWindow()
144
      {
145
      final float RATIO = 0.10f;
146
      float height = getScreenHeightInPixels();
147

    
148
      LinearLayout layoutTop = findViewById(R.id.upperBar);
149
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
150

    
151
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
152
      paramsTop.height = (int)(height*RATIO);
153
      layoutTop.setLayoutParams(paramsTop);
154
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
155
      paramsBot.height = (int)(height*RATIO);
156
      layoutBot.setLayoutParams(paramsBot);
157
      }
158

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

    
164
    private void huaweiHack()
165
      {
166
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
167
        {
168
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
169
        }
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    @Override
175
    public void onWindowFocusChanged(boolean hasFocus)
176
      {
177
      super.onWindowFocusChanged(hasFocus);
178

    
179
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
180
        {
181
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
182
        }
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    private void askForPermissions()
188
      {
189
      final int REQUEST_EXTERNAL_STORAGE = 1;
190

    
191
      String[] PERMISSIONS_STORAGE =
192
        {
193
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
194
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
195
        };
196

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

    
199
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
200
        {
201
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
202
        }
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
    
207
    @Override
208
    protected void onPause() 
209
      {
210
      super.onPause();
211
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
212
      view.onPause();
213
      DistortedLibrary.onPause();
214
      RubikScoresDownloader.onPause();
215
      savePreferences();
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
    
220
    @Override
221
    protected void onResume() 
222
      {
223
      super.onResume();
224
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
225
      view.onResume();
226
      view.initialize();
227
      restorePreferences();
228
      StateList.setState(this);
229

    
230
      if( mJustStarted )
231
        {
232
        mJustStarted = false;
233
        RubikScores scores = RubikScores.getInstance();
234
        scores.incrementNumRuns();
235
        scores.setCountry(this);
236
        }
237

    
238
      boolean success = false;
239
      RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
240
      int object = play.getObject();
241
      int size   = play.getSize();
242

    
243
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
244
        {
245
        ObjectList obj = ObjectList.getObject(object);
246
        int[] sizes = obj.getSizes();
247
        int sizeIndex = ObjectList.getSizeIndex(object,size);
248

    
249
        if( sizeIndex>=0 && sizeIndex<sizes.length )
250
          {
251
          success = true;
252
          view.getPreRender().changeObject(obj,size);
253
          }
254
        }
255

    
256
      if( !success )
257
        {
258
        ObjectList obj = ObjectList.getObject(RubikStatePlay.DEF_OBJECT);
259
        int s = RubikStatePlay.DEF_SIZE;
260

    
261
        play.setObjectAndSize(this,obj,s);
262
        view.getPreRender().changeObject(obj,s);
263
        }
264

    
265
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
266
      }
267
    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
    
270
    @Override
271
    protected void onDestroy() 
272
      {
273
      DistortedLibrary.onDestroy();
274
      super.onDestroy();
275
      }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
    private void savePreferences()
280
      {
281
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
282
      SharedPreferences.Editor editor = preferences.edit();
283

    
284
      editor.putBoolean("policyAccepted", mPolicyAccepted);
285

    
286
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
287
        {
288
        BaseEffect.Type.getType(i).savePreferences(editor);
289
        }
290

    
291
      for (int i = 0; i< StateList.LENGTH; i++)
292
        {
293
        StateList.getState(i).getStateClass().savePreferences(editor);
294
        }
295

    
296
      StateList.savePreferences(editor);
297
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
298
      view.getPreRender().savePreferences(editor);
299

    
300
      editor.apply();
301
      }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
    private void restorePreferences()
306
      {
307
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
308

    
309
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
310

    
311
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
312
        {
313
        BaseEffect.Type.getType(i).restorePreferences(preferences);
314
        }
315

    
316
      for (int i = 0; i< StateList.LENGTH; i++)
317
        {
318
        StateList.getState(i).getStateClass().restorePreferences(preferences);
319
        }
320

    
321
      StateList.restorePreferences(preferences);
322

    
323
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
324
      view.getPreRender().restorePreferences(preferences);
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    private void PrivacyPolicy()
330
      {
331
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
332
      priDiag.show(getSupportFragmentManager(), null);
333
      }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
    void OpenGLError(String message)
338
      {
339
      Bundle bundle = new Bundle();
340
      bundle.putString("error", message );
341

    
342
      RubikDialogError errDiag = new RubikDialogError();
343
      errDiag.setArguments(bundle);
344
      errDiag.show(getSupportFragmentManager(), null);
345
      }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348
// PUBLIC API
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
    public FirebaseAnalytics getAnalytics()
352
      {
353
      return mFirebaseAnalytics;
354
      }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
    public TwistyObject getObject()
359
      {
360
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
361
      RubikPreRender pre = view.getPreRender();
362
      return pre.getObject();
363
      }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
    public int getScreenWidthInPixels()
368
      {
369
      return mScreenWidth;
370
      }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
    public int getScreenHeightInPixels()
375
      {
376
      return mScreenHeight;
377
      }
378

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

    
381
    public RubikPreRender getPreRender()
382
      {
383
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
384
      return view.getPreRender();
385
      }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
390
      {
391
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
392
      RubikPreRender pre = view.getPreRender();
393

    
394
      if( reportChange )
395
        {
396
        TwistyObject oldObject = pre.getObject();
397
        ObjectList oldList = oldObject.getObjectList();
398
        int oldSize = oldObject.getSize();
399
        float fps = view.getRenderer().getFPS();
400
        fps = (int)(fps+0.5f);
401
        StringBuilder name = new StringBuilder();
402
        name.append(oldList.name());
403
        name.append('_');
404
        name.append(oldSize);
405
        name.append(' ');
406
        name.append(fps);
407
        name.append(" --> ");
408
        name.append(newObject.name());
409
        name.append('_');
410
        name.append(newSize);
411

    
412
        if( BuildConfig.DEBUG )
413
          {
414
          android.util.Log.e("rubik", name.toString());
415
          }
416
        else
417
          {
418
          FirebaseAnalytics analytics = getAnalytics();
419

    
420
          if( analytics!=null )
421
            {
422
            Bundle bundle = new Bundle();
423
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
424
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
425
            }
426
          }
427
        }
428

    
429
      pre.changeObject(newObject,newSize);
430
      }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    public void setupObject(ObjectList object, int size, int[][] moves)
435
      {
436
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
437
      RubikPreRender pre = view.getPreRender();
438
      pre.setupObject(object,size,moves);
439
      }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

    
443
    public static int getDrawableSize()
444
      {
445
      if( mScreenHeight<1000 )
446
        {
447
        return 0;
448
        }
449
      if( mScreenHeight<1600 )
450
        {
451
        return 1;
452
        }
453
      if( mScreenHeight<1900 )
454
        {
455
        return 2;
456
        }
457

    
458
      return 3;
459
      }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
    public static int getDrawable(int small, int medium, int big, int huge)
464
      {
465
      int size = getDrawableSize();
466

    
467
      switch(size)
468
        {
469
        case 0 : return small;
470
        case 1 : return medium;
471
        case 2 : return big;
472
        default: return huge;
473
        }
474
      }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
    public void acceptPrivacy()
479
      {
480
      mPolicyAccepted = true;
481
      }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
    public void declinePrivacy()
486
      {
487
      finish();
488
      }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
    public static boolean localeIsChinese()
493
      {
494
      String language;
495

    
496
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
497
        {
498
        language= LocaleList.getDefault().get(0).getLanguage();
499
        }
500
      else
501
        {
502
        language= Locale.getDefault().getLanguage();
503
        }
504

    
505
      return language.equals("zh");
506
      }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

    
510
    public boolean isVertical()
511
      {
512
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
513
      return view.isVertical();
514
      }
515

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

    
518
    public void toggleLock()
519
      {
520
      mIsLocked = !mIsLocked;
521
      }
522

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

    
525
    public boolean isLocked()
526
      {
527
      StateList state = StateList.getCurrentState();
528

    
529
      if( state== StateList.PLAY || state== StateList.READ || state== StateList.SOLV )
530
        {
531
        return mIsLocked;
532
        }
533

    
534
      return false;
535
      }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

    
539
    public boolean retLocked()
540
      {
541
      return mIsLocked;
542
      }
543
}
(1-1/4)