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