dhc 8 months ago
parent d5a69684eb
commit 543278354c

166
w.txt

@ -0,0 +1,166 @@
/*
* 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;
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;
// 抽象的AppWidgetProvider子类用于为笔记相关的小部件提供功能支持
// 注意它是抽象类,意味着具体功能需要由子类去实现特定的抽象方法
public abstract class NoteWidgetProvider extends AppWidgetProvider {
// 定义查询数据库时使用的投影(即要查询的列),用于获取笔记小部件相关信息
// 包含笔记的ID、背景颜色ID以及内容摘要等列
public static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET
};
// 定义列索引常量方便后续从游标Cursor中获取对应列的数据
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";
// 当小部件被删除时调用的方法
// 作用是将与被删除小部件相关的数据库记录中的WIDGET_ID设置为无效值
// 这样表示对应的笔记不再与该小部件关联
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
ContentValues values = new ContentValues();
// 设置要更新到数据库的WIDGET_ID为无效值
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
for (int i = 0; i < appWidgetIds.length; i++) {
// 通过ContentResolver更新数据库中对应的记录
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])});
}
}
// 私有方法用于获取与指定小部件ID相关的笔记小部件信息的游标Cursor
// 通过查询数据库筛选出符合特定条件小部件ID匹配且不属于回收站文件夹的笔记记录
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);
}
// 用于更新小部件的方法调用了另一个重载的update方法并传入默认的隐私模式参数false
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false);
}
// 私有方法,真正执行小部件更新的逻辑
// 根据传入的小部件ID数组逐个更新对应的小部件显示内容和相关交互设置等
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {
for (int i = 0; i < appWidgetIds.length; i++) {
// 检查小部件ID是否有效如果无效则跳过该小部件的更新
if (appWidgetIds[i]!= AppWidgetManager.INVALID_APPWIDGET_ID) {
int bgId = ResourceParser.getDefaultBgId(context);
String snippet = "";
// 创建一个启动NoteEditActivity的意图Intent用于点击小部件文本时进行相应操作
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());
// 获取与当前小部件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;
}
// 从游标中获取内容摘要、背景颜色ID以及笔记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));
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
// 根据隐私模式生成对应的PendingIntent用于处理小部件点击事件
PendingIntent pendingIntent = null;
if (privacyMode) {
// 如果是隐私模式设置特定的提示文本并创建启动NotesListActivity的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 {
// 非隐私模式下设置内容摘要文本并创建基于前面创建的intent的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更新小部件的显示内容
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
}
// 抽象方法由子类实现用于根据给定的背景颜色ID获取对应的背景资源ID
protected abstract int getBgResourceId(int bgId);
// 抽象方法由子类实现用于获取小部件对应的布局资源ID
protected abstract int getLayoutId();
// 抽象方法,由子类实现,用于获取小部件的类型
protected abstract int getWidgetType();
}
Loading…
Cancel
Save