diff --git a/src/widget/NoteWidgetProvider.java b/src/widget/NoteWidgetProvider.java new file mode 100644 index 0000000..0aba850 --- /dev/null +++ b/src/widget/NoteWidgetProvider.java @@ -0,0 +1,131 @@ +/** + * 便签桌面小部件的基类,提供通用功能实现 + * 支持显示便签内容并处理点击事件,派生类需实现UI细节 + */public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 查询便签数据时需要的字段列表 + public static final String [] PROJECTION = new String [] { + NoteColumns.ID, // 便签ID + NoteColumns.BG_COLOR_ID, // 便签背景色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_id字段置为无效值 + */ + @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])}); + } + } + + /** + * 获取与指定小部件ID关联的便签信息 + * 过滤条件: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); + } + + /** + * 更新小部件UI(隐私模式关闭) + */ + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + update(context, appWidgetManager, appWidgetIds, false); + } + + /** + * 核心更新逻辑,支持隐私模式 + * 1. 查询便签数据 + * 2. 设置背景和内容 + * 3. 配置点击事件 + */ + 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); + 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()); + + // 查询数据库获取便签信息 + Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); + if (c != null && c.moveToFirst()) { + // 检查是否存在多个便签关联到同一小部件ID(异常情况) + 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(); + } + + // 创建RemoteViews并设置UI元素 + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); + + // 根据隐私模式设置不同的点击行为 + 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); + } + } + } + + // 由派生类实现的抽象方法,用于定制UI细节 + protected abstract int getBgResourceId(int bgId); // 获取背景资源ID + protected abstract int getLayoutId(); // 获取布局资源ID + protected abstract int getWidgetType(); // 获取小部件类型} diff --git a/src/widget/NoteWidgetProvider_2x.java b/src/widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..9aedd38 --- /dev/null +++ b/src/widget/NoteWidgetProvider_2x.java @@ -0,0 +1,67 @@ +/* + * 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.appwidget.AppWidgetManager;import android.content.Context; +import net.micode.notes.R;import net.micode.notes.data.Notes;import net.micode.notes.tool.ResourceParser; +/** + * NoteWidgetProvider_2x 是一个用于处理 2x 尺寸桌面小部件的组件 + * 继承自 NoteWidgetProvider 基类,负责特定尺寸小部件的更新和显示 + */public class NoteWidgetProvider_2x extends NoteWidgetProvider { + + /** + * 当小部件需要更新时调用此方法 + * + * @param context 应用上下文 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 需要更新的小部件ID数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的更新方法处理通用逻辑 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取此小部件使用的布局资源ID + * + * @return 布局资源ID (R.layout.widget_2x) + */ + @Override + protected int getLayoutId() { + return R.layout.widget_2x; + } + + /** + * 根据背景ID获取对应的背景资源 + * + * @param bgId 背景ID,用于标识不同的背景样式 + * @return 背景资源ID + */ + @Override + protected int getBgResourceId(int bgId) { + // 通过资源解析工具获取2x小部件的背景资源 + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + /** + * 获取当前小部件的类型 + * + * @return 小部件类型常量 (Notes.TYPE_WIDGET_2X) + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; + }} diff --git a/src/widget/NoteWidgetProvider_4x.java b/src/widget/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..48f293b --- /dev/null +++ b/src/widget/NoteWidgetProvider_4x.java @@ -0,0 +1,66 @@ +/* + * 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.appwidget.AppWidgetManager;import android.content.Context; +import net.micode.notes.R;import net.micode.notes.data.Notes;import net.micode.notes.tool.ResourceParser; +/** + * 4x大小的便签桌面小部件提供者类。 + * 继承自NoteWidgetProvider,用于处理4x大小便签小部件的更新和显示。 + */public class NoteWidgetProvider_4x extends NoteWidgetProvider { + + /** + * 当小部件更新时调用的方法。 + * + * @param context 应用程序上下文 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 需要更新的小部件ID数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的更新方法处理小部件更新逻辑 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取小部件布局资源ID。 + * + * @return 4x小部件的布局资源ID + */ + protected int getLayoutId() { + return R.layout.widget_4x; + } + + /** + * 根据背景ID获取对应的背景资源ID。 + * + * @param bgId 背景ID + * @return 对应的背景资源ID + */ + @Override + protected int getBgResourceId(int bgId) { + // 使用资源解析器工具类获取4x小部件的背景资源 + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + /** + * 获取小部件类型。 + * + * @return 小部件类型常量,这里表示4x大小的便签小部件 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; + }}