ADD file via upload

main v12
mxvwfs5gq 2 months ago
parent 7c094b4fdb
commit 644b8501ec

@ -0,0 +1,485 @@
/*
* 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;
}
}
Loading…
Cancel
Save