1package eu.siacs.conversations;
2
3import android.app.Application;
4import android.content.Context;
5import android.content.SharedPreferences;
6import android.preference.PreferenceManager;
7
8import androidx.appcompat.app.AppCompatDelegate;
9
10import com.google.android.material.color.DynamicColors;
11import com.google.android.material.color.DynamicColorsOptions;
12
13import eu.siacs.conversations.utils.ExceptionHelper;
14import eu.siacs.conversations.utils.ThemeHelper;
15
16public class Conversations extends Application {
17
18 @Override
19 public void onCreate() {
20 super.onCreate();
21 ExceptionHelper.init(getApplicationContext());
22 applyThemeSettings();
23 }
24
25 public void applyThemeSettings() {
26 final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
27 if (sharedPreferences == null) {
28 return;
29 }
30 applyThemeSettings(sharedPreferences);
31 }
32
33 private void applyThemeSettings(final SharedPreferences sharedPreferences) {
34 AppCompatDelegate.setDefaultNightMode(getDesiredNightMode(this, sharedPreferences));
35 var dynamicColorsOptions =
36 new DynamicColorsOptions.Builder()
37 .setPrecondition((activity, t) -> isDynamicColorsDesired(activity))
38 .build();
39 DynamicColors.applyToActivitiesIfAvailable(this, dynamicColorsOptions);
40 }
41
42 public static int getDesiredNightMode(final Context context) {
43 final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
44 if (sharedPreferences == null) {
45 return AppCompatDelegate.getDefaultNightMode();
46 }
47 return getDesiredNightMode(context, sharedPreferences);
48 }
49
50 public static boolean isDynamicColorsDesired(final Context context) {
51 final var preferences = PreferenceManager.getDefaultSharedPreferences(context);
52 if (isCustomColorsDesired(context)) return false;
53 return preferences.getBoolean(AppSettings.DYNAMIC_COLORS, false);
54 }
55
56 public static boolean isCustomColorsDesired(final Context context) {
57 final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
58 final String theme =
59 sharedPreferences.getString(AppSettings.THEME, context.getString(R.string.theme));
60 return "custom".equals(theme);
61 }
62
63 private static int getDesiredNightMode(
64 final Context context, final SharedPreferences sharedPreferences) {
65 var theme =
66 sharedPreferences.getString(AppSettings.THEME, context.getString(R.string.theme));
67
68 // Migrate old themes to equivalent custom
69 if ("oledblack".equals(theme)) {
70 theme = "custom";
71 final var p = PreferenceManager.getDefaultSharedPreferences(context);
72 p
73 .edit()
74 .putString(AppSettings.THEME, "custom")
75 .putBoolean("custom_theme_automatic", false)
76 .putBoolean("custom_theme_dark", true)
77 .putInt("custom_dark_theme_primary", context.getColor(R.color.white))
78 .putInt("custom_dark_theme_primary_dark", context.getColor(android.R.color.black))
79 .putInt("custom_dark_theme_accent", context.getColor(R.color.yeller))
80 .putInt("custom_dark_theme_background_primary", context.getColor(android.R.color.black))
81 .commit();
82 }
83
84 // Migrate old themes to equivalent custom
85 if ("obsidian".equals(theme)) {
86 theme = "custom";
87 final var p = PreferenceManager.getDefaultSharedPreferences(context);
88 p
89 .edit()
90 .putString(AppSettings.THEME, "custom")
91 .putBoolean("custom_theme_automatic", false)
92 .putBoolean("custom_theme_dark", true)
93 .putInt("custom_dark_theme_primary", context.getColor(R.color.black_perpy))
94 .putInt("custom_dark_theme_primary_dark", context.getColor(R.color.black_perpy))
95 .putInt("custom_dark_theme_accent", context.getColor(R.color.yeller))
96 .putInt("custom_dark_theme_background_primary", context.getColor(R.color.blacker_perpy))
97 .commit();
98 }
99
100 if ("custom".equals(theme)) {
101 if (sharedPreferences.getBoolean("custom_theme_automatic", false)) return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
102 return sharedPreferences.getBoolean("custom_theme_dark", false) ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
103 }
104
105 return getDesiredNightMode(theme);
106 }
107
108 public static int getDesiredNightMode(final String theme) {
109 if ("automatic".equals(theme)) {
110 return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
111 } else if ("light".equals(theme)) {
112 return AppCompatDelegate.MODE_NIGHT_NO;
113 } else {
114 return AppCompatDelegate.MODE_NIGHT_YES;
115 }
116 }
117}