Project

General

Profile

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

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

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
import androidx.preference.PreferenceManager;
29

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

    
33
import org.distorted.bandaged.BandagedActivity;
34
import org.distorted.config.ConfigActivity;
35
import org.distorted.helpers.BaseActivity;
36
import org.distorted.info.InfoActivity;
37
import org.distorted.dialogs.RubikDialogAbout;
38
import org.distorted.dialogs.RubikDialogCreators;
39
import org.distorted.dialogs.RubikDialogExit;
40
import org.distorted.dialogs.RubikDialogScores;
41
import org.distorted.dialogs.RubikDialogUpdates;
42
import org.distorted.external.RubikNetwork;
43
import org.distorted.external.RubikScores;
44
import org.distorted.external.RubikUpdates;
45
import org.distorted.messaging.RubikInAppMessanging;
46
import org.distorted.objects.RubikObject;
47
import org.distorted.objects.RubikObjectList;
48
import org.distorted.patternui.PatternActivity;
49
import org.distorted.playui.PlayActivity;
50
import org.distorted.solverui.SolverActivity;
51
import org.distorted.tutorials.TutorialActivity;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

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

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

    
82
      mCurrVersion = getAppVers();
83

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

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

    
97
      thread.start();
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// this does not include possible insets
102

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

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

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    @Override
121
    public void onAttachedToWindow()
122
      {
123
      super.onAttachedToWindow();
124

    
125
      getWindowWidth(getResources().getConfiguration());
126
      mGrid = new MainScrollGrid();
127
      mGrid.createGrid(this,mScreenWidth,mSortMode);
128

    
129
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
130
        {
131
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
132
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
133

    
134
        if( insetHeight>0 )
135
          {
136
          LinearLayout.LayoutParams pH = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, RATIO_BAR);
137
          LinearLayout layoutHid = findViewById(R.id.hiddenBar);
138
          layoutHid.setLayoutParams(pH);
139

    
140
          LinearLayout.LayoutParams pS = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1-3*RATIO_BAR);
141
          ScrollView scroll = findViewById(R.id.objectScroll);
142
          scroll.setLayoutParams(pS);
143
          }
144
        }
145
      }
146

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

    
149
    @Override
150
    public void onConfigurationChanged(@NonNull Configuration conf)
