1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.playui;
|
11
|
|
12
|
import java.io.InputStream;
|
13
|
|
14
|
import android.content.SharedPreferences;
|
15
|
import android.os.Build;
|
16
|
import android.os.Bundle;
|
17
|
import android.util.DisplayMetrics;
|
18
|
import android.view.DisplayCutout;
|
19
|
import android.view.View;
|
20
|
import android.view.ViewGroup;
|
21
|
import android.view.WindowManager;
|
22
|
import android.widget.LinearLayout;
|
23
|
|
24
|
import androidx.appcompat.app.AppCompatActivity;
|
25
|
import androidx.preference.PreferenceManager;
|
26
|
|
27
|
import com.google.firebase.analytics.FirebaseAnalytics;
|
28
|
|
29
|
import org.distorted.library.main.DistortedLibrary;
|
30
|
import org.distorted.objectlib.main.InitAssets;
|
31
|
import org.distorted.objectlib.main.ObjectControl;
|
32
|
import org.distorted.objectlib.main.TwistyObject;
|
33
|
import org.distorted.dialogs.RubikDialogError;
|
34
|
import org.distorted.external.RubikFiles;
|
35
|
import org.distorted.main.MainActivity;
|
36
|
import org.distorted.main.R;
|
37
|
import org.distorted.objects.RubikObject;
|
38
|
import org.distorted.objects.RubikObjectList;
|
39
|
import org.distorted.os.OSInterface;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public class PlayActivity extends AppCompatActivity
|
44
|
{
|
45
|
public static final int FLAGS = MainActivity.FLAGS;
|
46
|
public static final float TITLE_TEXT_SIZE= 0.060f;
|
47
|
private static final int ACTIVITY_NUMBER = 6;
|
48
|
private static final float RATIO_BAR = MainActivity.RATIO_BAR;
|
49
|
private static final float RATIO_INSET = 0.09f;
|
50
|
|
51
|
private static final String KEY_FREE = "movesController_free";
|
52
|
private static final String KEY_SOLV = "movesController_solv";
|
53
|
|
54
|
private static int mScreenWidth, mScreenHeight;
|
55
|
private int mCurrentApiVersion;
|
56
|
private String mObjectName;
|
57
|
private int mNumScrambles;
|
58
|
private int mHeightUpperBar;
|
59
|
private boolean mObjectLocal;
|
60
|
private int mObjectOrdinal;
|
61
|
private boolean mModeFree;
|
62
|
private FirebaseAnalytics mFirebaseAnalytics;
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
@Override
|
67
|
protected void onCreate(Bundle savedState)
|
68
|
{
|
69
|
super.onCreate(savedState);
|
70
|
DistortedLibrary.onCreate(ACTIVITY_NUMBER);
|
71
|
setTheme(R.style.MaterialThemeNoActionBar);
|
72
|
setContentView(R.layout.play);
|
73
|
|
74
|
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
|
75
|
|
76
|
Bundle b = getIntent().getExtras();
|
77
|
|
78
|
if( b!=null )
|
79
|
{
|
80
|
mObjectName = b.getString("name");
|
81
|
mNumScrambles = b.getInt("scrambles");
|
82
|
mObjectLocal = b.getBoolean("local");
|
83
|
mObjectOrdinal = b.getInt("ordinal");
|
84
|
mModeFree = b.getBoolean("free");
|
85
|
}
|
86
|
else
|
87
|
{
|
88
|
mObjectName = "";
|
89
|
mNumScrambles = 0;
|
90
|
mObjectLocal = true;
|
91
|
mObjectOrdinal = 0;
|
92
|
mModeFree = true;
|
93
|
}
|
94
|
|
95
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
96
|
getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
|
97
|
mScreenWidth =displaymetrics.widthPixels;
|
98
|
mScreenHeight=displaymetrics.heightPixels;
|
99
|
|
100
|
hideNavigationBar();
|
101
|
cutoutHack();
|
102
|
computeBarHeights();
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
// this does not include possible insets
|
107
|
|
108
|
private void computeBarHeights()
|
109
|
{
|
110
|
int barHeight = (int)(mScreenHeight*RATIO_BAR);
|
111
|
mHeightUpperBar = barHeight;
|
112
|
|
113
|
LinearLayout layoutTop = findViewById(R.id.upperBar);
|
114
|
LinearLayout layoutBot = findViewById(R.id.lowerBar);
|
115
|
|
116
|
ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
|
117
|
paramsTop.height = mHeightUpperBar;
|
118
|
layoutTop.setLayoutParams(paramsTop);
|
119
|
ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
|
120
|
paramsBot.height = barHeight;
|
121
|
layoutBot.setLayoutParams(paramsBot);
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
|
126
|
private void hideNavigationBar()
|
127
|
{
|
128
|
mCurrentApiVersion = Build.VERSION.SDK_INT;
|
129
|
|
130
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
|
131
|
{
|
132
|
final View decorView = getWindow().getDecorView();
|
133
|
|
134
|
decorView.setSystemUiVisibility(FLAGS);
|
135
|
|
136
|
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
|
137
|
{
|
138
|
@Override
|
139
|
public void onSystemUiVisibilityChange(int visibility)
|
140
|
{
|
141
|
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
|
142
|
{
|
143
|
decorView.setSystemUiVisibility(FLAGS);
|
144
|
}
|
145
|
}
|
146
|
});
|
147
|
}
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
@Override
|
153
|
public void onAttachedToWindow()
|
154
|
{
|
155
|
super.onAttachedToWindow();
|
156
|
|
157
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
158
|
{
|
159
|
DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
|
160
|
int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
|
161
|
|
162
|
LinearLayout layoutHid = findViewById(R.id.hiddenBar);
|
163
|
ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
|
164
|
paramsHid.height = (int)(insetHeight*RATIO_INSET);
|
165
|
layoutHid.setLayoutParams(paramsHid);
|
166
|
mHeightUpperBar += paramsHid.height;
|
167
|
}
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
// do not avoid cutouts
|
172
|
|
173
|
private void cutoutHack()
|
174
|
{
|
175
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
176
|
{
|
177
|
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
|
183
|
@Override
|
184
|
public void onWindowFocusChanged(boolean hasFocus)
|
185
|
{
|
186
|
super.onWindowFocusChanged(hasFocus);
|
187
|
|
188
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
|
189
|
{
|
190
|
getWindow().getDecorView().setSystemUiVisibility(FLAGS);
|
191
|
}
|
192
|
}
|
193
|
|
194
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195
|
|
196
|
@Override
|
197
|
protected void onPause()
|
198
|
{
|
199
|
super.onPause();
|
200
|
PlayView view = findViewById(R.id.playView);
|
201
|
view.onPause();
|
202
|
savePreferences();
|
203
|
DistortedLibrary.onPause(ACTIVITY_NUMBER);
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
|
208
|
@Override
|
209
|
protected void onResume()
|
210
|
{
|
211
|
super.onResume();
|
212
|
DistortedLibrary.onResume(ACTIVITY_NUMBER);
|
213
|
PlayView view = findViewById(R.id.playView);
|
214
|
view.onResume();
|
215
|
|
216
|
ScreenList.switchScreen(this, mModeFree ? ScreenList.FREE : ScreenList.SCRA );
|
217
|
|
218
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
219
|
restorePreferences(preferences);
|
220
|
restoreMoves(preferences);
|
221
|
|
222
|
if( mObjectName.length()>0 )
|
223
|
{
|
224
|
changeIfDifferent(mObjectName,mObjectLocal,mObjectOrdinal,view.getObjectControl());
|
225
|
}
|
226
|
|
227
|
if( !mModeFree )
|
228
|
{
|
229
|
android.util.Log.e("D", "scrambles: "+mNumScrambles);
|
230
|
ObjectControl control = getControl();
|
231
|
control.scrambleObject(mNumScrambles);
|
232
|
}
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
@Override
|
238
|
protected void onDestroy()
|
239
|
{
|
240
|
super.onDestroy();
|
241
|
DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
|
246
|
private void savePreferences()
|
247
|
{
|
248
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
249
|
SharedPreferences.Editor editor = preferences.edit();
|
250
|
|
251
|
for( int i=0; i<ScreenList.LENGTH; i++ )
|
252
|
{
|
253
|
ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
|
254
|
}
|
255
|
|
256
|
ScreenList.savePreferences(editor);
|
257
|
|
258
|
ScreenList curr = ScreenList.getCurrentScreen();
|
259
|
|
260
|
if( curr==ScreenList.FREE )
|
261
|
{
|
262
|
ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
|
263
|
free.saveMovePreferences(KEY_FREE,editor);
|
264
|
}
|
265
|
if( curr==ScreenList.SOLV )
|
266
|
{
|
267
|
ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
|
268
|
solv.saveMovePreferences(KEY_SOLV,editor);
|
269
|
}
|
270
|
|
271
|
editor.apply();
|
272
|
}
|
273
|
|
274
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
275
|
|
276
|
private void restorePreferences(SharedPreferences preferences)
|
277
|
{
|
278
|
for( int i=0; i<ScreenList.LENGTH; i++)
|
279
|
{
|
280
|
ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
|
281
|
}
|
282
|
|
283
|
ScreenList.restorePreferences(preferences);
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
|
288
|
private void restoreMoves(SharedPreferences preferences)
|
289
|
{
|
290
|
ScreenList curr = ScreenList.getCurrentScreen();
|
291
|
|
292
|
if( curr==ScreenList.FREE )
|
293
|
{
|
294
|
ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
|
295
|
free.restoreMovePreferences(this,KEY_FREE,preferences);
|
296
|
}
|
297
|
if( curr==ScreenList.SOLV )
|
298
|
{
|
299
|
ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
|
300
|
solv.restoreMovePreferences(this,KEY_SOLV,preferences);
|
301
|
}
|
302
|
}
|
303
|
|
304
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
305
|
|
306
|
void OpenGLError()
|
307
|
{
|
308
|
RubikDialogError errDiag = new RubikDialogError();
|
309
|
errDiag.show(getSupportFragmentManager(), null);
|
310
|
}
|
311
|
|
312
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
313
|
|
314
|
private void changeIfDifferent(String upperName, boolean local, int ordinal, ObjectControl control)
|
315
|
{
|
316
|
if( local )
|
317
|
{
|
318
|
RubikFiles files = RubikFiles.getInstance();
|
319
|
int iconMode = TwistyObject.MODE_NORM;
|
320
|
String lowerName = upperName.toLowerCase();
|
321
|
InputStream jsonStream = files.openFile(this, lowerName+"_object.json");
|
322
|
InitAssets asset = new InitAssets(jsonStream, null, null);
|
323
|
control.changeIfDifferent(ordinal,upperName,iconMode,asset);
|
324
|
}
|
325
|
else
|
326
|
{
|
327
|
RubikObject object = RubikObjectList.getObject(ordinal);
|
328
|
int iconMode = TwistyObject.MODE_NORM;
|
329
|
InputStream jsonStream = object==null ? null : object.getObjectStream(this);
|
330
|
InputStream meshStream = object==null ? null : object.getMeshStream(this);
|
331
|
PlayView view = findViewById(R.id.playView);
|
332
|
OSInterface os = view.getInterface();
|
333
|
InitAssets asset = new InitAssets(jsonStream, meshStream, os);
|
334
|
control.changeIfDifferent(ordinal, upperName, iconMode, asset);
|
335
|
}
|
336
|
}
|
337
|
|
338
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
339
|
// PUBLIC API
|
340
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
341
|
|
342
|
public FirebaseAnalytics getAnalytics()
|
343
|
{
|
344
|
return mFirebaseAnalytics;
|
345
|
}
|
346
|
|
347
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
348
|
|
349
|
public int getScreenWidthInPixels()
|
350
|
{
|
351
|
return mScreenWidth;
|
352
|
}
|
353
|
|
354
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
355
|
|
356
|
public int getScreenHeightInPixels()
|
357
|
{
|
358
|
return mScreenHeight;
|
359
|
}
|
360
|
|
361
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
362
|
|
363
|
public int getNumScrambles()
|
364
|
{
|
365
|
return mNumScrambles;
|
366
|
}
|
367
|
|
368
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
369
|
|
370
|
public ObjectControl getControl()
|
371
|
{
|
372
|
PlayView view = findViewById(R.id.playView);
|
373
|
return view.getObjectControl();
|
374
|
}
|
375
|
}
|