From d962d22ec2d59cabeb1e34097ceb03e713b836b2 Mon Sep 17 00:00:00 2001 From: zhj <1076863210@qq.com> Date: Mon, 4 Mar 2024 19:08:13 +0800 Subject: [PATCH] =?UTF-8?q?widget=20=E4=BE=BF=E7=AD=BE=E5=B0=8F=E9=83=A8?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notes/widget/NoteWidgetProvider.java | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/源程序代码-小米便签-Notes-master (1)/源程序代码-小米便签-Notes-master/Notes-master/src/net/micode/notes/widget/NoteWidgetProvider.java b/源程序代码-小米便签-Notes-master (1)/源程序代码-小米便签-Notes-master/Notes-master/src/net/micode/notes/widget/NoteWidgetProvider.java index ec6f819..0efe60c 100644 --- a/源程序代码-小米便签-Notes-master (1)/源程序代码-小米便签-Notes-master/Notes-master/src/net/micode/notes/widget/NoteWidgetProvider.java +++ b/源程序代码-小米便签-Notes-master (1)/源程序代码-小米便签-Notes-master/Notes-master/src/net/micode/notes/widget/NoteWidgetProvider.java @@ -15,6 +15,7 @@ */ package net.micode.notes.widget; + import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; @@ -32,31 +33,39 @@ import net.micode.notes.tool.ResourceParser; 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 + // 查询便签小部件信息所需的投影 + 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; + 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"; @Override public void onDeleted(Context context, int[] appWidgetIds) { + // 当小部件被删除时,将小部件 ID 更新为无效的 ID 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])}); + new String[] { String.valueOf(appWidgetIds[i]) }); } } + /** + * 获取便签小部件信息的游标 + */ private Cursor getNoteWidgetInfo(Context context, int widgetId) { return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, PROJECTION, @@ -69,6 +78,9 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { update(context, appWidgetManager, appWidgetIds, false); } + /** + * 更新小部件的方法 + */ private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, boolean privacyMode) { for (int i = 0; i < appWidgetIds.length; i++) { @@ -83,7 +95,7 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { 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]); + Log.e(TAG, "相同小部件 ID 的多条信息:" + appWidgetIds[i]); c.close(); return; } @@ -103,9 +115,8 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); - /** - * Generate the pending intent to start host for the widget - */ + + // 生成启动活动的待定意图 PendingIntent pendingIntent = null; if (privacyMode) { rv.setTextViewText(R.id.widget_text, @@ -124,9 +135,18 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { } } + /** + * 根据 bgId 获取背景资源 ID + */ protected abstract int getBgResourceId(int bgId); + /** + * 获取小部件的布局 ID + */ protected abstract int getLayoutId(); + /** + * 获取小部件类型 + */ protected abstract int getWidgetType(); -} +} \ No newline at end of file