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
|
private boolean mJustStarted;
|
69
|
private FirebaseAnalytics mFirebaseAnalytics;
|
70
|
private static int mScreenWidth, mScreenHeight;
|
71
|
private boolean mPolicyAccepted, mIsChinese;
|
72
|
private int mCurrentApiVersion;
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
@Override
|
77
|
protected void onCreate(Bundle savedState)
|
78
|
{
|
79
|
super.onCreate(savedState);
|
80
|
setTheme(R.style.CustomActivityThemeNoActionBar);
|
81
|
setContentView(R.layout.main);
|
82
|
|
83
|
mJustStarted = true;
|
84
|
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
|
85
|
|
86
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
87
|
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
|
88
|
mScreenWidth =displaymetrics.widthPixels;
|
89
|
mScreenHeight=displaymetrics.heightPixels;
|
90
|
|
91
|
mIsChinese = localeIsChinese();
|
92
|
|
93
|
hideNavigationBar();
|
94
|
|
95
|
// askForPermissions();
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
private void hideNavigationBar()
|
101
|
{
|
102
|
mCurrentApiVersion = android.os.Build.VERSION.SDK_INT;
|
103
|
|
104
|
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
105
|
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
106
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
107
|
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
108
|
| View.SYSTEM_UI_FLAG_FULLSCREEN
|
109
|
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
|
110
|
|
111
|
// This work only for android 4.4+
|
112
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
|
113
|
{
|
114
|
getWindow().getDecorView().setSystemUiVisibility(flags);
|
115
|
|
116
|
// Code below is to handle presses of Volume up or Volume down.
|
117
|
// Without this, after pressing volume buttons, the navigation bar will
|
118
|
// show up and won't hide
|
119
|
final View decorView = getWindow().getDecorView();
|
120
|
|
121
|
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
|
122
|
{
|
123
|
@Override
|
124
|
public void onSystemUiVisibilityChange(int visibility)
|
125
|
{
|
126
|
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
|
127
|
{
|
128
|
decorView.setSystemUiVisibility(flags);
|
129
|
}
|
130
|
}
|
131
|
});
|
132
|
}
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
@Override
|
138
|
public void onWindowFocusChanged(boolean hasFocus)
|
139
|
{
|
140
|
super.onWindowFocusChanged(hasFocus);
|
141
|
|
142
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
|
143
|
{
|
144
|
getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
145
|
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
146
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
147
|
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
148
|
| View.SYSTEM_UI_FLAG_FULLSCREEN
|
149
|
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154
|
|
155
|
private void askForPermissions()
|
156
|
{
|
157
|
final int REQUEST_EXTERNAL_STORAGE = 1;
|
158
|
|
159
|
String[] PERMISSIONS_STORAGE =
|
160
|
{
|
161
|
android.Manifest.permission.READ_EXTERNAL_STORAGE,
|
162
|
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
|
163
|
};
|
164
|
|
165
|
int permission = androidx.core.app.ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
166
|
|
167
|
if (permission != android.content.pm.PackageManager.PERMISSION_GRANTED)
|
168
|
{
|
169
|
androidx.core.app.ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE );
|
170
|
}
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
@Override
|
176
|
protected void onPause()
|
177
|
{
|
178
|
super.onPause();
|
179
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
180
|
view.onPause();
|
181
|
DistortedLibrary.onPause();
|
182
|
RubikScoresDownloader.onPause();
|
183
|
savePreferences();
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
@Override
|
189
|
protected void onResume()
|
190
|
{
|
191
|
super.onResume();
|
192
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
193
|
view.onResume();
|
194
|
view.initialize();
|
195
|
restorePreferences();
|
196
|
RubikState.setState(this);
|
197
|
|
198
|
if( mJustStarted )
|
199
|
{
|
200
|
mJustStarted = false;
|
201
|
RubikScores scores = RubikScores.getInstance();
|
202
|
scores.incrementNumRuns();
|
203
|
scores.setCountry(this);
|
204
|
}
|
205
|
|
206
|
boolean success = false;
|
207
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
208
|
int object = play.getObject();
|
209
|
int size = play.getSize();
|
210
|
|
211
|
if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
|
212
|
{
|
213
|
RubikObjectList obj = RubikObjectList.getObject(object);
|
214
|
int[] sizes = obj.getSizes();
|
215
|
int sizeIndex = RubikObjectList.getSizeIndex(object,size);
|
216
|
|
217
|
if( sizeIndex>=0 && sizeIndex<sizes.length )
|
218
|
{
|
219
|
success = true;
|
220
|
view.getPreRender().changeObject(obj,size);
|
221
|
}
|
222
|
}
|
223
|
|
224
|
if( !success )
|
225
|
{
|
226
|
RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
|
227
|
int s = RubikStatePlay.DEF_SIZE;
|
228
|
|
229
|
play.setObjectAndSize(this,obj,s);
|
230
|
view.getPreRender().changeObject(obj,s);
|
231
|
}
|
232
|
|
233
|
if( mIsChinese && !mPolicyAccepted ) PrivacyPolicy();
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
@Override
|
239
|
protected void onDestroy()
|
240
|
{
|
241
|
DistortedLibrary.onDestroy();
|
242
|
super.onDestroy();
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
|
247
|
private void savePreferences()
|
248
|
{
|
249
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
250
|
SharedPreferences.Editor editor = preferences.edit();
|
251
|
|
252
|
editor.putBoolean("policyAccepted", mPolicyAccepted);
|
253
|
|
254
|
for (int i=0; i<BaseEffect.Type.LENGTH; i++)
|
255
|
{
|
256
|
BaseEffect.Type.getType(i).savePreferences(editor);
|
257
|
}
|
258
|
|
259
|
for (int i=0; i<RubikState.LENGTH; i++)
|
260
|
{
|
261
|
RubikState.getState(i).getStateClass().savePreferences(editor);
|
262
|
}
|
263
|
|
264
|
RubikState.savePreferences(editor);
|
265
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
266
|
view.getPreRender().savePreferences(editor);
|
267
|
|
268
|
editor.apply();
|
269
|
}
|
270
|
|
271
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272
|
|
273
|
private void restorePreferences()
|
274
|
{
|
275
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
276
|
|
277
|
mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
|
278
|
|
279
|
for (int i=0; i< BaseEffect.Type.LENGTH; i++)
|
280
|
{
|
281
|
BaseEffect.Type.getType(i).restorePreferences(preferences);
|
282
|
}
|
283
|
|
284
|
for (int i=0; i< RubikState.LENGTH; i++)
|
285
|
{
|
286
|
RubikState.getState(i).getStateClass().restorePreferences(preferences);
|
287
|
}
|
288
|
|
289
|
RubikState.restorePreferences(preferences);
|
290
|
|
291
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
292
|
view.getPreRender().restorePreferences(preferences);
|
293
|
}
|
294
|
|
295
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
296
|
|
297
|
private void PrivacyPolicy()
|
298
|
{
|
299
|
RubikDialogPrivacy priDiag = new RubikDialogPrivacy();
|
300
|
priDiag.show(getSupportFragmentManager(), null);
|
301
|
}
|
302
|
|
303
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304
|
|
305
|
void OpenGLError(String message)
|
306
|
{
|
307
|
Bundle bundle = new Bundle();
|
308
|
bundle.putString("error", message );
|
309
|
|
310
|
RubikDialogError errDiag = new RubikDialogError();
|
311
|
errDiag.setArguments(bundle);
|
312
|
errDiag.show(getSupportFragmentManager(), null);
|
313
|
}
|
314
|
|
315
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316
|
// PUBLIC API
|
317
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
318
|
|
319
|
public FirebaseAnalytics getAnalytics()
|
320
|
{
|
321
|
return mFirebaseAnalytics;
|
322
|
}
|
323
|
|
324
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
325
|
|
326
|
public RubikObject getObject()
|
327
|
{
|
328
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
329
|
RubikPreRender pre = view.getPreRender();
|
330
|
return pre.getObject();
|
331
|
}
|
332
|
|
333
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
334
|
|
335
|
public int getScreenWidthInPixels()
|
336
|
{
|
337
|
return mScreenWidth;
|
338
|
}
|
339
|
|
340
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
341
|
|
342
|
public int getScreenHeightInPixels()
|
343
|
{
|
344
|
return mScreenHeight;
|
345
|
}
|
346
|
|
347
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
348
|
|
349
|
public RubikPreRender getPreRender()
|
350
|
{
|
351
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
352
|
return view.getPreRender();
|
353
|
}
|
354
|
|
355
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
356
|
|
357
|
public void changeObject(RubikObjectList newObject, int newSize, boolean reportChange)
|
358
|
{
|
359
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
360
|
RubikPreRender pre = view.getPreRender();
|
361
|
|
362
|
if( reportChange )
|
363
|
{
|
364
|
RubikObject oldObject = pre.getObject();
|
365
|
RubikObjectList oldList = oldObject.getObjectList();
|
366
|
int oldSize = oldObject.getSize();
|
367
|
float fps = view.getRenderer().getFPS();
|
368
|
fps = (int)(fps+0.5f);
|
369
|
StringBuilder name = new StringBuilder();
|
370
|
name.append(oldList.name());
|
371
|
name.append('_');
|
372
|
name.append(oldSize);
|
373
|
name.append(' ');
|
374
|
name.append(fps);
|
375
|
name.append(" --> ");
|
376
|
name.append(newObject.name());
|
377
|
name.append('_');
|
378
|
name.append(newSize);
|
379
|
|
380
|
if( BuildConfig.DEBUG )
|
381
|
{
|
382
|
android.util.Log.e("rubik", name.toString());
|
383
|
}
|
384
|
else
|
385
|
{
|
386
|
FirebaseAnalytics analytics = getAnalytics();
|
387
|
|
388
|
if( analytics!=null )
|
389
|
{
|
390
|
Bundle bundle = new Bundle();
|
391
|
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
|
392
|
analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
|
393
|
}
|
394
|
}
|
395
|
}
|
396
|
|
397
|
pre.changeObject(newObject,newSize);
|
398
|
}
|
399
|
|
400
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
401
|
|
402
|
public void setupObject(RubikObjectList object, int size, int[][] moves)
|
403
|
{
|
404
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
405
|
RubikPreRender pre = view.getPreRender();
|
406
|
pre.setupObject(object,size,moves);
|
407
|
}
|
408
|
|
409
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
410
|
|
411
|
public static int getDrawableSize()
|
412
|
{
|
413
|
if( mScreenHeight<1000 )
|
414
|
{
|
415
|
return 0;
|
416
|
}
|
417
|
if( mScreenHeight<1600 )
|
418
|
{
|
419
|
return 1;
|
420
|
}
|
421
|
if( mScreenHeight<1900 )
|
422
|
{
|
423
|
return 2;
|
424
|
}
|
425
|
|
426
|
return 3;
|
427
|
}
|
428
|
|
429
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
430
|
|
431
|
public static int getDrawable(int small, int medium, int big, int huge)
|
432
|
{
|
433
|
int size = getDrawableSize();
|
434
|
|
435
|
switch(size)
|
436
|
{
|
437
|
case 0 : return small;
|
438
|
case 1 : return medium;
|
439
|
case 2 : return big;
|
440
|
default: return huge;
|
441
|
}
|
442
|
}
|
443
|
|
444
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
445
|
|
446
|
public void acceptPrivacy()
|
447
|
{
|
448
|
mPolicyAccepted = true;
|
449
|
}
|
450
|
|
451
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
452
|
|
453
|
public void declinePrivacy()
|
454
|
{
|
455
|
finish();
|
456
|
}
|
457
|
|
458
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
459
|
|
460
|
public static boolean localeIsChinese()
|
461
|
{
|
462
|
String language;
|
463
|
|
464
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
465
|
{
|
466
|
language= LocaleList.getDefault().get(0).getLanguage();
|
467
|
}
|
468
|
else
|
469
|
{
|
470
|
language= Locale.getDefault().getLanguage();
|
471
|
}
|
472
|
|
473
|
return language.equals("zh");
|
474
|
}
|
475
|
|
476
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
477
|
|
478
|
public boolean isVertical()
|
479
|
{
|
480
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
481
|
return view.isVertical();
|
482
|
}
|
483
|
}
|