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.
xiaomi-Notes/NoteWidgetProvider.java

143 lines
8.8 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.
*/
package net.micode.notes.widget;/*声明了该文件所属的包。*/
import android.app.PendingIntent;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.appwidget.AppWidgetManager;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.appwidget.AppWidgetProvider;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.content.ContentValues;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.content.Context;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.content.Intent;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.database.Cursor;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.util.Log;/*引入了所需的Android框架和自定义类的导入语句。*/
import android.widget.RemoteViews;/*引入了所需的Android框架和自定义类的导入语句。*/
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;/*引入了所需的Android框架和自定义类的导入语句。*/
public abstract class NoteWidgetProvider extends AppWidgetProvider {/*定义了一个名为 NoteWidgetProvider
的抽象类,它继承自 AppWidgetProvider。这意味着 NoteWidgetProvider 是一个小部件提供者,可以处理小部件的更新和广播。*/
public static final String [] PROJECTION = new String [] {/*定义了一个字符串数组 PROJECTION
用于指定查询数据库时返回的列。这里指定了笔记的ID、背景颜色ID和摘要。*/
NoteColumns.ID,
NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET
};
/*定义了三个常量,分别表示 PROJECTION 数组中ID、背景颜色ID和摘要的索引位置。*/
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) {/*重写了 onDeleted 方法,当小部件被删除时调用。*/
ContentValues values = new ContentValues();/*创建一个 ContentValues 对象,并设置 WIDGET_ID 为无效的小部件ID
用于更新数据库中的相关记录。*/
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
for (int i = 0; i < appWidgetIds.length; i++) {/*遍历被删除的小部件ID数组并更新数据库中相关记录
的 WIDGET_ID 为无效值。*/
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])});
}
}
private Cursor getNoteWidgetInfo(Context context, int widgetId) {/*定义了一个私有方法,用于查询数据库获
取指定小部件ID的笔记信息。*/
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);/*使用 ContentResolver 查询数据库,返回包含笔记信息的 Cursor 对象。*/
}
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false);
}/*定义了一个受保护的方法 update它调用另一个重载的 update 方法,并传递一个默认的隐私模式值 false。*/
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {/*定义了一个私有的重载方法 update用于根据小部件ID数组更新小部件视图。privacyMode 参数
用于指示是否处于隐私模式。*/
for (int i = 0; i < appWidgetIds.length; i++) {
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {/*遍历小部件ID数组并检查每个ID是否有效。*/
int bgId = ResourceParser.getDefaultBgId(context);/*初始化背景ID和摘要字符串。*/
String snippet = "";
Intent intent = new Intent(context, NoteEditActivity.class);/*创建一个意图,用于启动 NoteEditActivity。*/
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 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 rv = new RemoteViews(context.getPackageName(), getLayoutId());/*创建一个 RemoteViews 对象,
用于定义小部件的远程视图。*/
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));/*设置小部件背景图片。*/
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);/*向意图添加背景ID的额外数据。*/
/**
* Generate the pending intent to start host for the widget
*/
PendingIntent pendingIntent = null;/*初始化 PendingIntent 对象。*/
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 {/*如果不是隐私模式,则:设置文本视图为摘要文本,并创建指向 NoteEditActivity 的 PendingIntent。*/
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);/*设置文本视图的点击事件为之前创建的
PendingIntent。*/
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);/*使用新的远程视图更新小部件。*/
}
}
}
/*定义了三个抽象方法子类需要实现这些方法以提供背景资源ID、布局ID和小部件类型。*/
protected abstract int getBgResourceId(int bgId);
protected abstract int getLayoutId();
protected abstract int getWidgetType();
}