Project

General

Profile

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

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

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
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
87
      mScreenWidth =displaymetrics.widthPixels;
88
      mScreenHeight=displaymetrics.heightPixels;
89
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
90
                                                  // which is not yet hidden.
91
                                                  // TODO: figure this out exactly.
92
      hideNavigationBar();
93
      cutoutHack();
94
      computeBarHeights();
95
      }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// this does not include possible insets
99
100
    private void computeBarHeights()
101
      {
102 f3563327 Leszek Koltunski
      float height = mScreenHeight;
103
      int barHeight = (int)(height*RATIO_BAR);
104
      mHeightLowerBar = barHeight;
105
      mHeightUpperBar = barHeight;
106
107
      LinearLayout layoutTop = findViewById(R.id.upperBar);
108
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
109
110
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
111
      paramsTop.height = mHeightUpperBar;
112
      layoutTop.setLayoutParams(paramsTop);
113
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
114
      paramsBot.height = mHeightLowerBar;
115
      layoutBot.setLayoutParams(paramsBot);
116 9530f6b0 Leszek Koltunski
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
    private void hideNavigationBar()
121
      {
122
      mCurrentApiVersion = Build.VERSION.SDK_INT;
123
124
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
125
        {
126
        final View decorView = getWindow().getDecorView();
127
128
        decorView.setSystemUiVisibility(FLAGS);
129
130
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
131
          {
132
          @Override
133
          public void onSystemUiVisibilityChange(int visibility)
134
            {
135
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
136
              {
137
              decorView.setSystemUiVisibility(FLAGS);
138
              }
139
            }
140
          });
141
        }
142
      }
143
144 f3563327 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
    @Override
147
    public void onAttachedToWindow()
148
      {
149
      super.onAttachedToWindow();
150
151
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
152
        {
153
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
154
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
155
156
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
157
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
158
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
159
        layoutHid.setLayoutParams(paramsHid);
160
        mHeightUpperBar += paramsHid.height;
161
        }
162
      }
163
164 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// do not avoid cutouts
166
167
    private void cutoutHack()
168
      {
169
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
170
        {
171
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
172
        }
173
      }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
    @Override
178
    public void onWindowFocusChanged(boolean hasFocus)
179
      {
180
      super.onWindowFocusChanged(hasFocus);
181
182
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
183
        {
184
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
185
        }
186
      }
187
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
    
190
    @Override
191
    protected void onPause() 
192
      {
193
      super.onPause();
194
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
195
      view.onPause();
196 972f9eae Leszek Koltunski
      savePreferences();
197 9530f6b0 Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
198
      }
199
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
    
202
    @Override
203
    protected void onResume() 
204
      {
205
      super.onResume();
206
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
207
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
208
      view.onResume();
209
210
      if( mScreen==null ) mScreen = new BandagedPlayScreen();
211 972f9eae Leszek Koltunski
      mScreen.onAttachedToWindow(this, mObjectName);
212
      restorePreferences();
213 9530f6b0 Leszek Koltunski
214 88b94310 Leszek Koltunski
      if( mObjectName.length()>0 )
215 9530f6b0 Leszek Koltunski
        {
216 88b94310 Leszek Koltunski
        changeIfDifferent(mObjectName,view.getObjectControl());
217 9530f6b0 Leszek Koltunski
        }
218
      }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
    
222
    @Override
223
    protected void onDestroy() 
224
      {
225
      super.onDestroy();
226
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
227
      }
228
229 972f9eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  private void savePreferences()
232
    {
233
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
234
    SharedPreferences.Editor editor = preferences.edit();
235
    mScreen.savePreferences(this,editor);
236
237
    editor.apply();
238
    }
239
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
242
  private void restorePreferences()
243
    {
244
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
245
    mScreen.restorePreferences(this,preferences);
246
    }
247
248 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250
    void OpenGLError()
251
      {
252
      RubikDialogError errDiag = new RubikDialogError();
253
      errDiag.show(getSupportFragmentManager(), null);
254
      }
255
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
258 88b94310 Leszek Koltunski
    private void changeIfDifferent(String name,ObjectControl control)
259 9530f6b0 Leszek Koltunski
      {
260 88b94310 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
261 9530f6b0 Leszek Koltunski
262 88b94310 Leszek Koltunski
      int meshState          = TwistyObject.MESH_NICE;
263
      int iconMode           = TwistyObject.MODE_NORM;
264
      InputStream jsonStream = files.openFile(this,name+"_object.json");
265
      InputStream meshStream = null;
266
      int ordinal            = 0; // if jsonStream!=null, this doesn't matter
267
268
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
269 9530f6b0 Leszek Koltunski
      }
270
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
// PUBLIC API
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275 88b94310 Leszek Koltunski
    public void changeObject(String name)
276 9530f6b0 Leszek Koltunski
      {
277 88b94310 Leszek Koltunski
      mObjectName = name;
278 9530f6b0 Leszek Koltunski
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
279
      ObjectControl control = view.getObjectControl();
280 88b94310 Leszek Koltunski
      changeIfDifferent(name,control);
281 9530f6b0 Leszek Koltunski
      }
282
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284
285
    public FirebaseAnalytics getAnalytics()
286
      {
287
      return mFirebaseAnalytics;
288
      }
289
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
292
    public int getHeightBar()
293
      {
294 f3563327 Leszek Koltunski
      return mHeightLowerBar;
295 9530f6b0 Leszek Koltunski
      }
296
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
299
    public int getScreenWidthInPixels()
300
      {
301
      return mScreenWidth;
302
      }
303
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306
    public int getScreenHeightInPixels()
307
      {
308
      return mScreenHeight;
309
      }
310
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313
    public ObjectControl getControl()
314
      {
315
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
316
      return view.getObjectControl();
317
      }
318
319 88b94310 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
    public BandagedPlayScreen getScreen()
322
      {
323
      return mScreen;
324
      }
325
326 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
327
328
    public static int getDrawableSize()
329
      {
330
      if( mScreenHeight<1000 )
331
        {
332
        return 0;
333
        }
334
      if( mScreenHeight<1600 )
335
        {
336
        return 1;
337
        }
338
      if( mScreenHeight<1900 )
339
        {
340
        return 2;
341
        }
342
343
      return 3;
344
      }
345
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347
348
    public static int getDrawable(int small, int medium, int big, int huge)
349
      {
350
      int size = getDrawableSize();
351
352
      switch(size)
353
        {
354
        case 0 : return small;
355
        case 1 : return medium;
356
        case 2 : return big;
357
        default: return huge;
358
        }
359
      }
360
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362
363
    public boolean isVertical()
364
      {
365
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
366
      return view.isVertical();
367
      }
368
}