widget说明

Signed-off-by: reobert <3462461039@qq.com>
mas
3462461039 2 months ago
parent 82c6267aab
commit 9a36f541cc

@ -15,41 +15,88 @@
*/ */
package net.micode.notes.widget; package net.micode.notes.widget;
// 导入 Android 中用于创建延迟执行意图的类
import android.app.PendingIntent; import android.app.PendingIntent;
// 导入 Android 中用于管理桌面小部件的类
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
// 导入 Android 中桌面小部件提供者的基类
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
// 导入 Android 中用于存储键值对的类,常用于 ContentResolver 操作
import android.content.ContentValues; import android.content.ContentValues;
// 导入 Android 中表示应用程序环境的上下文类
import android.content.Context; import android.content.Context;
// 导入 Android 中用于在不同组件间传递消息的意图类
import android.content.Intent; import android.content.Intent;
// 导入 Android 中用于处理数据库查询结果的游标类
import android.database.Cursor; import android.database.Cursor;
// 导入 Android 中用于日志记录的类
import android.util.Log; import android.util.Log;
// 导入 Android 中用于远程操作视图的类
import android.widget.RemoteViews; import android.widget.RemoteViews;
// 导入应用资源类
import net.micode.notes.R; import net.micode.notes.R;
// 导入应用笔记数据相关类
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
// 导入应用笔记数据列相关类
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
// 导入应用资源解析工具类
import net.micode.notes.tool.ResourceParser; import net.micode.notes.tool.ResourceParser;
// 导入应用笔记编辑活动类
import net.micode.notes.ui.NoteEditActivity; import net.micode.notes.ui.NoteEditActivity;
// 导入应用笔记列表活动类
import net.micode.notes.ui.NotesListActivity; import net.micode.notes.ui.NotesListActivity;
/**
* NoteWidgetProvider AppWidgetProvider
*
*/
public abstract class NoteWidgetProvider extends AppWidgetProvider { public abstract class NoteWidgetProvider extends AppWidgetProvider {
/**
*
*/
public static final String [] PROJECTION = new String [] { public static final String [] PROJECTION = new String [] {
NoteColumns.ID, // 笔记的 ID 列
NoteColumns.BG_COLOR_ID, NoteColumns.ID,
NoteColumns.SNIPPET // 笔记的背景颜色 ID 列
NoteColumns.BG_COLOR_ID,
// 笔记的摘要列
NoteColumns.SNIPPET
}; };
/**
* PROJECTION ID
*/
public static final int COLUMN_ID = 0; public static final int COLUMN_ID = 0;
/**
* PROJECTION ID
*/
public static final int COLUMN_BG_COLOR_ID = 1; public static final int COLUMN_BG_COLOR_ID = 1;
/**
* PROJECTION
*/
public static final int COLUMN_SNIPPET = 2; public static final int COLUMN_SNIPPET = 2;
/**
*
*/
private static final String TAG = "NoteWidgetProvider"; private static final String TAG = "NoteWidgetProvider";
/**
*
* ID
*
* @param context
* @param appWidgetIds ID
*/
@Override @Override
public void onDeleted(Context context, int[] appWidgetIds) { public void onDeleted(Context context, int[] appWidgetIds) {
// 创建一个 ContentValues 对象,用于存储要更新的数据
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
// 将笔记的小部件 ID 设置为无效值
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// 遍历被删除的小部件 ID 数组
for (int i = 0; i < appWidgetIds.length; i++) { for (int i = 0; i < appWidgetIds.length; i++) {
// 更新与该小部件关联的笔记的小部件 ID
context.getContentResolver().update(Notes.CONTENT_NOTE_URI, context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values, values,
NoteColumns.WIDGET_ID + "=?", NoteColumns.WIDGET_ID + "=?",
@ -57,7 +104,15 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
} }
} }
/**
* ID
*
* @param context
* @param widgetId ID
* @return Cursor
*/
private Cursor getNoteWidgetInfo(Context context, int widgetId) { private Cursor getNoteWidgetInfo(Context context, int widgetId) {
// 查询与指定小部件 ID 关联的笔记信息
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION, PROJECTION,
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
@ -65,68 +120,134 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
null); null);
} }
/**
*
*
* @param context
* @param appWidgetManager
* @param appWidgetIds ID
*/
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用带隐私模式参数的更新方法,隐私模式为 false
update(context, appWidgetManager, appWidgetIds, false); update(context, appWidgetManager, appWidgetIds, false);
} }
/**
*
*
* @param context
* @param appWidgetManager
* @param appWidgetIds ID
* @param privacyMode
*/
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) { boolean privacyMode) {
// 遍历需要更新的小部件 ID 数组
for (int i = 0; i < appWidgetIds.length; i++) { for (int i = 0; i < appWidgetIds.length; i++) {
// 检查小部件 ID 是否有效
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
// 获取默认的背景颜色 ID
int bgId = ResourceParser.getDefaultBgId(context); int bgId = ResourceParser.getDefaultBgId(context);
// 初始化笔记摘要字符串
String snippet = ""; String snippet = "";
// 创建一个意图,用于启动笔记编辑活动
Intent intent = new Intent(context, NoteEditActivity.class); Intent intent = new Intent(context, NoteEditActivity.class);
// 设置意图标志,确保活动以单顶部模式启动
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// 将小部件 ID 作为额外数据添加到意图中
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
// 将小部件类型作为额外数据添加到意图中
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
// 获取与该小部件关联的笔记信息
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
// 检查游标是否不为空且有数据
if (c != null && c.moveToFirst()) { if (c != null && c.moveToFirst()) {
// 检查是否存在多个笔记与该小部件关联
if (c.getCount() > 1) { if (c.getCount() > 1) {
// 记录错误日志
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]); Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
// 关闭游标
c.close(); c.close();
return; return;
} }
// 获取笔记的摘要信息
snippet = c.getString(COLUMN_SNIPPET); snippet = c.getString(COLUMN_SNIPPET);
// 获取笔记的背景颜色 ID
bgId = c.getInt(COLUMN_BG_COLOR_ID); bgId = c.getInt(COLUMN_BG_COLOR_ID);
// 将笔记的 ID 作为额外数据添加到意图中
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
// 设置意图的操作类型为查看
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);
} else { } else {
// 如果没有关联的笔记,设置默认的摘要信息
snippet = context.getResources().getString(R.string.widget_havenot_content); snippet = context.getResources().getString(R.string.widget_havenot_content);
// 设置意图的操作类型为插入或编辑
intent.setAction(Intent.ACTION_INSERT_OR_EDIT); intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
} }
// 检查游标是否不为空,若不为空则关闭游标
if (c != null) { if (c != null) {
c.close(); c.close();
} }
// 创建一个 RemoteViews 对象,用于更新小部件的视图
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
// 设置小部件的背景图片资源
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
// 将背景颜色 ID 作为额外数据添加到意图中
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
/** /**
* Generate the pending intent to start host for the widget * 宿
*/ */
// 初始化延迟意图对象
PendingIntent pendingIntent = null; PendingIntent pendingIntent = null;
// 检查是否开启隐私模式
if (privacyMode) { if (privacyMode) {
// 设置小部件文本为隐私模式下的提示信息
rv.setTextViewText(R.id.widget_text, rv.setTextViewText(R.id.widget_text,
context.getString(R.string.widget_under_visit_mode)); context.getString(R.string.widget_under_visit_mode));
// 创建一个新的意图,用于启动笔记列表活动
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
} else { } else {
// 设置小部件文本为笔记的摘要信息
rv.setTextViewText(R.id.widget_text, snippet); rv.setTextViewText(R.id.widget_text, snippet);
// 创建一个延迟意图,用于启动笔记编辑活动
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
} }
// 设置小部件文本区域的点击事件为延迟意图
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
// 更新指定小部件的内容
appWidgetManager.updateAppWidget(appWidgetIds[i], rv); appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
} }
} }
} }
/**
* ID ID
*
*
* @param bgId ID
* @return ID
*/
protected abstract int getBgResourceId(int bgId); protected abstract int getBgResourceId(int bgId);
/**
* ID
*
*
* @return ID
*/
protected abstract int getLayoutId(); protected abstract int getLayoutId();
/**
*
*
*
* @return
*/
protected abstract int getWidgetType(); protected abstract int getWidgetType();
} }

