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