Project

General

Profile

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

magiccube / src / main / java / org / distorted / patternui / PatternActivity.java @ 017f24b3

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 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.patternui;
11

    
12
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
13

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

    
24
import androidx.appcompat.app.AppCompatActivity;
25
import androidx.preference.PreferenceManager;
26

    
27
import org.distorted.dialogs.RubikDialogError;
28
import org.distorted.library.main.DistortedLibrary;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.main.MainActivity;
31
import org.distorted.main.R;
32
import org.distorted.objectlib.main.InitAssets;
33
import org.distorted.objectlib.main.ObjectControl;
34
import org.distorted.objectlib.main.TwistyObject;
35
import org.distorted.objects.RubikObject;
36
import org.distorted.objects.RubikObjectList;
37
import org.distorted.os.OSInterface;
38

    
39
import java.io.InputStream;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class PatternActivity extends AppCompatActivity
44
{
45
    public static final float RATIO_BAR       = 0.100f;
46
    public static final float PADDING         = 0.010f;
47
    public static final float SMALL_MARGIN    = 0.004f;
48
    public static final float BUTTON_TEXT_SIZE= 0.050f;
49
    public static final float TITLE_TEXT_SIZE = 0.045f;
50

    
51
    private static final int ACTIVITY_NUMBER = 1;
52
    private static final float RATIO_INSET= 0.09f;
53

    
54
    private boolean mJustStarted;
55
    private static int mScreenWidth, mScreenHeight;
56
    private int mCurrentApiVersion;
57
    private int mHeightUpperBar;
58
    private int mObjectOrdinal;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    @Override
63
    protected void onCreate(Bundle savedState)
64
      {
65
      super.onCreate(savedState);
66
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
67
      setTheme(R.style.MaterialThemeNoActionBar);
68
      setContentView(R.layout.pattern);
69
      hideNavigationBar();
70

    
71
      mJustStarted = true;
72
      Bundle b = getIntent().getExtras();
73
      mObjectOrdinal = b!=null ? b.getInt("obj") : 0;
74

    
75
      DisplayMetrics displaymetrics = new DisplayMetrics();
76
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
77
      mScreenWidth =displaymetrics.widthPixels;
78
      mScreenHeight=displaymetrics.heightPixels;
79

    
80
      cutoutHack();
81
      computeBarHeights();
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
// this does not include possible insets
86

    
87
    private void computeBarHeights()
88
      {
89
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
90
      mHeightUpperBar = barHeight;
91

    
92
      LinearLayout layoutTop = findViewById(R.id.upperBar);
93
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
94

    
95
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
96
      paramsTop.height = mHeightUpperBar;
97
      layoutTop.setLayoutParams(paramsTop);
98
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
99
      paramsBot.height = barHeight;
100
      layoutBot.setLayoutParams(paramsBot);
101
      }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
    private void hideNavigationBar()
106
      {
107
      mCurrentApiVersion = Build.VERSION.SDK_INT;
108

    
109
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
110
        {
111
        final View decorView = getWindow().getDecorView();
112

    
113
        decorView.setSystemUiVisibility(MainActivity.FLAGS);
114

    
115
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
116
          {
117
          @Override
118
          public void onSystemUiVisibilityChange(int visibility)
119
            {
120
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
121
              {
122
              decorView.setSystemUiVisibility(MainActivity.FLAGS);
123
              }
124
            }
125
          });
126
        }
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    @Override
132
    public void onAttachedToWindow()
133
      {
134
      super.onAttachedToWindow();
135

    
136
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
137
        {
138
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
139
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
140

    
141
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
142
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
143
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
144
        layoutHid.setLayoutParams(paramsHid);
145
        mHeightUpperBar += paramsHid.height;
146
        }
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// do not avoid cutouts
151

    
152
    private void cutoutHack()
153
      {
154
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
155
        {
156
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
157
        }
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
    @Override
163
    public void onWindowFocusChanged(boolean hasFocus)
164
      {
165
      super.onWindowFocusChanged(hasFocus);
166

    
167
      if( mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus )
168
        {
169
        getWindow().getDecorView().setSystemUiVisibility(MainActivity.FLAGS);
170
        }
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
    
175
    @Override
176
    protected void onPause() 
177
      {
178
      super.onPause();
179
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
180
      view.onPause();
181
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
182
      savePreferences();
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187
    @Override
188
    protected void onResume() 
189
      {
190
      super.onResume();
191
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
192
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
193
      view.onResume();
194

    
195
      createObject();
196
      RubikObjectList.setCurrObject(mObjectOrdinal);
197
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
198
      restorePreferences(preferences);
199
      ScreenList.setScreen(this);
200

    
201
      if( mJustStarted )
202
        {
203
        mJustStarted = false;
204
        RubikObjectList.restoreMeshState(preferences);
205
        }
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210
    @Override
211
    protected void onDestroy() 
212
      {
213
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
214
      super.onDestroy();
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    private void savePreferences()
220
      {
221
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
222
      SharedPreferences.Editor editor = preferences.edit();
223

    
224
      for(int i=0; i< ScreenList.LENGTH; i++ )
225
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
226

    
227
      ScreenList.savePreferences(editor);
228

    
229
      boolean success = editor.commit();
230
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
231
      }
232

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

    
235
    private void restorePreferences(SharedPreferences preferences)
236
      {
237
      for (int i=0; i<ScreenList.LENGTH; i++)
238
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
239

    
240
      ScreenList.restorePreferences(preferences);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    void OpenGLError()
246
      {
247
      RubikDialogError errDiag = new RubikDialogError();
248
      errDiag.show(getSupportFragmentManager(), null);
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
// PUBLIC API
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
    public TwistyObject getObject()
256
      {
257
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
258
      return view.getObjectControl().getObject();
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
    public DistortedScreen getScreen()
264
      {
265
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
266
      return view.getRenderer().getScreen();
267
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
    public ObjectControl getControl()
272
      {
273
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
274
      return view.getObjectControl();
275
      }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
    public int getScreenWidthInPixels()
280
      {
281
      return mScreenWidth;
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    public int getScreenHeightInPixels()
287
      {
288
      return mScreenHeight;
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public void createObject()
294
      {
295
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
296
      ObjectControl control = view.getObjectControl();
297
      RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
298
      int iconMode  = TwistyObject.MODE_NORM;
299
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
300
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
301
      String name = object==null ? "NULL" : object.getUpperName();
302
      OSInterface os = view.getInterface();
303
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
304

    
305
      control.changeIfDifferent(mObjectOrdinal,name,MESH_NICE,iconMode,asset);
306
      }
307
}
(1-1/7)