Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 1a106eb8

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
// 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 88b94310 Leszek Koltunski
import android.content.Intent;
23 a41e3c94 Leszek Koltunski
import android.content.SharedPreferences;
24 7cb8d4b0 Leszek Koltunski
import android.graphics.Bitmap;
25 9530f6b0 Leszek Koltunski
import android.os.Build;
26
import android.os.Bundle;
27 a41e3c94 Leszek Koltunski
import android.preference.PreferenceManager;
28 9530f6b0 Leszek Koltunski
import android.util.DisplayMetrics;
29
import android.view.View;
30
import android.view.ViewGroup;
31
import android.view.WindowManager;
32
import android.widget.LinearLayout;
33
34
import androidx.appcompat.app.AppCompatActivity;
35
36
import com.google.firebase.analytics.FirebaseAnalytics;
37
38
import org.distorted.dialogs.RubikDialogError;
39 81493402 Leszek Koltunski
import org.distorted.external.RubikFiles;
40 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
41
import org.distorted.main.R;
42 48314d6a Leszek Koltunski
import org.distorted.main.RubikActivity;
43 9530f6b0 Leszek Koltunski
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
public class BandagedCreatorActivity extends AppCompatActivity
47
{
48
    private static final int ACTIVITY_NUMBER = 3;
49 28cb1607 Leszek Koltunski
    private static final float RATIO_BAR   = 0.10f;
50 83e021c5 Leszek Koltunski
    static final float RATIO_SCROLL= 0.30f;
51 9530f6b0 Leszek Koltunski
52
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
53
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
54
55
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
56
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
57
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
58
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
59
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
60
61
    private FirebaseAnalytics mFirebaseAnalytics;
62
    private static int mScreenWidth, mScreenHeight;
63
    private int mCurrentApiVersion;
64
    private BandagedCreatorScreen mScreen;
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
    @Override
69
    protected void onCreate(Bundle savedState)
70
      {
71
      super.onCreate(savedState);
72
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
73
      setTheme(R.style.MaterialThemeNoActionBar);
74
      setContentView(R.layout.bandaged);
75
76
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
77
78
      DisplayMetrics displaymetrics = new DisplayMetrics();
79
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
80
      mScreenWidth =displaymetrics.widthPixels;
81
      mScreenHeight=displaymetrics.heightPixels;
82
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
83
                                                  // which is not yet hidden.
84
                                                  // TODO: figure this out exactly.
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 48314d6a Leszek Koltunski
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
97 bc7e49ec Leszek Koltunski
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
98 9530f6b0 Leszek Koltunski
99
      LinearLayout layout = findViewById(R.id.lowerBar);
100
      ViewGroup.LayoutParams paramsL = layout.getLayoutParams();
101
      paramsL.height = barHeight;
102
      layout.setLayoutParams(paramsL);
103
104 9f8d4c92 Leszek Koltunski
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
105
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
106
      paramsT.height = viewHeight;
107 9530f6b0 Leszek Koltunski
108 48314d6a Leszek Koltunski
      int p = (int)(mScreenHeight* RubikActivity.PADDING);
109 9f8d4c92 Leszek Koltunski
      topLayout.setPadding(p,p,p,p);
110
      topLayout.setLayoutParams(paramsT);
111 48314d6a Leszek Koltunski
112
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
113
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
114
      paramsC.height = objectHeight;
115
      creator.setLayoutParams(paramsC);
116 9530f6b0 Leszek Koltunski
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
    private void hideNavigationBar()
121
      {
122
      mCurrentApiVersion = Build.VERSION.SDK_INT;
123
124
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
125
        {
126
        final View decorView = getWindow().getDecorView();
127
128
        decorView.setSystemUiVisibility(FLAGS);
129
130
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
131
          {
132
          @Override
133
          public void onSystemUiVisibilityChange(int visibility)
134
            {
135
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
136
              {
137
              decorView.setSystemUiVisibility(FLAGS);
138
              }
139
            }
140
          });
141
        }
142
      }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// do not avoid cutouts
146
147
    private void cutoutHack()
148
      {
149
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
150
        {
151
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
152
        }
153
      }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157
    @Override
158
    public void onWindowFocusChanged(boolean hasFocus)
159
      {
160
      super.onWindowFocusChanged(hasFocus);
161
162
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
163
        {
164
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
165
        }
166
      }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
    
170
    @Override
171
    protected void onPause() 
172
      {
173
      super.onPause();
174 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
175 9530f6b0 Leszek Koltunski
      view.onPause();
176
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
177 13a3dfa9 Leszek Koltunski
      savePreferences();
178 9530f6b0 Leszek Koltunski
      }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
    
182
    @Override
183
    protected void onResume() 
184
      {
185
      super.onResume();
186
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
187 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
188 9530f6b0 Leszek Koltunski
      view.onResume();
189
190
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
191 bebd7af5 Leszek Koltunski
      mScreen.onAttachedToWindow(this);
192 7cb8d4b0 Leszek Koltunski
193 a41e3c94 Leszek Koltunski
      restorePreferences();
194 7cb8d4b0 Leszek Koltunski
      BandagedCreatorWorkerThread.create(this);
195 9530f6b0 Leszek Koltunski
      }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
    
199
    @Override
200
    protected void onDestroy() 
201
      {
202
      super.onDestroy();
203
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
204
      }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
    void OpenGLError()
209
      {
210
      RubikDialogError errDiag = new RubikDialogError();
211
      errDiag.show(getSupportFragmentManager(), null);
212
      }
213
214 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216
    private void savePreferences()
217
      {
218
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
219
      SharedPreferences.Editor editor = preferences.edit();
220 a41e3c94 Leszek Koltunski
      String objects = mScreen.generateObjectStrings();
221
      editor.putString("bandagedObjects", objects );
222 13a3dfa9 Leszek Koltunski
      editor.apply();
223
      }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
    private void restorePreferences()
228
      {
229
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
230 a41e3c94 Leszek Koltunski
      String objects = preferences.getString("bandagedObjects","");
231
      mScreen.addObjects(this,objects);
232 13a3dfa9 Leszek Koltunski
      }
233
234 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// PUBLIC API
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238
    public FirebaseAnalytics getAnalytics()
239
      {
240
      return mFirebaseAnalytics;
241
      }
242
243 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
    public BandagedCreatorRenderer getRenderer()
246
      {
247
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
248
      return view.getRenderer();
249
      }
250
251 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
    public void addObject(String name)
254
      {
255 a41e3c94 Leszek Koltunski
      if( mScreen.objectDoesntExist(name) )
256
        {
257
        mScreen.addObject(this,name);
258
        }
259 e48ad1af Leszek Koltunski
      }
260
261 d3d639b1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263 d26274d1 Leszek Koltunski
    public void deleteObject(String name, int numCubits)
264 d3d639b1 Leszek Koltunski
      {
265
      mScreen.deleteObject(this,name);
266 81493402 Leszek Koltunski
267
      RubikFiles files = RubikFiles.getInstance();
268
      files.deleteIcon(this,name);
269
      files.deleteJsonObject(this,name);
270 d26274d1 Leszek Koltunski
271
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
272
      SharedPreferences.Editor editor = preferences.edit();
273
      String objName = name+" ";
274
      for(int i=0; i<numCubits; i++) editor.remove(objName+i);
275
      editor.apply();
276 d3d639b1 Leszek Koltunski
      }
277
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279
280
    public void playObject(String name)
281
      {
282 88b94310 Leszek Koltunski
      Intent intent = new Intent(this, BandagedPlayActivity.class);
283
      intent.putExtra("name", name);
284
      startActivity(intent);
285 d3d639b1 Leszek Koltunski
      }
286
287 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289 7cb8d4b0 Leszek Koltunski
    public void iconCreationDone(Bitmap bmp)
290 9530f6b0 Leszek Koltunski
      {
291 20b60ad9 Leszek Koltunski
      mScreen.iconCreationDone(this,bmp);
292 9530f6b0 Leszek Koltunski
      }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
    public int getScreenWidthInPixels()
297
      {
298
      return mScreenWidth;
299
      }
300
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
    public int getScreenHeightInPixels()
304
      {
305
      return mScreenHeight;
306
      }
307
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
    public static int getDrawableSize()
311
      {
312
      if( mScreenHeight<1000 )
313
        {
314
        return 0;
315
        }
316
      if( mScreenHeight<1600 )
317
        {
318
        return 1;
319
        }
320
      if( mScreenHeight<1900 )
321
        {
322
        return 2;
323
        }
324
325
      return 3;
326
      }
327
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
330
    public static int getDrawable(int small, int medium, int big, int huge)
331
      {
332
      int size = getDrawableSize();
333
334
      switch(size)
335
        {
336
        case 0 : return small;
337
        case 1 : return medium;
338
        case 2 : return big;
339
        default: return huge;
340
        }
341
      }
342
}