Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 707f79ff

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.bandaged;
21

    
22
import java.io.InputStream;
23

    
24
import android.content.Intent;
25
import android.content.SharedPreferences;
26
import android.graphics.Bitmap;
27
import android.os.Build;
28
import android.os.Bundle;
29
import android.preference.PreferenceManager;
30
import android.util.DisplayMetrics;
31
import android.view.View;
32
import android.view.ViewGroup;
33
import android.view.WindowManager;
34
import android.widget.LinearLayout;
35

    
36
import androidx.appcompat.app.AppCompatActivity;
37

    
38
import com.google.firebase.analytics.FirebaseAnalytics;
39

    
40
import org.distorted.dialogs.RubikDialogError;
41
import org.distorted.external.RubikFiles;
42
import org.distorted.library.main.DistortedLibrary;
43
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45
import org.distorted.objectlib.main.TwistyJson;
46
import org.distorted.objectlib.main.TwistyObject;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class BandagedCreatorActivity extends AppCompatActivity
51
{
52
    private static final int ACTIVITY_NUMBER = 3;
53
    private static final float RATIO_BAR   = 0.10f;
54
    static final float RATIO_SCROLL= 0.30f;
55

    
56
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
57
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
58

    
59
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
60
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
61
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
62
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
63
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
64

    
65
    private FirebaseAnalytics mFirebaseAnalytics;
66
    private static int mScreenWidth, mScreenHeight;
67
    private int mCurrentApiVersion;
68
    private BandagedCreatorScreen mScreen;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
    @Override
73
    protected void onCreate(Bundle savedState)
74
      {
75
      super.onCreate(savedState);
76
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
77
      setTheme(R.style.MaterialThemeNoActionBar);
78
      setContentView(R.layout.bandaged);
79

    
80
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
81

    
82
      DisplayMetrics displaymetrics = new DisplayMetrics();
83
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
84
      mScreenWidth =displaymetrics.widthPixels;
85
      mScreenHeight=displaymetrics.heightPixels;
86
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
87
                                                  // which is not yet hidden.
88
                                                  // TODO: figure this out exactly.
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 viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
101
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
102

    
103
      LinearLayout layout = findViewById(R.id.lowerBar);
104
      ViewGroup.LayoutParams paramsL = layout.getLayoutParams();
105
      paramsL.height = barHeight;
106
      layout.setLayoutParams(paramsL);
107

    
108
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
109
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
110
      paramsT.height = viewHeight;
111

    
112
      int p = (int)(mScreenHeight* RubikActivity.PADDING);
113
      topLayout.setPadding(p,p,p,p);
114
      topLayout.setLayoutParams(paramsT);
115

    
116
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
117
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
118
      paramsC.height = objectHeight;
119
      creator.setLayoutParams(paramsC);
120
      }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
    private void hideNavigationBar()
125
      {
126
      mCurrentApiVersion = Build.VERSION.SDK_INT;
127

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

    
132
        decorView.setSystemUiVisibility(FLAGS);
133

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// do not avoid cutouts
150

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

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

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
    
186
    @Override
187
    protected void onResume() 
188
      {
189
      super.onResume();
190
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
191
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
192
      view.onResume();
193

    
194
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
195
      mScreen.onAttachedToWindow(this);
196

    
197
      restorePreferences();
198
      BandagedCreatorWorkerThread.create(this);
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
    
203
    @Override
204
    protected void onDestroy() 
205
      {
206
      super.onDestroy();
207
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    void OpenGLError()
213
      {
214
      RubikDialogError errDiag = new RubikDialogError();
215
      errDiag.show(getSupportFragmentManager(), null);
216
      }
217

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

    
220
    private void savePreferences()
221
      {
222
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
223
      SharedPreferences.Editor editor = preferences.edit();
224
      String objects = mScreen.generateObjectStrings();
225
      editor.putString("bandagedObjects", objects );
226
      editor.apply();
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    private void restorePreferences()
232
      {
233
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
234
      String objects = preferences.getString("bandagedObjects","");
235
      mScreen.addObjects(this,objects);
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
// PUBLIC API
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
    public FirebaseAnalytics getAnalytics()
243
      {
244
      return mFirebaseAnalytics;
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
    public BandagedCreatorRenderer getRenderer()
250
      {
251
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
252
      return view.getRenderer();
253
      }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
    public boolean objectDoesntExist(String name)
258
      {
259
      return mScreen.objectDoesntExist(name);
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
    public void addObject(String name)
265
      {
266
      mScreen.addObject(this,name);
267
      }
268

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

    
271
    public void deleteObject(String name)
272
      {
273
      RubikFiles files = RubikFiles.getInstance();
274
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
275
      SharedPreferences.Editor editor = preferences.edit();
276

    
277
      int meshState          = TwistyObject.MESH_NICE;
278
      int iconMode           = TwistyObject.MODE_NORM;
279
      InputStream jsonStream = files.openFile(this,name+"_object.json");
280
      TwistyObject object = new TwistyJson( jsonStream, meshState, iconMode, null, null, 1.0f, null);
281
      object.removePreferences(editor);
282

    
283
      editor.apply();
284

    
285
      mScreen.deleteObject(this,name);
286
      files.deleteIcon(this,name);
287
      files.deleteJsonObject(this,name);
288
      }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
    public void playObject(String name)
293
      {
294
      Intent intent = new Intent(this, BandagedPlayActivity.class);
295
      intent.putExtra("name", name);
296
      startActivity(intent);
297
      }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
    public void iconCreationDone(Bitmap bmp)
302
      {
303
      mScreen.iconCreationDone(this,bmp);
304
      }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
    public int getScreenWidthInPixels()
309
      {
310
      return mScreenWidth;
311
      }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
    public int getScreenHeightInPixels()
316
      {
317
      return mScreenHeight;
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    public static int getDrawableSize()
323
      {
324
      if( mScreenHeight<1000 )
325
        {
326
        return 0;
327
        }
328
      if( mScreenHeight<1600 )
329
        {
330
        return 1;
331
        }
332
      if( mScreenHeight<1900 )
333
        {
334
        return 2;
335
        }
336

    
337
      return 3;
338
      }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
    public static int getDrawable(int small, int medium, int big, int huge)
343
      {
344
      int size = getDrawableSize();
345

    
346
      switch(size)
347
        {
348
        case 0 : return small;
349
        case 1 : return medium;
350
        case 2 : return big;
351
        default: return huge;
352
        }
353
      }
354
}
(1-1/13)