|
|
@ -15,6 +15,7 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.widget;
|
|
|
|
package net.micode.notes.widget;
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
import android.appwidget.AppWidgetProvider;
|
|
|
|
import android.appwidget.AppWidgetProvider;
|
|
|
@ -32,19 +33,31 @@ import net.micode.notes.tool.ResourceParser;
|
|
|
|
import net.micode.notes.ui.NoteEditActivity;
|
|
|
|
import net.micode.notes.ui.NoteEditActivity;
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 便签小部件的抽象基类,提供了小部件的基本功能实现
|
|
|
|
|
|
|
|
* 具体的小部件类型需要继承此类并实现抽象方法
|
|
|
|
|
|
|
|
*/
|
|
|
|
public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
|
|
|
// 查询便签信息时使用的投影,指定要获取的数据库列
|
|
|
|
public static final String [] PROJECTION = new String [] {
|
|
|
|
public static final String [] PROJECTION = new String [] {
|
|
|
|
NoteColumns.ID,
|
|
|
|
NoteColumns.ID, // 便签ID
|
|
|
|
NoteColumns.BG_COLOR_ID,
|
|
|
|
NoteColumns.BG_COLOR_ID, // 便签背景颜色ID
|
|
|
|
NoteColumns.SNIPPET
|
|
|
|
NoteColumns.SNIPPET // 便签摘要内容
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 投影结果集中各列的索引位置
|
|
|
|
public static final int COLUMN_ID = 0;
|
|
|
|
public static final int COLUMN_ID = 0;
|
|
|
|
public static final int COLUMN_BG_COLOR_ID = 1;
|
|
|
|
public static final int COLUMN_BG_COLOR_ID = 1;
|
|
|
|
public static final int COLUMN_SNIPPET = 2;
|
|
|
|
public static final int COLUMN_SNIPPET = 2;
|
|
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NoteWidgetProvider";
|
|
|
|
private static final String TAG = "NoteWidgetProvider";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 当一个或多个小部件被删除时调用
|
|
|
|
|
|
|
|
* 该方法会将被删除小部件关联的便签的widget_id字段置为INVALID_APPWIDGET_ID
|
|
|
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
|
|
|
* @param appWidgetIds 被删除的小部件ID数组
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
@ -57,6 +70,12 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 根据小部件ID查询关联的便签信息
|
|
|
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
|
|
|
* @param widgetId 小部件ID
|
|
|
|
|
|
|
|
* @return 返回包含便签信息的Cursor,若未找到则返回null
|
|
|
|
|
|
|
|
*/
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
PROJECTION,
|
|
|
|
PROJECTION,
|
|
|
@ -65,68 +84,113 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
null);
|
|
|
|
null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新小部件显示,默认非隐私模式
|
|
|
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
|
|
|
* @param appWidgetManager AppWidget管理器
|
|
|
|
|
|
|
|
* @param appWidgetIds 要更新的小部件ID数组
|
|
|
|
|
|
|
|
*/
|
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新小部件显示,可以指定是否为隐私模式
|
|
|
|
|
|
|
|
* 在隐私模式下,小部件会显示通用提示信息,而不是具体便签内容
|
|
|
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
|
|
|
* @param appWidgetManager AppWidget管理器
|
|
|
|
|
|
|
|
* @param appWidgetIds 要更新的小部件ID数组
|
|
|
|
|
|
|
|
* @param privacyMode 是否为隐私模式
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
|
|
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
|
|
|
boolean privacyMode) {
|
|
|
|
boolean privacyMode) {
|
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
|
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
|
|
|
|
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
|
|
|
|
|
|
|
|
// 设置默认值
|
|
|
|
int bgId = ResourceParser.getDefaultBgId(context);
|
|
|
|
int bgId = ResourceParser.getDefaultBgId(context);
|
|
|
|
String snippet = "";
|
|
|
|
String snippet = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建启动便签编辑界面的Intent
|
|
|
|
Intent intent = new Intent(context, NoteEditActivity.class);
|
|
|
|
Intent intent = new Intent(context, NoteEditActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查询与小部件关联的便签信息
|
|
|
|
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
|
|
|
|
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
|
|
|
|
if (c != null && c.moveToFirst()) {
|
|
|
|
if (c != null && c.moveToFirst()) {
|
|
|
|
|
|
|
|
// 检查是否存在多个便签关联到同一个小部件ID的异常情况
|
|
|
|
if (c.getCount() > 1) {
|
|
|
|
if (c.getCount() > 1) {
|
|
|
|
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
|
|
|
|
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
|
|
|
|
c.close();
|
|
|
|
c.close();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从查询结果中获取便签内容和背景信息
|
|
|
|
snippet = c.getString(COLUMN_SNIPPET);
|
|
|
|
snippet = c.getString(COLUMN_SNIPPET);
|
|
|
|
bgId = c.getInt(COLUMN_BG_COLOR_ID);
|
|
|
|
bgId = c.getInt(COLUMN_BG_COLOR_ID);
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 若未找到关联便签,设置默认提示信息
|
|
|
|
snippet = context.getResources().getString(R.string.widget_havenot_content);
|
|
|
|
snippet = context.getResources().getString(R.string.widget_havenot_content);
|
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭Cursor释放资源
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
c.close();
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建RemoteViews用于更新小部件UI
|
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Generate the pending intent to start host for the widget
|
|
|
|
* 生成用于启动小部件宿主的PendingIntent
|
|
|
|
|
|
|
|
* 根据是否为隐私模式设置不同的点击行为
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
PendingIntent pendingIntent = null;
|
|
|
|
PendingIntent pendingIntent = null;
|
|
|
|
if (privacyMode) {
|
|
|
|
if (privacyMode) {
|
|
|
|
|
|
|
|
// 隐私模式下显示通用提示,点击进入便签列表
|
|
|
|
rv.setTextViewText(R.id.widget_text,
|
|
|
|
rv.setTextViewText(R.id.widget_text,
|
|
|
|
context.getString(R.string.widget_under_visit_mode));
|
|
|
|
context.getString(R.string.widget_under_visit_mode));
|
|
|
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
|
|
|
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
|
|
|
|
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 非隐私模式下显示便签内容,点击进入便签编辑界面
|
|
|
|
rv.setTextViewText(R.id.widget_text, snippet);
|
|
|
|
rv.setTextViewText(R.id.widget_text, snippet);
|
|
|
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
|
|
|
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置点击事件并更新小部件
|
|
|
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
|
|
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取指定背景ID对应的资源ID
|
|
|
|
|
|
|
|
* 由具体子类实现
|
|
|
|
|
|
|
|
* @param bgId 背景ID
|
|
|
|
|
|
|
|
* @return 资源ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取小部件布局ID
|
|
|
|
|
|
|
|
* 由具体子类实现
|
|
|
|
|
|
|
|
* @return 布局ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取小部件类型
|
|
|
|
|
|
|
|
* 由具体子类实现
|
|
|
|
|
|
|
|
* @return 小部件类型
|
|
|
|
|
|
|
|
*/
|
|
|
|
protected abstract int getWidgetType();
|
|
|
|
protected abstract int getWidgetType();
|
|
|
|
}
|
|
|
|
}
|