Project

General

Profile

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

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

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

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class PlayActivity extends AppCompatActivity
42
{
43
    private static final int ACTIVITY_NUMBER = 6;
44
    private static final float RATIO_BAR     = MainActivity.RATIO_BAR;
45
    private static final float RATIO_INSET   = 0.09f;
46
    public static final int FLAGS            = MainActivity.FLAGS;
47

    
48
    private static int mScreenWidth, mScreenHeight;
49
    private int mCurrentApiVersion;
50
    private PlayScreen mScreen;
51
    private String mObjectName;
52
    private int mNumScrambles;
53
    private int mHeightUpperBar;
54
    private boolean mObjectLocal;
55
    private int mObjectOrdinal;
56
    private boolean mModeFree;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
    @Override
61
    protected void onCreate(Bundle savedState)
62
      {
63
      super.onCreate(savedState);
64
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
65
      setTheme(R.style.MaterialThemeNoActionBar);
66
      setContentView(R.layout.play);
67

    
68
      Bundle b = getIntent().getExtras();
69

    
70
      if( b!=null )
71
        {
72
        mObjectName    = b.getString("name");
73
        mNumScrambles  = b.getInt("scrambles");
74
        mObjectLocal   = b.getBoolean("local");
75
        mObjectOrdinal = b.getInt("ordinal");
76
        mModeFree      = b.getBoolean("free");
77
        }
78
      else
79
        {
80
        mObjectName = "";
81
        mNumScrambles = 0;
82
        mObjectLocal = true;
83
        mObjectOrdinal = 0;
84
        mModeFree = true;
85
        }
86

    
87
      DisplayMetrics displaymetrics = new DisplayMetrics();
88
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
89
      mScreenWidth =displaymetrics.widthPixels;
90
      mScreenHeight=displaymetrics.heightPixels;
91

    
92
      hideNavigationBar();
93
      cutoutHack();
94
      computeBarHeights();
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// this does not include possible insets
99

    
100
    private void computeBarHeights()
101
      {
102
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
103
      mHeightUpperBar = barHeight;
104

    
105
      LinearLayout layoutTop = findViewById(R.id.upperBar);
106
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
107

    
108
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
109
      paramsTop.height = mHeightUpperBar;
110
      layoutTop.setLayoutParams(paramsTop);
111
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
112
      paramsBot.height = barHeight;
113
      layoutBot.setLayoutParams(paramsBot);
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    private void hideNavigationBar()
119
      {
120
      mCurrentApiVersion = Build.VERSION.SDK_INT;
121

    
122
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
123
        {
124
        final View decorView = getWindow().getDecorView();
125

    
126
        decorView.setSystemUiVisibility(FLAGS);
127

    
128
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
129
          {
130
          @Override
131
          public void onSystemUiVisibilityChange(int visibility)
132
            {
133
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
134
              {
135
              decorView.setSystemUiVisibility(FLAGS);
136
              }
137
            }
138
          });
139
        }
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
    @Override
145
    public void onAttachedToWindow()
146
      {
147
      super.onAttachedToWindow();
148

    
149
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
150
        {
151
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
152
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
153

    
154
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
155
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
156
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
157
        layoutHid.setLayoutParams(paramsHid);
158
        mHeightUpperBar += paramsHid.height;
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// do not avoid cutouts
164

    
165
    private void cutoutHack()
166
      {
167
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
168
        {
169
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
170
        }
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    @Override
176
    public void onWindowFocusChanged(boolean hasFocus)
177
      {
178
      super.onWindowFocusChanged(hasFocus);
179

    
180
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
181
        {
182
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
183
        }
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
    
188
    @Override
189
    protected void onPause() 
190
      {
191
      super.onPause();
192
      PlayView view = findViewById(R.id.playView);
193
      view.onPause();
194
      savePreferences();
195
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
    
200
    @Override
201
    protected void onResume() 
202
      {
203
      super.onResume();
204
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
205
      PlayView view = findViewById(R.id.playView);
206
      view.onResume();
207

    
208
      if( mScreen==null ) mScreen = new PlayScreen();
209
      mScreen.onAttachedToWindow(this, mObjectName);
210
      restorePreferences();
211

    
212
      if( mObjectName.length()>0 )
213
        {
214
        changeIfDifferent(mObjectName,mObjectLocal,mObjectOrdinal,view.getObjectControl());
215
        }
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
    
220
    @Override
221
    protected void onDestroy() 
222
      {
223
      super.onDestroy();
224
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

    
235
    editor.apply();
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  private void restorePreferences()
241
    {
242
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
243
    mScreen.restorePreferences(this,preferences);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    void OpenGLError()
249
      {
250
      RubikDialogError errDiag = new RubikDialogError();
251
      errDiag.show(getSupportFragmentManager(), null);
252
      }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
    private void changeIfDifferent(String upperName, boolean local, int ordinal, ObjectControl control)
257
      {
258
      android.util.Log.e("D", "changing to "+upperName+" local: "+local+" ordinal: "+ordinal);
259

    
260
      if( local )
261
        {
262
        RubikFiles files = RubikFiles.getInstance();
263
        int iconMode = TwistyObject.MODE_NORM;
264
        String lowerName = upperName.toLowerCase();
265
        InputStream jsonStream = files.openFile(this, lowerName+"_object.json");
266
        InitAssets asset = new InitAssets(jsonStream, null, null);
267
        control.changeIfDifferent(ordinal,upperName,iconMode,asset);
268
        }
269
      else
270
        {
271
        RubikObject object = RubikObjectList.getObject(ordinal);
272
        int iconMode = TwistyObject.MODE_NORM;
273
        InputStream jsonStream = object==null ? null : object.getObjectStream(this);
274
        InputStream meshStream = object==null ? null : object.getMeshStream(this);
275
        PlayView view = findViewById(R.id.playView);
276
        OSInterface os = view.getInterface();
277
        InitAssets asset = new InitAssets(jsonStream, meshStream, os);
278
        control.changeIfDifferent(ordinal, upperName, iconMode, asset);
279
        }
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
// PUBLIC API
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    public int getScreenWidthInPixels()
287
      {
288
      return mScreenWidth;
289
      }
290

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

    
293
    public int getScreenHeightInPixels()
294
      {
295
      return mScreenHeight;
296
      }
297

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

    
300
    public int getNumScrambles()
301
      {
302
      return mNumScrambles;
303
      }
304

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

    
307
    public ObjectControl getControl()
308
      {
309
      PlayView view = findViewById(R.id.playView);
310
      return view.getObjectControl();
311
      }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
    public PlayScreen getScreen()
316
      {
317
      return mScreen;
318
      }
319
}
(1-1/5)