From 1a0f9d62c02e96fb7d65368d68ae707c614ff025 Mon Sep 17 00:00:00 2001 From: shihaoxuan <1514599782@qq.com> Date: Sun, 29 Dec 2024 20:51:25 +0800 Subject: [PATCH] explanatory note --- src/widget/NoteWidgetProvider.java | 162 ++++++++++++++++++++++++++ src/widget/NoteWidgetProvider_2x.java | 63 ++++++++++ src/widget/NoteWidgetProvider_4x.java | 63 ++++++++++ 3 files changed, 288 insertions(+) create mode 100644 src/widget/NoteWidgetProvider.java create mode 100644 src/widget/NoteWidgetProvider_2x.java create mode 100644 src/widget/NoteWidgetProvider_4x.java diff --git a/src/widget/NoteWidgetProvider.java b/src/widget/NoteWidgetProvider.java new file mode 100644 index 0000000..a39041a --- /dev/null +++ b/src/widget/NoteWidgetProvider.java @@ -0,0 +1,162 @@ +/* + * 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,用于管理Note小部件的抽象类 +public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 定义查询数据库时需要返回的列 + public static final String [] PROJECTION = new String [] { + NoteColumns.ID, // 笔记ID + NoteColumns.BG_COLOR_ID, // 笔记背景颜色ID + NoteColumns.SNIPPET // 笔记摘要 + }; + + // 定义PROJECTION数组中各列的索引,方便后续操作 + public static final int COLUMN_ID = 0; + public static final int COLUMN_BG_COLOR_ID = 1; + public static final int COLUMN_SNIPPET = 2; + + // 用于日志输出的标签 + private static final String TAG = "NoteWidgetProvider"; + + // 当小部件被删除时,更新数据库中对应的笔记,将WIDGET_ID设置为无效 + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + // 创建ContentValues对象,用于更新数据库 + ContentValues values = new ContentValues(); + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); // 设置WIDGET_ID为无效值 + // 遍历所有被删除的小部件ID + for (int i = 0; i < appWidgetIds.length; i++) { + // 更新数据库中对应小部件ID的笔记,将WIDGET_ID设置为无效 + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", + new String[] { String.valueOf(appWidgetIds[i])}); + } + } + + // 获取与小部件相关的笔记信息 + 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); + } + + // 更新小部件的显示内容,该方法被外部调用 + 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) { + // 遍历所有小部件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 = ""; + // 创建意图,用于启动NoteEditActivity + Intent intent = new Intent(context, NoteEditActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 设置意图标志,确保Activity单实例 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); // 传递小部件ID + 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; + } + // 获取笔记摘要和背景ID + 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对象,用于更新小部件视图 + 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,用于启动宿主Activity + PendingIntent pendingIntent = null; + if (privacyMode) { + // 隐私模式下,显示默认文本,并启动笔记列表Activity + 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 { + // 非隐私模式下,显示笔记摘要,并启动编辑笔记Activity + rv.setTextViewText(R.id.widget_text, snippet); + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + + // 设置点击事件,点击小部件时启动对应的Activity + 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/src/widget/NoteWidgetProvider_2x.java b/src/widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..db93365 --- /dev/null +++ b/src/widget/NoteWidgetProvider_2x.java @@ -0,0 +1,63 @@ +/* + * 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网格大小的笔记小部件(Note Widget)。 +// 这个类提供了小部件的更新逻辑,以及如何根据笔记数据渲染小部件的UI。 +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + + // 当AppWidgetManager需要更新小部件时,会调用此方法。 + // 这是AppWidgetProvider生命周期中的一个重要方法,用于响应小部件的更新请求。 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类(NoteWidgetProvider)的update方法,传入必要的参数,以更新小部件的UI。 + // 这里使用super关键字来访问父类的update方法,确保父类的逻辑被正确执行。 + super.update(context, appWidgetManager, appWidgetIds); + } + + // getLayoutId方法被用来获取小部件的布局资源ID。 + // 每个小部件都有自己的布局文件,这里返回的是2x2网格小部件的布局资源ID。 + @Override + protected int getLayoutId() { + // 返回R.layout.widget_2x,这是2x2网格小部件对应的布局资源ID。 + return R.layout.widget_2x; + } + + // getBgResourceId方法根据传入的背景ID(bgId),返回对应的背景资源ID。 + // 这个方法使用ResourceParser工具类中的WidgetBgResources来获取正确的背景资源ID。 + @Override + protected int getBgResourceId(int bgId) { + // 调用ResourceParser.WidgetBgResources的getWidget2xBgResource方法,传入bgId。 + // 这个方法负责根据bgId返回2x2网格小部件的背景资源ID。 + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + // getWidgetType方法返回当前小部件的类型。 + // 小部件类型用于区分不同的小部件,例如,不同的网格大小可能会有不同的类型。 + @Override + protected int getWidgetType() { + // 返回Notes.TYPE_WIDGET_2X,这是一个常量,表示当前小部件是2x2网格大小的笔记小部件。 + return Notes.TYPE_WIDGET_2X; + } +} \ No newline at end of file diff --git a/src/widget/NoteWidgetProvider_4x.java b/src/widget/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..d325630 --- /dev/null +++ b/src/widget/NoteWidgetProvider_4x.java @@ -0,0 +1,63 @@ +/* + * 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网格大小的Note小部件功能。 +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + + // 当AppWidgetManager需要更新小部件时,会调用此方法。 + // 这是AppWidgetProvider生命周期中的一个重要方法,用于响应小部件的更新请求。 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类(NoteWidgetProvider)的update方法,传入必要的参数,以更新小部件的UI。 + // 这里使用super关键字来访问父类的update方法,确保父类的逻辑被正确执行。 + super.update(context, appWidgetManager, appWidgetIds); + } + + // getLayoutId方法被用来获取小部件的布局资源ID。 + // 每个小部件都有自己的布局文件,这里返回的是4x4网格小部件的布局资源ID。 + // 注意:该方法没有@Override注解,这是一个遗漏,因为方法体与父类中的方法签名相同, + // 应该加上@Override注解以明确表示这是一个重写的方法。 + protected int getLayoutId() { + // 返回R.layout.widget_4x,这是4x4网格小部件对应的布局资源ID。 + return R.layout.widget_4x; + } + + // getBgResourceId方法根据传入的背景ID(bgId),返回对应的背景资源ID。 + // 这个方法使用ResourceParser工具类中的WidgetBgResources来获取正确的背景资源ID。 + @Override + protected int getBgResourceId(int bgId) { + // 调用ResourceParser.WidgetBgResources的getWidget4xBgResource方法,传入bgId。 + // 这个方法负责根据bgId返回4x4网格小部件的背景资源ID。 + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + // getWidgetType方法返回当前小部件的类型。 + // 小部件类型用于区分不同的小部件,例如,不同的网格大小可能会有不同的类型。 + @Override + protected int getWidgetType() { + // 返回Notes.TYPE_WIDGET_4X,这是一个常量,表示当前小部件是4x4网格大小的笔记小部件。 + return Notes.TYPE_WIDGET_4X; + } +} \ No newline at end of file