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.Bundle;
|
14
|
import android.view.View;
|
15
|
import android.view.ViewGroup;
|
16
|
|
17
|
import androidx.preference.PreferenceManager;
|
18
|
|
19
|
import org.distorted.dialogs.RubikDialogError;
|
20
|
import org.distorted.external.RubikScores;
|
21
|
import org.distorted.helpers.BaseActivity;
|
22
|
import org.distorted.library.main.DistortedLibrary;
|
23
|
import org.distorted.main.R;
|
24
|
import org.distorted.objectlib.main.InitAssets;
|
25
|
import org.distorted.objectlib.main.ObjectControl;
|
26
|
import org.distorted.objectlib.main.TwistyObject;
|
27
|
import org.distorted.objects.RubikObject;
|
28
|
import org.distorted.objects.RubikObjectList;
|
29
|
|
30
|
import java.io.InputStream;
|
31
|
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33
|
|
34
|
public class PurchaseActivity extends BaseActivity
|
35
|
{
|
36
|
private static final int ACTIVITY_NUMBER = 3;
|
37
|
private static final float RATIO_UBAR = 0.14f;
|
38
|
private static final float RATIO_LBAR = 0.10f;
|
39
|
private static final float RATIO_VIEW = 0.50f;
|
40
|
|
41
|
private PurchaseScreen mScreen;
|
42
|
private int mObjectOrdinal;
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
|
46
|
@Override
|
47
|
protected void onCreate(Bundle savedState)
|
48
|
{
|
49
|
super.onCreate(savedState);
|
50
|
DistortedLibrary.onCreate(ACTIVITY_NUMBER);
|
51
|
setContentView(R.layout.purchase);
|
52
|
|
53
|
Bundle b = getIntent().getExtras();
|
54
|
if(b != null) mObjectOrdinal = b.getInt("obj");
|
55
|
|
56
|
computeScreenDimensions();
|
57
|
hideNavigationBar();
|
58
|
cutoutHack();
|
59
|
setHeights();
|
60
|
}
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
private void setViewHeight(int id, int height)
|
65
|
{
|
66
|
View view = findViewById(id);
|
67
|
|
68
|
if( view!=null )
|
69
|
{
|
70
|
ViewGroup.LayoutParams params = view.getLayoutParams();
|
71
|
params.height = height;
|
72
|
view.setLayoutParams(params);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
// this does not include possible insets
|
78
|
|
79
|
private void setHeights()
|
80
|
{
|
81
|
int ubarHeight= (int)(mScreenHeight*RATIO_UBAR);
|
82
|
int lbarHeight= (int)(mScreenHeight*RATIO_LBAR);
|
83
|
int viewHeight= (int)(mScreenHeight*RATIO_VIEW);
|
84
|
int layHeight = (int)(mScreenHeight*(1-RATIO_UBAR-RATIO_LBAR-RATIO_VIEW)*0.5f);
|
85
|
|
86
|
setViewHeight(R.id.upperBar , ubarHeight);
|
87
|
setViewHeight(R.id.lowerBar , lbarHeight);
|
88
|
setViewHeight(R.id.purchaseSurfaceView, viewHeight);
|
89
|
setViewHeight(R.id.purchaseLayoutOne , layHeight);
|
90
|
setViewHeight(R.id.purchaseLayoutAll , layHeight);
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
@Override
|
96
|
protected void onPause()
|
97
|
{
|
98
|
super.onPause();
|
99
|
PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
|
100
|
view.onPause();
|
101
|
DistortedLibrary.onPause(ACTIVITY_NUMBER);
|
102
|
savePreferences();
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
@Override
|
108
|
protected void onResume()
|
109
|
{
|
110
|
super.onResume();
|
111
|
DistortedLibrary.onResume(ACTIVITY_NUMBER);
|
112
|
PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
|
113
|
view.onResume();
|
114
|
|
115
|
if( mScreen==null ) mScreen = new PurchaseScreen();
|
116
|
mScreen.onAttachedToWindow(this,mObjectOrdinal);
|
117
|
|
118
|
if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
|
119
|
{
|
120
|
RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
|
121
|
changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
|
122
|
}
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
@Override
|
128
|
protected void onDestroy()
|
129
|
{
|
130
|
super.onDestroy();
|
131
|
DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
private void savePreferences()
|
137
|
{
|
138
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
139
|
SharedPreferences.Editor editor = preferences.edit();
|
140
|
RubikScores scores = RubikScores.getInstance();
|
141
|
scores.savePreferencesMinimal(editor);
|
142
|
|
143
|
boolean success = editor.commit();
|
144
|
if( !success ) android.util.Log.e("D", "Failed to save preferences");
|
145
|
}
|
146
|
|
147
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148
|
|
149
|
void OpenGLError()
|
150
|
{
|
151
|
RubikDialogError errDiag = new RubikDialogError();
|
152
|
errDiag.show(getSupportFragmentManager(), null);
|
153
|
}
|
154
|
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
|
157
|
private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
|
158
|
{
|
159
|
if( object!=null )
|
160
|
{
|
161
|
int iconMode = TwistyObject.MODE_NORM;
|
162
|
InputStream jsonStream = object.getObjectStream(this);
|
163
|
InputStream meshStream = object.getMeshStream(this);
|
164
|
String name = object.getUpperName();
|
165
|
InitAssets asset = new InitAssets(jsonStream,meshStream,null);
|
166
|
control.changeIfDifferent(ordinal,name,iconMode,asset);
|
167
|
}
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
// PUBLIC API
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
public ObjectControl getControl()
|
175
|
{
|
176
|
PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
|
177
|
return view.getObjectControl();
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
PurchaseRenderer getRenderer()
|
183
|
{
|
184
|
PurchaseSurfaceView view = findViewById(R.id.purchaseSurfaceView);
|
185
|
return view.getRenderer();
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
void blockUI()
|
191
|
{
|
192
|
mScreen.blockUI();
|
193
|
}
|
194
|
|
195
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196
|
|
197
|
int getObjectPrice()
|
198
|
{
|
199
|
if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
|
200
|
{
|
201
|
RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
|
202
|
return object==null ? 0 : object.getPrice();
|
203
|
}
|
204
|
|
205
|
return 0;
|
206
|
}
|
207
|
}
|