Project

General

Profile

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

magiccube / src / main / java / org / distorted / solverui / SolverActivity.java @ 14da3188

1 cb30e768 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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.solverui;
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.helpers.OperatingSystemInterface;
31
import org.distorted.objectlib.main.InitAssets;
32
import org.distorted.objectlib.main.ObjectControl;
33
import org.distorted.objectlib.main.TwistyObject;
34
import org.distorted.objects.RubikObject;
35
import org.distorted.objects.RubikObjectList;
36
import org.distorted.os.OSInterface;
37
import org.distorted.solvers.ImplementedSolversList;
38
39
import java.io.InputStream;
40
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43
public class SolverActivity 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 c9f72ca3 leszek
    private static final int ACTIVITY_NUMBER = 4;
52 cb30e768 leszek
    private static final float RATIO_INSET= 0.09f;
53
54
    private static int mScreenWidth, mScreenHeight;
55
    private int mCurrentApiVersion;
56
    private int mHeightUpperBar;
57
    private int mSolverOrdinal;
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.solver);
69
      hideNavigationBar();
70
71
      Bundle b = getIntent().getExtras();
72
      mObjectOrdinal = b!=null ? b.getInt("obj") : 0;
73
      mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectOrdinal);
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
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
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
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
193
      view.onResume();
194
195
      createObject();
196
197
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
198 fb8a0bcd leszek
      restorePreferences(preferences);
199 cb30e768 leszek
      ScreenList.setScreen(this);
200
      }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
    
204
    @Override
205
    protected void onDestroy() 
206
      {
207
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
208
      super.onDestroy();
209
      }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
    private void savePreferences()
214
      {
215
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
216
      SharedPreferences.Editor editor = preferences.edit();
217
218
      for( int i=0; i< ScreenList.LENGTH; i++ )
219
        {
220
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
221
        }
222
223
      ScreenList.savePreferences(editor);
224
225
      boolean success = editor.commit();
226
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
227
      }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231 fb8a0bcd leszek
    private void restorePreferences(SharedPreferences preferences)
232 cb30e768 leszek
      {
233 fb8a0bcd leszek
      for( int i=0; i<ScreenList.LENGTH; i++ )
234 cb30e768 leszek
        {
235
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
236
        }
237
238
      ScreenList.restorePreferences(preferences);
239
      }
240
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
243
    void OpenGLError()
244
      {
245
      RubikDialogError errDiag = new RubikDialogError();
246
      errDiag.show(getSupportFragmentManager(), null);
247
      }
248
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
// PUBLIC API
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
    public TwistyObject getObject()
254
      {
255
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
256
      return view.getObjectControl().getObject();
257
      }
258
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
    public DistortedScreen getScreen()
262
      {
263
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
264
      return view.getRenderer().getScreen();
265
      }
266
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
269
    public ObjectControl getControl()
270
      {
271
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
272
      return view.getObjectControl();
273
      }
274
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277
    public int getScreenWidthInPixels()
278
      {
279
      return mScreenWidth;
280
      }
281
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284
    public int getScreenHeightInPixels()
285
      {
286
      return mScreenHeight;
287
      }
288
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291
    public int getSolverOrdinal()
292
      {
293
      return mSolverOrdinal;
294
      }
295
296 c02235d5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298
    public int getObjectOrdinal()
299
      {
300
      return mObjectOrdinal;
301
      }
302
303 cb30e768 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
304
305
    public void createObject()
306
      {
307
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
308
      ObjectControl control = view.getObjectControl();
309
      RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
310
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
311
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
312
      String name = object==null ? "NULL" : object.getUpperName();
313
      OSInterface os = view.getInterface();
314
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
315 fb8a0bcd leszek
      control.changeIfDifferent(mObjectOrdinal,name,TwistyObject.MODE_NORM,asset);
316 cb30e768 leszek
      }
317
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
320
    public OperatingSystemInterface getInterface()
321
      {
322
      SolverSurfaceView view  = findViewById(R.id.solverSurfaceView);
323
      return view.getInterface();
324
      }
325
}