|
|
|
@ -15,6 +15,7 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.widget;
|
|
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.appwidget.AppWidgetProvider;
|
|
|
|
@ -32,7 +33,11 @@ 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,
|
|
|
|
@ -47,6 +52,7 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
|
|
|
|
// 当小部件被删除时,将小部件 ID 更新为无效的 ID
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
|
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
|
@ -57,6 +63,9 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取便签小部件信息的游标
|
|
|
|
|
*/
|
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
|
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
PROJECTION,
|
|
|
|
@ -69,6 +78,9 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新小部件的方法
|
|
|
|
|
*/
|
|
|
|
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
|
|
|
|
boolean privacyMode) {
|
|
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
|
@ -83,7 +95,7 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
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]);
|
|
|
|
|
Log.e(TAG, "相同小部件 ID 的多条信息:" + appWidgetIds[i]);
|
|
|
|
|
c.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -103,9 +115,8 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
|
|
|
|
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,
|
|
|
|
@ -124,9 +135,18 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据 bgId 获取背景资源 ID
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取小部件的布局 ID
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取小部件类型
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getWidgetType();
|
|
|
|
|
}
|