diff --git a/widget/NoteWidgetProvider.java b/widget/NoteWidgetProvider.java new file mode 100644 index 0000000..d2dd78f --- /dev/null +++ b/widget/NoteWidgetProvider.java @@ -0,0 +1,132 @@ +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,用于管理笔记小部件的更新和删除 +public abstract class NoteWidgetProvider extends AppWidgetProvider { + + // 查询笔记小部件信息的投影,包括ID、背景颜色ID和片段 + public static final String[] PROJECTION = new String[]{ + NoteColumns.ID, + NoteColumns.BG_COLOR_ID, + NoteColumns.SNIPPET + }; + // 投影中ID列的索引 + public static final int COLUMN_ID = 0; + // 投影中背景颜色ID列的索引 + 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) { + 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])}); + } + } + + // 获取指定小部件ID的笔记小部件信息的方法 + 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); + } + + // 更新小部件的方法,可被子类调用,默认调用带隐私模式参数的更新方法,传入false + 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 i = 0; i < appWidgetIds.length; i++) { + 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); + 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()) { + 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); + } else { + snippet = context.getResources().getString(R.string.widget_havenot_content); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); + } + if (c!= null) { + c.close(); + } + + // 创建远程视图,用于更新小部件的UI + 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 = null; + if (privacyMode) { + 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 { + rv.setTextViewText(R.id.widget_text, snippet); + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + 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/widget/NoteWidgetProvider_2x.java b/widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..f5ebce1 --- /dev/null +++ b/widget/NoteWidgetProvider_2x.java @@ -0,0 +1,36 @@ +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; + +// 2x尺寸笔记小部件提供者类,继承自NoteWidgetProvider,实现特定于2x尺寸小部件的功能 +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + + // 当小部件更新时调用,调用父类的更新方法来更新小部件 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.update(context, appWidgetManager, appWidgetIds); + } + + // 获取2x尺寸小部件的布局ID + @Override + protected int getLayoutId() { + return R.layout.widget_2x; + } + + // 根据背景颜色ID获取2x尺寸小部件的背景资源ID + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + // 获取2x尺寸小部件的类型 + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; + } +} \ No newline at end of file diff --git a/widget/NoteWidgetProvider_4x.java b/widget/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..2a9758d --- /dev/null +++ b/widget/NoteWidgetProvider_4x.java @@ -0,0 +1,35 @@ +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; + +// 4x尺寸笔记小部件提供者类,继承自NoteWidgetProvider,用于创建和管理4x尺寸的笔记小部件 +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + + // 当小部件更新时被调用,调用父类的更新方法来执行更新操作 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.update(context, appWidgetManager, appWidgetIds); + } + + // 获取4x尺寸小部件的布局资源ID + protected int getLayoutId() { + return R.layout.widget_4x; + } + + // 根据背景颜色ID获取4x尺寸小部件的背景资源ID + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + // 获取4x尺寸小部件的类型 + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; + } +} \ No newline at end of file