That fuck shit the fascists are using
1package org.tm.archive.util;
2
3import android.app.Activity;
4import android.content.Context;
5import android.content.res.Configuration;
6import android.os.Build;
7
8import androidx.annotation.NonNull;
9import androidx.annotation.StyleRes;
10import androidx.appcompat.app.AppCompatDelegate;
11
12import org.signal.core.util.logging.Log;
13import org.tm.archive.R;
14import org.tm.archive.keyvalue.SettingsValues.Theme;
15import org.tm.archive.keyvalue.SignalStore;
16
17public class DynamicTheme {
18
19 private static final String TAG = Log.tag(DynamicTheme.class);
20
21 private static int globalNightModeConfiguration;
22
23 private int onCreateNightModeConfiguration;
24
25 public void onCreate(@NonNull Activity activity) {
26 int previousGlobalConfiguration = globalNightModeConfiguration;
27
28 onCreateNightModeConfiguration = ConfigurationUtil.getNightModeConfiguration(activity);
29 globalNightModeConfiguration = onCreateNightModeConfiguration;
30
31 activity.setTheme(getTheme());
32
33 if (previousGlobalConfiguration != globalNightModeConfiguration) {
34 Log.d(TAG, "Previous night mode has changed previous: " + previousGlobalConfiguration + " now: " + globalNightModeConfiguration);
35 CachedInflater.from(activity).clear();
36 }
37 }
38
39 public void onResume(@NonNull Activity activity) {
40 if (onCreateNightModeConfiguration != ConfigurationUtil.getNightModeConfiguration(activity)) {
41 Log.d(TAG, "Create configuration different from current previous: " + onCreateNightModeConfiguration + " now: " + ConfigurationUtil.getNightModeConfiguration(activity));
42 CachedInflater.from(activity).clear();
43 }
44 }
45
46 protected @StyleRes int getTheme() {
47 return R.style.Signal_DayNight;
48 }
49
50 public static boolean systemThemeAvailable() {
51 return Build.VERSION.SDK_INT >= 29;
52 }
53
54 public static void setDefaultDayNightMode(@NonNull Context context) {
55 Theme theme = SignalStore.settings().getTheme();
56
57 if (theme == Theme.SYSTEM) {
58 Log.d(TAG, "Setting to follow system expecting: " + ConfigurationUtil.getNightModeConfiguration(context.getApplicationContext()));
59 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
60 } else if (DynamicTheme.isDarkTheme(context)) {
61 Log.d(TAG, "Setting to always night");
62 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
63 } else {
64 Log.d(TAG, "Setting to always day");
65 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
66 }
67
68 CachedInflater.from(context).clear();
69 }
70
71 /**
72 * Takes the system theme into account.
73 */
74 public static boolean isDarkTheme(@NonNull Context context) {
75 Theme theme = SignalStore.settings().getTheme();
76
77 if (theme == Theme.SYSTEM && systemThemeAvailable()) {
78 return isSystemInDarkTheme(context);
79 } else {
80 return theme == Theme.DARK;
81 }
82 }
83
84 private static boolean isSystemInDarkTheme(@NonNull Context context) {
85 return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
86 }
87}