Project

General

Profile

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

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

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_GREEN = 0;
33
    protected static final int THEME_BLUE  = 1;
34
    protected static final int THEME_GREY  = 2;
35
    protected static final int THEME_PINK  = 3;
36
    protected static final int THEME_ORANGE= 4;
37

    
38
    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
    protected int mCurrentTheme;
47
    protected int mScreenWidth, mScreenHeight;
48
    protected int mHeightLowerBar, mHeightUpperBar;
49
    protected float mDensity;
50
    protected int mVeryDarkC, mDarkC, mNormalC, mMediumC, mLightC, mPassedC;
51
    protected int mDarkT, mLightT, mSelectC;
52
    protected SharedPreferences mPreferences;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
    @Override
57
    protected void onCreate(Bundle savedState)
58
      {
59
      mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
60
      mCurrentTheme = mPreferences.getInt("theme",THEME_GREEN);
61

    
62
      mDensity = getResources().getDisplayMetrics().density;
63

    
64
      switch(mCurrentTheme)
65
        {
66
        case THEME_GREEN : setTheme(R.style.GreenTheme);
67
                           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
                           mDarkT    = R.color.transDGreen;
74
                           mLightT   = R.color.transLGreen;
75
                           mSelectC  = R.color.selectGreen;
76
                           break;
77
        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
        case THEME_GREY  : setTheme(R.style.GreyTheme);
89
                           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
                           mDarkT    = R.color.transDGrey;
96
                           mLightT   = R.color.transLGrey;
97
                           mSelectC  = R.color.selectGrey;
98
                           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
        }
123

    
124
      super.onCreate(savedState);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    public int getVeryDarkColor() { return mVeryDarkC; }
130
    public int getDarkColor()     { return mDarkC; }
131
    public int getNormalColor()   { return mNormalC; }
132
    public int getMediumColor()   { return mMediumC; }
133
    public int getLightColor()    { return mLightC; }
134
    public int getDarkTrans()     { return mDarkT; }
135
    public int getLightTrans()    { return mLightT; }
136
    public int getSelectedColor() { return mSelectC; }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
    public void setUpBackgroundColor(DistortedFramebuffer df)
141
      {
142
      int color = getResources().getColor(mLightC);
143
      int r = (color>>16) & 0xff;
144
      int g = (color>> 8) & 0xff;
145
      int b = (color    ) & 0xff;
146

    
147
      float rC = (float)r/255;
148
      float gC = (float)g/255;
149
      float bC = (float)b/255;
150

    
151
      df.glClearColor(rC, gC, bC, 1.0f);
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    public void changeThemeTo(int theme)
157
      {
158
      mCurrentTheme = theme;
159

    
160
      SharedPreferences.Editor editor = mPreferences.edit();
161
      editor.putInt("theme", mCurrentTheme );
162
      editor.apply();
163

    
164
      recreate();
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
    protected void getWindowWidth(Configuration conf)
170
      {
171
      int dpi = getResources().getDisplayMetrics().densityDpi;
172
      float conv = ((float) dpi/DisplayMetrics.DENSITY_DEFAULT);
173

    
174
      mScreenWidth = (int) (conf.screenWidthDp*conv + 0.5f);
175
      mScreenHeight= (int) (conf.screenHeightDp*conv + 0.5f);
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    protected void computeScreenDimensions()
181
      {
182
      DisplayMetrics displaymetrics = new DisplayMetrics();
183
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
184
      mScreenWidth =displaymetrics.widthPixels;
185
      mScreenHeight=displaymetrics.heightPixels;
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    protected void computeLowerBarHeight(float ratio)
191
      {
192
      int barHeight = (int)(mScreenHeight*ratio);
193
      mHeightLowerBar = barHeight;
194

    
195
      LinearLayout layout = findViewById(R.id.lowerBar);
196
      ViewGroup.LayoutParams params = layout.getLayoutParams();
197
      params.height = barHeight;
198
      layout.setLayoutParams(params);
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// this does not include possible insets
203

    
204
    protected void computeUpperBarHeight(float ratio)
205
      {
206
      int barHeight = (int)(mScreenHeight*ratio);
207
      mHeightUpperBar = barHeight;
208

    
209
      LinearLayout layout = findViewById(R.id.upperBar);
210
      ViewGroup.LayoutParams params = layout.getLayoutParams();
211
      params.height = barHeight;
212
      layout.setLayoutParams(params);
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    protected void hideNavigationBar()
218
      {
219
      mCurrentApiVersion = Build.VERSION.SDK_INT;
220

    
221
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
222
        {
223
        final View decorView = getWindow().getDecorView();
224

    
225
        decorView.setSystemUiVisibility(FLAGS);
226

    
227
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
228
          {
229
          @Override
230
          public void onSystemUiVisibilityChange(int visibility)
231
            {
232
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
233
              {
234
              decorView.setSystemUiVisibility(FLAGS);
235
              }
236
            }
237
          });
238
        }
239
      }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// do not avoid cutouts
243

    
244
    protected void cutoutHack()
245
      {
246
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
247
        {
248
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
249
        }
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    @Override
255
    public void onWindowFocusChanged(boolean hasFocus)
256
      {
257
      super.onWindowFocusChanged(hasFocus);
258

    
259
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
260
        {
261
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
262
        }
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    public int getScreenWidthInPixels()
268
      {
269
      return mScreenWidth;
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
    public int getScreenHeightInPixels()
275
      {
276
      return mScreenHeight;
277
      }
278
}
(1-1/10)