Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.bandaged;
11

    
12
import static android.view.View.LAYOUT_DIRECTION_RTL;
13

    
14
import java.io.InputStream;
15

    
16
import android.content.Intent;
17
import android.content.SharedPreferences;
18
import android.content.res.Configuration;
19
import android.graphics.Bitmap;
20
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
import androidx.preference.PreferenceManager;
30

    
31
import org.distorted.dialogs.RubikDialogError;
32
import org.distorted.dialogs.RubikDialogMessage;
33
import org.distorted.external.RubikFiles;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.main.MainActivity;
36
import org.distorted.main.R;
37
import org.distorted.objectlib.main.InitAssets;
38
import org.distorted.objectlib.main.TwistyJson;
39
import org.distorted.objectlib.main.TwistyObject;
40
import org.distorted.os.OSInterface;
41
import org.distorted.playui.PlayActivity;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class BandagedActivity extends AppCompatActivity
46
{
47
    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
    private static final int ACTIVITY_NUMBER    = 2;
53
    private static final float RATIO_BAR        = MainActivity.RATIO_BAR;
54
    private static final float RATIO_BUT        = 0.07f;
55
    private static final int NUM_SCRAMBLES      = 300;
56

    
57
    private static int mScreenWidth, mScreenHeight;
58
    private int mCurrentApiVersion;
59
    private BandagedScreen mScreen;
60
    private boolean mRTL;
61
    private int mObjectOrdinal;
62
    private boolean mDisplayMessageDialog;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    @Override
67
    protected void onCreate(Bundle savedState)
68
      {
69
      setTheme(R.style.MaterialThemeNoActionBar);
70
      super.onCreate(savedState);
71

    
72
      Bundle b = getIntent().getExtras();
73
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
74

    
75
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
76
      setContentView(R.layout.bandaged);
77

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

    
83
      mDisplayMessageDialog = true;
84

    
85
      final Configuration config = getResources().getConfiguration();
86
      final int layoutDirection = config.getLayoutDirection();
87
      mRTL = layoutDirection==LAYOUT_DIRECTION_RTL;
88

    
89
      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
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
101
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
102
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
103
      int padding      = (int)(mScreenHeight*PADDING);
104

    
105
      LinearLayout botLayout = findViewById(R.id.lowerBar);
106
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
107
      paramsL.height = barHeight;
108
      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

    
116
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
117
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
118
      paramsT.height = viewHeight;
119
      topLayout.setPadding(padding,padding,padding,padding);
120
      topLayout.setLayoutParams(paramsT);
121

    
122
      BandagedView creator = findViewById(R.id.bandagedCreatorObjectView);
123
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
124
      paramsC.height = objectHeight;
125
      creator.setLayoutParams(paramsC);
126
      }
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
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
185
      view.onPause();
186
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
187
      savePreferences();
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
    
192
    @Override
193
    protected void onResume() 
194
      {
195
      super.onResume();
196

    
197
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
198
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
199
      view.onResume();
200

    
201
      if( mScreen==null )
202
        {
203
        int ordinal = getObjectOrdinal();
204
        mScreen = new BandagedScreen(ordinal);
205
        }
206

    
207
      mScreen.onAttachedToWindow(this);
208

    
209
      restorePreferences();
210
      BandagedWorkerThread.create(this);
211
      }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    private void savePreferences()
233
      {
234
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
235
      SharedPreferences.Editor editor = preferences.edit();
236
      mScreen.savePreferences(editor);
237

    
238
      editor.putBoolean("bandageDisplayDialog", mDisplayMessageDialog );
239

    
240
      editor.apply();
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    private void restorePreferences()
246
      {
247
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
248
      mScreen.restorePreferences(this,preferences);
249

    
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
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270
// PUBLIC API
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    public void changeObject(int x, int y, int z)
274
      {
275
      BandagedRenderer renderer = getRenderer();
276
      renderer.changeObject(x,y,z);
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    public boolean isRTL()
282
      {
283
      return mRTL;
284
      }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
    public BandagedRenderer getRenderer()
289
      {
290
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
291
      return view.getRenderer();
292
      }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
    public boolean objectDoesntExist(String name)
297
      {
298
      return mScreen.objectDoesntExist(name);
299
      }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
    public void addObject(String name)
304
      {
305
      mScreen.addObject(this,name);
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    public void deleteObject(String name)
311
      {
312
      RubikFiles files = RubikFiles.getInstance();
313
      InputStream jsonStream = files.openFile(this,name+"_object.json");
314
      InitAssets assets = new InitAssets(jsonStream,null,null);
315

    
316
      if( !assets.noJsonStream() )
317
        {
318
        TwistyObject object = new TwistyJson( TwistyObject.MODE_NORM, null, null, 1.0f, assets);
319

    
320
        if( !object.getError() )
321
          {
322
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
323
          SharedPreferences.Editor editor = preferences.edit();
324
          OSInterface os = new OSInterface(this,null);
325
          os.setEditor(editor);
326
          object.removePreferences(os);
327
          editor.apply();
328
          }
329
        }
330

    
331
      mScreen.deleteObject(this,name);
332
      files.deleteIcon(this,name);
333
      files.deleteJsonObject(this,name);
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
    public void playObject(String name)
339
      {
340
      Intent intent = new Intent(this, PlayActivity.class);
341
      intent.putExtra("level", -1);
342
      intent.putExtra("name", name);
343
      intent.putExtra("scrambles", NUM_SCRAMBLES);
344
      intent.putExtra("local", true);
345
      intent.putExtra("ordinal", 0);
346
      startActivity(intent);
347
      }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
    public void iconCreationDone(Bitmap bmp)
352
      {
353
      mScreen.iconCreationDone(this,bmp);
354
      }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
    public int getObjectOrdinal()
373
      {
374
      return mObjectOrdinal;
375
      }
376
}
(1-1/7)