Project

General

Profile

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

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

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

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

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

    
39
import org.distorted.objects.RubikObject;
40
import org.distorted.scores.RubikScores;
41
import org.distorted.scores.RubikScoresDownloader;
42
import org.distorted.objects.RubikObjectList;
43
import org.distorted.states.RubikState;
44
import org.distorted.states.RubikStatePlay;
45

    
46
import java.util.Locale;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

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

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

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

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

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

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

    
100
      mIsChinese = localeIsChinese();
101

    
102
      hideNavigationBar();
103

    
104
      // askForPermissions();
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
    private void hideNavigationBar()
110
      {
111
      mCurrentApiVersion = android.os.Build.VERSION.SDK_INT;
112

    
113
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
114
        {
115
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
116

    
117
        final View decorView = getWindow().getDecorView();
118

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    @Override
136
    public void onWindowFocusChanged(boolean hasFocus)
137
      {
138
      super.onWindowFocusChanged(hasFocus);
139

    
140
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
141
        {
142
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
143
        }
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    private void askForPermissions()
149
      {
150
      final int REQUEST_EXTERNAL_STORAGE = 1;
151

    
152
      String[] PERMISSIONS_STORAGE =
153
        {
154
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
155
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
156
        };
157

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

    
160
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
161
        {
162
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
163
        }
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    @Override
169
    protected void onPause() 
170
      {
171
      super.onPause();
172
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
173
      view.onPause();
174
      DistortedLibrary.onPause();
175
      RubikScoresDownloader.onPause();
176
      savePreferences();
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
    
181
    @Override
182
    protected void onResume() 
183
      {
184
      super.onResume();
185
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
186
      view.onResume();
187
      view.initialize();
188
      restorePreferences();
189
      RubikState.setState(this);
190

    
191
      if( mJustStarted )
192
        {
193
        mJustStarted = false;
194
        RubikScores scores = RubikScores.getInstance();
195
        scores.incrementNumRuns();
196
        scores.setCountry(this);
197
        }
198

    
199
      boolean success = false;
200
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
201
      int object = play.getObject();
202
      int size   = play.getSize();
203

    
204
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
205
        {
206
        RubikObjectList obj = RubikObjectList.getObject(object);
207
        int[] sizes = obj.getSizes();
208
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
209

    
210
        if( sizeIndex>=0 && sizeIndex<sizes.length )
211
          {
212
          success = true;
213
          view.getPreRender().changeObject(obj,size);
214
          }
215
        }
216

    
217
      if( !success )
218
        {
219
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
220
        int s = RubikStatePlay.DEF_SIZE;
221

    
222
        play.setObjectAndSize(this,obj,s);
223
        view.getPreRender().changeObject(obj,s);
224
        }
225

    
226
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
227
      }
228
    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
    
231
    @Override
232
    protected void onDestroy() 
233
      {
234
      DistortedLibrary.onDestroy();
235
      super.onDestroy();
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
    private void savePreferences()
241
      {
242
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
243
      SharedPreferences.Editor editor = preferences.edit();
244

    
245
      editor.putBoolean("policyAccepted", mPolicyAccepted);
246

    
247
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
248
        {
249
        BaseEffect.Type.getType(i).savePreferences(editor);
250
        }
251

    
252
      for (int i=0; i<RubikState.LENGTH; i++)
253
        {
254
        RubikState.getState(i).getStateClass().savePreferences(editor);
255
        }
256

    
257
      RubikState.savePreferences(editor);
258
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
259
      view.getPreRender().savePreferences(editor);
260

    
261
      editor.apply();
262
      }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
    private void restorePreferences()
267
      {
268
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
269

    
270
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
271

    
272
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
273
        {
274
        BaseEffect.Type.getType(i).restorePreferences(preferences);
275
        }
276

    
277
      for (int i=0; i< RubikState.LENGTH; i++)
278
        {
279
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
280
        }
281

    
282
      RubikState.restorePreferences(preferences);
283

    
284
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
285
      view.getPreRender().restorePreferences(preferences);
286
      }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
    private void PrivacyPolicy()
291
      {
292
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
293
      priDiag.show(getSupportFragmentManager(), null);
294
      }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
    void OpenGLError(String message)
299
      {
300
      Bundle bundle = new Bundle();
301
      bundle.putString("error", message );
302

    
303
      RubikDialogError errDiag = new RubikDialogError();
304
      errDiag.setArguments(bundle);
305
      errDiag.show(getSupportFragmentManager(), null);
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
// PUBLIC API
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
    public FirebaseAnalytics getAnalytics()
313
      {
314
      return mFirebaseAnalytics;
315
      }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
    public RubikObject getObject()
320
      {
321
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
322
      RubikPreRender pre = view.getPreRender();
323
      return pre.getObject();
324
      }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
    public int getScreenWidthInPixels()
329
      {
330
      return mScreenWidth;
331
      }
332

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

    
335
    public int getScreenHeightInPixels()
336
      {
337
      return mScreenHeight;
338
      }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
    public RubikPreRender getPreRender()
343
      {
344
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
345
      return view.getPreRender();
346
      }
347

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

    
350
    public void changeObject(RubikObjectList newObject, int newSize, boolean reportChange)
351
      {
352
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
353
      RubikPreRender pre = view.getPreRender();
354

    
355
      if( reportChange )
356
        {
357
        RubikObject oldObject = pre.getObject();
358
        RubikObjectList oldList = oldObject.getObjectList();
359
        int oldSize = oldObject.getSize();
360
        float fps = view.getRenderer().getFPS();
361
        fps = (int)(fps+0.5f);
362
        StringBuilder name = new StringBuilder();
363
        name.append(oldList.name());
364
        name.append('_');
365
        name.append(oldSize);
366
        name.append(' ');
367
        name.append(fps);
368
        name.append(" --> ");
369
        name.append(newObject.name());
370
        name.append('_');
371
        name.append(newSize);
372

    
373
        if( BuildConfig.DEBUG )
374
          {
375
          android.util.Log.e("rubik", name.toString());
376
          }
377
        else
378
          {
379
          FirebaseAnalytics analytics = getAnalytics();
380

    
381
          if( analytics!=null )
382
            {
383
            Bundle bundle = new Bundle();
384
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
385
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
386
            }
387
          }
388
        }
389

    
390
      pre.changeObject(newObject,newSize);
391
      }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
    public void setupObject(RubikObjectList object, int size, int[][] moves)
396
      {
397
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
398
      RubikPreRender pre = view.getPreRender();
399
      pre.setupObject(object,size,moves);
400
      }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
    public static int getDrawableSize()
405
      {
406
      if( mScreenHeight<1000 )
407
        {
408
        return 0;
409
        }
410
      if( mScreenHeight<1600 )
411
        {
412
        return 1;
413
        }
414
      if( mScreenHeight<1900 )
415
        {
416
        return 2;
417
        }
418

    
419
      return 3;
420
      }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
    public static int getDrawable(int small, int medium, int big, int huge)
425
      {
426
      int size = getDrawableSize();
427

    
428
      switch(size)
429
        {
430
        case 0 : return small;
431
        case 1 : return medium;
432
        case 2 : return big;
433
        default: return huge;
434
        }
435
      }
436

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

    
439
    public void acceptPrivacy()
440
      {
441
      mPolicyAccepted = true;
442
      }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
    public void declinePrivacy()
447
      {
448
      finish();
449
      }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
    public static boolean localeIsChinese()
454
      {
455
      String language;
456

    
457
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
458
        {
459
        language= LocaleList.getDefault().get(0).getLanguage();
460
        }
461
      else
462
        {
463
        language= Locale.getDefault().getLanguage();
464
        }
465

    
466
      return language.equals("zh");
467
      }
468

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

    
471
    public boolean isVertical()
472
      {
473
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
474
      return view.isVertical();
475
      }
476
}
(1-1/4)