Project

General

Profile

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

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

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.purchase.PurchaseActivity;
49
import org.distorted.tutorials.TutorialActivity;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
62
    private boolean mJustStarted;
63
    private FirebaseAnalytics mFirebaseAnalytics;
64
    private int mCurrentApiVersion;
65
    private String mOldVersion, mCurrVersion;
66
    private int mSolverIndex;
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.new_main);
80
      hideNavigationBar();
81

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

    
85
      cutoutHack();
86
      computeHeights();
87

    
88
      mCurrVersion = getAppVers();
89
      mSolverIndex = 0;
90

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

    
100
      thread.start();
101
      }
102

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

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

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

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

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

    
131
        decorView.setSystemUiVisibility(FLAGS);
132

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

    
154
      getWindowWidth(getResources().getConfiguration());
155
      mGrid = new MainScrollGrid();
156
      mGrid.createGrid(this,mScreenWidth);
157

    
158
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
159
        {
160
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
161
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
162

    
163
        if( insetHeight>0 )
164
          {
165
          LinearLayout.LayoutParams pH = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, RATIO_BAR);
166
          LinearLayout layoutHid = findViewById(R.id.hiddenBar);
167
          layoutHid.setLayoutParams(pH);
168

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
// do not avoid cutouts
178

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

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

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

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

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

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

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

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

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

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

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

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

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

    
249
      if( mJustStarted )
250
        {
251
        mJustStarted = false;
252
        RubikScores scores = RubikScores.getInstance();
253
        scores.incrementNumRuns();
254
        scores.setCountry(this);
255
        RubikObjectList.restoreMeshState(preferences);
256
        }
257

    
258
      if( !mOldVersion.equals(mCurrVersion) ) displayNovelties();
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

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

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

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

    
302
      editor.putString("appVersion", mCurrVersion );
303
      editor.putInt("solverIndex", mSolverIndex );
304

    
305
      RubikObjectList.savePreferences(editor);
306
      RubikObjectList.saveMeshState(editor);
307

    
308
      boolean success = editor.commit();
309
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
310
      }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
    private void restorePreferences(SharedPreferences preferences, boolean justStarted)
315
      {
316
      mOldVersion = preferences.getString("appVersion","");
317
      mSolverIndex = preferences.getInt("solverIndex",0);
318

    
319
      RubikObjectList.restorePreferences(this,preferences,justStarted);
320
      RubikScores scores = RubikScores.getInstance();
321

    
322
      if( scores.isVerified() )
323
        {
324
        FirebaseAnalytics analytics = getAnalytics();
325
        analytics.setUserId(scores.getName());
326
        }
327
      }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
    private void updateBubble(int num)
332
      {
333
      runOnUiThread(new Runnable()
334
        {
335
        @Override
336
        public void run()
337
          {
338
          mNumUpdates = num;
339

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

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
// PUBLIC API
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
    public FirebaseAnalytics getAnalytics()
361
      {
362
      return mFirebaseAnalytics;
363
      }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
    public void switchToTutorial(String url, int objectOrdinal)
368
      {
369
      Intent intent = new Intent(this, TutorialActivity.class);
370
      intent.putExtra("url", url);
371
      intent.putExtra("obj", objectOrdinal);
372
      startActivity(intent);
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    public void switchToConfig(int objectOrdinal)
378
      {
379
      Intent intent = new Intent(this, ConfigActivity.class);
380
      intent.putExtra("obj", objectOrdinal);
381
      startActivity(intent);
382
      }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
    public void switchToBandagedCreator(int objectOrdinal)
387
      {
388
      Intent intent = new Intent(this, BandagedCreatorActivity.class);
389
      intent.putExtra("obj", objectOrdinal);
390
      startActivity(intent);
391
      }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
    public void switchToPurchase(int objectOrdinal)
396
      {
397
      Intent intent = new Intent(this, PurchaseActivity.class);
398
      intent.putExtra("obj", objectOrdinal);
399
      startActivity(intent);
400
      }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
    public void setSolverIndex(int index)
405
      {
406
      mSolverIndex = index;
407
      }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
    public int getSolverIndex()
412
      {
413
      return mSolverIndex;
414
      }
415

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

    
418
    public void onScores(View v)
419
      {
420
      Bundle sBundle = new Bundle();
421
      sBundle.putString("argument", "false");
422
      RubikDialogScores scores = new RubikDialogScores();
423
      scores.setArguments(sBundle);
424
      scores.show(getSupportFragmentManager(), null);
425
      }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
    public void onUpdates(View v)
430
      {
431
      RubikDialogUpdates diag = new RubikDialogUpdates();
432
      diag.show( getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
433
      }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
    public void onExit(View v)
438
      {
439
      RubikDialogExit diag = new RubikDialogExit();
440
      diag.show(getSupportFragmentManager(), null);
441
      }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
    public void onAbout(View v)
446
      {
447
      RubikDialogAbout diag = new RubikDialogAbout();
448
      diag.show(getSupportFragmentManager(), null);
449
      }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
    public void onBandage(View v)
454
      {
455
      RubikDialogCreators diag = new RubikDialogCreators();
456
      diag.show(getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
457
      }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
    public void receiveUpdate(RubikUpdates updates)
462
      {
463
      int num = updates.getCompletedNumber();
464
      updateBubble(num);
465
      }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
    public void objectDownloaded(String shortName)
470
      {
471
      mNumUpdates--;
472
      updateBubble(mNumUpdates);
473
      mGrid.updateGrid(this,mScreenWidth);
474
      }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
    public int getType()
479
      {
480
      return 0;
481
      }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
    public void errorUpdate()
486
      {
487
      android.util.Log.e("D", "NewRubikActivity: Error receiving downloaded objects update");
488
      }
489
}
(1-1/3)