Project

General

Profile

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

magiccube / src / main / java / org / distorted / purchase / PurchaseActivity.java @ 50e6c5d6

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.InitAssets;
28
import org.distorted.objectlib.main.ObjectControl;
29
import org.distorted.objectlib.main.TwistyObject;
30
import org.distorted.objects.RubikObject;
31
import org.distorted.objects.RubikObjectList;
32

    
33
import java.io.InputStream;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

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

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

    
67
      if(b != null) mObjectOrdinal = b.getInt("obj");
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
      setHeights();
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

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

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

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

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

    
120
        decorView.setSystemUiVisibility(FLAGS);
121

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// do not avoid cutouts
138

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

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

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

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

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

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

    
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

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

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

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// PUBLIC API
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

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

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

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

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

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

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

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

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