Project

General

Profile

Download (16 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / main / MainActivity.java @ b710a574

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.BandagedCreatorActivity;
37
import org.distorted.config.ConfigActivity;
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.RubikObjectList;
48
import org.distorted.patternui.PatternActivity;
49
import org.distorted.solverui.SolverActivity;
50
import org.distorted.tutorials.TutorialActivity;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
public class MainActivity extends AppCompatActivity implements RubikNetwork.Updatee
55
{
56
    public static final float RATIO_BAR = 0.080f;
57
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
58
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
59
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
60
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
61
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
62

    
63
    private boolean mJustStarted;
64
    private FirebaseAnalytics mFirebaseAnalytics;
65
    private int mCurrentApiVersion;
66
    private String mOldVersion, mCurrVersion;
67
    private int mScreenWidth;
68
    private TextView mBubbleUpdates;
69
    private int mNumUpdates;
70
    private MainScrollGrid mGrid;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
    @Override
75
    protected void onCreate(Bundle savedState)
76
      {
77
      super.onCreate(savedState);
78
      setTheme(R.style.MaterialThemeNoActionBar);
79
      setContentView(R.layout.main);
80
      hideNavigationBar();
81

    
82
      mJustStarted = true;
83
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
84

    
85
      cutoutHack();
86
      computeHeights();
87

    
88
      mCurrVersion = getAppVers();
89

    
90
      Thread thread = new Thread()
91
        {
92
        public void run()
93
          {
94
          RubikInAppMessanging listener = new RubikInAppMessanging();
95
          FirebaseInAppMessaging.getInstance().addClickListener(listener);
96
          }
97
        };
98

    
99
      thread.start();
100
      }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// this does not include possible insets
104

    
105
    private void computeHeights()
106
      {
107
      LinearLayout.LayoutParams pU = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, RATIO_BAR);
108
      LinearLayout layoutTop = findViewById(R.id.upperBar);
109
      layoutTop.setLayoutParams(pU);
110

    
111
      LinearLayout.LayoutParams pS = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1-2*RATIO_BAR);
112
      ScrollView scroll = findViewById(R.id.objectScroll);
113
      scroll.setLayoutParams(pS);
114

    
115
      LinearLayout.LayoutParams pL = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, RATIO_BAR);
116
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
117
      layoutBot.setLayoutParams(pL);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    private void hideNavigationBar()
123
      {
124
      mCurrentApiVersion = Build.VERSION.SDK_INT;
125

    
126
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
127
        {
128
        final View decorView = getWindow().getDecorView();
129

    
130
        decorView.setSystemUiVisibility(FLAGS);
131

    
132
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
133
          {
134
          @Override
135
          public void onSystemUiVisibilityChange(int visibility)
136
            {
137
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
138
              {
139
              decorView.setSystemUiVisibility(FLAGS);
140
              }
141
            }
142
          });
143
        }
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    @Override
149
    public void onAttachedToWindow()
150
      {
151
      super.onAttachedToWindow();
152

    
153
      getWindowWidth(getResources().getConfiguration());
154
      mGrid = new MainScrollGrid();
155
      mGrid.createGrid(this,mScreenWidth);
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
        if( insetHeight>0 )
163
          {
164
          LinearLayout.LayoutParams pH = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, RATIO_BAR);
165
          LinearLayout layoutHid = findViewById(R.id.hiddenBar);
166
          layoutHid.setLayoutParams(pH);
167

    
168
          LinearLayout.LayoutParams pS = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1-3*RATIO_BAR);
169
          ScrollView scroll = findViewById(R.id.objectScroll);
170
          scroll.setLayoutParams(pS);
171
          }
172
        }
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// do not avoid cutouts
177

    
178
    private void cutoutHack()
