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 .putBoolean("custom_theme_color_match", true)
78 .putInt("custom_dark_theme_primary", context.getColor(R.color.white))
79 .putInt("custom_dark_theme_primary_dark", context.getColor(android.R.color.black))
80 .putInt("custom_dark_theme_accent", context.getColor(R.color.yeller))
81 .putInt("custom_dark_theme_background_primary", context.getColor(android.R.color.black))
82 .commit();
83 }
84
85 // Migrate old themes to equivalent custom
86 if ("obsidian".equals(theme)) {
87 theme = "custom";
88 final var p = PreferenceManager.getDefaultSharedPreferences(context);
89 p
90 .edit()
91 .putString(AppSettings.THEME, "custom")
92 .putBoolean("custom_theme_automatic", false)
93 .putBoolean("custom_theme_dark", true)
94 .putInt("custom_dark_theme_primary", context.getColor(R.color.black_perpy))
95 .putInt("custom_dark_theme_primary_dark", context.getColor(R.color.black_perpy))
96 .putInt("custom_dark_theme_accent", context.getColor(R.color.yeller))
97 .putInt("custom_dark_theme_background_primary", context.getColor(R.color.blacker_perpy))
98 .commit();
99 }
100
101 if ("custom".equals(theme)) {
102 if (sharedPreferences.getBoolean("custom_theme_automatic", false)) return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
103 return sharedPreferences.getBoolean("custom_theme_dark", false) ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
104 }
105
106 return getDesiredNightMode(theme);
107 }
108
109 public static int getDesiredNightMode(final String theme) {
110 if ("automatic".equals(theme)) {
111 return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
112 } else if ("light".equals(theme)) {
113 return AppCompatDelegate.MODE_NIGHT_NO;
114 } else {
115 return AppCompatDelegate.MODE_NIGHT_YES;
116 }
117 }
118}