From 09f2d591a003dfdb022236579cd93a2ff963612d Mon Sep 17 00:00:00 2001 From: pv4gs5wt7 <2508757972@qq.com> Date: Thu, 27 Mar 2025 18:58:36 +0800 Subject: [PATCH] zhushi --- .../notes/widget/NoteWidgetProvider.java | 65 ++++++++++++++----- .../notes/widget/NoteWidgetProvider_2x.java | 8 +++ .../notes/widget/NoteWidgetProvider_4x.java | 8 +++ 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/src/net/micode/notes/widget/NoteWidgetProvider.java b/src/src/net/micode/notes/widget/NoteWidgetProvider.java index ec6f819..d8cfecf 100644 --- a/src/src/net/micode/notes/widget/NoteWidgetProvider.java +++ b/src/src/net/micode/notes/widget/NoteWidgetProvider.java @@ -33,20 +33,26 @@ import net.micode.notes.ui.NoteEditActivity; import net.micode.notes.ui.NotesListActivity; public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 定义查询笔记信息的列 public static final String [] PROJECTION = new String [] { - NoteColumns.ID, - NoteColumns.BG_COLOR_ID, - NoteColumns.SNIPPET + NoteColumns.ID, // 笔记ID + NoteColumns.BG_COLOR_ID, // 背景颜色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; + + // 定义列的索引 + public static final int COLUMN_ID = 0; // 笔记ID列索引 + public static final int COLUMN_BG_COLOR_ID = 1; // 背景颜色ID列索引 + public static final int COLUMN_SNIPPET = 2; // 笔记摘要列索引 + + // 日志标签 private static final String TAG = "NoteWidgetProvider"; @Override public void onDeleted(Context context, int[] appWidgetIds) { + // 当小部件被删除时,更新数据库中的小部件ID为无效值 ContentValues values = new ContentValues(); values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); for (int i = 0; i < appWidgetIds.length; i++) { @@ -57,6 +63,7 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { } } + // 获取指定小部件ID的笔记信息 private Cursor getNoteWidgetInfo(Context context, int widgetId) { return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, PROJECTION, @@ -65,68 +72,94 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { 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) { - int bgId = ResourceParser.getDefaultBgId(context); - String snippet = ""; + int bgId = ResourceParser.getDefaultBgId(context); // 获取默认背景ID + 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()) { + // 检查是否有多个笔记使用相同的小部件ID 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); + snippet = c.getString(COLUMN_SNIPPET); // 获取笔记摘要 + bgId = c.getInt(COLUMN_BG_COLOR_ID); // 获取背景颜色ID + intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); // 添加笔记ID + intent.setAction(Intent.ACTION_VIEW); // 设置意图为查看笔记 } else { + // 如果没有找到笔记,显示默认提示信息 snippet = context.getResources().getString(R.string.widget_havenot_content); - intent.setAction(Intent.ACTION_INSERT_OR_EDIT); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // 设置意图为插入或编辑笔记 } if (c != null) { - c.close(); + 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); + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); // 设置背景图片 + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); // 添加背景ID到意图 /** * Generate the pending intent to start host for the widget */ PendingIntent pendingIntent = null; if (privacyMode) { + // 如果处于隐私模式,显示隐私提示信息 rv.setTextViewText(R.id.widget_text, context.getString(R.string.widget_under_visit_mode)); + // 创建PendingIntent,点击时启动NotesListActivity pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); } else { + // 显示笔记摘要 rv.setTextViewText(R.id.widget_text, snippet); + // 创建PendingIntent,点击时启动NoteEditActivity pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, PendingIntent.FLAG_UPDATE_CURRENT); } + // 设置点击事件 rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); + // 更新小部件 appWidgetManager.updateAppWidget(appWidgetIds[i], rv); } } } + // 获取背景资源ID + // 定义一个受保护的抽象方法,用于获取背景资源的ID + // 该方法接受一个整数类型的参数bgId,表示背景资源的标识符 + // 返回值是一个整数,表示对应的背景资源ID protected abstract int getBgResourceId(int bgId); + // 获取布局ID + // 定义一个受保护的抽象方法 getLayoutId + // 该方法返回一个整数,通常用于标识布局资源的ID protected abstract int getLayoutId(); + // 获取小部件类型 + // 定义一个受保护的抽象方法,用于获取小部件的类型 + // 该方法没有具体的实现,需要在子类中实现 + // 返回类型为int,表示小部件的类型 protected abstract int getWidgetType(); } diff --git a/src/src/net/micode/notes/widget/NoteWidgetProvider_2x.java b/src/src/net/micode/notes/widget/NoteWidgetProvider_2x.java index adcb2f7..845f28b 100644 --- a/src/src/net/micode/notes/widget/NoteWidgetProvider_2x.java +++ b/src/src/net/micode/notes/widget/NoteWidgetProvider_2x.java @@ -25,23 +25,31 @@ import net.micode.notes.tool.ResourceParser; public class NoteWidgetProvider_2x extends NoteWidgetProvider { + // 重写onUpdate方法,当Widget更新时调用 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的update方法,执行通用的更新逻辑 super.update(context, appWidgetManager, appWidgetIds); } + // 重写getLayoutId方法,返回当前Widget的布局资源ID @Override protected int getLayoutId() { + // 返回2x大小的Widget布局资源ID return R.layout.widget_2x; } + // 重写getBgResourceId方法,根据背景ID返回对应的背景资源ID @Override protected int getBgResourceId(int bgId) { + // 使用ResourceParser获取2x大小Widget的背景资源ID return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); } + // 重写getWidgetType方法,返回当前Widget的类型 @Override protected int getWidgetType() { + // 返回2x大小的Widget类型 return Notes.TYPE_WIDGET_2X; } } diff --git a/src/src/net/micode/notes/widget/NoteWidgetProvider_4x.java b/src/src/net/micode/notes/widget/NoteWidgetProvider_4x.java index c12a02e..5fe1172 100644 --- a/src/src/net/micode/notes/widget/NoteWidgetProvider_4x.java +++ b/src/src/net/micode/notes/widget/NoteWidgetProvider_4x.java @@ -25,22 +25,30 @@ import net.micode.notes.tool.ResourceParser; public class NoteWidgetProvider_4x extends NoteWidgetProvider { + // 重写onUpdate方法,当Widget更新时调用 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的update方法,执行通用的更新逻辑 super.update(context, appWidgetManager, appWidgetIds); } + // 获取当前Widget的布局资源ID protected int getLayoutId() { + // 返回4x大小的Widget布局资源ID return R.layout.widget_4x; } + // 重写getBgResourceId方法,根据背景ID获取对应的背景资源ID @Override protected int getBgResourceId(int bgId) { + // 使用ResourceParser获取4x大小Widget的背景资源ID return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); } + // 重写getWidgetType方法,获取当前Widget的类型 @Override protected int getWidgetType() { + // 返回4x大小的Widget类型 return Notes.TYPE_WIDGET_4X; } }