Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 9c2f0c91

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.WindowManager;
32

    
33
import com.google.firebase.analytics.FirebaseAnalytics;
34

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

    
40
import org.distorted.objects.TwistyObject;
41
import org.distorted.scores.RubikScores;
42
import org.distorted.scores.RubikScoresDownloader;
43
import org.distorted.objects.ObjectList;
44
import org.distorted.states.RubikState;
45
import org.distorted.states.RubikStatePlay;
46

    
47
import java.util.Locale;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
public class RubikActivity extends AppCompatActivity
52
{
53
    public static final float PADDING             = 0.01f;
54
    public static final float MARGIN              = 0.004f;
55
    public static final float LARGE_MARGIN        = 0.025f;
56
    public static final float BUTTON_TEXT_SIZE    = 0.05f;
57
    public static final float TITLE_TEXT_SIZE     = 0.06f;
58
    public static final float SOLVER_BMP_H_SIZE   = 0.11f;
59
    public static final float SOLVER_BMP_V_SIZE   = 0.06f;
60
    public static final float MENU_ITEM_SIZE      = 0.12f;
61
    public static final float PATTERN_GROUP_TEXT  = 0.03f;
62
    public static final float PATTERN_CHILD_TEXT  = 0.02f;
63
    public static final float SCORES_LEVEL_TEXT   = 0.035f;
64
    public static final float SCORES_ITEM_TEXT    = 0.030f;
65

    
66
    public static final float MENU_BIG_TEXT_SIZE   = 0.05f;
67
    public static final float MENU_MEDIUM_TEXT_SIZE= 0.04f;
68
    public static final float MENU_SMALL_TEXT_SIZE = 0.035f;
69

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

    
76
    public static final int FLAGS2=  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
77
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
78

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

    
95
      mJustStarted = true;
96
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
97

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

    
103
      mIsChinese = localeIsChinese();
104
      mIsLocked = false;
105

    
106
      hideNavigationBar();
107
      huaweiHack();
108

    
109
      // askForPermissions();
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    private void hideNavigationBar()
115
      {
116
      mCurrentApiVersion = Build.VERSION.SDK_INT;
117

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

    
122
        decorView.setSystemUiVisibility(FLAGS);
123

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

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

    
143
    private void huaweiHack()
144
      {
145
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
146
        {
147
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
148
        }
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    @Override
154
    public void onWindowFocusChanged(boolean hasFocus)
155
      {
156
      super.onWindowFocusChanged(hasFocus);
157

    
158
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
159
        {
160
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    private void askForPermissions()
167
      {
168
      final int REQUEST_EXTERNAL_STORAGE = 1;
169

    
170
      String[] PERMISSIONS_STORAGE =
171
        {
172
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
173
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
174
        };
175

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

    
178
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
179
        {
180
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
181
        }
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
    
186
    @Override
187
    protected void onPause() 
188
      {
189
      super.onPause();
190
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
191
      view.onPause();
192
      DistortedLibrary.onPause();
193
      RubikScoresDownloader.onPause();
194
      savePreferences();
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
    
199
    @Override
200
    protected void onResume() 
201
      {
202
      super.onResume();
203
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
204
      view.onResume();
205
      view.initialize();
206
      restorePreferences();
207
      RubikState.setState(this);
208

    
209
      if( mJustStarted )
210
        {
211
        mJustStarted = false;
212
        RubikScores scores = RubikScores.getInstance();
213
        scores.incrementNumRuns();
214
        scores.setCountry(this);
215
        }
216

    
217
      boolean success = false;
218
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
219
      int object = play.getObject();
220
      int size   = play.getSize();
221

    
222
      if( object>=0 && object< ObjectList.NUM_OBJECTS )
223
        {
224
        ObjectList obj = ObjectList.getObject(object);
225
        int[] sizes = obj.getSizes();
226
        int sizeIndex = ObjectList.getSizeIndex(object,size);
227

    
228
        if( sizeIndex>=0 && sizeIndex<sizes.length )
229
          {
230
          success = true;
231
          view.getPreRender().changeObject(obj,size);
232
          }
233
        }
234

    
235
      if( !success )
236
        {
237
        ObjectList obj = ObjectList.getObject(RubikStatePlay.DEF_OBJECT);
238
        int s = RubikStatePlay.DEF_SIZE;
239

    
240
        play.setObjectAndSize(this,obj,s);
241
        view.getPreRender().changeObject(obj,s);
242
        }
243

    
244
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
245
      }
246
    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
    
249
    @Override
250
    protected void onDestroy() 
251
      {
252
      DistortedLibrary.onDestroy();
253
      super.onDestroy();
254
      }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
    private void savePreferences()
259
      {
260
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
261
      SharedPreferences.Editor editor = preferences.edit();
262

    
263
      editor.putBoolean("policyAccepted", mPolicyAccepted);
264

    
265
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
266
        {
267
        BaseEffect.Type.getType(i).savePreferences(editor);
268
        }
269

    
270
      for (int i=0; i<RubikState.LENGTH; i++)
271
        {
272
        RubikState.getState(i).getStateClass().savePreferences(editor);
273
        }
274

    
275
      RubikState.savePreferences(editor);
276
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
277
      view.getPreRender().savePreferences(editor);
278

    
279
      editor.apply();
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    private void restorePreferences()
285
      {
286
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
287

    
288
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
289

    
290
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
291
        {
292
        BaseEffect.Type.getType(i).restorePreferences(preferences);
293
        }
294

    
295
      for (int i=0; i< RubikState.LENGTH; i++)
296
        {
297
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
298
        }
299

    
300
      RubikState.restorePreferences(preferences);
301

    
302
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
303
      view.getPreRender().restorePreferences(preferences);
304
      }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
    private void PrivacyPolicy()
309
      {
310
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
311
      priDiag.show(getSupportFragmentManager(), null);
312
      }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
    void OpenGLError(String message)
317
      {
318
      Bundle bundle = new Bundle();
319
      bundle.putString("error", message );
320

    
321
      RubikDialogError errDiag = new RubikDialogError();
322
      errDiag.setArguments(bundle);
323
      errDiag.show(getSupportFragmentManager(), null);
324
      }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
// PUBLIC API
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
    public FirebaseAnalytics getAnalytics()
331
      {
332
      return mFirebaseAnalytics;
333
      }
334

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

    
337
    public TwistyObject getObject()
338
      {
339
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
340
      RubikPreRender pre = view.getPreRender();
341
      return pre.getObject();
342
      }
343

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

    
346
    public int getScreenWidthInPixels()
347
      {
348
      return mScreenWidth;
349
      }
350

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

    
353
    public int getScreenHeightInPixels()
354
      {
355
      return mScreenHeight;
356
      }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
    public RubikPreRender getPreRender()
361
      {
362
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
363
      return view.getPreRender();
364
      }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
    public void changeObject(ObjectList newObject, int newSize, boolean reportChange)
369
      {
370
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
371
      RubikPreRender pre = view.getPreRender();
372

    
373
      if( reportChange )
374
        {
375
        TwistyObject oldObject = pre.getObject();
376
        ObjectList oldList = oldObject.getObjectList();
377
        int oldSize = oldObject.getSize();
378
        float fps = view.getRenderer().getFPS();
379
        fps = (int)(fps+0.5f);
380
        StringBuilder name = new StringBuilder();
381
        name.append(oldList.name());
382
        name.append('_');
383
        name.append(oldSize);
384
        name.append(' ');
385
        name.append(fps);
386
        name.append(" --> ");
387
        name.append(newObject.name());
388
        name.append('_');
389
        name.append(newSize);
390

    
391
        if( BuildConfig.DEBUG )
392
          {
393
          android.util.Log.e("rubik", name.toString());
394
          }
395
        else
396
          {
397
          FirebaseAnalytics analytics = getAnalytics();
398

    
399
          if( analytics!=null )
400
            {
401
            Bundle bundle = new Bundle();
402
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
403
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
404
            }
405
          }
406
        }
407

    
408
      pre.changeObject(newObject,newSize);
409
      }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
    public void setupObject(ObjectList object, int size, int[][] moves)
414
      {
415
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
416
      RubikPreRender pre = view.getPreRender();
417
      pre.setupObject(object,size,moves);
418
      }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
    public static int getDrawableSize()
423
      {
424
      if( mScreenHeight<1000 )
425
        {
426
        return 0;
427
        }
428
      if( mScreenHeight<1600 )
429
        {
430
        return 1;
431
        }
432
      if( mScreenHeight<1900 )
433
        {
434
        return 2;
435
        }
436

    
437
      return 3;
438
      }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
    public static int getDrawable(int small, int medium, int big, int huge)
443
      {
444
      int size = getDrawableSize();
445

    
446
      switch(size)
447
        {
448
        case 0 : return small;
449
        case 1 : return medium;
450
        case 2 : return big;
451
        default: return huge;
452
        }
453
      }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

    
457
    public void acceptPrivacy()
458
      {
459
      mPolicyAccepted = true;
460
      }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
    public void declinePrivacy()
465
      {
466
      finish();
467
      }
468

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

    
471
    public static boolean localeIsChinese()
472
      {
473
      String language;
474

    
475
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
476
        {
477
        language= LocaleList.getDefault().get(0).getLanguage();
478
        }
479
      else
480
        {
481
        language= Locale.getDefault().getLanguage();
482
        }
483

    
484
      return language.equals("zh");
485
      }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
    public boolean isVertical()
490
      {
491
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
492
      return view.isVertical();
493
      }
494

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

    
497
    public void toggleLock()
498
      {
499
      mIsLocked = !mIsLocked;
500
      }
501

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

    
504
    public boolean isLocked()
505
      {
506
      RubikState state = RubikState.getCurrentState();
507

    
508
      if( state==RubikState.PLAY || state==RubikState.READ || state==RubikState.SOLV )
509
        {
510
        return mIsLocked;
511
        }
512

    
513
      return false;
514
      }
515

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

    
518
    public boolean retLocked()
519
      {
520
      return mIsLocked;
521
      }
522
}
(1-1/4)