diff --git a/doc/软件设计规格说明书.doc b/doc/软件设计规格说明书.doc new file mode 100644 index 0000000..651d4e2 Binary files /dev/null and b/doc/软件设计规格说明书.doc differ diff --git a/doc/软件需求构思及描述.docx b/doc/软件需求构思及描述.docx new file mode 100644 index 0000000..6b64f33 Binary files /dev/null and b/doc/软件需求构思及描述.docx differ diff --git a/src/Model/Note.java b/src/Model/Note.java new file mode 100644 index 0000000..3256518 --- /dev/null +++ b/src/Model/Note.java @@ -0,0 +1,390 @@ +/* + * 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; + +/** + * 该类表示一个笔记对象,用于管理笔记的各种属性和数据, + * 包括笔记的基本信息(如创建时间、修改时间等)以及笔记的文本数据和通话数据。 + */ +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 对象,用于存储要插入数据库的笔记信息 + ContentValues values = new ContentValues(); + // 获取当前时间作为笔记的创建时间 + long createdTime = System.currentTimeMillis(); + // 将创建时间添加到 ContentValues 中 + 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); + // 设置笔记所属文件夹的 ID + values.put(NoteColumns.PARENT_ID, folderId); + // 使用内容解析器插入新笔记,并获取插入后的 URI + 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) { + // 若提取 ID 时出现异常,记录错误日志 + Log.e(TAG, "Get note id error :" + e.toString()); + noteId = 0; + } + if (noteId == -1) { + // 若笔记 ID 为 -1,抛出异常表示笔记 ID 错误 + throw new IllegalStateException("Wrong note 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) { + // 若笔记 ID 不合法,抛出异常 + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + if (!isLocalModified()) { + // 若笔记未被修改,直接返回 true + return true; + } + + /** + * 理论上,一旦数据发生变化,笔记的 LOCAL_MODIFIED 和 MODIFIED_DATE 应该被更新。 + * 为了数据安全,即使更新笔记失败,我们也会更新笔记的数据信息。 + */ + if (context.getContentResolver().update( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, + null) == 0) { + // 若更新笔记失败,记录错误日志 + Log.e(TAG, "Update note error, should not happen"); + // 不返回,继续执行后续操作 + } + // 清空笔记的差异值 + mNoteDiffValues.clear(); + + if (mNoteData.isLocalModified() + && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { + // 若笔记数据被修改且同步失败,返回 false + return false; + } + + return true; + } + + /** + * 内部类,用于管理笔记的数据,包括文本数据和通话数据。 + */ + private class NoteData { + // 文本数据的 ID + private long mTextDataId; + // 存储文本数据的 ContentValues 对象 + private ContentValues mTextDataValues; + // 通话数据的 ID + private long mCallDataId; + // 存储通话数据的 ContentValues 对象 + private ContentValues mCallDataValues; + // 日志标签,用于在日志中标识该内部类的信息 + private static final String TAG = "NoteData"; + + /** + * 构造函数,初始化文本数据和通话数据的 ContentValues 对象,以及 ID 为 0。 + */ + public NoteData() { + mTextDataValues = new ContentValues(); + mCallDataValues = new ContentValues(); + mTextDataId = 0; + mCallDataId = 0; + } + + /** + * 判断笔记数据是否被本地修改过。 + * + * @return 若笔记数据被本地修改过返回 true,否则返回 false + */ + boolean isLocalModified() { + return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; + } + + /** + * 设置笔记的文本数据的 ID。 + * + * @param id 文本数据的 ID + */ + void setTextDataId(long id) { + if(id <= 0) { + // 若文本数据 ID 不合法,抛出异常 + throw new IllegalArgumentException("Text data id should larger than 0"); + } + mTextDataId = id; + } + + /** + * 设置笔记的通话数据的 ID。 + * + * @param id 通话数据的 ID + */ + void setCallDataId(long id) { + if (id <= 0) { + // 若通话数据 ID 不合法,抛出异常 + throw new IllegalArgumentException("Call data id should larger than 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) { + /** + * 进行安全检查,确保笔记 ID 合法。 + */ + if (noteId <= 0) { + // 若笔记 ID 不合法,抛出异常 + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + // 创建一个 ContentProviderOperation 列表,用于批量操作 + ArrayList operationList = new ArrayList(); + // ContentProviderOperation 构建器 + ContentProviderOperation.Builder builder = null; + + if(mTextDataValues.size() > 0) { + // 若文本数据有修改 + mTextDataValues.put(DataColumns.NOTE_ID, noteId); + if (mTextDataId == 0) { + // 若文本数据 ID 为 0,说明是新的文本数据 + mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); + // 插入新的文本数据 + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mTextDataValues); + try { + // 获取插入后的文本数据 ID + setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); + } catch (NumberFormatException e) { + // 若获取 ID 失败,记录错误日志并清空文本数据 + Log.e(TAG, "Insert new text data fail with noteId" + noteId); + mTextDataValues.clear(); + return null; + } + } else { + // 若文本数据 ID 不为 0,说明是更新已有的文本数据 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mTextDataId)); + builder.withValues(mTextDataValues); + operationList.add(builder.build()); + } + // 清空文本数据的 ContentValues 对象 + mTextDataValues.clear(); + } + + if(mCallDataValues.size() > 0) { + // 若通话数据有修改 + mCallDataValues.put(DataColumns.NOTE_ID, noteId); + if (mCallDataId == 0) { + // 若通话数据 ID 为 0,说明是新的通话数据 + mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); + // 插入新的通话数据 + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mCallDataValues); + try { + // 获取插入后的通话数据 ID + setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); + } catch (NumberFormatException e) { + // 若获取 ID 失败,记录错误日志并清空通话数据 + Log.e(TAG, "Insert new call data fail with noteId" + noteId); + mCallDataValues.clear(); + return null; + } + } else { + // 若通话数据 ID 不为 0,说明是更新已有的通话数据 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mCallDataId)); + builder.withValues(mCallDataValues); + operationList.add(builder.build()); + } + // 清空通话数据的 ContentValues 对象 + mCallDataValues.clear(); + } + + if (operationList.size() > 0) { + try { + // 批量执行 ContentProviderOperation 列表中的操作 + 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/Model/WorkingNote.java b/src/Model/WorkingNote.java new file mode 100644 index 0000000..c825e1c --- /dev/null +++ b/src/Model/WorkingNote.java @@ -0,0 +1,523 @@ +/* + * 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, + DataColumns.CONTENT, + DataColumns.MIME_TYPE, + DataColumns.DATA1, + DataColumns.DATA2, + DataColumns.DATA3, + DataColumns.DATA4, + }; + + // 便签查询的投影列,用于从数据库中查询便签信息 + public static final String[] NOTE_PROJECTION = new String[] { + NoteColumns.PARENT_ID, + NoteColumns.ALERTED_DATE, + NoteColumns.BG_COLOR_ID, + NoteColumns.WIDGET_ID, + NoteColumns.WIDGET_TYPE, + NoteColumns.MODIFIED_DATE + }; + + // 数据 ID 列的索引 + private static final int DATA_ID_COLUMN = 0; + // 数据内容列的索引 + private static final int DATA_CONTENT_COLUMN = 1; + // 数据 MIME 类型列的索引 + private static final int DATA_MIME_TYPE_COLUMN = 2; + // 数据模式列的索引 + private static final int DATA_MODE_COLUMN = 3; + // 便签父文件夹 ID 列的索引 + private static final int NOTE_PARENT_ID_COLUMN = 0; + // 便签提醒日期列的索引 + private static final int NOTE_ALERTED_DATE_COLUMN = 1; + // 便签背景颜色 ID 列的索引 + private static final int NOTE_BG_COLOR_ID_COLUMN = 2; + // 便签小部件 ID 列的索引 + 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; + // 初始化提醒日期为 0,表示没有提醒 + mAlertDate = 0; + // 初始化修改日期为当前时间 + mModifiedDate = System.currentTimeMillis(); + mFolderId = folderId; + mNote = new Note(); + // 初始化便签 ID 为 0,表示新便签还未分配 ID + mNoteId = 0; + // 标记便签未被删除 + mIsDeleted = false; + // 初始化便签模式为 0 + 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; + mFolderId = folderId; + // 标记便签未被删除 + mIsDeleted = false; + mNote = new Note(); + // 加载便签信息 + loadNote(); + } + + /** + * 加载便签的基本信息,包括父文件夹 ID、背景颜色 ID、小部件 ID 等。 + */ + 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()) { + // 获取便签所在文件夹的 ID + mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); + // 获取便签的背景颜色 ID + mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); + // 获取便签小部件的 ID + 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); + // 抛出异常,表示找不到指定 ID 的便签 + 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 { + // 获取数据的 MIME 类型 + 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); + // 设置便签的文本数据 ID + mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); + } else if (DataConstants.CALL_NOTE.equals(type)) { + // 如果是通话便签类型,设置通话数据 ID + 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); + // 抛出异常,表示找不到指定 ID 的便签数据 + 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 新创建的便签对象 + */ + public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, + int widgetType, int defaultBgColorId) { + // 创建一个新的便签对象 + WorkingNote note = new WorkingNote(context, folderId); + // 设置便签的背景颜色 ID + note.setBgColorId(defaultBgColorId); + // 设置便签小部件的 ID + note.setWidgetId(widgetId); + // 设置便签小部件的类型 + note.setWidgetType(widgetType); + return note; + } + + /** + * 加载指定 ID 的便签。 + * + * @param context 上下文对象 + * @param id 便签的 ID + * @return 加载的便签对象 + */ + 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()) { + // 如果便签不在数据库中,创建一个新的便签 ID + 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 表示标记为已删除,否则为未删除 + */ + public void markDeleted(boolean mark) { + mIsDeleted = mark; + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + // 通知监听器小部件内容发生变化 + mNoteSettingStatusListener.onWidgetChanged(); + } + } + + /** + * 设置便签的背景颜色 ID。 + * + * @param id 背景颜色 ID + */ + public void setBgColorId(int id) { + if (id != mBgColorId) { + mBgColorId = id; + if (mNoteSettingStatusListener != null) { + // 通知监听器背景颜色发生变化 + mNoteSettingStatusListener.onBackgroundColorChanged(); + } + // 设置便签的背景颜色 ID + 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; + // 设置便签的小部件 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); + // 设置便签的父文件夹 ID 为通话记录文件夹的 ID + mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); + } + + /** + * 判断便签是否设置了提醒。 + * + * @return 如果设置了提醒返回 true,否则返回 false + */ + public boolean hasClockAlert() { + return (mAlertDate > 0 ? true : false); + } + + public String getContent() { + return mContent; + } + + public long getAlertDate() { + return mAlertDate; + } + + public long getModifiedDate() { + return mModifiedDate; + } + + public int getBgColorResId() { + return NoteBgResources.getNoteBgResource(mBgColorId); + } + + public int getBgColorId() { + return mBgColorId; + } + + public int getTitleBgResId() { + return NoteBgResources.getNoteTitleBgResource(mBgColorId); + } + + public int getCheckListMode() { + return mMode; + } + + public long getNoteId() { + return mNoteId; + } + + public long getFolderId() { + return mFolderId; + } + + public int getWidgetId() { + return mWidgetId; + } + + public int getWidgetType() { + return mWidgetType; + } + + public interface NoteSettingChangedListener { + /** + * Called when the background color of current note has just changed + */ + void onBackgroundColorChanged(); + + /** + * Called when user set clock + */ + void onClockAlertChanged(long date, boolean set); + + /** + * Call when user create note from widget + */ + void onWidgetChanged(); + + /** + * Call when switch between check list mode and normal mode + * @param oldMode is previous mode before change + * @param newMode is new mode + */ + void onCheckListModeChanged(int oldMode, int newMode); + } +} diff --git a/src/Widget/NoteWidgetProvider.java b/src/Widget/NoteWidgetProvider.java new file mode 100644 index 0000000..bac6ff6 --- /dev/null +++ b/src/Widget/NoteWidgetProvider.java @@ -0,0 +1,226 @@ +/* + * 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; + +/** + * 抽象类 NoteWidgetProvider,继承自 AppWidgetProvider,用于处理笔记小部件的相关操作。 + * 提供了小部件删除、更新等功能,并且定义了一些抽象方法供子类实现特定的资源获取。 + */ +public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 查询笔记信息时使用的投影,指定要查询的列 + public static final String [] PROJECTION = new String [] { + NoteColumns.ID, + NoteColumns.BG_COLOR_ID, + NoteColumns.SNIPPET + }; + + // 笔记 ID 在投影数组中的索引 + public static final int COLUMN_ID = 0; + // 笔记背景颜色 ID 在投影数组中的索引 + public static final int COLUMN_BG_COLOR_ID = 1; + // 笔记摘要在投影数组中的索引 + public static final int COLUMN_SNIPPET = 2; + + // 日志标签,用于在日志中标识该类的相关信息 + private static final String TAG = "NoteWidgetProvider"; + + /** + * 当小部件被删除时调用此方法。 + * 该方法会将对应笔记的小部件 ID 设置为无效值。 + * + * @param context 上下文对象 + * @param appWidgetIds 被删除的小部件 ID 数组 + */ + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + // 创建 ContentValues 对象,用于存储要更新的字段和值 + ContentValues values = new ContentValues(); + // 将笔记的小部件 ID 设置为无效值 + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + // 遍历被删除的小部件 ID 数组 + for (int i = 0; i < appWidgetIds.length; i++) { + // 更新笔记数据库中对应小部件 ID 的笔记记录 + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", + new String[] { String.valueOf(appWidgetIds[i])}); + } + } + + /** + * 根据小部件 ID 获取笔记小部件的信息。 + * + * @param context 上下文对象 + * @param widgetId 小部件 ID + * @return 包含笔记信息的游标,如果查询失败则返回 null + */ + private Cursor getNoteWidgetInfo(Context context, int widgetId) { + // 查询笔记数据库,获取指定小部件 ID 且不在回收站的笔记信息 + 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); + } + + /** + * 更新小部件的方法,默认不开启隐私模式。 + * + * @param context 上下文对象 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 要更新的小部件 ID 数组 + */ + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用带隐私模式参数的更新方法,隐私模式为 false + 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) { + // 遍历要更新的小部件 ID 数组 + 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); + // 将小部件 ID 作为额外数据添加到意图中 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); + // 将小部件类型作为额外数据添加到意图中 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); + + // 获取指定小部件 ID 的笔记信息游标 + 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); + // 从游标中获取笔记背景颜色 ID + bgId = c.getInt(COLUMN_BG_COLOR_ID); + // 将笔记 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 对象,用于设置小部件的布局 + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); + // 设置小部件的背景图片资源 + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); + // 将背景颜色 ID 作为额外数据添加到意图中 + 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); + // 更新指定小部件 ID 的小部件布局 + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); + } + } + } + + /** + * 抽象方法,用于获取指定背景颜色 ID 对应的背景资源 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/Widget/NoteWidgetProvider_2x.java b/src/Widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..904490b --- /dev/null +++ b/src/Widget/NoteWidgetProvider_2x.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; + +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 类继承自 NoteWidgetProvider,用于处理 2x 尺寸的笔记小部件。 + * 它重写了父类的一些抽象方法,以提供适合 2x 小部件的布局、背景资源和小部件类型。 + */ +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + + /** + * 当小部件需要更新时调用此方法。 + * 调用父类的 update 方法来完成小部件的更新操作。 + * + * @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。 + * + * @return 2x 小部件的布局资源 ID,即 R.layout.widget_2x + */ + @Override + protected int getLayoutId() { + return R.layout.widget_2x; + } + + /** + * 根据背景颜色 ID 获取 2x 小部件的背景资源 ID。 + * 调用 ResourceParser 类中的 WidgetBgResources 内部类的方法来获取相应的背景资源。 + * + * @param bgId 背景颜色 ID,用于指定要获取的背景颜色 + * @return 2x 小部件的背景资源 ID + */ + @Override + protected int getBgResourceId(int bgId) { + 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 new file mode 100644 index 0000000..ae17300 --- /dev/null +++ b/src/Widget/NoteWidgetProvider_4x.java @@ -0,0 +1,76 @@ +/* + * 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_4x 类继承自 NoteWidgetProvider,用于处理 4x 尺寸的笔记小部件。 + * 它重写了父类的一些方法,以提供适合 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) { + // 调用父类的 update 方法进行小部件更新 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取 4x 小部件的布局资源 ID。 + * + * @return 4x 小部件的布局资源 ID,即 R.layout.widget_4x + */ + protected int getLayoutId() { + return R.layout.widget_4x; + } + + /** + * 根据背景颜色 ID 获取 4x 小部件的背景资源 ID。 + * 借助 ResourceParser 类中的 WidgetBgResources 内部类来获取对应背景资源。 + * + * @param bgId 背景颜色 ID,代表不同的背景颜色 + * @return 4x 小部件对应背景颜色的背景资源 ID + */ + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + /** + * 获取 4x 小部件的类型。 + * + * @return 4x 小部件的类型,即 Notes.TYPE_WIDGET_4X + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; + } +} \ No newline at end of file diff --git a/src/tool/BackupUtils.java b/src/tool/BackupUtils.java new file mode 100644 index 0000000..0244587 --- /dev/null +++ b/src/tool/BackupUtils.java @@ -0,0 +1,441 @@ +/* + * 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.tool; + +import android.content.Context; +import android.database.Cursor; +import android.os.Environment; +import android.text.TextUtils; +import android.text.format.DateFormat; +import android.util.Log; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +// 该类用于实现笔记的备份功能,将笔记数据导出为文本文件 +public class BackupUtils { + // 日志标签,用于在日志中标识该类的相关信息 + private static final String TAG = "BackupUtils"; + // 单例模式的实例对象 + private static BackupUtils sInstance; + + // 获取 BackupUtils 的单例实例 + public static synchronized BackupUtils getInstance(Context context) { + // 如果实例还未创建,则创建一个新的实例 + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + + /** + * 以下状态用于表示备份或恢复操作的状态 + */ + // 当前 SD 卡未挂载 + public static final int STATE_SD_CARD_UNMOUONTED = 0; + // 备份文件不存在 + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; + // 数据格式不正确,可能被其他程序修改 + public static final int STATE_DATA_DESTROIED = 2; + // 运行时异常导致恢复或备份失败 + public static final int STATE_SYSTEM_ERROR = 3; + // 备份或恢复成功 + public static final int STATE_SUCCESS = 4; + + // 文本导出工具类的实例 + private TextExport mTextExport; + + // 私有构造函数,确保只能通过 getInstance 方法获取实例 + private BackupUtils(Context context) { + // 初始化 TextExport 实例 + mTextExport = new TextExport(context); + } + + // 检查外部存储是否可用 + private static boolean externalStorageAvailable() { + // 检查外部存储的状态是否为已挂载 + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + // 将笔记数据导出为文本文件 + public int exportToText() { + // 调用 TextExport 类的 exportToText 方法进行导出操作 + return mTextExport.exportToText(); + } + + // 获取导出的文本文件的文件名 + public String getExportedTextFileName() { + // 返回 TextExport 类中保存的文件名 + return mTextExport.mFileName; + } + + // 获取导出的文本文件的目录 + public String getExportedTextFileDir() { + // 返回 TextExport 类中保存的文件目录 + return mTextExport.mFileDirectory; + } + + // 内部类,用于实现将笔记数据导出为文本文件的具体逻辑 + private static class TextExport { + // 查询笔记信息时使用的投影,指定要查询的列 + private static final String[] NOTE_PROJECTION = { + NoteColumns.ID, + NoteColumns.MODIFIED_DATE, + NoteColumns.SNIPPET, + NoteColumns.TYPE + }; + + // 笔记 ID 在投影数组中的索引 + private static final int NOTE_COLUMN_ID = 0; + // 笔记修改日期在投影数组中的索引 + private static final int NOTE_COLUMN_MODIFIED_DATE = 1; + // 笔记摘要在投影数组中的索引 + private static final int NOTE_COLUMN_SNIPPET = 2; + + // 查询笔记数据信息时使用的投影,指定要查询的列 + private static final String[] DATA_PROJECTION = { + DataColumns.CONTENT, + DataColumns.MIME_TYPE, + DataColumns.DATA1, + DataColumns.DATA2, + DataColumns.DATA3, + DataColumns.DATA4, + }; + + // 笔记数据内容在投影数组中的索引 + private static final int DATA_COLUMN_CONTENT = 0; + // 笔记数据 MIME 类型在投影数组中的索引 + private static final int DATA_COLUMN_MIME_TYPE = 1; + // 通话记录日期在投影数组中的索引 + private static final int DATA_COLUMN_CALL_DATE = 2; + // 电话号码在投影数组中的索引 + private static final int DATA_COLUMN_PHONE_NUMBER = 4; + + // 文本格式数组,用于格式化导出的文本内容 + private final String[] TEXT_FORMAT; + // 文件夹名称的格式索引 + private static final int FORMAT_FOLDER_NAME = 0; + // 笔记日期的格式索引 + private static final int FORMAT_NOTE_DATE = 1; + // 笔记内容的格式索引 + private static final int FORMAT_NOTE_CONTENT = 2; + + // 上下文对象,用于获取资源和执行相关操作 + private Context mContext; + // 导出的文本文件的文件名 + private String mFileName; + // 导出的文本文件的目录 + private String mFileDirectory; + + // 构造函数,初始化 TextExport 类的实例 + public TextExport(Context context) { + // 从资源中获取文本格式数组 + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + // 保存上下文对象 + mContext = context; + // 初始化文件名和文件目录为空字符串 + mFileName = ""; + mFileDirectory = ""; + } + + // 根据索引获取文本格式字符串 + private String getFormat(int id) { + // 返回文本格式数组中指定索引的字符串 + return TEXT_FORMAT[id]; + } + + /** + * 将指定文件夹下的笔记导出为文本文件 + * + * @param folderId 文件夹的 ID + * @param ps 打印流,用于将导出的内容写入文件 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于该文件夹的笔记信息 + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[]{ + folderId + }, null); + + if (notesCursor != null) { + if (notesCursor.moveToFirst()) { + do { + // 打印笔记的最后修改日期 + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // 获取笔记的 ID + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + // 将该笔记导出为文本 + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + // 关闭游标,释放资源 + notesCursor.close(); + } + } + + /** + * 将指定 ID 的笔记导出为文本文件 + * + * @param noteId 笔记的 ID + * @param ps 打印流,用于将导出的内容写入文件 + */ + private void exportNoteToText(String noteId, PrintStream ps) { + // 查询属于该笔记的数据信息 + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[]{ + noteId + }, null); + + if (dataCursor != null) { + if (dataCursor.moveToFirst()) { + do { + // 获取数据的 MIME 类型 + String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + if (DataConstants.CALL_NOTE.equals(mimeType)) { + // 如果是通话记录笔记 + // 获取电话号码 + String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); + // 获取通话日期 + long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); + // 获取通话记录的位置信息 + String location = dataCursor.getString(DATA_COLUMN_CONTENT); + + if (!TextUtils.isEmpty(phoneNumber)) { + // 如果电话号码不为空,则打印电话号码 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + phoneNumber)); + } + // 打印通话日期 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat + .format(mContext.getString(R.string.format_datetime_mdhm), + callDate))); + if (!TextUtils.isEmpty(location)) { + // 如果位置信息不为空,则打印位置信息 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + location)); + } + } else if (DataConstants.NOTE.equals(mimeType)) { + // 如果是普通笔记 + // 获取笔记内容 + String content = dataCursor.getString(DATA_COLUMN_CONTENT); + if (!TextUtils.isEmpty(content)) { + // 如果笔记内容不为空,则打印笔记内容 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + content)); + } + } + } while (dataCursor.moveToNext()); + } + // 关闭游标,释放资源 + dataCursor.close(); + } + // 在笔记之间打印一个换行符 + try { + ps.write(new byte[]{ + Character.LINE_SEPARATOR, Character.LETTER_NUMBER + }); + } catch (IOException e) { + // 记录异常信息 + Log.e(TAG, e.toString()); + } + } + + /** + * 将笔记数据导出为用户可读的文本文件 + * + * @return 导出操作的状态码,可能的值为 STATE_SD_CARD_UNMOUONTED、STATE_SYSTEM_ERROR 或 STATE_SUCCESS + */ + public int exportToText() { + // 检查外部存储是否可用 + if (!externalStorageAvailable()) { + // 记录日志信息 + Log.d(TAG, "Media was not mounted"); + // 返回 SD 卡未挂载的状态码 + return STATE_SD_CARD_UNMOUONTED; + } + + // 获取用于导出文本的打印流 + PrintStream ps = getExportToTextPrintStream(); + if (ps == null) { + // 记录日志信息 + Log.e(TAG, "get print stream error"); + // 返回系统错误的状态码 + return STATE_SYSTEM_ERROR; + } + // 首先导出文件夹及其包含的笔记 + Cursor folderCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + + if (folderCursor != null) { + if (folderCursor.moveToFirst()) { + do { + // 获取文件夹名称 + String folderName = ""; + if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { + // 如果是通话记录文件夹,则从资源中获取文件夹名称 + folderName = mContext.getString(R.string.call_record_folder_name); + } else { + // 否则,从游标中获取文件夹摘要作为文件夹名称 + folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); + } + if (!TextUtils.isEmpty(folderName)) { + // 如果文件夹名称不为空,则打印文件夹名称 + ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); + } + // 获取文件夹 ID + String folderId = folderCursor.getString(NOTE_COLUMN_ID); + // 将该文件夹下的笔记导出为文本 + exportFolderToText(folderId, ps); + } while (folderCursor.moveToNext()); + } + // 关闭游标,释放资源 + folderCursor.close(); + } + + // 导出根文件夹下的笔记 + Cursor noteCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + + "=0", null, null); + + if (noteCursor != null) { + if (noteCursor.moveToFirst()) { + do { + // 打印笔记的最后修改日期 + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // 获取笔记的 ID + String noteId = noteCursor.getString(NOTE_COLUMN_ID); + // 将该笔记导出为文本 + exportNoteToText(noteId, ps); + } while (noteCursor.moveToNext()); + } + // 关闭游标,释放资源 + noteCursor.close(); + } + // 关闭打印流 + ps.close(); + + // 返回导出成功的状态码 + return STATE_SUCCESS; + } + + /** + * 获取一个指向导出文本文件的打印流 + * + * @return 打印流对象,如果创建文件或获取流失败,则返回 null + */ + private PrintStream getExportToTextPrintStream() { + // 生成一个存储在 SD 卡上的文件 + File file = generateFileMountedOnSDcard(mContext, R.string.file_path, + R.string.file_name_txt_format); + if (file == null) { + // 记录日志信息 + Log.e(TAG, "create file to exported failed"); + // 返回 null 表示创建文件失败 + return null; + } + // 保存文件名 + mFileName = file.getName(); + // 保存文件目录 + mFileDirectory = mContext.getString(R.string.file_path); + PrintStream ps = null; + try { + // 创建文件输出流 + FileOutputStream fos = new FileOutputStream(file); + // 创建打印流 + ps = new PrintStream(fos); + } catch (FileNotFoundException e) { + // 打印异常堆栈信息 + e.printStackTrace(); + // 返回 null 表示文件未找到 + return null; + } catch (NullPointerException e) { + // 打印异常堆栈信息 + e.printStackTrace(); + // 返回 null 表示空指针异常 + return null; + } + // 返回打印流对象 + return ps; + } + } + + /** + * 生成一个存储在 SD 卡上的文本文件,用于存储导出的数据 + * + * @param context 上下文对象,用于获取资源和执行相关操作 + * @param filePathResId 文件路径的资源 ID + * @param fileNameFormatResId 文件名格式的资源 ID + * @return 生成的文件对象,如果创建文件失败,则返回 null + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + // 创建一个 StringBuilder 对象,用于构建文件路径 + StringBuilder sb = new StringBuilder(); + // 添加外部存储目录 + sb.append(Environment.getExternalStorageDirectory()); + // 添加文件路径 + sb.append(context.getString(filePathResId)); + // 创建文件目录对象 + File filedir = new File(sb.toString()); + // 添加文件名 + sb.append(context.getString( + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), + System.currentTimeMillis()))); + // 创建文件对象 + File file = new File(sb.toString()); + + try { + // 如果文件目录不存在,则创建目录 + if (!filedir.exists()) { + filedir.mkdir(); + } + // 如果文件不存在,则创建文件 + if (!file.exists()) { + file.createNewFile(); + } + // 返回创建好的文件对象 + return file; + } catch (SecurityException e) { + // 打印安全异常信息 + e.printStackTrace(); + } catch (IOException e) { + // 打印输入输出异常信息 + e.printStackTrace(); + } + // 如果出现异常,返回 null + return null; + } +} \ No newline at end of file diff --git a/src/tool/DataUtils.java b/src/tool/DataUtils.java new file mode 100644 index 0000000..b83ec6a --- /dev/null +++ b/src/tool/DataUtils.java @@ -0,0 +1,467 @@ +//* + * 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.tool; + +import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.OperationApplicationException; +import android.database.Cursor; +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.NoteColumns; +import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; + +import java.util.ArrayList; +import java.util.HashSet; + +/** + * 该类提供了一系列用于操作笔记数据的工具方法,包括批量删除笔记、移动笔记到文件夹、获取文件夹数量等。 + */ +public class DataUtils { + // 日志标签,用于在日志中标识该类的输出信息 + public static final String TAG = "DataUtils"; + + /** + * 批量删除笔记的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param ids 要删除的笔记的 ID 集合 + * @return 如果删除成功返回 true,否则返回 false + */ + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 检查传入的 ID 集合是否为空 + if (ids == null) { + // 如果为空,记录日志并返回 true + Log.d(TAG, "the ids is null"); + return true; + } + // 检查 ID 集合的大小是否为 0 + if (ids.size() == 0) { + // 如果为 0,记录日志并返回 true + Log.d(TAG, "no id is in the hashset"); + return true; + } + + // 创建一个用于存储内容提供者操作的列表 + ArrayList operationList = new ArrayList(); + // 遍历 ID 集合 + for (long id : ids) { + // 检查是否为系统根文件夹的 ID,如果是则不进行删除操作并记录错误日志 + if(id == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "Don't delete system folder root"); + continue; + } + // 创建一个删除操作的构建器 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 将构建好的删除操作添加到操作列表中 + operationList.add(builder.build()); + } + try { + // 应用批量操作并获取操作结果 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果是否为空或无效 + if (results == null || results.length == 0 || results[0] == null) { + // 如果无效,记录日志并返回 false + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + // 操作成功,返回 true + return true; + } catch (RemoteException e) { + // 捕获远程异常并记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + // 捕获操作应用异常并记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + // 发生异常,返回 false + return false; + } + + /** + * 将单个笔记移动到指定文件夹的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param id 要移动的笔记的 ID + * @param srcFolderId 笔记的源文件夹 ID + * @param desFolderId 笔记要移动到的目标文件夹 ID + */ + public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { + // 创建一个 ContentValues 对象,用于存储要更新的字段和值 + ContentValues values = new ContentValues(); + // 设置笔记的父文件夹 ID 为目标文件夹 ID + values.put(NoteColumns.PARENT_ID, desFolderId); + // 设置笔记的原始父文件夹 ID 为源文件夹 ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); + // 设置笔记的本地修改标志为 1 + values.put(NoteColumns.LOCAL_MODIFIED, 1); + // 执行更新操作,将笔记移动到目标文件夹 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + } + + /** + * 批量将笔记移动到指定文件夹的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param ids 要移动的笔记的 ID 集合 + * @param folderId 笔记要移动到的目标文件夹 ID + * @return 如果移动成功返回 true,否则返回 false + */ + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, + long folderId) { + // 检查传入的 ID 集合是否为空 + if (ids == null) { + // 如果为空,记录日志并返回 true + Log.d(TAG, "the ids is null"); + return true; + } + + // 创建一个用于存储内容提供者操作的列表 + ArrayList operationList = new ArrayList(); + // 遍历 ID 集合 + for (long id : ids) { + // 创建一个更新操作的构建器 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 设置笔记的父文件夹 ID 为目标文件夹 ID + builder.withValue(NoteColumns.PARENT_ID, folderId); + // 设置笔记的本地修改标志为 1 + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); + // 将构建好的更新操作添加到操作列表中 + operationList.add(builder.build()); + } + + try { + // 应用批量操作并获取操作结果 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果是否为空或无效 + if (results == null || results.length == 0 || results[0] == null) { + // 如果无效,记录日志并返回 false + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + // 操作成功,返回 true + return true; + } catch (RemoteException e) { + // 捕获远程异常并记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + // 捕获操作应用异常并记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + // 发生异常,返回 false + return false; + } + + /** + * 获取除系统文件夹外的所有用户文件夹数量的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @return 用户文件夹的数量 + */ + public static int getUserFolderCount(ContentResolver resolver) { + // 查询笔记数据库,获取符合条件的文件夹数量 + Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, + new String[] { "COUNT(*)" }, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", + new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, + null); + + // 初始化文件夹数量为 0 + int count = 0; + // 检查游标是否不为空 + if(cursor != null) { + // 将游标移动到第一行 + if(cursor.moveToFirst()) { + try { + // 从游标中获取文件夹数量 + count = cursor.getInt(0); + } catch (IndexOutOfBoundsException e) { + // 捕获索引越界异常并记录错误日志 + Log.e(TAG, "get folder count failed:" + e.toString()); + } finally { + // 关闭游标 + cursor.close(); + } + } + } + // 返回文件夹数量 + return count; + } + + /** + * 检查笔记是否在数据库中可见的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param noteId 要检查的笔记的 ID + * @param type 笔记的类型 + * @return 如果笔记可见返回 true,否则返回 false + */ + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + // 查询笔记数据库,检查笔记是否存在且不在回收站中 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, + new String [] {String.valueOf(type)}, + null); + + // 初始化存在标志为 false + boolean exist = false; + // 检查游标是否不为空 + if (cursor != null) { + // 检查游标中的记录数量是否大于 0 + if (cursor.getCount() > 0) { + // 如果大于 0,说明笔记存在,设置存在标志为 true + exist = true; + } + // 关闭游标 + cursor.close(); + } + // 返回存在标志 + return exist; + } + + /** + * 检查笔记是否存在于数据库中的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param noteId 要检查的笔记的 ID + * @return 如果笔记存在返回 true,否则返回 false + */ + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + // 查询笔记数据库,检查笔记是否存在 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, null, null, null); + + // 初始化存在标志为 false + boolean exist = false; + // 检查游标是否不为空 + if (cursor != null) { + // 检查游标中的记录数量是否大于 0 + if (cursor.getCount() > 0) { + // 如果大于 0,说明笔记存在,设置存在标志为 true + exist = true; + } + // 关闭游标 + cursor.close(); + } + // 返回存在标志 + return exist; + } + + /** + * 检查数据是否存在于数据库中的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param dataId 要检查的数据的 ID + * @return 如果数据存在返回 true,否则返回 false + */ + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + // 查询数据数据库,检查数据是否存在 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), + null, null, null, null); + + // 初始化存在标志为 false + boolean exist = false; + // 检查游标是否不为空 + if (cursor != null) { + // 检查游标中的记录数量是否大于 0 + if (cursor.getCount() > 0) { + // 如果大于 0,说明数据存在,设置存在标志为 true + exist = true; + } + // 关闭游标 + cursor.close(); + } + // 返回存在标志 + return exist; + } + + /** + * 检查可见文件夹名称是否已存在的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param name 要检查的文件夹名称 + * @return 如果文件夹名称已存在返回 true,否则返回 false + */ + public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { + // 查询笔记数据库,检查可见文件夹名称是否已存在 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + + " AND " + NoteColumns.SNIPPET + "=?", + new String[] { name }, null); + // 初始化存在标志为 false + boolean exist = false; + // 检查游标是否不为空 + if(cursor != null) { + // 检查游标中的记录数量是否大于 0 + if(cursor.getCount() > 0) { + // 如果大于 0,说明文件夹名称已存在,设置存在标志为 true + exist = true; + } + // 关闭游标 + cursor.close(); + } + // 返回存在标志 + return exist; + } + + /** + * 获取指定文件夹下的笔记小部件属性集合的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param folderId 文件夹的 ID + * @return 笔记小部件属性的集合,如果没有则返回 null + */ + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + // 查询笔记数据库,获取指定文件夹下的笔记小部件信息 + Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, + new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, + NoteColumns.PARENT_ID + "=?", + new String[] { String.valueOf(folderId) }, + null); + + // 初始化小部件属性集合为 null + HashSet set = null; + // 检查游标是否不为空 + if (c != null) { + // 将游标移动到第一行 + if (c.moveToFirst()) { + // 创建一个小部件属性集合 + set = new HashSet(); + do { + try { + // 创建一个小部件属性对象 + AppWidgetAttribute widget = new AppWidgetAttribute(); + // 从游标中获取小部件 ID + widget.widgetId = c.getInt(0); + // 从游标中获取小部件类型 + widget.widgetType = c.getInt(1); + // 将小部件属性对象添加到集合中 + set.add(widget); + } catch (IndexOutOfBoundsException e) { + // 捕获索引越界异常并记录错误日志 + Log.e(TAG, e.toString()); + } + } while (c.moveToNext()); + } + // 关闭游标 + c.close(); + } + // 返回小部件属性集合 + return set; + } + + /** + * 根据笔记 ID 获取通话号码的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param noteId 笔记的 ID + * @return 通话号码,如果未找到则返回空字符串 + */ + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 查询数据数据库,获取指定笔记对应的通话号码 + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String [] { CallNote.PHONE_NUMBER }, + CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", + new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, + null); + + // 检查游标是否不为空且能移动到第一行 + if (cursor != null && cursor.moveToFirst()) { + try { + // 从游标中获取通话号码 + return cursor.getString(0); + } catch (IndexOutOfBoundsException e) { + // 捕获索引越界异常并记录错误日志 + Log.e(TAG, "Get call number fails " + e.toString()); + } finally { + // 关闭游标 + cursor.close(); + } + } + // 未找到通话号码,返回空字符串 + return ""; + } + + /** + * 根据电话号码和通话日期获取笔记 ID 的方法。 + * + * @param resolver 内容解析器,用于与内容提供者进行交互 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + * @return 笔记的 ID,如果未找到则返回 0 + */ + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + // 查询数据数据库 + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String [] { CallNote.NOTE_ID }, + CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + + CallNote.PHONE_NUMBER + ",?)", + new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, + null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + try { + return cursor.getLong(0); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + cursor.close(); + } + return 0; + } + + public static String getSnippetById(ContentResolver resolver, long noteId) { + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String [] { NoteColumns.SNIPPET }, + NoteColumns.ID + "=?", + new String [] { String.valueOf(noteId)}, + null); + + if (cursor != null) { + String snippet = ""; + if (cursor.moveToFirst()) { + snippet = cursor.getString(0); + } + cursor.close(); + return snippet; + } + throw new IllegalArgumentException("Note is not found with id: " + noteId); + } + + public static String getFormattedSnippet(String snippet) { + if (snippet != null) { + snippet = snippet.trim(); + int index = snippet.indexOf('\n'); + if (index != -1) { + snippet = snippet.substring(0, index); + } + } + return snippet; + } +} diff --git a/src/tool/GTaskStringUtils.java b/src/tool/GTaskStringUtils.java new file mode 100644 index 0000000..946c2b1 --- /dev/null +++ b/src/tool/GTaskStringUtils.java @@ -0,0 +1,163 @@ +/* + * 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.tool; + +/** + * 该类包含了与 Google 任务(GTask)相关的 JSON 字符串常量, + * 用于在处理 GTask 数据交互时作为键名使用,同时也定义了一些特定文件夹名称和元数据相关的常量。 + */ +public class GTaskStringUtils { + + // 表示动作 ID 的 JSON 键名 + public final static String GTASK_JSON_ACTION_ID = "action_id"; + + // 表示动作列表的 JSON 键名 + public final static String GTASK_JSON_ACTION_LIST = "action_list"; + + // 表示动作类型的 JSON 键名 + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + + // 表示创建动作类型的 JSON 值 + public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; + + // 表示获取所有数据动作类型的 JSON 值 + public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + + // 表示移动动作类型的 JSON 值 + public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; + + // 表示更新动作类型的 JSON 值 + public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + + // 表示创建者 ID 的 JSON 键名 + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + + // 表示子实体的 JSON 键名 + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + + // 表示客户端版本的 JSON 键名 + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + + // 表示任务是否完成的 JSON 键名 + public final static String GTASK_JSON_COMPLETED = "completed"; + + // 表示当前列表 ID 的 JSON 键名 + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + + // 表示默认列表 ID 的 JSON 键名 + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + + // 表示是否删除的 JSON 键名 + public final static String GTASK_JSON_DELETED = "deleted"; + + // 表示目标列表的 JSON 键名 + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + + // 表示目标父实体的 JSON 键名 + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + + // 表示目标父实体类型的 JSON 键名 + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + + // 表示实体增量的 JSON 键名 + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + + // 表示实体类型的 JSON 键名 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + + // 表示获取已删除数据的 JSON 键名 + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + + // 表示 ID 的 JSON 键名 + public final static String GTASK_JSON_ID = "id"; + + // 表示索引的 JSON 键名 + public final static String GTASK_JSON_INDEX = "index"; + + // 表示最后修改时间的 JSON 键名 + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + + // 表示最新同步点的 JSON 键名 + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + + // 表示列表 ID 的 JSON 键名 + public final static String GTASK_JSON_LIST_ID = "list_id"; + + // 表示列表集合的 JSON 键名 + public final static String GTASK_JSON_LISTS = "lists"; + + // 表示名称的 JSON 键名 + public final static String GTASK_JSON_NAME = "name"; + + // 表示新 ID 的 JSON 键名 + public final static String GTASK_JSON_NEW_ID = "new_id"; + + // 表示备注的 JSON 键名 + public final static String GTASK_JSON_NOTES = "notes"; + + // 表示父实体 ID 的 JSON 键名 + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + + // 表示前一个兄弟实体 ID 的 JSON 键名 + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + + // 表示结果的 JSON 键名 + public final static String GTASK_JSON_RESULTS = "results"; + + // 表示源列表的 JSON 键名 + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + + // 表示任务集合的 JSON 键名 + public final static String GTASK_JSON_TASKS = "tasks"; + + // 表示类型的 JSON 键名 + public final static String GTASK_JSON_TYPE = "type"; + + // 表示组类型的 JSON 值 + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + + // 表示任务类型的 JSON 值 + public final static String GTASK_JSON_TYPE_TASK = "TASK"; + + // 表示用户的 JSON 键名 + public final static String GTASK_JSON_USER = "user"; + + // MIUI 笔记文件夹前缀 + public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + + // 默认文件夹名称 + public final static String FOLDER_DEFAULT = "Default"; + + // 通话笔记文件夹名称 + public final static String FOLDER_CALL_NOTE = "Call_Note"; + + // 元数据文件夹名称 + public final static String FOLDER_META = "METADATA"; + + // 元数据头部 Google 任务 ID + public final static String META_HEAD_GTASK_ID = "meta_gid"; + + // 元数据头部笔记信息 + public final static String META_HEAD_NOTE = "meta_note"; + + // 元数据头部数据信息 + public final static String META_HEAD_DATA = "meta_data"; + + // 元数据笔记名称,提示不要更新和删除 + public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; +} +11 \ No newline at end of file diff --git a/src/tool/ResourceParser.java b/src/tool/ResourceParser.java new file mode 100644 index 0000000..729dca7 --- /dev/null +++ b/src/tool/ResourceParser.java @@ -0,0 +1,270 @@ +/* + * 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.tool; + +import android.content.Context; +import android.preference.PreferenceManager; + +import net.micode.notes.R; +import net.micode.notes.ui.NotesPreferenceActivity; + +/** + * 该类用于解析和管理笔记应用中的资源,包括背景颜色、字体大小、不同界面元素的背景资源等。 + */ +public class ResourceParser { + + // 定义笔记背景颜色的常量 + public static final int YELLOW = 0; + public static final int BLUE = 1; + public static final int WHITE = 2; + public static final int GREEN = 3; + public static final int RED = 4; + + // 默认的笔记背景颜色 + public static final int BG_DEFAULT_COLOR = YELLOW; + + // 定义笔记字体大小的常量 + public static final int TEXT_SMALL = 0; + public static final int TEXT_MEDIUM = 1; + public static final int TEXT_LARGE = 2; + public static final int TEXT_SUPER = 3; + + // 默认的笔记字体大小 + public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; + + /** + * 该静态内部类用于管理笔记编辑界面的背景资源。 + */ + public static class NoteBgResources { + // 笔记编辑界面的背景资源数组 + private final static int [] BG_EDIT_RESOURCES = new int [] { + R.drawable.edit_yellow, + R.drawable.edit_blue, + R.drawable.edit_white, + R.drawable.edit_green, + R.drawable.edit_red + }; + + // 笔记编辑界面标题的背景资源数组 + private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { + R.drawable.edit_title_yellow, + R.drawable.edit_title_blue, + R.drawable.edit_title_white, + R.drawable.edit_title_green, + R.drawable.edit_title_red + }; + + /** + * 根据传入的颜色 ID 获取笔记编辑界面的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 笔记编辑界面的背景资源 ID + */ + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + /** + * 根据传入的颜色 ID 获取笔记编辑界面标题的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 笔记编辑界面标题的背景资源 ID + */ + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + /** + * 获取默认的笔记背景颜色 ID。 + * 如果用户在偏好设置中开启了随机背景颜色选项,则随机返回一个颜色 ID; + * 否则返回默认的背景颜色 ID。 + * @param context 上下文对象 + * @return 默认的笔记背景颜色 ID + */ + public static int getDefaultBgId(Context context) { + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( + NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); + } else { + return BG_DEFAULT_COLOR; + } + } + + /** + * 该静态内部类用于管理笔记列表项的背景资源。 + */ + public static class NoteItemBgResources { + // 笔记列表项中第一项的背景资源数组 + private final static int [] BG_FIRST_RESOURCES = new int [] { + R.drawable.list_yellow_up, + R.drawable.list_blue_up, + R.drawable.list_white_up, + R.drawable.list_green_up, + R.drawable.list_red_up + }; + + // 笔记列表项中普通项的背景资源数组 + private final static int [] BG_NORMAL_RESOURCES = new int [] { + R.drawable.list_yellow_middle, + R.drawable.list_blue_middle, + R.drawable.list_white_middle, + R.drawable.list_green_middle, + R.drawable.list_red_middle + }; + + // 笔记列表项中最后一项的背景资源数组 + private final static int [] BG_LAST_RESOURCES = new int [] { + R.drawable.list_yellow_down, + R.drawable.list_blue_down, + R.drawable.list_white_down, + R.drawable.list_green_down, + R.drawable.list_red_down, + }; + + // 笔记列表项中单独一项的背景资源数组 + private final static int [] BG_SINGLE_RESOURCES = new int [] { + R.drawable.list_yellow_single, + R.drawable.list_blue_single, + R.drawable.list_white_single, + R.drawable.list_green_single, + R.drawable.list_red_single + }; + + /** + * 根据传入的颜色 ID 获取笔记列表项中第一项的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 笔记列表项中第一项的背景资源 ID + */ + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + } + + /** + * 根据传入的颜色 ID 获取笔记列表项中最后一项的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 笔记列表项中最后一项的背景资源 ID + */ + public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; + } + + /** + * 根据传入的颜色 ID 获取笔记列表项中单独一项的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 笔记列表项中单独一项的背景资源 ID + */ + public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; + } + + /** + * 根据传入的颜色 ID 获取笔记列表项中普通项的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 笔记列表项中普通项的背景资源 ID + */ + public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_RESOURCES[id]; + } + + /** + * 获取文件夹的背景资源 ID。 + * @return 文件夹的背景资源 ID + */ + public static int getFolderBgRes() { + return R.drawable.list_folder; + } + } + + /** + * 该静态内部类用于管理小部件的背景资源。 + */ + public static class WidgetBgResources { + // 2x 大小小部件的背景资源数组 + private final static int [] BG_2X_RESOURCES = new int [] { + R.drawable.widget_2x_yellow, + R.drawable.widget_2x_blue, + R.drawable.widget_2x_white, + R.drawable.widget_2x_green, + R.drawable.widget_2x_red, + }; + + /** + * 根据传入的颜色 ID 获取 2x 大小小部件的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 2x 大小小部件的背景资源 ID + */ + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 4x 大小小部件的背景资源数组 + private final static int [] BG_4X_RESOURCES = new int [] { + R.drawable.widget_4x_yellow, + R.drawable.widget_4x_blue, + R.drawable.widget_4x_white, + R.drawable.widget_4x_green, + R.drawable.widget_4x_red + }; + + /** + * 根据传入的颜色 ID 获取 4x 大小小部件的背景资源 ID。 + * @param id 颜色 ID,对应前面定义的颜色常量 + * @return 4x 大小小部件的背景资源 ID + */ + public static int getWidget4xBgResource(int id) { + return BG_4X_RESOURCES[id]; + } + } + + /** + * 该静态内部类用于管理文本外观资源。 + */ + public static class TextAppearanceResources { + // 文本外观资源数组 + private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { + R.style.TextAppearanceNormal, + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper + }; + + /** + * 根据传入的 ID 获取文本外观资源 ID。 + * 如果传入的 ID 超出了资源数组的长度,返回默认的字体大小对应的资源 ID。 + * @param id 文本外观 ID + * @return 文本外观资源 ID + */ + public static int getTexAppearanceResource(int id) { + /** + * HACKME: Fix bug of store the resource id in shared preference. + * The id may larger than the length of resources, in this case, + * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} + */ + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + /** + * 获取文本外观资源数组的长度。 + * @return 文本外观资源数组的长度 + */ + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } + } +} \ No newline at end of file diff --git a/src/项目原代码.txt b/src/项目原代码.txt new file mode 100644 index 0000000..5f25df6 --- /dev/null +++ b/src/项目原代码.txt @@ -0,0 +1 @@ +assdjgyuguyhgnb6 \ No newline at end of file