-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathActivityDesign.cs
More file actions
144 lines (117 loc) · 3.96 KB
/
Copy pathActivityDesign.cs
File metadata and controls
144 lines (117 loc) · 3.96 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
// This file is part of Keepass2Android, Copyright 2025 Philipp Crocoll.
//
// Keepass2Android is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Keepass2Android is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Linq;
using Android.App;
using Android.Content.Res;
using Android.OS;
using Android.Preferences;
using AndroidX.AppCompat.App;
using keepass2android;
namespace keepass2android
{
class ActivityDesign
{
private readonly Activity _activity;
private int? _currentThemeId;
private string _currentIconSet;
private readonly string _attributeTheme;
private bool _secureWindow;
public ActivityDesign(Activity activity)
{
_activity = activity;
try
{
var activityAttr = activity.GetType().GetCustomAttributes(false).Where(
x => x is Android.App.ActivityAttribute
).Cast<ActivityAttribute>().First();
_attributeTheme = activityAttr.Theme;
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
}
private bool SecureWindowPref()
{
return (PreferenceManager.GetDefaultSharedPreferences(_activity).GetBoolean(
_activity.GetString(Resource.String.ViewDatabaseSecure_key), true));
}
public void ApplyTheme()
{
_currentThemeId = NightModePreference;
AppCompatDelegate.DefaultNightMode = _currentThemeId.Value;
_secureWindow = SecureWindowPref();
_currentIconSet = PreferenceManager.GetDefaultSharedPreferences(_activity)
.GetString("IconSetKey", _activity.PackageName);
}
public void ReapplyTheme()
{
int newTheme = NightModePreference;
if (newTheme != _currentThemeId)
{
Kp2aLog.Log("recreating due to theme change.");
_activity.Recreate();
_activity.Finish(); //withouth this, we'll have two GroupActivities on the backstack afterwards
return;
}
if (PreferenceManager.GetDefaultSharedPreferences(_activity)
.GetString("IconSetKey", _activity.PackageName) != _currentIconSet)
{
Kp2aLog.Log("recreating due to icon set change.");
_activity.Recreate();
return;
}
if (SecureWindowPref() != _secureWindow)
{
Kp2aLog.Log("recreating due to secure window change.");
_activity.Recreate();
return;
}
}
public int NightModePreference
{
get
{
var prefs = PreferenceManager.GetDefaultSharedPreferences(_activity);
string design = prefs.GetString(_activity.GetString(Resource.String.design_key), _activity.GetString(Resource.String.design_default));
return design switch
{
"MaterialYou" => AppCompatDelegate.ModeNightFollowSystem,
"System" => AppCompatDelegate.ModeNightFollowSystem,
"Light" => AppCompatDelegate.ModeNightNo,
"Dark" => AppCompatDelegate.ModeNightYes,
_ => AppCompatDelegate.ModeNightFollowSystem,
};
}
}
public bool IsMaterialYouEnabled
{
get
{
var prefs = PreferenceManager.GetDefaultSharedPreferences(_activity);
string design = prefs.GetString(
_activity.GetString(Resource.String.design_key),
_activity.GetString(Resource.String.design_default)
);
return design == "MaterialYou";
}
}
public void ApplyDialogTheme()
{
_activity.SetTheme(Resource.Style.Kp2aTheme_Dialog);
}
}
}