Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedPlayActivity.java @ 7fe59aa5

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
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.bandaged;
21
22 972f9eae Leszek Koltunski
import android.content.SharedPreferences;
23 9530f6b0 Leszek Koltunski
import android.os.Build;
24
import android.os.Bundle;
25 972f9eae Leszek Koltunski
import android.preference.PreferenceManager;
26 9530f6b0 Leszek Koltunski
import android.util.DisplayMetrics;
27 f3563327 Leszek Koltunski
import android.view.DisplayCutout;
28 9530f6b0 Leszek Koltunski
import android.view.View;
29
import android.view.ViewGroup;
30
import android.view.WindowManager;
31
import android.widget.LinearLayout;
32
33
import androidx.appcompat.app.AppCompatActivity;
34
35
import com.google.firebase.analytics.FirebaseAnalytics;
36
37
import org.distorted.dialogs.RubikDialogError;
38 88b94310 Leszek Koltunski
import org.distorted.external.RubikFiles;
39 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.main.R;
41
import org.distorted.objectlib.main.ObjectControl;
42 7cb8d4b0 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
43 9530f6b0 Leszek Koltunski
44
import java.io.InputStream;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
public class BandagedPlayActivity extends AppCompatActivity
49
{
50
    private static final int ACTIVITY_NUMBER = 4;
51
    private static final float RATIO_BAR  = 0.10f;
52 f3563327 Leszek Koltunski
    private static final float RATIO_INSET= 0.09f;
53 9530f6b0 Leszek Koltunski
54
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
55
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
56
57
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
58
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
59
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
60
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
61
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
62
63
    private FirebaseAnalytics mFirebaseAnalytics;
64
    private static int mScreenWidth, mScreenHeight;
65
    private int mCurrentApiVersion;
66
    private BandagedPlayScreen mScreen;
67 88b94310 Leszek Koltunski
    private String mObjectName;
68 f3563327 Leszek Koltunski
    private int mHeightLowerBar, mHeightUpperBar;
69 9530f6b0 Leszek Koltunski
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72
    @Override
73
    protected void onCreate(Bundle savedState)
74
      {
75
      super.onCreate(savedState);
76
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
77
      setTheme(R.style.MaterialThemeNoActionBar);
78
      setContentView(R.layout.bandaged_play);
79
80
      Bundle b = getIntent().getExtras();
81 88b94310 Leszek Koltunski
      mObjectName = b!=null ? b.getString("name") : "";
82 9530f6b0 Leszek Koltunski
83
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
84
85
      DisplayMetrics displaymetrics = new DisplayMetrics();
86 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
87 9530f6b0 Leszek Koltunski
      mScreenWidth =displaymetrics.widthPixels;
88
      mScreenHeight=displaymetrics.heightPixels;
89 7fe59aa5 Leszek Koltunski
90 9530f6b0 Leszek Koltunski
      hideNavigationBar();
91
      cutoutHack();
92
      computeBarHeights();
93
      }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// this does not include possible insets
97
98
    private void computeBarHeights()
99
      {
100 7fe59aa5 Leszek Koltunski
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
101 f3563327 Leszek Koltunski
      mHeightLowerBar = barHeight;
102
      mHeightUpperBar = barHeight;
103
104
      LinearLayout layoutTop = findViewById(R.id.upperBar);
105
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
106
107
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
108
      paramsTop.height = mHeightUpperBar;
109
      layoutTop.setLayoutParams(paramsTop);
110
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
111
      paramsBot.height = mHeightLowerBar;
112
      layoutBot.setLayoutParams(paramsBot);
113 9530f6b0 Leszek Koltunski
      }
114
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117
    private void hideNavigationBar()
118
      {
119
      mCurrentApiVersion = Build.VERSION.SDK_INT;
120
121
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
122
        {
123
        final View decorView = getWindow().getDecorView();
124
125
        decorView.setSystemUiVisibility(FLAGS);
126
127
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
128
          {
129
          @Override
130
          public void onSystemUiVisibilityChange(int visibility)
131
            {
132
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
133
              {
134
              decorView.setSystemUiVisibility(FLAGS);
135
              }
136
            }
137
          });
138
        }
139
      }
140
141 f3563327 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
    @Override
144
    public void onAttachedToWindow()
145
      {
146
      super.onAttachedToWindow();
147
148
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
149
        {
150
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
151
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
152
153
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
154
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
155
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
156
        layoutHid.setLayoutParams(paramsHid);
157
        mHeightUpperBar += paramsHid.height;
158
        }
159
      }
160
161 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// do not avoid cutouts
163
164
    private void cutoutHack()
