Project

General

Profile

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

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

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.dialogs.RubikDialogMessage;
26
import org.distorted.library.main.DistortedLibrary;
27
import org.distorted.main.MainActivity;
28
import org.distorted.main.R;
29
import org.distorted.objectlib.main.InitAssets;
30
import org.distorted.objectlib.main.ObjectControl;
31
import org.distorted.objectlib.main.TwistyObject;
32
import org.distorted.objects.RubikObject;
33
import org.distorted.objects.RubikObjectList;
34
import org.distorted.os.OSInterface;
35

    
36
import java.io.InputStream;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
64
      Bundle b = getIntent().getExtras();
65
      if(b != null) mObjectOrdinal = b.getInt("obj");
66

    
67
      mDisplayMessageDialog = true;
68

    
69
      DisplayMetrics displaymetrics = new DisplayMetrics();
70
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
71
      mScreenWidth =displaymetrics.widthPixels;
72
      mScreenHeight=displaymetrics.heightPixels;
73

    
74
      hideNavigationBar();
75
      cutoutHack();
76
      computeBarHeights();
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// this does not include possible insets
81

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
    private void hideNavigationBar()
94
      {
95
      mCurrentApiVersion = Build.VERSION.SDK_INT;
96

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

    
101
        decorView.setSystemUiVisibility(FLAGS);
102

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// do not avoid cutouts
119

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

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

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
    @Override
156
    protected void onResume() 
157
      {
158
      super.onResume();
159
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
160
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
161
      view.onResume();
162
      String key ="";
163
      mSupportsAdjColors = false;
164

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

    
170
        if( object!=null )
171
          {
172
          key = object.getUpperName();
173
          mSupportsAdjColors = object.supportsAdjustableColors();
174
          }
175
        }
176

    
177
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
178
      OSInterface os = view.getInterface();
179
      os.setPreferences(preferences);
180

    
181
      restorePreferences(preferences);
182

    
183
      if( mScreen==null ) mScreen = new ConfigScreen();
184
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
185

    
186
      mPane = new ConfigScreenPane(this,key,os,mSupportsAdjColors);
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    @Override
206
    protected void onDestroy() 
207
      {
208
      super.onDestroy();
209
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    void OpenGLError()
215
      {
216
      RubikDialogError errDiag = new RubikDialogError();
217
      errDiag.show(getSupportFragmentManager(), null);
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

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

    
237
    private void savePreferences()
238
      {
239
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
240
      SharedPreferences.Editor editor = preferences.edit();
241

    
242
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
243
      OSInterface os = view.getInterface();
244
      os.setEditor(editor);
245
      view.getObjectControl().savePreferences();
246

    
247
      editor.putBoolean("configDisplayDialog", mDisplayMessageDialog );
248

    
249
      editor.apply();
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    private void restorePreferences(SharedPreferences preferences)
255
      {
256
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
257
      view.getObjectControl().restoreStickers();
258

    
259
      mDisplayMessageDialog = preferences.getBoolean("configDisplayDialog",true);
260

    
261
      if( mDisplayMessageDialog )
262
        {
263
        Bundle bundle = new Bundle();
264
        bundle.putString("argument", getString(R.string.config_message) );
265
        RubikDialogMessage diag = new RubikDialogMessage();
266
        diag.setArguments(bundle);
267
        diag.show( getSupportFragmentManager(), RubikDialogMessage.getDialogTag() );
268
        }
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
// PUBLIC API
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
    public ConfigRenderer getRenderer()
276
      {
277
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
278
      return view.getRenderer();
279
      }
280

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

    
283
    public void recreateColors()
284
      {
285
      ObjectControl control = getControl();
286
      control.recreateColors();
287
      }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    public void recreateStickers(int borders, int corners)
292
      {
293
      ObjectControl control = getControl();
294

    
295
      float border_step = ConfigScreenPane.BORDER_STEPS[borders];
296
      float corner_step = ConfigScreenPane.CORNER_STEPS[corners];
297

    
298
      control.recreateSticker(border_step,corner_step);
299
      }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
    public void resetUI()
304
      {
305
      mPane.resetUI(this);
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    public int getScreenWidthInPixels()
311
      {
312
      return mScreenWidth;
313
      }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
    public int getScreenHeightInPixels()
318
      {
319
      return mScreenHeight;
320
      }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
    public ObjectControl getControl()
325
      {
326
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
327
      return view.getObjectControl();
328
      }
329
}
(1-1/6)