diff --git a/doc/小米便签开源代码的泛读报告.docx b/doc/小米便签开源代码的泛读报告.docx index cb4e9ca..8c3124a 100644 Binary files a/doc/小米便签开源代码的泛读报告.docx and b/doc/小米便签开源代码的泛读报告.docx differ diff --git a/src/data/Contact.java b/src/data/Contact.java new file mode 100644 index 0000000..6cc8678 --- /dev/null +++ b/src/data/Contact.java @@ -0,0 +1,98 @@ +/* + * 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.data; + +import android.content.Context; +import android.database.Cursor; +import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.Data; +import android.telephony.PhoneNumberUtils; +import android.util.Log; + +import java.util.HashMap; + +/** + * Contact类:用于从系统联系人中查询电话号码对应的联系人姓名 + * 提供了联系人姓名缓存机制,避免重复查询相同号码 + */ +public class Contact { + // 联系人缓存:键为电话号码,值为对应的联系人姓名 + private static HashMap sContactCache; + // 日志标签 + private static final String TAG = "Contact"; + + // 联系人ID查询的SQL选择条件模板 + // 用于查找与给定电话号码匹配的联系人 + private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER + + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + + " AND " + Data.RAW_CONTACT_ID + " IN " + + "(SELECT raw_contact_id " + + " FROM phone_lookup" + + " WHERE min_match = '+')"; + + /** + * 根据电话号码获取联系人姓名 + * @param context 应用上下文 + * @param phoneNumber 电话号码 + * @return 联系人姓名,如果未找到则返回null + */ + public static String getContact(Context context, String phoneNumber) { + // 初始化联系人缓存(如果尚未初始化) + if(sContactCache == null) { + sContactCache = new HashMap(); + } + + // 检查缓存中是否已有该号码的联系人信息 + if(sContactCache.containsKey(phoneNumber)) { + return sContactCache.get(phoneNumber); + } + + // 构建实际的查询条件,替换模板中的占位符 + String selection = CALLER_ID_SELECTION.replace("+", + PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); + + // 执行联系人数据库查询 + Cursor cursor = context.getContentResolver().query( + Data.CONTENT_URI, // 查询的URI + new String [] { Phone.DISPLAY_NAME }, // 需要返回的字段 + selection, // 查询条件 + new String[] { phoneNumber }, // 查询参数 + null); // 排序方式 + + // 处理查询结果 + if (cursor != null && cursor.moveToFirst()) { + try { + // 获取联系人姓名 + String name = cursor.getString(0); + // 将结果存入缓存 + sContactCache.put(phoneNumber, name); + return name; + } catch (IndexOutOfBoundsException e) { + // 处理异常情况 + Log.e(TAG, " Cursor get string error " + e.toString()); + return null; + } finally { + // 确保关闭游标,避免资源泄漏 + cursor.close(); + } + } else { + // 未找到匹配的联系人 + Log.d(TAG, "No contact matched with number:" + phoneNumber); + return null; + } + } +} \ No newline at end of file diff --git a/src/data/Notes.java b/src/data/Notes.java new file mode 100644 index 0000000..aae7620 --- /dev/null +++ b/src/data/Notes.java @@ -0,0 +1,300 @@ +/** + * 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.data; + +import android.net.Uri; + +/** + * Notes类:提供便签应用的数据结构和常量定义 + * 包含便签类型、系统文件夹ID、意图额外数据键名、内容URI以及数据表列定义 + */ +public class Notes { + // 内容提供者的授权信息 + public static final String AUTHORITY = "micode_notes"; + // 日志标签 + public static final String TAG = "Notes"; + + // 便签类型常量 + public static final int TYPE_NOTE = 0; // 普通便签 + public static final int TYPE_FOLDER = 1; // 文件夹 + public static final int TYPE_SYSTEM = 2; // 系统便签 + + /** + * 系统文件夹标识符 + * {@link Notes#ID_ROOT_FOLDER } 是默认文件夹 + * {@link Notes#ID_TEMPARAY_FOLDER } 用于无文件夹归属的便签 + * {@link Notes#ID_CALL_RECORD_FOLDER} 用于存储通话记录 + */ + public static final int ID_ROOT_FOLDER = 0; // 根文件夹ID + public static final int ID_TEMPARAY_FOLDER = -1; // 临时文件夹ID + public static final int ID_CALL_RECORD_FOLDER = -2; // 通话记录文件夹ID + public static final int ID_TRASH_FOLER = -3; // 回收站文件夹ID + + // 意图额外数据的键名常量 + public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date"; // 提醒日期 + public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id"; // 背景颜色ID + public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; // 小部件ID + public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; // 小部件类型 + public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; // 文件夹ID + public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; // 通话日期 + + // 小部件类型常量 + public static final int TYPE_WIDGET_INVALIDE = -1; // 无效小部件 + public static final int TYPE_WIDGET_2X = 0; // 2x大小的小部件 + public static final int TYPE_WIDGET_4X = 1; // 4x大小的小部件 + + /** + * 数据常量类 + * 定义便签数据的MIME类型 + */ + public static class DataConstants { + public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; // 文本便签MIME类型 + public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; // 通话记录便签MIME类型 + } + + /** + * 用于查询所有便签和文件夹的URI + */ + public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); + + /** + * 用于查询数据的URI + */ + public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + + /** + * 便签表列定义接口 + * 定义了便签表中各字段的名称和含义 + */ + public interface NoteColumns { + /** + * 行的唯一ID + *

类型: INTEGER (long)

+ */ + public static final String ID = "_id"; + + /** + * 便签或文件夹的父ID + *

类型: INTEGER (long)

+ */ + public static final String PARENT_ID = "parent_id"; + + /** + * 便签或文件夹的创建日期 + *

类型: INTEGER (long)

+ */ + public static final String CREATED_DATE = "created_date"; + + /** + * 最新修改日期 + *

类型: INTEGER (long)

+ */ + public static final String MODIFIED_DATE = "modified_date"; + + /** + * 提醒日期 + *

类型: INTEGER (long)

+ */ + public static final String ALERTED_DATE = "alert_date"; + + /** + * 文件夹名称或便签的文本内容摘要 + *

类型: TEXT

+ */ + public static final String SNIPPET = "snippet"; + + /** + * 便签的小部件ID + *

类型: INTEGER (long)

+ */ + public static final String WIDGET_ID = "widget_id"; + + /** + * 便签的小部件类型 + *

类型: INTEGER (long)

+ */ + public static final String WIDGET_TYPE = "widget_type"; + + /** + * 便签的背景颜色ID + *

类型: INTEGER (long)

+ */ + public static final String BG_COLOR_ID = "bg_color_id"; + + /** + * 对于文本便签,没有附件;对于多媒体便签,至少有一个附件 + *

类型: INTEGER

+ */ + public static final String HAS_ATTACHMENT = "has_attachment"; + + /** + * 文件夹中的便签数量 + *

类型: INTEGER (long)

+ */ + public static final String NOTES_COUNT = "notes_count"; + + /** + * 文件类型:文件夹或便签 + *

类型: INTEGER

+ */ + public static final String TYPE = "type"; + + /** + * 最后同步ID + *

类型: INTEGER (long)

+ */ + public static final String SYNC_ID = "sync_id"; + + /** + * 指示本地是否修改的标志 + *

类型: INTEGER

+ */ + public static final String LOCAL_MODIFIED = "local_modified"; + + /** + * 移动到临时文件夹之前的原始父ID + *

类型 : INTEGER

+ */ + public static final String ORIGIN_PARENT_ID = "origin_parent_id"; + + /** + * Google任务ID + *

类型 : TEXT

+ */ + public static final String GTASK_ID = "gtask_id"; + + /** + * 版本代码 + *

类型 : INTEGER (long)

+ */ + public static final String VERSION = "version"; + } + + /** + * 数据表列定义接口 + * 定义了数据表中各字段的名称和含义 + */ + public interface DataColumns { + /** + * 行的唯一ID + *

类型: INTEGER (long)

+ */ + public static final String ID = "_id"; + + /** + * 此行表示的项目的MIME类型 + *

类型: Text

+ */ + public static final String MIME_TYPE = "mime_type"; + + /** + * 此数据所属便签的引用ID + *

类型: INTEGER (long)

+ */ + public static final String NOTE_ID = "note_id"; + + /** + * 便签或文件夹的创建日期 + *

类型: INTEGER (long)

+ */ + public static final String CREATED_DATE = "created_date"; + + /** + * 最新修改日期 + *

类型: INTEGER (long)

+ */ + public static final String MODIFIED_DATE = "modified_date"; + + /** + * 数据内容 + *

类型: TEXT

+ */ + public static final String CONTENT = "content"; + + /** + * 通用数据列,含义由{@link #MIMETYPE}指定,用于整数数据类型 + *

类型: INTEGER

+ */ + public static final String DATA1 = "data1"; + + /** + * 通用数据列,含义由{@link #MIMETYPE}指定,用于整数数据类型 + *

类型: INTEGER

+ */ + public static final String DATA2 = "data2"; + + /** + * 通用数据列,含义由{@link #MIMETYPE}指定,用于文本数据类型 + *

类型: TEXT

+ */ + public static final String DATA3 = "data3"; + + /** + * 通用数据列,含义由{@link #MIMETYPE}指定,用于文本数据类型 + *

类型: TEXT

+ */ + public static final String DATA4 = "data4"; + + /** + * 通用数据列,含义由{@link #MIMETYPE}指定,用于文本数据类型 + *

类型: TEXT

+ */ + public static final String DATA5 = "data5"; + } + + /** + * 文本便签类 + * 继承DataColumns接口,定义文本便签特有的常量和URI + */ + public static final class TextNote implements DataColumns { + /** + * 模式指示文本是否处于复选列表模式 + *

类型: Integer 1:复选列表模式 0:普通模式

+ */ + public static final String MODE = DATA1; + + public static final int MODE_CHECK_LIST = 1; + + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; + + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); + } + + /** + * 通话记录便签类 + * 继承DataColumns接口,定义通话记录便签特有的常量和URI + */ + public static final class CallNote implements DataColumns { + /** + * 此记录的通话日期 + *

类型: INTEGER (long)

+ */ + public static final String CALL_DATE = DATA1; + + /** + * 此记录的电话号码 + *

类型: TEXT

+ */ + public static final String PHONE_NUMBER = DATA3; + + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; + + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); + } +} \ No newline at end of file diff --git a/src/data/NotesDatabaseHelper.java b/src/data/NotesDatabaseHelper.java new file mode 100644 index 0000000..3529930 --- /dev/null +++ b/src/data/NotesDatabaseHelper.java @@ -0,0 +1,436 @@ +/* + * 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.data; + +import android.content.ContentValues; +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; + +/** + * NotesDatabaseHelper类:管理便签应用的SQLite数据库 + * 负责数据库的创建、升级和表结构管理 + * 通过触发器实现便签和文件夹之间的关联和数据一致性 + */ +public class NotesDatabaseHelper extends SQLiteOpenHelper { + // 数据库名称 + private static final String DB_NAME = "note.db"; + + // 数据库版本 + private static final int DB_VERSION = 4; + + /** + * 表名常量接口 + */ + public interface TABLE { + public static final String NOTE = "note"; // 便签表 + public static final String DATA = "data"; // 数据表 + } + + // 日志标签 + private static final String TAG = "NotesDatabaseHelper"; + + // 单例实例 + private static NotesDatabaseHelper mInstance; + + // 创建便签表的SQL语句 + private static final String CREATE_NOTE_TABLE_SQL = + "CREATE TABLE " + TABLE.NOTE + "(" + + NoteColumns.ID + " INTEGER PRIMARY KEY," + // 便签ID + NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + // 父文件夹ID + NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + // 提醒日期 + NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + // 背景颜色ID + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 创建日期 + NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + // 是否有附件 + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 修改日期 + NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + // 子便签数量 + NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + // 摘要 + NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + // 类型 + NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + // 小部件ID + NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + // 小部件类型 + NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + // 同步ID + NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + // 本地修改标志 + NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + // 原始父文件夹ID + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + // Google任务ID + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + // 版本号 + ")"; + + // 创建数据表的SQL语句 + private static final String CREATE_DATA_TABLE_SQL = + "CREATE TABLE " + TABLE.DATA + "(" + + DataColumns.ID + " INTEGER PRIMARY KEY," + // 数据ID + DataColumns.MIME_TYPE + " TEXT NOT NULL," + // MIME类型 + DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + // 关联的便签ID + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 创建日期 + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 修改日期 + DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + // 内容 + DataColumns.DATA1 + " INTEGER," + // 通用数据列1 + DataColumns.DATA2 + " INTEGER," + // 通用数据列2 + DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + // 通用数据列3 + DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + // 通用数据列4 + DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + // 通用数据列5 + ")"; + + // 创建数据表的索引SQL语句 + private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = + "CREATE INDEX IF NOT EXISTS note_id_index ON " + + TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; + + /** + * 当便签移动到新文件夹时,增加目标文件夹的便签计数 + */ + private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = + "CREATE TRIGGER increase_folder_count_on_update "+ + " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + + " END"; + + /** + * 当便签移出文件夹时,减少源文件夹的便签计数 + */ + private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = + "CREATE TRIGGER decrease_folder_count_on_update " + + " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + + " AND " + NoteColumns.NOTES_COUNT + ">0" + ";" + + " END"; + + /** + * 当插入新便签到文件夹时,增加文件夹的便签计数 + */ + private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = + "CREATE TRIGGER increase_folder_count_on_insert " + + " AFTER INSERT ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + + " END"; + + /** + * 当从文件夹中删除便签时,减少文件夹的便签计数 + */ + private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = + "CREATE TRIGGER decrease_folder_count_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + + " AND " + NoteColumns.NOTES_COUNT + ">0;" + + " END"; + + /** + * 当插入类型为{@link DataConstants#NOTE}的数据时,更新便签内容 + */ + private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER = + "CREATE TRIGGER update_note_content_on_insert " + + " AFTER INSERT ON " + TABLE.DATA + + " WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + + " END"; + + /** + * 当类型为{@link DataConstants#NOTE}的数据更新时,更新便签内容 + */ + private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER = + "CREATE TRIGGER update_note_content_on_update " + + " AFTER UPDATE ON " + TABLE.DATA + + " WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + + " END"; + + /** + * 当类型为{@link DataConstants#NOTE}的数据删除时,清空便签内容 + */ + private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER = + "CREATE TRIGGER update_note_content_on_delete " + + " AFTER delete ON " + TABLE.DATA + + " WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.SNIPPET + "=''" + + " WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" + + " END"; + + /** + * 当删除便签时,同时删除该便签关联的数据 + */ + private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER = + "CREATE TRIGGER delete_data_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN" + + " DELETE FROM " + TABLE.DATA + + " WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" + + " END"; + + /** + * 当删除文件夹时,同时删除该文件夹下的所有便签 + */ + private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = + "CREATE TRIGGER folder_delete_notes_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN" + + " DELETE FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + + " END"; + + /** + * 当文件夹被移入回收站时,将该文件夹下的所有便签也移入回收站 + */ + private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER = + "CREATE TRIGGER folder_move_notes_on_trash " + + " AFTER UPDATE ON " + TABLE.NOTE + + " WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + + " END"; + + /** + * 构造函数 + * @param context 上下文 + */ + public NotesDatabaseHelper(Context context) { + super(context, DB_NAME, null, DB_VERSION); + } + + /** + * 创建便签表 + * @param db 数据库实例 + */ + public void createNoteTable(SQLiteDatabase db) { + db.execSQL(CREATE_NOTE_TABLE_SQL); + reCreateNoteTableTriggers(db); + createSystemFolder(db); + Log.d(TAG, "note table has been created"); + } + + /** + * 重新创建便签表的触发器 + * @param db 数据库实例 + */ + private void reCreateNoteTableTriggers(SQLiteDatabase db) { + // 删除旧触发器 + db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update"); + db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update"); + db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert"); + db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash"); + + // 创建新触发器 + db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); + db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); + db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER); + db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER); + db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER); + db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER); + db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER); + } + + /** + * 创建系统文件夹 + * @param db 数据库实例 + */ + private void createSystemFolder(SQLiteDatabase db) { + ContentValues values = new ContentValues(); + + /** + * 通话记录文件夹,用于存放通话记录便签 + */ + values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + + /** + * 根文件夹,默认文件夹 + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + + /** + * 临时文件夹,用于移动便签 + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + + /** + * 创建回收站文件夹 + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + } + + /** + * 创建数据表 + * @param db 数据库实例 + */ + public void createDataTable(SQLiteDatabase db) { + db.execSQL(CREATE_DATA_TABLE_SQL); + reCreateDataTableTriggers(db); + db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL); + Log.d(TAG, "data table has been created"); + } + + /** + * 重新创建数据表的触发器 + * @param db 数据库实例 + */ + private void reCreateDataTableTriggers(SQLiteDatabase db) { + db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete"); + + db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER); + db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER); + db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); + } + + /** + * 获取单例实例 + * @param context 上下文 + * @return NotesDatabaseHelper实例 + */ + static synchronized NotesDatabaseHelper getInstance(Context context) { + if (mInstance == null) { + mInstance = new NotesDatabaseHelper(context); + } + return mInstance; + } + + /** + * 数据库初次创建时调用 + * @param db 数据库实例 + */ + @Override + public void onCreate(SQLiteDatabase db) { + createNoteTable(db); + createDataTable(db); + } + + /** + * 数据库版本升级时调用 + * @param db 数据库实例 + * @param oldVersion 旧版本号 + * @param newVersion 新版本号 + */ + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + boolean reCreateTriggers = false; + boolean skipV2 = false; + + // 升级到版本2 + if (oldVersion == 1) { + upgradeToV2(db); + skipV2 = true; // 这次升级包含了从v2到v3的升级 + oldVersion++; + } + + // 升级到版本3 + if (oldVersion == 2 && !skipV2) { + upgradeToV3(db); + reCreateTriggers = true; + oldVersion++; + } + + // 升级到版本4 + if (oldVersion == 3) { + upgradeToV4(db); + oldVersion++; + } + + // 重新创建触发器 + if (reCreateTriggers) { + reCreateNoteTableTriggers(db); + reCreateDataTableTriggers(db); + } + + // 检查升级是否成功 + if (oldVersion != newVersion) { + throw new IllegalStateException("Upgrade notes database to version " + newVersion + + "fails"); + } + } + + /** + * 升级到版本2 + * @param db 数据库实例 + */ + private void upgradeToV2(SQLiteDatabase db) { + db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); + db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA); + createNoteTable(db); + createDataTable(db); + } + + /** + * 升级到版本3 + * @param db 数据库实例 + */ + private void upgradeToV3(SQLiteDatabase db) { + // 删除未使用的触发器 + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update"); + + // 添加Google任务ID列 + db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID + + " TEXT NOT NULL DEFAULT ''"); + + // 添加回收站系统文件夹 + ContentValues values = new ContentValues(); + values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + } + + /** + * 升级到版本4 + * @param db 数据库实例 + */ + private void upgradeToV4(SQLiteDatabase db) { + // 添加版本号列 + db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION + + " INTEGER NOT NULL DEFAULT 0"); + } +} \ No newline at end of file diff --git a/src/data/NotesProvider.java b/src/data/NotesProvider.java new file mode 100644 index 0000000..072f183 --- /dev/null +++ b/src/data/NotesProvider.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.data; + +import android.app.SearchManager; +import android.content.ContentProvider; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Intent; +import android.content.UriMatcher; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.R; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.data.NotesDatabaseHelper.TABLE; + +/** + * NotesProvider类:提供便签数据的内容提供者 + * 负责处理对便签数据的查询、插入、更新和删除操作 + * 支持标准数据操作和搜索建议功能 + */ +public class NotesProvider extends ContentProvider { + // URI匹配器,用于识别不同类型的URI请求 + private static final UriMatcher mMatcher; + + // 数据库帮助类实例 + private NotesDatabaseHelper mHelper; + + // 日志标签 + private static final String TAG = "NotesProvider"; + + // URI匹配码常量 + private static final int URI_NOTE = 1; // 匹配所有便签 + private static final int URI_NOTE_ITEM = 2; // 匹配单个便签 + private static final int URI_DATA = 3; // 匹配所有数据 + private static final int URI_DATA_ITEM = 4; // 匹配单个数据 + private static final int URI_SEARCH = 5; // 匹配搜索请求 + private static final int URI_SEARCH_SUGGEST = 6; // 匹配搜索建议请求 + + // 初始化URI匹配器 + static { + mMatcher = new UriMatcher(UriMatcher.NO_MATCH); + mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); + mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); + mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); + mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM); + mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH); + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST); + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST); + } + + /** + * 便签搜索投影:定义搜索结果的列 + * x'0A'表示SQLite中的'\n'字符,为了在搜索结果中显示更多信息,会去除换行符和空白 + */ + private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + "," + + NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + "," + + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + "," + + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + "," + + R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + "," + + "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + "," + + "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA; + + // 便签摘要搜索查询SQL + private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION + + " FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.SNIPPET + " LIKE ?" + + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + + " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; + + /** + * 初始化内容提供者 + * @return 初始化成功返回true + */ + @Override + public boolean onCreate() { + mHelper = NotesDatabaseHelper.getInstance(getContext()); + return true; + } + + /** + * 查询数据 + * @param uri 查询的URI + * @param projection 需要返回的列 + * @param selection 查询条件 + * @param selectionArgs 查询条件参数 + * @param sortOrder 排序方式 + * @return 返回查询结果的Cursor + */ + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + Cursor c = null; + SQLiteDatabase db = mHelper.getReadableDatabase(); + String id = null; + + // 根据URI匹配结果执行不同的查询操作 + switch (mMatcher.match(uri)) { + case URI_NOTE: + // 查询所有便签 + c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null, + sortOrder); + break; + case URI_NOTE_ITEM: + // 查询单个便签 + id = uri.getPathSegments().get(1); + c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id + + parseSelection(selection), selectionArgs, null, null, sortOrder); + break; + case URI_DATA: + // 查询所有数据 + c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null, + sortOrder); + break; + case URI_DATA_ITEM: + // 查询单个数据 + id = uri.getPathSegments().get(1); + c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id + + parseSelection(selection), selectionArgs, null, null, sortOrder); + break; + case URI_SEARCH: + case URI_SEARCH_SUGGEST: + // 处理搜索和搜索建议请求 + if (sortOrder != null || projection != null) { + throw new IllegalArgumentException( + "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); + } + + String searchString = null; + if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { + if (uri.getPathSegments().size() > 1) { + searchString = uri.getPathSegments().get(1); + } + } else { + searchString = uri.getQueryParameter("pattern"); + } + + if (TextUtils.isEmpty(searchString)) { + return null; + } + + try { + // 执行搜索查询 + searchString = String.format("%%%s%%", searchString); + c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, + new String[] { searchString }); + } catch (IllegalStateException ex) { + Log.e(TAG, "got exception: " + ex.toString()); + } + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + // 设置通知URI,当数据变化时通知内容观察者 + if (c != null) { + c.setNotificationUri(getContext().getContentResolver(), uri); + } + return c; + } + + /** + * 插入数据 + * @param uri 插入的URI + * @param values 要插入的数据 + * @return 返回插入数据的URI + */ + @Override + public Uri insert(Uri uri, ContentValues values) { + SQLiteDatabase db = mHelper.getWritableDatabase(); + long dataId = 0, noteId = 0, insertedId = 0; + + // 根据URI匹配结果执行不同的插入操作 + switch (mMatcher.match(uri)) { + case URI_NOTE: + // 插入便签 + insertedId = noteId = db.insert(TABLE.NOTE, null, values); + break; + case URI_DATA: + // 插入数据 + if (values.containsKey(DataColumns.NOTE_ID)) { + noteId = values.getAsLong(DataColumns.NOTE_ID); + } else { + Log.d(TAG, "Wrong data format without note id:" + values.toString()); + } + insertedId = dataId = db.insert(TABLE.DATA, null, values); + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + // 通知数据变化 + if (noteId > 0) { + getContext().getContentResolver().notifyChange( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); + } + + if (dataId > 0) { + getContext().getContentResolver().notifyChange( + ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); + } + + return ContentUris.withAppendedId(uri, insertedId); + } + + /** + * 删除数据 + * @param uri 删除的URI + * @param selection 删除条件 + * @param selectionArgs 删除条件参数 + * @return 返回删除的行数 + */ + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + int count = 0; + String id = null; + SQLiteDatabase db = mHelper.getWritableDatabase(); + boolean deleteData = false; + + // 根据URI匹配结果执行不同的删除操作 + switch (mMatcher.match(uri)) { + case URI_NOTE: + // 删除便签 + selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; + count = db.delete(TABLE.NOTE, selection, selectionArgs); + break; + case URI_NOTE_ITEM: + // 删除单个便签 + id = uri.getPathSegments().get(1); + /** + * ID小于等于0的是系统文件夹,不允许删除 + */ + long noteId = Long.valueOf(id); + if (noteId <= 0) { + break; + } + count = db.delete(TABLE.NOTE, + NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs); + break; + case URI_DATA: + // 删除数据 + count = db.delete(TABLE.DATA, selection, selectionArgs); + deleteData = true; + break; + case URI_DATA_ITEM: + // 删除单个数据 + id = uri.getPathSegments().get(1); + count = db.delete(TABLE.DATA, + DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs); + deleteData = true; + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + // 通知数据变化 + if (count > 0) { + if (deleteData) { + getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); + } + getContext().getContentResolver().notifyChange(uri, null); + } + return count; + } + + /** + * 更新数据 + * @param uri 更新的URI + * @param values 要更新的数据 + * @param selection 更新条件 + * @param selectionArgs 更新条件参数 + * @return 返回更新的行数 + */ + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + int count = 0; + String id = null; + SQLiteDatabase db = mHelper.getWritableDatabase(); + boolean updateData = false; + + // 根据URI匹配结果执行不同的更新操作 + switch (mMatcher.match(uri)) { + case URI_NOTE: + // 更新便签版本并更新数据 + increaseNoteVersion(-1, selection, selectionArgs); + count = db.update(TABLE.NOTE, values, selection, selectionArgs); + break; + case URI_NOTE_ITEM: + // 更新单个便签版本并更新数据 + id = uri.getPathSegments().get(1); + increaseNoteVersion(Long.valueOf(id), selection, selectionArgs); + count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id + + parseSelection(selection), selectionArgs); + break; + case URI_DATA: + // 更新数据 + count = db.update(TABLE.DATA, values, selection, selectionArgs); + updateData = true; + break; + case URI_DATA_ITEM: + // 更新单个数据 + id = uri.getPathSegments().get(1); + count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id + + parseSelection(selection), selectionArgs); + updateData = true; + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + // 通知数据变化 + if (count > 0) { + if (updateData) { + getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); + } + getContext().getContentResolver().notifyChange(uri, null); + } + return count; + } + + /** + * 解析查询条件,用于构建SQL语句 + * @param selection 原始查询条件 + * @return 解析后的查询条件字符串 + */ + private String parseSelection(String selection) { + return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); + } + + /** + * 增加便签版本号 + * @param id 便签ID,如果为-1则更新所有符合条件的便签 + * @param selection 更新条件 + * @param selectionArgs 更新条件参数 + */ + private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { + StringBuilder sql = new StringBuilder(120); + sql.append("UPDATE "); + sql.append(TABLE.NOTE); + sql.append(" SET "); + sql.append(NoteColumns.VERSION); + sql.append("=" + NoteColumns.VERSION + "+1 "); + + if (id > 0 || !TextUtils.isEmpty(selection)) { + sql.append(" WHERE "); + } + if (id > 0) { + sql.append(NoteColumns.ID + "=" + String.valueOf(id)); + } + if (!TextUtils.isEmpty(selection)) { + String selectString = id > 0 ? parseSelection(selection) : selection; + for (String args : selectionArgs) { + selectString = selectString.replaceFirst("\\?", args); + } + sql.append(selectString); + } + + mHelper.getWritableDatabase().execSQL(sql.toString()); + } + + /** + * 获取URI对应的MIME类型 + * @param uri 请求的URI + * @return MIME类型字符串 + */ + @Override + public String getType(Uri uri) { + // TODO Auto-generated method stub + return null; + } +} \ No newline at end of file diff --git a/src/tool/BackupUtils.java b/src/tool/BackupUtils.java deleted file mode 100644 index a89566b..0000000 --- a/src/tool/BackupUtils.java +++ /dev/null @@ -1,408 +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.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; - -/** - * 备份工具类,用于将笔记导出为文本文件 - * 支持导出所有笔记、文件夹和通话记录,并按照指定格式保存到SD卡 - */ -public class BackupUtils { - private static final String TAG = "BackupUtils"; - // 单例模式实例 - private static BackupUtils sInstance; - - /** - * 获取BackupUtils的单例实例 - * - * @param context 应用上下文 - * @return 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; - - private BackupUtils(Context context) { - mTextExport = new TextExport(context); - } - - /** - * 检查外部存储是否可用 - * - * @return 可用返回true,否则返回false - */ - private static boolean externalStorageAvailable() { - return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); - } - - /** - * 导出笔记为文本文件 - * - * @return 操作状态码 - */ - public int exportToText() { - return mTextExport.exportToText(); - } - - /** - * 获取导出的文本文件名 - * - * @return 文件名 - */ - public String getExportedTextFileName() { - return mTextExport.mFileName; - } - - /** - * 获取导出的文本文件目录 - * - * @return 文件目录 - */ - public String getExportedTextFileDir() { - return mTextExport.mFileDirectory; - } - - /** - * 内部类:文本导出器,负责将笔记数据导出为文本格式 - */ - private static class TextExport { - // 笔记查询投影,指定要查询的列 - private static final String[] NOTE_PROJECTION = { - NoteColumns.ID, // 笔记ID - NoteColumns.MODIFIED_DATE, // 修改日期 - NoteColumns.SNIPPET, // 摘要 - NoteColumns.TYPE // 类型 - }; - - // 笔记投影列索引 - 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, // MIME类型 - DataColumns.DATA1, // 数据1(通话记录中存储通话日期) - DataColumns.DATA2, // 数据2 - DataColumns.DATA3, // 数据3 - DataColumns.DATA4, // 数据4(通话记录中存储电话号码) - }; - - // 数据投影列索引 - private static final int DATA_COLUMN_CONTENT = 0; - 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; // 导出的文件目录 - - public TextExport(Context context) { - // 从资源文件中获取导出格式数组 - TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); - mContext = context; - mFileName = ""; - mFileDirectory = ""; - } - - /** - * 获取指定ID的格式字符串 - * - * @param id 格式ID - * @return 格式字符串 - */ - 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)))); - // 查询属于该笔记的数据 - 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 { - 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 操作状态码 - */ - public int exportToText() { - // 检查外部存储是否可用 - if (!externalStorageAvailable()) { - Log.d(TAG, "Media was not mounted"); - 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)); - } - 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)))); - // 查询属于该笔记的数据 - 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"); - 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(); - return null; - } catch (NullPointerException e) { - e.printStackTrace(); - 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 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(); - } - - return null; - } -} \ No newline at end of file diff --git a/src/tool/DataUtils(1).java b/src/tool/DataUtils(1).java deleted file mode 100644 index 068accb..0000000 --- a/src/tool/DataUtils(1).java +++ /dev/null @@ -1,395 +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.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 ContentResolver对象 - * @param ids 要删除的笔记ID集合 - * @return 删除成功返回true,失败返回false - */ - public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { - if (ids == null) { - Log.d(TAG, "the ids is null"); - return true; - } - if (ids.size() == 0) { - Log.d(TAG, "no id is in the hashset"); - return true; - } - - ArrayList operationList = new ArrayList(); - for (long id : ids) { - 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) { - Log.d(TAG, "delete notes failed, ids:" + ids.toString()); - return false; - } - 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())); - } - return false; - } - - /** - * 将笔记移动到指定文件夹 - * - * @param resolver ContentResolver对象 - * @param id 要移动的笔记ID - * @param srcFolderId 源文件夹ID - * @param desFolderId 目标文件夹ID - */ - public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { - ContentValues values = new ContentValues(); - values.put(NoteColumns.PARENT_ID, desFolderId); - values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); - values.put(NoteColumns.LOCAL_MODIFIED, 1); - // 更新笔记的父文件夹信息 - resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); - } - - /** - * 批量将笔记移动到指定文件夹 - * - * @param resolver ContentResolver对象 - * @param ids 要移动的笔记ID集合 - * @param folderId 目标文件夹ID - * @return 移动成功返回true,失败返回false - */ - public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, - long folderId) { - if (ids == null) { - Log.d(TAG, "the ids is null"); - return true; - } - - ArrayList operationList = new ArrayList(); - for (long id : ids) { - // 构建更新操作 - ContentProviderOperation.Builder builder = ContentProviderOperation - .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); - builder.withValue(NoteColumns.PARENT_ID, folderId); - 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) { - Log.d(TAG, "delete notes failed, ids:" + ids.toString()); - return false; - } - 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())); - } - return false; - } - - /** - * 获取用户文件夹数量(不包括系统文件夹) - * - * @param resolver ContentResolver对象 - * @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); - - 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 ContentResolver对象 - * @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); - - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; - } - cursor.close(); - } - return exist; - } - - /** - * 检查笔记是否存在于数据库中 - * - * @param resolver ContentResolver对象 - * @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); - - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; - } - cursor.close(); - } - return exist; - } - - /** - * 检查数据项是否存在于数据库中 - * - * @param resolver ContentResolver对象 - * @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); - - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; - } - cursor.close(); - } - return exist; - } - - /** - * 检查可见文件夹中是否存在指定名称的文件夹 - * - * @param resolver ContentResolver对象 - * @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); - boolean exist = false; - if(cursor != null) { - if(cursor.getCount() > 0) { - exist = true; - } - cursor.close(); - } - return exist; - } - - /** - * 获取指定文件夹下的所有笔记小部件属性 - * - * @param resolver ContentResolver对象 - * @param folderId 文件夹ID - * @return 包含小部件属性的集合 - */ - 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); - - HashSet set = null; - if (c != null) { - if (c.moveToFirst()) { - set = new HashSet(); - do { - try { - AppWidgetAttribute widget = new AppWidgetAttribute(); - 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 ContentResolver对象 - * @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 ContentResolver对象 - * @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; - } - - /** - * 根据笔记ID获取摘要信息 - * - * @param resolver ContentResolver对象 - * @param noteId 笔记ID - * @return 摘要信息 - * @throws IllegalArgumentException 如果笔记不存在 - */ - 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); - } - - /** - * 格式化摘要信息(去除首尾空格,截取第一行) - * - * @param snippet 原始摘要 - * @return 格式化后的摘要 - */ - 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; - } -} \ No newline at end of file diff --git a/src/tool/GTaskStringUtils(1).java b/src/tool/GTaskStringUtils(1).java deleted file mode 100644 index afe38fc..0000000 --- a/src/tool/GTaskStringUtils(1).java +++ /dev/null @@ -1,168 +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.tool; - -/** - * Google Tasks同步相关的字符串常量定义类 - * 包含JSON数据结构中的字段名、文件夹名称、元数据标识等常量 - */ -public class GTaskStringUtils { - - // ====== JSON请求/响应字段名 ====== - - /** 操作ID,用于标识特定操作 */ - public final static String GTASK_JSON_ACTION_ID = "action_id"; - - /** 操作列表,包含多个操作的数组 */ - public final static String GTASK_JSON_ACTION_LIST = "action_list"; - - /** 操作类型字段 */ - public final static String GTASK_JSON_ACTION_TYPE = "action_type"; - - /** 创建操作类型值 */ - public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; - - /** 获取所有数据操作类型值 */ - public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; - - /** 移动操作类型值 */ - public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; - - /** 更新操作类型值 */ - public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; - - /** 创建者ID字段 */ - public final static String GTASK_JSON_CREATOR_ID = "creator_id"; - - /** 子实体字段 */ - public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; - - /** 客户端版本字段 */ - public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; - - /** 完成状态字段 */ - public final static String GTASK_JSON_COMPLETED = "completed"; - - /** 当前列表ID字段 */ - public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; - - /** 默认列表ID字段 */ - public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; - - /** 删除状态字段 */ - public final static String GTASK_JSON_DELETED = "deleted"; - - /** 目标列表字段 */ - public final static String GTASK_JSON_DEST_LIST = "dest_list"; - - /** 目标父级ID字段 */ - public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; - - /** 目标父级类型字段 */ - public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; - - /** 实体差异字段,用于增量同步 */ - public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; - - /** 实体类型字段 */ - public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; - - /** 获取已删除项的标识字段 */ - public final static String GTASK_JSON_GET_DELETED = "get_deleted"; - - /** ID字段 */ - public final static String GTASK_JSON_ID = "id"; - - /** 索引位置字段 */ - public final static String GTASK_JSON_INDEX = "index"; - - /** 最后修改时间字段 */ - public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; - - /** 最新同步点字段 */ - public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; - - /** 列表ID字段 */ - public final static String GTASK_JSON_LIST_ID = "list_id"; - - /** 列表集合字段 */ - public final static String GTASK_JSON_LISTS = "lists"; - - /** 名称字段 */ - public final static String GTASK_JSON_NAME = "name"; - - /** 新ID字段,用于创建操作返回 */ - public final static String GTASK_JSON_NEW_ID = "new_id"; - - /** 备注字段 */ - public final static String GTASK_JSON_NOTES = "notes"; - - /** 父级ID字段 */ - public final static String GTASK_JSON_PARENT_ID = "parent_id"; - - /** 前一个兄弟节点ID字段 */ - public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; - - /** 结果字段 */ - public final static String GTASK_JSON_RESULTS = "results"; - - /** 源列表字段 */ - public final static String GTASK_JSON_SOURCE_LIST = "source_list"; - - /** 任务集合字段 */ - public final static String GTASK_JSON_TASKS = "tasks"; - - /** 类型字段 */ - public final static String GTASK_JSON_TYPE = "type"; - - /** 组类型值 */ - public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; - - /** 任务类型值 */ - public final static String GTASK_JSON_TYPE_TASK = "TASK"; - - /** 用户字段 */ - public final static String GTASK_JSON_USER = "user"; - - // ====== 文件夹相关常量 ====== - - /** MIUI便签专用文件夹前缀,用于标识由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 Tasks 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"; -} \ No newline at end of file diff --git a/src/tool/ResourceParser(1).java b/src/tool/ResourceParser(1).java deleted file mode 100644 index ec7cd8e..0000000 --- a/src/tool/ResourceParser(1).java +++ /dev/null @@ -1,294 +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.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 - * - * @param id 颜色ID(使用ResourceParser中定义的颜色常量) - * @return 背景资源ID - */ - public static int getNoteBgResource(int id) { - return BG_EDIT_RESOURCES[id]; - } - - /** - * 获取指定颜色的便签编辑界面标题背景资源ID - * - * @param id 颜色ID(使用ResourceParser中定义的颜色常量) - * @return 标题背景资源ID - */ - public static int getNoteTitleBgResource(int id) { - return BG_EDIT_TITLE_RESOURCES[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 - * - * @param id 颜色ID - * @return 背景资源ID - */ - public static int getNoteBgFirstRes(int id) { - return BG_FIRST_RESOURCES[id]; - } - - /** - * 获取列表中最后一个便签的背景资源ID - * - * @param id 颜色ID - * @return 背景资源ID - */ - public static int getNoteBgLastRes(int id) { - return BG_LAST_RESOURCES[id]; - } - - /** - * 获取列表中单独便签的背景资源ID - * - * @param id 颜色ID - * @return 背景资源ID - */ - public static int getNoteBgSingleRes(int id) { - return BG_SINGLE_RESOURCES[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, - }; - - /** - * 获取2x尺寸小部件的背景资源ID - * - * @param id 颜色ID - * @return 背景资源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 - }; - - /** - * 获取4x尺寸小部件的背景资源ID - * - * @param id 颜色ID - * @return 背景资源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的文本外观资源 - * - * @param id 文本大小ID(使用ResourceParser中定义的文本大小常量) - * @return 文本外观资源ID - */ - public static int getTexAppearanceResource(int id) { - /** - * HACKME: 修复在共享偏好中存储资源ID的问题。 - * 如果ID大于资源数组长度,返回默认字体大小 - */ - 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