diff --git a/src/Notesmaster/app/build.gradle.kts b/src/Notesmaster/app/build.gradle.kts index a7418a0..7117999 100644 --- a/src/Notesmaster/app/build.gradle.kts +++ b/src/Notesmaster/app/build.gradle.kts @@ -51,9 +51,9 @@ dependencies { // "exclude" to listOf("") // ))) //修改为如下代码: - implementation(files("D:\\code\\abdroid\\Notesmaster\\httpcomponents-client-4.5.14-bin\\lib\\httpclient-osgi-4.5.14.jar")) - implementation(files("D:\\code\\abdroid\\Notesmaster\\httpcomponents-client-4.5.14-bin\\lib\\httpclient-win-4.5.14.jar")) - implementation(files("D:\\code\\abdroid\\Notesmaster\\httpcomponents-client-4.5.14-bin\\lib\\httpcore-4.4.16.jar")) + implementation(files("D:\\code\\AndroidCode\\Notesmaster\\httpcomponents-client-4.5.14-bin\\lib\\httpclient-osgi-4.5.14.jar")) + implementation(files("D:\\code\\AndroidCode\\Notesmaster\\httpcomponents-client-4.5.14-bin\\lib\\httpclient-win-4.5.14.jar")) + implementation(files("D:\\code\\AndroidCode\\Notesmaster\\httpcomponents-client-4.5.14-bin\\lib\\httpcore-4.4.16.jar")) testImplementation(libs.junit) androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.espresso.core) diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/data/Contact.java b/src/Notesmaster/app/src/main/java/net/micode/notes/data/Contact.java index d97ac5d..138127c 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/data/Contact.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/data/Contact.java @@ -7,7 +7,7 @@ * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software + * Unless required by 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 @@ -26,48 +26,62 @@ import android.util.Log; import java.util.HashMap; public class Contact { + // 用于缓存联系人信息的 HashMap,键为电话号码,值为联系人姓名 private static HashMap sContactCache; + // 日志标签 private static final String TAG = "Contact"; + // 查询联系人的 SQL 语句,使用 PHONE_NUMBERS_EQUAL 函数进行电话号码匹配,同时限定数据类型为电话,并且关联的 raw_contact_id 在 phone_lookup 表中 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 " + + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + + " AND " + Data.RAW_CONTACT_ID + " IN " + "(SELECT raw_contact_id " + " FROM phone_lookup" + " WHERE min_match = '+')"; + // 根据电话号码获取联系人姓名的静态方法 public static String getContact(Context context, String phoneNumber) { - if(sContactCache == null) { + // 若缓存为空,则创建一个新的 HashMap 用于存储联系人信息 + if (sContactCache == null) { sContactCache = new HashMap(); } - if(sContactCache.containsKey(phoneNumber)) { + // 若缓存中已经包含该电话号码,直接从缓存中获取联系人姓名 + if (sContactCache.containsKey(phoneNumber)) { return sContactCache.get(phoneNumber); } + // 替换 CALLER_ID_SELECTION 中的 "+" 为使用 PhoneNumberUtils 生成的最小匹配字符串,以便进行更准确的查询 String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); + // 使用 ContentResolver 查询联系人信息,查询 Data.CONTENT_URI,仅获取 DISPLAY_NAME 列,查询条件为 selection,查询参数为电话号码 Cursor cursor = context.getContentResolver().query( Data.CONTENT_URI, - new String [] { Phone.DISPLAY_NAME }, + new String[]{Phone.DISPLAY_NAME}, selection, - new String[] { phoneNumber }, + new String[]{phoneNumber}, null); - if (cursor != null && cursor.moveToFirst()) { + // 若游标不为空且有数据 + 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/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java b/src/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java index f240604..5b851e0 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java @@ -7,9 +7,9 @@ * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software + * Unless required by 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. + * 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. */ @@ -17,24 +17,29 @@ package net.micode.notes.data; import android.net.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; + // 定义不同类型的笔记 + public static final int TYPE_NOTE = 0; + public static final int TYPE_FOLDER = 1; + public static final int TYPE_SYSTEM = 2; /** - * Following IDs are system folders' identifiers - * {@link Notes#ID_ROOT_FOLDER } is default folder - * {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder - * {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records + * 以下是系统文件夹的标识符 + * {@link Notes#ID_ROOT_FOLDER } 是默认文件夹 + * {@link Notes#ID_TEMPARAY_FOLDER } 是用于没有所属文件夹的笔记 + * {@link Notes#ID_CALL_RECORD_FOLDER} 是存储通话记录的文件夹 */ public static final int ID_ROOT_FOLDER = 0; public static final int ID_TEMPARAY_FOLDER = -1; public static final int ID_CALL_RECORD_FOLDER = -2; public static final int ID_TRASH_FOLER = -3; + // 用于传递额外信息的 Intent 键 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"; public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; @@ -42,238 +47,236 @@ public class Notes { public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_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; - public static final int TYPE_WIDGET_4X = 1; + // 定义不同类型的部件 + public static final int TYPE_WIDGET_INVALIDE = -1; + public static final int TYPE_WIDGET_2X = 0; + public static final int TYPE_WIDGET_4X = 1; public static class DataConstants { + // 定义笔记和通话记录的内容项类型 public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; } /** - * Uri to query all notes and folders + * 用于查询所有笔记和文件夹的 Uri */ public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); /** - * Uri to query data + * 用于查询数据的 Uri */ public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); public interface NoteColumns { /** - * The unique ID for a row - *

Type: INTEGER (long)

+ * 行的唯一 ID + *

类型: INTEGER (long)

*/ public static final String ID = "_id"; /** - * The parent's id for note or folder - *

Type: INTEGER (long)

+ * 笔记或文件夹的父 ID + *

类型: INTEGER (long)

*/ public static final String PARENT_ID = "parent_id"; /** - * Created data for note or folder - *

Type: INTEGER (long)

+ * 笔记或文件夹的创建日期 + *

类型: INTEGER (long)

*/ public static final String CREATED_DATE = "created_date"; /** - * Latest modified date - *

Type: INTEGER (long)

+ * 最近修改日期 + *

类型: INTEGER (long)

*/ public static final String MODIFIED_DATE = "modified_date"; /** - * Alert date - *

Type: INTEGER (long)

+ * 提醒日期 + *

类型: INTEGER (long)

*/ public static final String ALERTED_DATE = "alert_date"; /** - * Folder's name or text content of note - *

Type: TEXT

+ * 文件夹的名称或笔记的文本内容 + *

类型: TEXT

*/ public static final String SNIPPET = "snippet"; /** - * Note's widget id - *

Type: INTEGER (long)

+ * 笔记的部件 ID + *

类型: INTEGER (long)

*/ public static final String WIDGET_ID = "widget_id"; /** - * Note's widget type - *

Type: INTEGER (long)

+ * 笔记的部件类型 + *

类型: INTEGER (long)

*/ public static final String WIDGET_TYPE = "widget_type"; /** - * Note's background color's id - *

Type: INTEGER (long)

+ * 笔记的背景颜色 ID + *

类型: INTEGER (long)

*/ 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

+ * 对于文本笔记,它没有附件,对于多媒体笔记,它至少有一个附件 + *

类型: INTEGER

*/ public static final String HAS_ATTACHMENT = "has_attachment"; /** - * Folder's count of notes - *

Type: INTEGER (long)

+ * 文件夹中笔记的数量 + *

类型: INTEGER (long)

*/ public static final String NOTES_COUNT = "notes_count"; /** - * The file type: folder or note - *

Type: INTEGER

+ * 文件类型:文件夹或笔记 + *

类型: INTEGER

*/ public static final String TYPE = "type"; /** - * The last sync id - *

Type: INTEGER (long)

+ * 最后同步的 ID + *

类型: INTEGER (long)

*/ public static final String SYNC_ID = "sync_id"; /** - * Sign to indicate local modified or not - *

Type: INTEGER

+ * 表示是否本地修改的标志 + *

类型: INTEGER

*/ public static final String LOCAL_MODIFIED = "local_modified"; /** - * Original parent id before moving into temporary folder - *

Type : INTEGER

+ * 移动到临时文件夹之前的原始父 ID + *

类型 : INTEGER

*/ public static final String ORIGIN_PARENT_ID = "origin_parent_id"; /** - * The gtask id - *

Type : TEXT

+ * gtask 的 ID + *

类型 : TEXT

*/ public static final String GTASK_ID = "gtask_id"; /** - * The version code - *

Type : INTEGER (long)

+ * 版本代码 + *

类型 : INTEGER (long)

*/ public static final String VERSION = "version"; } public interface DataColumns { /** - * The unique ID for a row - *

Type: INTEGER (long)

+ * 行的唯一 ID + *

类型: INTEGER (long)

*/ public static final String ID = "_id"; /** - * The MIME type of the item represented by this row. - *

Type: Text

+ * 该行表示的项的 MIME 类型 + *

类型: Text

*/ public static final String MIME_TYPE = "mime_type"; /** - * The reference id to note that this data belongs to - *

Type: INTEGER (long)

+ * 该数据所属笔记的引用 ID + *

类型: INTEGER (long)

*/ public static final String NOTE_ID = "note_id"; /** - * Created data for note or folder - *

Type: INTEGER (long)

+ * 笔记或文件夹的创建日期 + *

类型: INTEGER (long)

*/ public static final String CREATED_DATE = "created_date"; /** - * Latest modified date - *

Type: INTEGER (long)

+ * 最近修改日期 + *

类型: INTEGER (long)

*/ public static final String MODIFIED_DATE = "modified_date"; /** - * Data's content - *

Type: TEXT

+ * 数据的内容 + *

类型: TEXT

*/ public static final String CONTENT = "content"; /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

+ * 通用数据列,其含义取决于 {@link #MIMETYPE},用于整数数据类型 + *

类型: INTEGER

*/ public static final String DATA1 = "data1"; /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

+ * 通用数据列,其含义取决于 {@link #MIMETYPE},用于整数数据类型 + *

类型: INTEGER

*/ public static final String DATA2 = "data2"; /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

+ * 通用数据列,其含义取决于 {@link #MIMETYPE},用于文本数据类型 + *

类型: TEXT

*/ public static final String DATA3 = "data3"; /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

+ * 通用数据列,其含义取决于 {@link #MIMETYPE},用于文本数据类型 + *

类型: TEXT

*/ public static final String DATA4 = "data4"; /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

+ * 通用数据列,其含义取决于 {@link #MIMETYPE},用于文本数据类型 + *

类型: TEXT

*/ public static final String DATA5 = "data5"; } public static final class TextNote implements DataColumns { /** - * Mode to indicate the text in check list mode or not - *

Type: Integer 1:check list mode 0: normal mode

+ * 指示文本是否处于检查列表模式的模式 + *

类型: 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"; - + // 文本笔记的 Uri public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); } public static final class CallNote implements DataColumns { /** - * Call date for this record - *

Type: INTEGER (long)

+ * 此记录的通话日期 + *

类型: INTEGER (long)

*/ public static final String CALL_DATE = DATA1; /** - * Phone number for this record - *

Type: TEXT

+ * 此记录的电话号码 + *

类型: 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"; - + // 通话记录的 Uri public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); } -} +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java index ffe5d57..d470f7a 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java @@ -7,7 +7,7 @@ * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software + * Unless required by 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 @@ -28,196 +28,245 @@ import net.micode.notes.data.Notes.NoteColumns; 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," + - NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + - NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + - NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + - 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," + - NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + - NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + - NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + - NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + - NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + - NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + - ")"; - + "CREATE TABLE " + TABLE.NOTE + "(" + + // 笔记的唯一标识符,主键,自动递增 + NoteColumns.ID + " INTEGER PRIMARY KEY," + + // 笔记所属父级的 ID,默认为 0,表示没有父级 + NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + + // 笔记的提醒日期,默认为 0,表示没有设置提醒 + NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + + // 笔记的背景颜色 ID,默认为 0,可能对应某种颜色的编码 + NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + + // 笔记的创建日期,使用 strftime 函数获取当前时间戳并乘以 1000,转换为毫秒级 + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + // 表示笔记是否包含附件,默认为 0,表示没有附件 + NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + + // 笔记的最后修改日期,使用 strftime 函数获取当前时间戳并乘以 1000,转换为毫秒级 + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + // 文件夹中笔记的数量,默认为 0 + NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + + // 笔记的摘要信息,默认为空字符串 + NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + + // 笔记的类型,默认为 0,可能代表不同类型的笔记,如普通笔记、文件夹等 + NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + + // 笔记关联的部件 ID,默认为 0 + NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + + // 笔记部件的类型,默认为 -1,可能代表不同类型的部件 + NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + + // 笔记的同步 ID,默认为 0,用于数据同步 + NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + + // 表示笔记是否在本地修改,默认为 0,表示未修改 + NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + + // 笔记的原始父级 ID,默认为 0 + NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + + // 可能是用于 Google 任务的 ID,默认为空字符串 + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + + // 笔记的版本号,默认为 0 + 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," + - DataColumns.MIME_TYPE + " TEXT NOT NULL," + - DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + - 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," + - DataColumns.DATA2 + " INTEGER," + - DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + - DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + - DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + - ")"; - + "CREATE TABLE " + TABLE.DATA + "(" + + // 数据记录的唯一标识符,主键,自动递增 + DataColumns.ID + " INTEGER PRIMARY KEY," + + // 数据的 MIME 类型,用于区分不同类型的数据,如文本、图片等,不能为空 + DataColumns.MIME_TYPE + " TEXT NOT NULL," + + // 该数据记录所属笔记的 ID,默认为 0 + DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + + // 数据记录的创建日期,使用 strftime 函数获取当前时间戳并乘以 1000,转换为毫秒级 + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + // 数据记录的最后修改日期,使用 strftime 函数获取当前时间戳并乘以 1000,转换为毫秒级 + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + // 数据记录的内容,默认为空字符串 + DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + + // 通用数据列 1,可存储整数数据,可为空 + DataColumns.DATA1 + " INTEGER," + + // 通用数据列 2,可存储整数数据,可为空 + DataColumns.DATA2 + " INTEGER," + + // 通用数据列 3,可存储文本数据,默认为空字符串 + DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + + // 通用数据列 4,可存储文本数据,默认为空字符串 + DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + + // 通用数据列 5,可存储文本数据,默认为空字符串 + DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + + ")"; + + // 创建一个索引,用于提高基于 NOTE_ID 列的数据查询性能 private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = - "CREATE INDEX IF NOT EXISTS note_id_index ON " + - TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; + "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 + * 当笔记的父级 ID 更新时触发的触发器,用于增加文件夹中的笔记数量 + * 例如,将笔记移动到一个文件夹中时,该文件夹的笔记数量会加 1 */ 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"; + "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"; /** - * Decrease folder's note count when move note from folder + * 当笔记的父级 ID 更新时触发的触发器,用于减少文件夹中的笔记数量 + * 例如,将笔记从一个文件夹中移走时,该文件夹的笔记数量会减 1,但确保数量不会为负数 */ 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"; + "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"; /** - * Increase folder's note count when insert new note to the folder + * 当插入新笔记时触发的触发器,用于增加文件夹中的笔记数量 + * 新笔记插入到一个文件夹时,该文件夹的笔记数量会加 1 */ 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"; + "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"; /** - * Decrease folder's note count when delete note from the folder + * 当删除笔记时触发的触发器,用于减少文件夹中的笔记数量 + * 从文件夹中删除笔记时,该文件夹的笔记数量会减 1,但确保数量不会为负数 */ 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"; + "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"; /** - * Update note's content when insert data with type {@link DataConstants#NOTE} + * 当向数据表插入新数据且数据的 MIME 类型为 {@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"; + "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"; /** - * Update note's content when data with {@link DataConstants#NOTE} type has changed + * 当数据表中数据更新且数据的 MIME 类型为 {@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"; + "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"; /** - * Update note's content when data with {@link DataConstants#NOTE} type has deleted + * 当数据表中数据删除且数据的 MIME 类型为 {@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"; + "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"; /** - * 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 + - " BEGIN" + - " DELETE FROM " + TABLE.DATA + - " WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" + - " END"; + "CREATE TRIGGER delete_data_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN" + + " DELETE FROM " + TABLE.DATA + + " 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 + - " BEGIN" + - " DELETE FROM " + TABLE.NOTE + - " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + - " END"; + "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"; /** - * Move notes belong to folder which has been moved to trash folder + * 当笔记表中的文件夹被移动到回收站文件夹时触发的触发器 + * 将该文件夹下的所有笔记的父级 ID 更新为回收站文件夹的 ID */ 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"; + "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"; public NotesDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } + // 创建笔记表的方法 public void createNoteTable(SQLiteDatabase db) { + // 执行创建笔记表的 SQL 语句 db.execSQL(CREATE_NOTE_TABLE_SQL); + // 重新创建与笔记表相关的触发器 reCreateNoteTableTriggers(db); + // 创建系统文件夹 createSystemFolder(db); 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"); db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete"); @@ -226,6 +275,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { 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); @@ -235,18 +285,21 @@ 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 + * 创建通话记录文件夹,用于存储通话笔记 + * 将通话记录文件夹的信息存储在 ContentValues 中,并插入到笔记表中 */ 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 + * 创建根文件夹,作为默认文件夹 + * 清除 ContentValues 中的数据,存储根文件夹的信息并插入到笔记表中 */ values.clear(); values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER); @@ -254,7 +307,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.insert(TABLE.NOTE, null, values); /** - * temporary folder which is used for moving note + * 创建临时文件夹,用于暂存笔记 + * 清除 ContentValues 中的数据,存储临时文件夹的信息并插入到笔记表中 */ values.clear(); values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); @@ -262,7 +316,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.insert(TABLE.NOTE, null, values); /** - * create trash folder + * 创建回收站文件夹 + * 清除 ContentValues 中的数据,存储回收站文件夹的信息并插入到笔记表中 */ values.clear(); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); @@ -270,23 +325,31 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.insert(TABLE.NOTE, null, values); } + // 创建数据记录表的方法 public void createDataTable(SQLiteDatabase db) { + // 执行创建数据记录表的 SQL 语句 db.execSQL(CREATE_DATA_TABLE_SQL); + // 重新创建与数据记录表相关的触发器 reCreateDataTableTriggers(db); + // 创建索引,提高数据查询性能 db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL); 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"); 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); } + // 获取单例的方法,保证在多线程环境下只创建一个实例 static synchronized NotesDatabaseHelper getInstance(Context context) { if (mInstance == null) { mInstance = new NotesDatabaseHelper(context); @@ -296,6 +359,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { + // 调用创建笔记表和数据记录表的方法 createNoteTable(db); createDataTable(db); } @@ -306,57 +370,68 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { boolean skipV2 = false; if (oldVersion == 1) { + // 从版本 1 升级到版本 2 的操作 upgradeToV2(db); - skipV2 = true; // this upgrade including the upgrade from v2 to v3 + skipV2 = true; // 标记已完成从版本 2 到版本 3 的部分升级步骤 oldVersion++; } - if (oldVersion == 2 && !skipV2) { + if (oldVersion == 2 &&!skipV2) { + // 从版本 2 升级到版本 3 的操作 upgradeToV3(db); reCreateTriggers = true; oldVersion++; } if (oldVersion == 3) { + // 从版本 3 升级到版本 4 的操作 upgradeToV4(db); oldVersion++; } if (reCreateTriggers) { + // 重新创建触发器,以适应新的数据库结构 reCreateNoteTableTriggers(db); reCreateDataTableTriggers(db); } - if (oldVersion != newVersion) { + if (oldVersion!= newVersion) { + // 若升级未成功,抛出异常 throw new IllegalStateException("Upgrade notes database to version " + newVersion + "fails"); } } + // 从版本 1 升级到版本 2 的方法 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); } + // 从版本 2 升级到版本 3 的方法 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); } + // 从版本 3 升级到版本 4 的方法 private void upgradeToV4(SQLiteDatabase db) { + // 为笔记表添加新列 VERSION 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/Notesmaster/app/src/main/java/net/micode/notes/data/NotesProvider.java b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesProvider.java index edb0a60..d40ed45 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesProvider.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesProvider.java @@ -7,7 +7,7 @@ * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software + * Unless required by 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 @@ -36,22 +36,24 @@ import net.micode.notes.data.NotesDatabaseHelper.TABLE; public class NotesProvider extends ContentProvider { + // 用于匹配 URI 的 UriMatcher 实例 private static final UriMatcher mMatcher; - + // 数据库辅助类的实例,用于操作数据库 private NotesDatabaseHelper mHelper; - + // 日志标签 private static final String TAG = "NotesProvider"; - - 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 匹配的常量 + 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; static { + // 初始化 UriMatcher mMatcher = new UriMatcher(UriMatcher.NO_MATCH); + // 为不同的 URI 模式添加匹配规则 mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); @@ -62,57 +64,66 @@ public class NotesProvider extends ContentProvider { } /** - * x'0A' represents the '\n' character in sqlite. For title and content in the search result, - * we will trim '\n' and white space in order to show more information. + * x'0A' 表示 SQLite 中的 '\n' 字符。对于搜索结果中的标题和内容, + * 我们将修剪 '\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; + + 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; + // 搜索笔记摘要的查询语句 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; + + " FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.SNIPPET + " LIKE?" + + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + + " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; @Override public boolean onCreate() { + // 获取数据库辅助类的实例 mHelper = NotesDatabaseHelper.getInstance(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, - String sortOrder) { + 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) { + if (sortOrder!= null || projection!= null) { + // 如果设置了排序顺序或投影,抛出异常 throw new IllegalArgumentException( "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); } @@ -120,6 +131,7 @@ public class NotesProvider extends ContentProvider { String searchString = null; if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { if (uri.getPathSegments().size() > 1) { + // 获取搜索字符串 searchString = uri.getPathSegments().get(1); } } else { @@ -131,7 +143,9 @@ public class NotesProvider extends ContentProvider { } try { + // 格式化搜索字符串 searchString = String.format("%%%s%%", searchString); + // 执行原始查询 c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, new String[] { searchString }); } catch (IllegalStateException ex) { @@ -139,9 +153,11 @@ public class NotesProvider extends ContentProvider { } break; default: + // 不匹配的 URI 抛出异常 throw new IllegalArgumentException("Unknown URI " + uri); } - if (c != null) { + if (c!= null) { + // 设置通知 URI c.setNotificationUri(getContext().getContentResolver(), uri); } return c; @@ -149,30 +165,36 @@ public class NotesProvider extends ContentProvider { @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)) { + // 获取笔记 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: + // 不匹配的 URI 抛出异常 throw new IllegalArgumentException("Unknown URI " + uri); } - // Notify the note uri + // 通知笔记 URI 的变更 if (noteId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); } - // Notify the data uri + // 通知数据 URI 的变更 if (dataId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); @@ -185,43 +207,51 @@ public class NotesProvider extends ContentProvider { 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 that smaller than 0 is system folder which is not allowed to - * trash + * 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: + // 不匹配的 URI 抛出异常 throw new IllegalArgumentException("Unknown URI " + uri); } if (count > 0) { if (deleteData) { + // 通知笔记 URI 的变更 getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); } + // 通知 URI 的变更 getContext().getContentResolver().notifyChange(uri, null); } return count; @@ -231,46 +261,59 @@ public class NotesProvider extends ContentProvider { 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: + // 不匹配的 URI 抛出异常 throw new IllegalArgumentException("Unknown URI " + uri); } if (count > 0) { if (updateData) { + // 通知笔记 URI 的变更 getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); } + // 通知 URI 的变更 getContext().getContentResolver().notifyChange(uri, null); } return count; } + // 解析选择条件 private String parseSelection(String selection) { - return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); + return (!TextUtils.isEmpty(selection)? " AND (" + selection + ')' : ""); } + // 增加笔记的版本号 private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { StringBuilder sql = new StringBuilder(120); sql.append("UPDATE "); @@ -279,14 +322,14 @@ public class NotesProvider extends ContentProvider { sql.append(NoteColumns.VERSION); sql.append("=" + NoteColumns.VERSION + "+1 "); - if (id > 0 || !TextUtils.isEmpty(selection)) { + 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; + String selectString = id > 0? parseSelection(selection) : selection; for (String args : selectionArgs) { selectString = selectString.replaceFirst("\\?", args); } @@ -298,8 +341,8 @@ public class NotesProvider extends ContentProvider { @Override public String getType(Uri uri) { - // TODO Auto-generated method stub + // 待实现的方法,目前返回 null return null; } -} +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/gtask/data/MetaData.java b/src/Notesmaster/app/src/main/java/net/micode/notes/gtask/data/MetaData.java index 3a2050b..c6778d0 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/gtask/data/MetaData.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/gtask/data/MetaData.java @@ -26,37 +26,49 @@ import org.json.JSONObject; public class MetaData extends Task { + // 日志标签,使用类的简单名称作为标签 private final static String TAG = MetaData.class.getSimpleName(); - + // 存储相关的 GID 信息 private String mRelatedGid = null; + // 设置元数据的方法 public void setMeta(String gid, JSONObject metaInfo) { try { + // 将 GID 放入元信息的 JSON 对象中 metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); } catch (JSONException e) { + // 发生异常时打印错误日志 Log.e(TAG, "failed to put related gid"); } + // 将元信息对象转换为字符串并设置为笔记内容 setNotes(metaInfo.toString()); + // 设置名称为预定义的元数据笔记名称 setName(GTaskStringUtils.META_NOTE_NAME); } + // 获取相关 GID 的方法 public String getRelatedGid() { return mRelatedGid; } @Override public boolean isWorthSaving() { - return getNotes() != null; + // 判断是否值得保存,当笔记内容不为空时返回 true + return getNotes()!= null; } @Override public void setContentByRemoteJSON(JSONObject js) { + // 调用父类的方法设置内容 super.setContentByRemoteJSON(js); - if (getNotes() != null) { + if (getNotes()!= null) { try { + // 将笔记内容转换为 JSON 对象 JSONObject metaInfo = new JSONObject(getNotes().trim()); + // 从元信息中获取相关 GID mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); } catch (JSONException e) { + // 发生异常时打印警告日志并将相关 GID 设为 null Log.w(TAG, "failed to get related gid"); mRelatedGid = null; } @@ -65,18 +77,20 @@ public class MetaData extends Task { @Override public void setContentByLocalJSON(JSONObject js) { - // this function should not be called + // 此方法不应该被调用,如果被调用则抛出非法访问错误 throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); } @Override public JSONObject getLocalJSONFromContent() { + // 此方法不应该被调用,如果被调用则抛出非法访问错误 throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); } @Override public int getSyncAction(Cursor c) { + // 此方法不应该被调用,如果被调用则抛出非法访问错误 throw new IllegalAccessError("MetaData:getSyncAction should not be called"); } -} +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/tool/BackupUtils.java b/src/Notesmaster/app/src/main/java/net/micode/notes/tool/BackupUtils.java index 39f6ec4..1b865c5 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/tool/BackupUtils.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/tool/BackupUtils.java @@ -8,8 +8,8 @@ * 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. + * 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. */ @@ -37,10 +37,12 @@ import java.io.PrintStream; public class BackupUtils { + // 日志标签,用于在日志输出中标识来自 BackupUtils 类的信息,方便调试和错误追踪 private static final String TAG = "BackupUtils"; - // Singleton stuff + // 单例模式下的唯一 BackupUtils 实例 private static BackupUtils sInstance; + // 获取 BackupUtils 的单例实例,使用 synchronized 关键字确保多线程环境下的线程安全 public static synchronized BackupUtils getInstance(Context context) { if (sInstance == null) { sInstance = new BackupUtils(context); @@ -49,56 +51,62 @@ public class BackupUtils { } /** - * Following states are signs to represents backup or restore - * status + * 以下状态表示备份或恢复的状态 */ - // Currently, the sdcard is not mounted - public static final int STATE_SD_CARD_UNMOUONTED = 0; - // The backup file not exist - public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; - // The data is not well formated, may be changed by other programs - public static final int STATE_DATA_DESTROIED = 2; - // Some run-time exception which causes restore or backup fails - public static final int STATE_SYSTEM_ERROR = 3; - // Backup or restore success - public static final int STATE_SUCCESS = 4; + // 表示外部存储设备(如 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; + // 构造函数,接收一个 Context 对象,用于获取资源和与系统服务交互,同时初始化 TextExport 对象 private BackupUtils(Context context) { mTextExport = new TextExport(context); } + // 检查外部存储是否可用,通过比较存储状态与 MEDIA_MOUNTED 常量来判断 private static boolean externalStorageAvailable() { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } + // 导出数据为文本文件,调用 TextExport 类的 exportToText 方法进行实际的导出操作 public int exportToText() { return mTextExport.exportToText(); } + // 获取导出的文本文件的文件名,该文件名由 TextExport 类的 exportToText 方法生成 public String getExportedTextFileName() { return mTextExport.mFileName; } + // 获取导出的文本文件的目录,该目录由 TextExport 类的 exportToText 方法生成 public String getExportedTextFileDir() { return mTextExport.mFileDirectory; } + // 内部静态类 TextExport,负责将数据导出为文本的具体操作 private static class TextExport { + // 定义查询笔记时需要的列,包含笔记的 ID、修改日期、摘要和类型等信息 private static final String[] NOTE_PROJECTION = { NoteColumns.ID, NoteColumns.MODIFIED_DATE, NoteColumns.SNIPPET, NoteColumns.TYPE }; - + // 笔记列的索引,方便在查询结果中定位笔记的 ID private static final int NOTE_COLUMN_ID = 0; - + // 笔记列的索引,方便在查询结果中定位笔记的修改日期 private static final int NOTE_COLUMN_MODIFIED_DATE = 1; - + // 笔记列的索引,方便在查询结果中定位笔记的摘要 private static final int NOTE_COLUMN_SNIPPET = 2; - + // 定义查询数据时需要的列,包含内容、MIME 类型、数据 1 到数据 4 等信息 private static final String[] DATA_PROJECTION = { DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -107,129 +115,146 @@ public class BackupUtils { DataColumns.DATA3, DataColumns.DATA4, }; - + // 数据列的索引,方便在查询结果中定位数据的内容 private static final int DATA_COLUMN_CONTENT = 0; - + // 数据列的索引,方便在查询结果中定位数据的 MIME 类型 private static final int DATA_COLUMN_MIME_TYPE = 1; - + // 数据列的索引,方便在查询结果中定位通话日期 private static final int DATA_COLUMN_CALL_DATE = 2; - + // 数据列的索引,方便在查询结果中定位电话号码 private static final int DATA_COLUMN_PHONE_NUMBER = 4; - - private final String [] TEXT_FORMAT; - private static final int FORMAT_FOLDER_NAME = 0; - private static final int FORMAT_NOTE_DATE = 1; - private static final int FORMAT_NOTE_CONTENT = 2; + // 存储文本格式的数组,从资源文件中获取不同部分的格式信息,用于格式化输出文本 + private 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 = ""; } + // 根据索引获取文本格式,用于格式化输出文本 private String getFormat(int id) { return TEXT_FORMAT[id]; } - /** - * Export the folder identified by folder id to text - */ + // 将指定文件夹导出为文本,接收文件夹的 ID 和输出的打印流 private void exportFolderToText(String folderId, PrintStream ps) { - // Query notes belong to this folder + // 使用 ContentResolver 查询属于该文件夹的笔记 Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, - NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { - folderId + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[]{ + folderId }, null); - if (notesCursor != null) { + if (notesCursor!= null) { if (notesCursor.moveToFirst()) { do { - // Print note's last modified date + // 打印笔记的最后修改日期,使用 DateFormat 格式化日期,并使用 String.format 应用获取到的日期格式 ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( mContext.getString(R.string.format_datetime_mdhm), notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 获取笔记的 ID String noteId = notesCursor.getString(NOTE_COLUMN_ID); + // 调用 exportNoteToText 方法导出该笔记的内容 exportNoteToText(noteId, ps); } while (notesCursor.moveToNext()); } + // 关闭查询笔记的游标,释放资源 notesCursor.close(); } } - /** - * Export note identified by id to a print stream - */ + // 将指定笔记导出为文本,接收笔记的 ID 和输出的打印流 private void exportNoteToText(String noteId, PrintStream ps) { + // 使用 ContentResolver 查询属于该笔记的数据 Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, - DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { - noteId + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[]{ + noteId }, null); - if (dataCursor != null) { + if (dataCursor!= null) { if (dataCursor.moveToFirst()) { do { + // 获取数据的 MIME 类型 String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (DataConstants.CALL_NOTE.equals(mimeType)) { - // Print phone number + // 如果是通话记录类型的数据 + // 获取电话号码 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)); } - // Print call date + // 打印通话日期,使用 DateFormat 格式化日期,并使用 String.format 应用获取到的日期格式 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat .format(mContext.getString(R.string.format_datetime_mdhm), callDate))); - // Print call attachment location 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(); } - // print a line separator between note + // 在笔记之间添加分隔符,使用字节数组存储分隔符,包含换行符和可能的其他分隔符 try { - ps.write(new byte[] { + ps.write(new byte[]{ Character.LINE_SEPARATOR, Character.LETTER_NUMBER }); } catch (IOException e) { + // 打印 IO 异常信息到日志 Log.e(TAG, e.toString()); } } - /** - * Note will be exported as text which is user readable - */ + // 将数据导出为用户可读的文本文件 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; } - // First export folder and its notes + // 首先导出文件夹及其笔记,使用 ContentResolver 查询满足条件的文件夹 Cursor folderCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, @@ -237,71 +262,87 @@ public class BackupUtils { + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); - if (folderCursor != null) { + if (folderCursor!= null) { if (folderCursor.moveToFirst()) { do { - // Print folder's name + // 获取文件夹的名称 String folderName = ""; - if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { + if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { + // 如果是通话记录文件夹,使用资源中的名称 folderName = mContext.getString(R.string.call_record_folder_name); } else { + // 否则使用笔记的摘要作为文件夹名称 folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); } if (!TextUtils.isEmpty(folderName)) { + // 按照文件夹名称格式输出文件夹名称 ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); } + // 获取文件夹的 ID String folderId = folderCursor.getString(NOTE_COLUMN_ID); + // 调用 exportFolderToText 方法导出该文件夹的笔记 exportFolderToText(folderId, ps); } while (folderCursor.moveToNext()); } + // 关闭查询文件夹的游标,释放资源 folderCursor.close(); } - // Export notes in root's folder + // 导出根文件夹中的笔记,使用 ContentResolver 查询根文件夹中的笔记 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!= null) { if (noteCursor.moveToFirst()) { do { + // 打印笔记的最后修改日期,使用 DateFormat 格式化日期,并使用 String.format 应用获取到的日期格式 ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( mContext.getString(R.string.format_datetime_mdhm), noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 获取笔记的 ID String noteId = noteCursor.getString(NOTE_COLUMN_ID); + // 调用 exportNoteToText 方法导出该笔记的内容 exportNoteToText(noteId, ps); } while (noteCursor.moveToNext()); } + // 关闭查询笔记的游标,释放资源 noteCursor.close(); } + // 关闭打印流,完成导出操作 ps.close(); - + // 返回导出成功的状态 return STATE_SUCCESS; } - /** - * Get a print stream pointed to the file {@generateExportedTextFile} - */ + // 获取一个指向生成的文本文件的打印流 private PrintStream getExportToTextPrintStream() { + // 生成存储导出数据的文件 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; } @@ -309,14 +350,15 @@ public class BackupUtils { } } - /** - * Generate the text file to store imported data - */ + // 生成存储导入数据的文本文件,接收上下文、文件路径资源 ID 和文件名格式资源 ID 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), @@ -325,20 +367,22 @@ public class BackupUtils { try { if (!filedir.exists()) { + // 如果文件目录不存在,创建文件目录 filedir.mkdir(); } if (!file.exists()) { + // 如果文件不存在,创建文件 file.createNewFile(); } return file; } catch (SecurityException e) { + // 打印安全异常信息 e.printStackTrace(); } catch (IOException e) { + // 打印 IO 异常信息 e.printStackTrace(); } return null; } -} - - +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/tool/DataUtils.java b/src/Notesmaster/app/src/main/java/net/micode/notes/tool/DataUtils.java index 2a14982..4b3a3c7 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/tool/DataUtils.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/tool/DataUtils.java @@ -8,7 +8,7 @@ * 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, + * 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. @@ -36,99 +36,136 @@ import java.util.HashSet; public class DataUtils { + // 日志标签,用于日志输出中标识该类的信息,方便调试和错误追踪 public static final String TAG = "DataUtils"; + + // 批量删除笔记的方法,接收一个 ContentResolver 和一个包含笔记 ID 的 HashSet public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 如果传入的 ids 集合为 null,则打印日志并返回 true,表示操作“成功”(实际上未执行删除操作) if (ids == null) { Log.d(TAG, "the ids is null"); return true; } + // 如果 ids 集合中没有元素,则打印日志并返回 true,表示操作“成功”(实际上未执行删除操作) if (ids.size() == 0) { Log.d(TAG, "no id is in the hashset"); return true; } + // 存储 ContentProviderOperation 的列表,用于批量操作 ArrayList operationList = new ArrayList(); + // 遍历 ids 集合中的每个 id for (long id : ids) { - if(id == Notes.ID_ROOT_FOLDER) { + // 如果是系统根文件夹的 ID,则打印错误日志并跳过该 id,不执行删除操作 + if (id == Notes.ID_ROOT_FOLDER) { Log.e(TAG, "Don't delete system folder root"); continue; } + // 创建一个删除操作的构建器,用于删除指定 URI 上的笔记 ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 将构建好的操作添加到操作列表中 operationList.add(builder.build()); } try { + // 执行批量操作,使用 ContentResolver 的 applyBatch 方法 ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果,如果结果为空或第一个结果为 null,则认为删除操作失败 if (results == null || results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); return false; } + // 操作成功,返回 true return true; } catch (RemoteException e) { + // 处理远程操作异常,打印错误信息 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { + // 处理操作应用异常,打印错误信息 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } + // 操作失败,返回 false return false; } + // 将笔记移动到另一个文件夹的方法,接收 ContentResolver、笔记 ID、源文件夹 ID 和目标文件夹 ID public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { + // 创建 ContentValues 用于存储要更新的值 ContentValues values = new ContentValues(); + // 更新笔记的父文件夹 ID values.put(NoteColumns.PARENT_ID, desFolderId); + // 存储笔记的原始父文件夹 ID values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); + // 标记为本地已修改 values.put(NoteColumns.LOCAL_MODIFIED, 1); + // 使用 ContentResolver 的 update 方法更新笔记的信息 resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); } - public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, - long folderId) { + // 批量将笔记移动到指定文件夹的方法,接收 ContentResolver、笔记 ID 的 HashSet 和目标文件夹 ID + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { + // 如果传入的 ids 集合为 null,则打印日志并返回 true,表示操作“成功”(实际上未执行移动操作) if (ids == null) { Log.d(TAG, "the ids is null"); return true; } + // 存储 ContentProviderOperation 的列表,用于批量操作 ArrayList operationList = new ArrayList(); + // 遍历 ids 集合中的每个 id for (long id : ids) { + // 创建一个更新操作的构建器,用于更新指定 URI 上的笔记 ContentProviderOperation.Builder builder = ContentProviderOperation .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 更新笔记的父文件夹 ID builder.withValue(NoteColumns.PARENT_ID, folderId); + // 标记为本地已修改 builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); + // 将构建好的操作添加到操作列表中 operationList.add(builder.build()); } try { + // 执行批量操作,使用 ContentResolver 的 applyBatch 方法 ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果,如果结果为空或第一个结果为 null,则认为移动操作失败 if (results == null || results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); return false; } + // 操作成功,返回 true return true; } catch (RemoteException e) { + // 处理远程操作异常,打印错误信息 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { + // 处理操作应用异常,打印错误信息 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } + // 操作失败,返回 false return false; } - /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} - */ + // 获取用户创建的文件夹数量的方法,不包括系统文件夹 public static int getUserFolderCount(ContentResolver resolver) { - Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, - new String[] { "COUNT(*)" }, + // 查询满足条件的文件夹数量,使用 COUNT(*) 进行统计 + 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)}, + new String[]{String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, null); int count = 0; - if(cursor != null) { - if(cursor.moveToFirst()) { + 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(); } } @@ -136,160 +173,202 @@ public class DataUtils { return count; } + // 检查笔记是否在笔记数据库中可见的方法,接收 ContentResolver、笔记 ID 和笔记类型 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)}, + new String[]{String.valueOf(type)}, null); boolean exist = false; - if (cursor != null) { + if (cursor!= null) { + // 如果查询结果的行数大于 0,则认为笔记存在 if (cursor.getCount() > 0) { exist = true; } + // 关闭游标,释放资源 cursor.close(); } return exist; } + // 检查笔记是否存在于笔记数据库中的方法,接收 ContentResolver 和笔记 ID public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + // 查询指定笔记 ID 的笔记是否存在 Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, null, null, null); boolean exist = false; - if (cursor != null) { + if (cursor!= null) { + // 如果查询结果的行数大于 0,则认为笔记存在 if (cursor.getCount() > 0) { exist = true; } + // 关闭游标,释放资源 cursor.close(); } return exist; } + // 检查数据是否存在于数据数据库中的方法,接收 ContentResolver 和数据 ID public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + // 查询指定数据 ID 的数据是否存在 Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); boolean exist = false; - if (cursor != null) { + if (cursor!= null) { + // 如果查询结果的行数大于 0,则认为数据存在 if (cursor.getCount() > 0) { exist = true; } + // 关闭游标,释放资源 cursor.close(); } return exist; } + // 检查文件夹名称是否可见的方法,接收 ContentResolver 和文件夹名称 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); + " 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) { + if (cursor!= null) { + // 如果查询结果的行数大于 0,则认为文件夹存在 + if (cursor.getCount() > 0) { exist = true; } + // 关闭游标,释放资源 cursor.close(); } return exist; } + // 获取文件夹中笔记的小部件信息,接收 ContentResolver 和文件夹 ID public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + // 查询满足条件的笔记的小部件信息 Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, - new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, + new String[]{NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE}, NoteColumns.PARENT_ID + "=?", - new String[] { String.valueOf(folderId) }, + new String[]{String.valueOf(folderId)}, null); HashSet set = null; - if (c != null) { + if (c!= null) { if (c.moveToFirst()) { set = new HashSet(); do { try { + // 创建 AppWidgetAttribute 对象存储小部件信息 AppWidgetAttribute widget = new AppWidgetAttribute(); + // 获取小部件 ID widget.widgetId = c.getInt(0); + // 获取小部件类型 widget.widgetType = c.getInt(1); + // 将小部件信息添加到集合中 set.add(widget); } catch (IndexOutOfBoundsException e) { + // 处理索引越界异常,打印错误信息 Log.e(TAG, e.toString()); } } while (c.moveToNext()); } + // 关闭游标,释放资源 c.close(); } return set; } + // 根据笔记 ID 获取通话号码的方法,接收 ContentResolver 和笔记 ID public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 查询满足条件的通话号码 Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, - new String [] { CallNote.PHONE_NUMBER }, + new String[]{CallNote.PHONE_NUMBER}, CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", - new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, + new String[]{String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE}, null); - if (cursor != null && cursor.moveToFirst()) { + 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 的方法,接收 ContentResolver、电话号码和通话日期 public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + // 查询满足条件的笔记 ID Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, - new String [] { CallNote.NOTE_ID }, + 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 }, + + CallNote.PHONE_NUMBER + ",?)", + new String[]{String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber}, null); - if (cursor != null) { + if (cursor!= null) { if (cursor.moveToFirst()) { try { + // 获取笔记 ID return cursor.getLong(0); } catch (IndexOutOfBoundsException e) { + // 处理索引越界异常,打印错误信息 Log.e(TAG, "Get call note id fails " + e.toString()); } } + // 关闭游标,释放资源 cursor.close(); } return 0; } + // 根据笔记 ID 获取摘要的方法,接收 ContentResolver 和笔记 ID public static String getSnippetById(ContentResolver resolver, long noteId) { + // 查询满足条件的笔记摘要 Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, - new String [] { NoteColumns.SNIPPET }, + new String[]{NoteColumns.SNIPPET}, NoteColumns.ID + "=?", - new String [] { String.valueOf(noteId)}, + new String[]{String.valueOf(noteId)}, null); - if (cursor != null) { + if (cursor!= null) { String snippet = ""; if (cursor.moveToFirst()) { + // 获取笔记摘要 snippet = cursor.getString(0); } + // 关闭游标,释放资源 cursor.close(); return snippet; } + // 如果未找到笔记,抛出异常 throw new IllegalArgumentException("Note is not found with id: " + noteId); } + // 格式化摘要的方法,接收一个字符串 public static String getFormattedSnippet(String snippet) { - if (snippet != null) { + if (snippet!= null) { + // 去除字符串两端的空格 snippet = snippet.trim(); int index = snippet.indexOf('\n'); - if (index != -1) { + if (index!= -1) { + // 截取到第一个换行符之前的内容 snippet = snippet.substring(0, index); } } return snippet; } -} +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/tool/ResourceParser.java b/src/Notesmaster/app/src/main/java/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..4509cf2 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/tool/ResourceParser.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/tool/ResourceParser.java @@ -24,158 +24,250 @@ 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 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 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 + // 存储编辑笔记时不同颜色背景的资源 ID 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、RED 颜色的背景资源 + 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 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、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 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 编辑笔记的背景资源 ID + */ public static int getNoteBgResource(int id) { return BG_EDIT_RESOURCES[id]; } + /** + * 根据传入的颜色索引获取编辑笔记标题的背景资源 ID + * @param id 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 编辑笔记标题的背景资源 ID + */ public static int getNoteTitleBgResource(int id) { return BG_EDIT_TITLE_RESOURCES[id]; } } + /** + * 获取默认的背景颜色 ID + * 首先检查用户在偏好设置中是否设置了自定义背景颜色(使用 NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY 作为偏好键) + * 如果设置了,则从 NoteBgResources 中随机选择一个背景颜色;如果未设置,则使用 BG_DEFAULT_COLOR 作为默认颜色 + * @param context 应用程序的上下文,用于访问偏好设置 + * @return 背景颜色的索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + */ public static int getDefaultBgId(Context context) { + // 从默认的共享偏好中获取是否设置了背景颜色的偏好设置 if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + // 生成一个 0 到 NoteBgResources.BG_EDIT_RESOURCES 长度之间的随机数作为背景颜色索引 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 + // 存储笔记列表中第一个元素不同颜色背景的资源 ID 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、RED 颜色的第一个元素背景资源 + 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 + // 存储笔记列表中普通元素不同颜色背景的资源 ID 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、RED 颜色的普通元素背景资源 + 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, + // 存储笔记列表中最后一个元素不同颜色背景的资源 ID 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、RED 颜色的最后一个元素背景资源 + 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 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、RED 颜色的单个笔记背景资源 + 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 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 笔记列表中第一个元素的背景资源 ID + */ public static int getNoteBgFirstRes(int id) { return BG_FIRST_RESOURCES[id]; } + /** + * 根据传入的颜色索引获取笔记列表中最后一个元素的背景资源 ID + * @param id 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 笔记列表中最后一个元素的背景资源 ID + */ public static int getNoteBgLastRes(int id) { return BG_LAST_RESOURCES[id]; } + /** + * 根据传入的颜色索引获取单个笔记的背景资源 ID + * @param id 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 单个笔记的背景资源 ID + */ public static int getNoteBgSingleRes(int id) { return BG_SINGLE_RESOURCES[id]; } + /** + * 根据传入的颜色索引获取笔记列表中普通元素的背景资源 ID + * @param id 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 笔记列表中普通元素的背景资源 ID + */ public static int getNoteBgNormalRes(int id) { return BG_NORMAL_RESOURCES[id]; } + /** + * 获取文件夹的背景资源 ID + * @return 文件夹的背景资源 ID,固定为 R.drawable.list_folder + */ public static int getFolderBgRes() { return R.drawable.list_folder; } } public static class WidgetBgResources { - 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 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、RED 颜色的 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 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 2x 部件的背景资源 ID + */ public static int getWidget2xBgResource(int id) { return BG_2X_RESOURCES[id]; } - 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 数组 + // 数组元素分别对应 YELLOW、BLUE、WHITE、GREEN、RED 颜色的 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 颜色索引,应是 YELLOW、BLUE、WHITE、GREEN 或 RED 对应的整数 + * @return 4x 部件的背景资源 ID + */ public static int getWidget4xBgResource(int id) { return BG_4X_RESOURCES[id]; } } public static class TextAppearanceResources { - private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { - R.style.TextAppearanceNormal, - R.style.TextAppearanceMedium, - R.style.TextAppearanceLarge, - R.style.TextAppearanceSuper + // 存储不同文本大小的资源 ID 数组 + // 数组元素分别对应 TEXT_SMALL、TEXT_MEDIUM、TEXT_LARGE、TEXT_SUPER 字体大小的资源 + private final static int[] TEXTAPPEARANCE_RESOURCES = new int[]{ + R.style.TextAppearanceNormal, + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper }; + /** + * 根据传入的文本大小索引获取文本外观资源 ID + * 若传入的索引超出了 TEXTAPPEARANCE_RESOURCES 数组的范围,则返回默认的字体大小 BG_DEFAULT_FONT_SIZE + * @param id 文本大小索引,应是 TEXT_SMALL、TEXT_MEDIUM、TEXT_LARGE 或 TEXT_SUPER 对应的整数 + * @return 文本外观资源 ID + */ public static int getTexAppearanceResource(int id) { - /** - * HACKME: Fix bug of store the resource id in shared preference. - * The id may larger than the length of resources, in this case, - * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} - */ + // 检查传入的文本大小索引是否超出资源数组长度 if (id >= TEXTAPPEARANCE_RESOURCES.length) { + // 超出范围时使用默认字体大小 return BG_DEFAULT_FONT_SIZE; } return TEXTAPPEARANCE_RESOURCES[id]; } + /** + * 获取文本外观资源的数量 + * @return 文本外观资源的数量,等于 TEXTAPPEARANCE_RESOURCES 数组的长度 + */ public static int getResourcesSize() { return TEXTAPPEARANCE_RESOURCES.length; } } -} +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java index 85723be..3c6425f 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java @@ -9,7 +9,7 @@ * * 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. + * 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. */ @@ -41,101 +41,149 @@ import java.io.IOException; public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { + // 存储笔记的唯一标识符 private long mNoteId; + // 存储笔记的摘要信息 private String mSnippet; + // 摘要信息的最大预览长度 private static final int SNIPPET_PREW_MAX_LEN = 60; + // 用于播放闹钟声音的媒体播放器 MediaPlayer mPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // 请求窗口特性,去除标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); + // 获取当前窗口对象 final Window win = getWindow(); + // 给窗口添加标志,使窗口在锁屏时也能显示 win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); + // 检查屏幕是否亮屏 if (!isScreenOn()) { + // 如果屏幕未亮屏,添加以下标志: + // FLAG_KEEP_SCREEN_ON:保持屏幕亮屏 + // FLAG_TURN_SCREEN_ON:点亮屏幕 + // FLAG_ALLOW_LOCK_WHILE_SCREEN_ON:允许在屏幕亮屏时进行锁定操作 + // FLAG_LAYOUT_INSET_DECOR:确保窗口布局能正确显示在装饰区域内 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); } + // 获取启动该活动的意图 Intent intent = getIntent(); try { + // 从意图的数据中获取笔记的唯一标识符 mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); + // 根据笔记的唯一标识符获取摘要信息 mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); - mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0, + // 如果摘要信息的长度超过最大预览长度,截取摘要并添加额外信息 + mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN? mSnippet.substring(0, SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) : mSnippet; } catch (IllegalArgumentException e) { + // 打印异常信息 e.printStackTrace(); return; } + // 创建新的媒体播放器实例 mPlayer = new MediaPlayer(); + // 检查笔记是否存在于笔记数据库中且类型为笔记类型 if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { + // 显示操作对话框 showActionDialog(); + // 播放闹钟声音 playAlarmSound(); } else { + // 若笔记不存在,结束该活动 finish(); } } + // 检查屏幕是否亮屏的方法 private boolean isScreenOn() { + // 获取电源管理器服务 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + // 使用电源管理器检查屏幕是否亮屏 return pm.isScreenOn(); } + // 播放闹钟声音的方法 private void playAlarmSound() { + // 获取默认的闹钟铃声 URI Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); + // 获取系统设置中静音模式影响的流类型 int silentModeStreams = Settings.System.getInt(getContentResolver(), Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); - if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { + // 如果静音模式影响的流包含闹钟流 + if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM))!= 0) { + // 将媒体播放器的音频流类型设置为静音模式影响的流类型 mPlayer.setAudioStreamType(silentModeStreams); } else { + // 将媒体播放器的音频流类型设置为闹钟流类型 mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); } try { + // 设置媒体播放器的数据源为获取到的闹钟铃声 URI mPlayer.setDataSource(this, url); + // 准备播放媒体播放器 mPlayer.prepare(); + // 设置为循环播放 mPlayer.setLooping(true); + // 开始播放 mPlayer.start(); } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block + // 打印异常信息 e.printStackTrace(); } catch (SecurityException e) { - // TODO Auto-generated catch block + // 打印异常信息 e.printStackTrace(); } catch (IllegalStateException e) { - // TODO Auto-generated catch block + // 打印异常信息 e.printStackTrace(); } catch (IOException e) { - // TODO Auto-generated catch block + // 打印异常信息 e.printStackTrace(); } } + // 显示操作对话框的方法 private void showActionDialog() { + // 创建一个新的 AlertDialog 构建器 AlertDialog.Builder dialog = new AlertDialog.Builder(this); + // 设置对话框的标题为应用名称 dialog.setTitle(R.string.app_name); + // 设置对话框的消息为笔记的摘要信息 dialog.setMessage(mSnippet); + // 设置对话框的确认按钮及点击事件监听器 dialog.setPositiveButton(R.string.notealert_ok, this); + // 如果屏幕亮屏,设置对话框的取消按钮及点击事件监听器 if (isScreenOn()) { dialog.setNegativeButton(R.string.notealert_enter, this); } + // 显示对话框并设置对话框关闭监听器 dialog.show().setOnDismissListener(this); } + // 点击事件处理方法 public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_NEGATIVE: + // 当点击取消按钮时,创建新的意图,跳转到笔记编辑活动 Intent intent = new Intent(this, NoteEditActivity.class); + // 设置意图的动作 intent.setAction(Intent.ACTION_VIEW); + // 将笔记的唯一标识符作为额外信息添加到意图中 intent.putExtra(Intent.EXTRA_UID, mNoteId); + // 启动笔记编辑活动 startActivity(intent); break; default: @@ -143,16 +191,23 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD } } + // 对话框关闭事件处理方法 public void onDismiss(DialogInterface dialog) { + // 停止闹钟声音 stopAlarmSound(); + // 结束该活动 finish(); } + // 停止闹钟声音的方法 private void stopAlarmSound() { - if (mPlayer != null) { + if (mPlayer!= null) { + // 停止媒体播放器 mPlayer.stop(); + // 释放媒体播放器资源 mPlayer.release(); + // 将媒体播放器置为 null mPlayer = null; } } -} +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePicker.java b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePicker.java index 496b0cd..302b2b5 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePicker.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePicker.java @@ -1,19 +1,4 @@ -/* - * 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. - */ - +// 包声明,表明该类属于 net.micode.notes.ui 包 package net.micode.notes.ui; import java.text.DateFormatSymbols; @@ -28,86 +13,128 @@ import android.view.View; import android.widget.FrameLayout; import android.widget.NumberPicker; + +// 定义 DateTimePicker 类,继承自 FrameLayout,可作为一个日期时间选择器 public class DateTimePicker extends FrameLayout { + // 定义默认的启用状态,当为 true 时表示该日期时间选择器处于启用状态 private static final boolean DEFAULT_ENABLE_STATE = true; + // 半天包含的小时数,即 12 小时 private static final int HOURS_IN_HALF_DAY = 12; + // 一天包含的小时数,即 24 小时 private static final int HOURS_IN_ALL_DAY = 24; + // 一周包含的天数,即 7 天 private static final int DAYS_IN_ALL_WEEK = 7; + // 日期选择器的最小允许值,用于设置 NumberPicker 的最小范围 private static final int DATE_SPINNER_MIN_VAL = 0; + // 日期选择器的最大允许值,用于设置 NumberPicker 的最大范围 private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1; + // 24 小时制下小时选择器的最小允许值 private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0; + // 24 小时制下小时选择器的最大允许值 private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23; + // 12 小时制下小时选择器的最小允许值 private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1; + // 12 小时制下小时选择器的最大允许值 private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12; + // 分钟选择器的最小允许值 private static final int MINUT_SPINNER_MIN_VAL = 0; + // 分钟选择器的最大允许值 private static final int MINUT_SPINNER_MAX_VAL = 59; + // 上午/下午选择器的最小允许值,通常 0 表示上午(AM) private static final int AMPM_SPINNER_MIN_VAL = 0; + // 上午/下午选择器的最大允许值,通常 1 表示下午(PM) private static final int AMPM_SPINNER_MAX_VAL = 1; + // 用于选择日期的 NumberPicker 组件 private final NumberPicker mDateSpinner; + // 用于选择小时的 NumberPicker 组件 private final NumberPicker mHourSpinner; + // 用于选择分钟的 NumberPicker 组件 private final NumberPicker mMinuteSpinner; + // 用于选择上午/下午的 NumberPicker 组件 private final NumberPicker mAmPmSpinner; + // 存储日期和时间信息的 Calendar 对象,用于对日期和时间进行操作和存储 private Calendar mDate; - + // 存储日期显示的字符串数组,用于存储一周内的日期显示信息 private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK]; - + // 表示当前时间是否为上午,true 表示上午,false 表示下午 private boolean mIsAm; - + // 表示是否使用 24 小时制,true 表示使用 24 小时制,false 表示使用 12 小时制 private boolean mIs24HourView; - + // 表示该日期时间选择器是否处于启用状态,默认为启用 private boolean mIsEnabled = DEFAULT_ENABLE_STATE; - + // 表示是否正在进行初始化操作,用于控制初始化过程中的一些逻辑 private boolean mInitialising; - + // 日期时间更改的监听器,用于监听日期时间发生变化的事件 private OnDateTimeChangedListener mOnDateTimeChangedListener; + + // 日期选择器的监听器,当日期选择器的值发生变化时触发 private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { + // 当日期选择器的值发生变化时,根据新旧值的差值更新日期 mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal); + // 更新日期显示相关的控件 updateDateControl(); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } }; + + // 小时选择器的监听器,当小时选择器的值发生变化时触发 private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { + // 标记日期是否发生了变化 boolean isDateChanged = false; + // 获取一个 Calendar 实例,用于临时存储和计算日期的变化 Calendar cal = Calendar.getInstance(); if (!mIs24HourView) { + // 在 12 小时制下 if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) { + // 如果从上午的最后一小时(11 点)切换到下午的第一小时(12 点),日期加一天 cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, 1); isDateChanged = true; } else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { + // 如果从下午的第一小时(12 点)切换到上午的最后一小时(11 点),日期减一天 cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, -1); isDateChanged = true; } if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY || oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { - mIsAm = !mIsAm; + // 切换上午/下午标志 + mIsAm =!mIsAm; + // 更新上午/下午选择器的显示 updateAmPmControl(); } } else { + // 在 24 小时制下 if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) { + // 如果从 23 点切换到 0 点,日期加一天 cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, 1); isDateChanged = true; } else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) { + // 如果从 0 点切换到 23 点,日期减一天 cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, -1); isDateChanged = true; } } - int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY); + // 计算新的小时值,考虑 12 小时制和 24 小时制的转换 + int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm? 0 : HOURS_IN_HALF_DAY); + // 更新 Calendar 对象的小时值 mDate.set(Calendar.HOUR_OF_DAY, newHour); + // 调用日期时间更改的回调方法 onDateTimeChanged(); if (isDateChanged) { + // 如果日期发生了变化,更新年、月、日 setCurrentYear(cal.get(Calendar.YEAR)); setCurrentMonth(cal.get(Calendar.MONTH)); setCurrentDay(cal.get(Calendar.DAY_OF_MONTH)); @@ -115,105 +142,159 @@ public class DateTimePicker extends FrameLayout { } }; + + // 分钟选择器的监听器,当分钟选择器的值发生变化时触发 private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { + // 获取分钟选择器的最小和最大值 int minValue = mMinuteSpinner.getMinValue(); int maxValue = mMinuteSpinner.getMaxValue(); int offset = 0; if (oldVal == maxValue && newVal == minValue) { + // 如果分钟从最大值变为最小值,需要增加一小时 offset += 1; } else if (oldVal == minValue && newVal == maxValue) { + // 如果分钟从最小值变为最大值,需要减少一小时 offset -= 1; } - if (offset != 0) { + if (offset!= 0) { + // 根据分钟的变化更新日期的小时 mDate.add(Calendar.HOUR_OF_DAY, offset); + // 更新小时选择器的值 mHourSpinner.setValue(getCurrentHour()); + // 更新日期显示相关的控件 updateDateControl(); + // 获取当前小时 int newHour = getCurrentHourOfDay(); if (newHour >= HOURS_IN_HALF_DAY) { + // 如果超过 12 点,标记为下午 mIsAm = false; + // 更新上午/下午选择器的显示 updateAmPmControl(); } else { + // 否则标记为上午 mIsAm = true; updateAmPmControl(); } } + // 更新 Calendar 对象的分钟值 mDate.set(Calendar.MINUTE, newVal); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } }; + + // 上午/下午选择器的监听器,当上午/下午选择器的值发生变化时触发 private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { - mIsAm = !mIsAm; + // 切换上午/下午标志 + mIsAm =!mIsAm; if (mIsAm) { + // 如果切换到上午,日期时间减少 12 小时 mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY); } else { + // 如果切换到下午,日期时间增加 12 小时 mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY); } + // 更新上午/下午选择器的显示 updateAmPmControl(); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } }; + + // 日期时间更改的监听器接口,定义了日期时间发生变化时的回调方法 public interface OnDateTimeChangedListener { void onDateTimeChanged(DateTimePicker view, int year, int month, - int dayOfMonth, int hourOfDay, int minute); + int dayOfMonth, int hourOfDay, int minute); } + + // 构造函数,使用当前时间作为初始日期 public DateTimePicker(Context context) { this(context, System.currentTimeMillis()); } + + // 构造函数,使用指定的日期作为初始日期 public DateTimePicker(Context context, long date) { this(context, date, DateFormat.is24HourFormat(context)); } + + // 构造函数,使用指定的日期和是否 24 小时制作为初始设置 public DateTimePicker(Context context, long date, boolean is24HourView) { super(context); + // 获取一个 Calendar 实例 mDate = Calendar.getInstance(); mInitialising = true; + // 判断当前时间是否为上午 mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY; + // 从布局文件中加载该组件 inflate(context, R.layout.datetime_picker, this); + + // 初始化日期选择器 mDateSpinner = (NumberPicker) findViewById(R.id.date); mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL); mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL); mDateSpinner.setOnValueChangedListener(mOnDateChangedListener); + + // 初始化小时选择器 mHourSpinner = (NumberPicker) findViewById(R.id.hour); mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); - mMinuteSpinner = (NumberPicker) findViewById(R.id.minute); + + + // 初始化分钟选择器 + mMinuteSpinner = (NumberPicker) findViewById(R.id.minute); mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL); mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL); + // 设置长按更新间隔,以毫秒为单位 mMinuteSpinner.setOnLongPressUpdateInterval(100); mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); + + // 获取上午/下午的字符串表示(如 "AM" 和 "PM") String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings(); + // 初始化上午/下午选择器 mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm); mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL); mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL); mAmPmSpinner.setDisplayedValues(stringsForAmPm); mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener); - // update controls to initial state + + // 更新日期显示相关的控件 updateDateControl(); + // 更新小时显示相关的控件 updateHourControl(); + // 更新上午/下午显示相关的控件 updateAmPmControl(); + + // 设置是否使用 24 小时制 set24HourView(is24HourView); - // set to current time + + // 设置当前日期 setCurrentDate(date); + + // 设置启用状态 setEnabled(isEnabled()); - // set the content descriptions + + // 设置内容描述 mInitialising = false; } + + // 重写 setEnabled 方法,用于设置日期时间选择器的启用状态 @Override public void setEnabled(boolean enabled) { if (mIsEnabled == enabled) { @@ -227,145 +308,126 @@ public class DateTimePicker extends FrameLayout { mIsEnabled = enabled; } + + // 重写 isEnabled 方法,用于检查日期时间选择器是否处于启用状态 @Override public boolean isEnabled() { return mIsEnabled; } - /** - * Get the current date in millis - * - * @return the current date in millis - */ + + // 获取当前日期的毫秒表示 public long getCurrentDateInTimeMillis() { return mDate.getTimeInMillis(); } - /** - * Set the current date - * - * @param date The current date in millis - */ + + // 设置当前日期,通过传入日期的毫秒表示 public void setCurrentDate(long date) { + // 创建一个 Calendar 实例 Calendar cal = Calendar.getInstance(); + // 设置 Calendar 的时间为传入的日期 cal.setTimeInMillis(date); + // 调用另一个 setCurrentDate 方法,传入年、月、日、小时和分钟 setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)); } - /** - * Set the current date - * - * @param year The current year - * @param month The current month - * @param dayOfMonth The current dayOfMonth - * @param hourOfDay The current hourOfDay - * @param minute The current minute - */ - public void setCurrentDate(int year, int month, - int dayOfMonth, int hourOfDay, int minute) { + + // 设置当前日期,通过传入年、月、日、小时和分钟 + public void setCurrentDate(int year, int month, int dayOfMonth, int hourOfDay, int minute) { + // 设置年 setCurrentYear(year); + // 设置月 setCurrentMonth(month); + // 设置日 setCurrentDay(dayOfMonth); + // 设置小时 setCurrentHour(hourOfDay); + // 设置分钟 setCurrentMinute(minute); } - /** - * Get current year - * - * @return The current year - */ + + // 获取当前年 public int getCurrentYear() { return mDate.get(Calendar.YEAR); } - /** - * Set current year - * - * @param year The current year - */ + + // 设置当前年 public void setCurrentYear(int year) { if (!mInitialising && year == getCurrentYear()) { return; } mDate.set(Calendar.YEAR, year); + // 更新日期显示相关的控件 updateDateControl(); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } - /** - * Get current month in the year - * - * @return The current month in the year - */ + + // 获取当前月 public int getCurrentMonth() { return mDate.get(Calendar.MONTH); } - /** - * Set current month in the year - * - * @param month The month in the year - */ + + // 设置当前月 public void setCurrentMonth(int month) { if (!mInitialising && month == getCurrentMonth()) { return; } mDate.set(Calendar.MONTH, month); + // 更新日期显示相关的控件 updateDateControl(); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } - /** - * Get current day of the month - * - * @return The day of the month - */ + + // 获取当前日 public int getCurrentDay() { return mDate.get(Calendar.DAY_OF_MONTH); } - /** - * Set current day of the month - * - * @param dayOfMonth The day of the month - */ + + // 设置当前日 public void setCurrentDay(int dayOfMonth) { if (!mInitialising && dayOfMonth == getCurrentDay()) { return; } mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); + // 更新日期显示相关的控件 updateDateControl(); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } - /** - * Get current hour in 24 hour mode, in the range (0~23) - * @return The current hour in 24 hour mode - */ + + // 获取 24 小时制下的当前小时 public int getCurrentHourOfDay() { return mDate.get(Calendar.HOUR_OF_DAY); } + + // 获取当前小时,根据是否 24 小时制进行转换 private int getCurrentHour() { - if (mIs24HourView){ + if (mIs24HourView) { return getCurrentHourOfDay(); } else { int hour = getCurrentHourOfDay(); if (hour > HOURS_IN_HALF_DAY) { return hour - HOURS_IN_HALF_DAY; } else { - return hour == 0 ? HOURS_IN_HALF_DAY : hour; + return hour == 0? HOURS_IN_HALF_DAY : hour; } } } - /** - * Set current hour in 24 hour mode, in the range (0~23) - * - * @param hourOfDay - */ + + // 设置 24 小时制下的当前小时 public void setCurrentHour(int hourOfDay) { if (!mInitialising && hourOfDay == getCurrentHourOfDay()) { return; @@ -383,63 +445,65 @@ public class DateTimePicker extends FrameLayout { hourOfDay = HOURS_IN_HALF_DAY; } } + // 更新上午/下午选择器的显示 updateAmPmControl(); } mHourSpinner.setValue(hourOfDay); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } - /** - * Get currentMinute - * - * @return The Current Minute - */ + + // 获取当前分钟 public int getCurrentMinute() { return mDate.get(Calendar.MINUTE); } - /** - * Set current minute - */ + + // 设置当前分钟 public void setCurrentMinute(int minute) { if (!mInitialising && minute == getCurrentMinute()) { return; } mMinuteSpinner.setValue(minute); mDate.set(Calendar.MINUTE, minute); + // 调用日期时间更改的回调方法 onDateTimeChanged(); } - /** - * @return true if this is in 24 hour view else false. - */ - public boolean is24HourView () { + + // 检查是否使用 24 小时制 + public boolean is24HourView() { return mIs24HourView; } - /** - * Set whether in 24 hour or AM/PM mode. - * - * @param is24HourView True for 24 hour mode. False for AM/PM mode. - */ + + // 设置是否使用 24 小时制 public void set24HourView(boolean is24HourView) { if (mIs24HourView == is24HourView) { return; } mIs24HourView = is24HourView; - mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE); + mAmPmSpinner.setVisibility(is24HourView? View.GONE : View.VISIBLE); int hour = getCurrentHourOfDay(); + // 更新小时显示相关的控件 updateHourControl(); + // 设置当前小时 setCurrentHour(hour); + // 更新上午/下午显示相关的控件 updateAmPmControl(); } + + // 更新日期显示相关的控件 private void updateDateControl() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(mDate.getTimeInMillis()); + // 将日期向前偏移 4 天(一周的一半减 1) cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1); mDateSpinner.setDisplayedValues(null); for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) { + // 循环生成一周内的日期显示字符串 cal.add(Calendar.DAY_OF_YEAR, 1); mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal); } @@ -448,16 +512,22 @@ public class DateTimePicker extends FrameLayout { mDateSpinner.invalidate(); } + + // 更新上午/下午显示相关的控件 private void updateAmPmControl() { if (mIs24HourView) { + // 在 24 小时制下,隐藏上午/下午选择器 mAmPmSpinner.setVisibility(View.GONE); } else { - int index = mIsAm ? Calendar.AM : Calendar.PM; + // 根据当前是上午还是下午设置上午/下午选择器的值 + int index = mIsAm? Calendar.AM : Calendar.PM; mAmPmSpinner.setValue(index); mAmPmSpinner.setVisibility(View.VISIBLE); } } + + // 更新小时显示相关的控件 private void updateHourControl() { if (mIs24HourView) { mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW); @@ -468,18 +538,18 @@ public class DateTimePicker extends FrameLayout { } } - /** - * Set the callback that indicates the 'Set' button has been pressed. - * @param callback the callback, if null will do nothing - */ + + // 设置日期时间更改的监听器 public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) { mOnDateTimeChangedListener = callback; } + + // 当日期时间发生更改时调用该方法 private void onDateTimeChanged() { - if (mOnDateTimeChangedListener != null) { + if (mOnDateTimeChangedListener!= null) { mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(), getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute()); } } -} +} \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePickerDialog.java b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePickerDialog.java index 2c47ba4..c773191 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePickerDialog.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/DateTimePickerDialog.java @@ -14,6 +14,7 @@ * limitations under the License. */ +// 包声明,该类属于 net.micode.notes.ui 包 package net.micode.notes.ui; import java.util.Calendar; @@ -29,62 +30,139 @@ import android.content.DialogInterface.OnClickListener; import android.text.format.DateFormat; import android.text.format.DateUtils; + +// DateTimePickerDialog 类继承自 AlertDialog 并实现 OnClickListener 接口,用于创建一个包含日期时间选择器的对话框 public class DateTimePickerDialog extends AlertDialog implements OnClickListener { + // 存储用户选择的日期和时间信息的 Calendar 对象,使用 Calendar.getInstance() 初始化,以便获取当前日期和时间信息 private Calendar mDate = Calendar.getInstance(); + + // 布尔变量,用于标记是否使用 24 小时制,true 表示使用 24 小时制,false 表示使用 12 小时制 private boolean mIs24HourView; + + // 定义一个接口类型的变量,用于存储用户设置日期时间后的回调监听器 private OnDateTimeSetListener mOnDateTimeSetListener; + + // 日期时间选择器的实例,将其添加到对话框中供用户操作 private DateTimePicker mDateTimePicker; + + // 自定义的接口,用于监听用户设置日期时间的操作 public interface OnDateTimeSetListener { + // 当用户完成日期时间设置后会调用此方法 void OnDateTimeSet(AlertDialog dialog, long date); } + + // 构造函数,接收 Context 和初始日期作为参数 public DateTimePickerDialog(Context context, long date) { + // 调用父类(AlertDialog)的构造函数,传递上下文信息,以完成对话框的基本初始化 super(context); + + + // 创建一个新的 DateTimePicker 实例,将其作为对话框的主要视图元素 mDateTimePicker = new DateTimePicker(context); + + + // 将创建的 DateTimePicker 实例添加到对话框中,使其显示在对话框内 setView(mDateTimePicker); + + + // 为 DateTimePicker 注册日期时间更改监听器,当用户在日期时间选择器中更改日期或时间时会触发该监听器 mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { + @Override public void onDateTimeChanged(DateTimePicker view, int year, int month, - int dayOfMonth, int hourOfDay, int minute) { + int dayOfMonth, int hourOfDay, int minute) { + // 当用户在 DateTimePicker 中选择日期时间后,更新 mDate 中的年信息 mDate.set(Calendar.YEAR, year); + // 更新 mDate 中的月信息,注意 Calendar.MONTH 的值范围是 0 到 11 mDate.set(Calendar.MONTH, month); + // 更新 mDate 中的日信息 mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); + // 更新 mDate 中的小时信息 mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); + // 更新 mDate 中的分钟信息 mDate.set(Calendar.MINUTE, minute); + + + // 调用 updateTitle 方法更新对话框的标题,以反映用户的最新选择 updateTitle(mDate.getTimeInMillis()); } }); + + + // 将传入的初始日期设置到 mDate 中,确保初始日期的正确性 mDate.setTimeInMillis(date); + + + // 将 mDate 的秒设置为 0,以精确到分钟,避免秒级别的精度影响 mDate.set(Calendar.SECOND, 0); + + + // 将 mDate 的毫秒也设置为 0,进一步精确到分钟 + mDate.set(Calendar.MILLISECOND, 0); + + + // 将 DateTimePicker 的当前日期设置为存储在 mDate 中的日期,确保选择器初始显示的日期正确 mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); + + + // 为对话框设置确认按钮,按钮文本从资源文件中获取,并将当前对象作为点击监听器 setButton(context.getString(R.string.datetime_dialog_ok), this); - setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); + + + // 为对话框设置取消按钮,按钮文本从资源文件中获取,点击监听器设置为 null,即点击取消按钮不执行任何操作 + setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener) null); + + + // 根据系统的时间格式设置,决定是否使用 24 小时制,并调用 set24HourView 方法进行设置 set24HourView(DateFormat.is24HourFormat(this.getContext())); + + + // 调用 updateTitle 方法,根据当前的日期和时间设置更新对话框的标题 updateTitle(mDate.getTimeInMillis()); } + + // 设置是否使用 24 小时制的方法 public void set24HourView(boolean is24HourView) { + // 将 mIs24HourView 的值更新为传入的参数,以决定日期时间选择器的显示和操作方式 mIs24HourView = is24HourView; } + + // 设置日期时间设置监听器的方法,用于在用户完成日期时间设置后执行相应操作 public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) { + // 将传入的监听器存储在 mOnDateTimeSetListener 中,以便在用户点击确认按钮时调用 mOnDateTimeSetListener = callBack; } + + // 更新对话框标题的方法,根据日期和时间以及 24 小时制标志更新标题显示 private void updateTitle(long date) { + // 定义日期时间显示的格式标志,这里包含显示年、日期和时间 int flag = - DateUtils.FORMAT_SHOW_YEAR | - DateUtils.FORMAT_SHOW_DATE | - DateUtils.FORMAT_SHOW_TIME; - flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR; + DateUtils.FORMAT_SHOW_YEAR | + DateUtils.FORMAT_SHOW_DATE | + DateUtils.FORMAT_SHOW_TIME; + + + // 根据是否使用 24 小时制添加相应的格式标志,这里存在错误,应修改为 DateUtils.FORMAT_12HOUR + flag |= mIs24HourView? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR; + + + // 使用 DateUtils 类的 formatDateTime 方法将日期时间格式化为字符串,并将其设置为对话框的标题 setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); } + + // 实现 OnClickListener 接口的 onClick 方法,处理用户点击确认按钮的操作 + @Override public void onClick(DialogInterface arg0, int arg1) { - if (mOnDateTimeSetListener != null) { + // 当用户点击确认按钮时,检查是否已设置了 OnDateTimeSetListener + if (mOnDateTimeSetListener!= null) { + // 如果设置了监听器,则调用监听器的 OnDateTimeSet 方法,将当前对话框和用户选择的日期时间传递给监听器 mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); } } - } \ No newline at end of file diff --git a/src/Notesmaster/gradle/wrapper/gradle-wrapper.properties b/src/Notesmaster/gradle/wrapper/gradle-wrapper.properties index 87ca610..0587203 100644 --- a/src/Notesmaster/gradle/wrapper/gradle-wrapper.properties +++ b/src/Notesmaster/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Tue Dec 03 16:56:25 CST 2024 +#Tue Dec 03 17:32:01 CST 2024 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip diff --git a/src/注释.txt b/src/注释.txt deleted file mode 100644 index e69de29..0000000