Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedActivity.java @ c9f72ca3

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 44fec653 Leszek Koltunski
// 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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.bandaged;
11
12 2ceeb6b5 Leszek Koltunski
import static android.view.View.LAYOUT_DIRECTION_RTL;
13
14 707f79ff Leszek Koltunski
import java.io.InputStream;
15
16 88b94310 Leszek Koltunski
import android.content.Intent;
17 a41e3c94 Leszek Koltunski
import android.content.SharedPreferences;
18 2ceeb6b5 Leszek Koltunski
import android.content.res.Configuration;
19 7cb8d4b0 Leszek Koltunski
import android.graphics.Bitmap;
20 9530f6b0 Leszek Koltunski
import android.os.Build;
21
import android.os.Bundle;
22
import android.util.DisplayMetrics;
23
import android.view.View;
24
import android.view.ViewGroup;
25
import android.view.WindowManager;
26
import android.widget.LinearLayout;
27
28
import androidx.appcompat.app.AppCompatActivity;
29 ada8ab26 leszek
import androidx.preference.PreferenceManager;
30 9530f6b0 Leszek Koltunski
31
import org.distorted.dialogs.RubikDialogError;
32 81493402 Leszek Koltunski
import org.distorted.external.RubikFiles;
33 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
34 1c04d054 leszek
import org.distorted.main.MainActivity;
35 9530f6b0 Leszek Koltunski
import org.distorted.main.R;
36 9234018b Leszek Koltunski
import org.distorted.objectlib.main.InitAssets;
37 707f79ff Leszek Koltunski
import org.distorted.objectlib.main.TwistyJson;
38
import org.distorted.objectlib.main.TwistyObject;
39 21a1bb5d Leszek Koltunski
import org.distorted.os.OSInterface;
40 7ee8337b leszek
import org.distorted.playui.PlayActivity;
41 9530f6b0 Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 7ee8337b leszek
public class BandagedActivity extends AppCompatActivity
45 9530f6b0 Leszek Koltunski
{
46 c9f72ca3 leszek
    private static final int ACTIVITY_NUMBER    = 2;
47 1c04d054 leszek
    private static final float RATIO_BAR        = MainActivity.RATIO_BAR;
48 ada8ab26 leszek
    private static final float RATIO_BUT        = 0.07f;
49
    static final float RATIO_SCROLL             = 0.30f;
50
    public static final float SPINNER_TEXT_SIZE = 0.03f;
51 7ee8337b leszek
    private static final float PADDING          = 0.010f;
52
    public static final int FLAGS               = MainActivity.FLAGS;
53
    private static final int NUM_SCRAMBLES      = 300;
54 9530f6b0 Leszek Koltunski
55
    private static int mScreenWidth, mScreenHeight;
56
    private int mCurrentApiVersion;
57 7ee8337b leszek
    private BandagedScreen mScreen;
58 2ceeb6b5 Leszek Koltunski
    private boolean mRTL;
59 5a4ee169 leszek
    private int mObjectOrdinal;
60 9530f6b0 Leszek Koltunski
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63
    @Override
64
    protected void onCreate(Bundle savedState)
65
      {
66
      super.onCreate(savedState);
67 5597ccee leszek
68
      Bundle b = getIntent().getExtras();
69
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
70
71 9530f6b0 Leszek Koltunski
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
72
      setTheme(R.style.MaterialThemeNoActionBar);
73
      setContentView(R.layout.bandaged);
74
75
      DisplayMetrics displaymetrics = new DisplayMetrics();
76 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
77 9530f6b0 Leszek Koltunski
      mScreenWidth =displaymetrics.widthPixels;
78
      mScreenHeight=displaymetrics.heightPixels;
79 7fe59aa5 Leszek Koltunski
80 2ceeb6b5 Leszek Koltunski
      final Configuration config = getResources().getConfiguration();
81
      final int layoutDirection = config.getLayoutDirection();
82
      mRTL = layoutDirection==LAYOUT_DIRECTION_RTL;
83
84 9530f6b0 Leszek Koltunski
      hideNavigationBar();
85
      cutoutHack();
86
      computeHeights();
87
      }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// this does not include possible insets
91
92
    private void computeHeights()
93
      {
94
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
95 b9d062cf Leszek Koltunski
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
96 48314d6a Leszek Koltunski
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
97 bc7e49ec Leszek Koltunski
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
98 7ee8337b leszek
      int padding      = (int)(mScreenHeight*PADDING);
99 9530f6b0 Leszek Koltunski
100 b9d062cf Leszek Koltunski
      LinearLayout botLayout = findViewById(R.id.lowerBar);
101
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
102 9530f6b0 Leszek Koltunski
      paramsL.height = barHeight;
103 b9d062cf Leszek Koltunski
      botLayout.setLayoutParams(paramsL);
104
105
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
106
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
107
      paramsB.height = butHeight;
108
      butLayout.setPadding(padding,0,padding,padding);
109
      butLayout.setLayoutParams(paramsB);
110 9530f6b0 Leszek Koltunski
111 9f8d4c92 Leszek Koltunski
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
112
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
113
      paramsT.height = viewHeight;
114 b9d062cf Leszek Koltunski
      topLayout.setPadding(padding,padding,padding,padding);
115 9f8d4c92 Leszek Koltunski
      topLayout.setLayoutParams(paramsT);
116 48314d6a Leszek Koltunski
117 7ee8337b leszek
      BandagedView creator = findViewById(R.id.bandagedCreatorObjectView);
118 48314d6a Leszek Koltunski
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
119
      paramsC.height = objectHeight;
120
      creator.setLayoutParams(paramsC);
121 9530f6b0 Leszek Koltunski
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
    private void hideNavigationBar()
126
      {
127
      mCurrentApiVersion = Build.VERSION.SDK_INT;
128
129
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
130
        {
131
        final View decorView = getWindow().getDecorView();
132
133
        decorView.setSystemUiVisibility(FLAGS);
134
135
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
136
          {
137
          @Override
138
          public void onSystemUiVisibilityChange(int visibility)
139
            {
140
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
141
              {
142
              decorView.setSystemUiVisibility(FLAGS);
143
              }
144
            }
145
          });
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(FLAGS);
170
        }
171
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
    
175
    @Override
176
    protected void onPause() 
177
      {
178
      super.onPause();
179 7ee8337b leszek
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
180 9530f6b0 Leszek Koltunski
      view.onPause();
181
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
182 13a3dfa9 Leszek Koltunski
      savePreferences();
183 9530f6b0 Leszek Koltunski
      }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187
    @Override
188
    protected void onResume() 
189
      {
190
      super.onResume();
191 b3786da4 leszek
192 9530f6b0 Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
193 7ee8337b leszek
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
194 9530f6b0 Leszek Koltunski
      view.onResume();
195
196 7da27d2b leszek
      if( mScreen==null )
197
        {
198
        int ordinal = getObjectOrdinal();
199 7ee8337b leszek
        mScreen = new BandagedScreen(ordinal);
200 7da27d2b leszek
        }
201
202 bebd7af5 Leszek Koltunski
      mScreen.onAttachedToWindow(this);
203 7cb8d4b0 Leszek Koltunski
204 a41e3c94 Leszek Koltunski
      restorePreferences();
205 7ee8337b leszek
      BandagedWorkerThread.create(this);
206 9530f6b0 Leszek Koltunski
      }
207
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210
    @Override
211
    protected void onDestroy() 
212
      {
213
      super.onDestroy();
214
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
215
      }
216
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
    void OpenGLError()
220
      {
221
      RubikDialogError errDiag = new RubikDialogError();
222
      errDiag.show(getSupportFragmentManager(), null);
223
      }
224
225 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
    private void savePreferences()
228
      {
229
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
230
      SharedPreferences.Editor editor = preferences.edit();
231 b9d062cf Leszek Koltunski
      mScreen.savePreferences(editor);
232 13a3dfa9 Leszek Koltunski
      editor.apply();
233
      }
234
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237
    private void restorePreferences()
238
      {
239
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
240 b9d062cf Leszek Koltunski
      mScreen.restorePreferences(this,preferences);
241 13a3dfa9 Leszek Koltunski
      }
242
243 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
    public void changeObject(int x, int y, int z)
248
      {
249 7ee8337b leszek
      BandagedRenderer renderer = getRenderer();
250 b9d062cf Leszek Koltunski
      renderer.changeObject(x,y,z);
251
      }
252
253 2ceeb6b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
254
255
    public boolean isRTL()
256
      {
257
      return mRTL;
258
      }
259
260 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
261
262 7ee8337b leszek
    public BandagedRenderer getRenderer()
263 50ec342b Leszek Koltunski
      {
264 7ee8337b leszek
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
265 50ec342b Leszek Koltunski
      return view.getRenderer();
266
      }
267
268 bfb59352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
    public boolean objectDoesntExist(String name)
271
      {
272
      return mScreen.objectDoesntExist(name);
273
      }
274
275 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277
    public void addObject(String name)
278
      {
279 bfb59352 Leszek Koltunski
      mScreen.addObject(this,name);
280 e48ad1af Leszek Koltunski
      }
281
282 d3d639b1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284 85770de9 Leszek Koltunski
    public void deleteObject(String name)
285 d3d639b1 Leszek Koltunski
      {
286 81493402 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
287 707f79ff Leszek Koltunski
      InputStream jsonStream = files.openFile(this,name+"_object.json");
288 2876aeb6 Leszek Koltunski
      InitAssets assets = new InitAssets(jsonStream,null,null);
289 707f79ff Leszek Koltunski
290 9234018b Leszek Koltunski
      if( !assets.noJsonStream() )
291 213c15de Leszek Koltunski
        {
292 337f4660 leszek
        TwistyObject object = new TwistyJson( TwistyObject.MODE_NORM, null, null, 1.0f, assets);
293 a8be1a23 Leszek Koltunski
294
        if( !object.getError() )
295
          {
296
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
297
          SharedPreferences.Editor editor = preferences.edit();
298 88451205 Leszek Koltunski
          OSInterface os = new OSInterface(this,null);
299 05e4c778 Leszek Koltunski
          os.setEditor(editor);
300 21a1bb5d Leszek Koltunski
          object.removePreferences(os);
301 a8be1a23 Leszek Koltunski
          editor.apply();
302
          }
303 213c15de Leszek Koltunski
        }
304 707f79ff Leszek Koltunski
305
      mScreen.deleteObject(this,name);
306
      files.deleteIcon(this,name);
307
      files.deleteJsonObject(this,name);
308 d3d639b1 Leszek Koltunski
      }
309
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
312
    public void playObject(String name)
313
      {
314 7ee8337b leszek
      Intent intent = new Intent(this, PlayActivity.class);
315 88b94310 Leszek Koltunski
      intent.putExtra("name", name);
316 7ee8337b leszek
      intent.putExtra("scrambles", NUM_SCRAMBLES);
317
      intent.putExtra("local", true);
318
      intent.putExtra("ordinal", 0);
319
      intent.putExtra("free", true);
320 88b94310 Leszek Koltunski
      startActivity(intent);
321 d3d639b1 Leszek Koltunski
      }
322
323 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
324
325 7cb8d4b0 Leszek Koltunski
    public void iconCreationDone(Bitmap bmp)
326 9530f6b0 Leszek Koltunski
      {
327 20b60ad9 Leszek Koltunski
      mScreen.iconCreationDone(this,bmp);
328 9530f6b0 Leszek Koltunski
      }
329
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
332
    public int getScreenWidthInPixels()
333
      {
334
      return mScreenWidth;
335
      }
336
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338
339
    public int getScreenHeightInPixels()
340
      {
341
      return mScreenHeight;
342
      }
343
344 7da27d2b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
345
346
    public int getObjectOrdinal()
347
      {
348 5a4ee169 leszek
      return mObjectOrdinal;
349 7da27d2b leszek
      }
350 9530f6b0 Leszek Koltunski
}