Type: INTEGER (long)
- */ + //用于查询所有笔记的统一资源标识符 + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/note"); + + //常量表示笔记的唯一标识符 + //这个常量用于在查询笔记时指定要查询的列 public static final String ID = "_id"; - /** - * The parent's id for note or folder - *Type: INTEGER (long)
- */ + //常量表示笔记的标题 public static final String PARENT_ID = "parent_id"; - /** - * Created data for note or folder - *Type: INTEGER (long)
- */ + //常量表示笔记的创建日期 public static final String CREATED_DATE = "created_date"; - /** - * Latest modified date - *Type: INTEGER (long)
- */ + //常量表示笔记的修改日期 public static final String MODIFIED_DATE = "modified_date"; - - /** - * Alert date - *Type: INTEGER (long)
- */ + //常量表示笔记的提醒日期 public static final String ALERTED_DATE = "alert_date"; - /** - * Folder's name or text content of note - *Type: TEXT
- */ public static final String SNIPPET = "snippet"; - /** - * Note's widget id - *Type: INTEGER (long)
- */ + //常量表示部件的ID public static final String WIDGET_ID = "widget_id"; - /** - * Note's widget type - *Type: INTEGER (long)
- */ + //常量表示部件的类型 public static final String WIDGET_TYPE = "widget_type"; - /** - * Note's background color's id - *Type: INTEGER (long)
- */ + //常量表示笔记的背景颜色ID public static final String BG_COLOR_ID = "bg_color_id"; - /** - * For text note, it doesn't has attachment, for multi-media - * note, it has at least one attachment - *Type: INTEGER
- */ + //常量表示有附件的标志 public static final String HAS_ATTACHMENT = "has_attachment"; - /** - * Folder's count of notes - *Type: INTEGER (long)
- */ + //常量表示笔记的数量 public static final String NOTES_COUNT = "notes_count"; - /** - * The file type: folder or note - *Type: INTEGER
- */ + //常量表示笔记的类型 public static final String TYPE = "type"; - /** - * The last sync id - *Type: INTEGER (long)
- */ + //常量表示笔记的同步ID public static final String SYNC_ID = "sync_id"; - /** - * Sign to indicate local modified or not - *Type: INTEGER
- */ + //常量表示本地修改的标志 public static final String LOCAL_MODIFIED = "local_modified"; - /** - * Original parent id before moving into temporary folder - *Type : INTEGER
- */ + //常量表示原始父ID public static final String ORIGIN_PARENT_ID = "origin_parent_id"; - /** - * The gtask id - *Type : TEXT
- */ + //常量表示Google任务的ID public static final String GTASK_ID = "gtask_id"; - /** - * The version code - *Type : INTEGER (long)
- */ + //常量表示版本号 public static final String VERSION = "version"; } public interface DataColumns { - /** - * The unique ID for a row - *Type: INTEGER (long)
- */ + //用于查询所有数据的统一资源标识符 public static final String ID = "_id"; - /** - * The MIME type of the item represented by this row. - *Type: Text
- */ + //数据的MIME类型 public static final String MIME_TYPE = "mime_type"; - /** - * The reference id to note that this data belongs to - *Type: INTEGER (long)
- */ + //笔记的父ID public static final String NOTE_ID = "note_id"; - /** - * Created data for note or folder - *Type: INTEGER (long)
- */ + //笔记的创建日期 public static final String CREATED_DATE = "created_date"; - /** - * Latest modified date - *Type: INTEGER (long)
- */ + //笔记的修改日期 public static final String MODIFIED_DATE = "modified_date"; - /** - * Data's content - *Type: TEXT
- */ + //笔记的内容 public static final String CONTENT = "content"; @@ -241,6 +185,7 @@ public class Notes { public static final String DATA5 = "data5"; } + //用来存储一些静态常量 public static final class TextNote implements DataColumns { /** * Mode to indicate the text in check list mode or not @@ -257,6 +202,7 @@ public class Notes { public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); } + //用来存储一些静态常量 public static final class CallNote implements DataColumns { /** * Call date for this record diff --git a/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java b/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java index ffe5d57..bd7043b 100644 --- a/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java +++ b/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java @@ -27,6 +27,7 @@ import net.micode.notes.data.Notes.DataConstants; import net.micode.notes.data.Notes.NoteColumns; +//笔记数据库的数据库辅助类,用于创建和升级数据库 public class NotesDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "note.db"; @@ -42,6 +43,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { private static NotesDatabaseHelper mInstance; + //创建笔记表的SQL语句,用于存储笔记 private static final String CREATE_NOTE_TABLE_SQL = "CREATE TABLE " + TABLE.NOTE + "(" + NoteColumns.ID + " INTEGER PRIMARY KEY," + @@ -63,6 +65,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { 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," + @@ -78,13 +81,12 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + ")"; + //创建索引,用于提高查询效率 private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = "CREATE INDEX IF NOT EXISTS note_id_index ON " + TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; - /** - * Increase folder's note count when move note to the folder - */ + //将笔记移动到文件夹时,增加该文件夹的笔记数量 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 + @@ -94,9 +96,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + " END"; - /** - * Decrease folder's note count when move note from folder - */ + //将笔记移动到文件夹时,减少该文件夹的笔记数量 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 + @@ -107,9 +107,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " AND " + NoteColumns.NOTES_COUNT + ">0" + ";" + " END"; - /** - * Increase folder's note count when insert new note to the folder - */ + //将笔记移动到文件夹时,增加该文件夹的笔记数量 private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = "CREATE TRIGGER increase_folder_count_on_insert " + " AFTER INSERT ON " + TABLE.NOTE + @@ -119,9 +117,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + " END"; - /** - * Decrease folder's note count when delete note from the folder - */ + //将笔记移动到文件夹时,减少该文件夹的笔记数量 private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_delete " + " AFTER DELETE ON " + TABLE.NOTE + @@ -132,9 +128,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " AND " + NoteColumns.NOTES_COUNT + ">0;" + " END"; - /** - * Update note's content when insert data with type {@link DataConstants#NOTE} - */ + //当插入类型为{@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 + @@ -145,9 +139,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + " END"; - /** - * Update note's content when data with {@link DataConstants#NOTE} type has changed - */ + //当类型为{@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 + @@ -158,9 +150,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + " END"; - /** - * Update note's content when data with {@link DataConstants#NOTE} type has deleted - */ + //当类型为{@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 + @@ -171,9 +161,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" + " END"; - /** - * Delete datas belong to note which has been deleted - */ + //删除笔记时,删除对应的笔记数据 private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER = "CREATE TRIGGER delete_data_on_delete " + " AFTER DELETE ON " + TABLE.NOTE + @@ -182,9 +170,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" + " END"; - /** - * Delete notes belong to folder which has been deleted - */ + //删除文件夹时,删除对应的笔记 private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = "CREATE TRIGGER folder_delete_notes_on_delete " + " AFTER DELETE ON " + TABLE.NOTE + @@ -193,9 +179,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + " END"; - /** - * Move notes belong to folder which has been moved to trash folder - */ + //将文件夹移动到回收站时,将该文件夹下的笔记移动到回收站 private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER = "CREATE TRIGGER folder_move_notes_on_trash " + " AFTER UPDATE ON " + TABLE.NOTE + @@ -210,6 +194,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { super(context, DB_NAME, null, DB_VERSION); } + //创建数据库 public void createNoteTable(SQLiteDatabase db) { db.execSQL(CREATE_NOTE_TABLE_SQL); reCreateNoteTableTriggers(db); @@ -217,6 +202,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { Log.d(TAG, "note table has been created"); } + //重新创建触发器 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"); @@ -235,41 +221,35 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER); } + //创建系统文件夹 private void createSystemFolder(SQLiteDatabase db) { ContentValues values = new ContentValues(); - /** - * call record foler for call notes - */ + //通话记录文件夹,用于通话记录笔记 values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); - /** - * root folder which is default folder - */ + //根文件夹,用于存储所有笔记 values.clear(); values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); - /** - * temporary folder which is used for moving note - */ + //临时文件夹,用于存储没有文件夹的笔记 values.clear(); values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); - /** - * create trash folder - */ + //回收站文件夹,用于存储删除的笔记 values.clear(); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); } + //创建数据表 public void createDataTable(SQLiteDatabase db) { db.execSQL(CREATE_DATA_TABLE_SQL); reCreateDataTableTriggers(db); @@ -277,6 +257,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { Log.d(TAG, "data table has been created"); } + //重新创建触发器 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"); @@ -287,6 +268,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); } + //获取数据库实例 static synchronized NotesDatabaseHelper getInstance(Context context) { if (mInstance == null) { mInstance = new NotesDatabaseHelper(context); @@ -294,12 +276,14 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { return mInstance; } + //创建数据库 @Override public void onCreate(SQLiteDatabase db) { createNoteTable(db); createDataTable(db); } + //升级数据库 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { boolean reCreateTriggers = false; @@ -307,7 +291,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { if (oldVersion == 1) { upgradeToV2(db); - skipV2 = true; // this upgrade including the upgrade from v2 to v3 + skipV2 = true; //此次升级跳过V2 oldVersion++; } @@ -333,6 +317,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { } } + //升级到V2 private void upgradeToV2(SQLiteDatabase db) { db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA); @@ -340,21 +325,23 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { createDataTable(db); } + //升级到V3 private void upgradeToV3(SQLiteDatabase db) { - // drop unused triggers + //删除未使用的触发器 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"); - // add a column for gtask id + //添加一个用于gtask id的列 db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''"); - // add a trash system folder + //添加一个垃圾系统文件夹 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); } + //升级到V4 private void upgradeToV4(SQLiteDatabase db) { db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0"); diff --git a/app/src/main/java/net/micode/notes/data/NotesProvider.java b/app/src/main/java/net/micode/notes/data/NotesProvider.java index edb0a60..879c563 100644 --- a/app/src/main/java/net/micode/notes/data/NotesProvider.java +++ b/app/src/main/java/net/micode/notes/data/NotesProvider.java @@ -35,6 +35,8 @@ import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.NotesDatabaseHelper.TABLE; +//这个类是一个内容提供者,用于提供笔记和文件夹的数据 +//它继承自ContentProvider类,并重写了它的方法,以实现对笔记和文件夹数据的增删改查操作 public class NotesProvider extends ContentProvider { private static final UriMatcher mMatcher; @@ -73,6 +75,14 @@ public class NotesProvider extends ContentProvider { + "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + "," + "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA; + //查询笔记的SQL语句 + //LIKE操作符来匹配笔记的标题或内容 + //INNER JOIN操作符来连接笔记和文件夹表 + //ORDER BY操作符来按照笔记的修改日期排序 + //LIMIT操作符来限制查询结果的数量 + //GROUP BY操作符来按照笔记的ID分组 + //HAVING操作符来过滤查询结果 + //UNION操作符来合并查询结果 private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION + " FROM " + TABLE.NOTE + " WHERE " + NoteColumns.SNIPPET + " LIKE ?" @@ -166,13 +176,13 @@ public class NotesProvider extends ContentProvider { default: throw new IllegalArgumentException("Unknown URI " + uri); } - // Notify the note uri + //通知笔记统一资源标识符 if (noteId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); } - // Notify the data uri + //通知数据统一资源标识符 if (dataId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); @@ -194,10 +204,7 @@ public class NotesProvider extends ContentProvider { break; case URI_NOTE_ITEM: id = uri.getPathSegments().get(1); - /** - * ID that smaller than 0 is system folder which is not allowed to - * trash - */ + //小于0的ID代表系统文件夹,不允许将其放入回收站 long noteId = Long.valueOf(id); if (noteId <= 0) { break; diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index eb9d448..dfa1e51 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -agp = "8.3.0-alpha08" +agp = "8.10.0" junit = "4.13.2" androidx-test-ext-junit = "1.1.5" espresso-core = "3.5.1" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 85a0ad2..823b0d8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,7 @@ #Sun May 11 23:35:16 CST 2025 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.7-bin.zip +distributionUrl=https\://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.11.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists android.nonFinalResIds=false \ No newline at end of file