Project

General

Profile

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

magiccube / src / main / java / org / distorted / patternui / PatternActivity.java @ e9245b7b

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.060f;
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

    
198
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
199
      restorePreferences(preferences,mJustStarted);
200
      ScreenList.setScreen(this);
201

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

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

    
225
      for(int i = 0; i< ScreenList.LENGTH; i++ )
226
        {
227
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
228
        }
229

    
230
      RubikObjectList.savePreferences(editor);
231
      RubikObjectList.saveMeshState(editor);
232
      ScreenList.savePreferences(editor);
233
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
234
      OSInterface os = view.getInterface();
235
      os.setEditor(editor);
236
      view.getObjectControl().savePreferences();
237

    
238
      boolean success = editor.commit();
239
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    private void restorePreferences(SharedPreferences preferences, boolean justStarted)
245
      {
246
      RubikObjectList.restorePreferences(this,preferences,justStarted);
247

    
248
      for (int i = 0; i<ScreenList.LENGTH; i++)
249
        {
250
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
251
        }
252

    
253
      ScreenList.restorePreferences(preferences);
254

    
255
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
256
      OSInterface os = view.getInterface();
257
      os.setPreferences(preferences);
258
      view.getObjectControl().restorePreferences();
259
      }
260

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

    
263
    void OpenGLError()
264
      {
265
      RubikDialogError errDiag = new RubikDialogError();
266
      errDiag.show(getSupportFragmentManager(), null);
267
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270
// PUBLIC API
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    public TwistyObject getObject()
274
      {
275
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
276
      return view.getObjectControl().getObject();
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    public DistortedScreen getScreen()
282
      {
283
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
284
      return view.getRenderer().getScreen();
285
      }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
    public ObjectControl getControl()
290
      {
291
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
292
      return view.getObjectControl();
293
      }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
    public int getScreenWidthInPixels()
298
      {
299
      return mScreenWidth;
300
      }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
    public int getScreenHeightInPixels()
305
      {
306
      return mScreenHeight;
307
      }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
    public void createObject()
312
      {
313
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
314
      ObjectControl control = view.getObjectControl();
315
      RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
316
      int iconMode  = TwistyObject.MODE_NORM;
317
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
318
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
319
      String name = object==null ? "NULL" : object.getUpperName();
320
      OSInterface os = view.getInterface();
321
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
322

    
323
      control.changeIfDifferent(mObjectOrdinal,name,MESH_NICE,iconMode,asset);
324
      }
325
}
(1-1/7)