Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedPlayActivity.java @ 1c04d054

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.bandaged;
11

    
12
import android.content.SharedPreferences;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.util.DisplayMetrics;
16
import android.view.DisplayCutout;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.view.WindowManager;
20
import android.widget.LinearLayout;
21

    
22
import androidx.appcompat.app.AppCompatActivity;
23
import androidx.preference.PreferenceManager;
24

    
25
import org.distorted.dialogs.RubikDialogError;
26
import org.distorted.external.RubikFiles;
27
import org.distorted.library.main.DistortedLibrary;
28
import org.distorted.main.MainActivity;
29
import org.distorted.main.R;
30
import org.distorted.main_old.RubikActivity;
31
import org.distorted.objectlib.main.InitAssets;
32
import org.distorted.objectlib.main.ObjectControl;
33
import org.distorted.objectlib.main.TwistyObject;
34

    
35
import java.io.InputStream;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class BandagedPlayActivity extends AppCompatActivity
40
{
41
    private static final int ACTIVITY_NUMBER = 4;
42
    private static final float RATIO_BAR     = MainActivity.RATIO_BAR;
43
    private static final float RATIO_INSET   = 0.09f;
44
    public static final int FLAGS            = RubikActivity.FLAGS;
45

    
46
    private static int mScreenWidth, mScreenHeight;
47
    private int mCurrentApiVersion;
48
    private BandagedPlayScreen mScreen;
49
    private String mObjectName;
50
    private int mHeightUpperBar;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
    @Override
55
    protected void onCreate(Bundle savedState)
56
      {
57
      super.onCreate(savedState);
58
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
59
      setTheme(R.style.MaterialThemeNoActionBar);
60
      setContentView(R.layout.bandaged_play);
61

    
62
      Bundle b = getIntent().getExtras();
63
      mObjectName = b!=null ? b.getString("name") : "";
64

    
65
      DisplayMetrics displaymetrics = new DisplayMetrics();
66
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
67
      mScreenWidth =displaymetrics.widthPixels;
68
      mScreenHeight=displaymetrics.heightPixels;
69

    
70
      hideNavigationBar();
71
      cutoutHack();
72
      computeBarHeights();
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// this does not include possible insets
77

    
78
    private void computeBarHeights()
79
      {
80
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
81
      mHeightUpperBar = barHeight;
82

    
83
      LinearLayout layoutTop = findViewById(R.id.upperBar);
84
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
85

    
86
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
87
      paramsTop.height = mHeightUpperBar;
88
      layoutTop.setLayoutParams(paramsTop);
89
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
90
      paramsBot.height = barHeight;
91
      layoutBot.setLayoutParams(paramsBot);
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
    private void hideNavigationBar()
97
      {
98
      mCurrentApiVersion = Build.VERSION.SDK_INT;
99

    
100
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
101
        {
102
        final View decorView = getWindow().getDecorView();
103

    
104
        decorView.setSystemUiVisibility(FLAGS);
105

    
106
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
107
          {
108
          @Override
109
          public void onSystemUiVisibilityChange(int visibility)
110
            {
111
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
112
              {
113
              decorView.setSystemUiVisibility(FLAGS);
114
              }
115
            }
116
          });
117
        }
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    @Override
123
    public void onAttachedToWindow()
124
      {
125
      super.onAttachedToWindow();
126

    
127
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
128
        {
129
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
130
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
131

    
132
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
133
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
134
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
135
        layoutHid.setLayoutParams(paramsHid);
136
        mHeightUpperBar += paramsHid.height;
137
        }
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// do not avoid cutouts
142

    
143
    private void cutoutHack()
144
      {
145
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
146
        {
147
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
148
        }
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    @Override
154
    public void onWindowFocusChanged(boolean hasFocus)
155
      {
156
      super.onWindowFocusChanged(hasFocus);
157

    
158
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
159
        {
160
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
    
166
    @Override
167
    protected void onPause() 
168
      {
169
      super.onPause();
170
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
171
      view.onPause();
172
      savePreferences();
173
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
174
      }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178
    @Override
179
    protected void onResume() 
180
      {
181
      super.onResume();
182
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
183
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
184
      view.onResume();
185

    
186
      if( mScreen==null ) mScreen = new BandagedPlayScreen();
187
      mScreen.onAttachedToWindow(this, mObjectName);
188
      restorePreferences();
189

    
190
      if( mObjectName.length()>0 )
191
        {
192
        changeIfDifferent(mObjectName,view.getObjectControl());
193
        }
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
    
198
    @Override
199
    protected void onDestroy() 
200
      {
201
      super.onDestroy();
202
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private void savePreferences()
208
    {
209
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
210
    SharedPreferences.Editor editor = preferences.edit();
211
    mScreen.savePreferences(this,editor);
212

    
213
    editor.apply();
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private void restorePreferences()
219
    {
220
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
221
    mScreen.restorePreferences(this,preferences);
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

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

    
244
      control.changeIfDifferent(ordinal,name,meshState,iconMode,asset);
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
// PUBLIC API
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
    public int getScreenWidthInPixels()
252
      {
253
      return mScreenWidth;
254
      }
255

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

    
258
    public int getScreenHeightInPixels()
259
      {
260
      return mScreenHeight;
261
      }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
    public ObjectControl getControl()
266
      {
267
      BandagedPlayView view = findViewById(R.id.bandagedPlayView);
268
      return view.getObjectControl();
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    public BandagedPlayScreen getScreen()
274
      {
275
      return mScreen;
276
      }
277
}
(8-8/12)