179
      {
180
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
181
        {
182
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
183
        }
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
    private void getWindowWidth(Configuration conf)
189
      {
190
      int dpi = getResources().getDisplayMetrics().densityDpi;
191
      float conv = ((float) dpi/DisplayMetrics.DENSITY_DEFAULT);
192

    
193
      mScreenWidth = (int) (conf.screenWidthDp*conv + 0.5f);
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    @Override
199
    public void onWindowFocusChanged(boolean hasFocus)
200
      {
201
      super.onWindowFocusChanged(hasFocus);
202

    
203
      if( mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus )
204
        {
205
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
206
        }
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    @Override
212
    public void onConfigurationChanged(@NonNull Configuration conf)
213
      {
214
      super.onConfigurationChanged(conf);
215

    
216
      getWindowWidth(conf);
217
      mGrid.updateGrid(this,mScreenWidth);
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
    
222
    @Override
223
    protected void onPause() 
224
      {
225
      super.onPause();
226
      RubikNetwork.onPause();
227
      savePreferences();
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
    
232
    @Override
233
    protected void onResume() 
234
      {
235
      super.onResume();
236

    
237
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
238
      restorePreferences(preferences,mJustStarted);
239

    
240
      mBubbleUpdates = findViewById(R.id.bubbleUpdates);
241
      mBubbleUpdates.setVisibility(View.INVISIBLE);
242
      mNumUpdates = 0;
243

    
244
      RubikNetwork network = RubikNetwork.getInstance();
245
      network.signUpForUpdates(this);
246
      network.downloadUpdates(this);
247

    
248
      if( mJustStarted )
249
        {
250
        mJustStarted = false;
251
        RubikScores scores = RubikScores.getInstance();
252
        scores.incrementNumRuns();
253
        scores.setCountry(this);
254
        }
255

    
256
      if( !mOldVersion.equals(mCurrVersion) ) displayNovelties();
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    private void displayNovelties()
262
      {
263
      Bundle bundle = new Bundle();
264
      bundle.putString("argument",mOldVersion);
265
      RubikDialogAbout newDialog = new RubikDialogAbout();
266
      newDialog.setArguments(bundle);
267
      newDialog.show(getSupportFragmentManager(), RubikDialogAbout.getDialogTag() );
268
      }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
    
272
    @Override
273
    protected void onDestroy() 
274
      {
275
      super.onDestroy();
276
      }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
    private String getAppVers()
281
      {
282
      try
283
        {
284
        PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
285
        return pInfo.versionName;
286
        }
287
      catch( PackageManager.NameNotFoundException e )
288
        {
289
        return "unknown";
290
        }
291
      }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
    private void savePreferences()
296
      {
297
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
298
      SharedPreferences.Editor editor = preferences.edit();
299

    
300
      editor.putString("appVersion", mCurrVersion );
301

    
302
      RubikObjectList.savePreferences(editor);
303

    
304
      boolean success = editor.commit();
305
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    private void restorePreferences(SharedPreferences preferences, boolean justStarted)
311
      {
312
      mOldVersion = preferences.getString("appVersion","");
313

    
314
      RubikObjectList.restorePreferences(this,preferences,justStarted);
315
      RubikScores scores = RubikScores.getInstance();
316

    
317
      if( scores.isVerified() )
318
        {
319
        FirebaseAnalytics analytics = getAnalytics();
320
        analytics.setUserId(scores.getName());
321
        }
322
      }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    private void updateBubble(int num)
327
      {
328
      runOnUiThread(new Runnable()
329
        {
330
        @Override
331
        public void run()
332
          {
333
          mNumUpdates = num;
334

    
335
          if( num>0 )
336
            {
337
            String shownNum = String.valueOf(num);
338
            mBubbleUpdates.setText(shownNum);
339
            mBubbleUpdates.setVisibility(View.VISIBLE);
340
            int height = (int)(0.05f*mScreenWidth);
341
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
342
            }
343
          else
344
            {
345
            mBubbleUpdates.setVisibility(View.INVISIBLE);
346
            }
347
          }
348
        });
349
      }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352
// PUBLIC API
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
    public FirebaseAnalytics getAnalytics()
356
      {
357
      return mFirebaseAnalytics;
358
      }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
    public void switchToTutorial(int objectOrdinal)
363
      {
364
      Intent intent = new Intent(this, TutorialActivity.class);
365
      intent.putExtra("obj", objectOrdinal);
366
      startActivity(intent);
367
      }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
    public void switchToConfig(int objectOrdinal)
372
      {
373
      Intent intent = new Intent(this, ConfigActivity.class);
374
      intent.putExtra("obj", objectOrdinal);
375
      startActivity(intent);
376
      }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
    public void switchToBandagedCreator(int objectOrdinal)
381
      {
382
      Intent intent = new Intent(this, BandagedCreatorActivity.class);
383
      intent.putExtra("obj", objectOrdinal);
384
      startActivity(intent);
385
      }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
    public void switchToSolver(int objectOrdinal)
390
      {
391
      Intent intent = new Intent(this, SolverActivity.class);
392
      intent.putExtra("obj", objectOrdinal);
393
      startActivity(intent);
394
      }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
    public void switchToPattern(int objectOrdinal)
399
      {
400
      Intent intent = new Intent(this, PatternActivity.class);
401
      intent.putExtra("obj", objectOrdinal);
402
      startActivity(intent);
403
      }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
    public void onScores(View v)
408
      {
409
      Bundle sBundle = new Bundle();
410
      sBundle.putString("argument", "false");
411
      RubikDialogScores scores = new RubikDialogScores();
412
      scores.setArguments(sBundle);
413
      scores.show(getSupportFragmentManager(), null);
414
      }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
    public void onUpdates(View v)
419
      {
420
      RubikDialogUpdates diag = new RubikDialogUpdates();
421
      diag.show( getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
422
      }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
    public void onExit(View v)
427
      {
428
      RubikDialogExit diag = new RubikDialogExit();
429
      diag.show(getSupportFragmentManager(), null);
430
      }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    public void onAbout(View v)
435
      {
436
      RubikDialogAbout diag = new RubikDialogAbout();
437
      diag.show(getSupportFragmentManager(), null);
438
      }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
    public void onBandage(View v)
443
      {
444
      RubikDialogCreators diag = new RubikDialogCreators();
445
      diag.show(getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
446
      }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
    public void receiveUpdate(RubikUpdates updates)
451
      {
452
      int num = updates.getCompletedNumber();
453
      updateBubble(num);
454
      }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
    public void objectDownloaded(String shortName)
459
      {
460
      mNumUpdates--;
461
      updateBubble(mNumUpdates);
462
      mGrid.updateGrid(this,mScreenWidth);
463
      }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
    public int getType()
468
      {
469
      return 0;
470
      }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
    public void errorUpdate()
475
      {
476
      android.util.Log.e("D", "NewRubikActivity: Error receiving downloaded objects update");
477
      }
478
}
(1-1/3)