You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
king/widget/NoteWidgetProvider.java

225 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.
*/
// 包声明,表明该类属于 net.micode.notes.widget 包
package net.micode.notes.widget;
// 导入用于创建待定意图的类,用于在未来某个时刻执行操作
import android.app.PendingIntent;
// 导入应用小部件管理器类,用于管理和更新应用小部件
import android.appwidget.AppWidgetManager;
// 导入应用小部件提供者基类,用于处理小部件的各种事件
import android.appwidget.AppWidgetProvider;
// 导入用于存储键值对,通常用于数据库操作中插入或更新数据
import android.content.ContentValues;
// 导入 Android 上下文类,用于获取应用程序的环境信息
import android.content.Context;
// 导入 Android 用于在不同组件间传递消息的类
import android.content.Intent;
// 导入 Android 数据库游标类,用于处理数据库查询结果
import android.database.Cursor;
// 导入 Android 日志工具类,用于在开发和调试过程中记录信息
import android.util.Log;
// 导入用于更新远程视图的类,可用于更新应用小部件的视图
import android.widget.RemoteViews;
// 导入应用资源类,用于引用应用内的资源
import net.micode.notes.R;
// 导入 Notes 类,用于引用笔记相关的常量和信息
import net.micode.notes.data.Notes;
// 导入 Notes 类中的 NoteColumns 接口,用于引用笔记列的常量
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;
/**
* 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";
/**
* 当小部件被删除时调用,将对应笔记的小部件 ID 置为无效
* @param context 应用程序上下文
* @param appWidgetIds 被删除的小部件 ID 数组
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// 用于存储要更新的数据
ContentValues values = new ContentValues();
// 将笔记的小部件 ID 置为无效
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// 遍历被删除的小部件 ID 数组
for (int i = 0; i < appWidgetIds.length; i++) {
// 更新笔记数据,将对应小部件 ID 的笔记的小部件 ID 置为无效
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])});
}
}
/**
* 获取指定小部件 ID 对应的笔记信息
* @param context 应用程序上下文
* @param widgetId 小部件 ID
* @return 包含笔记信息的游标
*/
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
// 查询笔记信息,排除回收站中的笔记
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) },
null);
}
/**
* 更新小部件,默认不开启隐私模式
* @param context 应用程序上下文
* @param appWidgetManager 应用小部件管理器
* @param appWidgetIds 要更新的小部件 ID 数组
*/
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false);
}
/**
* 更新小部件,可选择是否开启隐私模式
* @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++) {
// 检查小部件 ID 是否有效
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);
// 传递小部件 ID 到意图中
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
// 传递小部件类型到意图中
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
// 获取指定小部件 ID 对应的笔记信息
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;
}
// 获取笔记摘要
snippet = c.getString(COLUMN_SNIPPET);
// 获取背景 ID
bgId = c.getInt(COLUMN_BG_COLOR_ID);
// 传递笔记 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 rv = new RemoteViews(context.getPackageName(), getLayoutId());
// 设置小部件的背景图片资源
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
// 传递背景 ID 到意图中
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
/**
* 生成用于启动小部件宿主的待定意图
*/
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();
}