Audinaut-subsonic-app-android/app/src/main/java/net/nullsum/audinaut/util/ThemeUtil.java

103 lines
4.1 KiB
Java
Raw Normal View History

2016-12-18 18:41:30 +01:00
/*
This file is part of Subsonic.
2018-03-24 20:25:12 +01:00
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 <http://www.gnu.org/licenses/>.
Copyright 2016 (C) Scott Jackson
2016-12-18 18:41:30 +01:00
*/
2017-01-09 08:01:12 +01:00
package net.nullsum.audinaut.util;
2016-12-18 18:41:30 +01:00
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
2017-01-09 08:01:12 +01:00
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.activity.SettingsActivity;
import net.nullsum.audinaut.activity.SubsonicFragmentActivity;
2016-12-18 18:41:30 +01:00
public final class ThemeUtil {
2018-03-25 03:28:28 +02:00
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";
2016-12-18 18:41:30 +01:00
2018-03-24 20:25:12 +01:00
public static String getTheme(Context context) {
SharedPreferences prefs = Util.getPreferences(context);
String theme = prefs.getString(Constants.PREFERENCES_KEY_THEME, null);
2016-12-18 18:41:30 +01:00
2018-03-25 03:28:28 +02:00
if (THEME_DAY_NIGHT.equals(theme)) {
2018-03-24 20:25:12 +01:00
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
2018-03-25 03:28:28 +02:00
if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
2018-03-24 20:25:12 +01:00
theme = THEME_DARK;
} else {
theme = THEME_LIGHT;
}
2018-03-25 03:28:28 +02:00
} else if (THEME_DAY_BLACK_NIGHT.equals(theme)) {
2018-03-24 20:25:12 +01:00
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
2018-03-25 03:28:28 +02:00
if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
2018-03-24 20:25:12 +01:00
theme = THEME_BLACK;
} else {
theme = THEME_LIGHT;
}
}
2016-12-18 18:41:30 +01:00
2018-03-24 20:25:12 +01:00
return theme;
}
2018-03-25 03:28:28 +02:00
2018-03-24 20:25:12 +01:00
public static int getThemeRes(Context context) {
return getThemeRes(context, getTheme(context));
}
2018-03-25 03:28:28 +02:00
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)) {
2018-04-25 01:58:40 +02:00
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;
2018-03-24 20:25:12 +01:00
}
} else {
2018-04-25 01:58:40 +02:00
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;
2018-03-24 20:25:12 +01:00
}
}
} else {
2018-04-25 01:58:40 +02:00
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;
2018-03-24 20:25:12 +01:00
}
}
}
2018-03-25 03:28:28 +02:00
2018-03-24 20:25:12 +01:00
public static void setTheme(Context context, String theme) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putString(Constants.PREFERENCES_KEY_THEME, theme);
editor.apply();
}
2016-12-18 18:41:30 +01:00
2018-03-24 20:25:12 +01:00
public static void applyTheme(Context context, String theme) {
context.setTheme(getThemeRes(context, theme));
}
2016-12-18 18:41:30 +01:00
}