/* * 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.preference.PreferenceManager; import net.micode.notes.R; import net.micode.notes.ui.NotesPreferenceActivity; // ResourceParser类主要用于解析和提供应用中各种与资源相关的信息,例如背景颜色、字体大小以及不同界面元素对应的资源ID等,方便在应用的不同地方统一获取和使用这些资源相关设置 public class ResourceParser { // 定义表示黄色的常量,用于标识某种颜色选项,可能在背景颜色等设置中使用,值为0,可作为索引或标识值 public static final int YELLOW = 0; // 定义表示蓝色的常量,类似黄色的作用,用于区分不同颜色选项,值为1 public static final int BLUE = 1; // 定义表示白色的常量,值为2 public static final int WHITE = 2; // 定义表示绿色的常量,值为3 public static final int GREEN = 3; // 定义表示红色的常量,值为4 public static final int RED = 4; // 定义默认的背景颜色的常量,初始设置为黄色(对应前面定义的YELLOW常量值),表示在没有特定设置时的默认背景颜色选项 public static final int BG_DEFAULT_COLOR = YELLOW; // 定义表示小字体的常量,用于字体大小相关的标识,值为0 public static final int TEXT_SMALL = 0; // 定义表示中字体的常量,值为1 public static final int TEXT_MEDIUM = 1; // 定义表示大字体的常量,值为2 public static final int TEXT_LARGE = 2; // 定义表示超大字体的常量,值为3 public static final int TEXT_SUPER = 3; // 定义默认的字体大小的常量,初始设置为中字体(对应前面定义的TEXT_MEDIUM常量值),表示在没有特定设置时的默认字体大小选项 public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; // 内部静态类NoteBgResources,用于管理笔记编辑界面相关的背景资源,例如背景图片资源ID等 public static class NoteBgResources { // 定义一个静态的整数数组,存储笔记编辑界面背景图片资源ID,对应不同的颜色选项(按顺序依次对应前面定义的颜色常量顺序) 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 }; // 定义一个静态的整数数组,存储笔记编辑界面标题背景图片资源ID,同样对应不同的颜色选项 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 }; // 静态方法,根据传入的索引id获取笔记编辑界面的背景图片资源ID,返回对应的BG_EDIT_RESOURCES数组中的元素(即资源ID) public static int getNoteBgResource(int id) { return BG_EDIT_RESOURCES[id]; } // 静态方法,根据传入的索引id获取笔记编辑界面标题的背景图片资源ID,返回对应的BG_EDIT_TITLE_RESOURCES数组中的元素(即资源ID) public static int getNoteTitleBgResource(int id) { return BG_EDIT_TITLE_RESOURCES[id]; } } // 静态方法,用于获取默认的背景颜色对应的ID值,根据应用的偏好设置(Preference)来决定返回值 public static int getDefaultBgId(Context context) { // 获取应用默认的共享偏好设置对象,通过它可以读取用户之前设置的各种偏好选项 if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { // 如果用户设置了特定的背景颜色选项(通过对应的偏好设置键值判断,这里假设为PREFERENCE_SET_BG_COLOR_KEY,值为true表示已设置) // 则随机生成一个索引值(范围是NoteBgResources类中背景资源数组的长度范围内),作为返回的背景颜色ID return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); } else { // 如果用户没有设置特定的背景颜色选项,则返回默认的背景颜色ID(前面定义的BG_DEFAULT_COLOR) return BG_DEFAULT_COLOR; } } // 内部静态类NoteItemBgResources,用于管理笔记列表项相关的背景资源,如不同位置(首个、中间、最后等)的背景图片资源ID public static class NoteItemBgResources { // 定义一个静态的整数数组,存储笔记列表项首个位置的背景图片资源ID,对应不同的颜色选项 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 }; // 定义一个静态的整数数组,存储笔记列表项中间位置的背景图片资源ID,对应不同的颜色选项 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 }; // 定义一个静态的整数数组,存储笔记列表项最后位置的背景图片资源ID,对应不同的颜色选项 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, }; // 定义一个静态的整数数组,存储笔记列表项单个显示时(可能没有前后相邻项的情况)的背景图片资源ID,对应不同的颜色选项 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 }; // 静态方法,根据传入的索引id获取笔记列表项首个位置的背景图片资源ID,返回对应的BG_FIRST_RESOURCES数组中的元素(即资源ID) public static int getNoteBgFirstRes(int id) { return BG_FIRST_RESOURCES[id]; } // 静态方法,根据传入的索引id获取笔记列表项最后位置的背景图片资源ID,返回对应的BG_LAST_RESOURCES数组中的元素(即资源ID) public static int getNoteBgLastRes(int id) { return BG_LAST_RESOURCES[id]; } // 静态方法,根据传入的索引id获取笔记列表项单个显示时的背景图片资源ID,返回对应的BG_SINGLE_RESOURCES数组中的元素(即资源ID) public static int getNoteBgSingleRes(int id) { return BG_SINGLE_RESOURCES[id]; } // 静态方法,根据传入的索引id获取笔记列表项中间位置的背景图片资源ID,返回对应的BG_NORMAL_RESOURCES数组中的元素(即资源ID) public static int getNoteBgNormalRes(int id) { return BG_NORMAL_RESOURCES[id]; } // 静态方法,获取文件夹的背景图片资源ID,返回固定的文件夹背景图片资源ID(这里假设为R.drawable.list_folder) public static int getFolderBgRes() { return R.drawable.list_folder; } } // 内部静态类WidgetBgResources,用于管理应用小部件(Widget)相关的背景资源,如不同尺寸小部件对应的背景图片资源ID public static class WidgetBgResources { // 定义一个静态的整数数组,存储2x尺寸小部件的背景图片资源ID,对应不同的颜色选项 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, }; // 静态方法,根据传入的索引id获取2x尺寸小部件的背景图片资源ID,返回对应的BG_2X_RESOURCES数组中的元素(即资源ID) public static int getWidget2xBgResource(int id) { return BG_2X_RESOURCES[id]; } // 定义一个静态的整数数组,存储4x尺寸小部件的背景图片资源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 }; // 静态方法,根据传入的索引id获取4x尺寸小部件的背景图片资源ID,返回对应的BG_4X_RESOURCES数组中的元素(即资源ID) public static int getWidget4xBgResource(int id) { return BG_4X_RESOURCES[id]; } } // 内部静态类TextAppearanceResources,用于管理文本外观相关的资源,如不同字体大小对应的样式资源ID等 public static class TextAppearanceResources { // 定义一个静态的整数数组,存储不同字体大小对应的文本外观样式资源ID(按顺序对应前面定义的字体大小常量顺序) private final static int[] TEXTAPPEARANCE_RESOURCES = new int[] { R.style.TextAppearanceNormal, R.style.TextAppearanceMedium, R.style.TextAppearanceLarge, R.style.TextAppearanceSuper }; // 静态方法,根据传入的索引id获取文本外观样式资源ID,同时进行了边界检查,如果传入的id超出资源数组长度范围,则返回默认的字体大小对应的资源ID(避免出现资源获取异常情况) 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; } } }