/* * 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.NoteBgResources(ResourceParser的内部类) 功能: 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返回,若未开启则返回默认的背景颜色ID(BG_DEFAULT_COLOR),以此来确定默认的背景资源ID。 NoteItemBgResources类相关函数 所属类:ResourceParser.NoteItemBgResources(ResourceParser的内部类) 功能: 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方法:直接返回代表文件夹背景资源的ID(R.drawable.list_folder),用于获取文件夹的背景资源。 WidgetBgResources类相关函数 所属类:ResourceParser.WidgetBgResources(ResourceParser的内部类) 功能: getWidget2xBgResource方法:根据传入的id参数,从BG_2X_RESOURCES数组中获取对应的2x部件背景资源的ID并返回,用于获取相应尺寸部件的背景资源。 getWidget4xBgResource方法:依据传入的id参数,从BG_4X_RESOURCES数组中获取对应的4x部件背景资源的ID并返回,方便获取特定尺寸部件的背景资源。 TextAppearanceResources类相关函数 所属类:ResourceParser.TextAppearanceResources(ResourceParser的内部类) 功能: getTexAppearanceResource方法:根据传入的id参数,先判断该参数是否超出TEXTAPPEARANCE_RESOURCES数组长度范围,若超出则返回默认的字体大小对应的资源ID(BG_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; } } }