Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ 2876aeb6

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.config;
11

    
12
import java.io.InputStream;
13

    
14
import android.os.Build;
15
import android.os.Bundle;
16
import android.util.DisplayMetrics;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.view.WindowManager;
20
import android.widget.LinearLayout;
21
import androidx.appcompat.app.AppCompatActivity;
22

    
23
import org.distorted.library.main.DistortedLibrary;
24
import org.distorted.objectlib.main.InitAssets;
25
import org.distorted.objectlib.main.ObjectControl;
26
import org.distorted.main.R;
27
import org.distorted.dialogs.RubikDialogError;
28
import org.distorted.objectlib.main.TwistyObject;
29
import org.distorted.objects.RubikObject;
30
import org.distorted.objects.RubikObjectList;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class ConfigActivity extends AppCompatActivity
35
{
36
    private static final int ACTIVITY_NUMBER = 2;
37
    private static final float RATIO_BAR  = 0.10f;
38

    
39
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
40
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
41
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
42
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
43
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
44

    
45
    private static int mScreenWidth, mScreenHeight;
46
    private int mCurrentApiVersion;
47
    private ConfigScreen mScreen;
48
    private int mObjectOrdinal;
49
    private int mHeightBar;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    @Override
54
    protected void onCreate(Bundle savedState)
55
      {
56
      super.onCreate(savedState);
57
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
58
      setTheme(R.style.MaterialThemeNoActionBar);
59
      setContentView(R.layout.config);
60

    
61
      Bundle b = getIntent().getExtras();
62

    
63
      if(b != null) mObjectOrdinal = b.getInt("obj");
64

    
65
      DisplayMetrics displaymetrics = new DisplayMetrics();
66
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
67
      mScreenWidth =displaymetrics.widthPixels;
68
      mScreenHeight=displaymetrics.heightPixels;
69

    
70
      hideNavigationBar();
71
      cutoutHack();
72
      computeBarHeights();
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// this does not include possible insets
77

    
78
    private void computeBarHeights()
79
      {
80
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
81
      mHeightBar = barHeight;
82

    
83
      LinearLayout layout = findViewById(R.id.lowerBar);
84
      ViewGroup.LayoutParams params = layout.getLayoutParams();
85
      params.height = barHeight;
86
      layout.setLayoutParams(params);
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
    private void hideNavigationBar()
92
      {
93
      mCurrentApiVersion = Build.VERSION.SDK_INT;
94

    
95
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
96
        {
97
        final View decorView = getWindow().getDecorView();
98

    
99
        decorView.setSystemUiVisibility(FLAGS);
100

    
101
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
102
          {
103
          @Override
104
          public void onSystemUiVisibilityChange(int visibility)
105
            {
106
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
107
              {
108
              decorView.setSystemUiVisibility(FLAGS);
109
              }
110
            }
111
          });
112
        }
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
// do not avoid cutouts
117

    
118
    private void cutoutHack()
119
      {
120
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
121
        {
122
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
123
        }
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    @Override
129
    public void onWindowFocusChanged(boolean hasFocus)
130
      {
131
      super.onWindowFocusChanged(hasFocus);
132

    
133
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
134
        {
135
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
136
        }
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141
    @Override
142
    protected void onPause() 
143
      {
144
      super.onPause();
145
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
146
      view.onPause();
147
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152
    @Override
153
    protected void onResume() 
154
      {
155
      super.onResume();
156
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
157
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
158
      view.onResume();
159

    
160
      if( mScreen==null ) mScreen = new ConfigScreen();
161
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
162

    
163
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
164
        {
165
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
166
        changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
167
        }
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
    
172
    @Override
173
    protected void onDestroy() 
174
      {
175
      super.onDestroy();
176
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    void OpenGLError()
182
      {
183
      RubikDialogError errDiag = new RubikDialogError();
184
      errDiag.show(getSupportFragmentManager(), null);
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
    private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
190
      {
191
      if( object!=null )
192
        {
193
        int meshState          = object.getMeshState();
194
        int iconMode           = TwistyObject.MODE_NORM;
195
        InputStream jsonStream = object.getObjectStream(this);
196
        InputStream meshStream = object.getMeshStream(this);
197
        String name            = object.getUpperName();
198
        InitAssets asset       = new InitAssets(jsonStream,meshStream,null);
199
        control.changeIfDifferent(ordinal,name,meshState,iconMode,asset);
200
        }
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// PUBLIC API
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    public void changeObject(int ordinal)
208
      {
209
      mObjectOrdinal = ordinal;
210
      RubikObject object = RubikObjectList.getObject(ordinal);
211
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
212
      ObjectControl control = view.getObjectControl();
213
      changeIfDifferent(object,ordinal,control);
214
      }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
    public void changeMeshState(RubikObject object, int ordinal)
219
      {
220
      if( object!=null )
221
        {
222
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
223
        ObjectControl control = view.getObjectControl();
224

    
225
        int meshState          = object.getMeshState();
226
        int iconMode           = TwistyObject.MODE_NORM;
227
        InputStream jsonStream = object.getObjectStream(this);
228
        InputStream meshStream = object.getMeshStream(this);
229
        InitAssets asset       = new InitAssets(jsonStream,meshStream,null);
230

    
231
        control.changeObject(ordinal,meshState,iconMode,asset);
232
        }
233
      }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
    public int getHeightBar()
238
      {
239
      return mHeightBar;
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    public int getScreenWidthInPixels()
245
      {
246
      return mScreenWidth;
247
      }
248

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

    
251
    public int getScreenHeightInPixels()
252
      {
253
      return mScreenHeight;
254
      }
255

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

    
258
    public ObjectControl getControl()
259
      {
260
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
261
      return view.getObjectControl();
262
      }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
    public static int getDrawableSize()
267
      {
268
      if( mScreenHeight<1000 )
269
        {
270
        return 0;
271
        }
272
      if( mScreenHeight<1600 )
273
        {
274
        return 1;
275
        }
276
      if( mScreenHeight<1900 )
277
        {
278
        return 2;
279
        }
280

    
281
      return 3;
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    public static int getDrawable(int small, int medium, int big, int huge)
287
      {
288
      int size = getDrawableSize();
289

    
290
      switch(size)
291
        {
292
        case 0 : return small;
293
        case 1 : return medium;
294
        case 2 : return big;
295
        default: return huge;
296
        }
297
      }
298

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

    
301
    public boolean isVertical()
302
      {
303
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
304
      return view.isVertical();
305
      }
306
}
(1-1/6)