Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedPlayActivity.java @ b8dab9fd

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 44fec653 Leszek Koltunski
// 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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.bandaged;
11
12 972f9eae Leszek Koltunski
import android.content.SharedPreferences;
13 9530f6b0 Leszek Koltunski
import android.os.Build;
14
import android.os.Bundle;
15 972f9eae Leszek Koltunski
import android.preference.PreferenceManager;
16 9530f6b0 Leszek Koltunski
import android.util.DisplayMetrics;
17 f3563327 Leszek Koltunski
import android.view.DisplayCutout;
18 9530f6b0 Leszek Koltunski
import android.view.View;
19
import android.view.ViewGroup;
20
import android.view.WindowManager;
21
import android.widget.LinearLayout;
22
23
import androidx.appcompat.app.AppCompatActivity;
24
25
import org.distorted.dialogs.RubikDialogError;
26 88b94310 Leszek Koltunski
import org.distorted.external.RubikFiles;
27 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
28
import org.distorted.main.R;
29
import org.distorted.objectlib.main.ObjectControl;
30 7cb8d4b0 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
31 9530f6b0 Leszek Koltunski
32
import java.io.InputStream;
33
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36
public class BandagedPlayActivity extends AppCompatActivity
37
{
38
    private static final int ACTIVITY_NUMBER = 4;
39
    private static final float RATIO_BAR  = 0.10f;
40 f3563327 Leszek Koltunski
    private static final float RATIO_INSET= 0.09f;
41 9530f6b0 Leszek Koltunski
42
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
43
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
44
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
45
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
46
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
47
48
    private static int mScreenWidth, mScreenHeight;
49
    private int mCurrentApiVersion;
50
    private BandagedPlayScreen mScreen;
51 88b94310 Leszek Koltunski
    private String mObjectName;
52 aa622165 Leszek Koltunski
    private int mHeightUpperBar;
53 9530f6b0 Leszek Koltunski
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
    @Override
57
    protected void onCreate(Bundle savedState)
58
      {
59
      super.onCreate(savedState);
60
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
61
      setTheme(R.style.MaterialThemeNoActionBar);
62
      setContentView(R.layout.bandaged_play);
63
64
      Bundle b = getIntent().getExtras();
65 88b94310 Leszek Koltunski
      mObjectName = b!=null ? b.getString("name") : "";
66 9530f6b0 Leszek Koltunski
67
      DisplayMetrics displaymetrics = new DisplayMetrics();
68 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
69 9530f6b0 Leszek Koltunski
      mScreenWidth =displaymetrics.widthPixels;
70
      mScreenHeight=displaymetrics.heightPixels;
71 7fe59aa5 Leszek Koltunski
72 9530f6b0 Leszek Koltunski
      hideNavigationBar();
73
      cutoutHack();
74
      computeBarHeights();
75
      }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
// this does not include possible insets
79
80
    private void computeBarHeights()
81
      {
82 7fe59aa5 Leszek Koltunski
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
83 f3563327 Leszek Koltunski
      mHeightUpperBar = barHeight;
84
85
      LinearLayout layoutTop = findViewById(R.id.upperBar);
86
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
87
88
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
89
      paramsTop.height = mHeightUpperBar;
90
      layoutTop.setLayoutParams(paramsTop);
91
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
92 aa622165 Leszek Koltunski
      paramsBot.height = barHeight;
93 f3563327 Leszek Koltunski
      layoutBot.setLayoutParams(paramsBot);
94 9530f6b0 Leszek Koltunski
      }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98
    private void hideNavigationBar()
99
      {
100
      mCurrentApiVersion = Build.VERSION.SDK_INT;
101
102
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
103
        {
104
        final View decorView = getWindow().getDecorView();
105
106
        decorView.setSystemUiVisibility(FLAGS);
107
108
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
109
          {
110
          @Override
111
          public void onSystemUiVisibilityChange(int visibility)
112
            {
113
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
114
              {
115
              decorView.setSystemUiVisibility(FLAGS);
116
              }
117
            }
118
          });
119
        }
120
      }
121
122 f3563327 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
    @Override
125
    public void onAttachedToWindow()
