Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / BaseActivity.java @ b372b2cd

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