diff --git a/doc/卜文旭——技术博客.docx b/doc/卜文旭——技术博客.docx new file mode 100644 index 0000000..695cbdd Binary files /dev/null and b/doc/卜文旭——技术博客.docx differ diff --git a/doc/卜文旭——质量分析报告.docx b/doc/卜文旭——质量分析报告.docx new file mode 100644 index 0000000..504d218 Binary files /dev/null and b/doc/卜文旭——质量分析报告.docx differ diff --git a/src/NoteWidgetProvider.java b/src/NoteWidgetProvider.java new file mode 100644 index 0000000..9ed21f1 --- /dev/null +++ b/src/NoteWidgetProvider.java @@ -0,0 +1,187 @@ +/* + * 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; // 指定此类属于 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; // 导入必要的Android组件和业务逻辑接口,包括处理意图、远程视图、AppWidget管理等 + +/* + 抽象类,用于实现笔记小部件的功能。 + 提供了小部件的更新、删除等基础功能,具体的小部件布局和背景资源由子类实现。 + */ +public abstract class NoteWidgetProvider extends AppWidgetProvider { + /* + 数据库查询时需要投影的字段数组。 + 包括笔记的ID、背景颜色ID和片段。 + */ + public static final String[] PROJECTION = new String[]{ + NoteColumns.ID, // 笔记的ID + NoteColumns.BG_COLOR_ID, // 笔记的背景颜色ID + NoteColumns.SNIPPET // 笔记的片段 + }; + + /* + 定义字段在查询结果中的索引位置。 + */ + public static final int COLUMN_ID = 0; // ID字段的索引 + public static final int COLUMN_BG_COLOR_ID = 1; // 背景颜色ID字段的索引 + public static final int COLUMN_SNIPPET = 2; // 片段字段的索引 + + private static final String TAG = "NoteWidgetProvider"; // 日志标签 + + /* + 当小部件被删除时调用。 + 将与小部件关联的笔记的WIDGET_ID字段更新为无效值。 + @param context 上下文 + @param appWidgetIds 要删除的小部件ID数组 + */ + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + ContentValues values = new ContentValues(); // 创建ContentValues对象用于更新数据 + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); // 设置WIDGET_ID为无效值 + for (int i = 0; i < appWidgetIds.length; i++) { + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, // 更新笔记数据 + values, + NoteColumns.WIDGET_ID + "=?", // 更新条件:WIDGET_ID等于当前小部件ID + new String[]{String.valueOf(appWidgetIds[i])}); + } + } + + /* + 查询与指定小部件ID关联的笔记信息 + @param context 上下文 + @param widgetId 小部件ID + @return 查询结果的Cursor + */ + private Cursor getNoteWidgetInfo(Context context, int widgetId) { + return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, // 查询笔记数据 + PROJECTION, // 查询的字段 + NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", // 查询条件:WIDGET_ID等于widgetId且PARENT_ID不等于ID_TRASH_FOLDER + new String[]{String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER)}, // 查询条件的参数 + null); // 不指定排序 + } + + /* + 更新小部件。 + 如果未指定隐私模式,则直接调用update方法。 + @param context 上下文 + @param appWidgetManager 小部件管理器 + @param appWidgetIds 要更新的小部件ID数组 + */ + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + update(context, appWidgetManager, appWidgetIds, false); // 默认不启用隐私模式 + } + + /* + 更新小部件的核心逻辑。 + 根据小部件ID查询关联的笔记信息,并更新小部件的显示内容。 + @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++) { + if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { // 确保小部件ID有效 + int bgId = ResourceParser.getDefaultBgId(context); // 获取默认背景颜色ID + String snippet = ""; // 初始化笔记片段为空字符串 + Intent intent = new Intent(context, NoteEditActivity.class); // 创建编辑笔记的Intent + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 设置Intent标志 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); // 添加小部件ID到Intent + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); // 添加小部件类型到Intent + + 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); // 获取笔记背景颜色ID + intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); // 添加笔记ID到Intent + intent.setAction(Intent.ACTION_VIEW); // 设置Intent动作 + } else { + snippet = context.getResources().getString(R.string.widget_havenot_content); // 如果未查询到笔记信息,显示默认内容 + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // 设置Intent动作 + } + + if (c != null) { + c.close(); // 关闭Cursor + } + + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); // 创建RemoteViews对象 + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); // 设置小部件背景图片 + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); // 添加背景颜色ID到Intent + + /* + 生成点击小部件时启动的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); // 创建跳转到笔记列表的PendingIntent + } else { + rv.setTextViewText(R.id.widget_text, snippet); // 设置小部件文本为笔记片段 + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, // 创建跳转到笔记编辑的PendingIntent + PendingIntent.FLAG_UPDATE_CURRENT); + } + + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); // 设置小部件点击事件 + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); // 更新小部件 + } + } + } + + /* + 获取背景资源ID的抽象方法。 + 具体实现由子类提供。 + @param bgId 背景颜色ID + @return 背景资源ID + */ + protected abstract int getBgResourceId(int bgId); + + /* + 获取小部件布局ID的抽象方法。 + 具体实现由子类提供。 + @return 小部件布局ID + */ + protected abstract int getLayoutId(); + + /* + 获取小部件类型的抽象方法。 + 具体实现由子类提供。 + @return 小部件类型 + */ + protected abstract int getWidgetType(); +} \ No newline at end of file diff --git a/src/NoteWidgetProvider_2x.java b/src/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..1047d7e --- /dev/null +++ b/src/NoteWidgetProvider_2x.java @@ -0,0 +1,73 @@ +/* + * 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; // 包路径,表示该类属于 net.micode.notes.widget 包 + +import android.appwidget.AppWidgetManager; // 导入 AppWidgetManager,用于管理小部件 +import android.content.Context; // 导入 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 { + /* + 当小部件需要更新时调用的方法。 + 调用父类的 update 方法来更新小部件。 + @param context 上下文 + *param appWidgetManager 小部件管理器 + @param appWidgetIds 需要更新的小部件 ID 数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.update(context, appWidgetManager, appWidgetIds); // 调用父类的 update 方法 + } + + /* + 获取小部件的布局 ID。 + 返回 2x 大小的小部件布局资源 ID。 + @return 小部件布局资源 ID + */ + @Override + protected int getLayoutId() { + return R.layout.widget_2x; // 返回 2x 大小的小部件布局资源 ID + } + + /* + 获取小部件的背景资源 ID。 + 根据背景颜色 ID,返回对应的 2x 大小的小部件背景资源 ID。 + @param bgId 背景颜色 ID + @return 背景资源 ID + */ + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); // 调用工具类获取背景资源 ID + } + + /* + 获取小部件的类型。 + 返回 2x 大小的小部件类型。 + @return 小部件类型 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; // 返回 2x 大小的小部件类型 + } +} \ No newline at end of file diff --git a/src/NoteWidgetProvider_4x.java b/src/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..7642d1f --- /dev/null +++ b/src/NoteWidgetProvider_4x.java @@ -0,0 +1,77 @@ +/* + * 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; // 包路径,表示该类属于 net.micode.notes.widget 包 + +import android.appwidget.AppWidgetManager; // 导入 AppWidgetManager,用于管理小部件 +import android.content.Context; // 导入 Context,用于获取应用上下文 + +import net.micode.notes.R; // 导入资源文件 +import net.micode.notes.data.Notes; // 导入笔记数据相关的类 +import net.micode.notes.tool.ResourceParser; // 导入资源解析工具类 + +/* + NoteWidgetProvider_4x 类,用于实现 4x 大小的笔记小部件。 + 继承自 NoteWidgetProvider 抽象类,实现了具体的布局、背景资源和小部件类型。 + */ +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); // 调用父类的 update 方法 + } + + /* + 获取小部件的布局 ID。 + 返回 4x 大小的小部件布局资源 ID。 + + @return 小部件布局资源 ID + */ + @Override + protected int getLayoutId() { + return R.layout.widget_4x; // 返回 4x 大小的小部件布局资源 ID + } + + /* + 获取小部件的背景资源 ID。 + 根据背景颜色 ID,返回对应的 4x 大小的小部件背景资源 ID。 + + @param bgId 背景颜色 ID + @return 背景资源 ID + */ + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); // 调用工具类获取背景资源 ID + } + + /* + 获取小部件的类型。 + 返回 4x 大小的小部件类型。 + + @return 小部件类型 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; // 返回 4x 大小的小部件类型 + } +} \ No newline at end of file