1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2024 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.helpers;
|
11
|
|
12
|
import android.content.SharedPreferences;
|
13
|
import android.content.res.Configuration;
|
14
|
import android.os.Build;
|
15
|
import android.os.Bundle;
|
16
|
import android.util.DisplayMetrics;
|
17
|
import android.view.View;
|
18
|
import android.view.ViewGroup;
|
19
|
import android.view.WindowManager;
|
20
|
import android.widget.LinearLayout;
|
21
|
|
22
|
import androidx.appcompat.app.AppCompatActivity;
|
23
|
import androidx.preference.PreferenceManager;
|
24
|
|
25
|
import org.distorted.library.main.DistortedFramebuffer;
|
26
|
import org.distorted.main.R;
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
|
30
|
public class BaseActivity extends AppCompatActivity
|
31
|
{
|
32
|
protected static final int THEME_GREY = 0;
|
33
|
protected static final int THEME_WHITE = 1;
|
34
|
protected static final int THEME_GREEN = 2;
|
35
|
|
36
|
public static final float RATIO_BAR = 0.080f;
|
37
|
public static final int FLAGS = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
38
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
39
|
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
40
|
| View.SYSTEM_UI_FLAG_FULLSCREEN
|
41
|
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
|
42
|
|
43
|
private int mCurrentApiVersion;
|
44
|
protected int mCurrentTheme;
|
45
|
protected int mScreenWidth, mScreenHeight;
|
46
|
protected int mHeightLowerBar, mHeightUpperBar;
|
47
|
protected float mDensity;
|
48
|
protected int mVeryDarkC, mDarkC, mNormalC, mMediumC, mLightC, mPassedC;
|
49
|
protected int mDarkT, mLightT, mSelectC;
|
50
|
protected SharedPreferences mPreferences;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
@Override
|
55
|
protected void onCreate(Bundle savedState)
|
56
|
{
|
57
|
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
58
|
mCurrentTheme = mPreferences.getInt("theme",THEME_GREY);
|
59
|
|
60
|
mDensity = getResources().getDisplayMetrics().density;
|
61
|
|
62
|
switch(mCurrentTheme)
|
63
|
{
|
64
|
case THEME_WHITE : setTheme(R.style.WhiteTheme);
|
65
|
mVeryDarkC= R.color.veryDarkWhite;
|
66
|
mDarkC = R.color.darkWhite;
|
67
|
mNormalC = R.color.normalWhite;
|
68
|
mMediumC = R.color.mediumWhite;
|
69
|
mLightC = R.color.lightWhite;
|
70
|
mPassedC = R.color.passedWhite;
|
71
|
mDarkT = R.color.transDWhite;
|
72
|
mLightT = R.color.transLWhite;
|
73
|
mSelectC = R.color.selectWhite;
|
74
|
break;
|
75
|
case THEME_GREEN : setTheme(R.style.GreenTheme);
|
76
|
mVeryDarkC= R.color.veryDarkGreen;
|
77
|
mDarkC = R.color.darkGreen;
|
78
|
mNormalC = R.color.normalGreen;
|
79
|
mMediumC = R.color.mediumGreen;
|
80
|
mLightC = R.color.lightGreen;
|
81
|
mPassedC = R.color.passedGreen;
|
82
|
mDarkT = R.color.transDGreen;
|
83
|
mLightT = R.color.transLGreen;
|
84
|
mSelectC = R.color.selectGreen;
|
85
|
break;
|
86
|
default : setTheme(R.style.GreyTheme);
|
87
|
mVeryDarkC= R.color.veryDarkGrey;
|
88
|
mDarkC = R.color.darkGrey;
|
89
|
mNormalC = R.color.normalGrey;
|
90
|
mMediumC = R.color.mediumGrey;
|
91
|
mLightC = R.color.lightGrey;
|
92
|
mPassedC = R.color.passedGrey;
|
93
|
mDarkT = R.color.transDGrey;
|
94
|
mLightT = R.color.transLGrey;
|
95
|
mSelectC = R.color.selectGrey;
|
96
|
break;
|
97
|
}
|
98
|
|
99
|
super.onCreate(savedState);
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
|
104
|
public int getVeryDarkColor()
|
105
|
{
|
106
|
return mVeryDarkC;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
public int getDarkColor()
|
112
|
{
|
113
|
return mDarkC;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
public int getNormalColor()
|
119
|
{
|
120
|
return mNormalC;
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public int getMediumColor()
|
126
|
{
|
127
|
return mMediumC;
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
public int getLightColor()
|
133
|
{
|
134
|
return mLightC;
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
public int getDarkTrans()
|
140
|
{
|
141
|
return mDarkT;
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
public int getLightTrans()
|
147
|
{
|
148
|
return mLightT;
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
public int getSelectedColor()
|
154
|
{
|
155
|
return mSelectC;
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void setUpBackgroundColor(DistortedFramebuffer df)
|
161
|
{
|
162
|
int color = getResources().getColor(mLightC);
|
163
|
int r = (color>>16) & 0xff;
|
164
|
int g = (color>> 8) & 0xff;
|
165
|
int b = (color ) & 0xff;
|
166
|
|
167
|
float rC = (float)r/255;
|
168
|
float gC = (float)g/255;
|
169
|
float bC = (float)b/255;
|
170
|
|
171
|
df.glClearColor(rC, gC, bC, 1.0f);
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
|
176
|
public void changeThemeTo(int theme)
|
177
|
{
|
178
|
mCurrentTheme = theme;
|
179
|
|
180
|
SharedPreferences.Editor editor = mPreferences.edit();
|
181
|
editor.putInt("theme", mCurrentTheme );
|
182
|
editor.apply();
|
183
|
|
184
|
recreate();
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
|
189
|
protected void getWindowWidth(Configuration conf)
|
190
|
{
|
191
|
int dpi = getResources().getDisplayMetrics().densityDpi;
|
192
|
float conv = ((float) dpi/DisplayMetrics.DENSITY_DEFAULT);
|
193
|
|
194
|
mScreenWidth = (int) (conf.screenWidthDp*conv + 0.5f);
|
195
|
mScreenHeight= (int) (conf.screenHeightDp*conv + 0.5f);
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
protected void computeScreenDimensions()
|
201
|
{
|
202
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
203
|
getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
|
204
|
mScreenWidth =displaymetrics.widthPixels;
|
205
|
mScreenHeight=displaymetrics.heightPixels;
|
206
|
}
|
207
|
|
208
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209
|
|
210
|
protected void computeLowerBarHeight(float ratio)
|
211
|
{
|
212
|
int barHeight = (int)(mScreenHeight*ratio);
|
213
|
mHeightLowerBar = barHeight;
|
214
|
|
215
|
LinearLayout layout = findViewById(R.id.lowerBar);
|
216
|
ViewGroup.LayoutParams params = layout.getLayoutParams();
|
217
|
params.height = barHeight;
|
218
|
layout.setLayoutParams(params);
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
// this does not include possible insets
|
223
|
|
224
|
protected void computeUpperBarHeight(float ratio)
|
225
|
{
|
226
|
int barHeight = (int)(mScreenHeight*ratio);
|
227
|
mHeightUpperBar = barHeight;
|
228
|
|
229
|
LinearLayout layout = findViewById(R.id.upperBar);
|
230
|
ViewGroup.LayoutParams params = layout.getLayoutParams();
|
231
|
params.height = barHeight;
|
232
|
layout.setLayoutParams(params);
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
protected void hideNavigationBar()
|
238
|
{
|
239
|
mCurrentApiVersion = Build.VERSION.SDK_INT;
|
240
|
|
241
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
|
242
|
{
|
243
|
final View decorView = getWindow().getDecorView();
|
244
|
|
245
|
decorView.setSystemUiVisibility(FLAGS);
|
246
|
|
247
|
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
|
248
|
{
|
249
|
@Override
|
250
|
public void onSystemUiVisibilityChange(int visibility)
|
251
|
{
|
252
|
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
|
253
|
{
|
254
|
decorView.setSystemUiVisibility(FLAGS);
|
255
|
}
|
256
|
}
|
257
|
});
|
258
|
}
|
259
|
}
|
260
|
|
261
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
262
|
// do not avoid cutouts
|
263
|
|
264
|
protected void cutoutHack()
|
265
|
{
|
266
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
267
|
{
|
268
|
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
|
269
|
}
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
|
274
|
@Override
|
275
|
public void onWindowFocusChanged(boolean hasFocus)
|
276
|
{
|
277
|
super.onWindowFocusChanged(hasFocus);
|
278
|
|
279
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
|
280
|
{
|
281
|
getWindow().getDecorView().setSystemUiVisibility(FLAGS);
|
282
|
}
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
public int getScreenWidthInPixels()
|
288
|
{
|
289
|
return mScreenWidth;
|
290
|
}
|
291
|
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
public int getScreenHeightInPixels()
|
295
|
{
|
296
|
return mScreenHeight;
|
297
|
}
|
298
|
|
299
|
}
|