Project

General

Profile

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

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

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.05f;
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_STABLE
69
                                   | 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
    private boolean mJustStarted;
76
    private FirebaseAnalytics mFirebaseAnalytics;
77
    private static int mScreenWidth, mScreenHeight;
78
    private boolean mPolicyAccepted, mIsChinese;
79
    private int mCurrentApiVersion;
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

    
90
      mJustStarted = true;
91
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
92

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

    
98
      mIsChinese = localeIsChinese();
99

    
100
      hideNavigationBar();
101

    
102
      // askForPermissions();
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

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

    
115
        final View decorView = getWindow().getDecorView();
116

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

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

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

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

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
    private void askForPermissions()
147
      {
148
      final int REQUEST_EXTERNAL_STORAGE = 1;
149

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

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

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

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

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

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

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

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

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

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

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

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

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

    
243
      editor.putBoolean("policyAccepted", mPolicyAccepted);
244

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

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

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

    
259
      editor.apply();
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
268
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
269

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

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

    
280
      RubikState.restorePreferences(preferences);
281

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

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

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

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

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

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307
// PUBLIC API
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    public FirebaseAnalytics getAnalytics()
311
      {
312
      return mFirebaseAnalytics;
313
      }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    public int getScreenWidthInPixels()
327
      {
328
      return mScreenWidth;
329
      }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
    public int getScreenHeightInPixels()
334
      {
335
      return mScreenHeight;
336
      }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

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

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

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

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

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

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

    
388
      pre.changeObject(newObject,newSize);
389
      }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

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

    
417
      return 3;
418
      }
419

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

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

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

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
    public void acceptPrivacy()
438
      {
439
      mPolicyAccepted = true;
440
      }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
    public void declinePrivacy()
445
      {
446
      finish();
447
      }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
    public static boolean localeIsChinese()
452
      {
453
      String language;
454

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

    
464
      return language.equals("zh");
465
      }
466

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

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