Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ 988e3d33

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 android.content.SharedPreferences;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.util.DisplayMetrics;
16
import android.view.View;
17
import android.view.ViewGroup;
18
import android.view.WindowManager;
19
import android.widget.LinearLayout;
20

    
21
import androidx.appcompat.app.AppCompatActivity;
22
import androidx.preference.PreferenceManager;
23

    
24
import org.distorted.dialogs.RubikDialogError;
25
import org.distorted.library.main.DistortedLibrary;
26
import org.distorted.main.MainActivity;
27
import org.distorted.main.R;
28
import org.distorted.objectlib.main.InitAssets;
29
import org.distorted.objectlib.main.ObjectControl;
30
import org.distorted.objectlib.main.TwistyObject;
31
import org.distorted.objects.RubikObject;
32
import org.distorted.objects.RubikObjectList;
33
import org.distorted.os.OSInterface;
34

    
35
import java.io.InputStream;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class ConfigActivity extends AppCompatActivity
40
{
41
    private static final int ACTIVITY_NUMBER = 7;
42
    private static final float RATIO_BAR  = MainActivity.RATIO_BAR;
43
    public static final int FLAGS = MainActivity.FLAGS;
44

    
45
    private static int mScreenWidth, mScreenHeight;
46
    private ConfigScreen mScreen;
47
    private ConfigScreenPane mPane;
48
    private int mCurrentApiVersion;
49
    private int mObjectOrdinal;
50
    private boolean mSupportsAdjColors;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

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

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

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

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

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

    
79
    private void computeBarHeights()
80
      {
81
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
82
      LinearLayout layout = findViewById(R.id.lowerBar);
83
      ViewGroup.LayoutParams params = layout.getLayoutParams();
84
      params.height = barHeight;
85
      layout.setLayoutParams(params);
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

    
98
        decorView.setSystemUiVisibility(FLAGS);
99

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
// do not avoid cutouts
116

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    @Override
141
    protected void onPause() 
142
      {
143
      super.onPause();
144
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
145
      view.onPause();
146
      savePreferences();
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
      String key ="";
160
      mSupportsAdjColors = false;
161

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

    
167
        if( object!=null )
168
          {
169
          key = object.getUpperName();
170
          mSupportsAdjColors = object.supportsAdjustableColors();
171
          }
172
        }
173

    
174
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
175
      OSInterface os = view.getInterface();
176
      os.setPreferences(preferences);
177

    
178
      restorePreferences();
179

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

    
183
      mPane = new ConfigScreenPane(this,key,os,mSupportsAdjColors);
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
    void repaintPuzzleFace(int cubit, int face)
189
      {
190
      if( mPane!=null && mSupportsAdjColors )
191
        {
192
        int color  = mPane.getCurrentColor();
193
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
194
        ObjectControl control = view.getObjectControl();
195
        control.repaintPuzzleFace(cubit,face,color);
196
        }
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
    @Override
202
    protected void onDestroy() 
203
      {
204
      super.onDestroy();
205
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
206
      }
207

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

    
210
    void OpenGLError()
211
      {
212
      RubikDialogError errDiag = new RubikDialogError();
213
      errDiag.show(getSupportFragmentManager(), null);
214
      }
215

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

    
218
    private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
219
      {
220
      if( object!=null )
221
        {
222
        int iconMode           = TwistyObject.MODE_NORM;
223
        InputStream jsonStream = object.getObjectStream(this);
224
        InputStream meshStream = object.getMeshStream(this);
225
        String name            = object.getUpperName();
226
        InitAssets asset       = new InitAssets(jsonStream,meshStream,null);
227
        control.changeIfDifferent(ordinal,name,iconMode,asset);
228
        }
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    private void savePreferences()
234
      {
235
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
236
      SharedPreferences.Editor editor = preferences.edit();
237

    
238
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
239
      OSInterface os = view.getInterface();
240
      os.setEditor(editor);
241
      view.getObjectControl().savePreferences();
242

    
243
      editor.apply();
244
      }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    private void restorePreferences()
249
      {
250
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
251
      view.getObjectControl().restoreStickers();
252
      }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
// PUBLIC API
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

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

    
266
    public void recreateColors()
267
      {
268
      ObjectControl control = getControl();
269
      control.recreateColors();
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
    public void recreateStickers(int borders, int corners)
275
      {
276
      ObjectControl control = getControl();
277

    
278
      float border_step = ConfigScreenPane.BORDER_STEPS[borders];
279
      float corner_step = ConfigScreenPane.CORNER_STEPS[corners];
280

    
281
      control.recreateSticker(border_step,corner_step);
282
      }
283

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

    
286
    public void resetUI()
287
      {
288
      mPane.resetUI(this);
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public int getScreenWidthInPixels()
294
      {
295
      return mScreenWidth;
296
      }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
    public int getScreenHeightInPixels()
301
      {
302
      return mScreenHeight;
303
      }
304

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

    
307
    public ObjectControl getControl()
308
      {
309
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
310
      return view.getObjectControl();
311
      }
312
}
(1-1/6)