1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 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.solverui;
|
11
|
|
12
|
import android.content.SharedPreferences;
|
13
|
import android.os.Build;
|
14
|
import android.os.Bundle;
|
15
|
import android.util.DisplayMetrics;
|
16
|
import android.view.DisplayCutout;
|
17
|
import android.view.View;
|
18
|
import android.view.ViewGroup;
|
19
|
import android.view.WindowManager;
|
20
|
import android.widget.LinearLayout;
|
21
|
|
22
|
import androidx.appcompat.app.AppCompatActivity;
|
23
|
import androidx.preference.PreferenceManager;
|
24
|
|
25
|
import org.distorted.dialogs.RubikDialogError;
|
26
|
import org.distorted.library.main.DistortedLibrary;
|
27
|
import org.distorted.library.main.DistortedScreen;
|
28
|
import org.distorted.main.MainActivity;
|
29
|
import org.distorted.main.R;
|
30
|
import org.distorted.objectlib.helpers.OperatingSystemInterface;
|
31
|
import org.distorted.objectlib.main.InitAssets;
|
32
|
import org.distorted.objectlib.main.ObjectControl;
|
33
|
import org.distorted.objectlib.main.TwistyObject;
|
34
|
import org.distorted.objects.RubikObject;
|
35
|
import org.distorted.objects.RubikObjectList;
|
36
|
import org.distorted.os.OSInterface;
|
37
|
import org.distorted.solvers.ImplementedSolversList;
|
38
|
|
39
|
import java.io.InputStream;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public class SolverActivity extends AppCompatActivity
|
44
|
{
|
45
|
public static final float RATIO_BAR = 0.100f;
|
46
|
public static final float PADDING = 0.010f;
|
47
|
public static final float SMALL_MARGIN = 0.004f;
|
48
|
public static final float BUTTON_TEXT_SIZE= 0.050f;
|
49
|
public static final float TITLE_TEXT_SIZE = 0.060f;
|
50
|
|
51
|
private static final int ACTIVITY_NUMBER = 4;
|
52
|
private static final float RATIO_INSET= 0.09f;
|
53
|
|
54
|
private boolean mJustStarted;
|
55
|
private static int mScreenWidth, mScreenHeight;
|
56
|
private int mCurrentApiVersion;
|
57
|
private int mHeightUpperBar;
|
58
|
private int mSolverOrdinal;
|
59
|
private int mObjectOrdinal;
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
@Override
|
64
|
protected void onCreate(Bundle savedState)
|
65
|
{
|
66
|
super.onCreate(savedState);
|
67
|
DistortedLibrary.onCreate(ACTIVITY_NUMBER);
|
68
|
setTheme(R.style.MaterialThemeNoActionBar);
|
69
|
setContentView(R.layout.solver);
|
70
|
hideNavigationBar();
|
71
|
|
72
|
mJustStarted = true;
|
73
|
Bundle b = getIntent().getExtras();
|
74
|
mObjectOrdinal = b!=null ? b.getInt("obj") : 0;
|
75
|
mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectOrdinal);
|
76
|
|
77
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
78
|
getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
|
79
|
mScreenWidth =displaymetrics.widthPixels;
|
80
|
mScreenHeight=displaymetrics.heightPixels;
|
81
|
|
82
|
cutoutHack();
|
83
|
computeBarHeights();
|
84
|
}
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
// this does not include possible insets
|
88
|
|
89
|
private void computeBarHeights()
|
90
|
{
|
91
|
int barHeight = (int)(mScreenHeight*RATIO_BAR);
|
92
|
mHeightUpperBar = barHeight;
|
93
|
|
94
|
LinearLayout layoutTop = findViewById(R.id.upperBar);
|
95
|
LinearLayout layoutBot = findViewById(R.id.lowerBar);
|
96
|
|
97
|
ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
|
98
|
paramsTop.height = mHeightUpperBar;
|
99
|
layoutTop.setLayoutParams(paramsTop);
|
100
|
ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
|
101
|
paramsBot.height = barHeight;
|
102
|
layoutBot.setLayoutParams(paramsBot);
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
private void hideNavigationBar()
|
108
|
{
|
109
|
mCurrentApiVersion = Build.VERSION.SDK_INT;
|
110
|
|
111
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
|
112
|
{
|
113
|
final View decorView = getWindow().getDecorView();
|
114
|
|
115
|
decorView.setSystemUiVisibility(MainActivity.FLAGS);
|
116
|
|
117
|
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
|
118
|
{
|
119
|
@Override
|
120
|
public void onSystemUiVisibilityChange(int visibility)
|
121
|
{
|
122
|
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
|
123
|
{
|
124
|
decorView.setSystemUiVisibility(MainActivity.FLAGS);
|
125
|
}
|
126
|
}
|
127
|
});
|
128
|
}
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
|
133
|
@Override
|
134
|
public void onAttachedToWindow()
|
135
|
{
|
136
|
super.onAttachedToWindow();
|
137
|
|
138
|
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
|
139
|
{
|
140
|
DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
|
141
|
int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
|
142
|
|
143
|
LinearLayout layoutHid = findViewById(R.id.hiddenBar);
|
144
|
ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
|
145
|
paramsHid.height = (int)(insetHeight*RATIO_INSET);
|
146
|
layoutHid.setLayoutParams(paramsHid);
|
147
|
mHeightUpperBar += paramsHid.height;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
// do not avoid cutouts
|
153
|
|
154
|
private void cutoutHack()
|
155
|
{
|
156
|
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
|
157
|
{
|
158
|
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
@Override
|
165
|
public void onWindowFocusChanged(boolean hasFocus)
|
166
|
{
|
167
|
super.onWindowFocusChanged(hasFocus);
|
168
|
|
169
|
if( mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus )
|
170
|
{
|
171
|
getWindow().getDecorView().setSystemUiVisibility(MainActivity.FLAGS);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
|
177
|
@Override
|
178
|
protected void onPause()
|
179
|
{
|
180
|
super.onPause();
|
181
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
182
|
view.onPause();
|
183
|
DistortedLibrary.onPause(ACTIVITY_NUMBER);
|
184
|
savePreferences();
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
|
189
|
@Override
|
190
|
protected void onResume()
|
191
|
{
|
192
|
super.onResume();
|
193
|
DistortedLibrary.onResume(ACTIVITY_NUMBER);
|
194
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
195
|
view.onResume();
|
196
|
|
197
|
createObject();
|
198
|
|
199
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
200
|
restorePreferences(preferences,mJustStarted);
|
201
|
ScreenList.setScreen(this);
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
|
206
|
@Override
|
207
|
protected void onDestroy()
|
208
|
{
|
209
|
DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
|
210
|
super.onDestroy();
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
|
215
|
private void savePreferences()
|
216
|
{
|
217
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
218
|
SharedPreferences.Editor editor = preferences.edit();
|
219
|
|
220
|
for( int i=0; i< ScreenList.LENGTH; i++ )
|
221
|
{
|
222
|
ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
|
223
|
}
|
224
|
|
225
|
RubikObjectList.savePreferences(editor);
|
226
|
ScreenList.savePreferences(editor);
|
227
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
228
|
OSInterface os = view.getInterface();
|
229
|
os.setEditor(editor);
|
230
|
view.getObjectControl().savePreferences();
|
231
|
|
232
|
boolean success = editor.commit();
|
233
|
if( !success ) android.util.Log.e("D", "Failed to save preferences");
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
private void restorePreferences(SharedPreferences preferences, boolean justStarted)
|
239
|
{
|
240
|
RubikObjectList.restorePreferences(this,preferences,justStarted);
|
241
|
|
242
|
for (int i=0; i<ScreenList.LENGTH; i++)
|
243
|
{
|
244
|
ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
|
245
|
}
|
246
|
|
247
|
ScreenList.restorePreferences(preferences);
|
248
|
|
249
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
250
|
OSInterface os = view.getInterface();
|
251
|
os.setPreferences(preferences);
|
252
|
view.getObjectControl().restorePreferences();
|
253
|
}
|
254
|
|
255
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
256
|
|
257
|
void OpenGLError()
|
258
|
{
|
259
|
RubikDialogError errDiag = new RubikDialogError();
|
260
|
errDiag.show(getSupportFragmentManager(), null);
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
// PUBLIC API
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
public TwistyObject getObject()
|
268
|
{
|
269
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
270
|
return view.getObjectControl().getObject();
|
271
|
}
|
272
|
|
273
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274
|
|
275
|
public DistortedScreen getScreen()
|
276
|
{
|
277
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
278
|
return view.getRenderer().getScreen();
|
279
|
}
|
280
|
|
281
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
282
|
|
283
|
public ObjectControl getControl()
|
284
|
{
|
285
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
286
|
return view.getObjectControl();
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
|
291
|
public int getScreenWidthInPixels()
|
292
|
{
|
293
|
return mScreenWidth;
|
294
|
}
|
295
|
|
296
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
297
|
|
298
|
public int getScreenHeightInPixels()
|
299
|
{
|
300
|
return mScreenHeight;
|
301
|
}
|
302
|
|
303
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304
|
|
305
|
public int getSolverOrdinal()
|
306
|
{
|
307
|
return mSolverOrdinal;
|
308
|
}
|
309
|
|
310
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
311
|
|
312
|
public int getObjectOrdinal()
|
313
|
{
|
314
|
return mObjectOrdinal;
|
315
|
}
|
316
|
|
317
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
318
|
|
319
|
public void createObject()
|
320
|
{
|
321
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
322
|
ObjectControl control = view.getObjectControl();
|
323
|
RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
|
324
|
int iconMode = TwistyObject.MODE_NORM;
|
325
|
InputStream jsonStream = object==null ? null : object.getObjectStream(this);
|
326
|
InputStream meshStream = object==null ? null : object.getMeshStream(this);
|
327
|
String name = object==null ? "NULL" : object.getUpperName();
|
328
|
OSInterface os = view.getInterface();
|
329
|
InitAssets asset = new InitAssets(jsonStream,meshStream,os);
|
330
|
|
331
|
control.changeIfDifferent(mObjectOrdinal,name,iconMode,asset);
|
332
|
}
|
333
|
|
334
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
335
|
|
336
|
public OperatingSystemInterface getInterface()
|
337
|
{
|
338
|
SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
|
339
|
return view.getInterface();
|
340
|
}
|
341
|
}
|