Project

General

Profile

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

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

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.external.RubikFiles;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.main.MainActivity;
35
import org.distorted.main.R;
36
import org.distorted.objectlib.main.InitAssets;
37
import org.distorted.objectlib.main.TwistyJson;
38
import org.distorted.objectlib.main.TwistyObject;
39
import org.distorted.os.OSInterface;
40
import org.distorted.playui.PlayActivity;
41

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

    
44
public class BandagedActivity extends AppCompatActivity
45
{
46
    public static final float SPINNER_TEXT_SIZE = 0.03f;
47
    public static final float PADDING           = 0.010f;
48
    public static final int FLAGS               = MainActivity.FLAGS;
49
    public static final float RATIO_SCROLL      = 0.30f;
50

    
51
    private static final int ACTIVITY_NUMBER    = 2;
52
    private static final float RATIO_BAR        = MainActivity.RATIO_BAR;
53
    private static final float RATIO_BUT        = 0.07f;
54
    private static final int NUM_SCRAMBLES      = 300;
55

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

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

    
64
    @Override
65
    protected void onCreate(Bundle savedState)
66
      {
67
      super.onCreate(savedState);
68

    
69
      Bundle b = getIntent().getExtras();
70
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
71

    
72
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
73
      setTheme(R.style.MaterialThemeNoActionBar);
74
      setContentView(R.layout.bandaged);
75

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

    
81
      final Configuration config = getResources().getConfiguration();
82
      final int layoutDirection = config.getLayoutDirection();
83
      mRTL = layoutDirection==LAYOUT_DIRECTION_RTL;
84

    
85
      hideNavigationBar();
86
      cutoutHack();
87
      computeHeights();
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
// this does not include possible insets
92

    
93
    private void computeHeights()
94
      {
95
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
96
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
97
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
98
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
99
      int padding      = (int)(mScreenHeight*PADDING);
100

    
101
      LinearLayout botLayout = findViewById(R.id.lowerBar);
102
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
103
      paramsL.height = barHeight;
104
      botLayout.setLayoutParams(paramsL);
105

    
106
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
107
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
108
      paramsB.height = butHeight;
109
      butLayout.setPadding(padding,0,padding,padding);
110
      butLayout.setLayoutParams(paramsB);
111

    
112
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
113
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
114
      paramsT.height = viewHeight;
115
      topLayout.setPadding(padding,padding,padding,padding);
116
      topLayout.setLayoutParams(paramsT);
117

    
118
      BandagedView creator = findViewById(R.id.bandagedCreatorObjectView);
119
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
120
      paramsC.height = objectHeight;
121
      creator.setLayoutParams(paramsC);
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    private void hideNavigationBar()
127
      {
128
      mCurrentApiVersion = Build.VERSION.SDK_INT;
129

    
130
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
131
        {
132
        final View decorView = getWindow().getDecorView();
133

    
134
        decorView.setSystemUiVisibility(FLAGS);
135

    
136
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
137
          {
138
          @Override
139
          public void onSystemUiVisibilityChange(int visibility)
140
            {
141
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
142
              {
143
              decorView.setSystemUiVisibility(FLAGS);
144
              }
145
            }
146
          });
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// do not avoid cutouts
152

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    @Override
164
    public void onWindowFocusChanged(boolean hasFocus)
165
      {
166
      super.onWindowFocusChanged(hasFocus);
167

    
168
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
169
        {
170
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
171
        }
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
    
176
    @Override
177
    protected void onPause() 
178
      {
179
      super.onPause();
180
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
181
      view.onPause();
182
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
183
      savePreferences();
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
    
188
    @Override
189
    protected void onResume() 
190
      {
191
      super.onResume();
192

    
193
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
194
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
195
      view.onResume();
196

    
197
      if( mScreen==null )
198
        {
199
        int ordinal = getObjectOrdinal();
200
        mScreen = new BandagedScreen(ordinal);
201
        }
202

    
203
      mScreen.onAttachedToWindow(this);
204

    
205
      restorePreferences();
206
      BandagedWorkerThread.create(this);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    
211
    @Override
212
    protected void onDestroy() 
213
      {
214
      super.onDestroy();
215
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    void OpenGLError()
221
      {
222
      RubikDialogError errDiag = new RubikDialogError();
223
      errDiag.show(getSupportFragmentManager(), null);
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    private void savePreferences()
229
      {
230
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
231
      SharedPreferences.Editor editor = preferences.edit();
232
      mScreen.savePreferences(editor);
233
      editor.apply();
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    private void restorePreferences()
239
      {
240
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
241
      mScreen.restorePreferences(this,preferences);
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
// PUBLIC API
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    public void changeObject(int x, int y, int z)
249
      {
250
      BandagedRenderer renderer = getRenderer();
251
      renderer.changeObject(x,y,z);
252
      }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
    public boolean isRTL()
257
      {
258
      return mRTL;
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
    public BandagedRenderer getRenderer()
264
      {
265
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
266
      return view.getRenderer();
267
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
    public boolean objectDoesntExist(String name)
272
      {
273
      return mScreen.objectDoesntExist(name);
274
      }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
    public void addObject(String name)
279
      {
280
      mScreen.addObject(this,name);
281
      }
282

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

    
285
    public void deleteObject(String name)
286
      {
287
      RubikFiles files = RubikFiles.getInstance();
288
      InputStream jsonStream = files.openFile(this,name+"_object.json");
289
      InitAssets assets = new InitAssets(jsonStream,null,null);
290

    
291
      if( !assets.noJsonStream() )
292
        {
293
        TwistyObject object = new TwistyJson( TwistyObject.MODE_NORM, null, null, 1.0f, assets);
294

    
295
        if( !object.getError() )
296
          {
297
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
298
          SharedPreferences.Editor editor = preferences.edit();
299
          OSInterface os = new OSInterface(this,null);
300
          os.setEditor(editor);
301
          object.removePreferences(os);
302
          editor.apply();
303
          }
304
        }
305

    
306
      mScreen.deleteObject(this,name);
307
      files.deleteIcon(this,name);
308
      files.deleteJsonObject(this,name);
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
    public void playObject(String name)
314
      {
315
      Intent intent = new Intent(this, PlayActivity.class);
316
      intent.putExtra("level", -1);
317
      intent.putExtra("name", name);
318
      intent.putExtra("scrambles", NUM_SCRAMBLES);
319
      intent.putExtra("local", true);
320
      intent.putExtra("ordinal", 0);
321
      startActivity(intent);
322
      }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    public void iconCreationDone(Bitmap bmp)
327
      {
328
      mScreen.iconCreationDone(this,bmp);
329
      }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
    public int getScreenWidthInPixels()
334
      {
335
      return mScreenWidth;
336
      }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
    public int getScreenHeightInPixels()
341
      {
342
      return mScreenHeight;
343
      }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
    public int getObjectOrdinal()
348
      {
349
      return mObjectOrdinal;
350
      }
351
}
(1-1/7)