Project

General

Profile

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

magiccube / src / main / java / org / distorted / playui / PlayActivity.java @ 953f5ac9

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.library.main.DistortedLibrary;
31
import org.distorted.objectlib.main.InitAssets;
32
import org.distorted.objectlib.main.ObjectControl;
33
import org.distorted.objectlib.main.TwistyObject;
34
import org.distorted.dialogs.RubikDialogError;
35
import org.distorted.external.RubikFiles;
36
import org.distorted.main.MainActivity;
37
import org.distorted.main.R;
38
import org.distorted.objects.RubikObject;
39
import org.distorted.objects.RubikObjectList;
40
import org.distorted.os.OSInterface;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

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

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

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

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

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

    
99
      mModeFree = (mLevel<0);
100

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

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

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

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

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

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

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

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

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

    
140
        decorView.setSystemUiVisibility(FLAGS);
141

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

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

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

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

    
168
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
169
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
170
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
171
        layoutHid.setLayoutParams(paramsHid);
172
        mHeightUpperBar += paramsHid.height;
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
    @Override
190
    public void onWindowFocusChanged(boolean hasFocus)
191
      {
192
      super.onWindowFocusChanged(hasFocus);
193

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

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

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

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

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

    
230
      ScreenList.switchScreen(this,sl);
231

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

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

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

    
244
      mJustStarted = false;
245
      }
246

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

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

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

    
268
    ScreenList.savePreferences(editor);
269

    
270
    ScreenList curr = ScreenList.getCurrentScreen();
271

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

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

    
288
    editor.apply();
289
    }
290

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

    
293
  private void restorePreferences(SharedPreferences preferences)
294
    {
295
    for( int i=0; i<ScreenList.LENGTH; i++)
296
      {
297
      ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
298
      }
299

    
300
    if( !mJustStarted ) ScreenList.restorePreferences(preferences);
301

    
302
    PlayView view = findViewById(R.id.playView);
303
    OSInterface os = view.getInterface();
304
    os.setPreferences(preferences);
305
    view.getObjectControl().restorePreferences();
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  private void restoreMoves(SharedPreferences preferences)
311
    {
312
    ScreenList curr = ScreenList.getCurrentScreen();
313

    
314
    if( curr==ScreenList.FREE )
315
      {
316
      ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
317
      free.restoreMovePreferences(this,KEY_FREE,preferences);
318
      }
319
    if( curr==ScreenList.SOLV )
320
      {
321
      ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
322
      solv.restoreMovePreferences(this,KEY_SOLV,preferences);
323
      }
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
    void OpenGLError()
329
      {
330
      RubikDialogError errDiag = new RubikDialogError();
331
      errDiag.show(getSupportFragmentManager(), null);
332
      }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

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

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
// PUBLIC API
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
    public FirebaseAnalytics getAnalytics()
364
      {
365
      return mFirebaseAnalytics;
366
      }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
    public int getScreenWidthInPixels()
371
      {
372
      return mScreenWidth;
373
      }
374

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

    
377
    public int getScreenHeightInPixels()
378
      {
379
      return mScreenHeight;
380
      }
381

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

    
384
    public int getNumScrambles()
385
      {
386
      return mNumScrambles;
387
      }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
    public int getLevel()
392
      {
393
      return mLevel;
394
      }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
    public int getObjectOrdinal()
399
      {
400
      return mObjectOrdinal;
401
      }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
    public ObjectControl getControl()
406
      {
407
      PlayView view = findViewById(R.id.playView);
408
      return view.getObjectControl();
409
      }
410
}
(1-1/12)