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.Log;
41import android.util.TypedValue;
42import android.widget.TextView;
43
44import androidx.annotation.StyleRes;
45import androidx.core.content.ContextCompat;
46
47import com.cheogram.android.ColorResourcesLoaderCreator;
48
49import com.google.android.material.snackbar.Snackbar;
50
51import java.util.HashMap;
52
53import eu.siacs.conversations.R;
54import eu.siacs.conversations.Config;
55import eu.siacs.conversations.ui.SettingsActivity;
56
57public class ThemeHelper {
58
59 public static HashMap<Integer, Integer> applyCustomColors(final Context context) {
60 HashMap<Integer, Integer> colors = new HashMap<>();
61 if (Build.VERSION.SDK_INT < 30) return colors;
62
63 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
64 if (sharedPreferences.contains("custom_theme_primary")) colors.put(R.color.custom_theme_primary, sharedPreferences.getInt("custom_theme_primary", 0));
65 if (sharedPreferences.contains("custom_theme_primary_dark")) colors.put(R.color.custom_theme_primary_dark, sharedPreferences.getInt("custom_theme_primary_dark", 0));
66 if (sharedPreferences.contains("custom_theme_accent")) colors.put(R.color.custom_theme_accent, sharedPreferences.getInt("custom_theme_accent", 0));
67 if (sharedPreferences.contains("custom_theme_background_primary")) {
68 int background_primary = sharedPreferences.getInt("custom_theme_background_primary", 0);
69 int alpha = (background_primary >> 24) & 0xFF;
70 int red = (background_primary >> 16) & 0xFF;
71 int green = (background_primary >> 8) & 0xFF;
72 int blue = background_primary & 0xFF;
73 colors.put(R.color.custom_theme_background_primary, background_primary);
74 colors.put(R.color.custom_theme_background_secondary, (int)((alpha << 24) | ((int)(red*.9) << 16) | ((int)(green*.9) << 8) | (int)(blue*.9)));
75 colors.put(R.color.custom_theme_background_tertiary, (int)((alpha << 24) | ((int)(red*.85) << 16) | ((int)(green*.85) << 8) | (int)(blue*.85)));
76 }
77 if (sharedPreferences.contains("custom_dark_theme_primary")) colors.put(R.color.custom_dark_theme_primary, sharedPreferences.getInt("custom_dark_theme_primary", 0));
78 if (sharedPreferences.contains("custom_dark_theme_primary_dark")) colors.put(R.color.custom_dark_theme_primary_dark, sharedPreferences.getInt("custom_dark_theme_primary_dark", 0));
79 if (sharedPreferences.contains("custom_dark_theme_accent")) colors.put(R.color.custom_dark_theme_accent, sharedPreferences.getInt("custom_dark_theme_accent", 0));
80 if (sharedPreferences.contains("custom_dark_theme_background_primary")) {
81 int background_primary = sharedPreferences.getInt("custom_dark_theme_background_primary", 0);
82 int alpha = (background_primary >> 24) & 0xFF;
83 int red = (background_primary >> 16) & 0xFF;
84 int green = (background_primary >> 8) & 0xFF;
85 int blue = background_primary & 0xFF;
86 colors.put(R.color.custom_dark_theme_background_primary, background_primary);
87 colors.put(R.color.custom_dark_theme_background_secondary, (int)((alpha << 24) | ((int)(red*.5) << 16) | ((int)(green*.5) << 8) | (int)(blue*.5)));
88 colors.put(R.color.custom_dark_theme_background_tertiary, (int)((alpha << 24) | ((int)(40 + red*.84) << 16) | ((int)(40 + green*.84) << 8) | (int)(40 + blue*.84)));
89 }
90 if (colors.isEmpty()) return colors;
91
92 ResourcesLoader loader = ColorResourcesLoaderCreator.create(context, colors);
93 try {
94 if (loader != null) context.getResources().addLoaders(loader);
95 } catch (final IllegalArgumentException e) {
96 Log.w(Config.LOGTAG, "Custom colour failed: " + e);
97 }
98 return colors;
99 }
100
101 public static int find(final Context context) {
102 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
103 final Resources resources = context.getResources();
104 final String setting = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme));
105 final boolean dark = isDark(sharedPreferences, resources);
106 final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size));
107 switch (fontSize) {
108 case "medium":
109 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian_Medium;
110 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack_Medium;
111 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark_Medium : R.style.ConversationsTheme_Custom_Medium;
112 return dark ? R.style.ConversationsTheme_Dark_Medium : R.style.ConversationsTheme_Medium;
113 case "large":
114 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian_Large;
115 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack_Large;
116 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark_Large : R.style.ConversationsTheme_Custom_Large;
117 return dark ? R.style.ConversationsTheme_Dark_Large : R.style.ConversationsTheme_Large;
118 default:
119 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian;
120 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack;
121 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark : R.style.ConversationsTheme_Custom;
122 return dark ? R.style.ConversationsTheme_Dark : R.style.ConversationsTheme;
123 }
124 }
125
126 public static int findDialog(Context context) {
127 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
128 final Resources resources = context.getResources();
129 final boolean dark = isDark(sharedPreferences, resources);
130 final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size));
131 switch (fontSize) {
132 case "medium":
133 return dark ? R.style.ConversationsTheme_Dark_Dialog_Medium : R.style.ConversationsTheme_Dialog_Medium;
134 case "large":
135 return dark ? R.style.ConversationsTheme_Dark_Dialog_Large : R.style.ConversationsTheme_Dialog_Large;
136 default:
137 return dark ? R.style.ConversationsTheme_Dark_Dialog : R.style.ConversationsTheme_Dialog;
138 }
139 }
140
141 private static boolean isDark(final SharedPreferences sharedPreferences, final Resources resources) {
142 final String setting = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme));
143 if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && "automatic".equals(setting)) ||
144 ("custom".equals(setting) && sharedPreferences.getBoolean("custom_theme_automatic", false))
145 ) {
146 return (resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
147 } else {
148 if ("custom".equals(setting)) return sharedPreferences.getBoolean("custom_theme_dark", false);
149 return "dark".equals(setting) || "obsidian".equals(setting) || "oledblack".equals(setting);
150 }
151 }
152
153 public static boolean isDark(@StyleRes int id) {
154 switch (id) {
155 case R.style.ConversationsTheme_Dark:
156 case R.style.ConversationsTheme_Dark_Large:
157 case R.style.ConversationsTheme_Dark_Medium:
158 case R.style.ConversationsTheme_CustomDark:
159 case R.style.ConversationsTheme_CustomDark_Large:
160 case R.style.ConversationsTheme_CustomDark_Medium:
161 case R.style.ConversationsTheme_Obsidian:
162 case R.style.ConversationsTheme_Obsidian_Large:
163 case R.style.ConversationsTheme_Obsidian_Medium:
164 case R.style.ConversationsTheme_OLEDBlack:
165 case R.style.ConversationsTheme_OLEDBlack_Large:
166 case R.style.ConversationsTheme_OLEDBlack_Medium:
167 return true;
168 default:
169 return false;
170 }
171 }
172
173 public static void fix(Snackbar snackbar) {
174 final Context context = snackbar.getContext();
175 TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.TextSizeBody1});
176 final float size = typedArray.getDimension(0,0f);
177 typedArray.recycle();
178 if (size != 0f) {
179 final TextView text = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
180 final TextView action = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_action);
181 if (text != null && action != null) {
182 text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
183 action.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
184 action.setTextColor(ContextCompat.getColor(context, R.color.blue_a100));
185 }
186 }
187 }
188}