|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
* You may obtain a copy of the License at
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
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) {
|
|
|
// 创建 ContentValues 对象,用于存储更新数据库的数值
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
|
// 将 NoteColumns.WIDGET_ID 的值设置为无效的小部件 ID(AppWidgetManager.INVALID_APPWIDGET_ID)
|
|
|
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
|
|
|
|
|
// 遍历传入的小部件 ID 数组
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
// 使用 ContentResolver 更新 Notes 数据库中的相应小部件 ID 的记录
|
|
|
context.getContentResolver().update(
|
|
|
Notes.CONTENT_NOTE_URI, // 更新的数据表的 URI
|
|
|
values, // 更新的数值
|
|
|
NoteColumns.WIDGET_ID + "=?", // 更新的条件,匹配小部件 ID
|
|
|
new String[] { String.valueOf(appWidgetIds[i]) } // 匹配条件的参数,传入当前小部件 ID
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 获取与小部件相关的笔记信息的方法
|
|
|
// 参数 context:上下文对象,用于访问内容提供程序
|
|
|
// 参数 widgetId:要获取信息的小部件的唯一标识符
|
|
|
// 返回值:包含笔记信息的游标对象
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
// 使用内容解析器查询 Notes 数据库中的记录
|
|
|
// 查询的条件是:NoteColumns.WIDGET_ID 等于给定 widgetId,同时 NoteColumns.PARENT_ID 不等于垃圾箱的 ID(Notes.ID_TRASH_FOLER)
|
|
|
// 查询的列是由 PROJECTION 常量指定的列数组
|
|
|
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 // 不排序
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// update 方法的重载,用于执行小部件更新操作
|
|
|
// 参数 context:上下文对象,用于访问资源和小部件管理器
|
|
|
// 参数 appWidgetManager:小部件管理器,用于更新小部件
|
|
|
// 参数 appWidgetIds:包含要更新的小部件 ID 的数组
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
// 调用 update 方法的重载版本,传入默认参数 false
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
}
|
|
|
|
|
|
|
|
|
// update 方法,用于更新小部件
|
|
|
// 参数 context:上下文对象,用于访问资源和小部件管理器
|
|
|
// 参数 appWidgetManager:小部件管理器,用于更新小部件
|
|
|
// 参数 appWidgetIds:包含要更新的小部件 ID 的数组
|
|
|
// 参数 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);
|
|
|
// 初始化一个空字符串用于存储文本片段
|
|
|
String snippet = "";
|
|
|
// 创建一个意图,用于打开 NoteEditActivity
|
|
|
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 the same widget id:" + appWidgetIds[i]);
|
|
|
c.close();
|
|
|
return;
|
|
|
}
|
|
|
// 从查询结果中获取文本片段和背景颜色 ID
|
|
|
snippet = c.getString(COLUMN_SNIPPET);
|
|
|
bgId = c.getInt(COLUMN_BG_COLOR_ID);
|
|
|
// 设置 intent 中的额外数据,包括笔记的唯一标识符和操作类型
|
|
|
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 rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
|
|
// 设置 intent 中的额外数据,包括背景 ID
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
|
|
|
|
|
|
// 生成用于启动小部件的挂起意图
|
|
|
PendingIntent pendingIntent = null;
|
|
|
if (privacyMode) {
|
|
|
// 如果小部件处于隐私模式,设置文本为特定文本并创建 NotesListActivity 的意图
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
protected abstract int getWidgetType();
|
|
|
}
|