Project

General

Profile

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

magiccube / src / main / java / org / distorted / purchase / PurchaseActivity.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.purchase;
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

    
20
import androidx.appcompat.app.AppCompatActivity;
21
import androidx.preference.PreferenceManager;
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.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

    
34
import java.io.InputStream;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class PurchaseActivity extends AppCompatActivity
39
{
40
    private static final int ACTIVITY_NUMBER = 3;
41
    private static final float RATIO_UBAR = 0.14f;
42
    private static final float RATIO_LBAR = 0.10f;
43
    private static final float RATIO_VIEW = 0.50f;
44
    public static final int FLAGS = MainActivity.FLAGS;
45

    
46
    private static int mScreenWidth, mScreenHeight;
47
    private int mCurrentApiVersion;
48
    private PurchaseScreen mScreen;
49
    private int mObjectOrdinal;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

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

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

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

    
70
      hideNavigationBar();
71
      cutoutHack();
72
      setHeights();
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
    private void setViewHeight(int id, int height)
78
      {
79
      View view = findViewById(id);
80

    
81
      if( view!=null )
82
        {
83
        ViewGroup.LayoutParams params = view.getLayoutParams();
84
        params.height = height;
85
        view.setLayoutParams(params);
86
        }
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// this does not include possible insets
91

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

    
99
      setViewHeight(R.id.upperBar           , ubarHeight);
100
      setViewHeight(R.id.lowerBar           , lbarHeight);
101
      setViewHeight(R.id.purchaseSurfaceView, viewHeight);
102
      setViewHeight(R.id.purchaseLayoutOne  , layHeight);
103
      setViewHeight(R.id.purchaseLayoutAll  , layHeight);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    private void hideNavigationBar()
109
      {
110
      mCurrentApiVersion = Build.VERSION.SDK_INT;
111

    
112
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
113
        {
114
        final View decorView = getWindow().getDecorView();
115

    
116
        decorView.setSystemUiVisibility(FLAGS);
117

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

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// do not avoid cutouts
134

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

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    @Override
146
    public void onWindowFocusChanged(boolean hasFocus)
147
      {
148
      super.onWindowFocusChanged(hasFocus);
149

    
150
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
151
        {
152
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
153
        }
154
      }
155

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

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

    
178
      if( mScreen==null ) mScreen = new PurchaseScreen();
179
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
180

    
181
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
182
        {
183
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
184
        changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
185
        }
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
    
190
    @Override
191
    protected void onDestroy() 
192
      {
193
      super.onDestroy();
194
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
195
      }
196

    
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

    
207
      boolean success = editor.commit();
208
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

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

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

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

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// PUBLIC API
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public int getScreenWidthInPixels()
239
      {
240
      return mScreenWidth;
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public int getScreenHeightInPixels()
246
      {
247
      return mScreenHeight;
248
      }
249

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

    
252
    public ObjectControl getControl()
253
      {
254
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
255
      return view.getObjectControl();
256
      }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
    PurchaseRenderer getRenderer()
261
      {
262
      PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
263
      return view.getRenderer();
264
      }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    void blockUI()
269
      {
270
      mScreen.blockUI();
271
      }
272

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

    
275
    int getObjectPrice()
276
      {
277
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
278
        {
279
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
280
        return object==null ? 0 : object.getPrice();
281
        }
282

    
283
      return 0;
284
      }
285
}
(1-1/6)