-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseActivity_old.cs
232 lines (189 loc) · 8.96 KB
/
BaseActivity_old.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.OS;
using AndroidX.AppCompat.App;
using AndroidX.Preference;
using System;
namespace com.companyname.NavigationGraph3
{
[Activity(Label = "BaseActivity")]
public class BaseActivity_old : AppCompatActivity
{
protected ISharedPreferences sharedPreferences;
private bool requestedNightMode; // changed from protected
private string requestedColorTheme;
private string requestedSystemTheme;
//private UiModeManager uiModeManager;
#region OnCreate
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
sharedPreferences = PreferenceManager.GetDefaultSharedPreferences(this);
requestedColorTheme = sharedPreferences.GetString("colorThemeValue", "1");
requestedNightMode = sharedPreferences.GetBoolean("darkTheme", false);
requestedSystemTheme = sharedPreferences.GetString("systemThemeValue", "1");
//SetDefaultNightMode(requestedNightMode);
SetDefaultNightMode_1(requestedNightMode);
//SetDefaultNightMode_2(requestedSystemTheme);
SetAppTheme(requestedColorTheme);
}
#endregion
#region SetAppTheme
private void SetAppTheme(string requestedColorTheme)
{
if (requestedColorTheme == "1")
SetTheme(Resource.Style.Theme_NavigationGraph_RedBmw);
else if (requestedColorTheme == "2")
SetTheme(Resource.Style.Theme_NavigationGraph_BlueAudi);
else if (requestedColorTheme == "3")
SetTheme(Resource.Style.Theme_NavigationGraph_GreenBmw);
}
#endregion
#region SetDefaultNightMode // This looks like the one I was looking for last night
private void SetDefaultNightMode(bool requestedNightMode)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Q) //Android 10 and above
{
// This works via the quick settings menu on Android 10 and above, correct back ground colour for both Light and Dark.
// However not via Settings - therefore need to disable in Settings for Android 10 and above
int defaultNightMode = AppCompatDelegate.DefaultNightMode;
switch (defaultNightMode)
{
case AppCompatDelegate.ModeNightNo:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
break;
case AppCompatDelegate.ModeNightYes:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
break;
case AppCompatDelegate.ModeNightUnspecified:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightUnspecified; //defaultNightMode; //
break;
case AppCompatDelegate.ModeNightFollowSystem:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightFollowSystem;
break;
}
}
else // Handle devices < Android 10
{
if (requestedNightMode)
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
else
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
}
}
#endregion
#region SetDefaultNightMode_1
private void SetDefaultNightMode_1(bool requestedNightMode)
{
// This works well from Settings, but only has a white background colour for both Dark and Light Modes
bool isNightModeActive = IsNightModeActive();
int mode = AppCompatDelegate.DefaultNightMode;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Q) //Android 10 and above
{
if (!requestedNightMode & !isNightModeActive)
return;
else if (requestedNightMode & !isNightModeActive)
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
else if (!requestedNightMode & isNightModeActive)
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
}
else
{
if (requestedNightMode)
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
else
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
}
}
#endregion
#region SetDefaultNightMode_2
private void SetDefaultNightMode_2(string currentSystemTheme )
{
bool isNightModeActive = IsNightModeActive();
if (currentSystemTheme == "1" && !isNightModeActive) // System Default
return;
else if (currentSystemTheme == "1" && isNightModeActive)
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
else if (currentSystemTheme == "2" && isNightModeActive) // Light
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
else if (currentSystemTheme == "2" && !isNightModeActive)
return;
else if (currentSystemTheme == "3" && !isNightModeActive) // Dark
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
else if (currentSystemTheme == "3" && isNightModeActive)
return;
}
#endregion
#region OnResume
protected override void OnResume()
{
// I don't think we even need this OnResume, as I've never seen Activity.Recreate being called from here.
base.OnResume();
string selectedTheme = sharedPreferences.GetString("colorThemeValue", "1");
if (requestedColorTheme != selectedTheme)
Recreate();
}
#endregion
#region IsNightModeActive
private bool IsNightModeActive()
{
// Notes
// ModeNightUnspecified = -100
// ModeNightFollowSystem = -1
// ModeNightNo = 1
// ModeNightYes = 2
// UiMode.Undefined = 0
// UiMode.NightNo = 0x10
// UiMode.NightYes = 0x20
// UiMode.NightMask = 48
//int defaultNightMode = AppCompatDelegate.DefaultNightMode;
//if (defaultNightMode == AppCompatDelegate.ModeNightYes)
// return true;
//if (defaultNightMode == AppCompatDelegate.ModeNightNo)
// return false;
return (int)(Resources.Configuration.UiMode & UiMode.NightMask) switch
{
(int)UiMode.NightYes => true,
(int)UiMode.NightNo => false,
(int)UiMode.NightUndefined => false,
_ => false,
};
}
#endregion
#region GetRequestSystemTheme
private AppCompatDelegate GetRequestedSystemTheme(string currentSystemTheme)
{
if (currentSystemTheme == "1")
return (AppCompatDelegate)AppCompatDelegate.ModeNightUnspecified;
else if (currentSystemTheme == "2")
return (AppCompatDelegate)AppCompatDelegate.ModeNightNo;
else if (currentSystemTheme == "3")
return (AppCompatDelegate)AppCompatDelegate.ModeNightYes;
else
return (AppCompatDelegate)AppCompatDelegate.ModeNightFollowSystem;
}
#endregion
}
}
//when(resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK)
//{
// Configuration.UI_MODE_NIGHT_YES->
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
// Configuration.UI_MODE_NIGHT_NO->
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
//int existingMode = AppCompatDelegate.DefaultNightMode;
//if (nightModeActive)
// AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
//else if (existingMode == AppCompatDelegate.ModeNightFollowSystem)
// AppCompatDelegate.DefaultNightMode = existingMode;
//else
// AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
//mode = AppCompatDelegate.getDefaultNightMode();
//SharedPreferences preferencesnight = android.preference.PreferenceManager.getDefaultSharedPreferences(MyApplication.this);
//if (preferencesnight.getBoolean(KEY_SIGNATURE, false))
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
//else if (mode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
// AppCompatDelegate.setDefaultNightMode(mode);
//else
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);