ThemeHelper.java

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