@ -14,34 +14,77 @@
* limitations under the License. * limitations under the License.
*/ */
/**
*
*/
package net.micode.notes.widget; package net.micode.notes.widget;
// 导入 Android 系统中用于管理桌面小部件的类
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
// 导入 Android 系统中表示应用程序环境的上下文类
import android.content.Context; import android.content.Context;
// 导入应用资源类
import net.micode.notes.R; import net.micode.notes.R;
// 导入应用笔记数据相关类
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
// 导入应用资源解析工具类
import net.micode.notes.tool.ResourceParser; import net.micode.notes.tool.ResourceParser;
/**
* NoteWidgetProvider_2x NoteWidgetProvider 2x
* 2x
*/
public class NoteWidgetProvider_2x extends NoteWidgetProvider { public class NoteWidgetProvider_2x extends NoteWidgetProvider {
/**
*
* update
*
* @param context 访
* @param appWidgetManager
* @param appWidgetIds ID
*/
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类的 update 方法更新小部件
super.update(context, appWidgetManager, appWidgetIds); super.update(context, appWidgetManager, appWidgetIds);
} }
/**
* 2x ID
* 2x ID
*
* @return 2x ID
*/
@Override @Override
protected int getLayoutId() { protected int getLayoutId() {
// 返回 2x 小部件的布局资源 ID
return R.layout.widget_2x; return R.layout.widget_2x;
} }
/**
* ID 2x ID
* 使 ResourceParser ID
*
* @param bgId ID
* @return 2x ID
*/
@Override @Override
protected int getBgResourceId(int bgId) { protected int getBgResourceId(int bgId) {
// 使用 ResourceParser 工具类获取 2x 小部件的背景资源 ID
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
} }
/**
* 2x
* 2x
*
* @return 2x
*/
@Override @Override
protected int getWidgetType() { protected int getWidgetType() {
// 返回 2x 小部件的类型常量
return Notes.TYPE_WIDGET_2X; return Notes.TYPE_WIDGET_2X;
} }
} }

