代码精读

qhh
SHarkii 2 months ago
parent d5f94f60ac
commit 21069feb8c

@ -32,101 +32,164 @@ 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, // 笔记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;
// 投影列的索引常量,提高代码可读性
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";
/**
*
* Widget ID
* @param context
* @param appWidgetIds ID
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
ContentValues values = new ContentValues();
// 将Widget ID设置为无效值
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])});
}
}
/**
* Widget ID
* @param context
* @param widgetId ID
* @return Cursornull
*/
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
// 查询条件Widget ID匹配且不是垃圾桶中的笔记
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_FOLER)},
null);
}
/**
* false
* @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 true
*/
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {
boolean privacyMode) {
for (int i = 0; i < appWidgetIds.length; i++) {
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
int bgId = ResourceParser.getDefaultBgId(context);
String snippet = "";
int bgId = ResourceParser.getDefaultBgId(context); // 默认背景ID
String snippet = ""; // 笔记摘要
// 构建点击小部件时启动的Intent
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());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 确保Activity在顶部
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); // 传递Widget ID
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); // 传递Widget类型
// 查询与Widget关联的笔记信息
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
if (c != null && c.moveToFirst()) {
// 检查是否有多个笔记关联到同一个Widget异常情况
if (c.getCount() > 1) {
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
c.close();
return;
}
// 提取笔记摘要和背景ID
snippet = c.getString(COLUMN_SNIPPET);
bgId = c.getInt(COLUMN_BG_COLOR_ID);
// 设置Intent动作为查看已有笔记
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动作为插入新笔记
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
}
if (c != null) {
c.close();
c.close(); // 关闭Cursor释放资源
}
// 创建RemoteViews对象用于更新Widget界面
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
// 设置Widget背景图片
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
// 向Intent中添加背景ID参数
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,
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);
}
// 设置Widget点击事件
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
// 更新Widget界面
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
}
/**
* ID
* @param bgId ID
* @return ID
*/
protected abstract int getBgResourceId(int bgId);
/**
* WidgetID
* @return WidgetID
*/
protected abstract int getLayoutId();
/**
* Widget
* @return Widget
*/
protected abstract int getWidgetType();
}
Loading…
Cancel
Save