Project

General

Profile

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

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

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 android.content.Intent;
23
import android.content.SharedPreferences;
24
import android.graphics.Bitmap;
25
import android.os.Build;
26
import android.os.Bundle;
27
import android.preference.PreferenceManager;
28
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
import org.distorted.external.RubikFiles;
40
import org.distorted.library.main.DistortedLibrary;
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class BandagedCreatorActivity extends AppCompatActivity
47
{
48
    private static final int ACTIVITY_NUMBER = 3;
49
    private static final float RATIO_BAR   = 0.10f;
50
    static final float RATIO_SCROLL= 0.30f;
51

    
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
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
97
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
98

    
99
      LinearLayout layout = findViewById(R.id.lowerBar);
100
      ViewGroup.LayoutParams paramsL = layout.getLayoutParams();
101
      paramsL.height = barHeight;
102
      layout.setLayoutParams(paramsL);
103

    
104
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
105
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
106
      paramsT.height = viewHeight;
107

    
108
      int p = (int)(mScreenHeight* RubikActivity.PADDING);
109
      topLayout.setPadding(p,p,p,p);
110
      topLayout.setLayoutParams(paramsT);
111

    
112
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
113
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
114
      paramsC.height = objectHeight;
115
      creator.setLayoutParams(paramsC);
116
      }
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
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
175
      view.onPause();
176
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
177
      savePreferences();
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
    
182
    @Override
183
    protected void onResume() 
184
      {
185
      super.onResume();
186
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
187
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
188
      view.onResume();
189

    
190
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
191
      mScreen.onAttachedToWindow(this);
192

    
193
      restorePreferences();
194
      BandagedCreatorWorkerThread.create(this);
195
      }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    private void restorePreferences()
228
      {
229
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
230
      String objects = preferences.getString("bandagedObjects","");
231
      mScreen.addObjects(this,objects);
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// PUBLIC API
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public FirebaseAnalytics getAnalytics()
239
      {
240
      return mFirebaseAnalytics;
241
      }
242

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

    
245
    public BandagedCreatorRenderer getRenderer()
246
      {
247
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
248
      return view.getRenderer();
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
    public void addObject(String name)
254
      {
255
      if( mScreen.objectDoesntExist(name) )
256
        {
257
        mScreen.addObject(this,name);
258
        }
259
      }
260

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

    
263
    public void deleteObject(String name, int numCubits)
264
      {
265
      mScreen.deleteObject(this,name);
266

    
267
      RubikFiles files = RubikFiles.getInstance();
268
      files.deleteIcon(this,name);
269
      files.deleteJsonObject(this,name);
270

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

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
    public void playObject(String name)
281
      {
282
      Intent intent = new Intent(this, BandagedPlayActivity.class);
283
      intent.putExtra("name", name);
284
      startActivity(intent);
285
      }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
    public void iconCreationDone(Bitmap bmp)
290
      {
291
      mScreen.iconCreationDone(this,bmp);
292
      }
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
}
(1-1/13)