Conversations.java

 1package eu.siacs.conversations;
 2
 3import android.annotation.SuppressLint;
 4import android.app.Application;
 5import android.content.Context;
 6import android.content.SharedPreferences;
 7import android.preference.PreferenceManager;
 8
 9import androidx.appcompat.app.AppCompatDelegate;
10
11import com.google.android.material.color.DynamicColors;
12import com.google.android.material.color.DynamicColorsOptions;
13
14import eu.siacs.conversations.utils.ExceptionHelper;
15
16public class Conversations extends Application {
17
18    @SuppressLint("StaticFieldLeak")
19    private static Context CONTEXT;
20
21    public static Context getContext() {
22        return Conversations.CONTEXT;
23    }
24
25    @Override
26    public void onCreate() {
27        super.onCreate();
28        CONTEXT = this.getApplicationContext();
29        ExceptionHelper.init(getApplicationContext());
30        applyThemeSettings();
31    }
32
33    public void applyThemeSettings() {
34        final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
35        if (sharedPreferences == null) {
36            return;
37        }
38        applyThemeSettings(sharedPreferences);
39    }
40
41    private void applyThemeSettings(final SharedPreferences sharedPreferences) {
42        AppCompatDelegate.setDefaultNightMode(getDesiredNightMode(this, sharedPreferences));
43        var dynamicColorsOptions =
44                new DynamicColorsOptions.Builder()
45                        .setPrecondition((activity, t) -> isDynamicColorsDesired(activity))
46                        .build();
47        DynamicColors.applyToActivitiesIfAvailable(this, dynamicColorsOptions);
48    }
49
50    public static int getDesiredNightMode(final Context context) {
51        final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
52        if (sharedPreferences == null) {
53            return AppCompatDelegate.getDefaultNightMode();
54        }
55        return getDesiredNightMode(context, sharedPreferences);
56    }
57
58    public static boolean isDynamicColorsDesired(final Context context) {
59        final var preferences = PreferenceManager.getDefaultSharedPreferences(context);
60        return preferences.getBoolean(AppSettings.DYNAMIC_COLORS, false);
61    }
62
63    private static int getDesiredNightMode(
64            final Context context, final SharedPreferences sharedPreferences) {
65        final String theme =
66                sharedPreferences.getString(AppSettings.THEME, context.getString(R.string.theme));
67        return getDesiredNightMode(theme);
68    }
69
70    public static int getDesiredNightMode(final String theme) {
71        if ("automatic".equals(theme)) {
72            return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
73        } else if ("light".equals(theme)) {
74            return AppCompatDelegate.MODE_NIGHT_NO;
75        } else {
76            return AppCompatDelegate.MODE_NIGHT_YES;
77        }
78    }
79}