Project

General

Profile

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

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

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
    private static final int ACTIVITY_NUMBER    = 2;
47
    private static final float RATIO_BAR        = MainActivity.RATIO_BAR;
48
    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
    private static final float PADDING          = 0.010f;
52
    public static final int FLAGS               = MainActivity.FLAGS;
53
    private static final int NUM_SCRAMBLES      = 300;
54

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

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

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

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

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

    
84
      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
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
96
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
97
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
98
      int padding      = (int)(mScreenHeight*PADDING);
99

    
100
      LinearLayout botLayout = findViewById(R.id.lowerBar);
101
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
102
      paramsL.height = barHeight;
103
      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

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

    
117
      BandagedView creator = findViewById(R.id.bandagedCreatorObjectView);
118
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
119
      paramsC.height = objectHeight;
120
      creator.setLayoutParams(paramsC);
121
      }
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
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
180
      view.onPause();
181
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
182
      savePreferences();
183
      }
184

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

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

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

    
202
      mScreen.onAttachedToWindow(this);
203

    
204
      restorePreferences();
205
      BandagedWorkerThread.create(this);
206
      }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

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

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

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

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

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

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

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

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

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

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

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
    public void iconCreationDone(Bitmap bmp)
326
      {
327
      mScreen.iconCreationDone(this,bmp);
328
      }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
345

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