Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / MainActivity.java @ 7f7fff03

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.Bundle;
18
import android.util.TypedValue;
19
import android.view.View;
20
import android.view.ViewGroup;
21
import android.widget.LinearLayout;
22
import android.widget.ScrollView;
23
import android.widget.TextView;
24

    
25
import androidx.annotation.NonNull;
26

    
27
import com.google.firebase.analytics.FirebaseAnalytics;
28
import com.google.firebase.inappmessaging.FirebaseInAppMessaging;
29

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
71
      mCurrentObject = 0;
72
      mJustStarted = true;
73
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
74

    
75
      hideNavigationBar();
76
      cutoutHack();
77
      computeHeights();
78

    
79
      getWindowWidth(getResources().getConfiguration());
80

    
81
      mCurrVersion = getAppVers();
82

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

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

    
96
      thread.start();
97
      }
98

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

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

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

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
    @Override
120
    public void onConfigurationChanged(@NonNull Configuration conf)
121
      {
122
      super.onConfigurationChanged(conf);
123
      getWindowWidth(conf);
124
      if( mGrid!=null ) mGrid.updateGrid(this,mScreenWidth,mDarkC,mPassedC);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    @Override
130
    protected void onPause() 
131
      {
132
      super.onPause();
133
      RubikNetwork.onPause();
134
      savePreferences();
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
    @Override
140
    protected void onResume() 
141
      {
142
      super.onResume();
143

    
144
      restorePreferences(mJustStarted);
145

    
146
      mGrid = new MainScrollGrid();
147
      mGrid.createGrid(this,mScreenWidth,mSortMode,mDarkC,mPassedC);
148

    
149
      RubikNetwork network = RubikNetwork.getInstance();
150
      network.signUpForUpdates(this);
151

    
152
      if( mJustStarted )
153
        {
154
        mJustStarted = false;
155

    
156
        network.downloadUpdates(this);
157
        RubikScores scores = RubikScores.getInstance();
158
        scores.incrementNumRuns();
159
        scores.setCountry(this);
160
        }
161

    
162
      if( !mOldVersion.equals(mCurrVersion) ) displayNovelties();
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    private void displayNovelties()
168
      {
169
      Bundle bundle = new Bundle();
170
      bundle.putString("argument",mOldVersion);
171
      RubikDialogAbout newDialog = new RubikDialogAbout();
172
      newDialog.setArguments(bundle);
173
      newDialog.show(getSupportFragmentManager(), RubikDialogAbout.getDialogTag() );
174
      }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178
    @Override
179
    protected void onDestroy() 
180
      {
181
      super.onDestroy();
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
    private String getAppVers()
187
      {
188
      try
189
        {
190
        PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0);
191
        return pInfo.versionName;
192
        }
193
      catch( PackageManager.NameNotFoundException e )
194
        {
195
        return "unknown";
196
        }
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
    private void savePreferences()
202
      {
203
      SharedPreferences.Editor editor = mPreferences.edit();
204

    
205
      editor.putString("appVersion", mCurrVersion );
206
      editor.putInt("sortMode", mSortMode);
207

    
208
      RubikObjectList.savePreferences(editor);
209

    
210
      boolean success = editor.commit();
211
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    private void restorePreferences(boolean justStarted)
217
      {
218
      mOldVersion = mPreferences.getString("appVersion","");
219
      mSortMode = mPreferences.getInt("sortMode", MainSettingsPopup.SORT_DEFAULT);
220

    
221
      RubikObjectList.restorePreferences(this,mPreferences,justStarted);
222
      RubikScores scores = RubikScores.getInstance();
223
      scores.restorePreferences(mPreferences);
224

    
225
      if( scores.isVerified() )
226
        {
227
        FirebaseAnalytics analytics = getAnalytics();
228
        analytics.setUserId(scores.getName());
229
        }
230
      }
231

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

    
234
    private void updateBubble(int num)
235
      {
236
      runOnUiThread(new Runnable()
237
        {
238
        @Override
239
        public void run()
240
          {
241
          mNumUpdates = num;
242

    
243
          if( num>0 )
244
            {
245
            String shownNum = String.valueOf(num);
246
            mBubbleUpdates.setText(shownNum);
247
            mBubbleUpdates.setVisibility(View.VISIBLE);
248
            int height = (int)(0.05f*mScreenWidth);
249
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
250
            }
251
          else
252
            {
253
            mBubbleUpdates.setVisibility(View.INVISIBLE);
254
            }
255
          }
256
        });
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
// PUBLIC API
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
    public FirebaseAnalytics getAnalytics()
264
      {
265
      return mFirebaseAnalytics;
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    public int getObjectOrdinal()
271
      {
272
      return mCurrentObject;
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    public void setCurrentObject(int current)
278
      {
279
      mCurrentObject = current;
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    public void switchToTutorial(int objectOrdinal)
285
      {
286
      Intent intent = new Intent(this, TutorialActivity.class);
287
      intent.putExtra("obj", objectOrdinal);
288
      startActivity(intent);
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public void switchToInfo(int objectOrdinal)
294
      {
295
      Intent intent = new Intent(this, InfoActivity.class);
296
      intent.putExtra("obj", objectOrdinal);
297
      startActivity(intent);
298
      }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    public void switchToConfig(int objectOrdinal)
303
      {
304
      Intent intent = new Intent(this, ConfigActivity.class);
305
      intent.putExtra("obj", objectOrdinal);
306
      startActivity(intent);
307
      }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
    public void switchToBandagedCreator(int objectOrdinal)
312
      {
313
      Intent intent = new Intent(this, BandagedActivity.class);
314
      intent.putExtra("obj", objectOrdinal);
315
      startActivity(intent);
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
    public void switchToSolver(int objectOrdinal)
321
      {
322
      Intent intent = new Intent(this, SolverActivity.class);
323
      intent.putExtra("obj", objectOrdinal);
324
      startActivity(intent);
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    public void switchToPattern(int objectOrdinal)
330
      {
331
      Intent intent = new Intent(this, PatternActivity.class);
332
      intent.putExtra("obj", objectOrdinal);
333
      startActivity(intent);
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
    public void switchToPlay(RubikObject object, int ordinal, int scrambles, int level)
339
      {
340
      boolean local = object.isLocal();
341
      String name = local ? object.getLowerName() : object.getUpperName();
342

    
343
      Intent intent = new Intent(this, PlayActivity.class);
344
      intent.putExtra("level", level);
345
      intent.putExtra("name", name );
346
      intent.putExtra("scrambles", scrambles);
347
      intent.putExtra("local", local );
348
      intent.putExtra("ordinal", ordinal );
349
      startActivity(intent);
350
      }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
    public void onScores(View v)
355
      {
356
      Bundle sBundle = new Bundle();
357
      sBundle.putString("argument", "false");
358
      RubikDialogScores scores = new RubikDialogScores();
359
      scores.setArguments(sBundle);
360
      scores.show(getSupportFragmentManager(), null);
361
      }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
    public void onSettings(View v)
366
      {
367
      int sw = (int)(345*mDensity + 0.5f);
368
      int sh = (int)(188*mDensity + 0.5f);
369

    
370
      int vw = v.getWidth();
371

    
372
      MainSettingsPopup popup = new MainSettingsPopup(this,mSortMode,mCurrentTheme,sw,sh);
373
      popup.displayPopup(this,v,((vw-sw)/2),0);
374
      }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
    public void onUpdates(View v)
379
      {
380
      RubikDialogUpdates diag = new RubikDialogUpdates();
381
      diag.show( getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
382
      }
383

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

    
386
    public void onExit(View v)
387
      {
388
      RubikDialogExit diag = new RubikDialogExit();
389
      diag.show(getSupportFragmentManager(), null);
390
      }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
    public void onAbout(View v)
395
      {
396
      RubikDialogAbout diag = new RubikDialogAbout();
397
      diag.show(getSupportFragmentManager(), null);
398
      }
399

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

    
402
    public void onBandage(View v)
403
      {
404
      RubikDialogCreators diag = new RubikDialogCreators();
405
      diag.show(getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
406
      }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
    public void receiveUpdate(RubikUpdates updates)
411
      {
412
      int num = updates.getCompletedNumber();
413
      updateBubble(num);
414
      }
415

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

    
418
    public void objectDownloaded(String shortName)
419
      {
420
      mNumUpdates--;
421
      updateBubble(mNumUpdates);
422
      mGrid.updateGrid(this,mScreenWidth,mDarkC,mPassedC);
423
      }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
    public int getType()
428
      {
429
      return 0;
430
      }
431

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

    
434
    public void sortObjectsBy(int sortMode)
435
      {
436
      mSortMode = sortMode;
437
      mGrid.createGrid(this,mScreenWidth,mSortMode,mDarkC,mPassedC);
438
      }
439

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

    
442
    public void errorUpdate()
443
      {
444
      android.util.Log.e("D", "NewRubikActivity: Error receiving downloaded objects update");
445
      }
446
}
(1-1/4)