diff --git a/doc/李自强技术博客.docx b/doc/李自强技术博客.docx new file mode 100644 index 0000000..f7a6b1b Binary files /dev/null and b/doc/李自强技术博客.docx differ diff --git a/doc/质量分析报告lzq.docx b/doc/质量分析报告lzq.docx new file mode 100644 index 0000000..aebae10 Binary files /dev/null and b/doc/质量分析报告lzq.docx differ diff --git a/doc/质量检测报告.docx b/doc/质量检测报告.docx deleted file mode 100644 index 83ff6e4..0000000 Binary files a/doc/质量检测报告.docx and /dev/null differ diff --git a/doc/赵皓阳 技术博客.docx b/doc/赵皓阳 技术博客.docx deleted file mode 100644 index db1e623..0000000 Binary files a/doc/赵皓阳 技术博客.docx and /dev/null differ diff --git a/src/Note.java b/src/Note.java new file mode 100644 index 0000000..a31e11c --- /dev/null +++ b/src/Note.java @@ -0,0 +1,347 @@ +/* + * 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.model; +import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.content.OperationApplicationException; +import android.net.Uri; +import android.os.RemoteException; +import android.util.Log; + +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.CallNote; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.data.Notes.TextNote; + +import java.util.ArrayList; + +/** + * Note 类用于管理笔记的创建、修改和同步操作 + * 包含笔记基本信息和相关数据(文本内容、通话记录等) + */ +public class Note { + private ContentValues mNoteDiffValues; // 存储笔记基本信息的变更值 + private NoteData mNoteData; // 存储笔记关联的数据(文本、通话记录等) + private static final String TAG = "Note"; + + /** + * 创建新笔记并返回其ID + * @param context 应用上下文 + * @param folderId 父文件夹ID + * @return 新创建的笔记ID + */ + public static synchronized long getNewNoteId(Context context, long folderId) { + // 创建新笔记并设置初始值 + ContentValues values = new ContentValues(); + long createdTime = System.currentTimeMillis(); + values.put(NoteColumns.CREATED_DATE, createdTime); // 设置创建时间 + values.put(NoteColumns.MODIFIED_DATE, createdTime); // 设置修改时间 + values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 设置笔记类型 + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 + values.put(NoteColumns.PARENT_ID, folderId); // 设置父文件夹ID + + // 插入新笔记到内容提供者 + Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); + + long noteId = 0; + try { + // 从返回的URI中解析笔记ID + noteId = Long.valueOf(uri.getPathSegments().get(1)); + } catch (NumberFormatException e) { + Log.e(TAG, "获取笔记ID错误: " + e.toString()); + noteId = 0; + } + if (noteId == -1) { + throw new IllegalStateException("错误的笔记ID: " + noteId); + } + return noteId; + } + + /** + * 构造函数,初始化笔记对象 + */ + public Note() { + mNoteDiffValues = new ContentValues(); // 初始化笔记变更值容器 + mNoteData = new NoteData(); // 初始化笔记数据容器 + } + + /** + * 设置笔记基本信息的值 + * @param key 键名 + * @param value 值 + */ + public void setNoteValue(String key, String value) { + mNoteDiffValues.put(key, value); + // 标记笔记已修改,并更新修改时间 + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + + /** + * 设置文本数据的值 + * @param key 键名 + * @param value 值 + */ + public void setTextData(String key, String value) { + mNoteData.setTextData(key, value); + } + + /** + * 设置文本数据ID + * @param id 文本数据ID + */ + public void setTextDataId(long id) { + mNoteData.setTextDataId(id); + } + + /** + * 获取文本数据ID + * @return 文本数据ID + */ + public long getTextDataId() { + return mNoteData.mTextDataId; + } + + /** + * 设置通话数据ID + * @param id 通话数据ID + */ + public void setCallDataId(long id) { + mNoteData.setCallDataId(id); + } + + /** + * 设置通话数据的值 + * @param key 键名 + * @param value 值 + */ + public void setCallData(String key, String value) { + mNoteData.setCallData(key, value); + } + + /** + * 检查笔记是否有本地修改 + * @return 如果有修改返回true,否则返回false + */ + public boolean isLocalModified() { + return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); + } + + /** + * 将笔记同步到内容提供者 + * @param context 应用上下文 + * @param noteId 笔记ID + * @return 同步成功返回true,失败返回false + */ + public boolean syncNote(Context context, long noteId) { + if (noteId <= 0) { + throw new IllegalArgumentException("错误的笔记ID: " + noteId); + } + + if (!isLocalModified()) { + return true; // 没有修改,无需同步 + } + + /** + * 理论上,一旦数据发生变化,笔记应在{@link NoteColumns#LOCAL_MODIFIED}和 + * {@link NoteColumns#MODIFIED_DATE}上更新。为了数据安全,即使更新笔记失败, + * 我们也会更新笔记数据信息 + */ + if (context.getContentResolver().update( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, + null) == 0) { + Log.e(TAG, "更新笔记错误,这种情况不应该发生"); + // 不返回,继续执行 + } + mNoteDiffValues.clear(); // 清除已同步的变更值 + + // 同步笔记数据,如果有修改且同步失败则返回false + if (mNoteData.isLocalModified() + && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { + return false; + } + + return true; + } + + /** + * 内部类,用于管理笔记关联的数据(文本内容、通话记录等) + */ + private class NoteData { + private long mTextDataId; // 文本数据ID + private ContentValues mTextDataValues; // 文本数据变更值 + private long mCallDataId; // 通话数据ID + private ContentValues mCallDataValues; // 通话数据变更值 + private static final String TAG = "NoteData"; + + /** + * 构造函数,初始化笔记数据对象 + */ + public NoteData() { + mTextDataValues = new ContentValues(); // 初始化文本数据容器 + mCallDataValues = new ContentValues(); // 初始化通话数据容器 + mTextDataId = 0; // 初始文本数据ID为0 + mCallDataId = 0; // 初始通话数据ID为0 + } + + /** + * 检查笔记数据是否有本地修改 + * @return 如果有修改返回true,否则返回false + */ + boolean isLocalModified() { + return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; + } + + /** + * 设置文本数据ID + * @param id 文本数据ID + */ + void setTextDataId(long id) { + if(id <= 0) { + throw new IllegalArgumentException("文本数据ID应大于0"); + } + mTextDataId = id; + } + + /** + * 设置通话数据ID + * @param id 通话数据ID + */ + void setCallDataId(long id) { + if (id <= 0) { + throw new IllegalArgumentException("通话数据ID应大于0"); + } + mCallDataId = id; + } + + /** + * 设置通话数据的值 + * @param key 键名 + * @param value 值 + */ + void setCallData(String key, String value) { + mCallDataValues.put(key, value); + // 标记笔记已修改,并更新修改时间 + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + + /** + * 设置文本数据的值 + * @param key 键名 + * @param value 值 + */ + void setTextData(String key, String value) { + mTextDataValues.put(key, value); + // 标记笔记已修改,并更新修改时间 + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + + /** + * 将笔记数据推送到内容提供者 + * @param context 应用上下文 + * @param noteId 笔记ID + * @return 成功返回笔记URI,失败返回null + */ + Uri pushIntoContentResolver(Context context, long noteId) { + /** + * 安全检查 + */ + if (noteId <= 0) { + throw new IllegalArgumentException("错误的笔记ID: " + noteId); + } + + // 创建批量操作列表 + ArrayList operationList = new ArrayList(); + ContentProviderOperation.Builder builder = null; + + // 处理文本数据变更 + if(mTextDataValues.size() > 0) { + mTextDataValues.put(DataColumns.NOTE_ID, noteId); // 设置关联的笔记ID + if (mTextDataId == 0) { + // 新增文本数据 + mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mTextDataValues); + try { + // 从返回的URI中获取并设置新的文本数据ID + setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); + } catch (NumberFormatException e) { + Log.e(TAG, "插入新文本数据失败,笔记ID: " + noteId); + mTextDataValues.clear(); + return null; + } + } else { + // 更新现有文本数据 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mTextDataId)); + builder.withValues(mTextDataValues); + operationList.add(builder.build()); + } + mTextDataValues.clear(); // 清除已处理的变更值 + } + + // 处理通话数据变更 + if(mCallDataValues.size() > 0) { + mCallDataValues.put(DataColumns.NOTE_ID, noteId); // 设置关联的笔记ID + if (mCallDataId == 0) { + // 新增通话数据 + mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mCallDataValues); + try { + // 从返回的URI中获取并设置新的通话数据ID + setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); + } catch (NumberFormatException e) { + Log.e(TAG, "插入新通话数据失败,笔记ID: " + noteId); + mCallDataValues.clear(); + return null; + } + } else { + // 更新现有通话数据 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mCallDataId)); + builder.withValues(mCallDataValues); + operationList.add(builder.build()); + } + mCallDataValues.clear(); // 清除已处理的变更值 + } + + // 执行批量操作 + if (operationList.size() > 0) { + try { + ContentProviderResult[] results = context.getContentResolver().applyBatch( + Notes.AUTHORITY, operationList); + return (results == null || results.length == 0 || results[0] == null) ? null + : ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); + } catch (RemoteException e) { + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + return null; + } catch (OperationApplicationException e) { + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + return null; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/src/NoteWidgetProvider.java b/src/NoteWidgetProvider.java deleted file mode 100644 index 9ed21f1..0000000 --- a/src/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; // 指定此类属于 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 deleted file mode 100644 index 1047d7e..0000000 --- a/src/NoteWidgetProvider_2x.java +++ /dev/null @@ -1,73 +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; // 包路径,表示该类属于 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 deleted file mode 100644 index 7642d1f..0000000 --- a/src/NoteWidgetProvider_4x.java +++ /dev/null @@ -1,77 +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; // 包路径,表示该类属于 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 diff --git a/src/WorkingNote.java b/src/WorkingNote.java new file mode 100644 index 0000000..dc9855b --- /dev/null +++ b/src/WorkingNote.java @@ -0,0 +1,512 @@ +/* + * 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.model; + +import android.appwidget.AppWidgetManager; +import android.content.ContentUris; +import android.content.Context; +import android.database.Cursor; +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.CallNote; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.data.Notes.TextNote; +import net.micode.notes.tool.ResourceParser.NoteBgResources; + +/** + * WorkingNote 类用于管理笔记的创建、加载、保存和修改操作 + * 提供了笔记的各种属性设置和获取方法,并支持与界面的交互回调 + */ +public class WorkingNote { + // 笔记核心操作类 + private Note mNote; + // 笔记ID + private long mNoteId; + // 笔记内容 + private String mContent; + // 笔记模式(普通文本或待办列表) + private int mMode; + // 提醒日期 + private long mAlertDate; + // 修改日期 + private long mModifiedDate; + // 背景颜色ID + private int mBgColorId; + // 桌面小部件ID + private int mWidgetId; + // 桌面小部件类型 + private int mWidgetType; + // 文件夹ID + private long mFolderId; + // 应用上下文 + private Context mContext; + // 日志标签 + private static final String TAG = "WorkingNote"; + // 标记是否已删除 + private boolean mIsDeleted; + // 笔记设置变更监听器 + private NoteSettingChangedListener mNoteSettingStatusListener; + + // 数据查询投影(用于查询笔记相关数据) + public static final String[] DATA_PROJECTION = new String[] { + DataColumns.ID, // 数据ID + DataColumns.CONTENT, // 内容 + DataColumns.MIME_TYPE, // 内容类型 + DataColumns.DATA1, // 数据1(通常用于存储模式) + DataColumns.DATA2, // 数据2 + DataColumns.DATA3, // 数据3 + DataColumns.DATA4, // 数据4 + }; + + // 笔记查询投影(用于查询笔记基本信息) + public static final String[] NOTE_PROJECTION = new String[] { + NoteColumns.PARENT_ID, // 父文件夹ID + NoteColumns.ALERTED_DATE, // 提醒日期 + NoteColumns.BG_COLOR_ID, // 背景颜色ID + NoteColumns.WIDGET_ID, // 桌面小部件ID + NoteColumns.WIDGET_TYPE, // 桌面小部件类型 + NoteColumns.MODIFIED_DATE // 修改日期 + }; + + // 数据查询列索引 + private static final int DATA_ID_COLUMN = 0; + private static final int DATA_CONTENT_COLUMN = 1; + private static final int DATA_MIME_TYPE_COLUMN = 2; + private static final int DATA_MODE_COLUMN = 3; + + // 笔记查询列索引 + private static final int NOTE_PARENT_ID_COLUMN = 0; + private static final int NOTE_ALERTED_DATE_COLUMN = 1; + private static final int NOTE_BG_COLOR_ID_COLUMN = 2; + private static final int NOTE_WIDGET_ID_COLUMN = 3; + private static final int NOTE_WIDGET_TYPE_COLUMN = 4; + private static final int NOTE_MODIFIED_DATE_COLUMN = 5; + + /** + * 私有构造函数,用于创建新笔记 + * @param context 应用上下文 + * @param folderId 父文件夹ID + */ + private WorkingNote(Context context, long folderId) { + mContext = context; + mAlertDate = 0; // 默认无提醒 + mModifiedDate = System.currentTimeMillis(); // 设置当前时间为修改时间 + mFolderId = folderId; // 设置父文件夹ID + mNote = new Note(); // 初始化笔记操作类 + mNoteId = 0; // 新笔记ID为0,表示尚未保存到数据库 + mIsDeleted = false; // 默认未删除 + mMode = 0; // 默认普通文本模式 + mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 默认无效小部件类型 + } + + /** + * 私有构造函数,用于加载已存在的笔记 + * @param context 应用上下文 + * @param noteId 笔记ID + * @param folderId 父文件夹ID + */ + private WorkingNote(Context context, long noteId, long folderId) { + mContext = context; + mNoteId = noteId; // 设置笔记ID + mFolderId = folderId; // 设置父文件夹ID + mIsDeleted = false; // 默认未删除 + mNote = new Note(); // 初始化笔记操作类 + loadNote(); // 从数据库加载笔记信息 + } + + /** + * 从数据库加载笔记基本信息 + */ + private void loadNote() { + // 查询笔记基本信息 + Cursor cursor = mContext.getContentResolver().query( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, + null, null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + // 从游标中获取并设置笔记各属性 + mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); + mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); + mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); + mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); + mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); + mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); + } + cursor.close(); + } else { + Log.e(TAG, "No note with id:" + mNoteId); + throw new IllegalArgumentException("Unable to find note with id " + mNoteId); + } + loadNoteData(); // 加载笔记关联的数据 + } + + /** + * 从数据库加载笔记关联的数据(如文本内容、通话记录等) + */ + private void loadNoteData() { + // 查询笔记关联的数据 + Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, + DataColumns.NOTE_ID + "=?", new String[] { + String.valueOf(mNoteId) + }, null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + // 根据数据类型处理不同的数据 + String type = cursor.getString(DATA_MIME_TYPE_COLUMN); + if (DataConstants.NOTE.equals(type)) { + // 文本笔记数据 + mContent = cursor.getString(DATA_CONTENT_COLUMN); + mMode = cursor.getInt(DATA_MODE_COLUMN); + mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); + } else if (DataConstants.CALL_NOTE.equals(type)) { + // 通话记录数据 + mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); + } else { + Log.d(TAG, "Wrong note type with type:" + type); + } + } while (cursor.moveToNext()); + } + cursor.close(); + } else { + Log.e(TAG, "No data with id:" + mNoteId); + throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); + } + } + + /** + * 创建新的空笔记 + * @param context 应用上下文 + * @param folderId 父文件夹ID + * @param widgetId 桌面小部件ID + * @param widgetType 桌面小部件类型 + * @param defaultBgColorId 默认背景颜色ID + * @return 新创建的WorkingNote对象 + */ + public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, + int widgetType, int defaultBgColorId) { + WorkingNote note = new WorkingNote(context, folderId); + note.setBgColorId(defaultBgColorId); // 设置默认背景颜色 + note.setWidgetId(widgetId); // 设置小部件ID + note.setWidgetType(widgetType); // 设置小部件类型 + return note; + } + + /** + * 从数据库加载现有笔记 + * @param context 应用上下文 + * @param id 笔记ID + * @return 加载的WorkingNote对象 + */ + public static WorkingNote load(Context context, long id) { + return new WorkingNote(context, id, 0); + } + + /** + * 同步保存笔记到数据库 + * @return 保存成功返回true,失败返回false + */ + public synchronized boolean saveNote() { + if (isWorthSaving()) { // 检查是否值得保存 + if (!existInDatabase()) { // 如果笔记还不存在于数据库中 + if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { + Log.e(TAG, "Create new note fail with id:" + mNoteId); + return false; + } + } + + // 同步笔记到数据库 + mNote.syncNote(mContext, mNoteId); + + /** + * 如果笔记有相关联的桌面小部件,更新小部件内容 + */ + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE + && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); + } + return true; + } else { + return false; + } + } + + /** + * 检查笔记是否已存在于数据库中 + * @return 存在返回true,否则返回false + */ + public boolean existInDatabase() { + return mNoteId > 0; + } + + /** + * 判断笔记是否值得保存 + * @return 值得保存返回true,否则返回false + */ + private boolean isWorthSaving() { + if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) + || (existInDatabase() && !mNote.isLocalModified())) { + return false; // 已删除、新笔记无内容或无修改的笔记不值得保存 + } else { + return true; + } + } + + /** + * 设置笔记设置变更监听器 + * @param l 监听器对象 + */ + public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { + mNoteSettingStatusListener = l; + } + + /** + * 设置提醒日期 + * @param date 提醒日期时间戳 + * @param set 是否设置提醒 + */ + public void setAlertDate(long date, boolean set) { + if (date != mAlertDate) { + mAlertDate = date; + mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); + } + if (mNoteSettingStatusListener != null) { + // 通知监听器提醒设置已变更 + mNoteSettingStatusListener.onClockAlertChanged(date, set); + } + } + + /** + * 标记笔记为已删除或未删除 + * @param mark true表示已删除,false表示未删除 + */ + public void markDeleted(boolean mark) { + mIsDeleted = mark; + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + // 通知监听器小部件已变更 + mNoteSettingStatusListener.onWidgetChanged(); + } + } + + /** + * 设置笔记背景颜色 + * @param id 背景颜色ID + */ + public void setBgColorId(int id) { + if (id != mBgColorId) { + mBgColorId = id; + if (mNoteSettingStatusListener != null) { + // 通知监听器背景颜色已变更 + mNoteSettingStatusListener.onBackgroundColorChanged(); + } + mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); + } + } + + /** + * 设置笔记模式(普通文本或待办列表) + * @param mode 模式值 + */ + public void setCheckListMode(int mode) { + if (mMode != mode) { + if (mNoteSettingStatusListener != null) { + // 通知监听器模式已变更 + mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); + } + mMode = mode; + mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); + } + } + + /** + * 设置桌面小部件类型 + * @param type 小部件类型 + */ + public void setWidgetType(int type) { + if (type != mWidgetType) { + mWidgetType = type; + mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); + } + } + + /** + * 设置桌面小部件ID + * @param id 小部件ID + */ + public void setWidgetId(int id) { + if (id != mWidgetId) { + mWidgetId = id; + mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); + } + } + + /** + * 设置笔记文本内容 + * @param text 文本内容 + */ + public void setWorkingText(String text) { + if (!TextUtils.equals(mContent, text)) { + mContent = text; + mNote.setTextData(DataColumns.CONTENT, mContent); + } + } + + /** + * 将笔记转换为通话记录笔记 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + */ + public void convertToCallNote(String phoneNumber, long callDate) { + mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); + mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); + mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); + } + + /** + * 检查笔记是否设置了提醒 + * @return 设置了提醒返回true,否则返回false + */ + public boolean hasClockAlert() { + return (mAlertDate > 0 ? true : false); + } + + /** + * 获取笔记内容 + * @return 笔记内容字符串 + */ + public String getContent() { + return mContent; + } + + /** + * 获取提醒日期 + * @return 提醒日期时间戳 + */ + public long getAlertDate() { + return mAlertDate; + } + + /** + * 获取修改日期 + * @return 修改日期时间戳 + */ + public long getModifiedDate() { + return mModifiedDate; + } + + /** + * 获取背景颜色资源ID + * @return 背景颜色资源ID + */ + public int getBgColorResId() { + return NoteBgResources.getNoteBgResource(mBgColorId); + } + + /** + * 获取背景颜色ID + * @return 背景颜色ID + */ + public int getBgColorId() { + return mBgColorId; + } + + /** + * 获取标题背景资源ID + * @return 标题背景资源ID + */ + public int getTitleBgResId() { + return NoteBgResources.getNoteTitleBgResource(mBgColorId); + } + + /** + * 获取笔记模式 + * @return 笔记模式值 + */ + public int getCheckListMode() { + return mMode; + } + + /** + * 获取笔记ID + * @return 笔记ID + */ + public long getNoteId() { + return mNoteId; + } + + /** + * 获取父文件夹ID + * @return 父文件夹ID + */ + public long getFolderId() { + return mFolderId; + } + + /** + * 获取桌面小部件ID + * @return 桌面小部件ID + */ + public int getWidgetId() { + return mWidgetId; + } + + /** + * 获取桌面小部件类型 + * @return 桌面小部件类型 + */ + public int getWidgetType() { + return mWidgetType; + } + + /** + * 笔记设置变更监听器接口 + */ + public interface NoteSettingChangedListener { + /** + * 当笔记背景颜色变更时调用 + */ + void onBackgroundColorChanged(); + + /** + * 当用户设置/取消提醒时调用 + * @param date 提醒日期 + * @param set 是否设置提醒 + */ + void onClockAlertChanged(long date, boolean set); + + /** + * 当从桌面小部件创建笔记时调用 + */ + void onWidgetChanged(); + + /** + * 当在待办列表模式和普通文本模式之间切换时调用 + * @param oldMode 变更前的模式 + * @param newMode 变更后的模式 + */ + void onCheckListModeChanged(int oldMode, int newMode); + } +} +