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

133 lines
7.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;// 导入PendingIntent类
import android.appwidget.AppWidgetManager;// 导入AppWidgetManager类
import android.appwidget.AppWidgetProvider;// 导入AppWidgetProvider类
import android.content.ContentValues;// 导入ContentValues类
import android.content.Context;// 导入Context类
import android.content.Intent;// 导入Intent类
import android.database.Cursor;// 导入Cursor类
import android.util.Log;// 导入Log类
import android.widget.RemoteViews;// 导入RemoteViews类
import net.micode.notes.R;// 导入R类用于访问资源
import net.micode.notes.data.Notes;// 导入Notes类
import net.micode.notes.data.Notes.NoteColumns;// 导入NoteColumns类
import net.micode.notes.tool.ResourceParser;// 导入ResourceParser类
import net.micode.notes.ui.NoteEditActivity;// 导入NoteEditActivity类
import net.micode.notes.ui.NotesListActivity;// 导入NotesListActivity类
public abstract class NoteWidgetProvider extends AppWidgetProvider {// 声明一个抽象类NoteWidgetProvider继承自AppWidgetProvider
public static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET
};// 定义一个常量数组PROJECTION包含NoteColumns的ID、BG_COLOR_ID和SNIPPET
public static final int COLUMN_ID = 0;// 定义COLUMN_ID常量值为0
public static final int COLUMN_BG_COLOR_ID = 1;// 定义COLUMN_BG_COLOR_ID常量值为1
public static final int COLUMN_SNIPPET = 2;// 定义COLUMN_SNIPPET常量值为2
private static final String TAG = "NoteWidgetProvider";// 定义TAG常量用于日志记录
@Override
public void onDeleted(Context context, int[] appWidgetIds) {// 重写onDeleted方法当小部件被删除时调用
ContentValues values = new ContentValues();// 创建ContentValues对象
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);// 将WIDGET_ID列的值设置为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])});// 更新数据库将指定小部件ID的WIDGET_ID列设置为INVALID_APPWIDGET_ID
}
}
private Cursor getNoteWidgetInfo(Context context, int widgetId) {// 定义getNoteWidgetInfo方法获取小部件信息
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);// 查询数据库返回符合条件的Cursor对象
}
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {// 定义update方法更新小部件
update(context, appWidgetManager, appWidgetIds, false);
}// 调用重载的update方法传入false作为privacyMode参数
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {// 定义重载的update方法更新小部件传入privacyMode参数
for (int i = 0; i < appWidgetIds.length; i++) {
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {// 如果小部件ID有效
int bgId = ResourceParser.getDefaultBgId(context);// 获取默认背景ID
String snippet = "";// 初始化snippet字符串
Intent intent = new Intent(context, NoteEditActivity.class);// 创建Intent对象指向NoteEditActivity
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);// 设置Intent标志确保活动位于任务栈顶部
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);// 添加额外数据指定小部件ID
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());// 添加额外数据,指定小部件类型
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);// 获取小部件信息的Cursor对象
if (c != null && c.moveToFirst()) {// 如果Cursor不为空且移动到第一条记录
if (c.getCount() > 1) {// 如果记录数大于1
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);// 记录错误日志
c.close();
return;
}
snippet = c.getString(COLUMN_SNIPPET);// 获取SNIPPET列的值
bgId = c.getInt(COLUMN_BG_COLOR_ID);// 获取BG_COLOR_ID列的值
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); // 添加额外数据指定记录ID
intent.setAction(Intent.ACTION_VIEW);
} else {
snippet = context.getResources().getString(R.string.widget_havenot_content);
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
}// 设置Intent动作为ACTION_INSERT_OR_EDIT
if (c != null) {
c.close();
}
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());// 创建RemoteViews对象指定布局ID
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;
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);// 创建PendingIntent对象指向NoteEditActivity
}
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);// 设置文本视图的点击
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
}
protected abstract int getBgResourceId(int bgId);// 声明抽象方法getBgResourceId获取背景资源ID
protected abstract int getLayoutId();// 声明抽象方法getLayoutId获取布局ID
protected abstract int getWidgetType();// 声明抽象方法getWidgetType
}