From 58f135178787af07f517c4605f6cf5a8b2d17754 Mon Sep 17 00:00:00 2001 From: chenjianan <931139743@qq.com> Date: Wed, 8 Jan 2025 22:39:10 +0800 Subject: [PATCH] chenjianan --- .../notes/widget/NoteWidgetProvider.java | 81 ++++++++++++------- .../notes/widget/NoteWidgetProvider_2x.java | 35 ++++++-- .../notes/widget/NoteWidgetProvider_4x.java | 35 ++++++-- 3 files changed, 110 insertions(+), 41 deletions(-) diff --git a/src/java/net/micode/notes/widget/NoteWidgetProvider.java b/src/java/net/micode/notes/widget/NoteWidgetProvider.java index e5659bc..d9d23f7 100644 --- a/src/java/net/micode/notes/widget/NoteWidgetProvider.java +++ b/src/java/net/micode/notes/widget/NoteWidgetProvider.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,6 +15,7 @@ */ package net.micode.notes.widget; + import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; @@ -33,32 +34,28 @@ import net.micode.notes.ui.NoteEditActivity; import net.micode.notes.ui.NotesListActivity; /** - * - * @ProjectName: minode - * @Package: net.micode.notes.widget - * @ClassName: NoteWidgetProvider - * @Description: 该类继承了AppWidgetProvider,实现挂件添加、删除、更新、启用等功能 - - * @Date: 2024-12-18 21:13 + * NoteWidgetProvider 类继承自 AppWidgetProvider,用于实现便签小部件的添加、删除、更新和启用等功能。 */ 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; - private static final String TAG = "NoteWidgetProvider"; + private static final String TAG = "NoteWidgetProvider"; // 日志标签 /** - * @method onDeleted - * @description 该方法用于删除Widget - * @date: 2024-12-18 21:18 - * @return void + * 当小部件被删除时调用此方法。 + * 更新数据库中与小部件ID相关的便签信息,将小部件ID设置为无效。 + * @param context 上下文 + * @param appWidgetIds 小部件ID数组 */ @Override public void onDeleted(Context context, int[] appWidgetIds) { @@ -72,44 +69,57 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { } } + /** + * 获取与小部件ID相关的便签信息。 + * @param context 上下文 + * @param widgetId 小部件ID + * @return 查询结果的游标 + */ 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) }, + new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLDER) }, null); } /** - * @method update - * @description 该方法用于添加或更新Widget - * @date: 2024-12-18 21:11 - * @return void + * 更新小部件。 + * @param context 上下文 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 小部件ID数组 */ protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { update(context, appWidgetManager, appWidgetIds, false); } + /** + * 更新小部件。 + * @param context 上下文 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 小部件ID数组 + * @param privacyMode 是否为隐私模式 + */ 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); + 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]); + 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); + snippet = c.getString(COLUMN_SNIPPET); // 获取便签摘要 + bgId = c.getInt(COLUMN_BG_COLOR_ID); // 获取背景ID intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); intent.setAction(Intent.ACTION_VIEW); } else { @@ -125,7 +135,7 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { 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 pendingIntent = null; if (privacyMode) { @@ -145,9 +155,22 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider { } } + /** + * 获取小部件的背景资源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 diff --git a/src/java/net/micode/notes/widget/NoteWidgetProvider_2x.java b/src/java/net/micode/notes/widget/NoteWidgetProvider_2x.java index adcb2f7..3b22139 100644 --- a/src/java/net/micode/notes/widget/NoteWidgetProvider_2x.java +++ b/src/java/net/micode/notes/widget/NoteWidgetProvider_2x.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -23,25 +23,48 @@ import net.micode.notes.R; import net.micode.notes.data.Notes; import net.micode.notes.tool.ResourceParser; - +/** + * NoteWidgetProvider_2x 类继承自 NoteWidgetProvider,用于创建和管理 2x 大小的便签小部件。 + * 它提供了更新小部件布局和背景资源的方法。 + */ public class NoteWidgetProvider_2x extends NoteWidgetProvider { + /** + * 当小部件需要更新时调用此方法。 + * 调用父类的 update 方法来更新小部件。 + * @param context 上下文 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 小部件ID数组 + */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.update(context, appWidgetManager, appWidgetIds); } + /** + * 获取小部件的布局资源ID。 + * @return 布局资源ID + */ @Override protected int getLayoutId() { - return R.layout.widget_2x; + return R.layout.widget_2x; // 返回2x小部件的布局资源ID } + /** + * 根据背景ID获取小部件的背景资源ID。 + * @param bgId 背景ID + * @return 背景资源ID + */ @Override protected int getBgResourceId(int bgId) { - return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); // 获取2x小部件的背景资源ID } + /** + * 获取小部件的类型。 + * @return 小部件类型 + */ @Override protected int getWidgetType() { - return Notes.TYPE_WIDGET_2X; + return Notes.TYPE_WIDGET_2X; // 返回2x小部件的类型 } -} +} \ No newline at end of file diff --git a/src/java/net/micode/notes/widget/NoteWidgetProvider_4x.java b/src/java/net/micode/notes/widget/NoteWidgetProvider_4x.java index c12a02e..ffc1aa2 100644 --- a/src/java/net/micode/notes/widget/NoteWidgetProvider_4x.java +++ b/src/java/net/micode/notes/widget/NoteWidgetProvider_4x.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -23,24 +23,47 @@ import net.micode.notes.R; import net.micode.notes.data.Notes; import net.micode.notes.tool.ResourceParser; - +/** + * NoteWidgetProvider_4x 类继承自 NoteWidgetProvider,用于创建和管理 4x 大小的便签小部件。 + * 它提供了更新小部件布局和背景资源的方法。 + */ public class NoteWidgetProvider_4x extends NoteWidgetProvider { + /** + * 当小部件需要更新时调用此方法。 + * 调用父类的 update 方法来更新小部件。 + * @param context 上下文 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 小部件ID数组 + */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.update(context, appWidgetManager, appWidgetIds); } + /** + * 获取小部件的布局资源ID。 + * @return 布局资源ID + */ protected int getLayoutId() { - return R.layout.widget_4x; + return R.layout.widget_4x; // 返回4x小部件的布局资源ID } + /** + * 根据背景ID获取小部件的背景资源ID。 + * @param bgId 背景ID + * @return 背景资源ID + */ @Override protected int getBgResourceId(int bgId) { - return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); // 获取4x小部件的背景资源ID } + /** + * 获取小部件的类型。 + * @return 小部件类型 + */ @Override protected int getWidgetType() { - return Notes.TYPE_WIDGET_4X; + return Notes.TYPE_WIDGET_4X; // 返回4x小部件的类型 } -} +} \ No newline at end of file