Project

General

Profile

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

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

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 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.library.main.DistortedLibrary;
27
import org.distorted.library.main.DistortedScreen;
28
import org.distorted.main.MainActivity;
29
import org.distorted.main.R;
30
import org.distorted.objectlib.main.InitAssets;
31
import org.distorted.objectlib.main.ObjectControl;
32
import org.distorted.objectlib.main.TwistyObject;
33
import org.distorted.objects.RubikObject;
34
import org.distorted.objects.RubikObjectList;
35
import org.distorted.os.OSInterface;
36

    
37
import java.io.InputStream;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
49
    private static final int ACTIVITY_NUMBER = 5;
50
    private static final float RATIO_INSET= 0.09f;
51

    
52
    private static int mScreenWidth, mScreenHeight;
53
    private int mCurrentApiVersion;
54
    private int mHeightUpperBar;
55
    private int mObjectOrdinal;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

    
68
      Bundle b = getIntent().getExtras();
69
      mObjectOrdinal = b!=null ? b.getInt("obj") : 0;
70

    
71
      DisplayMetrics displaymetrics = new DisplayMetrics();
72
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
73
      mScreenWidth =displaymetrics.widthPixels;
74
      mScreenHeight=displaymetrics.heightPixels;
75

    
76
      cutoutHack();
77
      computeBarHeights();
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// this does not include possible insets
82

    
83
    private void computeBarHeights()
84
      {
85
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
86
      mHeightUpperBar = barHeight;
87

    
88
      LinearLayout layoutTop = findViewById(R.id.upperBar);
89
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
90

    
91
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
92
      paramsTop.height = mHeightUpperBar;
93
      layoutTop.setLayoutParams(paramsTop);
94
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
95
      paramsBot.height = barHeight;
96
      layoutBot.setLayoutParams(paramsBot);
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    private void hideNavigationBar()
102
      {
103
      mCurrentApiVersion = Build.VERSION.SDK_INT;
104

    
105
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
106
        {
107
        final View decorView = getWindow().getDecorView();
108

    
109
        decorView.setSystemUiVisibility(MainActivity.FLAGS);
110

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    @Override
128
    public void onAttachedToWindow()
129
      {
130
      super.onAttachedToWindow();
131

    
132
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
133
        {
134
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
135
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
136

    
137
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
138
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
139
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
140
        layoutHid.setLayoutParams(paramsHid);
141
        mHeightUpperBar += paramsHid.height;
142
        }
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// do not avoid cutouts
147

    
148
    private void cutoutHack()
149
      {
150
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
151
        {
152
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
153
        }
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    @Override
159
    public void onWindowFocusChanged(boolean hasFocus)
160
      {
161
      super.onWindowFocusChanged(hasFocus);
162

    
163
      if( mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus )
164
        {
165
        getWindow().getDecorView().setSystemUiVisibility(MainActivity.FLAGS);
166
        }
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
    
171
    @Override
172
    protected void onPause() 
173
      {
174
      super.onPause();
175
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
176
      view.onPause();
177
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
178
      savePreferences();
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
    
183
    @Override
184
    protected void onResume() 
185
      {
186
      super.onResume();
187
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
188
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
189
      view.onResume();
190

    
191
      createObject();
192
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
193
      restorePreferences(preferences);
194
      ScreenList.setScreen(this);
195
      }
196

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
213
      for(int i=0; i< ScreenList.LENGTH; i++ )
214
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
215

    
216
      ScreenList.savePreferences(editor);
217

    
218
      boolean success = editor.commit();
219
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    private void restorePreferences(SharedPreferences preferences)
225
      {
226
      for (int i=0; i<ScreenList.LENGTH; i++)
227
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
228

    
229
      ScreenList.restorePreferences(preferences);
230
      }
231

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

    
234
    void OpenGLError()
235
      {
236
      RubikDialogError errDiag = new RubikDialogError();
237
      errDiag.show(getSupportFragmentManager(), null);
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// PUBLIC API
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    public TwistyObject getObject()
245
      {
246
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
247
      return view.getObjectControl().getObject();
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    public DistortedScreen getScreen()
253
      {
254
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
255
      return view.getRenderer().getScreen();
256
      }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
    public ObjectControl getControl()
261
      {
262
      PatternSurfaceView view = findViewById(R.id.patternSurfaceView);
263
      return view.getObjectControl();
264
      }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    public int getScreenWidthInPixels()
269
      {
270
      return mScreenWidth;
271
      }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
    public int getScreenHeightInPixels()
276
      {
277
      return mScreenHeight;
278
      }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
    public int getObjectOrdinal()
283
      {
284
      return mObjectOrdinal;
285
      }
286

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

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

    
301
      control.changeIfDifferent(mObjectOrdinal,name,iconMode,asset);
302
      }
303
}
(1-1/7)