Project

General

Profile

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

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

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
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
98
      view.onPause();
99
      DistortedLibrary.onPause();
100
      RubikScoresDownloader.onPause();
101
      savePreferences();
102
      super.onPause();
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

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

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

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

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

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

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

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

    
188
      editor.apply();
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

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

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

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

    
209
      RubikState.restorePreferences(preferences);
210

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
// PUBLIC API
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

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

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

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

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

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

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

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

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

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

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

    
345
      return 3;
346
      }
347

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

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

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

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

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

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

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

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

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

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

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