|
|
|
|
@ -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(); // 获取小部件类型标识
|
|
|
|
|
}
|