From 061de96ae5989c67479aec35a5b574f5717d7b3d Mon Sep 17 00:00:00 2001 From: fangzheng <13929869895@163.com> Date: Sat, 17 Jan 2026 16:48:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/widget/NoteWidgetProvider.java | 187 -------------------------- src/widget/NoteWidgetProvider_2x.java | 76 ----------- src/widget/NoteWidgetProvider_4x.java | 72 ---------- 3 files changed, 335 deletions(-) delete mode 100644 src/widget/NoteWidgetProvider.java delete mode 100644 src/widget/NoteWidgetProvider_2x.java delete mode 100644 src/widget/NoteWidgetProvider_4x.java diff --git a/src/widget/NoteWidgetProvider.java b/src/widget/NoteWidgetProvider.java deleted file mode 100644 index e953109..0000000 --- a/src/widget/NoteWidgetProvider.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * 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, // 便签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"; - - /** - * 当小部件被删除时调用 - * 清除数据库中与该小部件关联的记录 - */ - @Override - public void onDeleted(Context context, int[] appWidgetIds) { - ContentValues values = new ContentValues(); - // 将小部件ID设为无效值 - 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])}); - } - } - - /** - * 获取与指定小部件关联的便签信息 - * @param context 上下文 - * @param widgetId 小部件ID - * @return 包含便签信息的游标 - */ - private Cursor getNoteWidgetInfo(Context context, int widgetId) { - return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, - PROJECTION, - // 查询条件:小部件ID匹配且不在回收站中 - NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", - new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) }, - null); - } - - /** - * 公开的更新方法,调用带隐私模式参数的私有更新方法 - */ - protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - update(context, appWidgetManager, appWidgetIds, false); - } - - /** - * 核心更新方法,更新小部件的UI显示 - * @param context 上下文 - * @param appWidgetManager 小部件管理器 - * @param appWidgetIds 需要更新的小部件ID数组 - * @param privacyMode 是否隐私模式(隐藏内容) - */ - private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, - boolean privacyMode) { - for (int i = 0; i < appWidgetIds.length; i++) { - // 只处理有效的小部件ID - if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { - // 默认背景颜色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()) { - // 检查是否有多个便签关联到同一小部件(异常情况) - 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 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 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); - } - } - } - - /** - * 抽象方法:获取背景资源ID - * 由子类实现不同尺寸小部件的背景资源 - */ - protected abstract int getBgResourceId(int bgId); - - /** - * 抽象方法:获取布局ID - * 由子类实现不同尺寸小部件的布局文件 - */ - protected abstract int getLayoutId(); - - /** - * 抽象方法:获取小部件类型 - * 由子类定义具体的小部件类型(如2x、4x等) - */ - protected abstract int getWidgetType(); -} \ No newline at end of file diff --git a/src/widget/NoteWidgetProvider_2x.java b/src/widget/NoteWidgetProvider_2x.java deleted file mode 100644 index f48b84b..0000000 --- a/src/widget/NoteWidgetProvider_2x.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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; - -/** - * 2x尺寸的便签桌面小部件实现类 - * 继承自NoteWidgetProvider,专注于2x尺寸的布局、背景和类型定义 - */ -public class NoteWidgetProvider_2x extends NoteWidgetProvider { - - /** - * 小部件更新回调方法 - * 当小部件需要更新时(如添加到桌面、系统触发更新)调用 - * @param context 上下文对象,提供应用环境信息 - * @param appWidgetManager 小部件管理器,用于更新小部件状态 - * @param appWidgetIds 需要更新的小部件ID数组 - */ - @Override - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - // 调用父类的update方法执行通用更新逻辑(如查询数据、绑定事件) - super.update(context, appWidgetManager, appWidgetIds); - } - - /** - * 实现抽象方法,返回2x尺寸小部件的布局文件ID - * 布局文件定义了小部件的UI结构(如文本位置、背景容器等) - * @return 2x小部件布局ID(R.layout.widget_2x) - */ - @Override - protected int getLayoutId() { - return R.layout.widget_2x; - } - - /** - * 实现抽象方法,返回2x尺寸小部件的背景资源ID - * 根据便签的背景色ID(bgId)获取对应的2x尺寸背景图 - * @param bgId 便签背景色ID(如红色、黄色等) - * @return 2x小部件对应的背景资源ID - */ - @Override - protected int getBgResourceId(int bgId) { - // 通过资源解析工具获取2x尺寸的背景资源 - return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); - } - - /** - * 实现抽象方法,返回2x尺寸小部件的类型标识 - * 用于系统区分不同尺寸的小部件,关联对应的数据和逻辑 - * @return 2x小部件类型标识(Notes.TYPE_WIDGET_2X) - */ - @Override - protected int getWidgetType() { - return Notes.TYPE_WIDGET_2X; - } -} \ No newline at end of file diff --git a/src/widget/NoteWidgetProvider_4x.java b/src/widget/NoteWidgetProvider_4x.java deleted file mode 100644 index 59bf459..0000000 --- a/src/widget/NoteWidgetProvider_4x.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 { - /** - * 当系统触发小部件更新时调用(如小部件添加到桌面、定时刷新) - * 调用父类的update方法,触发通用更新逻辑(查询便签数据、渲染视图等) - * @param context 应用上下文,用于访问资源和数据库 - * @param appWidgetManager 系统小部件管理器,负责更新小部件视图 - * @param appWidgetIds 需要更新的小部件ID数组 - */ - @Override - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - super.update(context, appWidgetManager, appWidgetIds); - } - - /** - * 提供4x尺寸小部件的布局文件ID - * 布局文件widget_4x.xml定义了该小部件的UI结构(如文本区域、背景容器等) - * @return 4x小部件的布局资源ID - */ - protected int getLayoutId() { - return R.layout.widget_4x; - } - - /** - * 根据背景ID(bgId)获取4x小部件对应的背景资源 - * 通过ResourceParser工具类解析,确保背景与便签设置的颜色一致 - * @param bgId 背景标识ID(如红色、蓝色等) - * @return 4x小部件的背景资源ID - */ - @Override - protected int getBgResourceId(int bgId) { - return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); - } - - /** - * 返回4x小部件的类型标识,用于父类逻辑中区分小部件尺寸 - * 与Notes类中定义的TYPE_WIDGET_4X常量对应,确保数据关联正确 - * @return 4x小部件的类型标识 - */ - @Override - protected int getWidgetType() { - return Notes.TYPE_WIDGET_4X; - } -} \ No newline at end of file