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.
TeamProject/other/07_210340176 张嘉欣_代码标注/widget/NoteWidgetProvider.java

156 lines
7.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;
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,//笔记的ID号
NoteColumns.BG_COLOR_ID,//背景颜色的ID号
NoteColumns.SNIPPET//笔记的片段
};
/**
*抽象类NoteWidgetProvider并在其中定义了一个名为PROJECTION的常量数组。该数组中包含三个元素分别是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;
//定义了三个常量 COLUMN_ID、COLUMN_BG_COLOR_ID 和 COLUMN_SNIPPET它们分别被赋值为0、1和2。
private static final String TAG = "NoteWidgetProvider";
//定义了一个名为TAG的私有常量字符串其值为 "NoteWidgetProvider",这个常量通常用于日志输出中,方便开发人员在调试时快速定位和识别日志信息来源。
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
ContentValues values = new ContentValues();//要更新到数据库中的键值对。
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
for (int i = 0; i < appWidgetIds.length; i++) {
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])});
}
}
/**
*这段代码是一个名为 onDeleted() 的方法,用于在从主屏幕上删除指定的笔记小部件时调用。
*/
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) },//用ID来筛选同时要是存在于未被删除的文件夹中
null);
}
/**
*查询数据库中与指定小部件 ID 相关联的所有笔记记录,并返回表示查询结果的 Cursor 对象
*/
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false);
}
/**
* 用于更新小部件的显示内容。该方法接受三个参数context、appWidgetManager 和 appWidgetIds分别表示上下文对象、小部件管理器和要更新的小部件 ID 列表。
*/
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) {
int bgId = ResourceParser.getDefaultBgId(context);//通过调用 ResourceParser.getDefaultBgId(context) 方法来获取默认背景 ID 并赋值给整型变量 bgId。
String snippet = "";
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());
/**
* 创建一个 Intent 对象 intent将 NoteEditActivity 类作为目标 Activity并添加一些额外参数如小部件 ID、小部件类型等信息以及标志位 FLAG_ACTIVITY_SINGLE_TOP。
*
*/
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;
}
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();
}// Cursor 对象 c
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());//rv将其与小部件布局文件对应起来。
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
/**
* Generate the pending intent to start host for the widget
*/
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);//更新指定小部件的布局和显示内容。
}
}
}
/**
* update 的私有重载方法是一个私有方法,接受四个参数:上下文对象 context、小部件管理器 appWidgetManager、小部件 ID 列表 appWidgetIds 和一个布尔型参数 privacyMode表示是否启用隐私模式。在该方法中我们可以根据是否启用隐私模式来更新小部件的显示内容例如显示隐私模式的提示信息等。
*/
protected abstract int getBgResourceId(int bgId);
protected abstract int getLayoutId();
protected abstract int getWidgetType();
}