Project

General

Profile

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

magiccube / src / main / java / org / distorted / playui / PlayActivity.java @ ebaf99be

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
    editor.apply();
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  private void restorePreferences(SharedPreferences preferences)
289
    {
290
    for( int i=0; i<ScreenList.LENGTH; i++)
291
      {
292
      ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
293
      }
294

    
295
    if( !mJustStarted ) ScreenList.restorePreferences(preferences);
296
    }
297

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

    
300
  private void restoreMoves(SharedPreferences preferences)
301
    {
302
    ScreenList curr = ScreenList.getCurrentScreen();
303

    
304
    if( curr==ScreenList.FREE )
305
      {
306
      ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
307
      free.restoreMovePreferences(this,KEY_FREE,preferences);
308
      }
309
    if( curr==ScreenList.SOLV )
310
      {
311
      ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
312
      solv.restoreMovePreferences(this,KEY_SOLV,preferences);
313
      }
314
    }
315

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

    
318
    void OpenGLError()
319
      {
320
      RubikDialogError errDiag = new RubikDialogError();
321
      errDiag.show(getSupportFragmentManager(), null);
322
      }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    private void changeIfDifferent(String upperName, boolean local, int ordinal, ObjectControl control)
327
      {
328
      if( local )
329
        {
330
        RubikFiles files = RubikFiles.getInstance();
331
        int iconMode = TwistyObject.MODE_NORM;
332
        InputStream jsonStream = files.openFile(this, upperName+"_object.json");
333
        InitAssets asset = new InitAssets(jsonStream, null, null);
334
        control.changeIfDifferent(ordinal,upperName,iconMode,asset);
335
        }
336
      else
337
        {
338
        RubikObject object = RubikObjectList.getObject(ordinal);
339
        int iconMode = TwistyObject.MODE_NORM;
340
        InputStream jsonStream = object==null ? null : object.getObjectStream(this);
341
        InputStream meshStream = object==null ? null : object.getMeshStream(this);
342
        PlayView view = findViewById(R.id.playView);
343
        OSInterface os = view.getInterface();
344
        InitAssets asset = new InitAssets(jsonStream, meshStream, os);
345
        control.changeIfDifferent(ordinal, upperName, iconMode, asset);
346
        }
347
      }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
// PUBLIC API
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
    public FirebaseAnalytics getAnalytics()
354
      {
355
      return mFirebaseAnalytics;
356
      }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
    public int getScreenWidthInPixels()
361
      {
362
      return mScreenWidth;
363
      }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
    public int getScreenHeightInPixels()
368
      {
369
      return mScreenHeight;
370
      }
371

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

    
374
    public int getNumScrambles()
375
      {
376
      return mNumScrambles;
377
      }
378

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

    
381
    public int getLevel()
382
      {
383
      return mLevel;
384
      }
385

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

    
388
    public int getObjectOrdinal()
389
      {
390
      return mObjectOrdinal;
391
      }
392

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

    
395
    public ObjectControl getControl()
396
      {
397
      PlayView view = findViewById(R.id.playView);
398
      return view.getObjectControl();
399
      }
400
}
(1-1/12)