diff --git a/src/java/net/micode/notes/data/Contact.java b/src/java/net/micode/notes/data/Contact.java index 878fe67..92953df 100644 --- a/src/java/net/micode/notes/data/Contact.java +++ b/src/java/net/micode/notes/data/Contact.java @@ -31,8 +31,8 @@ import java.util.HashMap; * @Package: net.micode.notes.data * @ClassName: Contact * @Description: 该类实现建立联系人数据库的功能 - * @Author: 郑鲲鹏 - * @Date: 2023-12-16 17:57 + * @Author: 齐景熙 + * @Date: 2024-12-20 19:42 */ public class Contact { private static HashMap sContactCache; @@ -44,11 +44,11 @@ public class Contact { + "(SELECT raw_contact_id " + " FROM phone_lookup" + " WHERE min_match = '+')"; - /** + /** * @method getContact * @description 该方法用于识别电话号码对应的联系人 - * @date: 2023-12-16 19:24 - * @author: 郑鲲鹏 + * @date: 2024-12-20 20:02 + * @author: 齐景熙 * @return string */ public static String getContact(Context context, String phoneNumber) { @@ -57,7 +57,7 @@ public class Contact { } // 查找HashMap中是否有phoneNumber的信息 - // 2023-12-16 19:43 + // 2024-12-20 20:04 if(sContactCache.containsKey(phoneNumber)) { return sContactCache.get(phoneNumber); } @@ -66,7 +66,7 @@ public class Contact { PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); // 查找数据库中phoneNumber的信息 - // 2023-12-16 19:43 + // 2024-12-20 20:06 Cursor cursor = context.getContentResolver().query( Data.CONTENT_URI, new String [] { Phone.DISPLAY_NAME }, @@ -75,7 +75,7 @@ public class Contact { null); // 检查是否查询到联系人,找到则将相关信息加入HashMap中,未找到则处理异常 - // 2023-12-16 19:41 + // 2024-12-20 20:07 if (cursor != null && cursor.moveToFirst()) { try { String name = cursor.getString(0); diff --git a/src/java/net/micode/notes/data/Notes.java b/src/java/net/micode/notes/data/Notes.java index d40776d..b7c39dc 100644 --- a/src/java/net/micode/notes/data/Notes.java +++ b/src/java/net/micode/notes/data/Notes.java @@ -14,6 +14,18 @@ * limitations under the License. */ + +/** + * + * @ProjectName: minode + * @Package: net.micode.notes.data + * @ClassName: Notes + * @Description: 该类主要定义了与笔记相关的常量、枚举类型、接口和嵌套类,用于描述笔记的基本属性、类型、系统文件夹标识符、Intent、 + 额外数据键、小部件类型等,以及提供查询笔记和数据的 URI。它为笔记应用提供了数据结构和访问接口的基础框架,使得笔记 + 的创建、管理、查询等功能得以实现。 + * @Author: 齐景熙 + * @Date: 2024-12-21 19:42 + */ package net.micode.notes.data; import android.net.Uri; @@ -46,6 +58,13 @@ public class Notes { public static final int TYPE_WIDGET_2X = 0; public static final int TYPE_WIDGET_4X = 1; + + /** + * @method DataConstants + * @description 该嵌套类定义了笔记数据的常量,主要用于表示笔记数据的 MIME 类型。 + * @date: 2024-12-21 20:02 + * @author: 齐景熙 + */ public static class DataConstants { public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; @@ -61,6 +80,16 @@ public class Notes { */ public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + + /** + * @method NoteColumns + * @description 该接口定义了笔记列的名称和类型,用于描述笔记的基本属性,如 ID、父 ID、创建日期、修改日期、 + 提醒日期、摘要、小部件 ID、小部件类型、背景颜色 ID、是否有附件、笔记数量、类型、同步 ID、 + 本地修改标志、原始父 ID、GTask ID、版本号、置顶状态等。这些列名称和类型为笔记数据的存储和查 + 询提供了标准的字段定义。 + * @date: 2024-12-21 20:04 + * @author: 齐景熙 + */ public interface NoteColumns { /** * The unique ID for a row @@ -173,6 +202,15 @@ public class Notes { public static final String TOP = "top"; } + + /** + * @method DataColumns + * @description 该接口定义了数据列的名称和类型,用于描述数据的基本属性,如 ID、MIME 类型、所属笔记的 + ID、创建日期、修改日期、数据内容、通用数据列等。这些列名称和类型为数据的存储和查询提 + 供了标准的字段定义。 + * @date: 2024-12-21 20:05 + * @author: 齐景熙 + */ public interface DataColumns { /** * The unique ID for a row @@ -247,6 +285,14 @@ public class Notes { public static final String DATA5 = "data5"; } + + /** + * @method TextNote + * @description 该嵌套类实现了 DataColumns 接口,定义了文本笔记的特定属性和 MIME 类型。 + 它用于描述文本笔记的模式(如清单模式或普通模式)以及文本笔记的 MIME 类型和 URI。 + * @date: 2024-12-21 20:07 + * @author: 齐景熙 + */ public static final class TextNote implements DataColumns { /** * Mode to indicate the text in check list mode or not @@ -263,6 +309,13 @@ public class Notes { public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); } + /** + * @method CallNote + * @description 该嵌套类实现了 DataColumns 接口,定义了通话记录笔记的特定属性和 MIME 类型。它用于描述通 + 话记录笔记的通话日期、电话号码以及通话记录笔记的 MIME 类型和 URI。 + * @date: 2024-12-21 20:10 + * @author: 齐景熙 + */ public static final class CallNote implements DataColumns { /** * Call date for this record diff --git a/src/java/net/micode/notes/data/NotesDatabaseHelper.java b/src/java/net/micode/notes/data/NotesDatabaseHelper.java index a96128b..4c52c4e 100644 --- a/src/java/net/micode/notes/data/NotesDatabaseHelper.java +++ b/src/java/net/micode/notes/data/NotesDatabaseHelper.java @@ -14,6 +14,18 @@ * limitations under the License. */ + + /** + * + * @ProjectName: minode + * @Package: net.micode.notes.data + * @ClassName: NotesDatabaseHelper + * @Description: 该类继承自 SQLiteOpenHelper,用于创建和管理笔记应用的数据库。 + 它定义了数据库的名称、版本、表名、创建表和触发器的 SQL 语句,并 + 实现了数据库的创建、升级等操作。通过该类,可以方便地进行笔记数据的存储和管理。 + * @Author: 齐景熙 + * @Date: 2024-12-21 22:10 + */ package net.micode.notes.data; import android.content.ContentValues; @@ -207,10 +219,25 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + " END"; + + /** + * @method NotesDatabaseHelper + * @description 构造函数,用于初始化 NotesDatabaseHelper 实例。调用父类的构造函数,传入上下文、 + 数据库名称、工厂和数据库版本号,完成数据库帮助者的创建。 + * @date: 2024-12-21 22:10 + * @author: 齐景熙 + */ public NotesDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } + /** + * @method createNoteTable + * @description 创建笔记表。执行创建笔记表的 SQL 语句,并调用 reCreateNoteTableTriggers 方法重新 + 创建笔记表的触发器,最后创建系统文件夹。 + * @date: 2024-12-21 22:13 + * @author: 齐景熙 + */ public void createNoteTable(SQLiteDatabase db) { db.execSQL(CREATE_NOTE_TABLE_SQL); reCreateNoteTableTriggers(db); @@ -218,6 +245,14 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { Log.d(TAG, "note table has been created"); } + + /** + * @method reCreateNoteTableTriggers + * @description 重新创建笔记表的触发器。先删除已存在的触发器,然后依次创建增加文件夹笔记数量、减少文件 + 夹笔记数量、删除笔记数据、移动笔记到回收站等触发器。 + * @date: 2024-12-21 22:15 + * @author: 齐景熙 + */ 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"); @@ -236,6 +271,13 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER); } + /** + * @method createSystemFolder + * @description 创建系统文件夹。使用 ContentValues 设置系统文件夹的 ID 和类型,然后依次插入通话记录 + 文件夹、根文件夹、临时文件夹和回收站文件夹到笔记表中。 + * @date: 2024-12-21 22:20 + * @author: 齐景熙 + */ private void createSystemFolder(SQLiteDatabase db) { ContentValues values = new ContentValues(); @@ -271,6 +313,12 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.insert(TABLE.NOTE, null, values); } + /** + * @method createDataTable + * @description 创建数据表。执行创建数据表的 SQL 语句,调用 reCreateDataTableTriggers 方法重新创建数据表的触发器,并创建数据表的索引。 + * @date: 2024-12-21 22:23 + * @author: 齐景熙 + */ public void createDataTable(SQLiteDatabase db) { db.execSQL(CREATE_DATA_TABLE_SQL); reCreateDataTableTriggers(db); @@ -278,6 +326,13 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { Log.d(TAG, "data table has been created"); } + + /** + * @method reCreateDataTableTriggers + * @description 重新创建数据表的触发器。先删除已存在的触发器,然后依次创建更新笔记内容的触发器。 + * @date: 2024-12-21 22:25 + * @author: 齐景熙 + */ 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"); @@ -288,6 +343,12 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); } + /** + * @method getInstance + * @description 获取 NotesDatabaseHelper 的单例实例。如果实例不存在,则创建一个新的实例;否则,返回已存在的实例。 + * @date: 2024-12-21 22:30 + * @author: 齐景熙 + */ static synchronized NotesDatabaseHelper getInstance(Context context) { if (mInstance == null) { mInstance = new NotesDatabaseHelper(context); @@ -295,12 +356,27 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { return mInstance; } + + /** + * @method onCreate + * @description 数据库创建时的回调方法。调用 createNoteTable 和 createDataTable 方法创建笔记表和数据表。 + * @date: 2024-12-21 22:40 + * @author: 齐景熙 + */ @Override public void onCreate(SQLiteDatabase db) { createNoteTable(db); createDataTable(db); } + + /** + * @method onUpgrade + * @description 数据库版本升级时的回调方法。根据旧版本号和新版本号,依次调用相应的升级方法 + (如 upgradeToV2、upgradeToV3 等),并在需要时重新创建触发器。如果旧版本号和新版本号不匹配,则抛出异常。 + * @date: 2024-12-21 22:47 + * @author: 齐景熙 + */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { boolean reCreateTriggers = false; @@ -339,6 +415,13 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { } } + + /** + * @method upgradeToV2 + * @description 升级数据库到版本 2。删除旧的笔记表和数据表,然后重新创建笔记表和数据表。 + * @date: 2024-12-21 22:51 + * @author: 齐景熙 + */ private void upgradeToV2(SQLiteDatabase db) { db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA); @@ -346,6 +429,13 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { createDataTable(db); } + + /** + * @method upgradeToV3 + * @description 升级数据库到版本 3。删除不再使用的触发器,为笔记表添加 gtask_id 列,并创建回收站系统文件夹。 + * @date: 2024-12-21 22:55 + * @author: 齐景熙 + */ private void upgradeToV3(SQLiteDatabase db) { // drop unused triggers db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); @@ -361,11 +451,25 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.insert(TABLE.NOTE, null, values); } + + /** + * @method upgradeToV4 + * @description 升级数据库到版本 4。为笔记表添加 version 列。 + * @date: 2024-12-21 22:57 + * @author: 齐景熙 + */ private void upgradeToV4(SQLiteDatabase db) { db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0"); } + + /** + * @method upgradeToV5 + * @description 升级数据库到版本 5。为笔记表添加 top 列。 + * @date: 2024-12-21 23:00 + * @author: 齐景熙 + */ private void upgradeToV5(SQLiteDatabase db) { db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.TOP + " INTEGER NOT NULL DEFAULT 0"); diff --git a/src/java/net/micode/notes/data/NotesProvider.java b/src/java/net/micode/notes/data/NotesProvider.java index 8b23d6e..d26d815 100644 --- a/src/java/net/micode/notes/data/NotesProvider.java +++ b/src/java/net/micode/notes/data/NotesProvider.java @@ -14,6 +14,18 @@ * limitations under the License. */ + + /** + * + * @ProjectName: minode + * @Package: net.micode.notes.data + * @ClassName: NotesProvider + * @Description: 该类继承自 ContentProvider,用于实现笔记数据的访问和管理。它定义了 URI 匹配器、数据库帮助者 + 等成员变量,并重写了 ContentProvider 的主要方法,如 onCreate、query、insert、delete、update 等, + 实现了对笔记和数据表的查询、插入、删除和更新操作。通过该类,其他组件可以方便地访问和操作笔记数据。 + * @Author: 齐景熙 + * @Date: 2024-12-21 23:10 + */ package net.micode.notes.data; @@ -34,6 +46,13 @@ import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.NotesDatabaseHelper.TABLE; +/** + * @method NotesProvider + * @description 默认构造函数,无特殊功能。 + * @date: 2024-12-21 23:12 + * @author: 齐景熙 + */ + public class NotesProvider extends ContentProvider { private static final UriMatcher mMatcher; @@ -80,12 +99,27 @@ public class NotesProvider extends ContentProvider { + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; + + +/** + * @method onCreate + * @description ContentProvider 创建时的回调方法。获取上下文,调用 NotesDatabaseHelper.getInstance + 方法创建数据库帮助者对象 mHelper,并返回 true 表示创建成功。 + * @date: 2024-12-21 23:13 + * @author: 齐景熙 + */ @Override public boolean onCreate() { mHelper = NotesDatabaseHelper.getInstance(getContext()); return true; } +/** + * @method query + * @description 查询数据。根据匹配的 URI,执行不同的查询操作 + * @date: 2024-12-21 23:15 + * @author: 齐景熙 + */ @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { @@ -147,7 +181,12 @@ public class NotesProvider extends ContentProvider { } return c; } - +/** + * @method insert + * @description 插入数据。根据匹配的 URI,执行不同的插入操作 + * @date: 2024-12-21 23:17 + * @author: 齐景熙 + */ @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = mHelper.getWritableDatabase(); @@ -182,6 +221,12 @@ public class NotesProvider extends ContentProvider { return ContentUris.withAppendedId(uri, insertedId); } +/** + * @method delete + * @description 删除数据。根据匹配的 URI,执行不同的删除操作 + * @date: 2024-12-21 23:20 + * @author: 齐景熙 + */ @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0; @@ -227,7 +272,12 @@ public class NotesProvider extends ContentProvider { } return count; } - +/** + * @method update + * @description 更新数据。根据匹配的 URI,执行不同的更新操作 + * @date: 2024-12-21 23:22 + * @author: 齐景熙 + */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count = 0; @@ -267,11 +317,23 @@ public class NotesProvider extends ContentProvider { } return count; } - +/** + * @method parseSelection + * @description 解析查询条件。如果查询条件不为空,则返回包含查询条件的字符串;否则返回空字符串。 + * @date: 2024-12-21 23:25 + * @author: 齐景熙 + */ private String parseSelection(String selection) { return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } + + /** + * @method increaseNoteVersion + * @description 增加笔记版本号。根据笔记 ID 或查询条件,更新笔记表中相应笔记的版本号。 + * @date: 2024-12-21 23:27 + * @author: 齐景熙 + */ private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { StringBuilder sql = new StringBuilder(120); sql.append("UPDATE "); @@ -297,6 +359,13 @@ public class NotesProvider extends ContentProvider { mHelper.getWritableDatabase().execSQL(sql.toString()); } + + /** + * @method getType + * @description 获取 MIME 类型。该方法未实现,返回 null + * @date: 2024-12-21 23:30 + * @author: 齐景熙 + */ @Override public String getType(Uri uri) { // TODO Auto-generated method stub