|
|
/*
|
|
|
* 注意:此代码段的版权归 MiCode 开源社区所有(www.micode.net)
|
|
|
* 本代码遵循 Apache 2.0 许可证,您可以在 http://www.apache.org/licenses/LICENSE-2.0 查看许可证内容。
|
|
|
*/
|
|
|
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 {
|
|
|
|
|
|
// 定义查询数据库时需要的列名数组
|
|
|
public static final String[] PROJECTION = new String[]{
|
|
|
NoteColumns.ID, // 笔记ID
|
|
|
NoteColumns.BG_COLOR_ID, // 背景颜色ID
|
|
|
NoteColumns.SNIPPET // 笔记摘要或片段
|
|
|
};
|
|
|
|
|
|
// 定义对应于上述PROJECTION数组中各元素的索引常量
|
|
|
public static final int COLUMN_ID = 0; // 笔记ID的索引
|
|
|
public static final int COLUMN_BG_COLOR_ID = 1; // 背景颜色ID的索引
|
|
|
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); // 设置为无效的小部件ID
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
context.getContentResolver().update(
|
|
|
Notes.CONTENT_NOTE_URI, // 更新的URI
|
|
|
values, // 新值
|
|
|
NoteColumns.WIDGET_ID + "=?", // WHERE子句
|
|
|
new String[]{String.valueOf(appWidgetIds[i])} // WHERE参数
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据给定的小部件ID查询对应的笔记信息。
|
|
|
*
|
|
|
* @param context 上下文环境
|
|
|
* @param widgetId 小部件ID
|
|
|
* @return 返回包含笔记摘要、背景ID等信息的Cursor对象
|
|
|
*/
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
return context.getContentResolver().query(
|
|
|
Notes.CONTENT_NOTE_URI, // 查询的URI
|
|
|
PROJECTION, // 查询的列
|
|
|
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", // 查询条件
|
|
|
new String[]{String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER)}, // 查询条件参数
|
|
|
null // 排序依据
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新小部件显示内容的方法,可以根据是否隐私模式来调整显示内容。
|
|
|
*
|
|
|
* @param context 应用上下文
|
|
|
* @param appWidgetManager AppWidget管理器实例
|
|
|
* @param appWidgetIds 需要更新的小部件ID数组
|
|
|
*/
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 私有方法,根据是否隐私模式更新小部件显示内容。
|
|
|
*
|
|
|
* @param context 应用上下文
|
|
|
* @param appWidgetManager AppWidget管理器实例
|
|
|
* @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) { // 检查小部件ID是否有效
|
|
|
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]);
|
|
|
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); // 获取背景颜色ID
|
|
|
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); // 添加笔记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(); // 关闭游标
|
|
|
|
|
|
// 构建RemoteViews对象,设置其属性并为点击事件创建PendingIntent
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
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);
|
|
|
|
|
|
// 更新小部件UI
|
|
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 抽象方法,由子类实现,返回对应背景资源的ID。
|
|
|
*
|
|
|
* @param bgId 背景ID
|
|
|
* @return 返回对应的资源ID
|
|
|
*/
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
|
/**
|
|
|
* 抽象方法,由子类实现,返回小部件布局的资源ID。
|
|
|
*
|
|
|
* @return 返回布局的资源ID
|
|
|
*/
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
/**
|
|
|
* 抽象方法,由子类实现,返回小部件的类型。
|
|
|
*
|
|
|
* @return 返回小部件的类型
|
|
|
*/
|
|
|
protected abstract int getWidgetType();
|
|
|
} |