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_theme_background_primary_dark")) {
78 int background_primary = sharedPreferences.getInt("custom_theme_background_primary_dark", 0);
79 int alpha = (background_primary >> 24) & 0xFF;
80 int red = (background_primary >> 16) & 0xFF;
81 int green = (background_primary >> 8) & 0xFF;
82 int blue = background_primary & 0xFF;
83 colors.put(R.color.custom_theme_background_primary_dark, background_primary);
84 colors.put(R.color.custom_theme_background_secondary_dark, (int)((alpha << 24) | ((int)(red*.5) << 16) | ((int)(green*.5) << 8) | (int)(blue*.5)));
85 colors.put(R.color.custom_theme_background_tertiary_dark, (int)((alpha << 24) | ((int)(40 + red*.84) << 16) | ((int)(40 + green*.84) << 8) | (int)(40 + blue*.84)));
86 }
87 if (colors.isEmpty()) return colors;
88
89 ResourcesLoader loader = ColorResourcesLoaderCreator.create(context, colors);
90 try {
91 if (loader != null) context.getResources().addLoaders(loader);
92 } catch (final IllegalArgumentException e) {
93 Log.w(Config.LOGTAG, "Custom colour failed: " + e);
94 }
95 return colors;
96 }
97
98 public static int find(final Context context) {
99 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
100 final Resources resources = context.getResources();
101 final String setting = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme));
102 final boolean dark = isDark(sharedPreferences, resources);
103 final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size));
104 switch (fontSize) {
105 case "medium":
106 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian_Medium;
107 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack_Medium;
108 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark_Medium : R.style.ConversationsTheme_Custom_Medium;
109 return dark ? R.style.ConversationsTheme_Dark_Medium : R.style.ConversationsTheme_Medium;
110 case "large":
111 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian_Large;
112 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack_Large;
113 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark_Large : R.style.ConversationsTheme_Custom_Large;
114 return dark ? R.style.ConversationsTheme_Dark_Large : R.style.ConversationsTheme_Large;
115 default:
116 if ("obsidian".equals(setting)) return R.style.ConversationsTheme_Obsidian;
117 else if ("oledblack".equals(setting)) return R.style.ConversationsTheme_OLEDBlack;
118 else if ("custom".equals(setting)) return dark ? R.style.ConversationsTheme_CustomDark : R.style.ConversationsTheme_Custom;
119 return dark ? R.style.ConversationsTheme_Dark : R.style.ConversationsTheme;
120 }
121 }
122
123 public static int findDialog(Context context) {
124 final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
125 final Resources resources = context.getResources();
126 final boolean dark = isDark(sharedPreferences, resources);
127 final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size));
128 switch (fontSize) {
129 case "medium":
130 return dark ? R.style.ConversationsTheme_Dark_Dialog_Medium : R.style.ConversationsTheme_Dialog_Medium;
131 case "large":
132 return dark ? R.style.ConversationsTheme_Dark_Dialog_Large : R.style.ConversationsTheme_Dialog_Large;
133 default:
134 return dark ? R.style.ConversationsTheme_Dark_Dialog : R.style.ConversationsTheme_Dialog;
135 }
136 }
137
138 private static boolean isDark(final SharedPreferences sharedPreferences, final Resources resources) {
139 final String setting = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme));
140 if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && "automatic".equals(setting)) ||
141 ("custom".equals(setting) && sharedPreferences.getBoolean("custom_theme_automatic", false))
142 ) {
143 return (resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
144 } else {
145 if ("custom".equals(setting)) return sharedPreferences.getBoolean("custom_theme_dark", false);
146 return "dark".equals(setting) || "obsidian".equals(setting) || "oledblack".equals(setting);
147 }
148 }
149
150 public static boolean isDark(@StyleRes int id) {
151 switch (id) {
152 case R.style.ConversationsTheme_Dark:
153 case R.style.ConversationsTheme_Dark_Large:
154 case R.style.ConversationsTheme_Dark_Medium:
155 case R.style.ConversationsTheme_CustomDark:
156 case R.style.ConversationsTheme_CustomDark_Large:
157 case R.style.ConversationsTheme_CustomDark_Medium:
158 case R.style.ConversationsTheme_Obsidian:
159 case R.style.ConversationsTheme_Obsidian_Large:
160 case R.style.ConversationsTheme_Obsidian_Medium:
161 case R.style.ConversationsTheme_OLEDBlack:
162 case R.style.ConversationsTheme_OLEDBlack_Large:
163 case R.style.ConversationsTheme_OLEDBlack_Medium:
164 return true;
165 default:
166 return false;
167 }
168 }
169
170 public static void fix(Snackbar snackbar) {
171 final Context context = snackbar.getContext();
172 TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.TextSizeBody1});
173 final float size = typedArray.getDimension(0,0f);
174 typedArray.recycle();
175 if (size != 0f) {
176 final TextView text = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
177 final TextView action = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_action);
178 if (text != null && action != null) {
179 text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
180 action.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
181 action.setTextColor(ContextCompat.getColor(context, R.color.blue_a100));
182 }
183 }
184 }
185}