Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / BaseActivity.java @ 236cc1c1

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 float mDensity;
48
    protected SharedPreferences mPreferences;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
    @Override
53
    protected void onCreate(Bundle savedState)
54
      {
55
      mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
56
      mCurrentTheme = mPreferences.getInt("theme",THEME_GREY);
57

    
58
      mDensity = getResources().getDisplayMetrics().density;
59

    
60
      switch(mCurrentTheme)
61
        {
62
        case THEME_WHITE : setTheme(R.style.WhiteTheme); break;
63
        case THEME_GREEN : setTheme(R.style.GreenTheme); break;
64
        default          : setTheme(R.style.GreyTheme);  break;
65
        }
66

    
67
      super.onCreate(savedState);
68
      }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
    public void changeThemeTo(int theme)
73
      {
74
      mCurrentTheme = theme;
75

    
76
      SharedPreferences.Editor editor = mPreferences.edit();
77
      editor.putInt("theme", mCurrentTheme );
78
      editor.apply();
79

    
80
      recreate();
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
    protected void getWindowWidth(Configuration conf)
86
      {
87
      int dpi = getResources().getDisplayMetrics().densityDpi;
88
      float conv = ((float) dpi/DisplayMetrics.DENSITY_DEFAULT);
89

    
90
      mScreenWidth = (int) (conf.screenWidthDp*conv + 0.5f);
91
      mScreenHeight= (int) (conf.screenHeightDp*conv + 0.5f);
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
    protected void computeScreenDimensions()
97
      {
98
      DisplayMetrics displaymetrics = new DisplayMetrics();
99
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
100
      mScreenWidth =displaymetrics.widthPixels;
101
      mScreenHeight=displaymetrics.heightPixels;
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
    protected void computeLowerBarHeight(float ratio)
107
      {
108
      int barHeight = (int)(mScreenHeight*ratio);
109
      mHeightLowerBar = barHeight;
110

    
111
      LinearLayout layout = findViewById(R.id.lowerBar);
112
      ViewGroup.LayoutParams params = layout.getLayoutParams();
113
      params.height = barHeight;
114
      layout.setLayoutParams(params);
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// this does not include possible insets
119

    
120
    protected void computeUpperBarHeight(float ratio)
121
      {
122
      int barHeight = (int)(mScreenHeight*ratio);
123
      mHeightUpperBar = barHeight;
124

    
125
      LinearLayout layout = findViewById(R.id.upperBar);
126
      ViewGroup.LayoutParams params = layout.getLayoutParams();
127
      params.height = barHeight;
128
      layout.setLayoutParams(params);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
    protected void hideNavigationBar()
134
      {
135
      mCurrentApiVersion = Build.VERSION.SDK_INT;
136

    
137
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
138
        {
139
        final View decorView = getWindow().getDecorView();
140

    
141
        decorView.setSystemUiVisibility(FLAGS);
142

    
143
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
144
          {
145
          @Override
146
          public void onSystemUiVisibilityChange(int visibility)
147
            {
148
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
149
              {
150
              decorView.setSystemUiVisibility(FLAGS);
151
              }
152
            }
153
          });
154
        }
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// do not avoid cutouts
159

    
160
    protected void cutoutHack()
161
      {
162
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
163
        {
164
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
165
        }
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
    @Override
171
    public void onWindowFocusChanged(boolean hasFocus)
172
      {
173
      super.onWindowFocusChanged(hasFocus);
174

    
175
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
176
        {
177
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
178
        }
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
    public int getScreenWidthInPixels()
184
      {
185
      return mScreenWidth;
186
      }
187

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

    
190
    public int getScreenHeightInPixels()
191
      {
192
      return mScreenHeight;
193
      }
194

    
195
}
(1-1/6)