Project

General

Profile

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

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

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
      super.onCreate(savedState);
63
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
64
      setTheme(R.style.MaterialThemeNoActionBar);
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
      RubikObjectList.setCurrObject(mObjectOrdinal);
193
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
194
      restorePreferences(preferences);
195
      ScreenList.setScreen(this);
196
      }
197

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

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

    
217
      ScreenList.savePreferences(editor);
218

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

    
230
      ScreenList.restorePreferences(preferences);
231
      }
232

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

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

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// PUBLIC API
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

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

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

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

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

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

    
295
      control.changeIfDifferent(mObjectOrdinal,name,iconMode,asset);
296
      }
297
}
(1-1/7)