151
      {
152
      super.onConfigurationChanged(conf);
153

    
154
      getWindowWidth(conf);
155
      if( mGrid!=null ) mGrid.updateGrid(this,mScreenWidth);
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
    @Override
161
    protected void onPause() 
162
      {
163
      super.onPause();
164
      RubikNetwork.onPause();
165
      savePreferences();
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
    
170
    @Override
171
    protected void onResume() 
172
      {
173
      super.onResume();
174

    
175
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
176
      restorePreferences(preferences,mJustStarted);
177

    
178
      RubikNetwork network = RubikNetwork.getInstance();
179
      network.signUpForUpdates(this);
180

    
181
      if( mJustStarted )
182
        {
183
        mJustStarted = false;
184

    
185
        network.downloadUpdates(this);
186
        RubikScores scores = RubikScores.getInstance();
187
        scores.incrementNumRuns();
188
        scores.setCountry(this);
189
        }
190

    
191
      if( !mOldVersion.equals(mCurrVersion) ) displayNovelties();
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    private void displayNovelties()
197
      {
198
      Bundle bundle = new Bundle();
199
      bundle.putString("argument",mOldVersion);
200
      RubikDialogAbout newDialog = new RubikDialogAbout();
201
      newDialog.setArguments(bundle);
202
      newDialog.show(getSupportFragmentManager(), RubikDialogAbout.getDialogTag() );
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
    
207
    @Override
208
    protected void onDestroy() 
209
      {
210
      super.onDestroy();
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
    private String getAppVers()
216
      {
217
      try
218
        {
219
        PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
220
        return pInfo.versionName;
221
        }
222
      catch( PackageManager.NameNotFoundException e )
223
        {
224
        return "unknown";
225
        }
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    private void savePreferences()
231
      {
232
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
233
      SharedPreferences.Editor editor = preferences.edit();
234

    
235
      editor.putString("appVersion", mCurrVersion );
236
      editor.putInt("sortMode", mSortMode);
237

    
238
      RubikObjectList.savePreferences(editor);
239

    
240
      boolean success = editor.commit();
241
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    private void restorePreferences(SharedPreferences preferences, boolean justStarted)
247
      {
248
      mOldVersion = preferences.getString("appVersion","");
249
      mSortMode = preferences.getInt("sortMode", MainSettingsPopup.SORT_DEFAULT);
250

    
251
      RubikObjectList.restorePreferences(this,preferences,justStarted);
252
      RubikScores scores = RubikScores.getInstance();
253
      scores.restorePreferences(preferences);
254

    
255
      if( scores.isVerified() )
256
        {
257
        FirebaseAnalytics analytics = getAnalytics();
258
        analytics.setUserId(scores.getName());
259
        }
260
      }
261

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

    
264
    private void updateBubble(int num)
265
      {
266
      runOnUiThread(new Runnable()
267
        {
268
        @Override
269
        public void run()
270
          {
271
          mNumUpdates = num;
272

    
273
          if( num>0 )
274
            {
275
            String shownNum = String.valueOf(num);
276
            mBubbleUpdates.setText(shownNum);
277
            mBubbleUpdates.setVisibility(View.VISIBLE);
278
            int height = (int)(0.05f*mScreenWidth);
279
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
280
            }
281
          else
282
            {
283
            mBubbleUpdates.setVisibility(View.INVISIBLE);
284
            }
285
          }
286
        });
287
      }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290
// PUBLIC API
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public FirebaseAnalytics getAnalytics()
294
      {
295
      return mFirebaseAnalytics;
296
      }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
    public int getObjectOrdinal()
301
      {
302
      return mCurrentObject;
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    public void setCurrentObject(int current)
308
      {
309
      mCurrentObject = current;
310
      }
311

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

    
314
    public void switchToTutorial(int objectOrdinal)
315
      {
316
      Intent intent = new Intent(this, TutorialActivity.class);
317
      intent.putExtra("obj", objectOrdinal);
318
      startActivity(intent);
319
      }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
    public void switchToInfo(int objectOrdinal)
324
      {
325
      Intent intent = new Intent(this, InfoActivity.class);
326
      intent.putExtra("obj", objectOrdinal);
327
      startActivity(intent);
328
      }
329

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

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

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

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

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

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

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

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

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

    
368
    public void switchToPlay(RubikObject object, int ordinal, int scrambles, int level)
369
      {
370
      boolean local = object.isLocal();
371
      String name = local ? object.getLowerName() : object.getUpperName();
372

    
373
      Intent intent = new Intent(this, PlayActivity.class);
374
      intent.putExtra("level", level);
375
      intent.putExtra("name", name );
376
      intent.putExtra("scrambles", scrambles);
377
      intent.putExtra("local", local );
378
      intent.putExtra("ordinal", ordinal );
379
      startActivity(intent);
380
      }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
    public void onScores(View v)
385
      {
386
      Bundle sBundle = new Bundle();
387
      sBundle.putString("argument", "false");
388
      RubikDialogScores scores = new RubikDialogScores();
389
      scores.setArguments(sBundle);
390
      scores.show(getSupportFragmentManager(), null);
391
      }
392

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

    
395
    public void onSettings(View v)
396
      {
397
      int sw = (int)(mScreenWidth*0.7);
398
      int sh = (int)(mScreenHeight*0.2f);
399

    
400
      int vw = v.getWidth();
401

    
402
      MainSettingsPopup popup = new MainSettingsPopup(this,mSortMode,mScreenWidth,mScreenHeight);
403
      popup.displayPopup(this,v,sw,sh,((vw-sw)/2),0);
404
      }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
    public void onUpdates(View v)
409
      {
410
      RubikDialogUpdates diag = new RubikDialogUpdates();
411
      diag.show( getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
412
      }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
    public void onExit(View v)
417
      {
418
      RubikDialogExit diag = new RubikDialogExit();
419
      diag.show(getSupportFragmentManager(), null);
420
      }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
    public void onAbout(View v)
425
      {
426
      RubikDialogAbout diag = new RubikDialogAbout();
427
      diag.show(getSupportFragmentManager(), null);
428
      }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
    public void onBandage(View v)
433
      {
434
      RubikDialogCreators diag = new RubikDialogCreators();
435
      diag.show(getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
436
      }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
    public void receiveUpdate(RubikUpdates updates)
441
      {
442
      int num = updates.getCompletedNumber();
443
      updateBubble(num);
444
      }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
    public void objectDownloaded(String shortName)
449
      {
450
      mNumUpdates--;
451
      updateBubble(mNumUpdates);
452
      mGrid.updateGrid(this,mScreenWidth);
453
      }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

    
457
    public int getType()
458
      {
459
      return 0;
460
      }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
    public void sortObjectsBy(int sortMode)
465
      {
466
      mSortMode = sortMode;
467
      mGrid.createGrid(this,mScreenWidth,mSortMode);
468
      }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

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