Update ResourceParser.java

main
mxvwfs5gq 2 months ago
parent 9d298f4177
commit 8f98bc42b8

@ -17,165 +17,469 @@
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 {
public static final int YELLOW = 0;
public static final int BLUE = 1;
public static final int WHITE = 2;
public static final int GREEN = 3;
public static final int RED = 4;
// 防止实例化
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 static final int BG_DEFAULT_COLOR = YELLOW;
public final int id;
public static final int TEXT_SMALL = 0;
public static final int TEXT_MEDIUM = 1;
public static final int TEXT_LARGE = 2;
public static final int TEXT_SUPER = 3;
NoteColor(int id) {
this.id = id;
}
private static final int DEFAULT_ID = YELLOW.id;
/**
* ID
*/
public static int getDefault() {
return DEFAULT_ID;
}
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;
/**
*
*/
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 final static int [] BG_EDIT_RESOURCES = new int [] {
R.drawable.edit_yellow,
R.drawable.edit_blue,
R.drawable.edit_white,
R.drawable.edit_green,
R.drawable.edit_red
};
// 编辑界面背景资源
private static final SparseIntArray BG_EDIT_RESOURCES = new SparseIntArray();
private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] {
R.drawable.edit_title_yellow,
R.drawable.edit_title_blue,
R.drawable.edit_title_white,
R.drawable.edit_title_green,
R.drawable.edit_title_red
};
// 编辑界面标题背景资源
private static final SparseIntArray BG_EDIT_TITLE_RESOURCES = new SparseIntArray();
public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id];
// 静态初始化资源映射
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);
}
public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id];
/**
* 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)) {
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
} else {
return BG_DEFAULT_COLOR;
// 获取所有可用颜色ID
int[] colorIds = NoteBgResources.getAvailableColorIds();
if (colorIds.length > 0) {
return colorIds[(int) (Math.random() * colorIds.length)];
}
}
// 返回默认颜色
return NoteColor.getDefault();
}
public static class NoteItemBgResources {
private final static int [] BG_FIRST_RESOURCES = new int [] {
R.drawable.list_yellow_up,
R.drawable.list_blue_up,
R.drawable.list_white_up,
R.drawable.list_green_up,
R.drawable.list_red_up
};
/* ================== 笔记布局资源管理 ================== */
/**
*
*/
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 final static int [] BG_NORMAL_RESOURCES = new int [] {
R.drawable.list_yellow_middle,
R.drawable.list_blue_middle,
R.drawable.list_white_middle,
R.drawable.list_green_middle,
R.drawable.list_red_middle
};
// 单条笔记背景资源
private static final SparseIntArray BG_SINGLE_RESOURCES = new SparseIntArray();
private final static int [] BG_LAST_RESOURCES = new int [] {
R.drawable.list_yellow_down,
R.drawable.list_blue_down,
R.drawable.list_white_down,
R.drawable.list_green_down,
R.drawable.list_red_down,
};
// 文件夹背景资源
private static final SparseIntArray BG_FOLDER_RESOURCES = new SparseIntArray();
private final static int [] BG_SINGLE_RESOURCES = new int [] {
R.drawable.list_yellow_single,
R.drawable.list_blue_single,
R.drawable.list_white_single,
R.drawable.list_green_single,
R.drawable.list_red_single
};
// 静态初始化资源映射
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);
public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id];
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);
}
public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id];
/**
*
*
* @param colorId ID
* @return ID
*/
public static int getNoteBgLastRes(int colorId) {
return BG_LAST_RESOURCES.get(colorId, R.drawable.list_yellow_down);
}
public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id];
/**
*
*
* @param colorId ID
* @return ID
*/
public static int getNoteBgSingleRes(int colorId) {
return BG_SINGLE_RESOURCES.get(colorId, R.drawable.list_yellow_single);
}
public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id];
/**
*
*
* @param colorId ID
* @return ID
*/
public static int getNoteBgNormalRes(int colorId) {
return BG_NORMAL_RESOURCES.get(colorId, R.drawable.list_yellow_middle);
}
public static int getFolderBgRes() {
return R.drawable.list_folder;
/**
*
*
* @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 {
private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow,
R.drawable.widget_2x_blue,
R.drawable.widget_2x_white,
R.drawable.widget_2x_green,
R.drawable.widget_2x_red,
};
// 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);
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
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);
}
private final static int [] BG_4X_RESOURCES = new int [] {
R.drawable.widget_4x_yellow,
R.drawable.widget_4x_blue,
R.drawable.widget_4x_white,
R.drawable.widget_4x_green,
R.drawable.widget_4x_red
};
/**
* 2x
*
* @param colorId ID
* @return ID
*/
public static int getWidget2xBgResource(int colorId) {
return BG_2X_RESOURCES.get(colorId, R.drawable.widget_2x_yellow);
}
public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id];
/**
* 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 final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal,
R.style.TextAppearanceMedium,
R.style.TextAppearanceLarge,
R.style.TextAppearanceSuper
};
public static int getTexAppearanceResource(int id) {
/**
* HACKME: Fix bug of store the resource id in shared preference.
* The id may larger than the length of resources, in this case,
* return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
*/
if (id >= TEXTAPPEARANCE_RESOURCES.length) {
return BG_DEFAULT_FONT_SIZE;
// 文本外观资源映射
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
}
return TEXTAPPEARANCE_RESOURCES[id];
}
}
/* ================== 新功能:主题资源解析 ================== */
public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length;
/**
*
*
* @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;
}
}
}
Loading…
Cancel
Save