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.

485 lines
17 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.content.Context;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.SparseIntArray;
import net.micode.notes.R;
import net.micode.notes.ui.NotesPreferenceActivity;
/**
* 资源解析工具类 - 提供笔记应用中各种UI资源的统一管理
* 功能包括:
* 1. 笔记背景资源管理
* 2. 笔记布局资源管理
* 3. 小部件资源管理
* 4. 文本外观资源管理
*
* 设计原则:
* - 资源ID与语义常量分离
* - 添加深色模式支持
* - 增强资源选择的灵活性
*/
public class ResourceParser {
// 防止实例化
private ResourceParser() {
throw new AssertionError("This class cannot be instantiated");
}
/* ================== 颜色常量枚举 ================== */
/**
* 预定义颜色类型枚举
*/
public enum NoteColor {
YELLOW(0),
BLUE(1),
WHITE(2),
GREEN(3),
RED(4),
// 扩展更多颜色选项
PURPLE(5),
ORANGE(6);
public final int id;
NoteColor(int id) {
this.id = id;
}
private static final int DEFAULT_ID = YELLOW.id;
/**
* 获取默认颜色ID
*/
public static int getDefault() {
return DEFAULT_ID;
}
/**
* 从整数值获取颜色枚举
*/
public static NoteColor fromId(int id) {
for (NoteColor color : values()) {
if (color.id == id) {
return color;
}
}
return YELLOW;
}
}
/* ================== 字体大小常量枚举 ================== */
/**
* 预定义字体大小类型枚举
*/
public enum FontSize {
SMALL(0),
MEDIUM(1),
LARGE(2),
SUPER(3);
public final int id;
FontSize(int id) {
this.id = id;
}
private static final int DEFAULT_ID = MEDIUM.id;
/**
* 获取默认字体大小ID
*/
public static int getDefault() {
return DEFAULT_ID;
}
}
/* ================== 主题模式常量 ================== */
/**
* 应用主题模式
*/
public enum ThemeMode {
LIGHT(0),
DARK(1),
AUTO(2);
public final int id;
ThemeMode(int id) {
this.id = id;
}
public static ThemeMode fromId(int id) {
switch (id) {
case 1: return DARK;
case 2: return AUTO;
default: return LIGHT;
}
}
}
/* ================== 笔记背景资源管理 ================== */
/**
* 笔记背景资源集合
*/
public static class NoteBgResources {
// 编辑界面背景资源
private static final SparseIntArray BG_EDIT_RESOURCES = new SparseIntArray();
// 编辑界面标题背景资源
private static final SparseIntArray BG_EDIT_TITLE_RESOURCES = new SparseIntArray();
// 静态初始化资源映射
static {
// 浅色主题资源
BG_EDIT_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.edit_yellow);
BG_EDIT_RESOURCES.put(NoteColor.BLUE.id, R.drawable.edit_blue);
BG_EDIT_RESOURCES.put(NoteColor.WHITE.id, R.drawable.edit_white);
BG_EDIT_RESOURCES.put(NoteColor.GREEN.id, R.drawable.edit_green);
BG_EDIT_RESOURCES.put(NoteColor.RED.id, R.drawable.edit_red);
BG_EDIT_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.edit_purple);
BG_EDIT_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.edit_orange);
BG_EDIT_TITLE_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.edit_title_yellow);
BG_EDIT_TITLE_RESOURCES.put(NoteColor.BLUE.id, R.drawable.edit_title_blue);
BG_EDIT_TITLE_RESOURCES.put(NoteColor.WHITE.id, R.drawable.edit_title_white);
BG_EDIT_TITLE_RESOURCES.put(NoteColor.GREEN.id, R.drawable.edit_title_green);
BG_EDIT_TITLE_RESOURCES.put(NoteColor.RED.id, R.drawable.edit_title_red);
BG_EDIT_TITLE_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.edit_title_purple);
BG_EDIT_TITLE_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.edit_title_orange);
}
/**
* 获取笔记编辑背景资源ID
*
* @param colorId 颜色ID
* @return 对应的背景资源ID
*/
public static int getNoteBgResource(int colorId) {
return BG_EDIT_RESOURCES.get(colorId, R.drawable.edit_yellow);
}
/**
* 获取笔记标题背景资源ID
*
* @param colorId 颜色ID
* @return 对应的标题背景资源ID
*/
public static int getNoteTitleBgResource(int colorId) {
return BG_EDIT_TITLE_RESOURCES.get(colorId, R.drawable.edit_title_yellow);
}
/**
* 获取支持的颜色ID数组
* @return 所有可用的颜色ID数组
*/
public static int[] getAvailableColorIds() {
int[] ids = new int[BG_EDIT_RESOURCES.size()];
for (int i = 0; i < BG_EDIT_RESOURCES.size(); i++) {
ids[i] = BG_EDIT_RESOURCES.keyAt(i);
}
return ids;
}
}
/**
* 获取默认背景颜色ID
*
* @param context 上下文对象
* @return 背景颜色ID
*/
public static int getDefaultBgId(Context context) {
// 检查是否启用随机背景颜色
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
// 获取所有可用颜色ID
int[] colorIds = NoteBgResources.getAvailableColorIds();
if (colorIds.length > 0) {
return colorIds[(int) (Math.random() * colorIds.length)];
}
}
// 返回默认颜色
return NoteColor.getDefault();
}
/* ================== 笔记布局资源管理 ================== */
/**
* 笔记布局资源集合
*/
public static class NoteLayoutResources {
// 列表顶部背景资源
private static final SparseIntArray BG_FIRST_RESOURCES = new SparseIntArray();
// 列表中部背景资源
private static final SparseIntArray BG_NORMAL_RESOURCES = new SparseIntArray();
// 列表底部背景资源
private static final SparseIntArray BG_LAST_RESOURCES = new SparseIntArray();
// 单条笔记背景资源
private static final SparseIntArray BG_SINGLE_RESOURCES = new SparseIntArray();
// 文件夹背景资源
private static final SparseIntArray BG_FOLDER_RESOURCES = new SparseIntArray();
// 静态初始化资源映射
static {
// 浅色主题资源
BG_FIRST_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.list_yellow_up);
BG_FIRST_RESOURCES.put(NoteColor.BLUE.id, R.drawable.list_blue_up);
BG_FIRST_RESOURCES.put(NoteColor.WHITE.id, R.drawable.list_white_up);
BG_FIRST_RESOURCES.put(NoteColor.GREEN.id, R.drawable.list_green_up);
BG_FIRST_RESOURCES.put(NoteColor.RED.id, R.drawable.list_red_up);
BG_FIRST_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.list_purple_up);
BG_FIRST_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.list_orange_up);
BG_NORMAL_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.list_yellow_middle);
BG_NORMAL_RESOURCES.put(NoteColor.BLUE.id, R.drawable.list_blue_middle);
BG_NORMAL_RESOURCES.put(NoteColor.WHITE.id, R.drawable.list_white_middle);
BG_NORMAL_RESOURCES.put(NoteColor.GREEN.id, R.drawable.list_green_middle);
BG_NORMAL_RESOURCES.put(NoteColor.RED.id, R.drawable.list_red_middle);
BG_NORMAL_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.list_purple_middle);
BG_NORMAL_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.list_orange_middle);
BG_LAST_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.list_yellow_down);
BG_LAST_RESOURCES.put(NoteColor.BLUE.id, R.drawable.list_blue_down);
BG_LAST_RESOURCES.put(NoteColor.WHITE.id, R.drawable.list_white_down);
BG_LAST_RESOURCES.put(NoteColor.GREEN.id, R.drawable.list_green_down);
BG_LAST_RESOURCES.put(NoteColor.RED.id, R.drawable.list_red_down);
BG_LAST_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.list_purple_down);
BG_LAST_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.list_orange_down);
BG_SINGLE_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.list_yellow_single);
BG_SINGLE_RESOURCES.put(NoteColor.BLUE.id, R.drawable.list_blue_single);
BG_SINGLE_RESOURCES.put(NoteColor.WHITE.id, R.drawable.list_white_single);
BG_SINGLE_RESOURCES.put(NoteColor.GREEN.id, R.drawable.list_green_single);
BG_SINGLE_RESOURCES.put(NoteColor.RED.id, R.drawable.list_red_single);
BG_SINGLE_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.list_purple_single);
BG_SINGLE_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.list_orange_single);
BG_FOLDER_RESOURCES.put(ThemeMode.LIGHT.id, R.drawable.list_folder_light);
BG_FOLDER_RESOURCES.put(ThemeMode.DARK.id, R.drawable.list_folder_dark);
}
/**
* 获取列表首项背景资源
*
* @param colorId 颜色ID
* @return 背景资源ID
*/
public static int getNoteBgFirstRes(int colorId) {
return BG_FIRST_RESOURCES.get(colorId, R.drawable.list_yellow_up);
}
/**
* 获取列表末项背景资源
*
* @param colorId 颜色ID
* @return 背景资源ID
*/
public static int getNoteBgLastRes(int colorId) {
return BG_LAST_RESOURCES.get(colorId, R.drawable.list_yellow_down);
}
/**
* 获取单条笔记背景资源
*
* @param colorId 颜色ID
* @return 背景资源ID
*/
public static int getNoteBgSingleRes(int colorId) {
return BG_SINGLE_RESOURCES.get(colorId, R.drawable.list_yellow_single);
}
/**
* 获取列表中间项背景资源
*
* @param colorId 颜色ID
* @return 背景资源ID
*/
public static int getNoteBgNormalRes(int colorId) {
return BG_NORMAL_RESOURCES.get(colorId, R.drawable.list_yellow_middle);
}
/**
* 获取文件夹背景资源(支持主题模式)
*
* @param themeMode 主题模式
* @return 文件夹背景资源ID
*/
public static int getFolderBgRes(ThemeMode themeMode) {
return BG_FOLDER_RESOURCES.get(themeMode.id, R.drawable.list_folder_light);
}
}
/* ================== 小部件资源管理 ================== */
/**
* 小部件背景资源集合
*/
public static class WidgetBgResources {
// 2x小部件背景资源
private static final SparseIntArray BG_2X_RESOURCES = new SparseIntArray();
// 4x小部件背景资源
private static final SparseIntArray BG_4X_RESOURCES = new SparseIntArray();
// 静态初始化资源映射
static {
// 浅色主题资源
BG_2X_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.widget_2x_yellow);
BG_2X_RESOURCES.put(NoteColor.BLUE.id, R.drawable.widget_2x_blue);
BG_2X_RESOURCES.put(NoteColor.WHITE.id, R.drawable.widget_2x_white);
BG_2X_RESOURCES.put(NoteColor.GREEN.id, R.drawable.widget_2x_green);
BG_2X_RESOURCES.put(NoteColor.RED.id, R.drawable.widget_2x_red);
BG_2X_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.widget_2x_purple);
BG_2X_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.widget_2x_orange);
BG_4X_RESOURCES.put(NoteColor.YELLOW.id, R.drawable.widget_4x_yellow);
BG_4X_RESOURCES.put(NoteColor.BLUE.id, R.drawable.widget_4x_blue);
BG_4X_RESOURCES.put(NoteColor.WHITE.id, R.drawable.widget_4x_white);
BG_4X_RESOURCES.put(NoteColor.GREEN.id, R.drawable.widget_4x_green);
BG_4X_RESOURCES.put(NoteColor.RED.id, R.drawable.widget_4x_red);
BG_4X_RESOURCES.put(NoteColor.PURPLE.id, R.drawable.widget_4x_purple);
BG_4X_RESOURCES.put(NoteColor.ORANGE.id, R.drawable.widget_4x_orange);
}
/**
* 获取2x小部件背景资源
*
* @param colorId 颜色ID
* @return 背景资源ID
*/
public static int getWidget2xBgResource(int colorId) {
return BG_2X_RESOURCES.get(colorId, R.drawable.widget_2x_yellow);
}
/**
* 获取4x小部件背景资源
*
* @param colorId 颜色ID
* @return 背景资源ID
*/
public static int getWidget4xBgResource(int colorId) {
return BG_4X_RESOURCES.get(colorId, R.drawable.widget_4x_yellow);
}
}
/* ================== 文本外观资源管理 ================== */
/**
* 文本外观资源集合
*/
public static class TextAppearanceResources {
// 文本外观资源映射
private static final SparseIntArray TEXTAPPEARANCE_RESOURCES = new SparseIntArray();
// 静态初始化资源映射
static {
TEXTAPPEARANCE_RESOURCES.put(FontSize.SMALL.id, R.style.TextAppearanceSmall);
TEXTAPPEARANCE_RESOURCES.put(FontSize.MEDIUM.id, R.style.TextAppearanceMedium);
TEXTAPPEARANCE_RESOURCES.put(FontSize.LARGE.id, R.style.TextAppearanceLarge);
TEXTAPPEARANCE_RESOURCES.put(FontSize.SUPER.id, R.style.TextAppearanceSuper);
}
/**
* 获取文本外观资源
*
* @param fontSizeId 字体大小ID
* @return 文本外观资源ID
*/
public static int getTextAppearanceResource(int fontSizeId) {
// 边界检查防止越界
int resourceId = TEXTAPPEARANCE_RESOURCES.get(fontSizeId, -1);
return resourceId != -1 ? resourceId : FontSize.getDefault();
}
/**
* 获取可用文本大小数量
*
* @return 可用字体大小数量
*/
public static int getAvailableSizeCount() {
return TEXTAPPEARANCE_RESOURCES.size();
}
/**
* 获取实际字体大小值单位为sp
*
* @param context 上下文
* @param fontSizeId 字体大小ID
* @return 字体大小值sp
*/
public static float getFontSizeSp(Context context, int fontSizeId) {
Resources res = context.getResources();
// 映射字体大小ID到实际尺寸值
switch (fontSizeId) {
case 0: return res.getDimension(R.dimen.text_size_small) / res.getDisplayMetrics().scaledDensity;
case 1: return res.getDimension(R.dimen.text_size_medium) / res.getDisplayMetrics().scaledDensity;
case 2: return res.getDimension(R.dimen.text_size_large) / res.getDisplayMetrics().scaledDensity;
case 3: return res.getDimension(R.dimen.text_size_super) / res.getDisplayMetrics().scaledDensity;
default: return 16; // 默认16sp
}
}
}
/* ================== 新功能:主题资源解析 ================== */
/**
* 获取当前应用主题模式
*
* @param context 上下文
* @return 当前主题模式
*/
public static ThemeMode getCurrentThemeMode(Context context) {
// 从偏好设置获取主题ID
int themeId = PreferenceManager.getDefaultSharedPreferences(context)
.getInt(NotesPreferenceActivity.PREFERENCE_THEME_MODE, 0);
return ThemeMode.fromId(themeId);
}
/**
* 根据主题模式获取对应颜色ID
*
* @param colorId 基础颜色ID
* @param themeMode 主题模式
* @return 适应主题的颜色ID
*/
public static int getThemeAdjustedColor(int colorId, ThemeMode themeMode) {
// 深色模式下调整颜色饱和度
if (themeMode == ThemeMode.DARK) {
switch (NoteColor.fromId(colorId)) {
case YELLOW: return NoteColor.ORANGE.id;
case WHITE: return NoteColor.BLUE.id;
case GREEN: return NoteColor.PURPLE.id;
default: return colorId;
}
}
return colorId;
}
}