Project

General

Profile

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

magiccube / src / main / java / org / distorted / playui / PlayActivity.java @ 88913ad4

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.playui;
11

    
12
import java.io.InputStream;
13

    
14
import android.content.SharedPreferences;
15
import android.os.Build;
16
import android.os.Bundle;
17
import android.util.DisplayMetrics;
18
import android.view.DisplayCutout;
19
import android.view.View;
20
import android.view.ViewGroup;
21
import android.view.WindowManager;
22
import android.widget.LinearLayout;
23

    
24
import androidx.appcompat.app.AppCompatActivity;
25
import androidx.preference.PreferenceManager;
26

    
27
import com.google.firebase.analytics.FirebaseAnalytics;
28

    
29
import org.distorted.dialogs.RubikDialogScores;
30
import org.distorted.external.RubikScores;
31
import org.distorted.library.main.DistortedLibrary;
32
import org.distorted.objectlib.main.InitAssets;
33
import org.distorted.objectlib.main.ObjectControl;
34
import org.distorted.objectlib.main.TwistyObject;
35
import org.distorted.dialogs.RubikDialogError;
36
import org.distorted.external.RubikFiles;
37
import org.distorted.main.MainActivity;
38
import org.distorted.main.R;
39
import org.distorted.objects.RubikObject;
40
import org.distorted.objects.RubikObjectList;
41
import org.distorted.os.OSInterface;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class PlayActivity extends AppCompatActivity implements RubikDialogScores.ScoresInvoker
46
{
47
    public static final int FLAGS            = MainActivity.FLAGS;
48
    public static final float TITLE_TEXT_SIZE= 0.060f;
49
    private static final int ACTIVITY_NUMBER = 6;
50
    private static final float RATIO_BAR     = MainActivity.RATIO_BAR;
51
    private static final float RATIO_INSET   = 0.09f;
52

    
53
    private static final String KEY_FREE = "movesController_free";
54
    private static final String KEY_SOLV = "movesController_solv";
55

    
56
    private static int mScreenWidth, mScreenHeight;
57
    private int mCurrentApiVersion;
58
    private String mObjectName;
59
    private int mNumScrambles;
60
    private int mHeightUpperBar;
61
    private boolean mObjectLocal;
62
    private int mObjectOrdinal;
63
    private int mLevel;
64
    private boolean mModeFree;
65
    private boolean mJustStarted;
66
    private FirebaseAnalytics mFirebaseAnalytics;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
    @Override
71
    protected void onCreate(Bundle savedState)
72
      {
73
      super.onCreate(savedState);
74
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
75
      setTheme(R.style.MaterialThemeNoActionBar);
76
      setContentView(R.layout.play);
77

    
78
      mJustStarted = true;
79
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
80

    
81
      Bundle b = getIntent().getExtras();
82

    
83
      if( b!=null )
84
        {
85
        mLevel         = b.getInt("level");
86
        mObjectName    = b.getString("name");
87
        mNumScrambles  = b.getInt("scrambles");
88
        mObjectLocal   = b.getBoolean("local");
89
        mObjectOrdinal = b.getInt("ordinal");
90
        }
91
      else
92
        {
93
        mLevel = -1;
94
        mObjectName = "";
95
        mNumScrambles = 0;
96
        mObjectLocal = true;
97
        mObjectOrdinal = 0;
98
        }
99

    
100
      mModeFree = (mLevel<0);
101

    
102
      DisplayMetrics displaymetrics = new DisplayMetrics();
103
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
104
      mScreenWidth =displaymetrics.widthPixels;
105
      mScreenHeight=displaymetrics.heightPixels;
106

    
107
      hideNavigationBar();
108
      cutoutHack();
109
      computeBarHeights();
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
// this does not include possible insets
114

    
115
    private void computeBarHeights()
116
      {
117
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
118
      mHeightUpperBar = barHeight;
119

    
120
      LinearLayout layoutTop = findViewById(R.id.upperBar);
121
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
122

    
123
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
124
      paramsTop.height = mHeightUpperBar;
125
      layoutTop.setLayoutParams(paramsTop);
126
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
127
      paramsBot.height = barHeight;
128
      layoutBot.setLayoutParams(paramsBot);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
    private void hideNavigationBar()
134
      {
135
      mCurrentApiVersion = Build.VERSION.SDK_INT;
136

    
137
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
138
        {
139
        final View decorView = getWindow().getDecorView();
140

    
141
        decorView.setSystemUiVisibility(FLAGS);
142

    
143
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
144
          {
145
          @Override
146
          public void onSystemUiVisibilityChange(int visibility)
147
            {
148
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
149
              {
150
              decorView.setSystemUiVisibility(FLAGS);
151
              }
152
            }
153
          });
154
        }
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    @Override
160
    public void onAttachedToWindow()
161
      {
162
      super.onAttachedToWindow();
163

    
164
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
165
        {
166
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
167
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
168

    
169
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
170
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
171
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
172
        layoutHid.setLayoutParams(paramsHid);
173
        mHeightUpperBar += paramsHid.height;
174
        }
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// do not avoid cutouts
179

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    @Override
191
    public void onWindowFocusChanged(boolean hasFocus)
192
      {
193
      super.onWindowFocusChanged(hasFocus);
194

    
195
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
196
        {
197
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
198
        }
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
    
203
    @Override
204
    protected void onPause() 
205
      {
206
      super.onPause();
207
      PlayView view = findViewById(R.id.playView);
208
      view.onPause();
209
      savePreferences();
210
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
    
215
    @Override
216
    protected void onResume() 
217
      {
218
      super.onResume();
219
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
220
      PlayView view = findViewById(R.id.playView);
221
      ObjectControl control = view.getObjectControl();
222
      view.onResume();
223

    
224
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
225
      restorePreferences(preferences);
226

    
227
      ScreenList sl =  mJustStarted ?
228
                      (mModeFree ? ScreenList.FREE : ScreenList.SCRA) :
229
                      ScreenList.getCurrentScreen();
230

    
231
      ScreenList.switchScreen(this,sl);
232

    
233
      if( !mJustStarted ) restoreMoves(preferences);
234

    
235
      if( mObjectName.length()>0 )
236
        {
237
        changeIfDifferent(mObjectName,mObjectLocal,mObjectOrdinal,control);
238
        }
239

    
240
      if( mJustStarted && !mModeFree )
241
        {
242
        control.scrambleObject(mNumScrambles);
243
        }
244

    
245
      mJustStarted = false;
246
      }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
    
250
    @Override
251
    protected void onDestroy() 
252
      {
253
      super.onDestroy();
254
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
255
      }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  private void savePreferences()
260
    {
261
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
262
    SharedPreferences.Editor editor = preferences.edit();
263

    
264
    for( int i=0; i<ScreenList.LENGTH; i++ )
265
      {
266
      ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
267
      }
268

    
269
    ScreenList.savePreferences(editor);
270

    
271
    ScreenList curr = ScreenList.getCurrentScreen();
272

    
273
    if( curr==ScreenList.FREE )
274
      {
275
      ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
276
      free.saveMovePreferences(KEY_FREE,editor);
277
      }
278
    if( curr==ScreenList.SOLV )
279
      {
280
      ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
281
      solv.saveMovePreferences(KEY_SOLV,editor);
282
      }
283

    
284
    PlayView view = findViewById(R.id.playView);
285
    OSInterface os = view.getInterface();
286
    os.setEditor(editor);
287
    view.getObjectControl().savePreferences();
288

    
289
    RubikScores scores = RubikScores.getInstance();
290
    scores.savePreferences(editor);
291

    
292
    editor.apply();
293
    }
294

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

    
297
  private void restorePreferences(SharedPreferences preferences)
298
    {
299
    for( int i=0; i<ScreenList.LENGTH; i++)
300
      {
301
      ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
302
      }
303

    
304
    if( !mJustStarted ) ScreenList.restorePreferences(preferences);
305

    
306
    PlayView view = findViewById(R.id.playView);
307
    OSInterface os = view.getInterface();
308
    os.setPreferences(preferences);
309
    view.getObjectControl().restorePreferences();
310
    }
311

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

    
314
  private void restoreMoves(SharedPreferences preferences)
315
    {
316
    ScreenList curr = ScreenList.getCurrentScreen();
317

    
318
    if( curr==ScreenList.FREE )
319
      {
320
      ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
321
      free.restoreMovePreferences(this,KEY_FREE,preferences);
322
      }
323
    if( curr==ScreenList.SOLV )
324
      {
325
      ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
326
      solv.restoreMovePreferences(this,KEY_SOLV,preferences);
327
      }
328
    }
329

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

    
332
    void OpenGLError()
333
      {
334
      RubikDialogError errDiag = new RubikDialogError();
335
      errDiag.show(getSupportFragmentManager(), null);
336
      }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
    private void changeIfDifferent(String name, boolean local, int ordinal, ObjectControl control)
341
      {
342
      if( local )
343
        {
344
        RubikFiles files = RubikFiles.getInstance();
345
        int iconMode = TwistyObject.MODE_NORM;
346
        InputStream jsonStream = files.openFile(this, name+"_object.json");
347
        InitAssets asset = new InitAssets(jsonStream, null, null);
348
        control.changeIfDifferent(ordinal,name,iconMode,asset);
349
        }
350
      else
351
        {
352
        RubikObject object = RubikObjectList.getObject(ordinal);
353
        int iconMode = TwistyObject.MODE_NORM;
354
        InputStream jsonStream = object==null ? null : object.getObjectStream(this);
355
        InputStream meshStream = object==null ? null : object.getMeshStream(this);
356
        PlayView view = findViewById(R.id.playView);
357
        OSInterface os = view.getInterface();
358
        InitAssets asset = new InitAssets(jsonStream, meshStream, os);
359
        control.changeIfDifferent(ordinal, name, iconMode, asset);
360
        }
361
      }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364
// PUBLIC API
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
    public FirebaseAnalytics getAnalytics()
368
      {
369
      return mFirebaseAnalytics;
370
      }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
    public int getScreenWidthInPixels()
375
      {
376
      return mScreenWidth;
377
      }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
    public int getScreenHeightInPixels()
382
      {
383
      return mScreenHeight;
384
      }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
    public int getNumScrambles()
389
      {
390
      return mNumScrambles;
391
      }
392

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

    
395
    public int getLevel()
396
      {
397
      return mLevel;
398
      }
399

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

    
402
    public int getObjectOrdinal()
403
      {
404
      return mObjectOrdinal;
405
      }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
    public ObjectControl getControl()
410
      {
411
      PlayView view = findViewById(R.id.playView);
412
      return view.getObjectControl();
413
      }
414
}
(1-1/12)