1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.utils;
31
32import android.content.Context;
33import android.content.SharedPreferences;
34import android.content.res.Configuration;
35import android.content.res.Resources;
36import android.content.res.TypedArray;
37import android.content.res.loader.ResourcesLoader;
38import android.os.Build;
39import android.preference.PreferenceManager;
40import android.util.TypedValue;
41import android.widget.TextView;
42
43import androidx.annotation.StyleRes;
44import androidx.core.content.ContextCompat;
45
46import com.cheogram.android.ColorResourcesLoaderCreator;
47
48import com.google.android.material.snackbar.Snackbar;
49
50import java.util.HashMap;
51
52import eu.siacs.conversations.R;
53import eu.siacs.conversations.ui.SettingsActivity;
54
55public class ThemeHelper {
56
57 public static HashMap<Integer, Integer> applyCustomColors(final Context context) {
58 HashMap<Integer, Integer> colors = new HashMap<>();
59 if (Build.VERSION.SDK_INT < 30) return colors;
60
61 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
62 if (sharedPreferences.contains("custom_theme_primary")) colors.put(R.color.custom_theme_primary, sharedPreferences.getInt("custom_theme_primary", 0));
63 if (sharedPreferences.contains("custom_theme_primary_dark")) colors.put(R.color.custom_theme_primary_dark, sharedPreferences.getInt("custom_theme_primary_dark", 0));
64 if (sharedPreferences.contains("custom_theme_accent")) colors.put(R.color.custom_theme_accent, sharedPreferences.getInt("custom_theme_accent", 0));
65 if (colors.isEmpty()) return colors;
66
67 ResourcesLoader loader = ColorResourcesLoaderCreator.create(context, colors);
68 if (loader != null) context.getResources().addLoaders(loader);
69 return colors;
70 }
71
72 public static int find(final Context context) {
73 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
74 final Resources resources = context.getResources();
75 final String setting = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme));
76 final boolean dark = isDark(sharedPreferences, resources);
77 final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size));
78 switch (fontSize) {
79 case "medium":
80 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian_Medium;
81 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack_Medium;
82 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark_Medium : R.style.ConversationsTheme_Custom_Medium;
83 return dark ? R.style.ConversationsTheme_Dark_Medium : R.style.ConversationsTheme_Medium;
84 case "large":
85 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian_Large;
86 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack_Large;
87 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark_Large : R.style.ConversationsTheme_Custom_Large;
88 return dark ? R.style.ConversationsTheme_Dark_Large : R.style.ConversationsTheme_Large;
89 default:
90 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian;
91 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack;
92 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark : R.style.ConversationsTheme_Custom;
93 return dark ? R.style.ConversationsTheme_Dark : R.style.ConversationsTheme;
94 }
95 }
96
97 public static int findDialog(Context context) {
98 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
99 final Resources resources = context.getResources();
100 final boolean dark = isDark(sharedPreferences, resources);
101 final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size));
102 switch (fontSize) {
103 case "medium":
104 return dark ? R.style.ConversationsTheme_Dark_Dialog_Medium : R.style.ConversationsTheme_Dialog_Medium;
105 case "large":
106 return dark ? R.style.ConversationsTheme_Dark_Dialog_Large : R.style.ConversationsTheme_Dialog_Large;
107 default:
108 return dark ? R.style.ConversationsTheme_Dark_Dialog : R.style.ConversationsTheme_Dialog;
109 }
110 }
111
112 private static boolean isDark(final SharedPreferences sharedPreferences, final Resources resources) {
113 final String setting = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme));
114 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && "automatic".equals(setting)) {
115 return (resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
116 } else {
117 if ("custom".equals(setting)) return sharedPreferences.getBoolean("custom_theme_dark", false);
118 return "dark".equals(setting) || "obsidian".equals(setting) || "oledblack".equals(setting);
119 }
120 }
121
122 public static boolean isDark(@StyleRes int id) {
123 switch (id) {
124 case R.style.ConversationsTheme_Dark:
125 case R.style.ConversationsTheme_Dark_Large:
126 case R.style.ConversationsTheme_Dark_Medium:
127 case R.style.ConversationsTheme_CustomDark:
128 case R.style.ConversationsTheme_CustomDark_Large:
129 case R.style.ConversationsTheme_CustomDark_Medium:
130 case R.style.ConversationsTheme_Obsidian:
131 case R.style.ConversationsTheme_Obsidian_Large:
132 case R.style.ConversationsTheme_Obsidian_Medium:
133 case R.style.ConversationsTheme_OLEDBlack:
134 case R.style.ConversationsTheme_OLEDBlack_Large:
135 case R.style.ConversationsTheme_OLEDBlack_Medium:
136 return true;
137 default:
138 return false;
139 }
140 }
141
142 public static void fix(Snackbar snackbar) {
143 final Context context = snackbar.getContext();
144 TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.TextSizeBody1});
145 final float size = typedArray.getDimension(0,0f);
146 typedArray.recycle();
147 if (size != 0f) {
148 final TextView text = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
149 final TextView action = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_action);
150 if (text != null && action != null) {
151 text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
152 action.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
153 action.setTextColor(ContextCompat.getColor(context, R.color.blue_a100));
154 }
155 }
156 }
157}