diff --git a/src/Widget/NoteWidgetProvider.java b/src/Widget/NoteWidgetProvider.java new file mode 100644 index 0000000..bac6ff6 --- /dev/null +++ b/src/Widget/NoteWidgetProvider.java @@ -0,0 +1,226 @@ +/* + * 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; + +/** + * 抽象类 NoteWidgetProvider,继承自 AppWidgetProvider,用于处理笔记小部件的相关操作。 + * 提供了小部件删除、更新等功能,并且定义了一些抽象方法供子类实现特定的资源获取。 + */ +public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 查询笔记信息时使用的投影,指定要查询的列 + public static final String [] PROJECTION = new String [] { + NoteColumns.ID, + NoteColumns.BG_COLOR_ID, + NoteColumns.SNIPPET + }; + + // 笔记 ID 在投影数组中的索引 + public static final int COLUMN_ID = 0; + // 笔记背景颜色 ID 在投影数组中的索引 + public static final int COLUMN_BG_COLOR_ID = 1; + // 笔记摘要在投影数组中的索引 + public static final int COLUMN_SNIPPET = 2; + + // 日志标签,用于在日志中标识该类的相关信息 + private static final String TAG = "NoteWidgetProvider"; + + /** + * 当小部件被删除时调用此方法。 + * 该方法会将对应笔记的小部件 ID 设置为无效值。 + * + * @param context 上下文对象 + * @param appWidgetIds 被删除的小部件 ID 数组 + */ + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + // 创建 ContentValues 对象,用于存储要更新的字段和值 + ContentValues values = new ContentValues(); + // 将笔记的小部件 ID 设置为无效值 + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + // 遍历被删除的小部件 ID 数组 + for (int i = 0; i < appWidgetIds.length; i++) { + // 更新笔记数据库中对应小部件 ID 的笔记记录 + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", + new String[] { String.valueOf(appWidgetIds[i])}); + } + } + + /** + * 根据小部件 ID 获取笔记小部件的信息。 + * + * @param context 上下文对象 + * @param widgetId 小部件 ID + * @return 包含笔记信息的游标,如果查询失败则返回 null + */ + private Cursor getNoteWidgetInfo(Context context, int widgetId) { + // 查询笔记数据库,获取指定小部件 ID 且不在回收站的笔记信息 + 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); + } + + /** + * 更新小部件的方法,默认不开启隐私模式。 + * + * @param context 上下文对象 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 要更新的小部件 ID 数组 + */ + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用带隐私模式参数的更新方法,隐私模式为 false + update(context, appWidgetManager, appWidgetIds, false); + } + + /** + * 更新小部件的具体实现方法。 + * 根据小部件 ID 获取笔记信息,设置小部件的背景、文本和点击事件等。 + * + * @param context 上下文对象 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 要更新的小部件 ID 数组 + * @param privacyMode 是否开启隐私模式 + */ + private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, + boolean privacyMode) { + // 遍历要更新的小部件 ID 数组 + for (int i = 0; i < appWidgetIds.length; i++) { + // 检查小部件 ID 是否有效 + if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { + // 获取默认的背景颜色 ID + int bgId = ResourceParser.getDefaultBgId(context); + // 初始化笔记摘要为空字符串 + String snippet = ""; + // 创建一个启动笔记编辑活动的意图 + Intent intent = new Intent(context, NoteEditActivity.class); + // 设置意图标志,确保活动以单顶模式启动 + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + // 将小部件 ID 作为额外数据添加到意图中 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); + // 将小部件类型作为额外数据添加到意图中 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); + + // 获取指定小部件 ID 的笔记信息游标 + Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); + if (c != null && c.moveToFirst()) { + // 检查是否存在多条具有相同小部件 ID 的笔记记录 + if (c.getCount() > 1) { + // 记录错误日志 + Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]); + // 关闭游标 + c.close(); + return; + } + // 从游标中获取笔记摘要 + snippet = c.getString(COLUMN_SNIPPET); + // 从游标中获取笔记背景颜色 ID + bgId = c.getInt(COLUMN_BG_COLOR_ID); + // 将笔记 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 对象,用于设置小部件的布局 + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); + // 设置小部件的背景图片资源 + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); + // 将背景颜色 ID 作为额外数据添加到意图中 + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); + + /** + * 生成用于启动小部件宿主的待定意图 + */ + 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); + // 更新指定小部件 ID 的小部件布局 + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); + } + } + } + + /** + * 抽象方法,用于获取指定背景颜色 ID 对应的背景资源 ID。 + * 子类需要实现该方法来提供具体的背景资源。 + * + * @param bgId 背景颜色 ID + * @return 背景资源 ID + */ + protected abstract int getBgResourceId(int bgId); + + /** + * 抽象方法,用于获取小部件的布局 ID。 + * 子类需要实现该方法来提供具体的布局资源。 + * + * @return 布局 ID + */ + protected abstract int getLayoutId(); + + /** + * 抽象方法,用于获取小部件的类型。 + * 子类需要实现该方法来提供具体的小部件类型。 + * + * @return 小部件类型 + */ + protected abstract int getWidgetType(); +} \ No newline at end of file