Project

General

Profile

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

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

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