126
      {
127
      super.onAttachedToWindow();
128
129
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
130
        {
131
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
132
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
133
134
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
135
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
136
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
137
        layoutHid.setLayoutParams(paramsHid);
138
        mHeightUpperBar += paramsHid.height;
139
        }
140
      }
141
142 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
143
// do not avoid cutouts
144
145
    private void cutoutHack()
146
      {
147
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
148
        {
149
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
150
        }
151
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
    @Override
156
    public void onWindowFocusChanged(boolean hasFocus)
157
      {
158
      super.onWindowFocusChanged(hasFocus);
159
160
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
161
        {
162
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
163
        }
164
      }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    @Override
169
    protected void onPause() 
170
      {
171
      super.onPause();
172
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
173
      view.onPause();
174 972f9eae Leszek Koltunski
      savePreferences();
175 9530f6b0 Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
176
      }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
    
180
    @Override
181
    protected void onResume() 
182
      {
183
      super.onResume();
184
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
185
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
186
      view.onResume();
187
188
      if( mScreen==null ) mScreen = new BandagedPlayScreen();
189 972f9eae Leszek Koltunski
      mScreen.onAttachedToWindow(this, mObjectName);
190
      restorePreferences();
191 9530f6b0 Leszek Koltunski
192 88b94310 Leszek Koltunski
      if( mObjectName.length()>0 )
193 9530f6b0 Leszek Koltunski
        {
194 88b94310 Leszek Koltunski
        changeIfDifferent(mObjectName,view.getObjectControl());
195 9530f6b0 Leszek Koltunski
        }
196
      }
197
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
    
200
    @Override
201
    protected void onDestroy() 
202
      {
203
      super.onDestroy();
204
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
205
      }
206
207 972f9eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
  private void savePreferences()
210
    {
211
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
212
    SharedPreferences.Editor editor = preferences.edit();
213
    mScreen.savePreferences(this,editor);
214
215
    editor.apply();
216
    }
217
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
220
  private void restorePreferences()
221
    {
222
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
223
    mScreen.restorePreferences(this,preferences);
224
    }
225
226 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228
    void OpenGLError()
229
      {
230
      RubikDialogError errDiag = new RubikDialogError();
231
      errDiag.show(getSupportFragmentManager(), null);
232
      }
233
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236 88b94310 Leszek Koltunski
    private void changeIfDifferent(String name,ObjectControl control)
237 9530f6b0 Leszek Koltunski
      {
238 88b94310 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
239 9530f6b0 Leszek Koltunski
240 88b94310 Leszek Koltunski
      int meshState          = TwistyObject.MESH_NICE;
241
      int iconMode           = TwistyObject.MODE_NORM;
242
      InputStream jsonStream = files.openFile(this,name+"_object.json");
243
      InputStream meshStream = null;
244
      int ordinal            = 0; // if jsonStream!=null, this doesn't matter
245
246
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
247 9530f6b0 Leszek Koltunski
      }
248
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
// PUBLIC API
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
    public int getScreenWidthInPixels()
254
      {
255
      return mScreenWidth;
256
      }
257
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260
    public int getScreenHeightInPixels()
261
      {
262
      return mScreenHeight;
263
      }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267
    public ObjectControl getControl()
268
      {
269
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
270
      return view.getObjectControl();
271
      }
272
273 88b94310 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
    public BandagedPlayScreen getScreen()
276
      {
277
      return mScreen;
278
      }
279
280 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
    public static int getDrawableSize()
283
      {
284 aa622165 Leszek Koltunski
      if( mScreenHeight<1000 ) return 0;
285
      if( mScreenHeight<1600 ) return 1;
286
      if( mScreenHeight<1900 ) return 2;
287 9530f6b0 Leszek Koltunski
      return 3;
288
      }
289
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
292
    public static int getDrawable(int small, int medium, int big, int huge)
293
      {
294
      int size = getDrawableSize();
295
296
      switch(size)
297
        {
298
        case 0 : return small;
299
        case 1 : return medium;
300
        case 2 : return big;
301
        default: return huge;
302
        }
303
      }
304
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
    public boolean isVertical()
308
      {
309
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
310
      return view.isVertical();
311
      }
312
}