Project

General

Profile

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

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

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 44fec653 Leszek Koltunski
// 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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.bandaged;
11
12 972f9eae Leszek Koltunski
import android.content.SharedPreferences;
13 9530f6b0 Leszek Koltunski
import android.os.Build;
14
import android.os.Bundle;
15
import android.util.DisplayMetrics;
16 f3563327 Leszek Koltunski
import android.view.DisplayCutout;
17 9530f6b0 Leszek Koltunski
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 ada8ab26 leszek
import androidx.preference.PreferenceManager;
24 9530f6b0 Leszek Koltunski
25
import org.distorted.dialogs.RubikDialogError;
26 88b94310 Leszek Koltunski
import org.distorted.external.RubikFiles;
27 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
28 1c04d054 leszek
import org.distorted.main.MainActivity;
29 9530f6b0 Leszek Koltunski
import org.distorted.main.R;
30 1c04d054 leszek
import org.distorted.main_old.RubikActivity;
31 50e6c5d6 Leszek Koltunski
import org.distorted.objectlib.main.InitAssets;
32 9530f6b0 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
33 7cb8d4b0 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
34 9530f6b0 Leszek Koltunski
35
import java.io.InputStream;
36
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39
public class BandagedPlayActivity extends AppCompatActivity
40
{
41
    private static final int ACTIVITY_NUMBER = 4;
42 1c04d054 leszek
    private static final float RATIO_BAR     = MainActivity.RATIO_BAR;
43 ada8ab26 leszek
    private static final float RATIO_INSET   = 0.09f;
44
    public static final int FLAGS            = RubikActivity.FLAGS;
45 9530f6b0 Leszek Koltunski
46
    private static int mScreenWidth, mScreenHeight;
47
    private int mCurrentApiVersion;
48
    private BandagedPlayScreen mScreen;
49 88b94310 Leszek Koltunski
    private String mObjectName;
50 aa622165 Leszek Koltunski
    private int mHeightUpperBar;
51 9530f6b0 Leszek Koltunski
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 88b94310 Leszek Koltunski
      mObjectName = b!=null ? b.getString("name") : "";
64 9530f6b0 Leszek Koltunski
65
      DisplayMetrics displaymetrics = new DisplayMetrics();
66 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
67 9530f6b0 Leszek Koltunski
      mScreenWidth =displaymetrics.widthPixels;
68
      mScreenHeight=displaymetrics.heightPixels;
69 7fe59aa5 Leszek Koltunski
70 9530f6b0 Leszek Koltunski
      hideNavigationBar();
71
      cutoutHack();
72
      computeBarHeights();
73
      }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// this does not include possible insets
77
78
    private void computeBarHeights()
79
      {
80 7fe59aa5 Leszek Koltunski
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
81 f3563327 Leszek Koltunski
      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 aa622165 Leszek Koltunski
      paramsBot.height = barHeight;
91 f3563327 Leszek Koltunski
      layoutBot.setLayoutParams(paramsBot);
92 9530f6b0 Leszek Koltunski
      }
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 f3563327 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 972f9eae Leszek Koltunski
      savePreferences();
173 9530f6b0 Leszek Koltunski
      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 972f9eae Leszek Koltunski
      mScreen.onAttachedToWindow(this, mObjectName);
188
      restorePreferences();
189 9530f6b0 Leszek Koltunski
190 88b94310 Leszek Koltunski
      if( mObjectName.length()>0 )
191 9530f6b0 Leszek Koltunski
        {
192 88b94310 Leszek Koltunski
        changeIfDifferent(mObjectName,view.getObjectControl());
193 9530f6b0 Leszek Koltunski
        }
194
      }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
    
198
    @Override
199
    protected void onDestroy() 
200
      {
201
      super.onDestroy();
202
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
203
      }
204
205 972f9eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
    void OpenGLError()
227
      {
228
      RubikDialogError errDiag = new RubikDialogError();
229
      errDiag.show(getSupportFragmentManager(), null);
230
      }
231
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
234 88b94310 Leszek Koltunski
    private void changeIfDifferent(String name,ObjectControl control)
235 9530f6b0 Leszek Koltunski
      {
236 88b94310 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
237 9530f6b0 Leszek Koltunski
238 88b94310 Leszek Koltunski
      int meshState          = TwistyObject.MESH_NICE;
239
      int iconMode           = TwistyObject.MODE_NORM;
240
      InputStream jsonStream = files.openFile(this,name+"_object.json");
241 2876aeb6 Leszek Koltunski
      InitAssets asset       = new InitAssets(jsonStream,null,null);
242 88b94310 Leszek Koltunski
      int ordinal            = 0; // if jsonStream!=null, this doesn't matter
243
244 50e6c5d6 Leszek Koltunski
      control.changeIfDifferent(ordinal,name,meshState,iconMode,asset);
245 9530f6b0 Leszek Koltunski
      }
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 88b94310 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273
    public BandagedPlayScreen getScreen()
274
      {
275
      return mScreen;
276
      }
277 9530f6b0 Leszek Koltunski
}