Project

General

Profile

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

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

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.11f;
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()
338
      {
339
      RubikDialogError errDiag = new RubikDialogError();
340
      errDiag.show(getSupportFragmentManager(), null);
341
      }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344
// PUBLIC API
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
    public FirebaseAnalytics getAnalytics()
348
      {
349
      return mFirebaseAnalytics;
350
      }
351

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

    
354
    public TwistyObject getObject()
355
      {
356
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
357
      RubikPreRender pre = view.getPreRender();
358
      return pre.getObject();
359
      }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
    public int getScreenWidthInPixels()
364
      {
365
      return mScreenWidth;
366
      }
367

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

    
370
    public int getScreenHeightInPixels()
371
      {
372
      return mScreenHeight;
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    public RubikPreRender getPreRender()
378
      {
379
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
380
      return view.getPreRender();
381
      }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
386
      {
387
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
388
      RubikPreRender pre = view.getPreRender();
389

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

    
408
        if( BuildConfig.DEBUG )
409
          {
410
          android.util.Log.e("rubik", name.toString());
411
          }
412
        else
413
          {
414
          FirebaseAnalytics analytics = getAnalytics();
415

    
416
          if( analytics!=null )
417
            {
418
            Bundle bundle = new Bundle();
419
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
420
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
421
            }
422
          }
423
        }
424

    
425
      pre.changeObject(newObject,newSize);
426
      }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
    public void setupObject(ObjectList object, int size, int[][] moves)
431
      {
432
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
433
      RubikPreRender pre = view.getPreRender();
434
      pre.setupObject(object,size,moves);
435
      }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
    public static int getDrawableSize()
440
      {
441
      if( mScreenHeight<1000 )
442
        {
443
        return 0;
444
        }
445
      if( mScreenHeight<1600 )
446
        {
447
        return 1;
448
        }
449
      if( mScreenHeight<1900 )
450
        {
451
        return 2;
452
        }
453

    
454
      return 3;
455
      }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
    public static int getDrawable(int small, int medium, int big, int huge)
460
      {
461
      int size = getDrawableSize();
462

    
463
      switch(size)
464
        {
465
        case 0 : return small;
466
        case 1 : return medium;
467
        case 2 : return big;
468
        default: return huge;
469
        }
470
      }
471

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

    
474
    public void acceptPrivacy()
475
      {
476
      mPolicyAccepted = true;
477
      }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
    public void declinePrivacy()
482
      {
483
      finish();
484
      }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
    public static boolean localeIsChinese()
489
      {
490
      String language;
491

    
492
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
493
        {
494
        language= LocaleList.getDefault().get(0).getLanguage();
495
        }
496
      else
497
        {
498
        language= Locale.getDefault().getLanguage();
499
        }
500

    
501
      return language.equals("zh");
502
      }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
    public boolean isVertical()
507
      {
508
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
509
      return view.isVertical();
510
      }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

    
514
    public void toggleLock()
515
      {
516
      mIsLocked = !mIsLocked;
517
      }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
    public boolean isLocked()
522
      {
523
      StateList state = StateList.getCurrentState();
524

    
525
      if( state== StateList.PLAY || state== StateList.READ || state== StateList.SOLV )
526
        {
527
        return mIsLocked;
528
        }
529

    
530
      return false;
531
      }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534

    
535
    public boolean retLocked()
536
      {
537
      return mIsLocked;
538
      }
539
}
(1-1/4)