lixing
lixing 9 months ago
parent 4b53318c0c
commit 6c9965b62a

@ -0,0 +1,168 @@
/*
* 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;
// 导入所需的Android类和接口
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;
// 定义一个抽象类NoteWidgetProvider继承自AppWidgetProvider用于管理便签Widget
public abstract class NoteWidgetProvider extends AppWidgetProvider {
// 定义查询数据库时使用的列名数组
public static final String [] PROJECTION = new String [] {
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;
// 定义日志标签
private static final String TAG = "NoteWidgetProvider";
// 当Widget被删除时调用的方法
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// 为数据库更新准备ContentValues对象
ContentValues values = new ContentValues();
// 设置WIDGET_ID为无效值
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// 遍历所有被删除的Widget ID
for (int i = 0; i < appWidgetIds.length; i++) {
// 更新数据库中对应的Widget ID为无效
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])});
}
}
// 获取与特定Widget ID相关的便签信息
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);
}
// 更新Widget的方法可以被外部调用
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用内部的update方法不启用隐私模式
update(context, appWidgetManager, appWidgetIds, false);
}
// 更新Widget的内部方法根据是否启用隐私模式来更新Widget
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {
// 遍历所有Widget ID
for (int i = 0; i < appWidgetIds.length; i++) {
// 如果Widget ID有效
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
// 获取默认背景ID
int bgId = ResourceParser.getDefaultBgId(context);
// 初始化摘要字符串
String snippet = "";
// 创建Intent用于编辑便签
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());
// 获取便签信息的Cursor
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
// 如果Cursor不为空且第一条数据有效
if (c != null && c.moveToFirst()) {
// 如果有多条数据与Widget ID匹配记录错误日志并返回
if (c.getCount() > 1) {
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
c.close();
return;
}
// 从Cursor中获取摘要和背景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);
}
// 关闭Cursor
if (c != null) {
c.close();
}
// 创建RemoteViews对象用于更新Widget视图
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
// 设置背景图片资源ID
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
// 生成PendingIntent用于启动宿主Activity
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);
}
// 设置RemoteViews的PendingIntent
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
// 更新Widget
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
}
// 抽象方法用于获取背景资源ID
protected abstract int getBgResourceId(int bgId);
// 抽象方法用于获取布局ID
protected abstract int getLayoutId();
// 抽象方法用于获取Widget类型
protected abstract int getWidgetType();
}

@ -0,0 +1,46 @@
/*
* 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.
*/
// 定义了一个名为NoteWidgetProvider_2x的类它继承自NoteWidgetProvider类
public class NoteWidgetProvider_2x extends NoteWidgetProvider {
// 覆盖了onUpdate方法该方法是AppWidgetProvider的一个回调方法用于当小部件需要更新时被调用
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类的update方法传入context、appWidgetManager和appWidgetIds参数
super.update(context, appWidgetManager, appWidgetIds);
}
// 覆盖了getLayoutId方法该方法用于返回小部件的布局资源ID
@Override
protected int getLayoutId() {
// 返回名为widget_2x的布局资源ID这个布局资源定义了小部件的外观
return R.layout.widget_2x;
}
// 覆盖了getBgResourceId方法该方法用于根据传入的背景ID返回对应的背景资源ID
@Override
protected int getBgResourceId(int bgId) {
// 调用ResourceParser.WidgetBgResources类的getWidget2xBgResource方法传入bgId参数返回对应的背景资源ID
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
}
// 覆盖了getWidgetType方法该方法用于返回小部件的类型
@Override
protected int getWidgetType() {
// 返回Notes.TYPE_WIDGET_2X这是一个常量表示这是一个2x2大小的小部件
return Notes.TYPE_WIDGET_2X;
}
}

@ -0,0 +1,55 @@
/*
* 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.
*/
// 定义了一个名为NoteWidgetProvider_4x的类它继承自NoteWidgetProvider类
package net.micode.notes.widget;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.ResourceParser;
public class NoteWidgetProvider_4x extends NoteWidgetProvider {
// 覆盖了onUpdate方法该方法是AppWidgetProvider的一个回调方法用于当小部件需要更新时被调用
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类的update方法传入context、appWidgetManager和appWidgetIds参数
super.update(context, appWidgetManager, appWidgetIds);
}
// getLayoutId方法用于返回小部件的布局资源ID
// 注意:这个方法没有被标记为@Override这意味着它不是覆盖父类的方法而是NoteWidgetProvider_4x类中新增的方法
protected int getLayoutId() {
// 返回名为widget_4x的布局资源ID这个布局资源定义了小部件的外观
return R.layout.widget_4x;
}
// 覆盖了getBgResourceId方法该方法用于根据传入的背景ID返回对应的背景资源ID
@Override
protected int getBgResourceId(int bgId) {
// 调用ResourceParser.WidgetBgResources类的getWidget4xBgResource方法传入bgId参数返回对应的背景资源ID
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
}
// 覆盖了getWidgetType方法该方法用于返回小部件的类型
@Override
protected int getWidgetType() {
// 返回Notes.TYPE_WIDGET_4X这是一个常量表示这是一个4x4大小的小部件
return Notes.TYPE_WIDGET_4X;
}
}
Loading…
Cancel
Save