-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainActivity.cs
More file actions
151 lines (124 loc) · 5.59 KB
/
MainActivity.cs
File metadata and controls
151 lines (124 loc) · 5.59 KB
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
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
using AndroidX.AppCompat.App;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Core.View;
using AndroidX.Preference;
using Google.Android.Material.AppBar;
using System;
namespace com.companyname.SplashScreen1
{
[Activity(Label = "@string/app_name", MainLauncher = true) /* No theme set here */]
public class MainActivity : AppCompatActivity, IOnApplyWindowInsetsListener
{
private ISharedPreferences sharedPreferences;
private RadioGroup themeRadioGroup;
private UiNightMode uiNightMode;
private UiNightMode? previousUiNightMode = null;
#region OnCreate
protected override void OnCreate(Bundle savedInstanceState)
{
AndroidX.Core.SplashScreen.SplashScreen.InstallSplashScreen(this);
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
AppBarLayout appBar = FindViewById<AppBarLayout>(Resource.Id.app_bar);
themeRadioGroup = FindViewById<RadioGroup>(Resource.Id.theme);
SetSupportActionBar(toolbar);
WindowCompat.SetDecorFitsSystemWindows(this.Window, false);
ViewCompat.SetOnApplyWindowInsetsListener(appBar, this);
sharedPreferences = PreferenceManager.GetDefaultSharedPreferences(this);
uiNightMode = (UiNightMode)sharedPreferences.GetInt("night_mode", (int)UiNightMode.Auto);
if (Build.VERSION.SdkInt >= BuildVersionCodes.S)
{
themeRadioGroup.CheckedChange += ThemeRadioGroup_CheckedChange;
// Check the correct radio button (System Default, Light or Dark) of the themeRadioGroup based on the preference setting of nightMode
// There would be no radioButton checked without this check
// We want it normally to default to System Default.
int radioButtonId = GetCheckedRadioButtonId(uiNightMode);
if (themeRadioGroup.CheckedRadioButtonId != radioButtonId)
themeRadioGroup.Check(radioButtonId);
}
else
themeRadioGroup.Visibility = ViewStates.Gone;
}
#endregion
#region ThemeRadioGroup_CheckedChange
private void ThemeRadioGroup_CheckedChange(object sender, RadioGroup.CheckedChangeEventArgs e)
{
uiNightMode = GetNightModeOfCheckedRadioButton(e.CheckedId);
if (previousUiNightMode == null)
previousUiNightMode = uiNightMode;
if (previousUiNightMode != uiNightMode)
{
//Recreate(); // Don't appear to need this. Works with or without.
UpdateNightMode(uiNightMode);
}
}
#endregion
#region GetCheckedRadioButtonID
private int GetCheckedRadioButtonId(UiNightMode nightMode)
{
//UiNightMode.Auto 0
//UiNightMode.No 1
//UiNightMode.Yes 2
//UiNightMode.Custom 3;
return nightMode switch
{
UiNightMode.Auto => Resource.Id.theme_system,
UiNightMode.No => Resource.Id.theme_light,
UiNightMode.Yes => Resource.Id.theme_dark,
_ => Resource.Id.theme_system,
};
}
#endregion
#region GetNightModeOfCheckedRadioButton
private UiNightMode GetNightModeOfCheckedRadioButton(int checkedRadioButtonId)
{
return checkedRadioButtonId switch
{
Resource.Id.theme_system => UiNightMode.Auto,
Resource.Id.theme_light => UiNightMode.No,
Resource.Id.theme_dark => UiNightMode.Yes,
_ => throw new Exception("Unknown view Id: " + checkedRadioButtonId.ToString()),
};
}
#endregion
#region UpdateNightMode
private void UpdateNightMode(UiNightMode uiNightMode)
{
UiModeManager uiModeManager = GetSystemService("uimode") as UiModeManager;
if (Build.VERSION.SdkInt >= BuildVersionCodes.S)
uiModeManager?.SetApplicationNightMode((int)uiNightMode); // Only avaialable in Android 12 and above.
ISharedPreferencesEditor editor = sharedPreferences.Edit();
editor.PutInt("night_mode", (int)uiNightMode).Apply();
editor.Commit();
}
#endregion
#region OnApplyWindowInsets
public WindowInsetsCompat OnApplyWindowInsets(View v, WindowInsetsCompat insets)
{
if (v is AppBarLayout)
{
AndroidX.Core.Graphics.Insets statusBarsInsets = insets.GetInsets(WindowInsetsCompat.Type.StatusBars());
v.SetPadding(v.PaddingLeft, v.PaddingTop + statusBarsInsets.Top, v.PaddingRight, v.PaddingBottom);
}
return insets;
}
#endregion
#region OnBackPressed
public override void OnBackPressed()
{
base.OnBackPressed();
// Convenience for testing. Since this is a demo app, we kill the app when it is closed to ensure we see a cold start for every launch
// REMOVE for a normal app.
FinishAndRemoveTask();
Process.KillProcess(Process.MyPid());
}
#endregion
}
}