diff --git a/ResourceParser.java b/ResourceParser.java deleted file mode 100644 index 64293eb..0000000 --- a/ResourceParser.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * 版权所有 (c) 2010-2011, MiCode 开源社区 (www.micode.net) - * - * 本文件根据 Apache License, Version 2.0 (以下简称“许可证”)授权使用; - * 除非符合许可证的规定,否则您不得使用此文件。 - * 您可以在以下网址获取许可证的副本: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 除非适用法律要求或以书面形式同意,根据许可证分发的软件将按“现状”分发, - * 不提供任何明示或暗示的保证或条件。 - * 请参阅许可证以了解控制权限和许可证下限制的特定语言。 - */ - -package net.micode.notes.tool; - -import android.content.Context; // 引入Android上下文类,用于访问应用的资源和类以及调用应用级操作 -import android.preference.PreferenceManager; // 引入Android偏好设置管理器类,用于访问应用的偏好设置 - -import net.micode.notes.R; // 引入应用的资源类,用于访问应用资源 -import net.micode.notes.ui.NotesPreferenceActivity; // 引入应用的偏好设置活动类 - -// ResourceParser类,用于解析和管理应用的资源 -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; - - // NoteBgResources内部类,用于管理笔记背景资源 - 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 - }; - - // 获取指定ID的编辑背景资源 - public static int getNoteBgResource(int id) { - return BG_EDIT_RESOURCES[id]; - } - - // 获取指定ID的编辑标题背景资源 - public static int getNoteTitleBgResource(int id) { - return BG_EDIT_TITLE_RESOURCES[id]; - } - } - - // 获取默认背景颜色ID - public static int getDefaultBgId(Context context) { - // 检查用户是否设置了背景颜色偏好 - if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( - NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { - // 如果设置了,则随机返回一个背景颜色ID - return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); - } else { - // 否则返回默认背景颜色ID - return BG_DEFAULT_COLOR; - } - } - - // NoteItemBgResources内部类,用于管理笔记条目背景资源 - 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 - }; - - // 获取指定ID的第一项背景资源 - public static int getNoteBgFirstRes(int id) { - return BG_FIRST_RESOURCES[id]; - } - - // 获取指定ID的最后一项背景资源 - public static int getNoteBgLastRes(int id) { - return BG_LAST_RESOURCES[id]; - } - - // 获取指定ID的单个项背景资源 - public static int getNoteBgSingleRes(int id) { - return BG_SINGLE_RESOURCES[id]; - } - - // 获取指定ID的中间项背景资源 - public static int getNoteBgNormalRes(int id) { - return BG_NORMAL_RESOURCES[id]; - } - - // 获取文件夹背景资源 - public static int getFolderBgRes() { - return R.drawable.list_folder; - } - } - - // WidgetBgResources内部类,用于管理小部件背景资源 - public static class WidgetBgResources { - // 定义2x大小的小部件背景资源数组 - 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大小小部件背景资源 - 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, - R.drawable.widget_4x_blue, - R.drawable.widget_4x_white, - R.drawable.widget_4x_green, - R.drawable.widget_4x_red - }; - - // 获取指定ID的4x大小小部件背景资源 - public static int getWidget4xBgResource(int id) { - return BG_4X_RESOURCES[id]; - } - } - - // TextAppearanceResources内部类,用于管理文本外观资源 - public static class TextAppearanceResources { - // 定义文本外观资源数组 - private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { - R.style.TextAppearanceNormal, - R.style.TextAppearanceMedium, - R.style.TextAppearanceLarge, - R.style.TextAppearanceSuper - }; - - // 获取指定ID的文本外观资源 - // 注意:这里有一个修正(HACKME),用于处理存储在偏好设置中的资源ID可能超出资源数组长度的情况 - public static int getTexAppearanceResource(int id) { - if (id >= TEXTAPPEARANCE_RESOURCES.length) { - return BG_DEFAULT_FONT_SIZE; - } - return TEXTAPPEARANCE_RESOURCES[id]; - } - - // 获取文本外观资源数组的大小 - public static int getResourcesSize() { - return TEXTAPPEARANCE_RESOURCES.length; - } - } -} \ No newline at end of file diff --git a/widget/NoteWidgetProvider.java b/widget/NoteWidgetProvider.java new file mode 100644 index 0000000..0d6feb9 --- /dev/null +++ b/widget/NoteWidgetProvider.java @@ -0,0 +1,144 @@ +/* + * 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.widget; +import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProvider; +import android.content.ContentValues; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.util.Log; +import android.widget.RemoteViews; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.tool.ResourceParser; +import net.micode.notes.ui.NoteEditActivity; +import net.micode.notes.ui.NotesListActivity; + +// 继承自AppWidgetProvider的抽象类,用于管理便签小部件。 +public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 定义查询数据库时需要的列。 + public static final String [] PROJECTION = new String [] { + NoteColumns.ID, + NoteColumns.BG_COLOR_ID, + NoteColumns.SNIPPET + }; + + // 定义列索引常量。 + public static final int COLUMN_ID = 0; + public static final int COLUMN_BG_COLOR_ID = 1; + public static final int COLUMN_SNIPPET = 2; + + // 日志标签,用于Logcat中识别日志来源。 + private static final String TAG = "NoteWidgetProvider"; + + // 当小部件被删除时调用,更新数据库中对应的笔记widget id为无效值。 + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + ContentValues values = new ContentValues(); + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + for (int i = 0; i < appWidgetIds.length; i++) { + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", + new String[] { String.valueOf(appWidgetIds[i])}); + } + } + + // 获取小部件相关的笔记信息。 + private Cursor getNoteWidgetInfo(Context context, int widgetId) { + return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, + PROJECTION, + NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", + new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) }, + null); + } + + // 更新小部件显示。 + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + update(context, appWidgetManager, appWidgetIds, false); + } + + // 更新小部件显示,考虑隐私模式。 + private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, + boolean privacyMode) { + for (int i = 0; i < appWidgetIds.length; i++) { + if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { + int bgId = ResourceParser.getDefaultBgId(context); + String snippet = ""; + Intent intent = new Intent(context, NoteEditActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); + + Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); + if (c != null && c.moveToFirst()) { + if (c.getCount() > 1) { + Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]); + c.close(); + return; + } + snippet = c.getString(COLUMN_SNIPPET); + bgId = c.getInt(COLUMN_BG_COLOR_ID); + intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); + intent.setAction(Intent.ACTION_VIEW); + } else { + snippet = context.getResources().getString(R.string.widget_havenot_content); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); + } + + if (c != null) { + c.close(); + } + + // 设置小部件的布局和行为。 + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); + /** + * 生成启动宿主的PendingIntent。 + */ + PendingIntent pendingIntent = null; + if (privacyMode) { + rv.setTextViewText(R.id.widget_text, + context.getString(R.string.widget_under_visit_mode)); + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( + context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); + } else { + rv.setTextViewText(R.id.widget_text, snippet); + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); + } + } + } + + // 获取背景资源ID的方法,需由子类实现。 + protected abstract int getBgResourceId(int bgId); + + // 获取布局ID的方法,需由子类实现。 + protected abstract int getLayoutId(); + + // 获取小部件类型的方法,需由子类实现。 + protected abstract int getWidgetType(); +} \ No newline at end of file diff --git a/widget/NoteWidgetProvider_2x.java b/widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..b1967fe --- /dev/null +++ b/widget/NoteWidgetProvider_2x.java @@ -0,0 +1,55 @@ +/* + * 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.widget; + +import android.appwidget.AppWidgetManager; +import android.content.Context; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.tool.ResourceParser; + +// NoteWidgetProvider_2x 是 NoteWidgetProvider 的具体实现类,用于提供2x2网格大小的便签小部件。 +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + // 当小部件需要更新时调用此方法。 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的 update 方法来执行更新。 + super.update(context, appWidgetManager, appWidgetIds); + } + + // 返回小部件的布局资源ID。 + @Override + protected int getLayoutId() { + // 返回2x2网格小部件的布局资源ID。 + return R.layout.widget_2x; + } + + // 根据传入的背景ID返回对应的资源ID。 + @Override + protected int getBgResourceId(int bgId) { + // 使用 ResourceParser 类的静态方法来获取2x2网格小部件的背景资源ID。 + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + // 返回小部件的类型。 + @Override + protected int getWidgetType() { + // 返回小部件的类型为TYPE_WIDGET_2X,这是一个定义在Notes类中的常量。 + return Notes.TYPE_WIDGET_2X; + } +} \ No newline at end of file diff --git a/widget/NoteWidgetProvider_4x.java b/widget/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..39ad28e --- /dev/null +++ b/widget/NoteWidgetProvider_4x.java @@ -0,0 +1,55 @@ +/* + * 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.widget; + +import android.appwidget.AppWidgetManager; +import android.content.Context; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.tool.ResourceParser; + +// NoteWidgetProvider_4x 类继承自 NoteWidgetProvider 类,用于提供4x4网格大小的便签小部件。 +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + // 当小部件需要更新时,系统会调用此方法。 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的 update 方法来更新小部件。 + super.update(context, appWidgetManager, appWidgetIds); + } + + // 返回小部件的布局资源ID。 + @Override + protected int getLayoutId() { + // 返回4x4网格小部件的布局资源ID。 + return R.layout.widget_4x; + } + + // 根据传入的背景ID返回对应的资源ID。 + @Override + protected int getBgResourceId(int bgId) { + // 使用 ResourceParser 类的静态方法来获取4x4网格小部件的背景资源ID。 + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + // 返回小部件的类型。 + @Override + protected int getWidgetType() { + // 返回小部件的类型为TYPE_WIDGET_4X,这是一个定义在Notes类中的常量。 + return Notes.TYPE_WIDGET_4X; + } +} \ No newline at end of file