Project

General

Profile

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

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

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.os.Build;
23
import android.os.Bundle;
24
import android.util.DisplayMetrics;
25
import android.view.DisplayCutout;
26
import android.view.View;
27
import android.view.ViewGroup;
28
import android.view.WindowManager;
29
import android.widget.LinearLayout;
30

    
31
import androidx.appcompat.app.AppCompatActivity;
32

    
33
import com.google.firebase.analytics.FirebaseAnalytics;
34

    
35
import org.distorted.dialogs.RubikDialogError;
36
import org.distorted.external.RubikFiles;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.main.R;
39
import org.distorted.objectlib.main.ObjectControl;
40
import org.distorted.objectlib.main.TwistyObject;
41

    
42
import java.io.InputStream;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class BandagedPlayActivity extends AppCompatActivity
47
{
48
    private static final int ACTIVITY_NUMBER = 4;
49
    private static final float RATIO_BAR  = 0.10f;
50
    private static final float RATIO_INSET= 0.09f;
51

    
52
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
53
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
54

    
55
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
56
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
57
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
58
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
59
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
60

    
61
    private FirebaseAnalytics mFirebaseAnalytics;
62
    private static int mScreenWidth, mScreenHeight;
63
    private int mCurrentApiVersion;
64
    private BandagedPlayScreen mScreen;
65
    private String mObjectName;
66
    private int mHeightLowerBar, mHeightUpperBar;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
    @Override
71
    protected void onCreate(Bundle savedState)
72
      {
73
      super.onCreate(savedState);
74
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
75
      setTheme(R.style.MaterialThemeNoActionBar);
76
      setContentView(R.layout.bandaged_play);
77

    
78
      Bundle b = getIntent().getExtras();
79
      mObjectName = b!=null ? b.getString("name") : "";
80

    
81
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
82

    
83
      DisplayMetrics displaymetrics = new DisplayMetrics();
84
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
85
      mScreenWidth =displaymetrics.widthPixels;
86
      mScreenHeight=displaymetrics.heightPixels;
87
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
88
                                                  // which is not yet hidden.
89
                                                  // TODO: figure this out exactly.
90
      hideNavigationBar();
91
      cutoutHack();
92
      computeBarHeights();
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// this does not include possible insets
97

    
98
    private void computeBarHeights()
99
      {
100
      float height = mScreenHeight;
101
      int barHeight = (int)(height*RATIO_BAR);
102
      mHeightLowerBar = barHeight;
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 = mHeightLowerBar;
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
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
193
      view.onPause();
194
      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
      mScreen.onAttachedToWindow(this);
209

    
210
      if( mObjectName.length()>0 )
211
        {
212
        changeIfDifferent(mObjectName,view.getObjectControl());
213
        }
214
      }
215

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    void OpenGLError()
228
      {
229
      RubikDialogError errDiag = new RubikDialogError();
230
      errDiag.show(getSupportFragmentManager(), null);
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
    private void changeIfDifferent(String name,ObjectControl control)
236
      {
237
      RubikFiles files = RubikFiles.getInstance();
238

    
239
      int meshState          = TwistyObject.MESH_NICE;
240
      int iconMode           = TwistyObject.MODE_NORM;
241
      InputStream jsonStream = files.openFile(this,name+"_object.json");
242
      InputStream meshStream = null;
243
      int ordinal            = 0; // if jsonStream!=null, this doesn't matter
244

    
245
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
246
      }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
// PUBLIC API
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    public void changeObject(String name)
253
      {
254
      mObjectName = name;
255
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
256
      ObjectControl control = view.getObjectControl();
257
      changeIfDifferent(name,control);
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public FirebaseAnalytics getAnalytics()
263
      {
264
      return mFirebaseAnalytics;
265
      }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    public int getHeightBar()
270
      {
271
      return mHeightLowerBar;
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    public int getScreenWidthInPixels()
277
      {
278
      return mScreenWidth;
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
    public int getScreenHeightInPixels()
284
      {
285
      return mScreenHeight;
286
      }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
    public ObjectControl getControl()
291
      {
292
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
293
      return view.getObjectControl();
294
      }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
    public BandagedPlayScreen getScreen()
299
      {
300
      return mScreen;
301
      }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
    public static int getDrawableSize()
306
      {
307
      if( mScreenHeight<1000 )
308
        {
309
        return 0;
310
        }
311
      if( mScreenHeight<1600 )
312
        {
313
        return 1;
314
        }
315
      if( mScreenHeight<1900 )
316
        {
317
        return 2;
318
        }
319

    
320
      return 3;
321
      }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
    public static int getDrawable(int small, int medium, int big, int huge)
326
      {
327
      int size = getDrawableSize();
328

    
329
      switch(size)
330
        {
331
        case 0 : return small;
332
        case 1 : return medium;
333
        case 2 : return big;
334
        default: return huge;
335
        }
336
      }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
    public boolean isVertical()
341
      {
342
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
343
      return view.isVertical();
344
      }
345
}
(9-9/13)