|
|
|
|
@ -15,7 +15,6 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.widget;
|
|
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.appwidget.AppWidgetProvider;
|
|
|
|
|
@ -33,38 +32,23 @@ 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, // 便签ID
|
|
|
|
|
NoteColumns.BG_COLOR_ID, // 便签背景颜色ID
|
|
|
|
|
NoteColumns.SNIPPET // 便签摘要内容
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当一个或多个小部件被删除时调用
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param appWidgetIds 被删除的小部件ID数组
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
|
|
|
|
// 创建更新值,将对应便签的widget_id字段设为无效值
|
|
|
|
|
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,
|
|
|
|
|
values,
|
|
|
|
|
@ -73,14 +57,7 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据小部件ID获取对应的便签信息
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param widgetId 小部件ID
|
|
|
|
|
* @return 包含便签信息的Cursor对象
|
|
|
|
|
*/
|
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
|
|
// 查询条件:widget_id匹配且便签不在回收站
|
|
|
|
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
PROJECTION,
|
|
|
|
|
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
|
|
|
|
|
@ -88,107 +65,68 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新指定ID的小部件(非隐私模式)
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param appWidgetManager 小部件管理器
|
|
|
|
|
* @param appWidgetIds 需要更新的小部件ID数组
|
|
|
|
|
*/
|
|
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新指定ID的小部件,支持隐私模式
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param appWidgetManager 小部件管理器
|
|
|
|
|
* @param appWidgetIds 需要更新的小部件ID数组
|
|
|
|
|
* @param privacyMode 是否启用隐私模式
|
|
|
|
|
*/
|
|
|
|
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
|
|
|
|
boolean privacyMode) {
|
|
|
|
|
// 遍历所有需要更新的小部件ID
|
|
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
|
|
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
|
|
|
|
|
// 设置默认背景和内容
|
|
|
|
|
int bgId = ResourceParser.getDefaultBgId(context);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
// 查询小部件对应的便签信息
|
|
|
|
|
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
|
|
|
|
|
if (c != null && c.moveToFirst()) {
|
|
|
|
|
// 检查是否存在多个便签关联到同一个小部件ID(异常情况)
|
|
|
|
|
if (c.getCount() > 1) {
|
|
|
|
|
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
|
|
|
|
|
c.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从Cursor中获取便签内容和背景信息
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭Cursor释放资源
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建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
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置点击事件并更新小部件
|
|
|
|
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定背景ID对应的背景资源ID(由子类实现)
|
|
|
|
|
* @param bgId 背景ID
|
|
|
|
|
* @return 背景资源ID
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取小部件使用的布局资源ID(由子类实现)
|
|
|
|
|
* @return 布局资源ID
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取小部件类型(由子类实现)
|
|
|
|
|
* @return 小部件类型常量
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getWidgetType();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|