Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedPlayActivity.java @ 707f79ff

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
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
import android.content.SharedPreferences;
23
import android.os.Build;
24
import android.os.Bundle;
25
import android.preference.PreferenceManager;
26
import android.util.DisplayMetrics;
27
import android.view.DisplayCutout;
28
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
import org.distorted.external.RubikFiles;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.main.R;
41
import org.distorted.objectlib.main.ObjectControl;
42
import org.distorted.objectlib.main.TwistyObject;
43

    
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
    private static final float RATIO_INSET= 0.09f;
53

    
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
    private String mObjectName;
68
    private int mHeightLowerBar, mHeightUpperBar;
69

    
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
      mObjectName = b!=null ? b.getString("name") : "";
82

    
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
      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
      }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      savePreferences();
197
      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
      mScreen.onAttachedToWindow(this, mObjectName);
212
      restorePreferences();
213

    
214
      if( mObjectName.length()>0 )
215
        {
216
        changeIfDifferent(mObjectName,view.getObjectControl());
217
        }
218
      }
219

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

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
    private void changeIfDifferent(String name,ObjectControl control)
259
      {
260
      RubikFiles files = RubikFiles.getInstance();
261

    
262
      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
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
// PUBLIC API
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
    public void changeObject(String name)
276
      {
277
      mObjectName = name;
278
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
279
      ObjectControl control = view.getObjectControl();
280
      changeIfDifferent(name,control);
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
    public FirebaseAnalytics getAnalytics()
286
      {
287
      return mFirebaseAnalytics;
288
      }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
    public int getHeightBar()
293
      {
294
      return mHeightLowerBar;
295
      }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
    public BandagedPlayScreen getScreen()
322
      {
323
      return mScreen;
324
      }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
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
}
(9-9/13)