/* This file is part of Subsonic. Subsonic is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Subsonic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Subsonic. If not, see . Copyright 2016 (C) Scott Jackson */ package net.nullsum.audinaut.util; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; import net.nullsum.audinaut.R; import net.nullsum.audinaut.activity.SettingsActivity; import net.nullsum.audinaut.activity.SubsonicFragmentActivity; public final class ThemeUtil { private static final String THEME_DARK = "dark"; private static final String THEME_BLACK = "black"; private static final String THEME_LIGHT = "light"; private static final String THEME_DAY_NIGHT = "day/night"; private static final String THEME_DAY_BLACK_NIGHT = "day/black"; public static String getTheme(Context context) { SharedPreferences prefs = Util.getPreferences(context); String theme = prefs.getString(Constants.PREFERENCES_KEY_THEME, null); if (THEME_DAY_NIGHT.equals(theme)) { int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) { theme = THEME_DARK; } else { theme = THEME_LIGHT; } } else if (THEME_DAY_BLACK_NIGHT.equals(theme)) { int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) { theme = THEME_BLACK; } else { theme = THEME_LIGHT; } } return theme; } public static int getThemeRes(Context context) { return getThemeRes(context, getTheme(context)); } private static int getThemeRes(Context context, String theme) { if (context instanceof SubsonicFragmentActivity || context instanceof SettingsActivity) { if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) { switch (theme) { case THEME_DARK: return R.style.Theme_Audinaut_Dark_No_Actionbar; case THEME_BLACK: return R.style.Theme_Audinaut_Black_No_Actionbar; default: return R.style.Theme_Audinaut_Light_No_Actionbar; } } else { switch (theme) { case THEME_DARK: return R.style.Theme_Audinaut_Dark_No_Color; case THEME_BLACK: return R.style.Theme_Audinaut_Black_No_Color; default: return R.style.Theme_Audinaut_Light_No_Color; } } } else { switch (theme) { case THEME_DARK: return R.style.Theme_Audinaut_Dark; case THEME_BLACK: return R.style.Theme_Audinaut_Black; default: return R.style.Theme_Audinaut_Light; } } } public static void setTheme(Context context, String theme) { SharedPreferences.Editor editor = Util.getPreferences(context).edit(); editor.putString(Constants.PREFERENCES_KEY_THEME, theme); editor.apply(); } public static void applyTheme(Context context, String theme) { context.setTheme(getThemeRes(context, theme)); } }