Project

General

Profile

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

magiccube / src / main / java / org / distorted / purchase / PurchaseActivity.java @ 7dbbda72

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 int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
44
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
45
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
46
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
47
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
48

    
49
    private static int mScreenWidth, mScreenHeight;
50
    private int mCurrentApiVersion;
51
    private PurchaseScreen mScreen;
52
    private int mObjectOrdinal;
53

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

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

    
64
      Bundle b = getIntent().getExtras();
65

    
66
      if(b != null) mObjectOrdinal = b.getInt("obj");
67

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

    
73
      hideNavigationBar();
74
      cutoutHack();
75
      setHeights();
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
    private void setViewHeight(int id, int height)
81
      {
82
      View view = findViewById(id);
83

    
84
      if( view!=null )
85
        {
86
        ViewGroup.LayoutParams params = view.getLayoutParams();
87
        params.height = height;
88
        view.setLayoutParams(params);
89
        }
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
// this does not include possible insets
94

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

    
102
      setViewHeight(R.id.upperBar           , ubarHeight);
103
      setViewHeight(R.id.lowerBar           , lbarHeight);
104
      setViewHeight(R.id.purchaseSurfaceView, viewHeight);
105
      setViewHeight(R.id.purchaseLayoutOne  , layHeight);
106
      setViewHeight(R.id.purchaseLayoutAll  , layHeight);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

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

    
119
        decorView.setSystemUiVisibility(FLAGS);
120

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// do not avoid cutouts
137

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

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

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

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

    
184
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
185
        {
186
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
187
        changeIfDifferent(object,mObjectOrdinal,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

    
203
    private void savePreferences()
204
      {
205
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
206
      SharedPreferences.Editor editor = preferences.edit();
207
      RubikScores scores = RubikScores.getInstance();
208

    
209
      scores.savePreferencesMinimal(editor);
210
      RubikObjectList.savePreferencesMinimal(editor);
211

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

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

    
218
    void OpenGLError()
219
      {
220
      RubikDialogError errDiag = new RubikDialogError();
221
      errDiag.show(getSupportFragmentManager(), null);
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

    
236
        control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
237
        }
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// PUBLIC API
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
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
261
      return view.getObjectControl();
262
      }
263

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

    
266
    PurchaseRenderer getRenderer()
267
      {
268
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
269
      return view.getRenderer();
270
      }
271

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

    
274
    void blockUI()
275
      {
276
      mScreen.blockUI();
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    public static int getDrawableSize()
282
      {
283
      if( mScreenHeight<1000 ) return 0;
284
      if( mScreenHeight<1600 ) return 1;
285
      if( mScreenHeight<1900 ) return 2;
286
      return 3;
287
      }
288

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

    
291
    public static int getDrawable(int small, int medium, int big, int huge)
292
      {
293
      int size = getDrawableSize();
294

    
295
      switch(size)
296
        {
297
        case 0 : return small;
298
        case 1 : return medium;
299
        case 2 : return big;
300
        default: return huge;
301
        }
302
      }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
    int getObjectPrice()
307
      {
308
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
309
        {
310
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
311
        return object==null ? 0 : object.getPrice();
312
        }
313

    
314
      return 0;
315
      }
316
}
(1-1/6)