@ -2,45 +2,86 @@
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * 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 * 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; package net.micode.notes.widget;
// 导入 Android 系统中用于管理桌面小部件的类
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
// 导入 Android 系统中表示应用程序环境的上下文类
import android.content.Context; import android.content.Context;
// 导入应用资源类
import net.micode.notes.R; import net.micode.notes.R;
// 导入应用笔记数据相关类
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
// 导入应用资源解析工具类
import net.micode.notes.tool.ResourceParser; import net.micode.notes.tool.ResourceParser;
/**
* NoteWidgetProvider_4x NoteWidgetProvider 4x
* 4x
*/
public class NoteWidgetProvider_4x extends NoteWidgetProvider { public class NoteWidgetProvider_4x extends NoteWidgetProvider {
/**
*
* update
*
* @param context 访
* @param appWidgetManager
* @param appWidgetIds ID
*/
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类的 update 方法更新小部件
super.update(context, appWidgetManager, appWidgetIds); super.update(context, appWidgetManager, appWidgetIds);
} }
/**
* 4x ID
* 4x ID
*
* @return 4x ID
*/
@Override
protected int getLayoutId() { protected int getLayoutId() {
// 返回 4x 小部件的布局资源 ID
return R.layout.widget_4x; return R.layout.widget_4x;
} }
/**
* ID 4x ID
* 使 ResourceParser ID
*
* @param bgId ID
* @return 4x ID
*/
@Override @Override
protected int getBgResourceId(int bgId) { protected int getBgResourceId(int bgId) {
// 使用 ResourceParser 工具类获取 4x 小部件的背景资源 ID
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
} }
/**
* 4x
* 4x
*
* @return 4x
*/
@Override @Override
protected int getWidgetType() { protected int getWidgetType() {
// 返回 4x 小部件的类型常量
return Notes.TYPE_WIDGET_4X; return Notes.TYPE_WIDGET_4X;
} }
} }

Loading…
Cancel
Save