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.
xiangmuyi/ResourceParser.java

222 lines
12 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.
*/
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;
}
}
}