Project

General

Profile

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

magiccube / src / main / java / org / distorted / solverui / SolverActivity.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.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.dialogs.RubikDialogMessage;
27
import org.distorted.library.main.DistortedLibrary;
28
import org.distorted.library.main.DistortedScreen;
29
import org.distorted.main.MainActivity;
30
import org.distorted.main.R;
31
import org.distorted.objectlib.helpers.OperatingSystemInterface;
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
import org.distorted.solvers.ImplementedSolversList;
39

    
40
import java.io.InputStream;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
52
    private static final int ACTIVITY_NUMBER = 4;
53
    private static final float RATIO_INSET= 0.09f;
54

    
55
    private static int mScreenWidth, mScreenHeight;
56
    private int mCurrentApiVersion;
57
    private int mHeightUpperBar;
58
    private int mSolverOrdinal;
59
    private int mObjectOrdinal;
60
    private boolean mDisplayMessageDialog;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
73
      Bundle b = getIntent().getExtras();
74
      mObjectOrdinal = b!=null ? b.getInt("obj") : 0;
75
      mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectOrdinal);
76

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

    
82
      mDisplayMessageDialog = true;
83

    
84
      cutoutHack();
85
      computeBarHeights();
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// this does not include possible insets
90

    
91
    private void computeBarHeights()
92
      {
93
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
94
      mHeightUpperBar = barHeight;
95

    
96
      LinearLayout layoutTop = findViewById(R.id.upperBar);
97
      LinearLayout layoutBot = findViewById(R.id.lowerBar);
98

    
99
      ViewGroup.LayoutParams paramsTop = layoutTop.getLayoutParams();
100
      paramsTop.height = mHeightUpperBar;
101
      layoutTop.setLayoutParams(paramsTop);
102
      ViewGroup.LayoutParams paramsBot = layoutBot.getLayoutParams();
103
      paramsBot.height = barHeight;
104
      layoutBot.setLayoutParams(paramsBot);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
    private void hideNavigationBar()
110
      {
111
      mCurrentApiVersion = Build.VERSION.SDK_INT;
112

    
113
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
114
        {
115
        final View decorView = getWindow().getDecorView();
116

    
117
        decorView.setSystemUiVisibility(MainActivity.FLAGS);
118

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    @Override
136
    public void onAttachedToWindow()
137
      {
138
      super.onAttachedToWindow();
139

    
140
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
141
        {
142
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
143
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
144

    
145
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
146
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
147
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
148
        layoutHid.setLayoutParams(paramsHid);
149
        mHeightUpperBar += paramsHid.height;
150
        }
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// do not avoid cutouts
155

    
156
    private void cutoutHack()
157
      {
158
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
159
        {
160
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    @Override
167
    public void onWindowFocusChanged(boolean hasFocus)
168
      {
169
      super.onWindowFocusChanged(hasFocus);
170

    
171
      if( mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus )
172
        {
173
        getWindow().getDecorView().setSystemUiVisibility(MainActivity.FLAGS);
174
        }
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    @Override
180
    protected void onPause() 
181
      {
182
      super.onPause();
183
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
184
      view.onPause();
185
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
186
      savePreferences();
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
    
191
    @Override
192
    protected void onResume() 
193
      {
194
      super.onResume();
195
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
196
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
197
      view.onResume();
198

    
199
      createObject();
200

    
201
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
202
      restorePreferences(preferences);
203
      ScreenList.setScreen(this);
204
      }
205

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

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

    
227
      ScreenList.savePreferences(editor);
228

    
229
      editor.putBoolean("solverDisplayDialog", mDisplayMessageDialog );
230

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
    private void restorePreferences(SharedPreferences preferences)
238
      {
239
      for( int i=0; i<ScreenList.LENGTH; i++ )
240
        {
241
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
242
        }
243

    
244
      ScreenList.restorePreferences(preferences);
245

    
246
      mDisplayMessageDialog = preferences.getBoolean("solverDisplayDialog",true);
247

    
248
      if( mDisplayMessageDialog )
249
        {
250
        Bundle bundle = new Bundle();
251
        bundle.putString("argument", getString(R.string.solver_message) );
252
        RubikDialogMessage diag = new RubikDialogMessage();
253
        diag.setArguments(bundle);
254
        diag.show( getSupportFragmentManager(), RubikDialogMessage.getDialogTag() );
255
        }
256
      }
257

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

    
260
    void doNotShowDialogAnymore()
261
      {
262
      mDisplayMessageDialog = false;
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    void OpenGLError()
268
      {
269
      RubikDialogError errDiag = new RubikDialogError();
270
      errDiag.show(getSupportFragmentManager(), null);
271
      }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
// PUBLIC API
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    public TwistyObject getObject()
278
      {
279
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
280
      return view.getObjectControl().getObject();
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
    public DistortedScreen getScreen()
286
      {
287
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
288
      return view.getRenderer().getScreen();
289
      }
290

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

    
293
    public ObjectControl getControl()
294
      {
295
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
296
      return view.getObjectControl();
297
      }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
    public int getScreenWidthInPixels()
302
      {
303
      return mScreenWidth;
304
      }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
    public int getScreenHeightInPixels()
309
      {
310
      return mScreenHeight;
311
      }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
    public int getSolverOrdinal()
316
      {
317
      return mSolverOrdinal;
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    public int getObjectOrdinal()
323
      {
324
      return mObjectOrdinal;
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    public void createObject()
330
      {
331
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
332
      ObjectControl control = view.getObjectControl();
333
      RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
334
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
335
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
336
      String name = object==null ? "NULL" : object.getUpperName();
337
      OSInterface os = view.getInterface();
338
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
339
      control.changeIfDifferent(mObjectOrdinal,name,TwistyObject.MODE_NORM,asset);
340
      }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
    public OperatingSystemInterface getInterface()
345
      {
346
      SolverSurfaceView view  = findViewById(R.id.solverSurfaceView);
347
      return view.getInterface();
348
      }
349
}
(5-5/8)