Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 7ea57482

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 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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

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

    
102
      mIsChinese = localeIsChinese();
103

    
104
      hideNavigationBar();
105
      huaweiHack();
106

    
107
      // askForPermissions();
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

    
120
        decorView.setSystemUiVisibility(FLAGS);
121

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

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

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

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

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

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

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

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

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

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

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

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

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

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

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

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

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

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

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

    
277
      editor.apply();
278
      }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

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

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

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

    
298
      RubikState.restorePreferences(preferences);
299

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

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

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

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

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
// PUBLIC API
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

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

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

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

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

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

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

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

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

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

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

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

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

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

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

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

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

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

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

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

    
435
      return 3;
436
      }
437

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

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

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

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

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

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

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

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

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

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

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

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

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