|
|
|
@ -14,7 +14,10 @@
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 包声明,定义了这个类属于哪个包
|
|
|
|
|
package net.micode.notes.widget;
|
|
|
|
|
|
|
|
|
|
// 导入所需的类和接口
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.appwidget.AppWidgetProvider;
|
|
|
|
@ -32,21 +35,27 @@ import net.micode.notes.tool.ResourceParser;
|
|
|
|
|
import net.micode.notes.ui.NoteEditActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
|
|
|
|
|
// 定义一个抽象类NoteWidgetProvider,继承自AppWidgetProvider
|
|
|
|
|
public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
// 定义查询数据库时需要的列
|
|
|
|
|
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 i = 0; i < appWidgetIds.length; i++) {
|
|
|
|
@ -57,6 +66,7 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取小部件的笔记信息
|
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
|
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
PROJECTION,
|
|
|
|
@ -65,68 +75,88 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新小部件的方法
|
|
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 私有方法,用于更新小部件,包含隐私模式的逻辑
|
|
|
|
|
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);
|
|
|
|
|
// 默认摘要文本
|
|
|
|
|
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) {
|
|
|
|
|
// 如果有多个相同的widget id,记录错误并返回
|
|
|
|
|
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.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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭游标
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建RemoteViews对象,用于更新小部件视图
|
|
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
|
|
|
// 设置背景资源ID
|
|
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
|
|
|
|
// 将背景ID传递给意图
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
|
|
|
|
|
/**
|
|
|
|
|
* Generate the pending intent to start host for the widget
|
|
|
|
|
* 生成用于启动宿主的小部件的PendingIntent
|
|
|
|
|
*/
|
|
|
|
|
PendingIntent pendingIntent = null;
|
|
|
|
|
if (privacyMode) {
|
|
|
|
|
// 如果是隐私模式,显示隐私模式文本,并设置PendingIntent为启动笔记列表活动
|
|
|
|
|
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 {
|
|
|
|
|
// 否则,显示摘要文本,并设置PendingIntent为启动编辑活动
|
|
|
|
|
rv.setTextViewText(R.id.widget_text, snippet);
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
|
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置点击事件的PendingIntent
|
|
|
|
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
|
|
|
|
// 更新小部件
|
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 抽象方法,用于获取背景资源ID
|
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
|
|
|
|
|
// 抽象方法,用于获取布局ID
|
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
|
|
|
|
// 抽象方法,用于获取小部件类型
|
|
|
|
|
protected abstract int getWidgetType();
|
|
|
|
|
}
|
|
|
|
|
}
|