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.
xiaomi/src/tool/ResourceParser.java

284 lines
10 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.

/*
* 版权所有 (c) 2010-2011MiCode 开源社区 (www.micode.net)
* 根据 Apache 许可证 2.0 版本("许可证")授权;
* 除非符合许可证的规定,否则不得使用本文件。
* 您可以从以下网址获取许可证副本:
* http://www.apache.org/licenses/LICENSE-2.0
* 除非适用法律要求或书面同意,本软件按"原样"分发,
* 没有任何明示或暗示的保证或条件。
* 详见许可证中规定的权限和限制。
* 这是一份标准的Apache许可证2.0版本的开源声明)
*/
// 定义包路径
package net.micode.notes.tool;
// 导入Android相关类
import android.content.Context; // 上下文对象
import android.preference.PreferenceManager; // 偏好设置管理器
// 导入项目资源
import net.micode.notes.R; // 项目资源文件
import net.micode.notes.ui.NotesPreferenceActivity; // 偏好设置Activity
/**
* 资源解析工具类
* 用于获取和管理笔记应用的各种UI资源
*/
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;
// 默认背景颜色(黄色)
public static final int BG_DEFAULT_COLOR = YELLOW;
// ======================== 字体大小常量定义 ========================
// 小号字体
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;
// 默认字体大小(中号)
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;
/**
* 笔记背景资源类
* 提供编辑界面的背景资源获取方法
*/
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 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 // 红色标题背景
};
/**
* 获取笔记背景资源
* @param id 颜色IDYELLOW/BLUE/WHITE/GREEN/RED
* @return 对应的背景资源ID
*/
public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id];
}
/**
* 获取笔记标题背景资源
* @param id 颜色IDYELLOW/BLUE/WHITE/GREEN/RED
* @return 对应的标题背景资源ID
*/
public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id];
}
}
/**
* 获取默认背景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;
}
}
/**
* 笔记列表项背景资源类
* 提供笔记列表的各种背景资源获取方法
*/
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 // 红色背景第一项
};
// 列表中间项背景资源数组
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 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 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 // 红色背景单项
};
/**
* 获取列表第一项背景资源
* @param id 颜色ID
* @return 对应的背景资源ID
*/
public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id];
}
/**
* 获取列表最后一项背景资源
* @param id 颜色ID
* @return 对应的背景资源ID
*/
public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id];
}
/**
* 获取列表单项背景资源
* @param id 颜色ID
* @return 对应的背景资源ID
*/
public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id];
}
/**
* 获取列表中间项背景资源
* @param id 颜色ID
* @return 对应的背景资源ID
*/
public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id];
}
/**
* 获取文件夹背景资源
* @return 文件夹背景资源ID
*/
public static int getFolderBgRes() {
return R.drawable.list_folder;
}
}
/**
* 小部件背景资源类
* 提供小部件的背景资源获取方法
*/
public static class WidgetBgResources {
// 2x大小小部件背景资源数组
private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow, // 黄色2x小部件
R.drawable.widget_2x_blue, // 蓝色2x小部件
R.drawable.widget_2x_white, // 白色2x小部件
R.drawable.widget_2x_green, // 绿色2x小部件
R.drawable.widget_2x_red, // 红色2x小部件
};
/**
* 获取2x小部件背景资源
* @param id 颜色ID
* @return 对应的背景资源ID
*/
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
}
// 4x大小小部件背景资源数组
private final static int [] BG_4X_RESOURCES = new int [] {
R.drawable.widget_4x_yellow, // 黄色4x小部件
R.drawable.widget_4x_blue, // 蓝色4x小部件
R.drawable.widget_4x_white, // 白色4x小部件
R.drawable.widget_4x_green, // 绿色4x小部件
R.drawable.widget_4x_red // 红色4x小部件
};
/**
* 获取4x小部件背景资源
* @param id 颜色ID
* @return 对应的背景资源ID
*/
public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id];
}
}
/**
* 文本外观资源类
* 提供文本样式资源的获取方法
*/
public static class TextAppearanceResources {
// 文本样式资源数组
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal, // 普通文本样式
R.style.TextAppearanceMedium, // 中等文本样式
R.style.TextAppearanceLarge, // 大号文本样式
R.style.TextAppearanceSuper // 超大号文本样式
};
/**
* 获取文本样式资源
* @param id 字体大小IDTEXT_SMALL/MEDIUM/LARGE/SUPER
* @return 对应的样式资源ID
*/
public static int getTexAppearanceResource(int id) {
/*
* 修复在共享偏好设置中存储资源ID的bug
* 如果ID大于资源数组长度则返回默认字体大小
*/
if (id >= TEXTAPPEARANCE_RESOURCES.length) {
return BG_DEFAULT_FONT_SIZE;
}
return TEXTAPPEARANCE_RESOURCES[id];
}
/**
* 获取文本样式资源总数
* @return 样式资源数量
*/
public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length;
}
}
}