Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 82f42eeb

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
    
94
    @Override
95
    protected void onPause() 
96
      {
97
      super.onPause();
98
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
99
      view.onPause();
100
      DistortedLibrary.onPause();
101
      RubikScoresDownloader.onPause();
102
      savePreferences();
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
    @Override
108
    protected void onResume() 
109
      {
110
      super.onResume();
111
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
112
      view.onResume();
113
      view.initialize();
114
      restorePreferences();
115
      RubikState.setState(this);
116

    
117
      if( mJustStarted )
118
        {
119
        mJustStarted = false;
120
        RubikScores scores = RubikScores.getInstance();
121
        scores.incrementNumRuns();
122
        scores.setCountry(this);
123
        }
124

    
125
      boolean success = false;
126
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
127
      int object = play.getObject();
128
      int size   = play.getSize();
129

    
130
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
131
        {
132
        RubikObjectList obj = RubikObjectList.getObject(object);
133
        int[] sizes = obj.getSizes();
134
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
135

    
136
        if( sizeIndex>=0 && sizeIndex<sizes.length )
137
          {
138
          success = true;
139
          view.getPreRender().changeObject(obj,size);
140
          }
141
        }
142

    
143
      if( !success )
144
        {
145
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
146
        int s = RubikStatePlay.DEF_SIZE;
147

    
148
        play.setObjectAndSize(this,obj,s);
149
        view.getPreRender().changeObject(obj,s);
150
        }
151

    
152
      if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
153
      }
154
    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
    
157
    @Override
158
    protected void onDestroy() 
159
      {
160
      DistortedLibrary.onDestroy();
161
      super.onDestroy();
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    private void savePreferences()
167
      {
168
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
169
      SharedPreferences.Editor editor = preferences.edit();
170

    
171
      editor.putBoolean("policyAccepted", mPolicyAccepted);
172

    
173
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
174
        {
175
        BaseEffect.Type.getType(i).savePreferences(editor);
176
        }
177

    
178
      for (int i=0; i<RubikState.LENGTH; i++)
179
        {
180
        RubikState.getState(i).getStateClass().savePreferences(editor);
181
        }
182

    
183
      RubikState.savePreferences(editor);
184
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
185
      view.getPreRender().savePreferences(editor);
186

    
187
      editor.apply();
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
    private void restorePreferences()
193
      {
194
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
195

    
196
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
197

    
198
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
199
        {
200
        BaseEffect.Type.getType(i).restorePreferences(preferences);
201
        }
202

    
203
      for (int i=0; i< RubikState.LENGTH; i++)
204
        {
205
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
206
        }
207

    
208
      RubikState.restorePreferences(preferences);
209

    
210
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
211
      view.getPreRender().restorePreferences(preferences);
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    private void PrivacyPolicy()
217
      {
218
      RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
219
      priDiag.show(getSupportFragmentManager(), null);
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    void OpenGLError(String message)
225
      {
226
      Bundle bundle = new Bundle();
227
      bundle.putString("error", message );
228

    
229
      RubikDialogError errDiag = new RubikDialogError();
230
      errDiag.setArguments(bundle);
231
      errDiag.show(getSupportFragmentManager(), null);
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// PUBLIC API
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public FirebaseAnalytics getAnalytics()
239
      {
240
      return mFirebaseAnalytics;
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public RubikObject getObject()
246
      {
247
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
248
      RubikPreRender pre = view.getPreRender();
249
      return pre.getObject();
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public int getScreenWidthInPixels()
255
      {
256
      return mScreenWidth;
257
      }
258

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

    
261
    public int getScreenHeightInPixels()
262
      {
263
      return mScreenHeight;
264
      }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    public RubikPreRender getPreRender()
269
      {
270
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
271
      return view.getPreRender();
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    public void changeObject(RubikObjectList newObject, int newSize, boolean reportChange)
277
      {
278
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
279
      RubikPreRender pre = view.getPreRender();
280

    
281
      if( reportChange )
282
        {
283
        RubikObject oldObject = pre.getObject();
284
        RubikObjectList oldList = oldObject.getObjectList();
285
        int oldSize = oldObject.getSize();
286
        float fps = view.getRenderer().getFPS();
287
        StringBuilder name = new StringBuilder();
288
        name.append(oldList.name());
289
        name.append('_');
290
        name.append(oldSize);
291
        name.append(' ');
292
        name.append(fps);
293
        name.append(" --> ");
294
        name.append(newObject.name());
295
        name.append('_');
296
        name.append(newSize);
297

    
298
        if( BuildConfig.DEBUG )
299
          {
300
          android.util.Log.e("rubik", name.toString());
301
          }
302
        else
303
          {
304
          FirebaseAnalytics analytics = getAnalytics();
305

    
306
          if( analytics!=null )
307
            {
308
            Bundle bundle = new Bundle();
309
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
310
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
311
            }
312
          }
313
        }
314

    
315
      pre.changeObject(newObject,newSize);
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
    public void setupObject(RubikObjectList object, int size, int[][] moves)
321
      {
322
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
323
      RubikPreRender pre = view.getPreRender();
324
      pre.setupObject(object,size,moves);
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    public static int getDrawableSize()
330
      {
331
      if( mScreenHeight<1000 )
332
        {
333
        return 0;
334
        }
335
      if( mScreenHeight<1600 )
336
        {
337
        return 1;
338
        }
339
      if( mScreenHeight<1900 )
340
        {
341
        return 2;
342
        }
343

    
344
      return 3;
345
      }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
    public static int getDrawable(int small, int medium, int big, int huge)
350
      {
351
      int size = getDrawableSize();
352

    
353
      switch(size)
354
        {
355
        case 0 : return small;
356
        case 1 : return medium;
357
        case 2 : return big;
358
        default: return huge;
359
        }
360
      }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
    public void acceptPrivacy()
365
      {
366
      mPolicyAccepted = true;
367
      }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
    public void declinePrivacy()
372
      {
373
      finish();
374
      }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
    public static boolean localeIsChinese()
379
      {
380
      String language;
381

    
382
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
383
        {
384
        language= LocaleList.getDefault().get(0).getLanguage();
385
        }
386
      else
387
        {
388
        language= Locale.getDefault().getLanguage();
389
        }
390

    
391
      return language.equals("zh");
392
      }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

    
396
    public boolean isVertical()
397
      {
398
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
399
      return view.isVertical();
400
      }
401
}
(1-1/4)