Project

General

Profile

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

magiccube / src / main / java / org / distorted / purchase / PurchaseActivity.java @ 9dfb553f

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.purchase;
11

    
12
import android.content.SharedPreferences;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.preference.PreferenceManager;
16
import android.util.DisplayMetrics;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.view.WindowManager;
20

    
21
import androidx.appcompat.app.AppCompatActivity;
22

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

    
32
import java.io.InputStream;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class PurchaseActivity extends AppCompatActivity
37
{
38
    private static final int ACTIVITY_NUMBER = 5;
39
    private static final float RATIO_UBAR = 0.14f;
40
    private static final float RATIO_LBAR = 0.10f;
41
    private static final float RATIO_VIEW = 0.50f;
42

    
43
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
44
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
45

    
46
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
47
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
48
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
49
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
50
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
51

    
52
    private static int mScreenWidth, mScreenHeight;
53
    private int mCurrentApiVersion;
54
    private PurchaseScreen mScreen;
55
    private int mObjectOrdinal;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
    @Override
60
    protected void onCreate(Bundle savedState)
61
      {
62
      super.onCreate(savedState);
63
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
64
      setTheme(R.style.MaterialThemeNoActionBar);
65
      setContentView(R.layout.purchase);
66

    
67
      Bundle b = getIntent().getExtras();
68

    
69
      if(b != null) mObjectOrdinal = b.getInt("obj");
70

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

    
76
      hideNavigationBar();
77
      cutoutHack();
78
      setHeights();
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
    private void setViewHeight(int id, int height)
84
      {
85
      View view = findViewById(id);
86

    
87
      if( view!=null )
88
        {
89
        ViewGroup.LayoutParams params = view.getLayoutParams();
90
        params.height = height;
91
        view.setLayoutParams(params);
92
        }
93
      }
94

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

    
98
    private void setHeights()
99
      {
100
      int ubarHeight= (int)(mScreenHeight*RATIO_UBAR);
101
      int lbarHeight= (int)(mScreenHeight*RATIO_LBAR);
102
      int viewHeight= (int)(mScreenHeight*RATIO_VIEW);
103
      int layHeight = (int)(mScreenHeight*(1-RATIO_UBAR-RATIO_LBAR-RATIO_VIEW)*0.5f);
104

    
105
      setViewHeight(R.id.upperBar           , ubarHeight);
106
      setViewHeight(R.id.lowerBar           , lbarHeight);
107
      setViewHeight(R.id.purchaseSurfaceView, viewHeight);
108
      setViewHeight(R.id.purchaseLayoutOne  , layHeight);
109
      setViewHeight(R.id.purchaseLayoutAll  , layHeight);
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    private void hideNavigationBar()
115
      {
116
      mCurrentApiVersion = Build.VERSION.SDK_INT;
117

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

    
122
        decorView.setSystemUiVisibility(FLAGS);
123

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
// do not avoid cutouts
140

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
    @Override
165
    protected void onPause() 
166
      {
167
      super.onPause();
168
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
169
      view.onPause();
170
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
171
      savePreferences();
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
    
176
    @Override
177
    protected void onResume() 
178
      {
179
      super.onResume();
180
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
181
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
182
      view.onResume();
183

    
184
      if( mScreen==null ) mScreen = new PurchaseScreen();
185
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
186

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

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
    
196
    @Override
197
    protected void onDestroy() 
198
      {
199
      super.onDestroy();
200
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
201
      }
202

    
203

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

    
206
    private void savePreferences()
207
      {
208
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
209
      SharedPreferences.Editor editor = preferences.edit();
210
      RubikScores scores = RubikScores.getInstance();
211

    
212
      scores.savePreferencesMinimal(editor);
213
      RubikObjectList.savePreferencesMinimal(editor);
214

    
215
      boolean success = editor.commit();
216
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    void OpenGLError()
222
      {
223
      RubikDialogError errDiag = new RubikDialogError();
224
      errDiag.show(getSupportFragmentManager(), null);
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
    private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
230
      {
231
      if( object!=null )
232
        {
233
        int meshState          = object.getMeshState();
234
        int iconMode           = TwistyObject.MODE_NORM;
235
        InputStream jsonStream = object.getObjectStream(this);
236
        InputStream meshStream = object.getMeshStream(this);
237
        String name            = object.getUpperName();
238

    
239
        control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
240
        }
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    public int getScreenWidthInPixels()
248
      {
249
      return mScreenWidth;
250
      }
251

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

    
254
    public int getScreenHeightInPixels()
255
      {
256
      return mScreenHeight;
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    public ObjectControl getControl()
262
      {
263
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
264
      return view.getObjectControl();
265
      }
266

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

    
269
    PurchaseRenderer getRenderer()
270
      {
271
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
272
      return view.getRenderer();
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    void blockUI()
278
      {
279
      mScreen.blockUI();
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

    
299
      return 3;
300
      }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
    public static int getDrawable(int small, int medium, int big, int huge)
305
      {
306
      int size = getDrawableSize();
307

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

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
    int getObjectPrice()
320
      {
321
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
322
        {
323
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
324
        return object==null ? 0 : object.getPrice();
325
        }
326

    
327
      return 0;
328
      }
329
}
(1-1/6)