165
      {
166
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
167
        {
168
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
169
        }
170
      }
171
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
    @Override
175
    public void onWindowFocusChanged(boolean hasFocus)
176
      {
177
      super.onWindowFocusChanged(hasFocus);
178
179
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
180
        {
181
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
182
        }
183
      }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187
    @Override
188
    protected void onPause() 
189
      {
190
      super.onPause();
191
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
192
      view.onPause();
193 972f9eae Leszek Koltunski
      savePreferences();
194 9530f6b0 Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
195
      }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
    
199
    @Override
200
    protected void onResume() 
201
      {
202
      super.onResume();
203
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
204
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
205
      view.onResume();
206
207
      if( mScreen==null ) mScreen = new BandagedPlayScreen();
208 972f9eae Leszek Koltunski
      mScreen.onAttachedToWindow(this, mObjectName);
209
      restorePreferences();
210 9530f6b0 Leszek Koltunski
211 88b94310 Leszek Koltunski
      if( mObjectName.length()>0 )
212 9530f6b0 Leszek Koltunski
        {
213 88b94310 Leszek Koltunski
        changeIfDifferent(mObjectName,view.getObjectControl());
214 9530f6b0 Leszek Koltunski
        }
215
      }
216
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
    
219
    @Override
220
    protected void onDestroy() 
221
      {
222
      super.onDestroy();
223
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
224
      }
225
226 972f9eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228
  private void savePreferences()
229
    {
230
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
231
    SharedPreferences.Editor editor = preferences.edit();
232
    mScreen.savePreferences(this,editor);
233
234
    editor.apply();
235
    }
236
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239
  private void restorePreferences()
240
    {
241
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
242
    mScreen.restorePreferences(this,preferences);
243
    }
244
245 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
    void OpenGLError()
248
      {
249
      RubikDialogError errDiag = new RubikDialogError();
250
      errDiag.show(getSupportFragmentManager(), null);
251
      }
252
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
255 88b94310 Leszek Koltunski
    private void changeIfDifferent(String name,ObjectControl control)
256 9530f6b0 Leszek Koltunski
      {
257 88b94310 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
258 9530f6b0 Leszek Koltunski
259 88b94310 Leszek Koltunski
      int meshState          = TwistyObject.MESH_NICE;
260
      int iconMode           = TwistyObject.MODE_NORM;
261
      InputStream jsonStream = files.openFile(this,name+"_object.json");
262
      InputStream meshStream = null;
263
      int ordinal            = 0; // if jsonStream!=null, this doesn't matter
264
265
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
266 9530f6b0 Leszek Koltunski
      }
267
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
// PUBLIC API
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272 88b94310 Leszek Koltunski
    public void changeObject(String name)
273 9530f6b0 Leszek Koltunski
      {
274 88b94310 Leszek Koltunski
      mObjectName = name;
275 9530f6b0 Leszek Koltunski
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
276
      ObjectControl control = view.getObjectControl();
277 88b94310 Leszek Koltunski
      changeIfDifferent(name,control);
278 9530f6b0 Leszek Koltunski
      }
279
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
    public FirebaseAnalytics getAnalytics()
283
      {
284
      return mFirebaseAnalytics;
285
      }
286
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289
    public int getHeightBar()
290
      {
291 f3563327 Leszek Koltunski
      return mHeightLowerBar;
292 9530f6b0 Leszek Koltunski
      }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
    public int getScreenWidthInPixels()
297
      {
298
      return mScreenWidth;
299
      }
300
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
    public int getScreenHeightInPixels()
304
      {
305
      return mScreenHeight;
306
      }
307
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
    public ObjectControl getControl()
311
      {
312
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
313
      return view.getObjectControl();
314
      }
315
316 88b94310 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
317
318
    public BandagedPlayScreen getScreen()
319
      {
320
      return mScreen;
321
      }
322
323 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
324
325
    public static int getDrawableSize()
326
      {
327
      if( mScreenHeight<1000 )
328
        {
329
        return 0;
330
        }
331
      if( mScreenHeight<1600 )
332
        {
333
        return 1;
334
        }
335
      if( mScreenHeight<1900 )
336
        {
337
        return 2;
338
        }
339
340
      return 3;
341
      }
342
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344
345
    public static int getDrawable(int small, int medium, int big, int huge)
346
      {
347
      int size = getDrawableSize();
348
349
      switch(size)
350
        {
351
        case 0 : return small;
352
        case 1 : return medium;
353
        case 2 : return big;
354
        default: return huge;
355
        }
356
      }
357
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359
360
    public boolean isVertical()
361
      {
362
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
363
      return view.isVertical();
364
      }
365
}