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.
123456/src/java/net/micode/notes/tool/ResourceParser.java

211 lines
9.8 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.
* 总体分析
这段 Java 代码定义了ResourceParser类主要用于对应用中的各类资源如背景图片资源、文本外观样式资源等进行管理与解析方便根据不同的条件获取对应的资源标识符如资源 ID。它内部定义了多个静态内部类分别针对笔记背景、笔记项背景、部件背景以及文本外观等不同方面的资源进行组织同时还定义了一些表示默认状态如默认颜色、默认字体大小等的常量。通过结合应用的偏好设置等逻辑可以灵活地获取相应的资源 ID为实现界面的多样化显示例如不同颜色主题、不同字体大小展示等提供了资源获取的统一入口有助于提升代码在资源管理方面的可维护性和可扩展性。
函数分析
NoteBgResources类相关函数
所属类ResourceParser.NoteBgResourcesResourceParser的内部类
功能:
getNoteBgResource方法根据传入的id参数从预定义的BG_EDIT_RESOURCES数组中获取对应的笔记编辑背景资源的ID并返回用于获取指定的笔记编辑背景相关资源。
getNoteTitleBgResource方法依据传入的id参数从BG_EDIT_TITLE_RESOURCES数组中获取对应的笔记标题编辑背景资源的ID并返回方便获取相应的笔记标题编辑背景资源。
getDefaultBgId方法
所属类ResourceParser
功能通过获取应用的默认共享偏好设置判断是否开启了特定的背景颜色随机设置的偏好项NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY对应的布尔值若开启则随机生成一个在NoteBgResources.BG_EDIT_RESOURCES数组长度范围内的索引值作为背景ID返回若未开启则返回默认的背景颜色IDBG_DEFAULT_COLOR以此来确定默认的背景资源ID。
NoteItemBgResources类相关函数
所属类ResourceParser.NoteItemBgResourcesResourceParser的内部类
功能:
getNoteBgFirstRes方法根据传入的id参数从BG_FIRST_RESOURCES数组中获取对应的笔记项首行背景资源的ID并返回用于获取特定的笔记项首行背景资源。
getNoteBgLastRes方法依据传入的id参数从BG_LAST_RESOURCES数组中获取对应的笔记项末行背景资源的ID并返回方便获取相应的笔记项末行背景资源。
getNoteBgSingleRes方法按照传入的id参数从BG_SINGLE_RESOURCES数组中获取对应的单个笔记项背景资源的ID并返回用于获取单个笔记项对应的背景资源。
getNoteBgNormalRes方法根据传入的id参数从BG_NORMAL_RESOURCES数组中获取对应的笔记项普通行背景资源的ID并返回便于获取笔记项普通行的背景资源。
getFolderBgRes方法直接返回代表文件夹背景资源的IDR.drawable.list_folder用于获取文件夹的背景资源。
WidgetBgResources类相关函数
所属类ResourceParser.WidgetBgResourcesResourceParser的内部类
功能:
getWidget2xBgResource方法根据传入的id参数从BG_2X_RESOURCES数组中获取对应的2x部件背景资源的ID并返回用于获取相应尺寸部件的背景资源。
getWidget4xBgResource方法依据传入的id参数从BG_4X_RESOURCES数组中获取对应的4x部件背景资源的ID并返回方便获取特定尺寸部件的背景资源。
TextAppearanceResources类相关函数
所属类ResourceParser.TextAppearanceResourcesResourceParser的内部类
功能:
getTexAppearanceResource方法根据传入的id参数先判断该参数是否超出TEXTAPPEARANCE_RESOURCES数组长度范围若超出则返回默认的字体大小对应的资源IDBG_DEFAULT_FONT_SIZE若未超出则从该数组中获取对应的文本外观资源的ID并返回用于获取合适的文本外观样式资源。
getResourcesSize方法直接返回TEXTAPPEARANCE_RESOURCES数组的长度用于知晓文本外观资源的数量情况。
*/
package net.micode.notes.tool;
import android.content.Context;
import android.preference.PreferenceManager;
import net.micode.notes.R;
import net.micode.notes.ui.NotesPreferenceActivity;
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
};
public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id];
}
public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[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
};
public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id];
}
public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id];
}
public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id];
}
public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id];
}
public static int getFolderBgRes() {
return R.drawable.list_folder;
}
}
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,
};
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
}
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
};
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
};
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;
}
return TEXTAPPEARANCE_RESOURCES[id];
}
public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length;
}
}
}