Project

General

Profile

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

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

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.RubikObject;
41
import org.distorted.scores.RubikScores;
42
import org.distorted.scores.RubikScoresDownloader;
43
import org.distorted.objects.RubikObjectList;
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 BITMAP_TEXT_SIZE    = 0.06f;
59
    public static final float MENU_ITEM_SIZE      = 0.12f;
60
    public static final float PATTERN_GROUP_TEXT  = 0.03f;
61
    public static final float PATTERN_CHILD_TEXT  = 0.02f;
62
    public static final float SCORES_LEVEL_TEXT   = 0.035f;
63
    public static final float SCORES_ITEM_TEXT    = 0.030f;
64

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

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

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

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

    
93
      mJustStarted = true;
94
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
95

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

    
101
      mIsChinese = localeIsChinese();
102

    
103
      hideNavigationBar();
104
      huaweiHack();
105

    
106
      // askForPermissions();
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
    private void hideNavigationBar()
112
      {
113
      mCurrentApiVersion = Build.VERSION.SDK_INT;
114

    
115
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
116
        {
117
        final View decorView = getWindow().getDecorView();
118

    
119
        decorView.setSystemUiVisibility(FLAGS);
120

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

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

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    @Override
151
    public void onWindowFocusChanged(boolean hasFocus)
152
      {
153
      super.onWindowFocusChanged(hasFocus);
154

    
155
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
156
        {
157
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
158
        }
159
      }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    private void askForPermissions()
164
      {
165
      final int REQUEST_EXTERNAL_STORAGE = 1;
166

    
167
      String[] PERMISSIONS_STORAGE =
168
        {
169
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
170
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
171
        };
172

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

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

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

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

    
206
      if( mJustStarted )
207
        {
208
        mJustStarted = false;
209
        RubikScores scores = RubikScores.getInstance();
210
        scores.incrementNumRuns();
211
        scores.setCountry(this);
212
        }
213

    
214
      boolean success = false;
215
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
216
      int object = play.getObject();
217
      int size   = play.getSize();
218

    
219
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
220
        {
221
        RubikObjectList obj = RubikObjectList.getObject(object);
222
        int[] sizes = obj.getSizes();
223
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
224

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

    
232
      if( !success )
233
        {
234
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
235
        int s = RubikStatePlay.DEF_SIZE;
236

    
237
        play.setObjectAndSize(this,obj,s);
238
        view.getPreRender().changeObject(obj,s);
239
        }
240

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
    private void savePreferences()
256
      {
257
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
258
      SharedPreferences.Editor editor = preferences.edit();
259

    
260
      editor.putBoolean("policyAccepted", mPolicyAccepted);
261

    
262
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
263
        {
264
        BaseEffect.Type.getType(i).savePreferences(editor);
265
        }
266

    
267
      for (int i=0; i<RubikState.LENGTH; i++)
268
        {
269
        RubikState.getState(i).getStateClass().savePreferences(editor);
270
        }
271

    
272
      RubikState.savePreferences(editor);
273
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
274
      view.getPreRender().savePreferences(editor);
275

    
276
      editor.apply();
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    private void restorePreferences()
282
      {
283
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
284

    
285
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
286

    
287
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
288
        {
289
        BaseEffect.Type.getType(i).restorePreferences(preferences);
290
        }
291

    
292
      for (int i=0; i< RubikState.LENGTH; i++)
293
        {
294
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
295
        }
296

    
297
      RubikState.restorePreferences(preferences);
298

    
299
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
300
      view.getPreRender().restorePreferences(preferences);
301
      }
302

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

    
305
    private void PrivacyPolicy()
306
      {
307
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
308
      priDiag.show(getSupportFragmentManager(), null);
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
    void OpenGLError(String message)
314
      {
315
      Bundle bundle = new Bundle();
316
      bundle.putString("error", message );
317

    
318
      RubikDialogError errDiag = new RubikDialogError();
319
      errDiag.setArguments(bundle);
320
      errDiag.show(getSupportFragmentManager(), null);
321
      }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324
// PUBLIC API
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    public FirebaseAnalytics getAnalytics()
328
      {
329
      return mFirebaseAnalytics;
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    public RubikObject getObject()
335
      {
336
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
337
      RubikPreRender pre = view.getPreRender();
338
      return pre.getObject();
339
      }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
    public int getScreenWidthInPixels()
344
      {
345
      return mScreenWidth;
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
    public int getScreenHeightInPixels()
351
      {
352
      return mScreenHeight;
353
      }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
    public RubikPreRender getPreRender()
358
      {
359
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
360
      return view.getPreRender();
361
      }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
    public void changeObject(RubikObjectList newObject, int newSize, boolean reportChange)
366
      {
367
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
368
      RubikPreRender pre = view.getPreRender();
369

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

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

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

    
405
      pre.changeObject(newObject,newSize);
406
      }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
    public void setupObject(RubikObjectList object, int size, int[][] moves)
411
      {
412
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
413
      RubikPreRender pre = view.getPreRender();
414
      pre.setupObject(object,size,moves);
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

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

    
434
      return 3;
435
      }
436

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

    
439
    public static int getDrawable(int small, int medium, int big, int huge)
440
      {
441
      int size = getDrawableSize();
442

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

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
    public void acceptPrivacy()
455
      {
456
      mPolicyAccepted = true;
457
      }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
    public void declinePrivacy()
462
      {
463
      finish();
464
      }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
    public static boolean localeIsChinese()
469
      {
470
      String language;
471

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

    
481
      return language.equals("zh");
482
      }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
    public boolean isVertical()
487
      {
488
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
489
      return view.isVertical();
490
      }
491
}
(1-1/4)