Project

General

Profile

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

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

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