Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / MainActivity.java @ 2eb70e4a

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.TypedValue;
20
import android.view.DisplayCutout;
21
import android.view.View;
22
import android.view.ViewGroup;
23
import android.widget.LinearLayout;
24
import android.widget.ScrollView;
25
import android.widget.TextView;
26

    
27
import androidx.annotation.NonNull;
28

    
29
import com.google.firebase.analytics.FirebaseAnalytics;
30
import com.google.firebase.inappmessaging.FirebaseInAppMessaging;
31

    
32
import org.distorted.bandaged.BandagedActivity;
33
import org.distorted.config.ConfigActivity;
34
import org.distorted.helpers.BaseActivity;
35
import org.distorted.info.InfoActivity;
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.RubikObject;
46
import org.distorted.objects.RubikObjectList;
47
import org.distorted.patternui.PatternActivity;
48
import org.distorted.playui.PlayActivity;
49
import org.distorted.solverui.SolverActivity;
50
import org.distorted.tutorials.TutorialActivity;
51

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

    
54
public class MainActivity extends BaseActivity implements RubikNetwork.Updatee, RubikDialogScores.ScoresInvoker
55
{
56
    private boolean mJustStarted;
57
    private FirebaseAnalytics mFirebaseAnalytics;
58
    private String mOldVersion, mCurrVersion;
59
    private TextView mBubbleUpdates;
60
    private int mNumUpdates;
61
    private int mCurrentObject;
62
    private MainScrollGrid mGrid;
63
    private int mSortMode;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
    @Override
68
    protected void onCreate(Bundle savedState)
69
      {
70
      super.onCreate(savedState);
71
      setContentView(R.layout.main);
72

    
73
      mCurrentObject = 0;
74
      mJustStarted = true;
75
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
76

    
77
      hideNavigationBar();
78
      cutoutHack();
79
      computeHeights();
80

    
81
      getWindowWidth(getResources().getConfiguration());
82
android.util.Log.e("D", "onCreate: "+mScreenWidth+" "+mScreenHeight);
83

    
84
      mCurrVersion = getAppVers();
85

    
86
      mBubbleUpdates = findViewById(R.id.bubbleUpdates);
87
      mBubbleUpdates.setVisibility(View.INVISIBLE);
88
      mNumUpdates = 0;
89

    
90
      mGrid = new MainScrollGrid();
91
      mGrid.createGrid(this,mScreenWidth,mSortMode);
92

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

    
102
      thread.start();
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
// this does not include possible insets
107

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

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

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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
/*
125
    @Override
126
    public void onAttachedToWindow()
127
      {
128
      super.onAttachedToWindow();
129

    
130
      getWindowWidth(getResources().getConfiguration());
131

    
132

    
133
android.util.Log.e("D", "onAttachedToWindow: "+mScreenWidth+" "+mScreenHeight);
134

    
135

    
136
      mGrid = new MainScrollGrid();
137
      mGrid.createGrid(this,mScreenWidth,mSortMode);
138

    
139
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
140
        {
141
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
142
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
143

    
144
        if( insetHeight>0 )
145
          {
146
          LinearLayout.LayoutParams pH = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, RATIO_HID);
147
          LinearLayout layoutHid = findViewById(R.id.hiddenBar);
148
          layoutHid.setLayoutParams(pH);
149

    
150
          LinearLayout.LayoutParams pS = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1-2*RATIO_BAR-RATIO_HID);
151
          ScrollView scroll = findViewById(R.id.objectScroll);
152
          scroll.setLayoutParams(pS);
153
          }
154
        }
155
      }
156
*/
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    @Override
160
    public void onConfigurationChanged(@NonNull Configuration conf)
161
      {
162
      super.onConfigurationChanged(conf);
163

    
164
android.util.Log.e("D", "onConfigurationChanged");
165

    
166
      getWindowWidth(conf);
167
      if( mGrid!=null ) mGrid.updateGrid(this,mScreenWidth);
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
    
172
    @Override
173
    protected void onPause() 
174
      {
175
      super.onPause();
176
      RubikNetwork.onPause();
177
      savePreferences();
178

    
179

    
180
android.util.Log.e("D", "onPause");
181

    
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
    
186
    @Override
187
    protected void onResume() 
188
      {
189
      super.onResume();
190

    
191
android.util.Log.e("D", "onResume");
192

    
193
      restorePreferences(mJustStarted);
194

    
195
      RubikNetwork network = RubikNetwork.getInstance();
196
      network.signUpForUpdates(this);
197

    
198
      if( mJustStarted )
199
        {
200
        mJustStarted = false;
201

    
202
        network.downloadUpdates(this);
203
        RubikScores scores = RubikScores.getInstance();
204
        scores.incrementNumRuns();
205
        scores.setCountry(this);
206
        }
207

    
208
      if( !mOldVersion.equals(mCurrVersion) ) displayNovelties();
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
    private void displayNovelties()
214
      {
215
      Bundle bundle = new Bundle();
216
      bundle.putString("argument",mOldVersion);
217
      RubikDialogAbout newDialog = new RubikDialogAbout();
218
      newDialog.setArguments(bundle);
219
      newDialog.show(getSupportFragmentManager(), RubikDialogAbout.getDialogTag() );
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
    
224
    @Override
225
    protected void onDestroy() 
226
      {
227
      super.onDestroy();
228

    
229
android.util.Log.e("D", "onDestroy");
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    private String getAppVers()
235
      {
236
      try
237
        {
238
        PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
239
        return pInfo.versionName;
240
        }
241
      catch( PackageManager.NameNotFoundException e )
242
        {
243
        return "unknown";
244
        }
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
    private void savePreferences()
250
      {
251
      SharedPreferences.Editor editor = mPreferences.edit();
252

    
253
      editor.putString("appVersion", mCurrVersion );
254
      editor.putInt("sortMode", mSortMode);
255

    
256
      RubikObjectList.savePreferences(editor);
257

    
258
      boolean success = editor.commit();
259
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
    private void restorePreferences(boolean justStarted)
265
      {
266
      mOldVersion = mPreferences.getString("appVersion","");
267
      mSortMode = mPreferences.getInt("sortMode", MainSettingsPopup.SORT_DEFAULT);
268

    
269
      RubikObjectList.restorePreferences(this,mPreferences,justStarted);
270
      RubikScores scores = RubikScores.getInstance();
271
      scores.restorePreferences(mPreferences);
272

    
273
      if( scores.isVerified() )
274
        {
275
        FirebaseAnalytics analytics = getAnalytics();
276
        analytics.setUserId(scores.getName());
277
        }
278
      }
279

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

    
282
    private void updateBubble(int num)
283
      {
284
      runOnUiThread(new Runnable()
285
        {
286
        @Override
287
        public void run()
288
          {
289
          mNumUpdates = num;
290

    
291
          if( num>0 )
292
            {
293
            String shownNum = String.valueOf(num);
294
            mBubbleUpdates.setText(shownNum);
295
            mBubbleUpdates.setVisibility(View.VISIBLE);
296
            int height = (int)(0.05f*mScreenWidth);
297
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
298
            }
299
          else
300
            {
301
            mBubbleUpdates.setVisibility(View.INVISIBLE);
302
            }
303
          }
304
        });
305
      }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308
// PUBLIC API
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
    public FirebaseAnalytics getAnalytics()
312
      {
313
      return mFirebaseAnalytics;
314
      }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
    public int getObjectOrdinal()
319
      {
320
      return mCurrentObject;
321
      }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
    public void setCurrentObject(int current)
326
      {
327
      mCurrentObject = current;
328
      }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
    public void switchToTutorial(int objectOrdinal)
333
      {
334
      Intent intent = new Intent(this, TutorialActivity.class);
335
      intent.putExtra("obj", objectOrdinal);
336
      startActivity(intent);
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
    public void switchToInfo(int objectOrdinal)
342
      {
343
      Intent intent = new Intent(this, InfoActivity.class);
344
      intent.putExtra("obj", objectOrdinal);
345
      startActivity(intent);
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
    public void switchToConfig(int objectOrdinal)
351
      {
352
      Intent intent = new Intent(this, ConfigActivity.class);
353
      intent.putExtra("obj", objectOrdinal);
354
      startActivity(intent);
355
      }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
    public void switchToBandagedCreator(int objectOrdinal)
360
      {
361
      Intent intent = new Intent(this, BandagedActivity.class);
362
      intent.putExtra("obj", objectOrdinal);
363
      startActivity(intent);
364
      }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

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

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

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

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

    
386
    public void switchToPlay(RubikObject object, int ordinal, int scrambles, int level)
387
      {
388
      boolean local = object.isLocal();
389
      String name = local ? object.getLowerName() : object.getUpperName();
390

    
391
      Intent intent = new Intent(this, PlayActivity.class);
392
      intent.putExtra("level", level);
393
      intent.putExtra("name", name );
394
      intent.putExtra("scrambles", scrambles);
395
      intent.putExtra("local", local );
396
      intent.putExtra("ordinal", ordinal );
397
      startActivity(intent);
398
      }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
    public void onScores(View v)
403
      {
404
      Bundle sBundle = new Bundle();
405
      sBundle.putString("argument", "false");
406
      RubikDialogScores scores = new RubikDialogScores();
407
      scores.setArguments(sBundle);
408
      scores.show(getSupportFragmentManager(), null);
409
      }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
    public void onSettings(View v)
414
      {
415
      int sw = (int)(mScreenWidth*0.70);
416
      int sh = (int)(mScreenHeight*0.25f);
417

    
418
      int vw = v.getWidth();
419

    
420
      MainSettingsPopup popup = new MainSettingsPopup(this,mSortMode,mCurrentTheme,mScreenWidth,sw,sh);
421
      popup.displayPopup(this,v,((vw-sw)/2),0);
422
      }
423

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

    
426
    public void onUpdates(View v)
427
      {
428
      RubikDialogUpdates diag = new RubikDialogUpdates();
429
      diag.show( getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
430
      }
431

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

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

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

    
442
    public void onAbout(View v)
443
      {
444
      RubikDialogAbout diag = new RubikDialogAbout();
445
      diag.show(getSupportFragmentManager(), null);
446
      }
447

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

    
450
    public void onBandage(View v)
451
      {
452
      RubikDialogCreators diag = new RubikDialogCreators();
453
      diag.show(getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
454
      }
455

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

    
458
    public void receiveUpdate(RubikUpdates updates)
459
      {
460
      int num = updates.getCompletedNumber();
461
      updateBubble(num);
462
      }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
    public void objectDownloaded(String shortName)
467
      {
468
      mNumUpdates--;
469
      updateBubble(mNumUpdates);
470
      mGrid.updateGrid(this,mScreenWidth);
471
      }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
    public int getType()
476
      {
477
      return 0;
478
      }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
    public void sortObjectsBy(int sortMode)
483
      {
484
      mSortMode = sortMode;
485
      mGrid.createGrid(this,mScreenWidth,mSortMode);
486
      }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
    public void errorUpdate()
491
      {
492
      android.util.Log.e("D", "NewRubikActivity: Error receiving downloaded objects update");
493
      }
494
}
(1-1/4)