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 return preferences.getBoolean(AppSettings.DYNAMIC_COLORS, false);
53 }
54
55 public static boolean isCustomColorsDesired(final Context context) {
56 final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
57 final String theme =
58 sharedPreferences.getString(AppSettings.THEME, context.getString(R.string.theme));
59 return "custom".equals(theme);
60 }
61
62 private static int getDesiredNightMode(
63 final Context context, final SharedPreferences sharedPreferences) {
64 var theme =
65 sharedPreferences.getString(AppSettings.THEME, context.getString(R.string.theme));
66
67 // Migrate old themes to equivalent custom
68 if ("oledblack".equals(theme)) {
69 theme = "custom";
70 final var p = PreferenceManager.getDefaultSharedPreferences(context);
71 p
72 .edit()
73 .putString(AppSettings.THEME, "custom")
74 .putBoolean("custom_theme_automatic", false)
75 .putBoolean("custom_theme_dark", true)
76 .putInt("custom_dark_theme_primary", context.getColor(R.color.white))
77 .putInt("custom_dark_theme_primary_dark", context.getColor(android.R.color.black))
78 .putInt("custom_dark_theme_accent", context.getColor(R.color.yeller))
79 .putInt("custom_dark_theme_background_primary", context.getColor(android.R.color.black))
80 .commit();
81 }
82
83 // Migrate old themes to equivalent custom
84 if ("obsidian".equals(theme)) {
85 theme = "custom";
86 final var p = PreferenceManager.getDefaultSharedPreferences(context);
87 p
88 .edit()
89 .putString(AppSettings.THEME, "custom")
90 .putBoolean("custom_theme_automatic", false)
91 .putBoolean("custom_theme_dark", true)
92 .putInt("custom_dark_theme_primary", context.getColor(R.color.black_perpy))
93 .putInt("custom_dark_theme_primary_dark", context.getColor(R.color.black_perpy))
94 .putInt("custom_dark_theme_accent", context.getColor(R.color.yeller))
95 .putInt("custom_dark_theme_background_primary", context.getColor(R.color.blacker_perpy))
96 .commit();
97 }
98
99 if ("custom".equals(theme)) {
100 if (sharedPreferences.getBoolean("custom_theme_automatic", false)) return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
101 return sharedPreferences.getBoolean("custom_theme_dark", false) ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
102 }
103
104 return getDesiredNightMode(theme);
105 }
106
107 public static int getDesiredNightMode(final String theme) {
108 if ("automatic".equals(theme)) {
109 return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
110 } else if ("light".equals(theme)) {
111 return AppCompatDelegate.MODE_NIGHT_NO;
112 } else {
113 return AppCompatDelegate.MODE_NIGHT_YES;
114 }
115 }
116}