Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 44f95b2a

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

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

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

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

    
45
import java.util.Locale;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

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

    
67
    private boolean mJustStarted;
68
    private FirebaseAnalytics mFirebaseAnalytics;
69
    private static int mScreenWidth, mScreenHeight;
70
    private boolean mPolicyAccepted, mIsChinese;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
    @Override
75
    protected void onCreate(Bundle savedState)
76
      {
77
      super.onCreate(savedState);
78
      setTheme(R.style.CustomActivityThemeNoActionBar);
79
      setContentView(R.layout.main);
80

    
81
      mJustStarted = true;
82
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
83

    
84
      DisplayMetrics displaymetrics = new DisplayMetrics();
85
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
86
      mScreenWidth =displaymetrics.widthPixels;
87
      mScreenHeight=displaymetrics.heightPixels;
88

    
89
      mIsChinese = localeIsChinese();
90
/*
91
      final int REQUEST_EXTERNAL_STORAGE = 1;
92

    
93
      String[] PERMISSIONS_STORAGE =
94
        {
95
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
96
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
97
        };
98

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

    
101
      if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
102
        {
103
        androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
104
        }
105
*/
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    @Override
111
    protected void onPause() 
112
      {
113
      super.onPause();
114
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
115
      view.onPause();
116
      DistortedLibrary.onPause();
117
      RubikScoresDownloader.onPause();
118
      savePreferences();
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
    @Override
124
    protected void onResume() 
125
      {
126
      super.onResume();
127
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
128
      view.onResume();
129
      view.initialize();
130
      restorePreferences();
131
      RubikState.setState(this);
132

    
133
      if( mJustStarted )
134
        {
135
        mJustStarted = false;
136
        RubikScores scores = RubikScores.getInstance();
137
        scores.incrementNumRuns();
138
        scores.setCountry(this);
139
        }
140

    
141
      boolean success = false;
142
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
143
      int object = play.getObject();
144
      int size   = play.getSize();
145

    
146
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
147
        {
148
        RubikObjectList obj = RubikObjectList.getObject(object);
149
        int[] sizes = obj.getSizes();
150
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
151

    
152
        if( sizeIndex>=0 && sizeIndex<sizes.length )
153
          {
154
          success = true;
155
          view.getPreRender().changeObject(obj,size);
156
          }
157
        }
158

    
159
      if( !success )
160
        {
161
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
162
        int s = RubikStatePlay.DEF_SIZE;
163

    
164
        play.setObjectAndSize(this,obj,s);
165
        view.getPreRender().changeObject(obj,s);
166
        }
167

    
168
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
169
      }
170
    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
    
173
    @Override
174
    protected void onDestroy() 
175
      {
176
      DistortedLibrary.onDestroy();
177
      super.onDestroy();
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
    private void savePreferences()
183
      {
184
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
185
      SharedPreferences.Editor editor = preferences.edit();
186

    
187
      editor.putBoolean("policyAccepted", mPolicyAccepted);
188

    
189
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
190
        {
191
        BaseEffect.Type.getType(i).savePreferences(editor);
192
        }
193

    
194
      for (int i=0; i<RubikState.LENGTH; i++)
195
        {
196
        RubikState.getState(i).getStateClass().savePreferences(editor);
197
        }
198

    
199
      RubikState.savePreferences(editor);
200
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
201
      view.getPreRender().savePreferences(editor);
202

    
203
      editor.apply();
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    private void restorePreferences()
209
      {
210
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
211

    
212
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
213

    
214
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
215
        {
216
        BaseEffect.Type.getType(i).restorePreferences(preferences);
217
        }
218

    
219
      for (int i=0; i< RubikState.LENGTH; i++)
220
        {
221
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
222
        }
223

    
224
      RubikState.restorePreferences(preferences);
225

    
226
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
227
      view.getPreRender().restorePreferences(preferences);
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    private void PrivacyPolicy()
233
      {
234
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
235
      priDiag.show(getSupportFragmentManager(), null);
236
      }
237

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

    
240
    void OpenGLError(String message)
241
      {
242
      Bundle bundle = new Bundle();
243
      bundle.putString("error", message );
244

    
245
      RubikDialogError errDiag = new RubikDialogError();
246
      errDiag.setArguments(bundle);
247
      errDiag.show(getSupportFragmentManager(), null);
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
// PUBLIC API
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public FirebaseAnalytics getAnalytics()
255
      {
256
      return mFirebaseAnalytics;
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    public RubikObject getObject()
262
      {
263
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
264
      RubikPreRender pre = view.getPreRender();
265
      return pre.getObject();
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    public int getScreenWidthInPixels()
271
      {
272
      return mScreenWidth;
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    public int getScreenHeightInPixels()
278
      {
279
      return mScreenHeight;
280
      }
281

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

    
284
    public RubikPreRender getPreRender()
285
      {
286
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
287
      return view.getPreRender();
288
      }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
    public void changeObject(RubikObjectList newObject, int newSize, boolean reportChange)
293
      {
294
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
295
      RubikPreRender pre = view.getPreRender();
296

    
297
      if( reportChange )
298
        {
299
        RubikObject oldObject = pre.getObject();
300
        RubikObjectList oldList = oldObject.getObjectList();
301
        int oldSize = oldObject.getSize();
302
        float fps = view.getRenderer().getFPS();
303
        fps = (int)(fps+0.5f);
304
        StringBuilder name = new StringBuilder();
305
        name.append(oldList.name());
306
        name.append('_');
307
        name.append(oldSize);
308
        name.append(' ');
309
        name.append(fps);
310
        name.append(" --> ");
311
        name.append(newObject.name());
312
        name.append('_');
313
        name.append(newSize);
314

    
315
        if( BuildConfig.DEBUG )
316
          {
317
          android.util.Log.e("rubik", name.toString());
318
          }
319
        else
320
          {
321
          FirebaseAnalytics analytics = getAnalytics();
322

    
323
          if( analytics!=null )
324
            {
325
            Bundle bundle = new Bundle();
326
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
327
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
328
            }
329
          }
330
        }
331

    
332
      pre.changeObject(newObject,newSize);
333
      }
334

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

    
337
    public void setupObject(RubikObjectList object, int size, int[][] moves)
338
      {
339
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
340
      RubikPreRender pre = view.getPreRender();
341
      pre.setupObject(object,size,moves);
342
      }
343

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

    
346
    public static int getDrawableSize()
347
      {
348
      if( mScreenHeight<1000 )
349
        {
350
        return 0;
351
        }
352
      if( mScreenHeight<1600 )
353
        {
354
        return 1;
355
        }
356
      if( mScreenHeight<1900 )
357
        {
358
        return 2;
359
        }
360

    
361
      return 3;
362
      }
363

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

    
366
    public static int getDrawable(int small, int medium, int big, int huge)
367
      {
368
      int size = getDrawableSize();
369

    
370
      switch(size)
371
        {
372
        case 0 : return small;
373
        case 1 : return medium;
374
        case 2 : return big;
375
        default: return huge;
376
        }
377
      }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
    public void acceptPrivacy()
382
      {
383
      mPolicyAccepted = true;
384
      }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
    public void declinePrivacy()
389
      {
390
      finish();
391
      }
392

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

    
395
    public static boolean localeIsChinese()
396
      {
397
      String language;
398

    
399
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
400
        {
401
        language= LocaleList.getDefault().get(0).getLanguage();
402
        }
403
      else
404
        {
405
        language= Locale.getDefault().getLanguage();
406
        }
407

    
408
      return language.equals("zh");
409
      }
410

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

    
413
    public boolean isVertical()
414
      {
415
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
416
      return view.isVertical();
417
      }
418
}
(1-1/4)