You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
8.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.tool;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import net.micode.notes.R;
/**
* 主题管理器
* 负责应用主题的切换、管理和应用,支持浅色、深色和跟随系统主题
*/
public class ThemeManager {
// 主题模式偏好设置键
private static final String PREF_THEME_MODE = "theme_mode";
// 跟随系统主题偏好设置键
private static final String PREF_FOLLOW_SYSTEM = "follow_system_theme";
// 主题模式常量
public static final int THEME_LIGHT = 0; // 浅色主题
public static final int THEME_DARK = 1; // 深色主题
public static final int THEME_SYSTEM = 2; // 跟随系统主题
/**
* 为Activity应用主题
* 根据当前主题设置应用相应的主题样式
* @param activity 要应用主题的Activity
*/
public static void applyTheme(Activity activity) {
int themeMode = getThemeMode(activity);
switch (themeMode) {
case THEME_LIGHT:
// 应用浅色主题
activity.setTheme(R.style.AppTheme);
break;
case THEME_DARK:
// 应用深色主题
activity.setTheme(R.style.AppTheme_Dark);
break;
case THEME_SYSTEM:
// 跟随系统主题
if (isSystemDarkMode(activity)) {
activity.setTheme(R.style.AppTheme_Dark);
} else {
activity.setTheme(R.style.AppTheme);
}
break;
}
}
/**
* 为Activity应用笔记主题
* 专门用于笔记编辑界面的主题设置
* @param activity 要应用主题的Activity
*/
public static void applyNoteTheme(Activity activity) {
int themeMode = getThemeMode(activity);
switch (themeMode) {
case THEME_LIGHT:
// 应用浅色笔记主题
activity.setTheme(R.style.NoteTheme);
break;
case THEME_DARK:
// 应用深色笔记主题
activity.setTheme(R.style.NoteTheme_Dark);
break;
case THEME_SYSTEM:
// 跟随系统主题
if (isSystemDarkMode(activity)) {
activity.setTheme(R.style.NoteTheme_Dark);
} else {
activity.setTheme(R.style.NoteTheme);
}
break;
}
}
/**
* 获取当前主题模式
* @param context 上下文对象
* @return 主题模式常量
*/
public static int getThemeMode(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getInt(PREF_THEME_MODE, THEME_SYSTEM);
}
/**
* 设置主题模式
* @param context 上下文对象
* @param themeMode 要设置的主题模式
*/
public static void setThemeMode(Context context, int themeMode) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putInt(PREF_THEME_MODE, themeMode).apply();
}
/**
* 检查系统是否处于深色模式
* @param context 上下文对象
* @return true表示系统处于深色模式false表示浅色模式
*/
public static boolean isSystemDarkMode(Context context) {
int nightModeFlags = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
return nightModeFlags == Configuration.UI_MODE_NIGHT_YES;
}
/**
* 检查当前主题是否为深色模式
* @param context 上下文对象
* @return true表示当前为深色主题false表示浅色主题
*/
public static boolean isDarkMode(Context context) {
int themeMode = getThemeMode(context);
switch (themeMode) {
case THEME_DARK:
return true;
case THEME_LIGHT:
return false;
case THEME_SYSTEM:
return isSystemDarkMode(context);
default:
return false;
}
}
/**
* 获取主题名称用于显示
* @param context 上下文对象
* @param themeMode 主题模式
* @return 主题名称字符串
*/
public static String getThemeName(Context context, int themeMode) {
switch (themeMode) {
case THEME_LIGHT:
return context.getString(R.string.theme_light);
case THEME_DARK:
return context.getString(R.string.theme_dark);
case THEME_SYSTEM:
return context.getString(R.string.theme_system);
default:
return context.getString(R.string.theme_system);
}
}
/**
* Get background color for current theme
*/
public static int getBackgroundColor(Context context) {
if (isDarkMode(context)) {
return context.getResources().getColor(R.color.backgroundDark);
} else {
return context.getResources().getColor(android.R.color.white);
}
}
/**
* Get text color for current theme
*/
public static int getTextColor(Context context) {
if (isDarkMode(context)) {
return context.getResources().getColor(R.color.textPrimaryDark);
} else {
return context.getResources().getColor(android.R.color.black);
}
}
/**
* Get secondary text color for current theme
*/
public static int getSecondaryTextColor(Context context) {
if (isDarkMode(context)) {
return context.getResources().getColor(R.color.textSecondaryDark);
} else {
return context.getResources().getColor(android.R.color.darker_gray);
}
}
/**
* Get note background resource based on color ID and theme
* 修改为使用动态着色,统一使用蓝色背景
*/
public static int getNoteBackgroundResource(Context context, int colorId) {
boolean isDark = isDarkMode(context);
// 深色模式使用深色背景
if (isDark) {
return R.drawable.edit_dark;
} else {
// 浅色模式统一使用蓝色背景,通过动态着色实现其他颜色
return ResourceParser.NoteBgResources.getNoteBgResource(colorId);
}
}
/**
* Get tag color for theme
*/
public static int getTagColor(Context context, int tagColorId) {
boolean isDark = isDarkMode(context);
if (isDark) {
switch (tagColorId) {
case 0: return context.getResources().getColor(R.color.tagBlueDark);
case 1: return context.getResources().getColor(R.color.tagGreenDark);
case 2: return context.getResources().getColor(R.color.tagOrangeDark);
case 3: return context.getResources().getColor(R.color.tagRedDark);
case 4: return context.getResources().getColor(R.color.tagPurpleDark);
case 5: return context.getResources().getColor(R.color.tagGrayDark);
default: return context.getResources().getColor(R.color.tagBlueDark);
}
} else {
switch (tagColorId) {
case 0: return context.getResources().getColor(android.R.color.holo_blue_bright);
case 1: return context.getResources().getColor(android.R.color.holo_green_light);
case 2: return context.getResources().getColor(android.R.color.holo_orange_light);
case 3: return context.getResources().getColor(android.R.color.holo_red_light);
case 4: return context.getResources().getColor(android.R.color.holo_purple);
case 5: return context.getResources().getColor(android.R.color.darker_gray);
default: return context.getResources().getColor(android.R.color.holo_blue_bright);
}
}
}
}