Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ 280dc794

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 android.os.Build;
23
import android.os.Bundle;
24
import android.util.DisplayMetrics;
25
import android.view.View;
26
import android.view.ViewGroup;
27
import android.view.WindowManager;
28
import android.widget.LinearLayout;
29

    
30
import androidx.appcompat.app.AppCompatActivity;
31

    
32
import com.google.firebase.analytics.FirebaseAnalytics;
33

    
34
import org.distorted.dialogs.RubikDialogError;
35
import org.distorted.dmesh.ObjectMesh;
36
import org.distorted.jsons.ObjectJson;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.main.R;
39
import org.distorted.objectlib.main.ObjectControl;
40
import org.distorted.objects.RubikObject;
41
import org.distorted.objects.RubikObjectList;
42

    
43
import java.io.InputStream;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class ConfigActivity extends AppCompatActivity
48
{
49
    private static final int ACTIVITY_NUMBER = 2;
50
    private static final float RATIO_BAR  = 0.10f;
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 ConfigScreen mScreen;
65
    private int mObjectOrdinal;
66
    private int mHeightBar;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

    
78
      Bundle b = getIntent().getExtras();
79

    
80
      if(b != null) mObjectOrdinal = b.getInt("obj");
81

    
82
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
83

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// this does not include possible insets
98

    
99
    private void computeBarHeights()
100
      {
101
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
102
      mHeightBar = barHeight;
103

    
104
      LinearLayout layout = findViewById(R.id.lowerBar);
105
      ViewGroup.LayoutParams params = layout.getLayoutParams();
106
      params.height = barHeight;
107
      layout.setLayoutParams(params);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    private void hideNavigationBar()
113
      {
114
      mCurrentApiVersion = Build.VERSION.SDK_INT;
115

    
116
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
117
        {
118
        final View decorView = getWindow().getDecorView();
119

    
120
        decorView.setSystemUiVisibility(FLAGS);
121

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// do not avoid cutouts
138

    
139
    private void cutoutHack()
140
      {
141
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
142
        {
143
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
144
        }
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
    @Override
150
    public void onWindowFocusChanged(boolean hasFocus)
151
      {
152
      super.onWindowFocusChanged(hasFocus);
153

    
154
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
155
        {
156
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
157
        }
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
    
162
    @Override
163
    protected void onPause() 
164
      {
165
      super.onPause();
166
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
167
      view.onPause();
168
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
    
173
    @Override
174
    protected void onResume() 
175
      {
176
      super.onResume();
177
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
178
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
179
      view.onResume();
180

    
181
      if( mScreen==null ) mScreen = new ConfigScreen();
182
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
183

    
184
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
185
        {
186
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
187
        changeIfDifferent(object,view.getObjectControl());
188
        }
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    @Override
194
    protected void onDestroy() 
195
      {
196
      super.onDestroy();
197
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
    void OpenGLError()
203
      {
204
      RubikDialogError errDiag = new RubikDialogError();
205
      errDiag.show(getSupportFragmentManager(), null);
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    private void changeIfDifferent(RubikObject object,ObjectControl control)
211
      {
212
      if( object!=null )
213
        {
214
        int jsonID = object.getJsonID();
215
        int meshID = object.getMeshID();
216

    
217
        int meshState = object.getMeshState();
218

    
219
        InputStream jsonStream = ObjectJson.getStream(jsonID,this);
220
        InputStream meshStream = ObjectMesh.getStream(meshID,this);
221

    
222
        control.changeIfDifferent(object.getOrdinal(),meshState,jsonStream,meshStream);
223
        }
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
// PUBLIC API
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    public void changeObject(int ordinal)
231
      {
232
      mObjectOrdinal = ordinal;
233
      RubikObject object = RubikObjectList.getObject(ordinal);
234
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
235
      ObjectControl control = view.getObjectControl();
236
      changeIfDifferent(object,control);
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    public void changeMeshState(RubikObject object)
242
      {
243
      if( object!=null )
244
        {
245
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
246
        ObjectControl control = view.getObjectControl();
247

    
248
        int jsonID = object.getJsonID();
249
        int meshID = object.getMeshID();
250

    
251
        int meshState = object.getMeshState();
252

    
253
        InputStream jsonStream = ObjectJson.getStream(jsonID,this);
254
        InputStream meshStream = ObjectMesh.getStream(meshID,this);
255

    
256
        control.changeObject(object.getOrdinal(),meshState,jsonStream,meshStream);
257
        }
258
      }
259

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

    
262
    public FirebaseAnalytics getAnalytics()
263
      {
264
      return mFirebaseAnalytics;
265
      }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    public int getHeightBar()
270
      {
271
      return mHeightBar;
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    public int getScreenWidthInPixels()
277
      {
278
      return mScreenWidth;
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
    public int getScreenHeightInPixels()
284
      {
285
      return mScreenHeight;
286
      }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
    public ObjectControl getControl()
291
      {
292
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
293
      return view.getObjectControl();
294
      }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
    public static int getDrawableSize()
299
      {
300
      if( mScreenHeight<1000 )
301
        {
302
        return 0;
303
        }
304
      if( mScreenHeight<1600 )
305
        {
306
        return 1;
307
        }
308
      if( mScreenHeight<1900 )
309
        {
310
        return 2;
311
        }
312

    
313
      return 3;
314
      }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
    public static int getDrawable(int small, int medium, int big, int huge)
319
      {
320
      int size = getDrawableSize();
321

    
322
      switch(size)
323
        {
324
        case 0 : return small;
325
        case 1 : return medium;
326
        case 2 : return big;
327
        default: return huge;
328
        }
329
      }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
    public boolean isVertical()
334
      {
335
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
336
      return view.isVertical();
337
      }
338
}
(1-1/6)