diff --git a/src/net/micode/notes/widget/NoteWidgetProvider.java b/src/net/micode/notes/widget/NoteWidgetProvider.java new file mode 100644 index 0000000..2f4045b --- /dev/null +++ b/src/net/micode/notes/widget/NoteWidgetProvider.java @@ -0,0 +1,131 @@ +/* + * 该抽象类作为便签小部件的提供者,处理小部件的创建、更新和删除逻辑, + * 并负责将数据库中的便签数据绑定到小部件视图上。 + */ + + 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; + + public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 数据库查询投影,指定需要获取的列:便签ID、背景颜色ID和内容片段 + 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; + + private static final String TAG = "NoteWidgetProvider"; + + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + // 当小部件被删除时,将关联便签的WIDGET_ID设为无效值,解除绑定 + ContentValues values = new ContentValues(); + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + for (int appWidgetId : appWidgetIds) { + context.getContentResolver().update( + Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", + new String[]{String.valueOf(appWidgetId)} + ); + } + } + + // 查询指定widgetId关联的便签信息(排除废纸篓中的便签) + 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)}, + null + ); + } + + // 更新小部件视图的公共方法 + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + update(context, appWidgetManager, appWidgetIds, false); + } + + // 核心更新逻辑:遍历所有小部件ID,更新每个小部件的内容和样式 + private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, + boolean privacyMode) { + for (int appWidgetId : appWidgetIds) { + if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) continue; + + int bgId = ResourceParser.getDefaultBgId(context); // 默认背景资源 + String snippet = ""; + Intent intent = new Intent(context, NoteEditActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 单例模式启动Activity + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetId); + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); + + // 从数据库获取关联的便签数据 + try (Cursor cursor = getNoteWidgetInfo(context, appWidgetId)) { + if (cursor != null && cursor.moveToFirst()) { + if (cursor.getCount() > 1) { + Log.e(TAG, "Multiple notes with same widget id: " + appWidgetId); + continue; // 异常情况:多个便签关联同一小部件 + } + snippet = cursor.getString(COLUMN_SNIPPET); + bgId = cursor.getInt(COLUMN_BG_COLOR_ID); + intent.putExtra(Intent.EXTRA_UID, cursor.getLong(COLUMN_ID)); + intent.setAction(Intent.ACTION_VIEW); // 查看现有便签 + } else { + snippet = context.getString(R.string.widget_havenot_content); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // 创建新便签 + } + } + + // 构建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 pendingIntent; + if (privacyMode) { + rv.setTextViewText(R.id.widget_text, context.getString(R.string.widget_under_visit_mode)); + pendingIntent = PendingIntent.getActivity( + context, appWidgetId, + new Intent(context, NotesListActivity.class), + PendingIntent.FLAG_UPDATE_CURRENT + ); + } else { + rv.setTextViewText(R.id.widget_text, snippet); + pendingIntent = PendingIntent.getActivity( + context, appWidgetId, intent, + PendingIntent.FLAG_UPDATE_CURRENT + ); + } + + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); + appWidgetManager.updateAppWidget(appWidgetId, rv); // 更新小部件视图 + } + } + + /* 抽象方法:子类需实现具体逻辑 */ + protected abstract int getBgResourceId(int bgId); // 根据颜色ID获取实际资源ID + protected abstract int getLayoutId(); // 获取小部件布局文件ID + protected abstract int getWidgetType(); // 获取小部件类型标识 + } \ No newline at end of file diff --git a/src/net/micode/notes/widget/NoteWidgetProvider_2x.java b/src/net/micode/notes/widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..3bc061a --- /dev/null +++ b/src/net/micode/notes/widget/NoteWidgetProvider_2x.java @@ -0,0 +1,57 @@ +/* + * 该类实现2x尺寸的便签小部件具体逻辑,继承自通用便签小部件提供者 + * 主要负责定义2x尺寸小部件的布局、背景资源和类型标识 + */ + + 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; + + public class NoteWidgetProvider_2x extends NoteWidgetProvider { + /** + * 小部件更新入口方法 + * @param context 上下文环境 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 需要更新小部件的ID数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类通用更新逻辑,保持功能一致性 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取2x小部件布局资源ID + * @return 返回R.layout.widget_2x布局资源标识 + */ + @Override + protected int getLayoutId() { + // 指定2x尺寸小部件的专属布局文件 + return R.layout.widget_2x; + } + + /** + * 获取2x小部件背景资源映射 + * @param bgId 背景颜色标识(来自数据库) + * @return 对应的实际背景图片资源ID + */ + @Override + protected int getBgResourceId(int bgId) { + // 通过资源解析工具获取2x专属背景资源 + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + /** + * 获取小部件类型标识 + * @return 返回2x小部件类型常量TYPE_WIDGET_2X + */ + @Override + protected int getWidgetType() { + // 在数据库和业务逻辑中用于标识2x小部件类型 + return Notes.TYPE_WIDGET_2X; + } + } \ No newline at end of file diff --git a/src/net/micode/notes/widget/NoteWidgetProvider_4x.java b/src/net/micode/notes/widget/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..6b9c541 --- /dev/null +++ b/src/net/micode/notes/widget/NoteWidgetProvider_4x.java @@ -0,0 +1,57 @@ +/* + * 4x尺寸便签小部件实现类,继承基础小部件提供者,定义4x规格专属属性 + * 主要职责包括:指定4x布局、匹配4x背景资源、标识4x部件类型 + */ + + 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; + + public class NoteWidgetProvider_4x extends NoteWidgetProvider { + /** + * 小部件更新生命周期回调 + * @param context 上下文对象,用于访问系统服务 + * @param appWidgetManager 小部件管理器实例 + * @param appWidgetIds 需要更新小部件的ID集合 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 复用父类通用更新逻辑,保持各尺寸小部件行为一致性 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取4x专属布局资源 + * @return 返回4x规格布局文件widget_4x.xml的编译后ID + */ + @Override + protected int getLayoutId() { + // 定义4x尺寸特有的UI布局结构 + return R.layout.widget_4x; + } + + /** + * 转换背景色标识为具体资源 + * @param bgId 颜色配置编号(来自数据库存储值) + * @return 对应4x尺寸的背景图片资源ID + */ + @Override + protected int getBgResourceId(int bgId) { + // 通过资源解析工具获取4x专用背景图 + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + /** + * 声明小部件类型标识 + * @return 返回系统常量TYPE_WIDGET_4X,用于数据库记录和类型判断 + */ + @Override + protected int getWidgetType() { + // 标识当前为4x规格小部件,区别于2x/其他尺寸 + return Notes.TYPE_WIDGET_4X; + } + } \ No newline at end of file