Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ aec421fd

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.config;
21

    
22
import java.io.InputStream;
23

    
24
import android.os.Build;
25
import android.os.Bundle;
26
import android.util.DisplayMetrics;
27
import android.view.View;
28
import android.view.ViewGroup;
29
import android.view.WindowManager;
30
import android.widget.LinearLayout;
31
import androidx.appcompat.app.AppCompatActivity;
32
import com.google.firebase.analytics.FirebaseAnalytics;
33

    
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.objectlib.main.ObjectControl;
36
import org.distorted.main.R;
37
import org.distorted.dialogs.RubikDialogError;
38
import org.distorted.objects.RubikObject;
39
import org.distorted.objects.RubikObjectList;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class ConfigActivity extends AppCompatActivity
44
{
45
    private static final int ACTIVITY_NUMBER = 2;
46
    private static final float RATIO_BAR  = 0.10f;
47

    
48
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
49
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
50

    
51
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
52
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
53
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
54
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
55
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
56

    
57
    private FirebaseAnalytics mFirebaseAnalytics;
58
    private static int mScreenWidth, mScreenHeight;
59
    private int mCurrentApiVersion;
60
    private ConfigScreen mScreen;
61
    private int mObjectOrdinal;
62
    private int mHeightBar;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    @Override
67
    protected void onCreate(Bundle savedState)
68
      {
69
      super.onCreate(savedState);
70
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
71
      setTheme(R.style.MaterialThemeNoActionBar);
72
      setContentView(R.layout.config);
73

    
74
      Bundle b = getIntent().getExtras();
75

    
76
      if(b != null) mObjectOrdinal = b.getInt("obj");
77

    
78
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
79

    
80
      DisplayMetrics displaymetrics = new DisplayMetrics();
81
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
82
      mScreenWidth =displaymetrics.widthPixels;
83
      mScreenHeight=displaymetrics.heightPixels;
84
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
85
                                                  // which is not yet hidden.
86
                                                  // TODO: figure this out exactly.
87
      hideNavigationBar();
88
      cutoutHack();
89
      computeBarHeights();
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
// this does not include possible insets
94

    
95
    private void computeBarHeights()
96
      {
97
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
98
      mHeightBar = barHeight;
99

    
100
      LinearLayout layout = findViewById(R.id.lowerBar);
101
      ViewGroup.LayoutParams params = layout.getLayoutParams();
102
      params.height = barHeight;
103
      layout.setLayoutParams(params);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    private void hideNavigationBar()
109
      {
110
      mCurrentApiVersion = Build.VERSION.SDK_INT;
111

    
112
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
113
        {
114
        final View decorView = getWindow().getDecorView();
115

    
116
        decorView.setSystemUiVisibility(FLAGS);
117

    
118
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
119
          {
120
          @Override
121
          public void onSystemUiVisibilityChange(int visibility)
122
            {
123
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
124
              {
125
              decorView.setSystemUiVisibility(FLAGS);
126
              }
127
            }
128
          });
129
        }
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// do not avoid cutouts
134

    
135
    private void cutoutHack()
136
      {
137
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
138
        {
139
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
140
        }
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    @Override
146
    public void onWindowFocusChanged(boolean hasFocus)
147
      {
148
      super.onWindowFocusChanged(hasFocus);
149

    
150
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
151
        {
152
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
153
        }
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
    @Override
159
    protected void onPause() 
160
      {
161
      super.onPause();
162
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
163
      view.onPause();
164
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
    
169
    @Override
170
    protected void onResume() 
171
      {
172
      super.onResume();
173
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
174
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
175
      view.onResume();
176

    
177
      if( mScreen==null ) mScreen = new ConfigScreen();
178
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
179

    
180
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
181
        {
182
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
183
        changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
184
        }
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
    
189
    @Override
190
    protected void onDestroy() 
191
      {
192
      super.onDestroy();
193
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    void OpenGLError()
199
      {
200
      RubikDialogError errDiag = new RubikDialogError();
201
      errDiag.show(getSupportFragmentManager(), null);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
207
      {
208
      if( object!=null )
209
        {
210
        int meshState          = object.getMeshState();
211
        InputStream jsonStream = object.getObjectStream(this);
212
        InputStream meshStream = object.getMeshStream(this);
213
        String name            = object.getUpperName();
214

    
215
        control.changeIfDifferent(ordinal,name,meshState,jsonStream,meshStream);
216
        }
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
// PUBLIC API
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
    public void changeObject(int ordinal)
224
      {
225
      mObjectOrdinal = ordinal;
226
      RubikObject object = RubikObjectList.getObject(ordinal);
227
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
228
      ObjectControl control = view.getObjectControl();
229
      changeIfDifferent(object,ordinal,control);
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    public void changeMeshState(RubikObject object, int ordinal)
235
      {
236
      if( object!=null )
237
        {
238
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
239
        ObjectControl control = view.getObjectControl();
240

    
241
        int meshState          = object.getMeshState();
242
        InputStream jsonStream = object.getObjectStream(this);
243
        InputStream meshStream = object.getMeshStream(this);
244

    
245
        control.changeObject(ordinal,meshState,jsonStream,meshStream);
246
        }
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
    public FirebaseAnalytics getAnalytics()
252
      {
253
      return mFirebaseAnalytics;
254
      }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
    public int getHeightBar()
259
      {
260
      return mHeightBar;
261
      }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
    public int getScreenWidthInPixels()
266
      {
267
      return mScreenWidth;
268
      }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
    public int getScreenHeightInPixels()
273
      {
274
      return mScreenHeight;
275
      }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
    public ObjectControl getControl()
280
      {
281
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
282
      return view.getObjectControl();
283
      }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
    public static int getDrawableSize()
288
      {
289
      if( mScreenHeight<1000 )
290
        {
291
        return 0;
292
        }
293
      if( mScreenHeight<1600 )
294
        {
295
        return 1;
296
        }
297
      if( mScreenHeight<1900 )
298
        {
299
        return 2;
300
        }
301

    
302
      return 3;
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    public static int getDrawable(int small, int medium, int big, int huge)
308
      {
309
      int size = getDrawableSize();
310

    
311
      switch(size)
312
        {
313
        case 0 : return small;
314
        case 1 : return medium;
315
        case 2 : return big;
316
        default: return huge;
317
        }
318
      }
319

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

    
322
    public boolean isVertical()
323
      {
324
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
325
      return view.isVertical();
326
      }
327
}
(1-1/6)