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

3 months ago
/*
* 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
IDID*/
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) {/* updateIDprivacyMode
*/
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();
}