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.main;
|
11
|
|
12
|
import android.content.Intent;
|
13
|
import android.content.SharedPreferences;
|
14
|
import android.content.pm.PackageInfo;
|
15
|
import android.content.pm.PackageManager;
|
16
|
import android.os.Build;
|
17
|
import android.os.Bundle;
|
18
|
import android.util.DisplayMetrics;
|
19
|
import android.util.TypedValue;
|
20
|
import android.view.DisplayCutout;
|
21
|
import android.view.View;
|
22
|
import android.view.ViewGroup;
|
23
|
import android.view.WindowManager;
|
24
|
import android.widget.LinearLayout;
|
25
|
import android.widget.ScrollView;
|
26
|
import android.widget.TextView;
|
27
|
|
28
|
import androidx.appcompat.app.AppCompatActivity;
|
29
|
import androidx.preference.PreferenceManager;
|
30
|
|
31
|
import com.google.firebase.analytics.FirebaseAnalytics;
|
32
|
import com.google.firebase.inappmessaging.FirebaseInAppMessaging;
|
33
|
|
34
|
import org.distorted.bandaged.BandagedCreatorActivity;
|
35
|
import org.distorted.config.ConfigActivity;
|
36
|
import org.distorted.dialogs.RubikDialogAbout;
|
37
|
import org.distorted.dialogs.RubikDialogCreators;
|
38
|
import org.distorted.dialogs.RubikDialogExit;
|
39
|
import org.distorted.dialogs.RubikDialogScores;
|
40
|
import org.distorted.dialogs.RubikDialogUpdates;
|
41
|
import org.distorted.external.RubikNetwork;
|
42
|
import org.distorted.external.RubikScores;
|
43
|
import org.distorted.external.RubikUpdates;
|
44
|
import org.distorted.messaging.RubikInAppMessanging;
|
45
|
import org.distorted.objects.RubikObjectList;
|
46
|
import org.distorted.purchase.PurchaseActivity;
|
47
|
import org.distorted.tutorials.TutorialActivity;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
public class MainActivity extends AppCompatActivity implements RubikNetwork.Updatee
|
52
|
{
|
53
|
public static final float RATIO_BAR = 0.080f;
|
54
|
public static final int FLAGS = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
55
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
56
|
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
57
|
| View.SYSTEM_UI_FLAG_FULLSCREEN
|
58
|
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
|
59
|
|
60
|
private static final float RATIO_INSET= 0.09f;
|
61
|
|
62
|
private boolean mJustStarted;
|
63
|
private FirebaseAnalytics mFirebaseAnalytics;
|
64
|
private static int mScreenWidth, mScreenHeight;
|
65
|
private int mCurrentApiVersion;
|
66
|
private int mHeightUpperBar;
|
67
|
private String mOldVersion, mCurrVersion;
|
68
|
private int mSolverIndex;
|
69
|
private TextView mBubbleUpdates;
|
70
|
private int mNumUpdates;
|
71
|
private MainScrollGrid mGrid;
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
@Override
|
76
|
protected void onCreate(Bundle savedState)
|
77
|
{
|
78
|
super.onCreate(savedState);
|
79
|
setTheme(R.style.MaterialThemeNoActionBar);
|
80
|
setContentView(R.layout.new_main);
|
81
|
hideNavigationBar();
|
82
|
|
83
|
mJustStarted = true;
|
84
|
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
|
85
|
|
86
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
87
|
getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
|
88
|
mScreenWidth =displaymetrics.widthPixels;
|
89
|
mScreenHeight=displaymetrics.heightPixels;
|
90
|
|
91
|
cutoutHack();
|
92
|
computeHeights();
|
93
|
|
94
|
mCurrVersion = getAppVers();
|
95
|
mSolverIndex = 0;
|
96
|
|
97
|
Thread thread = new Thread()
|
98
|
{
|
99
|
public void run()
|
100
|
{
|
101
|
RubikInAppMessanging listener = new RubikInAppMessanging();
|
102
|
FirebaseInAppMessaging.getInstance().addClickListener(listener);
|
103
|
}
|
104
|
};
|
105
|
|
106
|
thread.start();
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
// this does not include possible insets
|
111
|
|
112
|
private void computeHeights()
|
113
|
{
|
114
|
int barHeight = (int)(mScreenHeight*RATIO_BAR);
|
115
|
int scrollHeight = (int)(mScreenHeight*(1-2*RATIO_BAR));
|
116
|
mHeightUpperBar = barHeight;
|
117
|
|
118
|
LinearLayout layoutTop = findViewById(R.id.upperBar);
|
119
|
ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
|
120
|
paramsTop.height = barHeight;
|
121
|
layoutTop.setLayoutParams(paramsTop);
|
122
|
|
123
|
LinearLayout layoutBot = findViewById(R.id.lowerBar);
|
124
|
ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
|
125
|
paramsBot.height = barHeight;
|
126
|
layoutBot.setLayoutParams(paramsBot);
|
127
|
|
128
|
ScrollView scroll = findViewById(R.id.objectScroll);
|
129
|
ViewGroup.LayoutParams paramsScroll = scroll.getLayoutParams();
|
130
|
paramsScroll.height = scrollHeight;
|
131
|
scroll.setLayoutParams(paramsScroll);
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
private void hideNavigationBar()
|
137
|
{
|
138
|
mCurrentApiVersion = Build.VERSION.SDK_INT;
|
139
|
|
140
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
|
141
|
{
|
142
|
final View decorView = getWindow().getDecorView();
|
143
|
|
144
|
decorView.setSystemUiVisibility(FLAGS);
|
145
|
|
146
|
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
|
147
|
{
|
148
|
@Override
|
149
|
public void onSystemUiVisibilityChange(int visibility)
|
150
|
{
|
151
|
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
|
152
|
{
|
153
|
decorView.setSystemUiVisibility(FLAGS);
|
154
|
}
|
155
|
}
|
156
|
});
|
157
|
}
|
158
|
}
|
159
|
|
160
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161
|
|
162
|
@Override
|
163
|
public void onAttachedToWindow()
|
164
|
{
|
165
|
super.onAttachedToWindow();
|
166
|
|
167
|
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
|
168
|
{
|
169
|
DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
|
170
|
int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
|
171
|
|
172
|
LinearLayout layoutHid = findViewById(R.id.hiddenBar);
|
173
|
ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
|
174
|
paramsHid.height = (int)(insetHeight*RATIO_INSET);
|
175
|
layoutHid.setLayoutParams(paramsHid);
|
176
|
mHeightUpperBar += paramsHid.height;
|
177
|
}
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
// do not avoid cutouts
|
182
|
|
183
|
private void cutoutHack()
|
184
|
{
|
185
|
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
|
186
|
{
|
187
|
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
|
188
|
}
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
|
193
|
@Override
|
194
|
public void onWindowFocusChanged(boolean hasFocus)
|
195
|
{
|
196
|
super.onWindowFocusChanged(hasFocus);
|
197
|
|
198
|
if( mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus )
|
199
|
{
|
200
|
getWindow().getDecorView().setSystemUiVisibility(FLAGS);
|
201
|
}
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
|
206
|
@Override
|
207
|
protected void onPause()
|
208
|
{
|
209
|
super.onPause();
|
210
|
RubikNetwork.onPause();
|
211
|
savePreferences();
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
|
216
|
@Override
|
217
|
protected void onResume()
|
218
|
{
|
219
|
super.onResume();
|
220
|
|
221
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
222
|
restorePreferences(preferences,mJustStarted);
|
223
|
|
224
|
mBubbleUpdates = findViewById(R.id.bubbleUpdates);
|
225
|
mBubbleUpdates.setVisibility(View.INVISIBLE);
|
226
|
mNumUpdates = 0;
|
227
|
|
228
|
RubikNetwork network = RubikNetwork.getInstance();
|
229
|
network.signUpForUpdates(this);
|
230
|
network.downloadUpdates(this);
|
231
|
|
232
|
mGrid = new MainScrollGrid();
|
233
|
mGrid.createGrid(this,mScreenWidth,mScreenHeight);
|
234
|
|
235
|
if( mJustStarted )
|
236
|
{
|
237
|
mJustStarted = false;
|
238
|
RubikScores scores = RubikScores.getInstance();
|
239
|
scores.incrementNumRuns();
|
240
|
scores.setCountry(this);
|
241
|
RubikObjectList.restoreMeshState(preferences);
|
242
|
}
|
243
|
|
244
|
if( !mOldVersion.equals(mCurrVersion) ) displayNovelties();
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
private void displayNovelties()
|
250
|
{
|
251
|
Bundle bundle = new Bundle();
|
252
|
bundle.putString("argument",mOldVersion);
|
253
|
RubikDialogAbout newDialog = new RubikDialogAbout();
|
254
|
newDialog.setArguments(bundle);
|
255
|
newDialog.show(getSupportFragmentManager(), RubikDialogAbout.getDialogTag() );
|
256
|
}
|
257
|
|
258
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
259
|
|
260
|
@Override
|
261
|
protected void onDestroy()
|
262
|
{
|
263
|
super.onDestroy();
|
264
|
}
|
265
|
|
266
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
267
|
|
268
|
private String getAppVers()
|
269
|
{
|
270
|
try
|
271
|
{
|
272
|
PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
|
273
|
return pInfo.versionName;
|
274
|
}
|
275
|
catch( PackageManager.NameNotFoundException e )
|
276
|
{
|
277
|
return "unknown";
|
278
|
}
|
279
|
}
|
280
|
|
281
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
282
|
|
283
|
private void savePreferences()
|
284
|
{
|
285
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
286
|
SharedPreferences.Editor editor = preferences.edit();
|
287
|
|
288
|
editor.putString("appVersion", mCurrVersion );
|
289
|
editor.putInt("solverIndex", mSolverIndex );
|
290
|
|
291
|
RubikObjectList.savePreferences(editor);
|
292
|
RubikObjectList.saveMeshState(editor);
|
293
|
|
294
|
boolean success = editor.commit();
|
295
|
if( !success ) android.util.Log.e("D", "Failed to save preferences");
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
|
300
|
private void restorePreferences(SharedPreferences preferences, boolean justStarted)
|
301
|
{
|
302
|
mOldVersion = preferences.getString("appVersion","");
|
303
|
mSolverIndex = preferences.getInt("solverIndex",0);
|
304
|
|
305
|
RubikObjectList.restorePreferences(this,preferences,justStarted);
|
306
|
RubikScores scores = RubikScores.getInstance();
|
307
|
|
308
|
if( scores.isVerified() )
|
309
|
{
|
310
|
FirebaseAnalytics analytics = getAnalytics();
|
311
|
analytics.setUserId(scores.getName());
|
312
|
}
|
313
|
}
|
314
|
|
315
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316
|
// PUBLIC API
|
317
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
318
|
|
319
|
public FirebaseAnalytics getAnalytics()
|
320
|
{
|
321
|
return mFirebaseAnalytics;
|
322
|
}
|
323
|
|
324
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
325
|
|
326
|
public int getHeightUpperBar()
|
327
|
{
|
328
|
return mHeightUpperBar;
|
329
|
}
|
330
|
|
331
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332
|
|
333
|
public int getScreenWidthInPixels()
|
334
|
{
|
335
|
return mScreenWidth;
|
336
|
}
|
337
|
|
338
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
339
|
|
340
|
public int getScreenHeightInPixels()
|
341
|
{
|
342
|
return mScreenHeight;
|
343
|
}
|
344
|
|
345
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
346
|
|
347
|
public void switchToTutorial(String url, int objectOrdinal)
|
348
|
{
|
349
|
Intent intent = new Intent(this, TutorialActivity.class);
|
350
|
intent.putExtra("url", url);
|
351
|
intent.putExtra("obj", objectOrdinal);
|
352
|
startActivity(intent);
|
353
|
}
|
354
|
|
355
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
356
|
|
357
|
public void switchToConfig(int objectOrdinal)
|
358
|
{
|
359
|
Intent intent = new Intent(this, ConfigActivity.class);
|
360
|
intent.putExtra("obj", objectOrdinal);
|
361
|
startActivity(intent);
|
362
|
}
|
363
|
|
364
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
365
|
|
366
|
public void switchToBandagedCreator(int objectOrdinal)
|
367
|
{
|
368
|
Intent intent = new Intent(this, BandagedCreatorActivity.class);
|
369
|
intent.putExtra("obj", objectOrdinal);
|
370
|
startActivity(intent);
|
371
|
}
|
372
|
|
373
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
374
|
|
375
|
public void switchToPurchase(int objectOrdinal)
|
376
|
{
|
377
|
Intent intent = new Intent(this, PurchaseActivity.class);
|
378
|
intent.putExtra("obj", objectOrdinal);
|
379
|
startActivity(intent);
|
380
|
}
|
381
|
|
382
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
383
|
|
384
|
public void setSolverIndex(int index)
|
385
|
{
|
386
|
mSolverIndex = index;
|
387
|
}
|
388
|
|
389
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
390
|
|
391
|
public int getSolverIndex()
|
392
|
{
|
393
|
return mSolverIndex;
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
|
398
|
public void onScores(View v)
|
399
|
{
|
400
|
Bundle sBundle = new Bundle();
|
401
|
sBundle.putString("argument", "false");
|
402
|
RubikDialogScores scores = new RubikDialogScores();
|
403
|
scores.setArguments(sBundle);
|
404
|
scores.show(getSupportFragmentManager(), null);
|
405
|
}
|
406
|
|
407
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
408
|
|
409
|
public void onUpdates(View v)
|
410
|
{
|
411
|
RubikDialogUpdates diag = new RubikDialogUpdates();
|
412
|
diag.show( getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
|
413
|
}
|
414
|
|
415
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
416
|
|
417
|
public void onExit(View v)
|
418
|
{
|
419
|
RubikDialogExit diag = new RubikDialogExit();
|
420
|
diag.show(getSupportFragmentManager(), null);
|
421
|
}
|
422
|
|
423
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
424
|
|
425
|
public void onAbout(View v)
|
426
|
{
|
427
|
RubikDialogAbout diag = new RubikDialogAbout();
|
428
|
diag.show(getSupportFragmentManager(), null);
|
429
|
}
|
430
|
|
431
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
432
|
|
433
|
public void onBandage(View v)
|
434
|
{
|
435
|
RubikDialogCreators diag = new RubikDialogCreators();
|
436
|
diag.show(getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
|
437
|
}
|
438
|
|
439
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
440
|
|
441
|
public void updateBubble(int num)
|
442
|
{
|
443
|
runOnUiThread(new Runnable()
|
444
|
{
|
445
|
@Override
|
446
|
public void run()
|
447
|
{
|
448
|
mNumUpdates = num;
|
449
|
|
450
|
if( num>0 )
|
451
|
{
|
452
|
String shownNum = String.valueOf(num);
|
453
|
mBubbleUpdates.setText(shownNum);
|
454
|
mBubbleUpdates.setVisibility(View.VISIBLE);
|
455
|
int height = (int)(0.05f*mScreenWidth);
|
456
|
mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
|
457
|
}
|
458
|
else
|
459
|
{
|
460
|
mBubbleUpdates.setVisibility(View.INVISIBLE);
|
461
|
}
|
462
|
}
|
463
|
});
|
464
|
}
|
465
|
|
466
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
467
|
|
468
|
public void receiveUpdate(RubikUpdates updates)
|
469
|
{
|
470
|
int num = updates.getCompletedNumber();
|
471
|
updateBubble(num);
|
472
|
}
|
473
|
|
474
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
475
|
|
476
|
public void objectDownloaded(String shortName)
|
477
|
{
|
478
|
mNumUpdates--;
|
479
|
updateBubble(mNumUpdates);
|
480
|
mGrid.updateGrid(this,mScreenWidth,mScreenHeight);
|
481
|
}
|
482
|
|
483
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
484
|
|
485
|
public int getType()
|
486
|
{
|
487
|
return 0;
|
488
|
}
|
489
|
|
490
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
491
|
|
492
|
public void errorUpdate()
|
493
|
{
|
494
|
android.util.Log.e("D", "NewRubikActivity: Error receiving downloaded objects update");
|
495
|
}
|
496
|
}
|