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.main.R;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
|
29
|
public class BaseActivity extends AppCompatActivity
|
30
|
{
|
31
|
protected static final int THEME_GREY = 0;
|
32
|
protected static final int THEME_WHITE = 1;
|
33
|
protected static final int THEME_GREEN = 2;
|
34
|
|
35
|
public static final float RATIO_HID = 0.050f;
|
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 SharedPreferences mPreferences;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
@Override
|
52
|
protected void onCreate(Bundle savedState)
|
53
|
{
|
54
|
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
55
|
mCurrentTheme = mPreferences.getInt("theme",THEME_GREY);
|
56
|
|
57
|
switch(mCurrentTheme)
|
58
|
{
|
59
|
case THEME_WHITE : setTheme(R.style.WhiteTheme); break;
|
60
|
case THEME_GREEN : setTheme(R.style.GreenTheme); break;
|
61
|
default : setTheme(R.style.GreyTheme); break;
|
62
|
}
|
63
|
|
64
|
android.util.Log.e("D", "current theme: "+mCurrentTheme);
|
65
|
|
66
|
super.onCreate(savedState);
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
public void changeThemeTo(int theme)
|
72
|
{
|
73
|
mCurrentTheme = theme;
|
74
|
|
75
|
SharedPreferences.Editor editor = mPreferences.edit();
|
76
|
editor.putInt("theme", mCurrentTheme );
|
77
|
editor.apply();
|
78
|
|
79
|
recreate();
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
|
84
|
protected void getWindowWidth(Configuration conf)
|
85
|
{
|
86
|
int dpi = getResources().getDisplayMetrics().densityDpi;
|
87
|
float conv = ((float) dpi/DisplayMetrics.DENSITY_DEFAULT);
|
88
|
|
89
|
mScreenWidth = (int) (conf.screenWidthDp*conv + 0.5f);
|
90
|
mScreenHeight= (int) (conf.screenHeightDp*conv + 0.5f);
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
protected void computeScreenDimensions()
|
96
|
{
|
97
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
98
|
getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
|
99
|
mScreenWidth =displaymetrics.widthPixels;
|
100
|
mScreenHeight=displaymetrics.heightPixels;
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
|
105
|
protected void computeLowerBarHeight(float ratio)
|
106
|
{
|
107
|
int barHeight = (int)(mScreenHeight*ratio);
|
108
|
mHeightLowerBar = barHeight;
|
109
|
|
110
|
LinearLayout layout = findViewById(R.id.lowerBar);
|
111
|
ViewGroup.LayoutParams params = layout.getLayoutParams();
|
112
|
params.height = barHeight;
|
113
|
layout.setLayoutParams(params);
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
// this does not include possible insets
|
118
|
|
119
|
protected void computeUpperBarHeight(float ratio)
|
120
|
{
|
121
|
int barHeight = (int)(mScreenHeight*ratio);
|
122
|
mHeightUpperBar = barHeight;
|
123
|
|
124
|
LinearLayout layout = findViewById(R.id.upperBar);
|
125
|
ViewGroup.LayoutParams params = layout.getLayoutParams();
|
126
|
params.height = barHeight;
|
127
|
layout.setLayoutParams(params);
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
protected void hideNavigationBar()
|
133
|
{
|
134
|
mCurrentApiVersion = Build.VERSION.SDK_INT;
|
135
|
|
136
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
|
137
|
{
|
138
|
final View decorView = getWindow().getDecorView();
|
139
|
|
140
|
decorView.setSystemUiVisibility(FLAGS);
|
141
|
|
142
|
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
|
143
|
{
|
144
|
@Override
|
145
|
public void onSystemUiVisibilityChange(int visibility)
|
146
|
{
|
147
|
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
|
148
|
{
|
149
|
decorView.setSystemUiVisibility(FLAGS);
|
150
|
}
|
151
|
}
|
152
|
});
|
153
|
}
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
// do not avoid cutouts
|
158
|
|
159
|
protected void cutoutHack()
|
160
|
{
|
161
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
162
|
{
|
163
|
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
|
164
|
}
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
@Override
|
170
|
public void onWindowFocusChanged(boolean hasFocus)
|
171
|
{
|
172
|
super.onWindowFocusChanged(hasFocus);
|
173
|
|
174
|
if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
|
175
|
{
|
176
|
getWindow().getDecorView().setSystemUiVisibility(FLAGS);
|
177
|
}
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
public int getScreenWidthInPixels()
|
183
|
{
|
184
|
return mScreenWidth;
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
|
189
|
public int getScreenHeightInPixels()
|
190
|
{
|
191
|
return mScreenHeight;
|
192
|
}
|
193
|
|
194
|
}
|