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

172 lines
8.6 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,
NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET
};// 定义了一个静态常量数组 PROJECTION用于指定在查询便签小部件信息时需要返回的列
public static final int COLUMN_ID = 0;// 定义了一个公共静态常量 COLUMN_ID其值为 0。这个常量用于表示在查询便签小部件信息时ID 列所在的位置。
public static final int COLUMN_BG_COLOR_ID = 1;// 定义了另一个公共静态常量 COLUMN_BG_COLOR_ID其值为 1。这个常量用于表示在查询便签小部件信息时背景颜色 ID
// 列所在的位置。
public static final int COLUMN_SNIPPET = 2;// 定义了第三个公共静态常量 COLUMN_SNIPPET其值为
// 2。这个常量用于表示在查询便签小部件信息时摘要snippet列所在的位置。
private static final String TAG = "NoteWidgetProvider";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// 当小部件被删除时,将小部件 ID 更新为无效的 ID
ContentValues values = new ContentValues();
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);// 将所有具有特定小部件ID的便签的小部件ID设置为无效的ID。
for (int i = 0; i < appWidgetIds.length; i++) {
// 遍历给定的小部件ID数组 appWidgetIds。
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i]) });
}
}
/**
* 获取便签小部件信息的游标
*/
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);
}// 使用 context.getContentResolver().query() 方法查询数据库中的便签信息。
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false);
}// 使用保护的方法更新便签小部件。
/**
* 更新小部件的方法
*/
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {
for (int i = 0; i < appWidgetIds.length; i++) {
// 历传入的小部件ID数组 appWidgetIds。
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
// 在循环中检查当前小部件ID是否有效即不等于 AppWidgetManager.INVALID_APPWIDGET_ID。
int bgId = ResourceParser.getDefaultBgId(context);// 获取默认背景ID。
String snippet = "";// 创建一个空字符串 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 中添加小部件ID和小部件类型的额外信息。
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
// 进行判断,对便签信息进行存储等相关操作。
if (c != null && c.moveToFirst()) {
if (c.getCount() > 1) {
Log.e(TAG, "相同小部件 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();
}
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());// 创建一个 RemoteViews
// 对象,用于更新小部件的视图。
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
// 设置小部件布局中 R.id.widget_bg_image 控件的背景图片资源,背景图片资源由 getBgResourceId(bgId)
// 方法根据传入的背景ID bgId 获取。
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);// 将背景ID添加到 Intent 中。
// 生成启动活动的待定意图
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 {
// 如果没有隐私将小部件的文本内容设置为便签的摘要信息snippet
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 设置给小部件的文本视图R.id.widget_text
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
// 更新小部件的内容和点击事件处理逻辑。
}
}
}
/**
* 根据 bgId 获取背景资源 ID
*/
protected abstract int getBgResourceId(int bgId);
/**
* 获取小部件的布局 ID
*/
protected abstract int getLayoutId();
/**
* 获取小部件类型
*/
protected abstract int getWidgetType();
}
// 代码实现了一个抽象类
// NoteWidgetProvider用于管理便签小部件的显示和更新。它提供了获取便签小部件信息、更新小部件等功能并定义了一些抽象方法需要在子类中实现。