diff --git a/src/mi_note/.gitignore b/src/mi_note/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/src/mi_note/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/src/mi_note/.idea/.gitignore b/src/mi_note/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/src/mi_note/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/src/mi_note/.idea/AndroidProjectSystem.xml b/src/mi_note/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/src/mi_note/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/mi_note/.idea/checkstyle-idea.xml b/src/mi_note/.idea/checkstyle-idea.xml new file mode 100644 index 0000000..e9f575b --- /dev/null +++ b/src/mi_note/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + 10.21.3 + JavaOnly + true + + + \ No newline at end of file diff --git a/src/mi_note/.idea/compiler.xml b/src/mi_note/.idea/compiler.xml new file mode 100644 index 0000000..b86273d --- /dev/null +++ b/src/mi_note/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/mi_note/.idea/deploymentTargetSelector.xml b/src/mi_note/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..9493d23 --- /dev/null +++ b/src/mi_note/.idea/deploymentTargetSelector.xml @@ -0,0 +1,26 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/.idea/gradle.xml b/src/mi_note/.idea/gradle.xml new file mode 100644 index 0000000..86ab739 --- /dev/null +++ b/src/mi_note/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/src/mi_note/.idea/migrations.xml b/src/mi_note/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/src/mi_note/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/src/mi_note/.idea/misc.xml b/src/mi_note/.idea/misc.xml new file mode 100644 index 0000000..b2c751a --- /dev/null +++ b/src/mi_note/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/.idea/runConfigurations.xml b/src/mi_note/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/src/mi_note/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/src/mi_note/.idea/vcs.xml b/src/mi_note/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/src/mi_note/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/mi_note/app/.gitignore b/src/mi_note/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/src/mi_note/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/src/mi_note/app/build.gradle b/src/mi_note/app/build.gradle new file mode 100644 index 0000000..f6a9329 --- /dev/null +++ b/src/mi_note/app/build.gradle @@ -0,0 +1,50 @@ +plugins { + alias(libs.plugins.android.application) +} + +android { + namespace 'net.micode.notes' + compileSdk 35 + + defaultConfig { + applicationId "net.micode.notes" + minSdk 24 + targetSdk 35 + minSdkVersion 24 + targetSdkVersion 33 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + packagingOptions { + exclude 'META-INF/DEPENDENCIES' + exclude 'META-INF/NOTICE' + exclude 'META-INF/LICENSE' + exclude 'META-INF/LICENSE.txt' + exclude 'META-INF/NOTICE.txt' + } + buildToolsVersion '35.0.0' +} + +dependencies { + implementation 'org.apache.httpcomponents:httpclient:4.5.13' + implementation libs.appcompat + implementation libs.material + implementation libs.activity + implementation libs.constraintlayout + testImplementation libs.junit + androidTestImplementation libs.ext.junit + androidTestImplementation libs.espresso.core +} diff --git a/src/mi_note/app/proguard-rules.pro b/src/mi_note/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/src/mi_note/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/src/mi_note/app/src/androidTest/java/com/example/mi_note/ExampleInstrumentedTest.java b/src/mi_note/app/src/androidTest/java/com/example/mi_note/ExampleInstrumentedTest.java new file mode 100644 index 0000000..c8d6ddd --- /dev/null +++ b/src/mi_note/app/src/androidTest/java/com/example/mi_note/ExampleInstrumentedTest.java @@ -0,0 +1,43 @@ +// 声明当前Java文件所在的包路径(包名) +package com.example.mi_note; + +// 导入Android上下文相关类 +import android.content.Context; + +// 导入AndroidX测试库的核心测试工具类 +import androidx.test.platform.app.InstrumentationRegistry; +// 导入AndroidX测试运行器 +import androidx.test.ext.junit.runners.AndroidJUnit4; + +// 导入JUnit测试框架注解 +import org.junit.Test; +// 导入JUnit测试运行器接口 +import org.junit.runner.RunWith; + +// 导入JUnit断言方法 +import static org.junit.Assert.*; + +/** + * 设备测试类(在Android设备/模拟器上运行的测试) + * + * @see Testing documentation + */ +// 使用AndroidJUnit4测试运行器 +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + + // 标识这是一个测试方法 + @Test + public void useAppContext() { + // 获取被测应用的上下文对象 + // InstrumentationRegistry是测试环境的注册中心 + // getInstrumentation()获取当前测试的Instrumentation实例 + // getTargetContext()返回被测应用的上下文 + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + // 使用断言验证包名是否正确 + // 参数1:预期值(期望的包名) + // 参数2:实际值(从上下文中获取的包名) + assertEquals("com.example.mi_note", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/AndroidManifest.xml b/src/mi_note/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..58fdfcb --- /dev/null +++ b/src/mi_note/app/src/main/AndroidManifest.xml @@ -0,0 +1,152 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/data/Contact.java b/src/mi_note/app/src/main/java/net/micode/notes/data/Contact.java new file mode 100644 index 0000000..4cc0931 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/data/Contact.java @@ -0,0 +1,105 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 声明该类所在的包 +package net.micode.notes.data; + +// 导入android.content 包下的Context类,用于访问应用程序资源和类 +import android.content.Context; +// 导入android.database 包下的Cursor类,用于遍历查询结果集 +import android.database.Cursor; +// 导入android.provider.ContactsContract.CommonDataKinds 包下的Phone类,用于访问联系人的电话号码相关信息 +import android.provider.ContactsContract.CommonDataKinds.Phone; +// 导入android.provider.ContactsContract 包下的Data类,用于访问联系人的数据 +import android.provider.ContactsContract.Data; +// 导入android.telephony 包下的PhoneNumberUtils类,用于处理电话号码相关操作 +import android.telephony.PhoneNumberUtils; +// 导入android.util 包下的Log类,用于日志记录 +import android.util.Log; + +// 导入java.util 包下的HashMap类,用于存储键值对 +import java.util.HashMap; + +// 定义一个名为Contact的公共类 +public class Contact { + // 声明一个静态的HashMap对象sContactCache,用于缓存电话号码和对应的联系人姓名 + private static HashMap sContactCache; + // 定义一个静态常量TAG,用于日志标记 + private static final String TAG = "Contact"; + + // 定义一个静态常量CALLER_ID_SELECTION,用于构建查询联系人的SQL选择语句 + private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER + + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + + " AND " + Data.RAW_CONTACT_ID + " IN " + + "(SELECT raw_contact_id " + + " FROM phone_lookup" + + " WHERE min_match = '+')"; + + /** + * 根据电话号码获取联系人姓名 + * @param context 上下文对象,用于访问内容解析器 + * @param phoneNumber 要查询的电话号码 + * @return 联系人姓名,如果未找到则返回null + */ + public static String getContact(Context context, String phoneNumber) { + // 检查sContactCache是否为空,如果为空则进行初始化 + if(sContactCache == null) { + sContactCache = new HashMap<>(); + } + + // 检查sContactCache中是否已经包含该电话号码 + if(sContactCache.containsKey(phoneNumber)) { + // 如果包含,则直接从缓存中获取联系人姓名并返回 + return sContactCache.get(phoneNumber); + } + + // 替换CALLER_ID_SELECTION中的占位符"+"为电话号码的最小匹配格式 + String selection = CALLER_ID_SELECTION.replace("+", + PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); + // 使用上下文的内容解析器进行查询操作 + Cursor cursor = context.getContentResolver().query( + // 查询的内容URI + Data.CONTENT_URI, + // 查询要返回的列,这里只需要联系人的显示名称 + new String [] { Phone.DISPLAY_NAME }, + // 查询的选择条件 + selection, + // 选择条件中的参数 + new String[] { phoneNumber }, + // 排序规则,这里为null表示不排序 + null); + + // 检查游标是否不为空且能移动到第一行 + if (cursor != null && cursor.moveToFirst()) { + try { + // 从游标中获取联系人的显示名称 + String name = cursor.getString(0); + // 将电话号码和对应的联系人姓名存入缓存 + sContactCache.put(phoneNumber, name); + // 返回联系人姓名 + return name; + } catch (IndexOutOfBoundsException e) { + // 捕获游标获取字符串时可能出现的索引越界异常,并记录错误日志 + Log.e(TAG, " Cursor get string error " + e.toString()); + // 出现异常则返回null + return null; + } finally { + // 无论是否出现异常,最后都要关闭游标 + cursor.close(); + } + } else { + // 如果游标为空或无法移动到第一行,记录未找到匹配联系人的日志 + Log.d(TAG, "No contact matched with number:" + phoneNumber); + // 返回null表示未找到匹配的联系人 + return null; + } + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/data/Notes.java b/src/mi_note/app/src/main/java/net/micode/notes/data/Notes.java new file mode 100644 index 0000000..6f703fc --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/data/Notes.java @@ -0,0 +1,280 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义Notes应用的数据库结构及相关常量 +package net.micode.notes.data; + +// 导入Android的Uri类,用于处理内容URI +import android.net.Uri; + +// Notes类定义数据库表和字段的常量 +public class Notes { + // 内容提供者的Authority标识 + 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; // 系统文件夹 + + /** + * 以下是系统文件夹的标识ID + * {@link Notes#ID_ROOT_FOLDER } 是默认文件夹 + * {@link Notes#ID_TEMPARAY_FOLDER } 用于存放无归属文件夹的笔记 + * {@link Notes#ID_CALL_RECORD_FOLDER} 用于存放通话记录 + */ + public static final int ID_ROOT_FOLDER = 0; // 根文件夹ID + public static final int ID_TEMPARAY_FOLDER = -1; // 临时文件夹ID + public static final int ID_CALL_RECORD_FOLDER = -2; // 通话记录文件夹ID + public static final int ID_TRASH_FOLER = -3; // 回收站文件夹ID + + // 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"; // 背景色ID + public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; // 小部件ID + public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; // 小部件类型 + public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; // 文件夹ID + public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; // 通话日期 + + // 小部件类型常量 + public static final int TYPE_WIDGET_INVALIDE = -1; // 无效小部件 + public static final int TYPE_WIDGET_2X = 0; // 2x大小小部件 + public static final int TYPE_WIDGET_4X = 1; // 4x大小小部件 + + // 数据常量内部类 + public static class DataConstants { + public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; // 文本笔记的MIME类型 + public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; // 通话笔记的MIME类型 + } + + /** + * 查询所有笔记和文件夹的URI + */ + public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); + + /** + * 查询数据的URI + */ + public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + + // 笔记表列名接口 + public interface NoteColumns { + /** + * 行的唯一ID + *

类型: INTEGER (long)

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

类型: INTEGER (long)

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

类型: INTEGER (long)

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

类型: INTEGER (long)

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

类型: INTEGER (long)

+ */ + public static final String ALERTED_DATE = "alert_date"; + + /** + * 文件夹名称或笔记的文本内容 + *

类型: TEXT

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

类型: INTEGER (long)

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

类型: INTEGER (long)

+ */ + public static final String WIDGET_TYPE = "widget_type"; + + /** + * 笔记的背景色ID + *

类型: INTEGER (long)

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

类型: INTEGER

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

类型: INTEGER (long)

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

类型: INTEGER

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

类型: INTEGER (long)

+ */ + public static final String SYNC_ID = "sync_id"; + + /** + * 标记是否本地修改过 + *

类型: INTEGER

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

类型: INTEGER

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

类型: TEXT

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

类型: INTEGER (long)

+ */ + public static final String VERSION = "version"; + } + + // 数据表列名接口 + public interface DataColumns { + /** + * 行的唯一ID + *

类型: INTEGER (long)

+ */ + public static final String ID = "_id"; + + /** + * 该行数据项的MIME类型 + *

类型: Text

+ */ + public static final String MIME_TYPE = "mime_type"; + + /** + * 该数据所属笔记的引用ID + *

类型: INTEGER (long)

+ */ + public static final String NOTE_ID = "note_id"; + + /** + * 数据项的创建日期 + *

类型: INTEGER (long)

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

类型: INTEGER (long)

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

类型: TEXT

+ */ + public static final String CONTENT = "content"; + + /** + * 通用数据列1,含义由MIME_TYPE决定,用于整数类型 + *

类型: INTEGER

+ */ + public static final String DATA1 = "data1"; + + /** + * 通用数据列2,含义由MIME_TYPE决定,用于整数类型 + *

类型: INTEGER

+ */ + public static final String DATA2 = "data2"; + + /** + * 通用数据列3,含义由MIME_TYPE决定,用于文本类型 + *

类型: TEXT

+ */ + public static final String DATA3 = "data3"; + + /** + * 通用数据列4,含义由MIME_TYPE决定,用于文本类型 + *

类型: TEXT

+ */ + public static final String DATA4 = "data4"; + + /** + * 通用数据列5,含义由MIME_TYPE决定,用于文本类型 + *

类型: TEXT

+ */ + public static final String DATA5 = "data5"; + } + + // 文本笔记数据列 + public static final class TextNote implements DataColumns { + /** + * 标记文本是否为检查清单模式 + *

类型: Integer 1:检查清单模式 0: 普通模式

+ */ + public static final String MODE = DATA1; + + public static final int MODE_CHECK_LIST = 1; // 检查清单模式 + + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; // 多文本笔记的MIME类型 + + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; // 单文本笔记的MIME类型 + + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); // 文本笔记的URI + } + + // 通话笔记数据列 + public static final class CallNote implements DataColumns { + /** + * 该记录的通话日期 + *

类型: INTEGER (long)

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

类型: TEXT

+ */ + public static final String PHONE_NUMBER = DATA3; + + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; // 多通话笔记的MIME类型 + + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; // 单通话笔记的MIME类型 + + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); // 通话笔记的URI + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java b/src/mi_note/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java new file mode 100644 index 0000000..f0094fe --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java @@ -0,0 +1,389 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + *(注:这是一份标准的Apache许可证2.0版本的开源声明) +*/ + +// 笔记应用的数据库辅助类,用于创建和管理SQLite数据库 +package net.micode.notes.data; + +// 导入所需的Android类 +import android.content.ContentValues; +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +// 导入项目中的笔记数据相关常量 +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; + +// SQLiteOpenHelper的子类,用于管理笔记数据库 +public class NotesDatabaseHelper extends SQLiteOpenHelper { + // 数据库名称常量 + private static final String DB_NAME = "note.db"; + + // 数据库版本号常量 + private static final int DB_VERSION = 4; + + // 内部接口,定义数据库表名 + public interface TABLE { + public static final String NOTE = "note"; // 笔记表名 + public static final String DATA = "data"; // 数据表名 + } + + // 日志标签 + private static final String TAG = "NotesDatabaseHelper"; + + // 单例实例 + private static NotesDatabaseHelper mInstance; + + // 创建笔记表的SQL语句 + private static final String CREATE_NOTE_TABLE_SQL = + "CREATE TABLE " + TABLE.NOTE + "(" + + NoteColumns.ID + " INTEGER PRIMARY KEY," + // ID主键 + NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + // 父ID + NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + // 提醒日期 + NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + // 背景色ID + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 创建日期(当前时间戳) + NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + // 是否有附件 + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 修改日期 + NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + // 笔记数量 + NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + // 内容摘要 + NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + // 类型 + NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + // 小部件ID + NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + // 小部件类型 + NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + // 同步ID + NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + // 本地修改标志 + NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + // 原始父ID + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + // Google任务ID + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + // 版本号 + ")"; + + // 创建数据表的SQL语句 + private static final String CREATE_DATA_TABLE_SQL = + "CREATE TABLE " + TABLE.DATA + "(" + + DataColumns.ID + " INTEGER PRIMARY KEY," + // ID主键 + DataColumns.MIME_TYPE + " TEXT NOT NULL," + // MIME类型 + DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + // 关联的笔记ID + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 创建日期 + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 修改日期 + DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + // 内容 + DataColumns.DATA1 + " INTEGER," + // 通用数据列1 + DataColumns.DATA2 + " INTEGER," + // 通用数据列2 + DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + // 通用数据列3 + DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + // 通用数据列4 + DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + // 通用数据列5 + ")"; + + // 创建数据表note_id索引的SQL语句 + private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = + "CREATE INDEX IF NOT EXISTS note_id_index ON " + + TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; + + /** + * 触发器:当笔记的父ID更新时增加文件夹的笔记计数 + */ + private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = + "CREATE TRIGGER increase_folder_count_on_update "+ + " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + + " END"; + + /** + * 触发器:当笔记的父ID更新时减少原文件夹的笔记计数 + */ + private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = + "CREATE TRIGGER decrease_folder_count_on_update " + + " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + + " AND " + NoteColumns.NOTES_COUNT + ">0" + ";" + + " END"; + + /** + * 触发器:当插入新笔记时增加文件夹的笔记计数 + */ + private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = + "CREATE TRIGGER increase_folder_count_on_insert " + + " AFTER INSERT ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + + " END"; + + /** + * 触发器:当删除笔记时减少文件夹的笔记计数 + */ + private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = + "CREATE TRIGGER decrease_folder_count_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + + " AND " + NoteColumns.NOTES_COUNT + ">0;" + + " END"; + + /** + * 触发器:当插入数据且类型为普通笔记时更新笔记内容 + */ + 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"; + + /** + * 触发器:当更新数据且类型为普通笔记时更新笔记内容 + */ + 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"; + + /** + * 触发器:当删除数据且类型为普通笔记时清空笔记内容 + */ + private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER = + "CREATE TRIGGER update_note_content_on_delete " + + " AFTER delete ON " + TABLE.DATA + + " WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.SNIPPET + "=''" + + " WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" + + " END"; + + /** + * 触发器:当删除笔记时删除关联的所有数据 + */ + private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER = + "CREATE TRIGGER delete_data_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN" + + " DELETE FROM " + TABLE.DATA + + " WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" + + " END"; + + /** + * 触发器:当删除文件夹时删除其中的所有笔记 + */ + private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = + "CREATE TRIGGER folder_delete_notes_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN" + + " DELETE FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + + " END"; + + /** + * 触发器:当文件夹被移动到回收站时,将其中的笔记也移动到回收站 + */ + private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER = + "CREATE TRIGGER folder_move_notes_on_trash " + + " AFTER UPDATE ON " + TABLE.NOTE + + " WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + + " END"; + + // 构造函数 + public NotesDatabaseHelper(Context context) { + super(context, DB_NAME, null, DB_VERSION); + } + + // 创建笔记表 + public void createNoteTable(SQLiteDatabase db) { + db.execSQL(CREATE_NOTE_TABLE_SQL); // 执行建表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"); + db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert"); + db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash"); + + // 创建新触发器 + db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); + db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); + db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER); + db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER); + db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER); + db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER); + db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER); + } + + // 创建系统文件夹 + private void createSystemFolder(SQLiteDatabase db) { + ContentValues values = new ContentValues(); + + /* + * 创建通话记录文件夹 + */ + values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + + /* + * 创建根文件夹(默认文件夹) + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + /* + * 创建临时文件夹(用于移动笔记) + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + /* + * 创建回收站文件夹 + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + } + + // 创建数据表 + public void createDataTable(SQLiteDatabase db) { + db.execSQL(CREATE_DATA_TABLE_SQL); // 执行建表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); + } + return mInstance; + } + + // 数据库创建时的回调方法 + @Override + public void onCreate(SQLiteDatabase db) { + createNoteTable(db); // 创建笔记表 + createDataTable(db); // 创建数据表 + } + + // 数据库升级时的回调方法 + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + boolean reCreateTriggers = false; // 是否需要重建触发器 + boolean skipV2 = false; // 是否跳过V2升级 + + // 从版本1升级到版本2 + if (oldVersion == 1) { + upgradeToV2(db); // 执行V2升级 + skipV2 = true; // 此次升级包含从V2到V3的升级 + oldVersion++; // 版本号递增 + } + + // 从版本2升级到版本3(如果不需要跳过) + if (oldVersion == 2 && !skipV2) { + upgradeToV3(db); // 执行V3升级 + reCreateTriggers = true; // 需要重建触发器 + oldVersion++; // 版本号递增 + } + + // 从版本3升级到版本4 + if (oldVersion == 3) { + upgradeToV4(db); // 执行V4升级 + oldVersion++; // 版本号递增 + } + + // 如果需要重建触发器 + if (reCreateTriggers) { + reCreateNoteTableTriggers(db); // 重建笔记表触发器 + reCreateDataTableTriggers(db); // 重建数据表触发器 + } + + // 如果升级未完成,抛出异常 + if (oldVersion != newVersion) { + throw new IllegalStateException("Upgrade notes database to version " + newVersion + "fails"); + } + } + + // 升级到版本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); // 创建数据表 + } + + // 升级到版本3的实现 + private void upgradeToV3(SQLiteDatabase db) { + // 删除不再使用的触发器 + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update"); + + // 添加gtask_id列 + db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''"); + + // 添加回收站系统文件夹 + ContentValues values = new ContentValues(); + values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + } + + // 升级到版本4的实现 + 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/mi_note/app/src/main/java/net/micode/notes/data/NotesProvider.java b/src/mi_note/app/src/main/java/net/micode/notes/data/NotesProvider.java new file mode 100644 index 0000000..cb18f06 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/data/NotesProvider.java @@ -0,0 +1,314 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 笔记内容提供者的实现类,提供对笔记数据的CRUD操作 +package net.micode.notes.data; + +// 导入所需的Android类 +import android.app.SearchManager; // 搜索相关功能 +import android.content.ContentProvider; // 内容提供者基类 +import android.content.ContentUris; // URI工具类 +import android.content.ContentValues; // 键值对存储类 +import android.content.Intent; // 意图相关 +import android.content.UriMatcher; // URI匹配器 +import android.database.Cursor; // 数据库查询结果 +import android.database.sqlite.SQLiteDatabase; // SQLite数据库 +import android.net.Uri; // URI处理 +import android.text.TextUtils; // 文本处理工具 +import android.util.Log; // 日志工具 + +// 导入项目资源 +import androidx.annotation.NonNull; + +import net.micode.notes.R; // 资源文件 +import net.micode.notes.data.Notes.DataColumns; // 数据列常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列常量 +import net.micode.notes.data.NotesDatabaseHelper.TABLE; // 数据库表名 + +import java.util.Objects; + +// 内容提供者实现类 +public class NotesProvider extends ContentProvider { + private static final UriMatcher mMatcher; // URI匹配器,用于匹配不同的URI请求 + + private NotesDatabaseHelper mHelper; // 数据库帮助类 + + private static final String TAG = "NotesProvider"; // 日志标签 + + // URI类型常量 + private static final int URI_NOTE = 1; // 笔记表操作 + private static final int URI_NOTE_ITEM = 2; // 单条笔记操作 + private static final int URI_DATA = 3; // 数据表操作 + private static final int URI_DATA_ITEM = 4; // 单条数据操作 + private static final int URI_SEARCH = 5; // 搜索操作 + private static final int URI_SEARCH_SUGGEST = 6; // 搜索建议 + + // 静态初始化块,配置URI匹配规则 + static { + mMatcher = new UriMatcher(UriMatcher.NO_MATCH); // 创建URI匹配器 + mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); // 匹配笔记表URI + mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); // 匹配单条笔记URI + mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); // 匹配数据表URI + mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM); // 匹配单条数据URI + mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH); // 匹配搜索URI + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST); // 匹配搜索建议URI + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST); // 匹配带参数的搜索建议URI + } + + /** + * x'0A'代表SQLite中的换行符。对于搜索结果中的标题和内容, + * 我们将删除换行符和空格以显示更多信息。 + */ + // 搜索结果的投影(列)定义 + private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + "," // 笔记ID + + NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + "," // 建议项的额外数据 + + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + "," // 建议项主文本 + + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + "," // 建议项次文本 + + R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + "," // 建议项图标 + + "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + "," // 建议项点击动作 + + "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA; // 建议项数据 + + // 搜索查询SQL语句 + private static final 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; // 只查询普通笔记 + + // 内容提供者创建时调用 + @Override + public boolean onCreate() { + mHelper = NotesDatabaseHelper.getInstance(getContext()); // 获取数据库帮助类实例 + return true; + } + + // 查询方法 + @Override + public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + Cursor c = null; // 查询结果游标 + SQLiteDatabase db = mHelper.getReadableDatabase(); // 获取可读数据库 + String id = null; // ID变量 + switch (mMatcher.match(uri)) { // 根据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); // 从URI中获取笔记ID + 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); // 从URI中获取数据ID + c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id + + parseSelection(selection), selectionArgs, null, null, sortOrder); + break; + case URI_SEARCH: // 搜索请求 + case URI_SEARCH_SUGGEST: // 搜索建议请求 + if (sortOrder != null || projection != null) { + throw new IllegalArgumentException( + "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); + } + + String searchString = null; // 搜索关键字 + if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { // 处理搜索建议请求 + if (uri.getPathSegments().size() > 1) { + searchString = uri.getPathSegments().get(1); // 从URI获取搜索词 + } + } else { // 处理普通搜索请求 + searchString = uri.getQueryParameter("pattern"); // 从查询参数获取搜索模式 + } + + if (TextUtils.isEmpty(searchString)) { // 搜索词为空则返回空 + return null; + } + + try { + searchString = String.format("%%%s%%", searchString); // 添加通配符 + c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, // 执行原始查询 + new String[] { searchString }); + } catch (IllegalStateException ex) { + Log.e(TAG, "got exception: " + ex.toString()); // 记录异常 + } + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); // 未知URI异常 + } + if (c != null) { + c.setNotificationUri(Objects.requireNonNull(getContext()).getContentResolver(), uri); // 设置通知URI + } + return c; // 返回查询结果 + } + + // 插入方法 + @Override + public Uri insert(@NonNull Uri uri, ContentValues values) { + SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写数据库 + long dataId = 0, noteId = 0, insertedId = 0; // ID变量 + insertedId = switch (mMatcher.match(uri)) { // 根据URI类型处理不同插入 + case URI_NOTE -> // 插入笔记 + noteId = db.insert(TABLE.NOTE, null, values); + 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()); + } + yield dataId = db.insert(TABLE.DATA, null, values); + } + default -> throw new IllegalArgumentException("Unknown URI " + uri); // 未知URI异常 + }; + // 通知笔记URI变更 + if (noteId > 0) { + Objects.requireNonNull(getContext()).getContentResolver().notifyChange( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); + } + + // 通知数据URI变更 + if (dataId > 0) { + Objects.requireNonNull(getContext()).getContentResolver().notifyChange( + ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); + } + + return ContentUris.withAppendedId(uri, insertedId); // 返回插入数据的URI + } + + // 删除方法 + @Override + public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) { + int count = 0; // 删除计数 + String id = null; // ID变量 + SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写数据库 + boolean deleteData = false; // 数据删除标志 + switch (mMatcher.match(uri)) { // 根据URI类型处理不同删除 + case URI_NOTE: // 删除笔记 + selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; // 只删除ID大于0的笔记 + count = db.delete(TABLE.NOTE, selection, selectionArgs); + break; + case URI_NOTE_ITEM: // 删除单条笔记 + id = uri.getPathSegments().get(1); // 从URI获取笔记ID + /* + * 小于0的ID是系统文件夹,不允许删除 + */ + long noteId = Long.parseLong(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); // 从URI获取数据ID + count = db.delete(TABLE.DATA, + DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs); + deleteData = true; // 标记为数据删除 + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); // 未知URI异常 + } + if (count > 0) { // 如果有数据被删除 + if (deleteData) { // 如果是数据删除,通知笔记URI变更 + Objects.requireNonNull(getContext()).getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); + } + Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null); // 通知当前URI变更 + } + return count; // 返回删除数量 + } + + // 更新方法 + @Override + public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) { + int count = 0; // 更新计数 + String id = null; // ID变量 + SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写数据库 + boolean updateData = false; // 数据更新标志 + switch (mMatcher.match(uri)) { // 根据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); // 从URI获取笔记ID + increaseNoteVersion(Long.parseLong(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); // 从URI获取数据ID + count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id + + parseSelection(selection), selectionArgs); + updateData = true; // 标记为数据更新 + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); // 未知URI异常 + } + + if (count > 0) { // 如果有数据被更新 + if (updateData) { // 如果是数据更新,通知笔记URI变更 + Objects.requireNonNull(getContext()).getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); + } + Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null); // 通知当前URI变更 + } + return count; // 返回更新数量 + } + + // 解析选择条件,添加AND连接 + private String parseSelection(String selection) { + return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); + } + + // 增加笔记版本号 + private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { + StringBuilder sql = new StringBuilder(120); // SQL语句构建器 + sql.append("UPDATE "); + sql.append(TABLE.NOTE); + sql.append(" SET "); + sql.append(NoteColumns.VERSION); + sql.append("=" + NoteColumns.VERSION + "+1 "); // 版本号+1 + + if (id > 0 || !TextUtils.isEmpty(selection)) { // 有条件时添加WHERE + sql.append(" WHERE "); + } + if (id > 0) { // 指定ID的条件 + sql.append(NoteColumns.ID + "=").append(id); + } + if (!TextUtils.isEmpty(selection)) { // 处理其他条件 + String selectString = id > 0 ? parseSelection(selection) : selection; + for (String args : selectionArgs) { // 替换参数占位符 + selectString = selectString.replaceFirst("\\?", args); + } + sql.append(selectString); + } + + mHelper.getWritableDatabase().execSQL(sql.toString()); // 执行SQL + } + + // 获取URI类型(未实现) + @Override + public String getType(@NonNull Uri uri) { + // TODO Auto-generated method stub + return null; + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/MetaData.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/MetaData.java new file mode 100644 index 0000000..377521f --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/MetaData.java @@ -0,0 +1,132 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义MetaData类所在的包路径 +package net.micode.notes.gtask.data; + +// 导入Android数据库Cursor类 +import android.database.Cursor; +// 导入Android日志工具类 +import android.util.Log; + +// 导入应用中自定义的字符串工具类 +import net.micode.notes.tool.GTaskStringUtils; + +// 导入JSON处理相关类 +import org.json.JSONException; +import org.json.JSONObject; + +/** + * MetaData类 - 继承自Task类 + * 功能:处理Google Tasks元数据信息 + * 特性: + * 1. 存储关联任务的GID(Google ID) + * 2. 将元数据信息以JSON格式存储在notes字段中 + * 3. 仅用于远程数据同步,不支持本地操作 + */ +public class MetaData extends Task { + // 日志标签,使用类名作为标识 + private final static String TAG = MetaData.class.getSimpleName(); + + // 存储关联任务的Google ID + private String mRelatedGid = null; + + /** + * 设置元数据信息 + * @param gid 关联任务的Google ID + * @param metaInfo 包含元数据的JSON对象 + */ + 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"); // 记录错误日志 + } + // 将JSON对象转为字符串存储到notes字段 + setNotes(metaInfo.toString()); + // 设置固定名称标识这是一个元数据任务 + setName(GTaskStringUtils.META_NOTE_NAME); + } + + /** + * 获取关联任务的Google ID + * @return 关联GID,可能为null + */ + public String getRelatedGid() { + return mRelatedGid; + } + + /** + * 判断是否值得保存 + * @return notes字段不为空时返回true + */ + @Override + public boolean isWorthSaving() { + return getNotes() != null; + } + + /** + * 从远程JSON数据设置内容 + * @param js 包含远程数据的JSON对象 + */ + @Override + public void setContentByRemoteJSON(JSONObject js) { + // 先调用父类方法处理基础字段 + super.setContentByRemoteJSON(js); + + // 如果notes字段不为空,解析其中的GID + if (getNotes() != null) { + try { + // 将notes字符串转为JSON对象 + JSONObject metaInfo = new JSONObject(getNotes().trim()); + // 从JSON中获取关联GID + mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); + } catch (JSONException e) { + Log.w(TAG, "failed to get related gid"); // 记录警告日志 + mRelatedGid = null; // 解析失败时重置GID + } + } + } + + /** + * 从本地JSON设置内容(不支持) + * @param js JSON对象 + * @throws IllegalAccessError 总是抛出此异常 + */ + @Override + public void setContentByLocalJSON(JSONObject js) { + // 元数据不应从本地JSON设置,直接抛出异常 + throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); + } + + /** + * 获取本地JSON内容(不支持) + * @throws IllegalAccessError 总是抛出此异常 + */ + @Override + public JSONObject getLocalJSONFromContent() { + // 元数据不应生成本地JSON,直接抛出异常 + throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); + } + + /** + * 获取同步动作(不支持) + * @param c 数据库Cursor + * @throws IllegalAccessError 总是抛出此异常 + */ + @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/mi_note/app/src/main/java/net/micode/notes/gtask/data/Node.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/Node.java new file mode 100644 index 0000000..ec33700 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/Node.java @@ -0,0 +1,157 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义Node类所在的包路径 +package net.micode.notes.gtask.data; +// 导入Android数据库Cursor类(用于本地数据操作) +import android.database.Cursor; +// 导入JSON处理类 +import org.json.JSONObject; +/** + * 抽象Node类 - GTasks数据同步的基础节点类 + * 功能: + * 1. 定义通用的同步动作常量 + * 2. 提供基本属性管理(GID、名称、修改时间等) + * 3. 声明数据同步的核心抽象方法 + */ +public abstract class Node { + // 同步动作常量:无操作 + public static final int SYNC_ACTION_NONE = 0; + // 同步动作常量:添加到远程 + public static final int SYNC_ACTION_ADD_REMOTE = 1; + // 同步动作常量:添加到本地 + public static final int SYNC_ACTION_ADD_LOCAL = 2; + // 同步动作常量:从远程删除 + public static final int SYNC_ACTION_DEL_REMOTE = 3; + // 同步动作常量:从本地删除 + public static final int SYNC_ACTION_DEL_LOCAL = 4; + // 同步动作常量:更新远程 + public static final int SYNC_ACTION_UPDATE_REMOTE = 5; + // 同步动作常量:更新本地 + public static final int SYNC_ACTION_UPDATE_LOCAL = 6; + // 同步动作常量:更新冲突 + public static final int SYNC_ACTION_UPDATE_CONFLICT = 7; + // 同步动作常量:错误状态 + public static final int SYNC_ACTION_ERROR = 8; + // Google任务ID(全局唯一标识) + private String mGid; + // 节点名称 + private String mName; + // 最后修改时间戳(毫秒) + private long mLastModified; + // 删除标记 + private boolean mDeleted; + + /** + * 构造函数 + * 初始化默认值: + * - GID为null + * - 名称为空字符串 + * - 最后修改时间为0 + * - 未删除状态 + */ + public Node() { + mGid = null; + mName = ""; + mLastModified = 0; + mDeleted = false; + } + /** + * 抽象方法:获取创建动作的JSON数据 + * @param actionId 动作ID + * @return 包含创建动作数据的JSON对象 + */ + public abstract JSONObject getCreateAction(int actionId); + + /** + * 抽象方法:获取更新动作的JSON数据 + * @param actionId 动作ID + * @return 包含更新动作数据的JSON对象 + */ + public abstract JSONObject getUpdateAction(int actionId); + /** + * 抽象方法:从远程JSON数据设置内容 + * @param js 包含远程数据的JSON对象 + */ + public abstract void setContentByRemoteJSON(JSONObject js); + /** + * 抽象方法:从本地JSON数据设置内容 + * @param js 包含本地数据的JSON对象 + */ + public abstract void setContentByLocalJSON(JSONObject js); + /** + * 抽象方法:从内容生成本地JSON数据 + * @return 包含本地数据的JSON对象 + */ + public abstract JSONObject getLocalJSONFromContent(); + /** + * 抽象方法:获取同步动作类型 + * @param c 数据库Cursor对象 + * @return 同步动作常量(SYNC_ACTION_*) + */ + public abstract int getSyncAction(Cursor c); + /** + * 设置Google任务ID + * @param gid 全局唯一标识 + */ + public void setGid(String gid) { + this.mGid = gid; + } + /** + * 设置节点名称 + * @param name 显示名称 + */ + public void setName(String name) { + this.mName = name; + } + /** + * 设置最后修改时间 + * @param lastModified 时间戳(毫秒) + */ + public void setLastModified(long lastModified) { + this.mLastModified = lastModified; + } + /** + * 设置删除标记 + * @param deleted 是否已删除 + */ + public void setDeleted(boolean deleted) { + this.mDeleted = deleted; + } + /** + * 获取Google任务ID + * @return 全局唯一标识(可能为null) + */ + public String getGid() { + return this.mGid; + } + /** + * 获取节点名称 + * @return 显示名称 + */ + public String getName() { + return this.mName; + } + /** + * 获取最后修改时间 + * @return 时间戳(毫秒) + */ + public long getLastModified() { + return this.mLastModified; + } + /** + * 获取删除状态 + * @return 是否已删除 + */ + public boolean getDeleted() { + return this.mDeleted; + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/SqlData.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/SqlData.java new file mode 100644 index 0000000..aa230c3 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/SqlData.java @@ -0,0 +1,258 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义当前类所在的包路径 +package net.micode.notes.gtask.data; + +// 导入Android内容提供者相关类 +import android.content.ContentResolver; // 内容解析器,用于访问ContentProvider +import android.content.ContentUris; // 用于构建带有ID的URI +import android.content.ContentValues; // 键值对存储,用于数据库操作 +import android.content.Context; // 应用上下文 +import android.database.Cursor; // 数据库查询结果集 +import android.net.Uri; // 通用资源标识符 +import android.os.Build; // 获取Android版本信息 +import android.util.Log; // 日志工具 + +// 导入AndroidX注解(版本检查) +import androidx.annotation.RequiresApi; + +// 导入笔记应用数据相关常量 +import net.micode.notes.data.Notes; // 笔记数据常量 +import net.micode.notes.data.Notes.DataColumns; // 数据列名常量 +import net.micode.notes.data.Notes.DataConstants; // 数据相关常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列名常量 +import net.micode.notes.data.NotesDatabaseHelper.TABLE; // 数据库表名 +// 导入同步异常类 +import net.micode.notes.gtask.exception.ActionFailureException; + +// 导入JSON处理相关类 +import org.json.JSONException; // JSON异常 +import org.json.JSONObject; // JSON对象 + +// 导入Java工具类 +import java.util.Objects; // 对象工具类 + +/** + * SqlData类 - 处理笔记数据的SQLite数据库操作 + * 功能: + * 1. 封装笔记数据的增删改查操作 + * 2. 管理本地数据与JSON格式的相互转换 + * 3. 处理数据版本控制 + */ +public class SqlData { + // 日志标签 + private static final String TAG = SqlData.class.getSimpleName(); + + // 无效ID常量 + private static final int INVALID_ID = -99999; + + // 数据查询的列投影(指定需要查询的列) + public static final String[] PROJECTION_DATA = new String[] { + DataColumns.ID, // 数据ID + DataColumns.MIME_TYPE, // MIME类型 + DataColumns.CONTENT, // 内容 + DataColumns.DATA1, // 扩展数据1 + DataColumns.DATA3 // 扩展数据3 + }; + + // 列索引常量 + public static final int DATA_ID_COLUMN = 0; // ID列索引 + public static final int DATA_MIME_TYPE_COLUMN = 1; // MIME类型列索引 + public static final int DATA_CONTENT_COLUMN = 2; // 内容列索引 + public static final int DATA_CONTENT_DATA_1_COLUMN = 3; // 扩展数据1列索引 + public static final int DATA_CONTENT_DATA_3_COLUMN = 4; // 扩展数据3列索引 + + // 内容解析器实例 + private final ContentResolver mContentResolver; + + // 是否为新创建数据的标志 + private boolean mIsCreate; + + // 数据字段 + private long mDataId; // 数据ID + private String mDataMimeType; // MIME类型 + private String mDataContent; // 内容 + private long mDataContentData1; // 扩展数据1(长整型) + private String mDataContentData3; // 扩展数据3(字符串) + + // 存储有变动的数据值(用于增量更新) + private final ContentValues mDiffDataValues; + + /** + * 构造函数(用于创建新数据) + * @param context 应用上下文 + */ + public SqlData(Context context) { + mContentResolver = context.getContentResolver(); + mIsCreate = true; // 标记为新创建 + mDataId = INVALID_ID; // 初始化无效ID + mDataMimeType = DataConstants.NOTE; // 默认MIME类型 + mDataContent = ""; // 空内容 + mDataContentData1 = 0; // 默认扩展数据1 + mDataContentData3 = ""; // 空扩展数据3 + mDiffDataValues = new ContentValues(); // 初始化变更集合 + } + + /** + * 构造函数(基于Cursor加载已有数据) + * @param context 应用上下文 + * @param c 数据库Cursor对象 + */ + public SqlData(Context context, Cursor c) { + mContentResolver = context.getContentResolver(); + mIsCreate = false; // 标记为已存在数据 + loadFromCursor(c); // 从Cursor加载数据 + mDiffDataValues = new ContentValues(); // 初始化变更集合 + } + + /** + * 从Cursor加载数据 + * @param c 数据库Cursor对象 + */ + private void loadFromCursor(Cursor c) { + mDataId = c.getLong(DATA_ID_COLUMN); + mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); + mDataContent = c.getString(DATA_CONTENT_COLUMN); + mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN); + mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); + } + + /** + * 设置内容(从JSON对象) + * @param js 包含数据的JSON对象 + * @throws JSONException JSON解析异常 + */ + public void setContent(JSONObject js) throws JSONException { + // 处理数据ID + long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; + if (mIsCreate || mDataId != dataId) { + mDiffDataValues.put(DataColumns.ID, dataId); + } + mDataId = dataId; + + // 处理MIME类型 + String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE) + : DataConstants.NOTE; + if (mIsCreate || !mDataMimeType.equals(dataMimeType)) { + mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType); + } + mDataMimeType = dataMimeType; + + // 处理内容 + String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : ""; + if (mIsCreate || !mDataContent.equals(dataContent)) { + mDiffDataValues.put(DataColumns.CONTENT, dataContent); + } + mDataContent = dataContent; + + // 处理扩展数据1 + long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0; + if (mIsCreate || mDataContentData1 != dataContentData1) { + mDiffDataValues.put(DataColumns.DATA1, dataContentData1); + } + mDataContentData1 = dataContentData1; + + // 处理扩展数据3 + String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : ""; + if (mIsCreate || !mDataContentData3.equals(dataContentData3)) { + mDiffDataValues.put(DataColumns.DATA3, dataContentData3); + } + mDataContentData3 = dataContentData3; + } + + /** + * 获取内容(转换为JSON对象) + * @return 包含数据的JSON对象 + * @throws JSONException JSON构造异常 + */ + public JSONObject getContent() throws JSONException { + if (mIsCreate) { + Log.e(TAG, "it seems that we haven't created this in database yet"); + return null; + } + // 构建JSON对象 + JSONObject js = new JSONObject(); + js.put(DataColumns.ID, mDataId); + js.put(DataColumns.MIME_TYPE, mDataMimeType); + js.put(DataColumns.CONTENT, mDataContent); + js.put(DataColumns.DATA1, mDataContentData1); + js.put(DataColumns.DATA3, mDataContentData3); + return js; + } + + /** + * 提交数据到数据库 + * @param noteId 关联的笔记ID + * @param validateVersion 是否验证版本 + * @param version 版本号(当validateVersion为true时使用) + */ + @RequiresApi(api = Build.VERSION_CODES.R) + public void commit(long noteId, boolean validateVersion, long version) { + // 处理新建数据的情况 + if (mIsCreate) { + // 如果ID无效且有ID变更,移除无效ID + if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { + mDiffDataValues.remove(DataColumns.ID); + } + + // 设置关联笔记ID + mDiffDataValues.put(DataColumns.NOTE_ID, noteId); + // 插入新数据 + Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues); + try { + // 从返回的URI中解析出新数据的ID + mDataId = Long.parseLong(Objects.requireNonNull(uri).getPathSegments().get(1)); + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e); + throw new ActionFailureException("create note failed"); + } + } else { + // 处理更新已有数据的情况 + if (!mDiffDataValues.isEmpty()) { + int result; + if (!validateVersion) { + // 不验证版本的普通更新 + result = mContentResolver.update( + ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, mDataId), + mDiffDataValues, + null, + null + ); + } else { + // 带版本验证的更新 + result = mContentResolver.update( + ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, mDataId), + mDiffDataValues, + " ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.VERSION + "=?)", + new String[] { String.valueOf(noteId), String.valueOf(version) } + ); + } + if (result == 0) { + Log.w(TAG, "there is no update. maybe user updates note when syncing"); + } + } + } + + // 重置状态 + mDiffDataValues.clear(); + mIsCreate = false; + } + /** + * 获取数据ID + * @return 数据ID + */ + public long getId() { + return mDataId; + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/SqlNote.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/SqlNote.java new file mode 100644 index 0000000..75751f6 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/SqlNote.java @@ -0,0 +1,628 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义当前类所在的包路径 +package net.micode.notes.gtask.data; +// Android Widget相关类 +import android.appwidget.AppWidgetManager; // 管理应用小部件 +// Android内容提供者相关类 +import android.content.ContentResolver; // 内容解析器,用于访问ContentProvider +import android.content.ContentValues; // 键值对存储,用于数据库操作 +import android.content.Context; // 应用上下文 +// Android数据库相关类 +import android.database.Cursor; // 数据库查询结果集 +// Android网络相关类 +import android.net.Uri; // 通用资源标识符 +// Android系统相关类 +import android.os.Build; // 获取Android版本信息 +import android.util.Log; // 日志工具 +// AndroidX注解支持 +import androidx.annotation.RequiresApi; // 版本要求注解 +// 笔记应用数据相关类 +import net.micode.notes.data.Notes; // 笔记数据常量 +import net.micode.notes.data.Notes.DataColumns; // 数据表列名常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记表列名常量 +// 异常处理类 +import net.micode.notes.gtask.exception.ActionFailureException; // 同步操作失败异常 +// 工具类 +import net.micode.notes.tool.GTaskStringUtils; // Google Task字符串工具 +import net.micode.notes.tool.ResourceParser; // 资源解析工具 +// JSON处理相关类 +import org.json.JSONArray; // JSON数组处理 +import org.json.JSONException; // JSON异常 +import org.json.JSONObject; // JSON对象处理 +// Java集合类 +import java.util.ArrayList; // 动态数组 +import java.util.Objects; // 对象工具类 + +// 定义SqlNote类,用于管理笔记数据的数据库操作 +public class SqlNote { + // 日志标签 + private static final String TAG = SqlNote.class.getSimpleName(); + + // 无效ID常量 + private static final int INVALID_ID = -99999; + + // 笔记数据查询投影列(指定需要查询的字段) + public static final String[] PROJECTION_NOTE = new String[] { + NoteColumns.ID, // 笔记ID + NoteColumns.ALERTED_DATE, // 提醒日期 + NoteColumns.BG_COLOR_ID, // 背景颜色ID + NoteColumns.CREATED_DATE, // 创建日期 + NoteColumns.HAS_ATTACHMENT, // 是否有附件 + NoteColumns.MODIFIED_DATE, // 修改日期 + NoteColumns.NOTES_COUNT, // 子笔记数量 + NoteColumns.PARENT_ID, // 父笔记ID + NoteColumns.SNIPPET, // 内容摘要 + NoteColumns.TYPE, // 笔记类型 + NoteColumns.WIDGET_ID, // 小部件ID + NoteColumns.WIDGET_TYPE, // 小部件类型 + NoteColumns.SYNC_ID, // 同步ID + NoteColumns.LOCAL_MODIFIED, // 本地修改标记 + NoteColumns.ORIGIN_PARENT_ID, // 原始父笔记ID + NoteColumns.GTASK_ID, // Google任务ID + NoteColumns.VERSION // 数据版本 + }; + + // 列索引常量定义 + public static final int ID_COLUMN = 0; // ID列索引 + public static final int ALERTED_DATE_COLUMN = 1; // 提醒日期列索引 + public static final int BG_COLOR_ID_COLUMN = 2; // 背景颜色列索引 + public static final int CREATED_DATE_COLUMN = 3; // 创建日期列索引 + public static final int HAS_ATTACHMENT_COLUMN = 4; // 附件标记列索引 + public static final int MODIFIED_DATE_COLUMN = 5; // 修改日期列索引 + public static final int NOTES_COUNT_COLUMN = 6; // 子笔记数列索引 + public static final int PARENT_ID_COLUMN = 7; // 父笔记ID列索引 + public static final int SNIPPET_COLUMN = 8; // 摘要列索引 + public static final int TYPE_COLUMN = 9; // 类型列索引 + public static final int WIDGET_ID_COLUMN = 10; // 小部件ID列索引 + public static final int WIDGET_TYPE_COLUMN = 11; // 小部件类型列索引 + public static final int SYNC_ID_COLUMN = 12; // 同步ID列索引 + public static final int LOCAL_MODIFIED_COLUMN = 13; // 本地修改列索引 + public static final int ORIGIN_PARENT_ID_COLUMN = 14; // 原始父笔记ID列索引 + public static final int GTASK_ID_COLUMN = 15; // Google任务ID列索引 + public static final int VERSION_COLUMN = 16; // 版本列索引 + + // 上下文对象 + private final Context mContext; + // 内容解析器 + private final ContentResolver mContentResolver; + // 是否为新建笔记标记 + private boolean mIsCreate; + + // 笔记字段 + private long mId; // 笔记ID + private long mAlertDate; // 提醒时间 + private int mBgColorId; // 背景颜色ID + private long mCreatedDate; // 创建时间 + private int mHasAttachment; // 是否有附件(0/1) + private long mModifiedDate; // 最后修改时间 + private long mParentId; // 父笔记ID + private String mSnippet; // 内容摘要 + private int mType; // 笔记类型 + private int mWidgetId; // 关联小部件ID + private int mWidgetType; // 小部件类型 + private long mOriginParent; // 原始父笔记ID + private long mVersion; // 数据版本 + + // 存储变动的笔记值 + private final ContentValues mDiffNoteValues; + // 笔记关联的数据列表 + private final ArrayList mDataList; + + /** + * 构造函数(用于创建新笔记) + * @param context 应用上下文 + */ + public SqlNote(Context context) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = true; // 标记为新创建 + // 初始化默认值 + mId = INVALID_ID; + mAlertDate = 0; + mBgColorId = ResourceParser.getDefaultBgId(context); // 获取默认背景 + mCreatedDate = System.currentTimeMillis(); // 当前时间为创建时间 + mHasAttachment = 0; + mModifiedDate = System.currentTimeMillis(); // 当前时间为修改时间 + mParentId = 0; + mSnippet = ""; + mType = Notes.TYPE_NOTE; // 默认为普通笔记类型 + mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; // 无效小部件ID + mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 无效小部件类型 + mOriginParent = 0; + mVersion = 0; + mDiffNoteValues = new ContentValues(); // 初始化变更值容器 + mDataList = new ArrayList<>(); // 初始化数据列表 + } + + /** + * 构造函数(基于Cursor加载已有笔记) + * @param context 应用上下文 + * @param c 数据库Cursor + */ + public SqlNote(Context context, Cursor c) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = false; // 标记为已有笔记 + loadFromCursor(c); // 从Cursor加载数据 + mDataList = new ArrayList<>(); + // 如果是普通笔记类型,加载关联数据 + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + mDiffNoteValues = new ContentValues(); + } + + /** + * 构造函数(基于笔记ID加载) + * @param context 应用上下文 + * @param id 笔记ID + */ + public SqlNote(Context context, long id) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = false; // 标记为已有笔记 + loadFromCursor(id); // 通过ID加载笔记 + mDataList = new ArrayList<>(); + // 如果是普通笔记类型,加载关联数据 + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + mDiffNoteValues = new ContentValues(); + } + + /** + * 通过笔记ID从数据库加载数据 + * @param id 笔记ID + */ + private void loadFromCursor(long id) { + // 使用try-with-resources确保Cursor自动关闭 + try (Cursor c = mContentResolver.query( + Notes.CONTENT_NOTE_URI, // 笔记内容URI + PROJECTION_NOTE, // 查询列 + "(_id=?)", // 查询条件 + new String[]{ String.valueOf(id) }, // 参数 + null)) { // 排序 + if (c != null) { + c.moveToNext(); // 移动到第一行 + loadFromCursor(c); // 从Cursor加载数据 + } else { + Log.w(TAG, "loadFromCursor: cursor = null"); + } + } + } + + /** + * 从Cursor加载笔记数据 + * @param c 数据库Cursor + */ + private void loadFromCursor(Cursor c) { + mId = c.getLong(ID_COLUMN); + mAlertDate = c.getLong(ALERTED_DATE_COLUMN); + mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); + mCreatedDate = c.getLong(CREATED_DATE_COLUMN); + mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN); + mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN); + mParentId = c.getLong(PARENT_ID_COLUMN); + mSnippet = c.getString(SNIPPET_COLUMN); + mType = c.getInt(TYPE_COLUMN); + mWidgetId = c.getInt(WIDGET_ID_COLUMN); + mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); + mVersion = c.getLong(VERSION_COLUMN); + } + + /** + * 加载笔记关联的数据内容 + */ + private void loadDataContent() { + mDataList.clear(); // 清空现有数据 + // 查询关联数据 + try (Cursor c = mContentResolver.query( + Notes.CONTENT_DATA_URI, // 数据内容URI + SqlData.PROJECTION_DATA, // 数据表查询列 + "(note_id=?)", // 查询条件 + new String[]{ String.valueOf(mId) }, // 参数 + null)) { // 排序 + if (c != null) { + if (c.getCount() == 0) { + Log.w(TAG, "it seems that the note has not data"); + return; + } + // 遍历所有数据行 + while (c.moveToNext()) { + SqlData data = new SqlData(mContext, c); // 创建数据对象 + mDataList.add(data); // 添加到列表 + } + } else { + Log.w(TAG, "loadDataContent: cursor = null"); + } + } + } + + /** + * 从JSON设置笔记内容 + * @param js 包含笔记数据的JSON对象 + * @return 是否设置成功 + */ + public boolean setContent(JSONObject js) { + try { + // 获取笔记数据 + JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + + // 检查笔记类型 + if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { + Log.w(TAG, "cannot set system folder"); + } + // 处理文件夹类型 + else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { + // 只能更新摘要和类型 + String snippet = note.has(NoteColumns.SNIPPET) ? + note.getString(NoteColumns.SNIPPET) : ""; + if (mIsCreate || !mSnippet.equals(snippet)) { + mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); + } + mSnippet = snippet; + + int type = note.has(NoteColumns.TYPE) ? + note.getInt(NoteColumns.TYPE) : Notes.TYPE_NOTE; + if (mIsCreate || mType != type) { + mDiffNoteValues.put(NoteColumns.TYPE, type); + } + mType = type; + } + // 处理普通笔记类型 + else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) { + // 获取关联数据数组 + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + + // 处理笔记ID + long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID; + if (mIsCreate || mId != id) { + mDiffNoteValues.put(NoteColumns.ID, id); + } + mId = id; + + // 处理提醒日期 + long alertDate = note.has(NoteColumns.ALERTED_DATE) ? + note.getLong(NoteColumns.ALERTED_DATE) : 0; + if (mIsCreate || mAlertDate != alertDate) { + mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate); + } + mAlertDate = alertDate; + + // 处理背景颜色 + int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? + note.getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext); + if (mIsCreate || mBgColorId != bgColorId) { + mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId); + } + mBgColorId = bgColorId; + + // 处理创建日期 + long createDate = note.has(NoteColumns.CREATED_DATE) ? + note.getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis(); + if (mIsCreate || mCreatedDate != createDate) { + mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate); + } + mCreatedDate = createDate; + + // 处理附件标记 + int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? + note.getInt(NoteColumns.HAS_ATTACHMENT) : 0; + if (mIsCreate || mHasAttachment != hasAttachment) { + mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment); + } + mHasAttachment = hasAttachment; + + // 处理修改日期 + long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? + note.getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis(); + if (mIsCreate || mModifiedDate != modifiedDate) { + mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate); + } + mModifiedDate = modifiedDate; + + // 处理父笔记ID + long parentId = note.has(NoteColumns.PARENT_ID) ? + note.getLong(NoteColumns.PARENT_ID) : 0; + if (mIsCreate || mParentId != parentId) { + mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId); + } + mParentId = parentId; + + // 处理内容摘要 + String snippet = note.has(NoteColumns.SNIPPET) ? + note.getString(NoteColumns.SNIPPET) : ""; + if (mIsCreate || !mSnippet.equals(snippet)) { + mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); + } + mSnippet = snippet; + + // 处理笔记类型 + int type = note.has(NoteColumns.TYPE) ? + note.getInt(NoteColumns.TYPE) : Notes.TYPE_NOTE; + if (mIsCreate || mType != type) { + mDiffNoteValues.put(NoteColumns.TYPE, type); + } + mType = type; + + // 处理小部件ID + int widgetId = note.has(NoteColumns.WIDGET_ID) ? + note.getInt(NoteColumns.WIDGET_ID) : AppWidgetManager.INVALID_APPWIDGET_ID; + if (mIsCreate || mWidgetId != widgetId) { + mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); + } + mWidgetId = widgetId; + + // 处理小部件类型 + int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? + note.getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE; + if (mIsCreate || mWidgetType != widgetType) { + mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); + } + mWidgetType = widgetType; + + // 处理原始父笔记ID + long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? + note.getLong(NoteColumns.ORIGIN_PARENT_ID) : 0; + if (mIsCreate || mOriginParent != originParent) { + mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); + } + mOriginParent = originParent; + + // 处理关联数据 + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + SqlData sqlData = null; + + // 查找已有数据 + if (data.has(DataColumns.ID)) { + long dataId = data.getLong(DataColumns.ID); + for (SqlData temp : mDataList) { + if (dataId == temp.getId()) { + sqlData = temp; + break; + } + } + } + + // 如果不存在则创建新数据 + if (sqlData == null) { + sqlData = new SqlData(mContext); + mDataList.add(sqlData); + } + + // 设置数据内容 + sqlData.setContent(data); + } + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return false; + } + return true; + } + + /** + * 获取笔记内容的JSON表示 + * @return JSON对象,包含笔记数据 + */ + public JSONObject getContent() { + try { + JSONObject js = new JSONObject(); + + // 检查是否已创建 + if (mIsCreate) { + Log.e(TAG, "it seems that we haven't created this in database yet"); + return null; + } + + JSONObject note = new JSONObject(); + // 处理普通笔记类型 + if (mType == Notes.TYPE_NOTE) { + // 添加所有笔记字段 + note.put(NoteColumns.ID, mId); + note.put(NoteColumns.ALERTED_DATE, mAlertDate); + note.put(NoteColumns.BG_COLOR_ID, mBgColorId); + note.put(NoteColumns.CREATED_DATE, mCreatedDate); + note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); + note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); + note.put(NoteColumns.PARENT_ID, mParentId); + note.put(NoteColumns.SNIPPET, mSnippet); + note.put(NoteColumns.TYPE, mType); + note.put(NoteColumns.WIDGET_ID, mWidgetId); + note.put(NoteColumns.WIDGET_TYPE, mWidgetType); + note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent); + js.put(GTaskStringUtils.META_HEAD_NOTE, note); + + // 处理关联数据 + JSONArray dataArray = new JSONArray(); + for (SqlData sqlData : mDataList) { + JSONObject data = sqlData.getContent(); + if (data != null) { + dataArray.put(data); + } + } + js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); + } + // 处理文件夹和系统类型 + else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) { + // 只包含必要字段 + note.put(NoteColumns.ID, mId); + note.put(NoteColumns.TYPE, mType); + note.put(NoteColumns.SNIPPET, mSnippet); + js.put(GTaskStringUtils.META_HEAD_NOTE, note); + } + + return js; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + return null; + } + + /** + * 设置父笔记ID + * @param id 父笔记ID + */ + public void setParentId(long id) { + mParentId = id; + mDiffNoteValues.put(NoteColumns.PARENT_ID, id); + } + + /** + * 设置Google任务ID + * @param gid Google任务ID + */ + public void setGtaskId(String gid) { + mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); + } + + /** + * 设置同步ID + * @param syncId 同步ID + */ + public void setSyncId(long syncId) { + mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); + } + + /** + * 重置本地修改标记 + */ + public void resetLocalModified() { + mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); + } + + /** + * 获取笔记ID + * @return 笔记ID + */ + public long getId() { + return mId; + } + + /** + * 获取父笔记ID + * @return 父笔记ID + */ + public long getParentId() { + return mParentId; + } + + /** + * 获取内容摘要 + * @return 内容摘要 + */ + public String getSnippet() { + return mSnippet; + } + + /** + * 检查是否为普通笔记类型 + * @return 如果是普通笔记返回true + */ + public boolean isNoteType() { + return mType == Notes.TYPE_NOTE; + } + + /** + * 提交笔记数据到数据库 + * @param validateVersion 是否验证版本 + */ + @RequiresApi(api = Build.VERSION_CODES.R) + public void commit(boolean validateVersion) { + // 处理新建笔记 + if (mIsCreate) { + // 如果ID无效且有ID变更,移除无效ID + if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { + mDiffNoteValues.remove(NoteColumns.ID); + } + + // 插入新笔记 + Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); + try { + // 从返回的URI中解析出新笔记ID + mId = Long.parseLong(Objects.requireNonNull(uri).getPathSegments().get(1)); + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e); + throw new ActionFailureException("create note failed"); + } + + // 检查是否创建成功 + if (mId == 0) { + throw new IllegalStateException("Create thread id failed"); + } + + // 如果是普通笔记,提交关联数据 + if (mType == Notes.TYPE_NOTE) { + for (SqlData sqlData : mDataList) { + sqlData.commit(mId, false, -1); + } + } + } + // 处理已有笔记更新 + else { + // 检查笔记ID有效性 + if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) { + Log.e(TAG, "No such note"); + throw new IllegalStateException("Try to update note with invalid id"); + } + + // 如果有变更值 + if (!mDiffNoteValues.isEmpty()) { + mVersion++; // 版本号递增 + int result; + + // 根据是否验证版本执行不同的更新 + if (!validateVersion) { + // 普通更新 + result = mContentResolver.update( + Notes.CONTENT_NOTE_URI, + mDiffNoteValues, + "(" + NoteColumns.ID + "=?)", + new String[]{ String.valueOf(mId) } + ); + } else { + // 带版本验证的更新 + result = mContentResolver.update( + Notes.CONTENT_NOTE_URI, + mDiffNoteValues, + "(" + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", + new String[]{ String.valueOf(mId), String.valueOf(mVersion) } + ); + } + + // 检查更新结果 + if (result == 0) { + Log.w(TAG, "there is no update. maybe user updates note when syncing"); + } + } + + // 如果是普通笔记,提交关联数据 + if (mType == Notes.TYPE_NOTE) { + for (SqlData sqlData : mDataList) { + sqlData.commit(mId, validateVersion, mVersion); + } + } + } + + // 刷新本地数据 + loadFromCursor(mId); + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + + // 重置状态 + mDiffNoteValues.clear(); + mIsCreate = false; + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/Task.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/Task.java new file mode 100644 index 0000000..80afb90 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/Task.java @@ -0,0 +1,454 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义当前类所在的包路径 +package net.micode.notes.gtask.data; + +// Android数据库相关类 +import android.database.Cursor; // 数据库查询结果集 +// Android文本处理工具 +import android.text.TextUtils; // 字符串处理工具类 +// Android日志工具 +import android.util.Log; // 日志记录工具 + +// 笔记应用数据相关常量 +import androidx.annotation.NonNull; + +import net.micode.notes.data.Notes; // 笔记核心数据类 +import net.micode.notes.data.Notes.DataColumns; // 数据表列名常量 +import net.micode.notes.data.Notes.DataConstants; // 数据相关常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记表列名常量 + +// 异常处理类 +import net.micode.notes.gtask.exception.ActionFailureException; // 同步操作失败异常 + +// Google Task工具类 +import net.micode.notes.tool.GTaskStringUtils; // Google Task字符串工具 + +// JSON处理相关类 +import org.json.JSONArray; // JSON数组处理 +import org.json.JSONException; // JSON异常处理 +import org.json.JSONObject; // JSON对象处理 + +// Java工具类 +import java.util.Objects; // 对象工具类 +// Task类继承自Node基类,表示Google Task中的一个任务项 +public class Task extends Node { + // 日志标签 + private static final String TAG = Task.class.getSimpleName(); + + // 任务状态字段 + private boolean mCompleted; // 任务是否已完成 + private String mNotes; // 任务备注/描述信息 + private JSONObject mMetaInfo; // 存储元信息的JSON对象 + + // 任务关系字段 + private Task mPriorSibling; // 前一个兄弟任务(用于排序) + private TaskList mParent; // 所属任务列表 + + // 默认构造函数 + public Task() { + super(); // 调用父类构造函数 + mCompleted = false; // 默认未完成 + mNotes = null; // 默认无备注 + mPriorSibling = null; // 默认无前驱任务 + mParent = null; // 默认无父列表 + mMetaInfo = null; // 默认无元信息 + } + + /** + * 生成创建任务的JSON动作 + * @param actionId 动作ID + * @return 包含创建任务数据的JSON对象 + */ + public JSONObject getCreateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // 设置动作类型为创建 + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); + + // 设置动作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // 设置任务索引位置 + js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this)); + + // 构建任务实体数据 + JSONObject entity = new JSONObject(); + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); // 任务名称 + entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); // 创建者ID + entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_TASK); // 实体类型设为任务 + // 如果有备注则添加 + if (getNotes() != null) { + entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); + } + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); // 添加实体数据 + + // 设置父任务列表ID + js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid()); + + // 设置目标父类型为组 + js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_GROUP); + + // 设置列表ID + js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid()); + + // 如果有前驱任务则设置其ID + if (mPriorSibling != null) { + js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid()); + } + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("生成任务创建JSON对象失败"); + } + + return js; + } + + /** + * 生成更新任务的JSON动作 + * @param actionId 动作ID + * @return 包含更新数据的JSON对象 + */ + public JSONObject getUpdateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // 设置动作类型为更新 + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); + + // 设置动作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // 设置任务全局ID + js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); + + // 构建更新数据 + JSONObject entity = new JSONObject(); + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); // 更新名称 + // 如果有备注则更新 + if (getNotes() != null) { + entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); + } + // 设置删除状态 + entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); // 添加更新数据 + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("生成任务更新JSON对象失败"); + } + + return js; + } + + /** + * 从远程JSON数据设置任务内容 + * @param js 包含远程数据的JSON对象 + */ + public void setContentByRemoteJSON(JSONObject js) { + if (js != null) { + try { + // 设置任务ID + if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { + setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); + } + + // 设置最后修改时间 + if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { + setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); + } + + // 设置任务名称 + if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { + setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); + } + + // 设置任务备注 + if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) { + setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES)); + } + + // 设置删除状态 + if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) { + setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED)); + } + + // 设置完成状态 + if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) { + setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED)); + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("从JSON获取任务内容失败"); + } + } + } + + /** + * 从本地JSON数据设置任务内容 + * @param js 包含本地数据的JSON对象 + */ + public void setContentByLocalJSON(JSONObject js) { + // 检查数据有效性 + if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE) + || !js.has(GTaskStringUtils.META_HEAD_DATA)) { + Log.w(TAG, "setContentByLocalJSON: 无可用数据"); + } + + try { + JSONObject note = Objects.requireNonNull(js).getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + + // 验证类型是否为普通笔记 + if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) { + Log.e(TAG, "无效的类型"); + return; + } + + // 从数据数组中查找笔记内容 + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { + setName(data.getString(DataColumns.CONTENT)); // 设置任务名称 + break; + } + } + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + } + + /** + * 从任务内容生成本地JSON格式 + * @return 包含任务数据的JSON对象 + */ + public JSONObject getLocalJSONFromContent() { + String name = getName(); + try { + if (mMetaInfo == null) { + // 从网页端创建的新任务 + if (name == null) { + Log.w(TAG, "笔记内容为空"); + return null; + } + + // 构建新的JSON结构 + return getJsonObject(name); + } else { + // 已同步的任务 + JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + + // 更新笔记内容 + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { + data.put(DataColumns.CONTENT, getName()); + break; + } + } + + note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 确保类型正确 + return mMetaInfo; // 返回修改后的元信息 + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return null; + } + } + + @NonNull + private static JSONObject getJsonObject(String name) throws JSONException { + JSONObject js = new JSONObject(); + JSONObject note = new JSONObject(); + JSONArray dataArray = new JSONArray(); + JSONObject data = new JSONObject(); + data.put(DataColumns.CONTENT, name); // 设置内容 + dataArray.put(data); + js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); // 添加数据数组 + note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 设置类型 + js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 添加笔记头 + return js; + } + + /** + * 设置任务元信息 + * @param metaData 元数据对象 + */ + public void setMetaInfo(MetaData metaData) { + if (metaData != null && metaData.getNotes() != null) { + try { + // 将元数据中的notes字符串转换为JSON对象 + mMetaInfo = new JSONObject(metaData.getNotes()); + } catch (JSONException e) { + Log.w(TAG, e.toString()); + mMetaInfo = null; + } + } + } + + /** + * 获取同步动作类型 + * @param c 数据库Cursor + * @return 同步动作常量 + */ + public int getSyncAction(Cursor c) { + try { + JSONObject noteInfo = null; + // 检查元信息中是否有笔记头 + if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) { + noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + } + + if (noteInfo == null) { + Log.w(TAG, "笔记元信息似乎已被删除"); + return SYNC_ACTION_UPDATE_REMOTE; // 需要从远程更新 + } + + if (!noteInfo.has(NoteColumns.ID)) { + Log.w(TAG, "远程笔记ID似乎已被删除"); + return SYNC_ACTION_UPDATE_LOCAL; // 需要更新本地 + } + + // 验证笔记ID是否匹配 + if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) { + Log.w(TAG, "笔记ID不匹配"); + return SYNC_ACTION_UPDATE_LOCAL; + } + + if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { + // 本地无修改 + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // 双方都无更新 + return SYNC_ACTION_NONE; + } else { + // 应用远程修改到本地 + return SYNC_ACTION_UPDATE_LOCAL; + } + } else { + // 验证任务ID是否匹配 + if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { + Log.e(TAG, "任务ID不匹配"); + return SYNC_ACTION_ERROR; + } + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // 仅本地有修改 + return SYNC_ACTION_UPDATE_REMOTE; + } else { + // 存在冲突 + return SYNC_ACTION_UPDATE_CONFLICT; + } + } + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + + return SYNC_ACTION_ERROR; // 默认返回错误 + } + + /* + * 检查任务是否值得保存 + * @return 如果有有效数据返回true + */ + /** + * 检查当前任务对象是否包含有效数据,值得保存 + * 满足以下任一条件即认为值得保存: + * 1. 存在元信息数据 或 + * 2. 任务名称非空且不为纯空格 或 + * 3. 任务备注非空且不为纯空格 + * + * @return 如果包含有效数据返回true,否则false + */ + public boolean isWorthSaving() { + return mMetaInfo != null || // 存在元信息 + (getName() != null && !getName().trim().isEmpty()) || // 有有效名称 + (getNotes() != null && !getNotes().trim().isEmpty()); // 有有效备注 + } + + /** + * 设置任务完成状态 + * @param completed true表示任务已完成,false表示未完成 + */ + public void setCompleted(boolean completed) { + this.mCompleted = completed; // 更新完成状态字段 + } + + /** + * 设置任务备注内容 + * @param notes 要设置的备注文本,可为null表示清空备注 + */ + public void setNotes(String notes) { + this.mNotes = notes; // 更新备注字段 + } + + /** + * 设置前驱任务(用于任务排序) + * @param priorSibling 前一个任务节点,可为null表示没有前驱 + */ + public void setPriorSibling(Task priorSibling) { + this.mPriorSibling = priorSibling; // 更新前驱任务引用 + } + + /** + * 设置所属任务列表 + * @param parent 父任务列表对象,不可为null + */ + public void setParent(TaskList parent) { + this.mParent = parent; // 更新父列表引用 + } + + /** + * 获取任务完成状态 + * @return true表示任务已完成,false表示未完成 + */ + public boolean getCompleted() { + return this.mCompleted; // 返回当前完成状态 + } + + /** + * 获取任务备注内容 + * @return 备注文本,可能为null表示无备注 + */ + public String getNotes() { + return this.mNotes; // 返回当前备注内容 + } + + /** + * 获取前驱任务(用于任务排序) + * @return 前一个任务节点,可能为null表示没有前驱 + */ + public Task getPriorSibling() { + return this.mPriorSibling; // 返回前驱任务引用 + } + + /** + * 获取所属任务列表 + * @return 父任务列表对象 + */ + public TaskList getParent() { + return this.mParent; // 返回父列表引用 + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/TaskList.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/TaskList.java new file mode 100644 index 0000000..27db52b --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/data/TaskList.java @@ -0,0 +1,435 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 声明当前类所在的包路径,属于笔记应用的Google任务数据模块 +package net.micode.notes.gtask.data; +// 导入Android数据库相关类 +import android.database.Cursor; // 用于操作和遍历数据库查询结果集 +import android.util.Log; // Android日志工具类 +// 导入笔记应用的核心数据类 +import net.micode.notes.data.Notes; // 笔记数据常量类 +import net.micode.notes.data.Notes.NoteColumns; // 笔记表列名常量 +// 导入异常处理类 +import net.micode.notes.gtask.exception.ActionFailureException; +// 当Google任务操作失败时抛出的自定义异常 +// 导入Google任务字符串处理工具类 +import net.micode.notes.tool.GTaskStringUtils; +// 包含Google任务API使用的JSON键名常量 +// 导入JSON处理相关类 +import org.json.JSONException; // JSON解析异常类 +import org.json.JSONObject; // JSON对象操作类 +// 导入Java工具类 +import java.util.ArrayList; // 动态数组集合 +import java.util.Objects; // 对象操作工具类(判空等) +// 继承自Node基类,表示Google Tasks中的任务列表 +public class TaskList extends Node { + // 日志标签,使用类名作为标识 + private static final String TAG = TaskList.class.getSimpleName(); + // 任务列表的排序索引 + private int mIndex; + // 存储子任务的动态数组 + private final ArrayList mChildren; + // 构造函数 + public TaskList() { + super(); // 调用父类Node的构造函数 + mChildren = new ArrayList<>(); // 初始化子任务列表 + mIndex = 1; // 默认索引从1开始 + } + /** + * 生成创建任务列表的JSON动作 + * @param actionId 动作ID(用于标识操作序列) + * @return 包含创建命令的JSON对象 + * @throws ActionFailureException 当JSON操作失败时抛出 + */ + public JSONObject getCreateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // 设置动作类型为"create" + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); + + // 设置动作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // 设置任务列表索引 + js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex); + + // 构建实体数据对象 + JSONObject entity = new JSONObject(); + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); // 列表名称 + entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); // 创建者ID + entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_GROUP); // 实体类型设为"group" + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); // 添加实体数据 + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("生成任务列表创建JSON失败"); + } + + return js; + } + + /** + * 生成更新任务列表的JSON动作 + * @param actionId 动作ID + * @return 包含更新命令的JSON对象 + */ + public JSONObject getUpdateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // 设置动作类型为"update" + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); + + // 设置动作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // 设置任务列表全局ID + js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); + + // 构建更新数据对象 + JSONObject entity = new JSONObject(); + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); // 更新名称 + entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); // 删除状态 + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); // 添加更新数据 + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("生成任务列表更新JSON失败"); + } + + return js; + } + + /** + * 从远程JSON数据设置任务列表内容 + * @param js 包含远程数据的JSON对象 + * @throws ActionFailureException 当JSON解析失败时抛出 + */ + public void setContentByRemoteJSON(JSONObject js) { + if (js != null) { + try { + // 设置任务列表ID + if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { + setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); + } + + // 设置最后修改时间戳 + if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { + setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); + } + + // 设置任务列表名称 + if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { + setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); + } + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("从JSON解析任务列表内容失败"); + } + } + } + + /** + * 从本地JSON数据设置任务列表内容 + * @param js 包含本地数据的JSON对象 + */ + public void setContentByLocalJSON(JSONObject js) { + // 检查数据有效性 + if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) { + Log.w(TAG, "setContentByLocalJSON: 无可用数据"); + return; + } + + try { + JSONObject folder = Objects.requireNonNull(js).getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + + // 处理文件夹类型 + if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { + String name = folder.getString(NoteColumns.SNIPPET); + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name); // 添加MIUI前缀 + } + // 处理系统文件夹类型 + else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { + if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER) { + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT); + } else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER) { + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE); + } else { + Log.e(TAG, "无效的系统文件夹"); + } + } else { + Log.e(TAG, "错误的类型"); + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + } + + /** + * 从任务列表内容生成本地JSON格式 + * @return 包含任务列表数据的JSON对象,失败返回null + */ + public JSONObject getLocalJSONFromContent() { + try { + JSONObject js = new JSONObject(); + JSONObject folder = new JSONObject(); + + // 处理文件夹名称(移除MIUI前缀) + String folderName = getName(); + if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)) { + folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length()); + } + + // 设置文件夹基本属性 + folder.put(NoteColumns.SNIPPET, folderName); + + // 设置文件夹类型 + if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT) || + folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE)) { + folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); // 系统文件夹 + } else { + folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); // 普通文件夹 + } + + js.put(GTaskStringUtils.META_HEAD_NOTE, folder); + return js; + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return null; + } + } + + /** + * 获取同步动作类型 + * @param c 数据库Cursor对象,包含本地数据 + * @return 同步动作常量(SYNC_ACTION_*) + */ + public int getSyncAction(Cursor c) { + try { + // 检查本地是否有修改 + if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { + // 无本地修改时 + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + return SYNC_ACTION_NONE; // 双方数据一致 + } else { + return SYNC_ACTION_UPDATE_LOCAL; // 需要更新本地 + } + } else { + // 有本地修改时验证任务ID + if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { + Log.e(TAG, "任务ID不匹配"); + return SYNC_ACTION_ERROR; + } + return SYNC_ACTION_UPDATE_REMOTE; // 需要更新远程 + } + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + return SYNC_ACTION_ERROR; // 默认返回错误 + } + + // 以下是子任务管理方法 + + /** + * 获取子任务数量 + * @return 当前子任务总数 + */ + public int getChildTaskCount() { + return mChildren.size(); + } + + /** + * 添加子任务到列表末尾 + * @param task 要添加的子任务 + * @return 是否添加成功 + */ + public boolean addChildTask(Task task) { + boolean ret = false; + if (task != null && !mChildren.contains(task)) { + ret = mChildren.add(task); + if (ret) { + // 设置前驱任务和父列表 + task.setPriorSibling(mChildren.get(mChildren.size() - 1)); + task.setParent(this); + } + } + return ret; + } + + /** + * 在指定位置添加子任务 + * @param task 要添加的子任务 + * @param index 插入位置 + * @return 是否添加成功 + */ + public boolean addChildTask(Task task, int index) { + // 检查索引有效性 + if (index < 0 || index > mChildren.size()) { + Log.e(TAG, "添加子任务:无效的索引"); + return false; + } + + // 检查任务是否已存在 + int pos = mChildren.indexOf(task); + if (task != null && pos == -1) { + mChildren.add(index, task); + + // 更新任务链关系 + Task preTask = (index != 0) ? mChildren.get(index - 1) : null; + Task afterTask = (index != mChildren.size() - 1) ? mChildren.get(index + 1) : null; + + task.setPriorSibling(preTask); + if (afterTask != null) { + afterTask.setPriorSibling(task); + } + } + + return true; + } + + /** + * 移除指定子任务 + * @param task 要移除的子任务 + * @return 是否移除成功 + */ + public boolean removeChildTask(Task task) { + boolean ret = false; + int index = mChildren.indexOf(task); + if (index != -1) { + ret = mChildren.remove(task); + + if (ret) { + // 重置被移除任务的关系 + task.setPriorSibling(null); + task.setParent(null); + + // 更新剩余任务的关系 + if (index < mChildren.size()) { + mChildren.get(index).setPriorSibling( + (index == 0) ? null : mChildren.get(index - 1)); + } + } + } + return ret; + } + + /** + * 移动子任务到新位置 + * @param task 要移动的子任务 + * @param index 目标位置 + * @return 是否移动成功 + */ + public boolean moveChildTask(Task task, int index) { + // 检查索引有效性 + if (index < 0 || index >= mChildren.size()) { + Log.e(TAG, "移动子任务:无效的索引"); + return false; + } + + int pos = mChildren.indexOf(task); + if (pos == -1) { + Log.e(TAG, "移动子任务:任务不在列表中"); + return false; + } + + if (pos == index) { + return true; // 位置未改变 + } + + // 先移除再添加实现移动 + return (removeChildTask(task) && addChildTask(task, index)); + } + + // 以下是查询方法 + + /** + * 通过GID查找子任务 + * @param gid 要查找的任务全局ID + * @return 找到的任务对象,未找到返回null + */ + public Task findChildTaskByGid(String gid) { + for (Task t : mChildren) { + if (t.getGid().equals(gid)) { + return t; + } + } + return null; + } + + /** + * 获取子任务索引 + * @param task 要查询的子任务 + * @return 索引位置,不存在返回-1 + */ + public int getChildTaskIndex(Task task) { + return mChildren.indexOf(task); + } + + /** + * 通过索引获取子任务 + * @param index 要获取的索引 + * @return 子任务对象,索引无效返回null + */ + public Task getChildTaskByIndex(int index) { + if (index < 0 || index >= mChildren.size()) { + Log.e(TAG, "getTaskByIndex: 无效的索引"); + return null; + } + return mChildren.get(index); + } + + /** + * 通过GID获取子任务(别名方法) + * @param gid 要查找的任务全局ID + * @return 找到的任务对象,未找到返回null + */ + public Task getChilTaskByGid(String gid) { + return findChildTaskByGid(gid); + } + + /** + * 获取所有子任务列表 + * @return 子任务ArrayList(注意:返回的是引用) + */ + public ArrayList getChildTaskList() { + return this.mChildren; + } + + // 以下是索引属性的getter/setter + + /** + * 设置任务列表索引 + * @param index 新的索引值 + */ + public void setIndex(int index) { + this.mIndex = index; + } + + /** + * 获取当前索引 + * @return 当前索引值 + */ + public int getIndex() { + return this.mIndex; + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/exception/ActionFailureException.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/exception/ActionFailureException.java new file mode 100644 index 0000000..a1945f6 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/exception/ActionFailureException.java @@ -0,0 +1,46 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义该异常类所在的包路径,属于Google任务模块的异常处理包 +package net.micode.notes.gtask.exception; +import java.io.Serial; +/** + * 自定义运行时异常,表示Google Task操作失败 + * 继承自RuntimeException,属于非受检异常(unchecked exception) + * 主要用于GTasks同步过程中的操作失败场景 + */ +public class ActionFailureException extends RuntimeException { + // 序列化版本UID,用于控制反序列化的版本兼容性 + // 保持该值不变可以确保序列化/反序列化的一致性 + @Serial + private static final long serialVersionUID = 4425249765923293627L; + /** + * 默认构造函数,创建不带详细信息的异常对象 + */ + public ActionFailureException() { + super(); // 调用父类RuntimeException的无参构造 + } + /** + * 带错误信息的构造函数 + * @param paramString 错误描述信息,将显示在异常堆栈中 + */ + public ActionFailureException(String paramString) { + super(paramString); // 调用父类带消息的构造方法 + } + /** + * 带错误信息和原因的构造函数 + * @param paramString 错误描述信息 + * @param paramThrowable 导致该异常的原始Throwable对象(原因) + */ + public ActionFailureException(String paramString, Throwable paramThrowable) { + super(paramString, paramThrowable); // 调用父类带cause的构造方法 + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/exception/NetworkFailureException.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/exception/NetworkFailureException.java new file mode 100644 index 0000000..8c644e0 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/exception/NetworkFailureException.java @@ -0,0 +1,50 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 声明当前类所在的包路径,属于Google任务模块的异常处理包 +package net.micode.notes.gtask.exception; +// 导入Java序列化相关注解 +import java.io.Serial; +/** + * 自定义受检异常(checked exception),表示网络操作失败 + * 继承自java.lang.Exception ,调用方必须显式捕获或抛出 + * 主要在网络请求失败、连接超时等场景使用 + */ +public class NetworkFailureException extends Exception { + // 序列化版本唯一标识符,使用@Serial注解标记 + // 该值用于控制不同版本间的序列化兼容性 + @Serial + private static final long serialVersionUID = 2107610287180234136L; + /** + * 默认构造函数,创建无详细错误信息的异常对象 + * 用于不需要额外错误信息的简单场景 + */ + public NetworkFailureException() { + super(); // 调用父类Exception的无参构造方法 + } + /** + * 带错误描述的构造函数 + * @param paramString 错误详细信息,会显示在异常堆栈中 + * 示例:"HTTP 404 Not Found" + */ + public NetworkFailureException(String paramString) { + super(paramString); // 调用父类带错误信息的构造方法 + } + /** + * 完整参数构造函数 + * @param paramString 自定义错误描述信息 + * @param paramThrowable 引起当前异常的根原因(Throwable对象) + * 示例:new NetworkFailureException("请求失败", socketTimeoutEx) + */ + public NetworkFailureException(String paramString, Throwable paramThrowable) { + super(paramString, paramThrowable); // 调用父类带cause的构造方法 + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskASyncTask.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskASyncTask.java new file mode 100644 index 0000000..997eaa9 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskASyncTask.java @@ -0,0 +1,156 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义远程同步任务异步处理类 +package net.micode.notes.gtask.remote; + +// 导入安卓通知相关类 +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +// 导入安卓基础组件 +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Build; + +// 导入AndroidX兼容库注解 +import androidx.annotation.RequiresApi; + +// 导入应用资源 +import net.micode.notes.R; +// 导入界面活动类 +import net.micode.notes.ui.NotesListActivity; +import net.micode.notes.ui.NotesPreferenceActivity; + +// 异步任务类,用于处理Google任务同步 +public class GTaskASyncTask extends AsyncTask { + + // 定义同步完成回调接口 + public interface OnCompleteListener { + void onComplete(); // 同步完成时触发 + } + + // 上下文对象 + private final Context mContext; + // 通知管理器 + private final NotificationManager mNotifiManager; + // Google任务管理器实例 + private final GTaskManager mTaskManager; + // 完成监听器 + private final OnCompleteListener mOnCompleteListener; + + // 构造函数 + public GTaskASyncTask(Context context, OnCompleteListener listener) { + mContext = context; // 初始化上下文 + mOnCompleteListener = listener; // 设置完成监听器 + // 获取系统通知服务 + mNotifiManager = (NotificationManager) mContext + .getSystemService(Context.NOTIFICATION_SERVICE); + // 获取任务管理器单例 + mTaskManager = GTaskManager.getInstance(); + } + + // 取消同步操作 + public void cancelSync() { + mTaskManager.cancelSync(); // 调用任务管理器取消方法 + } + + // 发布进度更新 + public void publishProgess(String message) { + publishProgress(message // 包装成字符串数组 + ); + } + + // 显示通知的方法(兼容新版API) + private void showNotification(int tickerId, String content) { + // 准备跳转意图 + PendingIntent pendingIntent; + // 根据通知类型设置不同的跳转目标 + if (tickerId != R.string.ticker_success) { + // 失败时跳转到设置页 + pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, + NotesPreferenceActivity.class), PendingIntent.FLAG_IMMUTABLE); + } else { + // 成功时跳转到列表页 + pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, + NotesListActivity.class), PendingIntent.FLAG_IMMUTABLE); + } + + // 构建通知对象 + Notification.Builder builder = new Notification.Builder(mContext) + .setAutoCancel(true) // 点击后自动消失 + .setContentTitle(mContext.getString(R.string.app_name)) // 设置标题 + .setContentText(content) // 设置内容 + .setContentIntent(pendingIntent) // 设置点击意图 + .setWhen(System.currentTimeMillis()) // 设置时间 + .setOngoing(true); // 设置为持续通知 + + // 生成通知对象 + Notification notification = builder.getNotification(); + // 定义通知ID + int GTASK_SYNC_NOTIFICATION_ID = 5234235; + // 显示通知 + mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); + } + + // 后台执行方法(API级别限制) + @RequiresApi(api = Build.VERSION_CODES.R) + @Override + protected Integer doInBackground(Void... unused) { + // 发布登录进度通知 + publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity + .getSyncAccountName(mContext))); + // 执行同步操作 + return mTaskManager.sync(mContext, this); + } + + // 进度更新回调 + @Override + protected void onProgressUpdate(String... progress) { + // 显示同步中通知 + showNotification(R.string.ticker_syncing, progress[0]); + // 如果是服务上下文,发送广播 + if (mContext instanceof GTaskSyncService) { + ((GTaskSyncService) mContext).sendBroadcast(progress[0]); + } + } + + // 执行完成回调 + @Override + protected void onPostExecute(Integer result) { + // 根据同步结果显示不同通知 + if (result == GTaskManager.STATE_SUCCESS) { + // 同步成功通知 + showNotification(R.string.ticker_success, mContext.getString( + R.string.success_sync_account, mTaskManager.getSyncAccount())); + // 记录最后同步时间 + NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis()); + } else if (result == GTaskManager.STATE_NETWORK_ERROR) { + // 网络错误通知 + showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network)); + } else if (result == GTaskManager.STATE_INTERNAL_ERROR) { + // 内部错误通知 + showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal)); + } else if (result == GTaskManager.STATE_SYNC_CANCELLED) { + // 取消同步通知 + showNotification(R.string.ticker_cancel, mContext + .getString(R.string.error_sync_cancelled)); + } + + // 触发完成回调 + if (mOnCompleteListener != null) { + // 在新线程中执行回调 + new Thread(mOnCompleteListener::onComplete).start(); + } + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java new file mode 100644 index 0000000..edb338c --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java @@ -0,0 +1,611 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义Google任务远程同步包路径 +package net.micode.notes.gtask.remote; +// 导入Android账号管理相关类 +import android.accounts.Account; // 系统账号对象,存储账号基本信息 +import android.accounts.AccountManager; // 管理系统账号的入口类 +import android.accounts.AccountManagerFuture; // 异步账号操作返回结果 +import android.app.Activity; // 活动组件基类 +import android.os.Bundle; // 键值对数据包,用于组件间数据传输 +import android.text.TextUtils; // 文本处理工具类 +import android.util.Log; // 日志工具 +// 导入AndroidX注解 +import androidx.annotation.NonNull; // 非空参数注解 +// 导入Google任务数据模型 +import net.micode.notes.gtask.data.Node; // 节点基类 +import net.micode.notes.gtask.data.Task; // 任务实体类 +import net.micode.notes.gtask.data.TaskList; // 任务列表实体类 +// 导入自定义异常 +import net.micode.notes.gtask.exception.ActionFailureException; // 操作失败异常 +import net.micode.notes.gtask.exception.NetworkFailureException; // 网络连接异常 +// 导入工具类 +import net.micode.notes.tool.GTaskStringUtils; // Google任务字符串常量工具 +import net.micode.notes.ui.NotesPreferenceActivity; // 设置界面Activity +// 导入Apache HTTP组件(已废弃,但部分旧系统仍在使用) +import org.apache.http.HttpEntity; // HTTP消息实体接口 +import org.apache.http.HttpResponse; // HTTP响应对象 +import org.apache.http.client.entity.UrlEncodedFormEntity; // URL编码表单实体 +import org.apache.http.client.methods.HttpGet; // HTTP GET请求方法 +import org.apache.http.client.methods.HttpPost; // HTTP POST请求方法 +import org.apache.http.cookie.Cookie; // Cookie信息对象 +import org.apache.http.impl.client.BasicCookieStore; // 基础Cookie存储 +import org.apache.http.impl.client.DefaultHttpClient; // 默认HTTP客户端(已废弃) +import org.apache.http.message.BasicNameValuePair; // 键值对参数对象 +import org.apache.http.params.BasicHttpParams; // HTTP参数集合 +import org.apache.http.params.HttpConnectionParams; // 连接相关参数 +import org.apache.http.params.HttpParams; // HTTP参数接口 +import org.apache.http.params.HttpProtocolParams; // 协议相关参数 +// 导入JSON处理类 +import org.json.JSONArray; // JSON数组处理 +import org.json.JSONException; // JSON解析异常 +import org.json.JSONObject; // JSON对象处理 +// 导入IO流相关类 +import java.io.BufferedReader; // 缓冲字符输入流 +import java.io.IOException; // IO异常 +import java.io.InputStream; // 字节输入流 +import java.io.InputStreamReader; // 字节流转字符流 +import java.util.LinkedList; // 链表实现 +import java.util.List; // 列表接口 +// 导入压缩流处理 +import java.util.zip.GZIPInputStream; // GZIP解压流 +import java.util.zip.Inflater; // 解压器 +import java.util.zip.InflaterInputStream; // 解压输入流 + +// Google任务客户端实现类,负责与Google Tasks API交互 +public class GTaskClient { + // 日志标签 + private static final String TAG = GTaskClient.class.getSimpleName(); + // Google任务基础URL + private static final String GTASK_URL = "https://mail.google.com/tasks/"; + // GET请求URL + private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig"; + // POST请求URL + private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig"; + // 单例实例 + private static GTaskClient mInstance = null; + // HTTP客户端(使用已废弃的Apache HTTP Client) + private DefaultHttpClient mHttpClient; + // GET请求实际URL(可能包含自定义域名) + private String mGetUrl; + // POST请求实际URL(可能包含自定义域名) + private String mPostUrl; + // 客户端版本号 + private long mClientVersion; + // 登录状态标识 + private boolean mLoggedin; + // 上次登录时间戳 + private long mLastLoginTime; + // 操作ID计数器 + private int mActionId; + // 当前账号信息 + private Account mAccount; + // 待提交的更新操作集合 + private JSONArray mUpdateArray; + // 私有构造方法(单例模式) + private GTaskClient() { + mHttpClient = null; + mGetUrl = GTASK_GET_URL; // 默认使用官方GET URL + mPostUrl = GTASK_POST_URL; // 默认使用官方POST URL + mClientVersion = -1; // 初始无效版本 + mLoggedin = false; // 初始未登录 + mLastLoginTime = 0; // 初始时间戳 + mActionId = 1; // 操作ID从1开始 + mAccount = null; // 初始无账号 + mUpdateArray = null; // 初始无待更新操作 + } + // 获取单例实例(线程安全) + public static synchronized GTaskClient getInstance() { + if (mInstance == null) { + mInstance = new GTaskClient(); // 懒加载初始化 + } + return mInstance; + } + // 登录方法 + public boolean login(Activity activity) { + // 检查登录是否过期(假设cookie 5分钟后失效) + final long interval = 1000 * 60 * 5; // 5分钟毫秒数 + if (mLastLoginTime + interval < System.currentTimeMillis()) { + mLoggedin = false; // 标记需要重新登录 + } + + // 检查账号是否变更 + if (mLoggedin && !TextUtils.equals(getSyncAccount().name, + NotesPreferenceActivity.getSyncAccountName(activity))) { + mLoggedin = false; // 账号变更需重新登录 + } + + // 已登录直接返回 + if (mLoggedin) { + Log.d(TAG, "已经登录"); + return true; + } + + mLastLoginTime = System.currentTimeMillis(); // 更新登录时间 + String authToken = loginGoogleAccount(activity, false); // 获取认证令牌 + if (authToken == null) { + Log.e(TAG, "Google账号登录失败"); + return false; + } + + // 处理非gmail.com 域名的企业账号 + if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || + mAccount.name.toLowerCase().endsWith("googlemail.com"))) { + // 构建自定义域名URL + StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); + int index = mAccount.name.indexOf('@') + 1; + String suffix = mAccount.name.substring(index); + url.append(suffix).append("/"); + mGetUrl = url + "ig"; // 更新GET URL + mPostUrl = url + "r/ig"; // 更新POST URL + + // 尝试用自定义域名登录 + if (tryToLoginGtask(activity, authToken)) { + mLoggedin = true; + } + } + + // 自定义域名登录失败时尝试官方URL + if (!mLoggedin) { + mGetUrl = GTASK_GET_URL; // 恢复默认GET URL + mPostUrl = GTASK_POST_URL; // 恢复默认POST URL + if (!tryToLoginGtask(activity, authToken)) { + return false; + } + } + + mLoggedin = true; // 标记登录成功 + return true; + } + + // 获取Google账号认证令牌 + private String loginGoogleAccount(Activity activity, boolean invalidateToken) { + String authToken; + // 获取账号管理器实例 + AccountManager accountManager = AccountManager.get(activity); + // 获取所有Google账号 + Account[] accounts = accountManager.getAccountsByType("com.google"); + + if (accounts.length == 0) { + Log.e(TAG, "没有可用的Google账号"); + return null; + } + + // 获取配置中设置的同步账号名 + String accountName = NotesPreferenceActivity.getSyncAccountName(activity); + Account account = null; + // 查找匹配账号 + for (Account a : accounts) { + if (a.name.equals(accountName)) { + account = a; + break; + } + } + if (account != null) { + mAccount = account; // 保存当前账号 + } else { + Log.e(TAG, "找不到设置中配置的账号"); + return null; + } + + // 异步获取认证令牌 + AccountManagerFuture accountManagerFuture = accountManager.getAuthToken( + account, "goanna_mobile", null, activity, null, null); + try { + Bundle authTokenBundle = accountManagerFuture.getResult(); + authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); + // 需要失效令牌时重新获取 + if (invalidateToken) { + accountManager.invalidateAuthToken("com.google", authToken); + loginGoogleAccount(activity, false); + } + } catch (Exception e) { + Log.e(TAG, "获取认证令牌失败"); + authToken = null; + } + + return authToken; + } + + // 尝试登录Google Tasks服务 + private boolean tryToLoginGtask(Activity activity, String authToken) { + if (loginGtask(authToken)) { + // 令牌可能过期,失效后重试 + authToken = loginGoogleAccount(activity, true); + if (authToken == null) { + Log.e(TAG, "Google账号登录失败"); + return false; + } + + if (loginGtask(authToken)) { + Log.e(TAG, "Google Tasks登录失败"); + return false; + } + } + return true; + } + + // 实际执行Google Tasks登录 + private boolean loginGtask(String authToken) { + // 设置HTTP请求超时(连接10秒,socket15秒) + int timeoutConnection = 10000; + int timeoutSocket = 15000; + HttpParams httpParameters = new BasicHttpParams(); + HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); + HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); + + // 初始化HTTP客户端 + mHttpClient = new DefaultHttpClient(httpParameters); + BasicCookieStore localBasicCookieStore = new BasicCookieStore(); + mHttpClient.setCookieStore(localBasicCookieStore); // 设置cookie存储 + HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); + + // 执行登录请求 + try { + String loginUrl = mGetUrl + "?auth=" + authToken; // 构造登录URL + HttpGet httpGet = new HttpGet(loginUrl); + HttpResponse response = mHttpClient.execute(httpGet); + + // 检查认证cookie + List cookies = mHttpClient.getCookieStore().getCookies(); + boolean hasAuthCookie = false; + for (Cookie cookie : cookies) { + if (cookie.getName().contains("GTL")) { + hasAuthCookie = true; + } + } + if (!hasAuthCookie) { + Log.w(TAG, "缺少认证cookie"); + } + + // 从响应中提取客户端版本号 + String resString = getResponseContent(response.getEntity()); + String jsBegin = "_setup("; + String jsEnd = ")}"; + int begin = resString.indexOf(jsBegin); + int end = resString.lastIndexOf(jsEnd); + String jsString = null; + if (begin != -1 && end != -1 && begin < end) { + jsString = resString.substring(begin + jsBegin.length(), end); + } + JSONObject js = new JSONObject(jsString); + mClientVersion = js.getLong("v"); // 保存客户端版本 + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return true; // 解析失败返回true触发重试 + } catch (Exception e) { + Log.e(TAG, "HTTP GET请求失败"); + return true; // 网络异常返回true触发重试 + } + + return false; // 登录成功返回false + } + + // 生成递增的操作ID + private int getActionId() { + return mActionId++; + } + + // 创建HTTP POST请求对象 + private HttpPost createHttpPost() { + HttpPost httpPost = new HttpPost(mPostUrl); + // 设置请求头 + httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); + httpPost.setHeader("AT", "1"); // 认证令牌标识 + return httpPost; + } + + // 从HTTP实体获取响应内容(支持gzip/deflate压缩) + private String getResponseContent(HttpEntity entity) throws IOException { + String contentEncoding = null; + if (entity.getContentEncoding() != null) { + contentEncoding = entity.getContentEncoding().getValue(); + Log.d(TAG, "内容编码: " + contentEncoding); + } + + InputStream input = entity.getContent(); + // 根据编码类型选择解压方式 + if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) { + input = new GZIPInputStream(entity.getContent()); + } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) { + Inflater inflater = new Inflater(true); + input = new InflaterInputStream(entity.getContent(), inflater); + } + + try { + // 读取响应内容 + InputStreamReader isr = new InputStreamReader(input); + BufferedReader br = new BufferedReader(isr); + StringBuilder sb = new StringBuilder(); + + while (true) { + String buff = br.readLine(); + if (buff == null) { + return sb.toString(); // 返回完整内容 + } + sb = sb.append(buff); + } + } finally { + input.close(); // 确保流关闭 + } + } + + // 执行POST请求并返回JSON响应 + private JSONObject postRequest(JSONObject js) throws NetworkFailureException { + if (!mLoggedin) { + Log.e(TAG, "请先登录"); + throw new ActionFailureException("未登录"); + } + + HttpPost httpPost = createHttpPost(); + try { + // 构造请求参数 + LinkedList list = new LinkedList<>(); + list.add(new BasicNameValuePair("r", js.toString())); + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); + httpPost.setEntity(entity); + + // 执行请求并解析响应 + HttpResponse response = mHttpClient.execute(httpPost); + String jsString = getResponseContent(response.getEntity()); + return new JSONObject(jsString); + + } catch (IOException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("POST请求失败"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("响应内容转JSON失败"); + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("请求过程中发生错误"); + } + } + + // 创建任务 + public void createTask(Task task) throws NetworkFailureException { + commitUpdate(); // 先提交待更新操作 + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + + // 添加创建动作 + actionList.put(task.getCreateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // 添加客户端版本 + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + // 发送请求并处理响应 + JSONObject jsResponse = postRequest(jsPost); + JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( + GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("创建任务时处理JSON失败"); + } + } + + // 创建任务列表 + public void createTaskList(TaskList tasklist) throws NetworkFailureException { + commitUpdate(); // 先提交待更新操作 + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + + // 添加创建动作 + actionList.put(tasklist.getCreateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // 添加客户端版本 + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + // 发送请求并处理响应 + JSONObject jsResponse = postRequest(jsPost); + JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( + GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("创建任务列表时处理JSON失败"); + } + } + + // 提交所有待更新操作 + public void commitUpdate() throws NetworkFailureException { + if (mUpdateArray != null) { // 有待更新操作时才执行 + try { + JSONObject jsPost = new JSONObject(); + + // 添加动作列表 + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); + + // 添加客户端版本 + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); // 发送请求 + mUpdateArray = null; // 清空待更新操作 + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("提交更新时处理JSON失败"); + } + } + } + + // 添加节点更新操作 + public void addUpdateNode(Node node) throws NetworkFailureException { + if (node != null) { + // 避免过多更新操作(限制最多10个) + if (mUpdateArray != null && mUpdateArray.length() > 10) { + commitUpdate(); // 超过限制先提交 + } + + if (mUpdateArray == null) + mUpdateArray = new JSONArray(); + mUpdateArray.put(node.getUpdateAction(getActionId())); // 添加更新动作 + } + } + + // 移动任务 + public void moveTask(Task task, TaskList preParent, TaskList curParent) + throws NetworkFailureException { + commitUpdate(); // 先提交待更新操作 + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + JSONObject action = new JSONObject(); + + // 设置移动动作参数 + action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); + action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); + action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); + + // 同列表移动时设置前驱任务ID + if (preParent == curParent && task.getPriorSibling() != null) { + action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling()); + } + + action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); + action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); + + // 跨列表移动时设置目标列表ID + if (preParent != curParent) { + action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid()); + } + + actionList.put(action); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // 添加客户端版本 + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); // 发送请求 + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("移动任务时处理JSON失败"); + } + } + + // 删除节点 + public void deleteNode(Node node) throws NetworkFailureException { + commitUpdate(); // 先提交待更新操作 + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + + // 设置删除标记并添加更新动作 + node.setDeleted(true); + actionList.put(node.getUpdateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // 添加客户端版本 + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); // 发送请求 + mUpdateArray = null; // 清空待更新操作 + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("删除节点时处理JSON失败"); + } + } + + // 获取所有任务列表 + public JSONArray getTaskLists() throws NetworkFailureException { + if (!mLoggedin) { + Log.e(TAG, "请先登录"); + throw new ActionFailureException("未登录"); + } + + try { + HttpGet httpGet = new HttpGet(mGetUrl); // 创建GET请求 + HttpResponse response = mHttpClient.execute(httpGet); + + // 从响应中提取任务列表数据 + String resString = getResponseContent(response.getEntity()); + String jsBegin = "_setup("; + String jsEnd = ")}"; + int begin = resString.indexOf(jsBegin); + int end = resString.lastIndexOf(jsEnd); + String jsString = null; + if (begin != -1 && end != -1 && begin < end) { + jsString = resString.substring(begin + jsBegin.length(), end); + } + JSONObject js = new JSONObject(jsString); + return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); + } catch (IOException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("获取任务列表时HTTP请求失败"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("获取任务列表时处理JSON失败"); + } + } + // 获取特定任务列表详情 + public JSONArray getTaskList(String listGid) throws NetworkFailureException { + commitUpdate(); // 先提交待更新操作 + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = getJsonArray(listGid); // 获取动作数组 + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // 添加客户端版本 + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + JSONObject jsResponse = postRequest(jsPost); + return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("获取任务列表详情时处理JSON失败"); + } + } + + // 构造获取任务列表的动作数组 + @NonNull + private JSONArray getJsonArray(String listGid) throws JSONException { + JSONArray actionList = new JSONArray(); + JSONObject action = new JSONObject(); + // 设置获取全部动作参数 + action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); + action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); + action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); + action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); // 不获取已删除项 + actionList.put(action); + return actionList; + } + // 获取当前同步账号 + public Account getSyncAccount() { + return mAccount; + } + // 重置待更新操作数组 + public void resetUpdateArray() { + mUpdateArray = null; + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java new file mode 100644 index 0000000..c64b259 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java @@ -0,0 +1,843 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义Google任务远程操作包路径 +package net.micode.notes.gtask.remote; +// 导入Android基础组件 +import android.app.Activity; // 活动组件,提供用户交互界面 +import android.content.ContentResolver; // 内容解析器,用于访问ContentProvider +import android.content.ContentUris; // 用于操作ContentProvider的URI工具 +import android.content.ContentValues; // 键值对集合,用于数据库操作 +import android.content.Context; // 应用上下文,提供全局资源访问 +import android.database.Cursor; // 数据库查询结果集 +import android.os.Build; // 系统版本信息 +import android.util.Log; // 日志工具类 +// 导入AndroidX兼容库注解 +import androidx.annotation.RequiresApi; // API版本要求注解 +// 导入应用资源 +import net.micode.notes.R; // 资源ID引用 +// 导入笔记数据相关类 +import net.micode.notes.data.Notes; // 笔记数据库常量 +import net.micode.notes.data.Notes.DataColumns; // 数据表列名常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记表列名常量 +// 导入Google任务数据模型 +import net.micode.notes.gtask.data.MetaData; // 元数据封装类 +import net.micode.notes.gtask.data.Node; // 节点基类 +import net.micode.notes.gtask.data.SqlNote; // SQLite笔记实体 +import net.micode.notes.gtask.data.Task; // 任务项实现 +import net.micode.notes.gtask.data.TaskList; // 任务列表实现 +// 导入自定义异常 +import net.micode.notes.gtask.exception.ActionFailureException; // 操作失败异常 +import net.micode.notes.gtask.exception.NetworkFailureException; // 网络异常 +// 导入工具类 +import net.micode.notes.tool.DataUtils; // 数据转换工具 +import net.micode.notes.tool.GTaskStringUtils; // Google任务字符串工具 +// 导入JSON处理库 +import org.json.JSONArray; // JSON数组处理 +import org.json.JSONException; // JSON解析异常 +import org.json.JSONObject; // JSON对象处理 +// 导入Java集合框架 +import java.util.HashMap; // 哈希映射表 +import java.util.HashSet; // 哈希集合 +// 集合迭代器 +import java.util.Map; // 映射接口 +import java.util.Objects; // 对象工具类 +// Google任务同步管理器(实现本地笔记与Google Tasks双向同步) +public class GTaskManager { + // 日志标签,使用类名作为TAG + private static final String TAG = GTaskManager.class.getSimpleName(); + + // 同步状态常量定义 + public static final int STATE_SUCCESS = 0; // 同步成功状态码 + public static final int STATE_NETWORK_ERROR = 1; // 网络错误状态码 + public static final int STATE_INTERNAL_ERROR = 2; // 内部错误状态码 + public static final int STATE_SYNC_IN_PROGRESS = 3; // 同步进行中状态码 + public static final int STATE_SYNC_CANCELLED = 4; // 同步已取消状态码 + + // 单例实例变量 + private static GTaskManager mInstance = null; + + // 上下文相关变量 + private Activity mActivity; // 用于获取认证令牌的Activity + private Context mContext; // 应用上下文 + private ContentResolver mContentResolver; // 内容解析器 + + // 同步状态标志 + private boolean mSyncing; // 是否正在同步的标志 + private boolean mCancelled; // 是否已取消同步的标志 + + // 数据存储结构 + private final HashMap mGTaskListHashMap; // Google任务列表缓存(key: 任务列表ID) + private final HashMap mGTaskHashMap; // 所有任务节点缓存(key: 任务ID) + private final HashMap mMetaHashMap; // 元数据缓存(key: 关联任务ID) + private TaskList mMetaList; // 元数据专用任务列表 + + // ID映射关系 + private final HashSet mLocalDeleteIdMap; // 待删除的本地笔记ID集合 + private final HashMap mGidToNid; // Google任务ID到本地笔记ID的映射 + private final HashMap mNidToGid; // 本地笔记ID到Google任务ID的映射 + + // 私有构造函数(单例模式) + private GTaskManager() { + mSyncing = false; // 初始化时未在同步状态 + mCancelled = false; // 初始化时未取消 + mGTaskListHashMap = new HashMap<>(); // 初始化任务列表缓存 + mGTaskHashMap = new HashMap<>(); // 初始化任务节点缓存 + mMetaHashMap = new HashMap<>(); // 初始化元数据缓存 + mMetaList = null; // 初始化元数据列表为空 + mLocalDeleteIdMap = new HashSet<>(); // 初始化待删除ID集合 + mGidToNid = new HashMap<>(); // 初始化GoogleID到本地ID映射 + mNidToGid = new HashMap<>(); // 初始化本地ID到GoogleID映射 + } + + // 获取单例实例(线程安全) + public static synchronized GTaskManager getInstance() { + if (mInstance == null) { // 如果实例不存在 + mInstance = new GTaskManager(); // 创建新实例 + } + return mInstance; // 返回单例实例 + } + + // 设置Activity上下文(用于认证) + public synchronized void setActivityContext(Activity activity) { + mActivity = activity; // 保存Activity引用 + } + + // 执行同步操作(主入口方法) + @RequiresApi(api = Build.VERSION_CODES.R) + public int sync(Context context, GTaskASyncTask asyncTask) { + // 检查是否已在同步中 + if (mSyncing) { + Log.d(TAG, "Sync is in progress"); // 记录日志 + return STATE_SYNC_IN_PROGRESS; // 返回同步中状态 + } + + // 初始化同步环境 + mContext = context; // 保存上下文 + mContentResolver = mContext.getContentResolver(); // 获取内容解析器 + mSyncing = true; // 设置同步标志 + mCancelled = false; // 重置取消标志 + + // 清空所有缓存数据 + mGTaskListHashMap.clear(); + mGTaskHashMap.clear(); + mMetaHashMap.clear(); + mLocalDeleteIdMap.clear(); + mGidToNid.clear(); + mNidToGid.clear(); + + try { + // 获取Google任务客户端实例 + GTaskClient client = GTaskClient.getInstance(); + client.resetUpdateArray(); // 重置更新队列 + + // 步骤1:登录Google Tasks(如果未取消) + if (!mCancelled) { + if (!client.login(mActivity)) { // 尝试登录 + throw new NetworkFailureException("login google task failed"); // 登录失败抛出异常 + } + } + + // 步骤2:初始化远程任务列表 + asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list)); // 更新进度 + initGTaskList(); // 初始化任务列表 + + // 步骤3:执行内容同步 + asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing)); // 更新进度 + syncContent(); // 执行内容同步 + + } catch (NetworkFailureException e) { + Log.e(TAG, e.toString()); // 记录网络错误日志 + return STATE_NETWORK_ERROR; // 返回网络错误状态 + } catch (ActionFailureException e) { + Log.e(TAG, e.toString()); // 记录操作失败日志 + return STATE_INTERNAL_ERROR; // 返回内部错误状态 + } catch (Exception e) { + Log.e(TAG, e.toString()); // 记录未知异常日志 + e.printStackTrace(); // 打印堆栈轨迹 + return STATE_INTERNAL_ERROR; // 返回内部错误状态 + } finally { + // 最终清理工作 + mGTaskListHashMap.clear(); // 清空任务列表缓存 + mGTaskHashMap.clear(); // 清空任务节点缓存 + mMetaHashMap.clear(); // 清空元数据缓存 + mLocalDeleteIdMap.clear(); // 清空待删除ID集合 + mGidToNid.clear(); // 清空ID映射 + mNidToGid.clear(); // 清空反向ID映射 + mSyncing = false; // 重置同步标志 + } + + // 返回最终状态(根据是否取消) + return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS; + } + + // 初始化远程任务列表 + private void initGTaskList() throws NetworkFailureException { + if (mCancelled) return; // 如果已取消则直接返回 + + GTaskClient client = GTaskClient.getInstance(); // 获取客户端实例 + try { + // 获取所有任务列表 + JSONArray jsTaskLists = client.getTaskLists(); + + // 首先初始化元数据列表 + mMetaList = null; // 重置元数据列表 + for (int i = 0; i < jsTaskLists.length(); i++) { + JSONObject object = jsTaskLists.getJSONObject(i); // 获取单个列表 + String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); // 获取列表ID + String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME); // 获取列表名称 + + // 检查是否是元数据专用列表 + if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) { + mMetaList = new TaskList(); // 创建元数据列表 + mMetaList.setContentByRemoteJSON(object); // 设置内容 + + // 加载该列表下的所有元数据项 + JSONArray jsMetas = client.getTaskList(gid); + for (int j = 0; j < jsMetas.length(); j++) { + object = jsMetas.getJSONObject(j); // 获取元数据项 + MetaData metaData = new MetaData(); // 创建元数据对象 + metaData.setContentByRemoteJSON(object); // 设置内容 + if (metaData.isWorthSaving()) { // 检查是否需要保存 + mMetaList.addChildTask(metaData); // 添加到元数据列表 + if (metaData.getGid() != null) { // 如果有有效ID + mMetaHashMap.put(metaData.getRelatedGid(), metaData); // 加入缓存 + } + } + } + } + } + + // 如果不存在元数据列表则创建 + if (mMetaList == null) { + mMetaList = new TaskList(); // 创建新列表 + mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META); // 设置名称 + GTaskClient.getInstance().createTaskList(mMetaList); // 在服务器创建 + } + + // 初始化普通任务列表 + for (int i = 0; i < jsTaskLists.length(); i++) { + JSONObject object = jsTaskLists.getJSONObject(i); // 获取单个列表 + String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); // 获取列表ID + String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME); // 获取列表名称 + + // 检查是否是MIUI前缀的普通列表(排除元数据列表) + if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX) + && !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) { + TaskList tasklist = new TaskList(); // 创建任务列表 + tasklist.setContentByRemoteJSON(object); // 设置内容 + mGTaskListHashMap.put(gid, tasklist); // 加入列表缓存 + mGTaskHashMap.put(gid, tasklist); // 加入节点缓存 + + // 加载该列表下的所有任务 + JSONArray jsTasks = client.getTaskList(gid); + for (int j = 0; j < jsTasks.length(); j++) { + object = jsTasks.getJSONObject(j); // 获取单个任务 + gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); // 获取任务ID + Task task = new Task(); // 创建任务对象 + task.setContentByRemoteJSON(object); // 设置内容 + if (task.isWorthSaving()) { // 检查是否需要保存 + task.setMetaInfo(mMetaHashMap.get(gid)); // 关联元数据 + tasklist.addChildTask(task); // 添加到列表 + mGTaskHashMap.put(gid, task); // 加入节点缓存 + } + } + } + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); // 记录JSON异常 + e.printStackTrace(); // 打印堆栈轨迹 + throw new ActionFailureException("initGTaskList: handing JSONObject failed"); // 抛出操作异常 + } + } + + // 执行内容同步(核心方法) + @RequiresApi(api = Build.VERSION_CODES.R) + private void syncContent() throws NetworkFailureException { + int syncType; // 同步类型标识 + Cursor c = null; // 数据库游标 + String gid; // Google任务ID + Node node; // 任务节点 + + mLocalDeleteIdMap.clear(); // 清空待删除集合 + + if (mCancelled) return; // 检查是否已取消 + + /* ===== 阶段1:处理本地已删除的笔记 ===== */ + try { + // 查询回收站中的笔记(非系统类型且父目录为回收站) + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type<>? AND parent_id=?)", new String[] { + String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER) + }, null); + if (c != null) { + while (c.moveToNext()) { // 遍历结果集 + gid = c.getString(SqlNote.GTASK_ID_COLUMN); // 获取Google ID + node = mGTaskHashMap.get(gid); // 查找对应节点 + if (node != null) { // 如果存在对应节点 + mGTaskHashMap.remove(gid); // 从缓存移除 + doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c); // 执行远程删除 + } + mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN)); // 记录待删除ID + } + } else { + Log.w(TAG, "failed to query trash folder"); // 记录查询失败 + } + } finally { + if (c != null) { + c.close(); // 关闭游标 + c = null; + } + } + + /* ===== 阶段2:同步文件夹 ===== */ + syncFolder(); // 同步文件夹结构 + + /* ===== 阶段3:处理数据库中的现有笔记 ===== */ + try { + // 查询所有普通笔记(非回收站) + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type=? AND parent_id<>?)", new String[] { + String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER) + }, NoteColumns.TYPE + " DESC"); + if (c != null) { + while (c.moveToNext()) { // 遍历结果集 + gid = c.getString(SqlNote.GTASK_ID_COLUMN); // 获取Google ID + node = mGTaskHashMap.get(gid); // 查找对应节点 + + // 确定同步类型 + if (node != null) { // 如果存在对应节点 + mGTaskHashMap.remove(gid); // 从缓存移除 + mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN)); // 建立ID映射 + mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid); // 建立反向映射 + syncType = node.getSyncAction(c); // 获取同步类型 + } else { + // 根据是否存在Google ID判断是本地新增还是远程删除 + syncType = c.getString(SqlNote.GTASK_ID_COLUMN).trim().isEmpty() ? + Node.SYNC_ACTION_ADD_REMOTE : Node.SYNC_ACTION_DEL_LOCAL; + } + doContentSync(syncType, node, c); // 执行同步操作 + } + } else { + Log.w(TAG, "failed to query existing note in database"); // 记录查询失败 + } + } finally { + if (c != null) { + c.close(); // 关闭游标 + } + } + + /* ===== 阶段4:处理剩余的远程节点(本地不存在) ===== */ + for (Map.Entry entry : mGTaskHashMap.entrySet()) { + node = entry.getValue(); // 获取节点 + doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); // 执行本地添加 + } + + /* ===== 阶段5:批量删除本地标记项 ===== */ + if (!mCancelled) { + if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) { + throw new ActionFailureException("failed to batch-delete local deleted notes"); // 删除失败抛出异常 + } + } + + /* ===== 阶段6:刷新本地同步ID ===== */ + if (!mCancelled) { + GTaskClient.getInstance().commitUpdate(); // 提交未完成的更新 + refreshLocalSyncId(); // 刷新本地同步ID + } + } + // 同步文件夹方法(需要Android R及以上版本) + @RequiresApi(api = Build.VERSION_CODES.R) + private void syncFolder() throws NetworkFailureException { + Cursor c = null; // 数据库查询游标 + String gid; // Google任务ID + Node node; // 任务节点对象 + int syncType; // 同步类型标识 + + if (mCancelled) return; // 检查同步是否被取消 + + /* ===== 1. 处理根文件夹 ===== */ + try { + // 查询根文件夹(ID_ROOT_FOLDER) + c = mContentResolver.query( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, Notes.ID_ROOT_FOLDER), + SqlNote.PROJECTION_NOTE, null, null, null); + + if (c != null && c.moveToNext()) { + gid = c.getString(SqlNote.GTASK_ID_COLUMN); // 获取Google ID + node = mGTaskHashMap.get(gid); // 查找对应节点 + + if (node != null) { // 如果远程存在对应节点 + mGTaskHashMap.remove(gid); // 从缓存移除 + // 建立ID映射关系 + mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER); + mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid); + + // 系统文件夹:仅在名称不匹配时更新远程 + if (!node.getName().equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_DEFAULT)) { + doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c); + } + } else { // 如果远程不存在 + doContentSync(Node.SYNC_ACTION_ADD_REMOTE, null, c); // 在远程创建 + } + } else { + Log.w(TAG, "failed to query root folder"); // 记录查询失败 + } + } finally { + if (c != null) c.close(); // 确保游标关闭 + } + + /* ===== 2. 处理通话记录文件夹 ===== */ + try { + // 查询通话记录文件夹(ID_CALL_RECORD_FOLDER) + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(_id=?)", new String[]{String.valueOf(Notes.ID_CALL_RECORD_FOLDER)}, null); + + if (c != null && c.moveToNext()) { + gid = c.getString(SqlNote.GTASK_ID_COLUMN); // 获取Google ID + node = mGTaskHashMap.get(gid); // 查找对应节点 + + if (node != null) { // 如果远程存在对应节点 + mGTaskHashMap.remove(gid); // 从缓存移除 + // 建立ID映射关系 + mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER); + mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid); + + // 系统文件夹:仅在名称不匹配时更新远程 + if (!node.getName().equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_CALL_NOTE)) { + doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c); + } + } else { // 如果远程不存在 + doContentSync(Node.SYNC_ACTION_ADD_REMOTE, null, c); // 在远程创建 + } + } else { + Log.w(TAG, "failed to query call note folder"); // 记录查询失败 + } + } finally { + if (c != null) c.close(); // 确保游标关闭 + } + + /* ===== 3. 处理普通本地文件夹 ===== */ + try { + // 查询所有普通文件夹(非回收站) + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type=? AND parent_id<>?)", + new String[]{ + String.valueOf(Notes.TYPE_FOLDER), + String.valueOf(Notes.ID_TRASH_FOLER) + }, + NoteColumns.TYPE + " DESC"); + + if (c != null) { + while (c.moveToNext()) { // 遍历查询结果 + gid = c.getString(SqlNote.GTASK_ID_COLUMN); // 获取Google ID + node = mGTaskHashMap.get(gid); // 查找对应节点 + + // 确定同步类型 + if (node != null) { // 存在对应节点 + mGTaskHashMap.remove(gid); // 从缓存移除 + // 建立ID映射关系 + mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN)); + mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid); + syncType = node.getSyncAction(c); // 获取同步类型 + } else { + // 根据Google ID是否存在判断是本地新增还是远程删除 + syncType = c.getString(SqlNote.GTASK_ID_COLUMN).trim().isEmpty() ? + Node.SYNC_ACTION_ADD_REMOTE : Node.SYNC_ACTION_DEL_LOCAL; + } + doContentSync(syncType, node, c); // 执行同步操作 + } + } else { + Log.w(TAG, "failed to query existing folder"); // 记录查询失败 + } + } finally { + if (c != null) c.close(); // 确保游标关闭 + } + + /* ===== 4. 处理远程新增的文件夹 ===== */ + for (Map.Entry entry : mGTaskListHashMap.entrySet()) { + gid = entry.getKey(); // 获取Google ID + node = entry.getValue(); // 获取节点对象 + + // 如果缓存中仍存在该节点(说明本地不存在) + if (mGTaskHashMap.containsKey(gid)) { + mGTaskHashMap.remove(gid); // 从缓存移除 + doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); // 在本地创建 + } + } + + // 如果未被取消,提交所有未完成的更新 + if (!mCancelled) GTaskClient.getInstance().commitUpdate(); + } + + // 执行具体同步操作(根据同步类型分发) + @RequiresApi(api = Build.VERSION_CODES.R) + private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException { + if (mCancelled) return; // 检查是否已取消 + + MetaData meta; // 元数据对象 + switch (syncType) { + case Node.SYNC_ACTION_ADD_LOCAL: // 本地添加操作 + addLocalNode(node); // 在本地添加节点 + break; + case Node.SYNC_ACTION_ADD_REMOTE: // 远程添加操作 + addRemoteNode(node, c); // 在远程添加节点 + break; + case Node.SYNC_ACTION_DEL_LOCAL: // 本地删除操作 + // 查找关联的元数据 + meta = mMetaHashMap.get(c.getString(SqlNote.GTASK_ID_COLUMN)); + if (meta != null) { + GTaskClient.getInstance().deleteNode(meta); // 删除元数据 + } + mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN)); // 记录待删除ID + break; + case Node.SYNC_ACTION_DEL_REMOTE: // 远程删除操作 + // 查找关联的元数据 + meta = mMetaHashMap.get(node.getGid()); + if (meta != null) { + GTaskClient.getInstance().deleteNode(meta); // 删除元数据 + } + GTaskClient.getInstance().deleteNode(node); // 删除节点 + break; + case Node.SYNC_ACTION_UPDATE_LOCAL: // 本地更新操作 + updateLocalNode(node, c); // 更新本地节点 + break; + case Node.SYNC_ACTION_UPDATE_REMOTE: // 远程更新操作 + updateRemoteNode(node, c); // 更新远程节点 + break; + case Node.SYNC_ACTION_UPDATE_CONFLICT: // 冲突处理(目前简单采用远程更新) + updateRemoteNode(node, c); + break; + case Node.SYNC_ACTION_NONE: // 无操作 + break; + case Node.SYNC_ACTION_ERROR: // 错误状态 + default: + throw new ActionFailureException("unkown sync action type"); // 抛出异常 + } + } + + // 在本地添加节点 + @RequiresApi(api = Build.VERSION_CODES.R) + private void addLocalNode(Node node) throws NetworkFailureException { + if (mCancelled) return; // 检查是否已取消 + + SqlNote sqlNote; // 本地数据库笔记对象 + if (node instanceof TaskList) { // 如果是任务列表(文件夹) + // 处理系统预置文件夹 + if (node.getName().equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_DEFAULT)) { + sqlNote = new SqlNote(mContext, Notes.ID_ROOT_FOLDER); // 根文件夹 + } + else if (node.getName().equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_CALL_NOTE)) { + sqlNote = new SqlNote(mContext, Notes.ID_CALL_RECORD_FOLDER); // 通话记录文件夹 + } + else { // 普通文件夹 + sqlNote = new SqlNote(mContext); + sqlNote.setContent(node.getLocalJSONFromContent()); // 设置内容 + sqlNote.setParentId(Notes.ID_ROOT_FOLDER); // 设置父ID为根文件夹 + } + } + else { // 普通任务节点 + sqlNote = new SqlNote(mContext); + JSONObject js = node.getLocalJSONFromContent(); // 获取节点内容 + + try { + // 处理笔记ID冲突 + if (js.has(GTaskStringUtils.META_HEAD_NOTE)) { + JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + if (note.has(NoteColumns.ID)) { + long id = note.getLong(NoteColumns.ID); + if (DataUtils.existInNoteDatabase(mContentResolver, id)) { + note.remove(NoteColumns.ID); // ID已存在则移除 + } + } + } + + // 处理数据项ID冲突 + if (js.has(GTaskStringUtils.META_HEAD_DATA)) { + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + if (data.has(DataColumns.ID)) { + long dataId = data.getLong(DataColumns.ID); + if (DataUtils.existInDataDatabase(mContentResolver, dataId)) { + data.remove(DataColumns.ID); // ID已存在则移除 + } + } + } + } + } catch (JSONException e) { + Log.w(TAG, e.toString()); // 记录JSON异常 + e.printStackTrace(); + } + + sqlNote.setContent(js); // 设置笔记内容 + + // 设置父文件夹ID + Long parentId = mGidToNid.get(((Task) node).getParent().getGid()); + if (parentId == null) { + Log.e(TAG, "cannot find task's parent id locally"); + throw new ActionFailureException("cannot add local node"); + } + sqlNote.setParentId(parentId); // 设置父节点ID + } + + // 创建本地节点 + sqlNote.setGtaskId(node.getGid()); // 设置Google任务ID + sqlNote.commit(false); // 提交到数据库 + + // 更新ID映射关系 + mGidToNid.put(node.getGid(), sqlNote.getId()); + mNidToGid.put(sqlNote.getId(), node.getGid()); + + // 更新远程元数据 + updateRemoteMeta(node.getGid(), sqlNote); + } + // 更新本地节点(需要Android R及以上版本) + @RequiresApi(api = Build.VERSION_CODES.R) + private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException { + if (mCancelled) { + return; // 如果同步已取消则直接返回 + } + + SqlNote sqlNote; + // 步骤1:更新本地笔记 + sqlNote = new SqlNote(mContext, c); // 根据游标创建SqlNote对象 + sqlNote.setContent(node.getLocalJSONFromContent()); // 设置节点内容 + + // 步骤2:设置父节点ID(任务节点取父任务列表ID,否则使用根文件夹ID) + Long parentId = (node instanceof Task) ? + mGidToNid.get(((Task) node).getParent().getGid()) : // 获取任务的父列表本地ID + Long.valueOf(Notes.ID_ROOT_FOLDER); // 默认使用根文件夹ID + if (parentId == null) { + Log.e(TAG, "无法在本地找到任务的父ID"); + throw new ActionFailureException("无法更新本地节点"); + } + sqlNote.setParentId(parentId); // 设置父节点ID + sqlNote.commit(true); // 提交修改到数据库 + + // 步骤3:更新元数据信息 + updateRemoteMeta(node.getGid(), sqlNote); + } + + // 添加远程节点(需要Android R及以上版本) + @RequiresApi(api = Build.VERSION_CODES.R) + private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException { + if (mCancelled) { + return; // 如果同步已取消则直接返回 + } + + SqlNote sqlNote = new SqlNote(mContext, c); // 根据游标创建SqlNote对象 + Node n; // 要创建的节点 + + // 步骤1:判断节点类型并处理 + if (sqlNote.isNoteType()) { + // 情况1:普通笔记节点 + Task task = new Task(); // 创建新任务 + task.setContentByLocalJSON(sqlNote.getContent()); // 设置任务内容 + + // 获取父任务列表的Google ID + String parentGid = mNidToGid.get(sqlNote.getParentId()); + if (parentGid == null) { + Log.e(TAG, "无法找到任务的父任务列表"); + throw new ActionFailureException("无法添加远程任务"); + } + // 将任务添加到父列表 + Objects.requireNonNull(mGTaskListHashMap.get(parentGid)).addChildTask(task); + + // 在远程创建任务 + GTaskClient.getInstance().createTask(task); + n = task; // 设置返回节点 + + // 添加元数据 + updateRemoteMeta(task.getGid(), sqlNote); + } else { + // 情况2:文件夹节点 + TaskList tasklist = null; + + // 构建文件夹名称(带MIUI前缀) + String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX; + if (sqlNote.getId() == Notes.ID_ROOT_FOLDER) { + folderName += GTaskStringUtils.FOLDER_DEFAULT; // 根文件夹 + } else if (sqlNote.getId() == Notes.ID_CALL_RECORD_FOLDER) { + folderName += GTaskStringUtils.FOLDER_CALL_NOTE; // 通话记录文件夹 + } else { + folderName += sqlNote.getSnippet(); // 普通文件夹使用笔记摘要 + } + + // 检查是否已存在同名文件夹 + for (Map.Entry entry : mGTaskListHashMap.entrySet()) { + String gid = entry.getKey(); + TaskList list = entry.getValue(); + + if (list.getName().equals(folderName)) { + tasklist = list; // 找到已有文件夹 + mGTaskHashMap.remove(gid); // 从缓存移除 + break; + } + } + + // 如果不存在则创建新文件夹 + if (tasklist == null) { + tasklist = new TaskList(); // 创建新任务列表 + tasklist.setContentByLocalJSON(sqlNote.getContent()); // 设置内容 + GTaskClient.getInstance().createTaskList(tasklist); // 远程创建 + mGTaskListHashMap.put(tasklist.getGid(), tasklist); // 加入缓存 + } + n = tasklist; // 设置返回节点 + } + + // 步骤2:更新本地笔记 + sqlNote.setGtaskId(n.getGid()); // 设置Google任务ID + sqlNote.commit(false); // 提交修改到数据库 + sqlNote.resetLocalModified(); // 重置本地修改标记 + sqlNote.commit(true); // 再次提交确保状态更新 + + // 步骤3:更新ID映射关系 + mGidToNid.put(n.getGid(), sqlNote.getId()); // Google ID → 本地ID + mNidToGid.put(sqlNote.getId(), n.getGid()); // 本地ID → Google ID + } + + // 更新远程节点(需要Android R及以上版本) + @RequiresApi(api = Build.VERSION_CODES.R) + private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException { + if (mCancelled) { + return; // 如果同步已取消则直接返回 + } + + SqlNote sqlNote = new SqlNote(mContext, c); // 根据游标创建SqlNote对象 + + // 步骤1:更新远程节点内容 + node.setContentByLocalJSON(sqlNote.getContent()); // 设置节点内容 + GTaskClient.getInstance().addUpdateNode(node); // 添加更新到队列 + + // 步骤2:更新元数据 + updateRemoteMeta(node.getGid(), sqlNote); + + // 步骤3:处理任务移动(如果是笔记类型) + if (sqlNote.isNoteType()) { + Task task = (Task) node; // 转换为任务对象 + TaskList preParentList = task.getParent(); // 获取原父列表 + + // 获取当前父列表的Google ID + String curParentGid = mNidToGid.get(sqlNote.getParentId()); + if (curParentGid == null) { + Log.e(TAG, "无法找到任务的父任务列表"); + throw new ActionFailureException("无法更新远程任务"); + } + TaskList curParentList = mGTaskListHashMap.get(curParentGid); // 获取当前父列表 + + // 如果父列表发生变化 + if (preParentList != curParentList) { + preParentList.removeChildTask(task); // 从原列表移除 + if (curParentList != null) { + curParentList.addChildTask(task); // 添加到新列表 + } + // 在远程执行移动操作 + GTaskClient.getInstance().moveTask(task, preParentList, curParentList); + } + } + + // 步骤4:清除本地修改标记 + sqlNote.resetLocalModified(); // 重置修改标记 + sqlNote.commit(true); // 提交到数据库 + } + + // 更新远程元数据信息 + private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException { + // 只处理有效的笔记类型数据(非文件夹) + if (sqlNote != null && sqlNote.isNoteType()) { + // 尝试从缓存获取现有元数据 + MetaData metaData = mMetaHashMap.get(gid); + + if (metaData != null) { + // 情况1:元数据已存在 - 更新操作 + metaData.setMeta(gid, sqlNote.getContent()); // 设置新的元数据内容 + GTaskClient.getInstance().addUpdateNode(metaData); // 添加到更新队列 + } else { + // 情况2:元数据不存在 - 创建操作 + metaData = new MetaData(); // 创建新元数据对象 + metaData.setMeta(gid, sqlNote.getContent()); // 设置元数据内容 + mMetaList.addChildTask(metaData); // 添加到元数据列表 + mMetaHashMap.put(gid, metaData); // 加入缓存 + GTaskClient.getInstance().createTask(metaData); // 在远程创建 + } + } + } + + // 刷新本地同步ID(确保与远程一致) + private void refreshLocalSyncId() throws NetworkFailureException { + if (mCancelled) { + return; // 如果同步已取消则直接返回 + } + + /* 步骤1:重新加载远程任务列表 */ + mGTaskHashMap.clear(); // 清空任务缓存 + mGTaskListHashMap.clear(); // 清空任务列表缓存 + mMetaHashMap.clear(); // 清空元数据缓存 + initGTaskList(); // 重新初始化任务列表 + + /* 步骤2:更新本地同步ID */ + try (Cursor c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type<>? AND parent_id<>?)", new String[]{ + String.valueOf(Notes.TYPE_SYSTEM), + String.valueOf(Notes.ID_TRASH_FOLER) + }, + NoteColumns.TYPE + " DESC")) { + // 查询所有需要同步的笔记(非系统类型且不在回收站) + + if (c != null) { + while (c.moveToNext()) { // 遍历查询结果 + String gid = c.getString(SqlNote.GTASK_ID_COLUMN); // 获取Google ID + Node node = mGTaskHashMap.get(gid); // 查找对应远程节点 + + if (node != null) { + mGTaskHashMap.remove(gid); // 从缓存移除 + + // 准备更新数据 + ContentValues values = new ContentValues(); + // 使用远程节点的最后修改时间作为同步ID + values.put(NoteColumns.SYNC_ID, node.getLastModified()); + + // 更新数据库 + mContentResolver.update( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, + c.getLong(SqlNote.ID_COLUMN)), // 定位要更新的笔记 + values, // 更新内容 + null, // WHERE条件 + null // WHERE参数 + ); + } else { + // 错误处理:本地存在但远程不存在的笔记 + Log.e(TAG, "本地存在但远程找不到对应的任务"); + throw new ActionFailureException("同步后仍有本地条目缺少Google ID"); + } + } + } else { + Log.w(TAG, "查询本地笔记刷新同步ID失败"); + } + } + // 确保关闭游标 + } + // 获取当前同步账号名称 + public String getSyncAccount() { + // 从GTaskClient获取账号信息并返回名称 + return GTaskClient.getInstance().getSyncAccount().name; + } + // 取消同步操作 + public void cancelSync() { + mCancelled = true; // 设置取消标志 + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java new file mode 100644 index 0000000..bdb7a4b --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java @@ -0,0 +1,167 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 声明Google任务同步服务类,继承自Service +package net.micode.notes.gtask.remote; +// 导入安卓活动(Activity)相关类 +// 提供用户交互界面的基础组件,代表一个应用屏幕 +import android.app.Activity; +// 导入安卓服务(Service)相关类 +// 后台运行组件,无用户界面,用于执行长时间操作 +import android.app.Service; +// 导入上下文(Context)相关类 +// 提供应用环境全局信息,是访问应用资源的入口 +import android.content.Context; +// 导入意图(Intent)相关类 +// 用于组件间通信的消息对象,可包含操作指令和数据 +import android.content.Intent; +// 导入Bundle数据包相关类 +// 键值对集合,用于在不同组件间传递数据 +import android.os.Bundle; +// 导入绑定接口(IBinder)相关类 +// 跨进程通信(IPC)的核心接口,用于Service与Activity绑定 +import android.os.IBinder; + +// Google任务同步后台服务 +public class GTaskSyncService extends Service { + // 同步动作的Intent键名 + public final static String ACTION_STRING_NAME = "sync_action_type"; + + // 启动同步的动作值 + public final static int ACTION_START_SYNC = 0; + + // 取消同步的动作值 + public final static int ACTION_CANCEL_SYNC = 1; + + // 无效动作值 + public final static int ACTION_INVALID = 2; + + // 广播名称 + public final static String GTASK_SERVICE_BROADCAST_NAME = + "net.micode.notes.gtask.remote.gtask_sync_service"; + + // 广播键名:是否正在同步 + public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing"; + + // 广播键名:进度消息 + public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg"; + + // 同步任务实例 + private static GTaskASyncTask mSyncTask = null; + + // 同步进度文本 + private static String mSyncProgress = ""; + + // 开始同步方法 + private void startSync() { + if (mSyncTask == null) { // 如果没有正在进行的同步任务 + // 创建新的异步任务实例 + mSyncTask = new GTaskASyncTask(this, () -> { + // 同步完成后的回调 + mSyncTask = null; // 清空任务引用 + sendBroadcast(""); // 发送空广播 + stopSelf(); // 停止服务 + }); + sendBroadcast(""); // 发送初始广播 + mSyncTask.execute(); // 执行异步任务 + } + } + + // 取消同步方法 + private void cancelSync() { + if (mSyncTask != null) { // 如果存在同步任务 + mSyncTask.cancelSync(); // 调用取消方法 + } + } + + // 服务创建时调用 + @Override + public void onCreate() { + mSyncTask = null; // 初始化任务引用 + } + + // 服务启动命令处理 + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Bundle bundle = intent.getExtras(); // 获取附加数据 + if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) { + // 根据动作类型执行不同操作 + switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) { + case ACTION_START_SYNC: + startSync(); // 开始同步 + break; + case ACTION_CANCEL_SYNC: + cancelSync(); // 取消同步 + break; + default: + break; + } + return START_STICKY; // 服务被终止后自动重启 + } + return super.onStartCommand(intent, flags, startId); + } + + // 内存不足时调用 + @Override + public void onLowMemory() { + if (mSyncTask != null) { // 如果正在同步 + mSyncTask.cancelSync(); // 取消当前同步 + } + } + + // 绑定服务时调用(本服务不需要绑定) + @Override + public IBinder onBind(Intent intent) { + return null; // 返回null表示不支持绑定 + } + + // 发送广播方法 + public void sendBroadcast(String msg) { + mSyncProgress = msg; // 更新进度文本 + Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); // 创建广播Intent + // 添加是否正在同步的状态 + intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null); + // 添加进度消息 + intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg); + sendBroadcast(intent); // 发送广播 + } + + // 静态方法:从Activity启动同步 + public static void startSync(Activity activity) { + GTaskManager.getInstance().setActivityContext(activity); // 设置活动上下文 + // 创建启动服务的Intent + Intent intent = new Intent(activity, GTaskSyncService.class); + // 设置动作为开始同步 + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, + GTaskSyncService.ACTION_START_SYNC); + activity.startService(intent); // 启动服务 + } + + // 静态方法:取消同步 + public static void cancelSync(Context context) { + // 创建取消同步的服务Intent + Intent intent = new Intent(context, GTaskSyncService.class); + // 设置动作为取消同步 + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, + GTaskSyncService.ACTION_CANCEL_SYNC); + context.startService(intent); // 启动服务 + } + + // 静态方法:检查是否正在同步 + public static boolean isSyncing() { + return mSyncTask != null; // 通过任务实例判断 + } + + // 静态方法:获取当前进度文本 + public static String getProgressString() { + return mSyncProgress; // 返回进度消息 + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/model/Note.java b/src/mi_note/app/src/main/java/net/micode/notes/model/Note.java new file mode 100644 index 0000000..525d87a --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/model/Note.java @@ -0,0 +1,342 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义笔记模型的包路径 +package net.micode.notes.model; + +// 导入所需的Android类库 +import android.content.ContentProviderOperation; // 内容提供者批量操作 +import android.content.ContentProviderResult; // 内容提供者操作结果 +import android.content.ContentUris; // URI工具类 +import android.content.ContentValues; // 键值对存储类 +import android.content.Context; // 上下文对象 +import android.content.OperationApplicationException;// 操作应用异常 +import android.net.Uri; // URI处理类 +import android.os.Build; +import android.os.RemoteException; // 远程调用异常 +import android.util.Log; // 日志工具 + +// 导入项目相关的数据类 +import androidx.annotation.RequiresApi; + +import net.micode.notes.data.Notes; // 笔记常量定义 +import net.micode.notes.data.Notes.CallNote; // 通话笔记类型 +import net.micode.notes.data.Notes.DataColumns; // 数据列定义 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列定义 +import net.micode.notes.data.Notes.TextNote; // 文本笔记类型 + +// 导入Java工具类 +import java.util.ArrayList; // 动态数组 +import java.util.Objects; + +// 笔记模型类 +public class Note { + private final ContentValues mNoteDiffValues; // 存储笔记差异值的ContentValues + private final NoteData mNoteData; // 笔记数据对象 + private static final String TAG = "Note"; // 日志标签 + + /** + * 创建新笔记ID + * @param context 上下文对象 + * @param folderId 文件夹ID + * @return 新创建的笔记ID + */ + public static synchronized long getNewNoteId(Context context, long folderId) { + // 创建ContentValues存储新笔记初始值 + ContentValues values = new ContentValues(); + long createdTime = System.currentTimeMillis(); + values.put(NoteColumns.CREATED_DATE, createdTime); // 设置创建时间 + values.put(NoteColumns.MODIFIED_DATE, createdTime); // 设置修改时间 + values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 设置笔记类型为普通笔记 + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 + values.put(NoteColumns.PARENT_ID, folderId); // 设置所属文件夹ID + + // 通过内容解析器插入新笔记 + Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); + + long noteId = 0; + try { + // 从返回的URI中解析出笔记ID + noteId = Long.parseLong(Objects.requireNonNull(uri).getPathSegments().get(1)); + } catch (NumberFormatException e) { + // 处理数字格式异常 + Log.e(TAG, "Get note id error :" + e); + } + + // 检查笔记ID是否合法 + if (noteId == -1) { + throw new IllegalStateException("Wrong note id:" + noteId); + } + return noteId; + } + + // 构造函数 + public Note() { + mNoteDiffValues = new ContentValues(); // 初始化笔记差异值存储 + mNoteData = new NoteData(); // 初始化笔记数据对象 + } + + /** + * 设置笔记值 + * @param key 列名 + * @param value 值 + */ + public void setNoteValue(String key, String value) { + mNoteDiffValues.put(key, value); // 存储差异值 + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间 + } + + /** + * 设置文本数据 + * @param key 列名 + * @param value 值 + */ + public void setTextData(String key, String value) { + mNoteData.setTextData(key, value); // 委托给NoteData对象处理 + } + + /** + * 设置文本数据ID + * @param id 文本数据ID + */ + public void setTextDataId(long id) { + mNoteData.setTextDataId(id); // 委托给NoteData对象处理 + } + + /** + * 获取文本数据ID + * @return 文本数据ID + */ + public long getTextDataId() { + return mNoteData.mTextDataId; // 从NoteData对象获取 + } + + /** + * 设置通话数据ID + * @param id 通话数据ID + */ + public void setCallDataId(long id) { + mNoteData.setCallDataId(id); // 委托给NoteData对象处理 + } + + /** + * 设置通话数据 + * @param key 列名 + * @param value 值 + */ + public void setCallData(String key, String value) { + mNoteData.setCallData(key, value); // 委托给NoteData对象处理 + } + + /** + * 检查是否有本地修改 + * @return 是否有未同步的修改 + */ + @RequiresApi(api = Build.VERSION_CODES.R) + public boolean isLocalModified() { + return !mNoteDiffValues.isEmpty() || mNoteData.isLocalModified(); + } + + /** + * 同步笔记到数据库 + * @param context 上下文对象 + * @param noteId 笔记ID + * @return 是否同步成功 + */ + @RequiresApi(api = Build.VERSION_CODES.R) + public boolean syncNote(Context context, long noteId) { + if (noteId <= 0) { + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + // 如果没有修改则直接返回成功 + if (!isLocalModified()) { + return true; + } + /* + * 理论上,数据变更后笔记的LOCAL_MODIFIED和MODIFIED_DATE字段应该更新。 + * 为了数据安全,即使笔记更新失败,我们也会继续更新笔记数据信息 + */ + if (context.getContentResolver().update( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + mNoteDiffValues, + null, + null) == 0) { + Log.e(TAG, "Update note error, should not happen"); + // 不返回,继续执行后续操作 + } + mNoteDiffValues.clear(); // 清空已应用的差异值 + + // 如果有笔记数据修改且推送失败,返回失败 + return !mNoteData.isLocalModified() + || (mNoteData.pushIntoContentResolver(context, noteId) != null); + } + + // 内部类:笔记数据 + private class NoteData { + private long mTextDataId; // 文本数据ID + private final ContentValues mTextDataValues; // 文本数据差异值 + private long mCallDataId; // 通话数据ID + private final ContentValues mCallDataValues; // 通话数据差异值 + private static final String TAG = "NoteData"; // 日志标签 + + // 构造函数 + public NoteData() { + mTextDataValues = new ContentValues(); // 初始化文本数据存储 + mCallDataValues = new ContentValues(); // 初始化通话数据存储 + mTextDataId = 0; // 初始化文本数据ID + mCallDataId = 0; // 初始化通话数据ID + } + + /** + * 检查是否有本地修改 + * @return 是否有未同步的修改 + */ + @RequiresApi(api = Build.VERSION_CODES.R) + boolean isLocalModified() { + return !mTextDataValues.isEmpty() || !mCallDataValues.isEmpty(); + } + + /** + * 设置文本数据ID + * @param id 文本数据ID + */ + void setTextDataId(long id) { + if(id <= 0) { + throw new IllegalArgumentException("Text data id should larger than 0"); + } + mTextDataId = id; + } + + /** + * 设置通话数据ID + * @param id 通话数据ID + */ + void setCallDataId(long id) { + if (id <= 0) { + throw new IllegalArgumentException("Call data id should larger than 0"); + } + mCallDataId = id; + } + + /** + * 设置通话数据 + * @param key 列名 + * @param value 值 + */ + void setCallData(String key, String value) { + mCallDataValues.put(key, value); // 存储通话数据差异值 + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间 + } + + /** + * 设置文本数据 + * @param key 列名 + * @param value 值 + */ + void setTextData(String key, String value) { + mTextDataValues.put(key, value); // 存储文本数据差异值 + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间 + } + + /** + * 将数据推送到内容提供者 + * @param context 上下文对象 + * @param noteId 笔记ID + * @return 操作结果的URI + */ + @RequiresApi(api = Build.VERSION_CODES.R) + Uri pushIntoContentResolver(Context context, long noteId) { + // 安全检查 + if (noteId <= 0) { + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + // 创建批量操作列表 + ArrayList operationList = new ArrayList<>(); + ContentProviderOperation.Builder builder; + + // 处理文本数据 + if(!mTextDataValues.isEmpty()) { + mTextDataValues.put(DataColumns.NOTE_ID, noteId); // 设置所属笔记ID + + // 如果是新数据则插入 + if (mTextDataId == 0) { + mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mTextDataValues); + try { + // 从返回的URI中获取新数据ID + if (uri != null) { + setTextDataId(Long.parseLong(uri.getPathSegments().get(1))); + } + } catch (NumberFormatException e) { + Log.e(TAG, "Insert new text data fail with noteId" + noteId); + mTextDataValues.clear(); + return null; + } + } else { + // 已有数据则更新 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mTextDataId)); + builder.withValues(mTextDataValues); + operationList.add(builder.build()); + } + mTextDataValues.clear(); // 清空已应用的差异值 + } + + // 处理通话数据 + if(!mCallDataValues.isEmpty()) { + mCallDataValues.put(DataColumns.NOTE_ID, noteId); // 设置所属笔记ID + + // 如果是新数据则插入 + if (mCallDataId == 0) { + mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mCallDataValues); + try { + // 从返回的URI中获取新数据ID + setCallDataId(Long.parseLong(Objects.requireNonNull(uri).getPathSegments().get(1))); + } catch (NumberFormatException e) { + Log.e(TAG, "Insert new call data fail with noteId" + noteId); + mCallDataValues.clear(); + return null; + } + } else { + // 已有数据则更新 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mCallDataId)); + builder.withValues(mCallDataValues); + operationList.add(builder.build()); + } + mCallDataValues.clear(); // 清空已应用的差异值 + } + + // 执行批量操作 + if (!operationList.isEmpty()) { + try { + ContentProviderResult[] results = context.getContentResolver().applyBatch( + Notes.AUTHORITY, operationList); + // 返回操作结果 + return results.length == 0 || results[0] == null ? null + : ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, String.format("%s: %s", e, e.getMessage())); + return null; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/model/WorkingNote.java b/src/mi_note/app/src/main/java/net/micode/notes/model/WorkingNote.java new file mode 100644 index 0000000..790ae4b --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/model/WorkingNote.java @@ -0,0 +1,441 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义工作笔记模型的包路径 +package net.micode.notes.model; + +// 导入所需的Android类库 +import android.appwidget.AppWidgetManager; // 应用小部件管理 +import android.content.ContentUris; // URI工具类 +import android.content.Context; // 上下文对象 +import android.database.Cursor; // 数据库查询结果 +import android.os.Build; +import android.text.TextUtils; // 文本处理工具 +import android.util.Log; // 日志工具 + +// 导入项目相关的数据类 +import androidx.annotation.RequiresApi; + +import net.micode.notes.data.Notes; // 笔记常量定义 +import net.micode.notes.data.Notes.CallNote; // 通话笔记类型 +import net.micode.notes.data.Notes.DataColumns; // 数据列定义 +import net.micode.notes.data.Notes.DataConstants; // 数据常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列定义 +import net.micode.notes.data.Notes.TextNote; // 文本笔记类型 +import net.micode.notes.tool.ResourceParser.NoteBgResources; // 笔记背景资源 + +// 工作笔记类,封装当前编辑笔记的所有操作 +public class WorkingNote { + // 成员变量定义 + private final Note mNote; // 基础笔记对象 + private long mNoteId; // 笔记ID + public String mContent; // 笔记内容 + private int mMode; /mMode/ 笔记模式(普通/清单模式) + private long mAlertDate; // 提醒时间 + private long mModifiedDate; // 最后修改时间 + private int mBgColorId; // 背景颜色ID + private int mWidgetId; // 关联小部件ID + private int mWidgetType; // 小部件类型 + private long mFolderId; // 所在文件夹ID + private final Context mContext; // 上下文对象 + private static final String TAG = "WorkingNote"; // 日志标签 + private boolean mIsDeleted; // 是否已删除标记 + private NoteSettingChangedListener mNoteSettingStatusListener; // 笔记设置变更监听器 + + // 数据表查询列定义 + public static final String[] DATA_PROJECTION = new String[] { + DataColumns.ID, // 数据ID + DataColumns.CONTENT, // 内容 + DataColumns.MIME_TYPE, // MIME类型 + DataColumns.DATA1, // 扩展数据1 + DataColumns.DATA2, // 扩展数据2 + DataColumns.DATA3, // 扩展数据3 + DataColumns.DATA4, // 扩展数据4 + }; + + // 笔记表查询列定义 + public static final String[] NOTE_PROJECTION = new String[] { + NoteColumns.PARENT_ID, // 父文件夹ID + NoteColumns.ALERTED_DATE, // 提醒日期 + NoteColumns.BG_COLOR_ID, // 背景颜色ID + NoteColumns.WIDGET_ID, // 小部件ID + NoteColumns.WIDGET_TYPE, // 小部件类型 + NoteColumns.MODIFIED_DATE // 修改日期 + }; + + // 数据表列索引常量 + private static final int DATA_ID_COLUMN = 0; // ID列索引 + private static final int DATA_CONTENT_COLUMN = 1; // 内容列索引 + private static final int DATA_MIME_TYPE_COLUMN = 2; // MIME类型列索引 + private static final int DATA_MODE_COLUMN = 3; // 模式列索引 + + // 笔记表列索引常量 + private static final int NOTE_PARENT_ID_COLUMN = 0; // 父文件夹ID列索引 + private static final int NOTE_ALERTED_DATE_COLUMN = 1; // 提醒日期列索引 + private static final int NOTE_BG_COLOR_ID_COLUMN = 2; // 背景颜色ID列索引 + private static final int NOTE_WIDGET_ID_COLUMN = 3; // 小部件ID列索引 + private static final int NOTE_WIDGET_TYPE_COLUMN = 4; // 小部件类型列索引 + private static final int NOTE_MODIFIED_DATE_COLUMN = 5; // 修改日期列索引 + + /** + * 创建新工作笔记的构造函数(私有) + * @param context 上下文对象 + * @param folderId 所属文件夹ID + */ + private WorkingNote(Context context, long folderId) { + mContext = context; + mAlertDate = 0; // 初始无提醒 + mModifiedDate = System.currentTimeMillis(); // 设置当前时间为修改时间 + mFolderId = folderId; // 设置所属文件夹 + mNote = new Note(); // 创建基础笔记对象 + mNoteId = 0; // 新笔记ID为0 + mIsDeleted = false; // 未删除状态 + mMode = 0; // 默认普通模式 + mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 无效小部件类型 + } + + /** + * 加载现有工作笔记的构造函数(私有) + * @param context 上下文对象 + * @param noteId 笔记ID + * @param folderId 所属文件夹ID + */ + private WorkingNote(Context context, long noteId, long folderId) { + mContext = context; + mNoteId = noteId; // 设置笔记ID + mFolderId = folderId; // 设置所属文件夹 + mIsDeleted = false; // 未删除状态 + mNote = new Note(); // 创建基础笔记对象 + loadNote(); // 加载笔记数据 + } + + /** + * 从数据库加载笔记基本信息 + */ + private void loadNote() { + // 查询笔记表数据 + Cursor cursor = mContext.getContentResolver().query( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), + NOTE_PROJECTION, null, null, null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + // 从游标中读取笔记属性 + mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); + mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); + mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); + mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); + mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); + mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); + } + cursor.close(); // 关闭游标 + } else { + Log.e(TAG, "No note with id:" + mNoteId); + throw new IllegalArgumentException("Unable to find note with id " + mNoteId); + } + loadNoteData(); // 加载笔记内容数据 + } + + /** + * 从数据库加载笔记内容数据 + */ + private void loadNoteData() { + // 查询数据表内容 + Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, + DataColumns.NOTE_ID + "=?", new String[] { String.valueOf(mNoteId) }, null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + String type = cursor.getString(DATA_MIME_TYPE_COLUMN); + if (DataConstants.NOTE.equals(type)) { // 文本笔记数据 + mContent = cursor.getString(DATA_CONTENT_COLUMN); + mMode = cursor.getInt(DATA_MODE_COLUMN); + mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); + } else if (DataConstants.CALL_NOTE.equals(type)) { // 通话笔记数据 + mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); + } else { + Log.d(TAG, "Wrong note type with type:" + type); + } + } while (cursor.moveToNext()); + } + cursor.close(); // 关闭游标 + } else { + Log.e(TAG, "No data with id:" + mNoteId); + throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); + } + } + + /** + * 创建新的空白笔记 + * @param context 上下文对象 + * @param folderId 所属文件夹ID + * @param widgetId 小部件ID + * @param widgetType 小部件类型 + * @param defaultBgColorId 默认背景颜色ID + * @return 新创建的工作笔记对象 + */ + public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, + int widgetType, int defaultBgColorId) { + WorkingNote note = new WorkingNote(context, folderId); + note.setBgColorId(defaultBgColorId); // 设置默认背景 + note.setWidgetId(widgetId); // 设置小部件ID + note.setWidgetType(widgetType); // 设置小部件类型 + return note; + } + + /** + * 加载指定ID的笔记 + * @param context 上下文对象 + * @param id 笔记ID + * @return 加载的工作笔记对象 + */ + public static WorkingNote load(Context context, long id) { + return new WorkingNote(context, id, 0); + } + + /** + * 保存笔记到数据库 + * @return 是否保存成功 + */ + @RequiresApi(api = Build.VERSION_CODES.R) + public synchronized boolean saveNote() { + if (isWorthSaving()) { // 检查是否需要保存 + if (!existInDatabase()) { // 新笔记需要先创建 + if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { + Log.e(TAG, "Create new note fail with id:" + mNoteId); + return false; + } + } + + // 同步笔记数据到数据库 + mNote.syncNote(mContext, mNoteId); + + // 如果有关联小部件,通知更新 + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE + && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); + } + return true; + } else { + return false; + } + } + + /** + * 检查笔记是否已存在于数据库中 + * @return 是否存在 + */ + public boolean existInDatabase() { + return mNoteId > 0; + } + + /** + * 检查笔记是否值得保存 + * @return 是否需要保存 + */ + @RequiresApi(api = Build.VERSION_CODES.R) + private boolean isWorthSaving() { + // 已有笔记但未修改 + return !mIsDeleted && // 已删除 + (existInDatabase() || !TextUtils.isEmpty(mContent)) && // 新笔记且内容为空 + (!existInDatabase() || mNote.isLocalModified()); + } + + /** + * 设置笔记设置变更监听器 + * @param l 监听器对象 + */ + public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { + mNoteSettingStatusListener = l; + } + + /** + * 设置提醒时间 + * @param date 提醒时间戳 + * @param set 是否设置提醒 + */ + public void setAlertDate(long date, boolean set) { + if (date != mAlertDate) { + mAlertDate = date; + mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); + } + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onClockAlertChanged(date, set); + } + } + + /** + * 标记笔记删除状态 + * @param mark 是否删除 + */ + public void markDeleted(boolean mark) { + mIsDeleted = mark; + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); + } + } + + /** + * 设置背景颜色ID + * @param id 颜色ID + */ + public void setBgColorId(int id) { + if (id != mBgColorId) { + mBgColorId = id; + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onBackgroundColorChanged(); + } + mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); + } + } + + /** + * 设置清单模式 + * @param mode 模式值 + */ + public void setCheckListMode(int mode) { + if (mMode != mode) { + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); + } + mMode = mode; + mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); + } + } + + /** + * 设置小部件类型 + * @param type 类型值 + */ + public void setWidgetType(int type) { + if (type != mWidgetType) { + mWidgetType = type; + mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); + } + } + + /** + * 设置小部件ID + * @param id 小部件ID + */ + public void setWidgetId(int id) { + if (id != mWidgetId) { + mWidgetId = id; + mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); + } + } + + /** + * 设置工作文本内容 + * @param text 文本内容 + */ + public void setWorkingText(String text) { + if (!TextUtils.equals(mContent, text)) { + mContent = text; + mNote.setTextData(DataColumns.CONTENT, mContent); + } + } + + /** + * 将笔记转换为通话笔记 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + */ + public void convertToCallNote(String phoneNumber, long callDate) { + // 设置通话数据 + mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); + mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); + // 设置父文件夹为通话记录文件夹 + mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); + } + + // 以下是一系列获取笔记属性的方法 + + public boolean hasClockAlert() { + return (mAlertDate > 0); + } + + public String getContent() { + return mContent; + } + + public long getAlertDate() { + return mAlertDate; + } + + public long getModifiedDate() { + return mModifiedDate; + } + + public int getBgColorResId() { + return NoteBgResources.getNoteBgResource(mBgColorId); + } + + public int getBgColorId() { + return mBgColorId; + } + + public int getTitleBgResId() { + return NoteBgResources.getNoteTitleBgResource(mBgColorId); + } + + public int getCheckListMode() { + return mMode; + } + + public long getNoteId() { + return mNoteId; + } + + public long getFolderId() { + return mFolderId; + } + + public int getWidgetId() { + return mWidgetId; + } + + public int getWidgetType() { + return mWidgetType; + } + + /** + * 笔记设置变更监听器接口 + */ + public interface NoteSettingChangedListener { + /** + * 当笔记背景颜色变更时调用 + */ + void onBackgroundColorChanged(); + + /** + * 当用户设置提醒时调用 + * @param date 提醒时间 + * @param set 是否设置 + */ + void onClockAlertChanged(long date, boolean set); + + /** + * 当用户从小部件创建笔记时调用 + */ + void onWidgetChanged(); + + /** + * 当在清单模式和普通模式间切换时调用 + * @param oldMode 原模式 + * @param newMode 新模式 + */ + void onCheckListModeChanged(int oldMode, int newMode); + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/tool/BackupUtils.java b/src/mi_note/app/src/main/java/net/micode/notes/tool/BackupUtils.java new file mode 100644 index 0000000..dc0f1ea --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/tool/BackupUtils.java @@ -0,0 +1,398 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义备份工具类的包路径 +package net.micode.notes.tool; + +// 导入所需的Android类库 +import android.content.Context; // 上下文对象 +import android.database.Cursor; // 数据库查询结果 +import android.os.Environment; // 环境信息类 +import android.text.TextUtils; // 文本处理工具 +import android.text.format.DateFormat; // 日期格式化工具 +import android.util.Log; // 日志工具 + +// 导入项目资源 +import net.micode.notes.R; // 项目资源 +// 导入项目数据类 +import net.micode.notes.data.Notes; // 笔记常量定义 +import net.micode.notes.data.Notes.DataColumns; // 数据列定义 +import net.micode.notes.data.Notes.DataConstants; // 数据常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列定义 + +// 导入Java IO类 +import java.io.File; // 文件操作类 +import java.io.FileNotFoundException; // 文件未找到异常 +import java.io.FileOutputStream; // 文件输出流 +import java.io.IOException; // IO异常 +import java.io.PrintStream; // 打印流 + +/** + * 备份工具类,提供笔记导出为文本文件的功能 + */ +public class BackupUtils { + public static final int STATE_SD_CARD_UNMOUNTED =1; + private static final String TAG = "BackupUtils"; // 日志标签 + + // 单例实现部分 + private static BackupUtils sInstance; // 单例实例 + + /** + * 获取备份工具单例 + * @param context 上下文对象 + * @return 备份工具实例 + */ + public static synchronized BackupUtils getInstance(Context context) { + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + + /** + * 备份/恢复操作状态常量 + */ + // SD卡未挂载状态 + public static final int STATE_SD_CARD_UNMOUONTED = 0; + // 备份文件不存在状态 + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; + // 数据格式损坏状态 + public static final int STATE_DATA_DESTROIED = 2; + // 系统错误状态 + public static final int STATE_SYSTEM_ERROR = 3; + // 操作成功状态 + public static final int STATE_SUCCESS = 4; + + private final TextExport mTextExport; // 文本导出器 + + /** + * 私有构造函数 + * @param context 上下文对象 + */ + private BackupUtils(Context context) { + mTextExport = new TextExport(context); // 初始化文本导出器 + } + + /** + * 检查外部存储是否可用 + * @return 是否可用 + */ + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + /** + * 导出笔记为文本文件 + * @return 操作状态码 + */ + public int exportToText() { + return mTextExport.exportToText(); + } + + /** + * 获取导出的文本文件名 + * @return 文件名 + */ + public String getExportedTextFileName() { + return mTextExport.mFileName; + } + + /** + * 获取导出的文本文件目录 + * @return 目录路径 + */ + public String getExportedTextFileDir() { + return mTextExport.mFileDirectory; + } + + /** + * 内部类:文本导出器 + */ + private static class TextExport { + // 笔记表查询列定义 + private static final String[] NOTE_PROJECTION = { + NoteColumns.ID, // 笔记ID + NoteColumns.MODIFIED_DATE, // 修改日期 + NoteColumns.SNIPPET, // 笔记摘要 + NoteColumns.TYPE // 笔记类型 + }; + + // 笔记表列索引常量 + private static final int NOTE_COLUMN_ID = 0; // ID列索引 + private static final int NOTE_COLUMN_MODIFIED_DATE = 1; // 修改日期列索引 + private static final int NOTE_COLUMN_SNIPPET = 2; // 摘要列索引 + + // 数据表查询列定义 + private static final String[] DATA_PROJECTION = { + DataColumns.CONTENT, // 内容 + DataColumns.MIME_TYPE, // MIME类型 + DataColumns.DATA1, // 扩展数据1 + DataColumns.DATA2, // 扩展数据2 + DataColumns.DATA3, // 扩展数据3 + DataColumns.DATA4, // 扩展数据4 + }; + + // 数据表列索引常量 + private static final int DATA_COLUMN_CONTENT = 0; // 内容列索引 + private static final int DATA_COLUMN_MIME_TYPE = 1; // MIME类型列索引 + 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 Context mContext; // 上下文对象 + private String mFileName; // 导出文件名 + private String mFileDirectory; // 导出文件目录 + + /** + * 构造函数 + * @param context 上下文对象 + */ + public TextExport(Context context) { + // 从资源文件中获取文本格式 + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + mContext = context; + mFileName = ""; // 初始化文件名 + mFileDirectory = ""; // 初始化文件目录 + } + + /** + * 获取指定格式的文本 + * @param id 格式ID + * @return 格式字符串 + */ + private String getFormat(int id) { + return TEXT_FORMAT[id]; + } + + /** + * 导出文件夹内容到文本 + * @param folderId 文件夹ID + * @param ps 打印流 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于该文件夹的所有笔记 + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { folderId }, null); + + if (notesCursor != null) { + if (notesCursor.moveToFirst()) { + do { + // 打印笔记的修改日期 + ps.printf((getFormat(FORMAT_NOTE_DATE)) + "%n", DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))); + // 导出该笔记的内容 + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + notesCursor.close(); // 关闭游标 + } + } + + /** + * 导出笔记内容到文本 + * @param noteId 笔记ID + * @param ps 打印流 + */ + private void exportNoteToText(String noteId, PrintStream ps) { + // 查询该笔记的所有数据 + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { noteId }, null); + + if (dataCursor != null) { + if (dataCursor.moveToFirst()) { + do { + String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + if (DataConstants.CALL_NOTE.equals(mimeType)) { // 通话笔记 + // 打印电话号码 + String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); + long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); + String location = dataCursor.getString(DATA_COLUMN_CONTENT); + + if (!TextUtils.isEmpty(phoneNumber)) { + ps.printf((getFormat(FORMAT_NOTE_CONTENT)) + "%n", + phoneNumber); + } + // 打印通话日期 + ps.printf((getFormat(FORMAT_NOTE_CONTENT)) + "%n", DateFormat + .format(mContext.getString(R.string.format_datetime_mdhm), + callDate)); + // 打印通话位置 + if (!TextUtils.isEmpty(location)) { + ps.printf((getFormat(FORMAT_NOTE_CONTENT)) + "%n", + location); + } + } else if (DataConstants.NOTE.equals(mimeType)) { // 普通笔记 + String content = dataCursor.getString(DATA_COLUMN_CONTENT); + if (!TextUtils.isEmpty(content)) { + ps.printf((getFormat(FORMAT_NOTE_CONTENT)) + "%n", + content); + } + } + } while (dataCursor.moveToNext()); + } + dataCursor.close(); // 关闭游标 + } + // 在笔记之间打印分隔线 + try { + ps.write(new byte[] { + Character.LINE_SEPARATOR, Character.LETTER_NUMBER + }); + } catch (IOException e) { + Log.e(TAG, e.toString()); + } + } + + /** + * 将笔记导出为文本文件 + * @return 操作状态码 + */ + public int exportToText() { + // 检查SD卡是否挂载 + if (!externalStorageAvailable()) { + Log.d(TAG, "Media was not mounted"); + return STATE_SD_CARD_UNMOUONTED; + } + + // 获取打印流 + PrintStream ps = getExportToTextPrintStream(); + if (ps == null) { + Log.e(TAG, "get print stream error"); + return STATE_SYSTEM_ERROR; + } + + // 首先导出文件夹及其笔记 + Cursor folderCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + + if (folderCursor != null) { + if (folderCursor.moveToFirst()) { + do { + // 打印文件夹名称 + String folderName; + if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { + folderName = mContext.getString(R.string.call_record_folder_name); + } else { + folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); + } + if (!TextUtils.isEmpty(folderName)) { + ps.printf((getFormat(FORMAT_FOLDER_NAME)) + "%n", folderName); + } + String folderId = folderCursor.getString(NOTE_COLUMN_ID); + exportFolderToText(folderId, ps); // 导出文件夹内容 + } while (folderCursor.moveToNext()); + } + folderCursor.close(); // 关闭游标 + } + + // 导出根目录下的笔记 + Cursor noteCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + + "=0", null, null); + + if (noteCursor != null) { + if (noteCursor.moveToFirst()) { + do { + ps.printf((getFormat(FORMAT_NOTE_DATE)) + "%n", DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))); + // 导出该笔记的内容 + String noteId = noteCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (noteCursor.moveToNext()); + } + noteCursor.close(); // 关闭游标 + } + ps.close(); // 关闭打印流 + + return STATE_SUCCESS; // 返回成功状态 + } + + /** + * 获取导出文本的打印流 + * @return 打印流对象 + */ + 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; + try { + FileOutputStream fos = new FileOutputStream(file); + ps = new PrintStream(fos); + } catch (FileNotFoundException | NullPointerException e) { + e.printStackTrace(); + return null; + } + return ps; + } + } + + /** + * 在SD卡上生成导出文件 + * @param context 上下文对象 + * @param filePathResId 文件路径资源ID + * @param fileNameFormatResId 文件名格式资源ID + * @return 生成的文件对象 + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + StringBuilder sb = new StringBuilder(); + // 构建文件路径 + sb.append(Environment.getExternalStorageDirectory()); // SD卡根目录 + sb.append(context.getString(filePathResId)); // 附加路径 + File filedir = new File(sb.toString()); // 创建目录对象 + // 构建文件名 + sb.append(context.getString( + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), + System.currentTimeMillis()))); // 按日期格式命名 + File file = new File(sb.toString()); // 创建文件对象 + + try { + // 创建目录(如果不存在) + if (!filedir.exists()) { + filedir.mkdir(); + } + // 创建文件(如果不存在) + if (!file.exists()) { + file.createNewFile(); + } + return file; + } catch (SecurityException | IOException e) { + e.printStackTrace(); + } + + return null; // 创建失败返回null + } +} + + diff --git a/src/mi_note/app/src/main/java/net/micode/notes/tool/DataUtils.java b/src/mi_note/app/src/main/java/net/micode/notes/tool/DataUtils.java new file mode 100644 index 0000000..ee36846 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/tool/DataUtils.java @@ -0,0 +1,403 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.mi code.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义数据工具类的包路径 +package net.micode.notes.tool; + +// 导入Android内容提供者相关类 +import android.content.ContentProviderOperation; // 内容提供者操作 +import android.content.ContentProviderResult; // 内容提供者操作结果 +import android.content.ContentResolver; // 内容解析器 +import android.content.ContentUris; // URI工具 +import android.content.ContentValues; // 内容值对象 +import android.content.OperationApplicationException; // 操作应用异常 +// 导入数据库相关类 +import android.database.Cursor; // 数据库游标 +// 导入远程异常类 +import android.os.RemoteException; // 远程调用异常 +// 导入日志工具 +import android.util.Log; // 日志工具 + +// 导入项目数据类 +import net.micode.notes.data.Notes; // 笔记常量定义 +import net.micode.notes.data.Notes.CallNote; // 通话笔记类型 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列定义 +// 导入小部件属性类 +import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; // 小部件属性 + +// 导入集合类 +import java.util.ArrayList; // 动态数组 +import java.util.HashSet; // 哈希集合 +import android.os.Environment; +/** + * 数据工具类,提供批量操作数据库的方法 + */ +public class DataUtils { + // 日志标签 + public static final String TAG = "DataUtils"; + + /** + * 批量删除笔记 + * @param resolver 内容解析器 + * @param ids 要删除的笔记ID集合 + * @return 是否删除成功 + */ + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 参数校验 + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; // 没有要删除的笔记视为成功 + } + if (ids.isEmpty()) { + Log.d(TAG, "no id is in the hashset"); + return true; // 空集合视为成功 + } + + // 创建批量操作列表 + ArrayList operationList = new ArrayList<>(); + // 遍历要删除的笔记ID + for (long id : ids) { + if(id == Notes.ID_ROOT_FOLDER) { // 防止删除系统根文件夹 + Log.e(TAG, "Don't delete system folder root"); + continue; + } + // 创建删除操作 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + operationList.add(builder.build()); // 添加到操作列表 + } + try { + // 执行批量操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果 + if (results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids); + return false; + } + return true; + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, String.format("%s: %s", e, e.getMessage())); + } + return false; + } + + /** + * 移动笔记到指定文件夹 + * @param resolver 内容解析器 + * @param id 笔记ID + * @param srcFolderId 源文件夹ID + * @param desFolderId 目标文件夹ID + */ + public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { + // 创建要更新的内容值 + ContentValues values = new ContentValues(); + values.put(NoteColumns.PARENT_ID, desFolderId); // 设置新父文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 记录原父文件夹ID + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为已修改 + // 执行更新 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + } + + /** + * 批量移动笔记到指定文件夹 + * @param resolver 内容解析器 + * @param ids 要移动的笔记ID集合 + * @param folderId 目标文件夹ID + * @return 是否移动成功 + */ + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, + long folderId) { + // 参数校验 + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; // 没有要移动的笔记视为成功 + } + + // 创建批量操作列表 + ArrayList operationList = new ArrayList<>(); + // 遍历要移动的笔记ID + for (long id : ids) { + // 创建更新操作 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + builder.withValue(NoteColumns.PARENT_ID, folderId); // 设置新父文件夹ID + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); // 标记为已修改 + operationList.add(builder.build()); // 添加到操作列表 + } + + try { + // 执行批量操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果 + if (results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids); + return false; + } + return true; + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, String.format("%s: %s", e, e.getMessage())); + } + return false; + } + + /** + * 获取用户文件夹数量(排除系统文件夹) + * @param resolver 内容解析器 + * @return 用户文件夹数量 + */ + public static int getUserFolderCount(ContentResolver resolver) { + // 查询符合条件的文件夹数量 + Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, + new String[] { "COUNT(*)" }, // 只查询计数 + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", // 查询条件 + new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, // 参数值 + null); // 无排序 + + int count = 0; + if(cursor != null) { + if(cursor.moveToFirst()) { + try { + count = cursor.getInt(0); // 获取计数结果 + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "get folder count failed:" + e); + } finally { + cursor.close(); // 确保关闭游标 + } + } + } + return count; + } + + /** + * 检查指定类型笔记是否在数据库中可见(不在回收站) + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @param type 笔记类型 + * @return 是否可见 + */ + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + // 查询指定条件的笔记 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, // 查询所有列 + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, // 条件 + new String [] {String.valueOf(type)}, // 参数值 + null); // 无排序 + + boolean exist = false; + if (cursor != null) { + if (cursor.getCount() > 0) { // 如果有记录 + exist = true; + } + cursor.close(); // 关闭游标 + } + return exist; + } + + /** + * 检查笔记是否存在于笔记数据库中 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 是否存在 + */ + 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.getCount() > 0) { // 如果有记录 + exist = true; + } + cursor.close(); // 关闭游标 + } + return exist; + } + + /** + * 检查数据项是否存在于数据数据库中 + * @param resolver 内容解析器 + * @param dataId 数据ID + * @return 是否存在 + */ + 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.getCount() > 0) { // 如果有记录 + exist = true; + } + cursor.close(); // 关闭游标 + } + return exist; + } + + /** + * 检查指定名称的文件夹是否已存在 + * @param resolver 内容解析器 + * @param name 文件夹名称 + * @return 是否存在 + */ + public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { + // 查询指定名称的文件夹 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + // 文件夹类型 + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + // 不在回收站 + " AND " + NoteColumns.SNIPPET + "=?", // 名称匹配 + new String[] { name }, null); // 参数值 + + boolean exist = false; + if(cursor != null) { + if(cursor.getCount() > 0) { // 如果有记录 + exist = true; + } + cursor.close(); // 关闭游标 + } + return exist; + } + + /** + * 获取文件夹关联的小部件集合 + * @param resolver 内容解析器 + * @param folderId 文件夹ID + * @return 小部件属性集合 + */ + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + // 查询文件夹下笔记的小部件信息 + Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, + new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, // 查询小部件ID和类型 + NoteColumns.PARENT_ID + "=?", // 指定父文件夹 + new String[] { String.valueOf(folderId) }, // 参数值 + null); // 无排序 + + HashSet set = null; + if (c != null) { + if (c.moveToFirst()) { + set = new HashSet<>(); // 创建集合 + do { + try { + // 创建小部件属性对象并添加到集合 + AppWidgetAttribute widget = new AppWidgetAttribute(); + widget.widgetId = c.getInt(0); // 小部件ID + widget.widgetType.set(c.getInt(1)); // 小部件类型 + set.add(widget); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, e.toString()); + } + } while (c.moveToNext()); + } + c.close(); // 关闭游标 + } + return set; + } + + /** + * 根据笔记ID获取通话号码 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 通话号码 + */ + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 查询指定笔记的通话数据 + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String [] { CallNote.PHONE_NUMBER }, // 只查询电话号码 + CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", // 条件 + new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, // 参数值 + null); // 无排序 + + if (cursor != null && cursor.moveToFirst()) { + try (cursor) { + return cursor.getString(0); // 返回电话号码 + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call number fails " + e); + } + // 确保关闭游标 + } + return ""; // 未找到返回空字符串 + } + + /** + * 根据电话号码和通话日期获取笔记ID + * @param resolver 内容解析器 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + * @return 笔记ID + */ + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + // 查询符合条件的数据项 + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String [] { CallNote.NOTE_ID }, // 只查询笔记ID + CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + + CallNote.PHONE_NUMBER + ",?)", // 条件 + new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, // 参数值 + null); // 无排序 + + if (cursor != null) { + if (cursor.moveToFirst()) { + try { + return cursor.getLong(0); // 返回笔记ID + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call note id fails " + e); + } + } + cursor.close(); // 关闭游标 + } + return 0; // 未找到返回0 + } + + /** + * 根据笔记ID获取摘要内容 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 笔记摘要 + */ + public static String getSnippetById(ContentResolver resolver, long noteId) { + // 查询指定笔记的摘要 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String [] { NoteColumns.SNIPPET }, // 只查询摘要 + NoteColumns.ID + "=?", // 条件 + new String [] { String.valueOf(noteId)}, // 参数值 + null); // 无排序 + + if (cursor != null) { + String snippet = ""; + if (cursor.moveToFirst()) { + snippet = cursor.getString(0); // 获取摘要 + } + cursor.close(); // 关闭游标 + return snippet; + } + throw new IllegalArgumentException("Note is not found with id: " + noteId); // 未找到抛出异常 + } + + /** + * 格式化摘要内容(去除首尾空格和换行) + * @param snippet 原始摘要 + * @return 格式化后的摘要 + */ + public static String getFormattedSnippet(String snippet) { + if (snippet != null) { + snippet = snippet.trim(); // 去除首尾空格 + int index = snippet.indexOf('\n'); // 查找第一个换行符 + if (index != -1) { + snippet = snippet.substring(0, index); // 截取到第一个换行符前的内容 + } + } + return snippet; + } + // 在DataUtils类中添加方法 + public static boolean externalStorageAvailable() { + return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java b/src/mi_note/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java new file mode 100644 index 0000000..b7f2779 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java @@ -0,0 +1,119 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.tool; + +/** + * Google Tasks API JSON字段常量工具类 + * 包含与Google Tasks API交互时使用的所有JSON键名和固定值 + */ +public class GTaskStringUtils { + // ======================== Google Tasks API JSON字段常量 ======================== + // 操作ID字段 + public final static String GTASK_JSON_ACTION_ID = "action_id"; + // 操作列表字段 + public final static String GTASK_JSON_ACTION_LIST = "action_list"; + // 操作类型字段 + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + // 创建操作类型 + public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; + // 获取全部操作类型 + public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + // 移动操作类型 + public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; + // 更新操作类型 + public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + // 创建者ID字段 + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + // 子实体字段 + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + // 客户端版本字段 + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + // 完成状态字段 + public final static String GTASK_JSON_COMPLETED = "completed"; + // 当前列表ID字段 + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + // 默认列表ID字段 + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + // 删除状态字段 + public final static String GTASK_JSON_DELETED = "deleted"; + // 目标列表字段 + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + // 目标父级字段 + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + // 目标父级类型字段 + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + // 实体变更字段 + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + // 实体类型字段 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + // 获取已删除项字段 + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + // ID字段 + public final static String GTASK_JSON_ID = "id"; + // 索引字段 + public final static String GTASK_JSON_INDEX = "index"; + // 最后修改时间字段 + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + // 最新同步点字段 + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + // 列表ID字段 + public final static String GTASK_JSON_LIST_ID = "list_id"; + // 列表集合字段 + public final static String GTASK_JSON_LISTS = "lists"; + // 名称字段 + public final static String GTASK_JSON_NAME = "name"; + // 新ID字段(用于创建后获取服务器分配的ID) + public final static String GTASK_JSON_NEW_ID = "new_id"; + // 笔记内容字段 + public final static String GTASK_JSON_NOTES = "notes"; + // 父级ID字段 + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + // 前一个兄弟节点ID字段 + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + // 结果集字段 + public final static String GTASK_JSON_RESULTS = "results"; + // 源列表字段 + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + // 任务集合字段 + public final static String GTASK_JSON_TASKS = "tasks"; + // 类型字段 + public final static String GTASK_JSON_TYPE = "type"; + // 分组类型 + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + // 任务类型 + public final static String GTASK_JSON_TYPE_TASK = "TASK"; + // 用户信息字段 + public final static String GTASK_JSON_USER = "user"; + + // ======================== MIUI笔记特定常量 ======================== + + // MIUI笔记文件夹前缀(用于标识MIUI创建的文件夹) + public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + // 默认文件夹名称 + public final static String FOLDER_DEFAULT = "Default"; + // 通话笔记文件夹名称 + public final static String FOLDER_CALL_NOTE = "Call_Note"; + // 元数据文件夹名称 + public final static String FOLDER_META = "METADATA"; + + // ======================== 元数据相关常量 ======================== + + // 元数据头-Google Task ID + public final static String META_HEAD_GTASK_ID = "meta_gid"; + // 元数据头-笔记信息 + public final static String META_HEAD_NOTE = "meta_note"; + // 元数据头-数据信息 + public final static String META_HEAD_DATA = "meta_data"; + // 元数据笔记的特殊名称(提示用户不要修改或删除) + public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/tool/ResourceParser.java b/src/mi_note/app/src/main/java/net/micode/notes/tool/ResourceParser.java new file mode 100644 index 0000000..57a6516 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/tool/ResourceParser.java @@ -0,0 +1,283 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.tool; + +// 导入Android相关类 +import android.content.Context; // 上下文对象 +import android.preference.PreferenceManager; // 偏好设置管理器 + +// 导入项目资源 +import net.micode.notes.R; // 项目资源文件 +import net.micode.notes.ui.NotesPreferenceActivity; // 偏好设置Activity + +/** + * 资源解析工具类 + * 用于获取和管理笔记应用的各种UI资源 + */ +public class ResourceParser { + + // ======================== 颜色常量定义 ======================== + + // 黄色背景标识 + public static final int YELLOW = 0; + // 蓝色背景标识 + public static final int BLUE = 1; + // 白色背景标识 + public static final int WHITE = 2; + // 绿色背景标识 + public static final int GREEN = 3; + // 红色背景标识 + public static final int RED = 4; + + // 默认背景颜色(黄色) + public static final int BG_DEFAULT_COLOR = YELLOW; + + // ======================== 字体大小常量定义 ======================== + + // 小号字体 + public static final int TEXT_SMALL = 0; + // 中号字体 + public static final int TEXT_MEDIUM = 1; + // 大号字体 + public static final int TEXT_LARGE = 2; + // 超大号字体 + public static final int TEXT_SUPER = 3; + + // 默认字体大小(中号) + public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; + + /** + * 笔记背景资源类 + * 提供编辑界面的背景资源获取方法 + */ + public static class NoteBgResources { + // 编辑区域背景资源数组 + private final static int [] BG_EDIT_RESOURCES = new int [] { + R.drawable.edit_yellow, // 黄色背景 + R.drawable.edit_blue, // 蓝色背景 + R.drawable.edit_white, // 白色背景 + R.drawable.edit_green, // 绿色背景 + R.drawable.edit_red // 红色背景 + }; + + // 编辑区域标题背景资源数组 + private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { + R.drawable.edit_title_yellow, // 黄色标题背景 + R.drawable.edit_title_blue, // 蓝色标题背景 + R.drawable.edit_title_white, // 白色标题背景 + R.drawable.edit_title_green, // 绿色标题背景 + R.drawable.edit_title_red // 红色标题背景 + }; + + /** + * 获取笔记背景资源 + * @param id 颜色ID(YELLOW/BLUE/WHITE/GREEN/RED) + * @return 对应的背景资源ID + */ + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + /** + * 获取笔记标题背景资源 + * @param id 颜色ID(YELLOW/BLUE/WHITE/GREEN/RED) + * @return 对应的标题背景资源ID + */ + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + /** + * 获取默认背景ID + * @param context 上下文对象 + * @return 背景颜色ID + */ + public static int getDefaultBgId(Context context) { + // 检查用户是否启用了随机背景色 + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( + NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + // 返回随机背景色 + return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); + } else { + // 返回默认背景色 + return BG_DEFAULT_COLOR; + } + } + + /** + * 笔记列表项背景资源类 + * 提供笔记列表的各种背景资源获取方法 + */ + public static class NoteItemBgResources { + // 列表第一项背景资源数组 + private final static int [] BG_FIRST_RESOURCES = new int [] { + R.drawable.list_yellow_up, // 黄色背景第一项 + R.drawable.list_blue_up, // 蓝色背景第一项 + R.drawable.list_white_up, // 白色背景第一项 + R.drawable.list_green_up, // 绿色背景第一项 + R.drawable.list_red_up // 红色背景第一项 + }; + + // 列表中间项背景资源数组 + private final static int [] BG_NORMAL_RESOURCES = new int [] { + R.drawable.list_yellow_middle, // 黄色背景中间项 + R.drawable.list_blue_middle, // 蓝色背景中间项 + R.drawable.list_white_middle, // 白色背景中间项 + R.drawable.list_green_middle, // 绿色背景中间项 + R.drawable.list_red_middle // 红色背景中间项 + }; + + // 列表最后一项背景资源数组 + private final static int [] BG_LAST_RESOURCES = new int [] { + R.drawable.list_yellow_down, // 黄色背景最后一项 + R.drawable.list_blue_down, // 蓝色背景最后一项 + R.drawable.list_white_down, // 白色背景最后一项 + R.drawable.list_green_down, // 绿色背景最后一项 + R.drawable.list_red_down, // 红色背景最后一项 + }; + + // 列表单项背景资源数组(当列表只有一项时使用) + private final static int [] BG_SINGLE_RESOURCES = new int [] { + R.drawable.list_yellow_single, // 黄色背景单项 + R.drawable.list_blue_single, // 蓝色背景单项 + R.drawable.list_white_single, // 白色背景单项 + R.drawable.list_green_single, // 绿色背景单项 + R.drawable.list_red_single // 红色背景单项 + }; + + /** + * 获取列表第一项背景资源 + * @param id 颜色ID + * @return 对应的背景资源ID + */ + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + } + + /** + * 获取列表最后一项背景资源 + * @param id 颜色ID + * @return 对应的背景资源ID + */ + public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; + } + + /** + * 获取列表单项背景资源 + * @param id 颜色ID + * @return 对应的背景资源ID + */ + public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; + } + + /** + * 获取列表中间项背景资源 + * @param id 颜色ID + * @return 对应的背景资源ID + */ + public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_RESOURCES[id]; + } + + /** + * 获取文件夹背景资源 + * @return 文件夹背景资源ID + */ + public static int getFolderBgRes() { + return R.drawable.list_folder; + } + } + + /** + * 小部件背景资源类 + * 提供小部件的背景资源获取方法 + */ + public static class WidgetBgResources { + // 2x大小小部件背景资源数组 + private final static int [] BG_2X_RESOURCES = new int [] { + R.drawable.widget_2x_yellow, // 黄色2x小部件 + R.drawable.widget_2x_blue, // 蓝色2x小部件 + R.drawable.widget_2x_white, // 白色2x小部件 + R.drawable.widget_2x_green, // 绿色2x小部件 + R.drawable.widget_2x_red, // 红色2x小部件 + }; + + /** + * 获取2x小部件背景资源 + * @param id 颜色ID + * @return 对应的背景资源ID + */ + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 4x大小小部件背景资源数组 + private final static int [] BG_4X_RESOURCES = new int [] { + R.drawable.widget_4x_yellow, // 黄色4x小部件 + R.drawable.widget_4x_blue, // 蓝色4x小部件 + R.drawable.widget_4x_white, // 白色4x小部件 + R.drawable.widget_4x_green, // 绿色4x小部件 + R.drawable.widget_4x_red // 红色4x小部件 + }; + + /** + * 获取4x小部件背景资源 + * @param id 颜色ID + * @return 对应的背景资源ID + */ + public static int getWidget4xBgResource(int id) { + return BG_4X_RESOURCES[id]; + } + } + + /** + * 文本外观资源类 + * 提供文本样式资源的获取方法 + */ + public static class TextAppearanceResources { + // 文本样式资源数组 + private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { + R.style.TextAppearanceNormal, // 普通文本样式 + R.style.TextAppearanceMedium, // 中等文本样式 + R.style.TextAppearanceLarge, // 大号文本样式 + R.style.TextAppearanceSuper // 超大号文本样式 + }; + + /** + * 获取文本样式资源 + * @param id 字体大小ID(TEXT_SMALL/MEDIUM/LARGE/SUPER) + * @return 对应的样式资源ID + */ + public static int getTexAppearanceResource(int id) { + /* + * 修复在共享偏好设置中存储资源ID的bug + * 如果ID大于资源数组长度,则返回默认字体大小 + */ + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + /** + * 获取文本样式资源总数 + * @return 样式资源数量 + */ + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java new file mode 100644 index 0000000..0c802b5 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java @@ -0,0 +1,192 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.ui; + +// 导入Android相关类库 +import android.app.Activity; // Activity基类 +import android.app.AlertDialog; // 对话框组件 +import android.content.Context; // 上下文对象 +import android.content.DialogInterface; // 对话框接口 +import android.content.DialogInterface.OnClickListener; // 对话框点击监听 +import android.content.DialogInterface.OnDismissListener; // 对话框关闭监听 +import android.content.Intent; // 意图对象 +import android.media.AudioManager; // 音频管理 +import android.media.MediaPlayer; // 媒体播放器 +import android.media.RingtoneManager; // 铃声管理 +import android.net.Uri; // URI处理 +import android.os.Bundle; // Bundle数据存储 +import android.os.PowerManager; // 电源管理 +import android.provider.Settings; // 系统设置 +import android.view.Window; // 窗口管理 +import android.view.WindowManager; // 窗口管理器 + +// 导入项目资源 +import net.micode.notes.R; // 资源文件 +// 导入数据类 +import net.micode.notes.data.Notes; // 笔记常量 +// 导入工具类 +import net.micode.notes.tool.DataUtils; // 数据工具类 + +// IO异常处理 +import java.io.IOException; +import java.util.Objects; + +/** + * 闹钟提醒Activity + * 处理笔记提醒通知,包括显示对话框和播放提醒音效 + */ +public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { + // 成员变量 + private long mNoteId; // 当前笔记ID + 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()) { + 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 // 窗口装饰布局 + ); + } + + // 获取启动Activity的Intent + Intent intent = getIntent(); + + try { + // 从Intent URI中解析出笔记ID(格式:content://xxx/notes/1) + mNoteId = Long.parseLong(Objects.requireNonNull(intent.getData()).getPathSegments().get(1)); + // 通过ContentResolver获取笔记摘要 + mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); + // 截取摘要前60个字符,超出部分添加省略提示 + 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(); // 笔记不可见则直接结束Activity + } + } + + /** + * 检查屏幕是否点亮 + * @return boolean 屏幕状态 + */ + 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) { + mPlayer.setAudioStreamType(silentModeStreams); + } else { + mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); + } + + try { + mPlayer.setDataSource(this, url); // 设置音频源 + mPlayer.prepare(); // 准备播放 + mPlayer.setLooping(true); // 设置循环播放 + mPlayer.start(); // 开始播放 + } catch (IllegalArgumentException | SecurityException | IllegalStateException | + IOException e) { + e.printStackTrace(); + } + } + + /** + * 显示操作对话框 + */ + private void showActionDialog() { + 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) { + if (which == DialogInterface.BUTTON_NEGATIVE) { // "进入"按钮 + Intent intent = new Intent(this, NoteEditActivity.class); + intent.setAction(Intent.ACTION_VIEW); // 设置动作为查看 + intent.putExtra(Intent.EXTRA_UID, mNoteId); // 传递笔记ID + startActivity(intent); // 启动笔记编辑Activity + } + } + + /** + * 对话框关闭事件处理 + */ + public void onDismiss(DialogInterface dialog) { + stopAlarmSound(); // 停止音效 + finish(); // 结束当前Activity + } + + /** + * 停止播放提醒音效 + */ + private void stopAlarmSound() { + if (mPlayer != null) { + mPlayer.stop(); // 停止播放 + mPlayer.release(); // 释放资源 + mPlayer = null; // 置空引用 + } + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmInitReceiver.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmInitReceiver.java new file mode 100644 index 0000000..fc05f63 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmInitReceiver.java @@ -0,0 +1,101 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.ui; + +// 导入Android相关类库 +import android.app.AlarmManager; // 系统闹钟服务 +import android.app.PendingIntent; // 延时意图,用于延迟执行操作 +import android.content.BroadcastReceiver; // 广播接收器基类 +import android.content.ContentUris; // Content URI工具类 +import android.content.Context; // 上下文对象 +import android.content.Intent; // 意图对象 +import android.database.Cursor; // 数据库游标 + +// 导入项目数据类 +import net.micode.notes.data.Notes; // 笔记常量定义 +import net.micode.notes.data.Notes.NoteColumns; // 笔记表列名定义 + +/** + * 闹钟初始化接收器 + * 在系统启动或时区变更时重新设置所有未来的笔记提醒 + */ +public class AlarmInitReceiver extends BroadcastReceiver { + + // 查询笔记的投影列(只需要ID和提醒日期) + private static final String [] PROJECTION = new String [] { + NoteColumns.ID, // 笔记ID列 + NoteColumns.ALERTED_DATE // 提醒日期列 + }; + + // 列索引常量 + private static final int COLUMN_ID = 0; // ID列索引 + private static final int COLUMN_ALERTED_DATE = 1; // 提醒日期列索引 + + /** + * 广播接收回调方法 + * 当接收到BOOT_COMPLETED(系统启动完成)或TIMEZONE_CHANGED(时区变更)广播时触发 + * + * @param context 上下文对象 + * @param intent 接收到的广播意图 + */ + @Override + public void onReceive(Context context, Intent intent) { + // 获取当前系统时间 + long currentDate = System.currentTimeMillis(); + + // 查询所有未来需要提醒的笔记(类型为普通笔记且提醒日期大于当前时间) + Cursor c = context.getContentResolver().query( + Notes.CONTENT_NOTE_URI, // 笔记内容URI + PROJECTION, // 查询的列 + NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, + new String[] { String.valueOf(currentDate) }, // 参数:当前时间 + null); // 无排序要求 + + // 处理查询结果 + if (c != null) { + // 如果查询到数据 + if (c.moveToFirst()) { + do { + // 获取笔记的提醒时间 + long alertDate = c.getLong(COLUMN_ALERTED_DATE); + + // 创建闹钟触发时要发送的广播意图 + Intent sender = new Intent(context, AlarmReceiver.class); + // 设置数据URI为笔记的完整URI(content://xxx/notes/[id]) + sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, + c.getLong(COLUMN_ID))); + + // 创建PendingIntent用于延迟发送广播 + PendingIntent pendingIntent = PendingIntent.getBroadcast( + context, // 上下文 + 0, // requestCode + sender, // 要发送的Intent + 0); // flags + + // 获取系统闹钟服务 + AlarmManager alermManager = (AlarmManager) context + .getSystemService(Context.ALARM_SERVICE); + + // 设置闹钟(RTC_WAKEUP表示即使设备休眠也唤醒CPU) + alermManager.set( + AlarmManager.RTC_WAKEUP, // 使用实时时钟并在触发时唤醒设备 + alertDate, // 触发时间 + pendingIntent); // 触发时要执行的PendingIntent + + } while (c.moveToNext()); // 处理下一条记录 + } + // 关闭游标释放资源 + c.close(); + } + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmReceiver.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmReceiver.java new file mode 100644 index 0000000..9b1179f --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/AlarmReceiver.java @@ -0,0 +1,54 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义包路径,指明该类所在的包位置 +package net.micode.notes.ui; + +// 导入Android相关类库 +import android.annotation.SuppressLint; +import android.content.BroadcastReceiver; // 广播接收器基类 +import android.content.Context; // 上下文对象,提供应用环境信息 +import android.content.Intent; // 意图对象,用于组件间通信 + +/** + * 闹钟广播接收器 + * 接收系统闹钟触发广播,启动闹钟提醒界面 + * 功能说明: + * 1. 继承自BroadcastReceiver,专门处理闹钟触发事件 + * 2. 当闹钟触发时,启动AlarmAlertActivity显示提醒 + * 3. 作为系统闹钟和用户界面之间的桥梁 + */ +public class AlarmReceiver extends BroadcastReceiver { + + /** + * 广播接收回调方法 + * 当闹钟触发时由系统自动调用 + * + * @param context 上下文对象,提供启动Activity所需的运行环境 + * @param intent 包含触发闹钟的相关信息,特别是笔记的URI数据 + */ + @Override + public void onReceive(Context context, @SuppressLint("UnsafeIntentLaunch") Intent intent) { + // 修改Intent的目标组件为AlarmAlertActivity + intent.setClass(context, AlarmAlertActivity.class); + + // 添加NEW_TASK标志,因为从非Activity上下文启动Activity需要此标志 + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + // 启动闹钟提醒Activity + context.startActivity(intent); + /// 注意点: + /// 1. 原Intent中已经包含了笔记URI数据(在AlarmInitReceiver中设置) + /// 2. FLAG_ACTIVITY_NEW_TASK确保可以正确启动Activity + /// 3. 此方法执行时间应尽量短,避免ANR(应用无响应)错误 + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/DateTimePicker.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/DateTimePicker.java new file mode 100644 index 0000000..b08bd1e --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/DateTimePicker.java @@ -0,0 +1,457 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.ui; + +// 导入Java工具类 +import java.text.DateFormatSymbols; // 用于获取AM/PM本地化字符串 +import java.util.Calendar; // 日期时间处理类 + +// 导入项目资源 +import net.micode.notes.R; // 资源ID引用 + +// 导入Android相关类 +import android.content.Context; // 上下文对象 +import android.text.format.DateFormat; // 日期格式化工具 +import android.view.View; // 基础视图类 +import android.widget.FrameLayout; // 帧布局容器 +import android.widget.NumberPicker;// 数字选择器控件 + +/** + * 自定义日期时间选择器组件 + * 包含日期、小时、分钟和AM/PM选择器 + */ +public class DateTimePicker extends FrameLayout { + // 默认启用状态 + private static final boolean DEFAULT_ENABLE_STATE = true; + + // 时间相关常量 + private static final int HOURS_IN_HALF_DAY = 12; // 半天的12小时 + private static final int HOURS_IN_ALL_DAY = 24; // 全天24小时 + private static final int DAYS_IN_ALL_WEEK = 7; // 一周7天 + + // 数字选择器范围常量 + private static final int DATE_SPINNER_MIN_VAL = 0; + private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1; + private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0; + private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23; + private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1; + 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; + private static final int AMPM_SPINNER_MIN_VAL = 0; + private static final int AMPM_SPINNER_MAX_VAL = 1; + // 控件成员变量 + private final NumberPicker mDateSpinner; // 日期选择器 + private final NumberPicker mHourSpinner; // 小时选择器 + private final NumberPicker mMinuteSpinner;// 分钟选择器 + private final NumberPicker mAmPmSpinner; // AM/PM选择器 + // 状态变量 + private final Calendar mDate; // 当前选择的日期时间 + private final String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK]; // 日期显示值数组 + private boolean mIsAm; // 是否为上午 + private boolean mIs24HourView; // 是否为24小时制 + private boolean mIsEnabled = DEFAULT_ENABLE_STATE; // 是否启用 + private boolean mInitialising; // 是否正在初始化 + + // 回调接口 + private OnDateTimeChangedListener mOnDateTimeChangedListener; + + /** + * 日期时间变化回调接口 + */ + public interface OnDateTimeChangedListener { + void onDateTimeChanged(DateTimePicker view, int year, int month, + 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)); + } + + // 主构造函数 + public DateTimePicker(Context context, long date, boolean is24HourView) { + super(context); + mDate = Calendar.getInstance(); + mInitialising = true; + mIsAm = (getCurrentHourOfDay() < HOURS_IN_HALF_DAY); + + // 加载布局 + inflate(context, R.layout.datetime_picker, this); + + // 初始化日期选择器 + mDateSpinner = findViewById(R.id.date); + mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL); + mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL); + // 日期变化监听器 + // 计算日期差值并更新日历 + // 更新日期显示 + // 触发回调 + NumberPicker.OnValueChangeListener mOnDateChangedListener = (picker, oldVal, newVal) -> { + // 计算日期差值并更新日历 + mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal); + updateDateControl(); // 更新日期显示 + onDateTimeChanged(); // 触发回调 + }; + mDateSpinner.setOnValueChangedListener(mOnDateChangedListener); + + // 初始化小时选择器 + mHourSpinner = findViewById(R.id.hour); + // 小时变化监听器(处理12/24小时制的复杂逻辑) + // 处理12小时制下的日期变更 + // 从PM到AM跨日情况 + // 从AM到PM跨日情况 + // AM/PM切换点检查 + // 处理24小时制下的日期变更 + // 23点转到0点(次日) + // 0点转到23点(前一天) + // 更新实际小时值(考虑AM/PM) + // 处理需要变更日期的情况 + NumberPicker.OnValueChangeListener mOnHourChangedListener = (picker, oldVal, newVal) -> { + boolean isDateChanged = false; + Calendar cal = Calendar.getInstance(); + + // 处理12小时制下的日期变更 + if (!mIs24HourView) { + // 从PM到AM跨日情况 + if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) { + cal.setTimeInMillis(mDate.getTimeInMillis()); + cal.add(Calendar.DAY_OF_YEAR, 1); + isDateChanged = true; + } + // 从AM到PM跨日情况 + else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { + cal.setTimeInMillis(mDate.getTimeInMillis()); + cal.add(Calendar.DAY_OF_YEAR, -1); + isDateChanged = true; + } + // AM/PM切换点检查 + 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; + updateAmPmControl(); + } + } + // 处理24小时制下的日期变更 + else { + // 23点转到0点(次日) + if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) { + cal.setTimeInMillis(mDate.getTimeInMillis()); + cal.add(Calendar.DAY_OF_YEAR, 1); + isDateChanged = true; + } + // 0点转到23点(前一天) + else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) { + cal.setTimeInMillis(mDate.getTimeInMillis()); + cal.add(Calendar.DAY_OF_YEAR, -1); + isDateChanged = true; + } + } + + // 更新实际小时值(考虑AM/PM) + int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY); + 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)); + } + }; + mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); + + // 初始化分钟选择器 + mMinuteSpinner = findViewById(R.id.minute); + mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL); + mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL); + mMinuteSpinner.setOnLongPressUpdateInterval(100); // 长按加速间隔 + // 分钟变化监听器(处理59->00和00->59的边界情况) + // 处理分钟循环滚动情况 + // 下一小时 + // 上一小时 + // 处理AM/PM切换 + NumberPicker.OnValueChangeListener mOnMinuteChangedListener = (picker, oldVal, 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) { + mDate.add(Calendar.HOUR_OF_DAY, offset); + mHourSpinner.setValue(getCurrentHour()); + updateDateControl(); + + // 处理AM/PM切换 + int newHour = getCurrentHourOfDay(); + if (newHour >= HOURS_IN_HALF_DAY) { + mIsAm = false; + updateAmPmControl(); + } else { + mIsAm = true; + updateAmPmControl(); + } + } + + mDate.set(Calendar.MINUTE, newVal); + onDateTimeChanged(); + }; + mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); + + // 初始化AM/PM选择器(使用本地化字符串) + String[] ampmStrings = new DateFormatSymbols().getAmPmStrings(); + mAmPmSpinner = findViewById(R.id.amPm); + mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL); + mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL); + mAmPmSpinner.setDisplayedValues(ampmStrings); + // AM/PM变化监听器 + // 切换AM/PM状态 + // 调整12小时 + NumberPicker.OnValueChangeListener mOnAmPmChangedListener = (picker, oldVal, newVal) -> { + mIsAm = !mIsAm; // 切换AM/PM状态 + + // 调整12小时 + if (mIsAm) { + mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY); + } else { + mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY); + } + + updateAmPmControl(); + onDateTimeChanged(); + }; + mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener); + + // 初始化控件状态 + updateDateControl(); + updateHourControl(); + updateAmPmControl(); + + // 设置24小时制模式 + set24HourView(is24HourView); + + // 设置当前时间 + setCurrentDate(date); + + // 设置初始启用状态 + setEnabled(DEFAULT_ENABLE_STATE); + + // 初始化完成 + mInitialising = false; + } + + // 启用/禁用控件 + @Override + public void setEnabled(boolean enabled) { + if (mIsEnabled == enabled) return; + + super.setEnabled(enabled); + mDateSpinner.setEnabled(enabled); + mMinuteSpinner.setEnabled(enabled); + mHourSpinner.setEnabled(enabled); + mAmPmSpinner.setEnabled(enabled); + mIsEnabled = enabled; + } + + // 获取当前时间戳 + public long getCurrentDateInTimeMillis() { + return mDate.getTimeInMillis(); + } + + // 设置当前时间(时间戳) + public void setCurrentDate(long date) { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(date); + 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) + ); + } + + // 设置当前时间(各部分) + public void setCurrentDate(int year, int month, int dayOfMonth, + int hourOfDay, int minute) { + setCurrentYear(year); + setCurrentMonth(month); + setCurrentDay(dayOfMonth); + setCurrentHour(hourOfDay); + setCurrentMinute(minute); + } + + // 获取/设置年份 + public int getCurrentYear() { return mDate.get(Calendar.YEAR); } + public void setCurrentYear(int year) { + if (!mInitialising && year == getCurrentYear()) return; + mDate.set(Calendar.YEAR, year); + updateDateControl(); + onDateTimeChanged(); + } + + // 获取/设置月份 + public int getCurrentMonth() { return mDate.get(Calendar.MONTH); } + public void setCurrentMonth(int month) { + if (!mInitialising && month == getCurrentMonth()) return; + mDate.set(Calendar.MONTH, month); + updateDateControl(); + onDateTimeChanged(); + } + + // 获取/设置日 + public int getCurrentDay() { return mDate.get(Calendar.DAY_OF_MONTH); } + public void setCurrentDay(int dayOfMonth) { + if (!mInitialising && dayOfMonth == getCurrentDay()) return; + mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); + updateDateControl(); + onDateTimeChanged(); + } + + // 获取24小时制小时 + public int getCurrentHourOfDay() { + return mDate.get(Calendar.HOUR_OF_DAY); + } + + // 获取当前显示的小时(考虑12/24小时制) + private int getCurrentHour() { + 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; + } + } + } + + // 设置小时 + public void setCurrentHour(int hourOfDay) { + if (!mInitialising && hourOfDay == getCurrentHourOfDay()) return; + + mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); + if (!mIs24HourView) { + // 处理12小时制下的AM/PM状态 + if (hourOfDay >= HOURS_IN_HALF_DAY) { + mIsAm = false; + if (hourOfDay > HOURS_IN_HALF_DAY) { + hourOfDay -= HOURS_IN_HALF_DAY; + } + } else { + mIsAm = true; + if (hourOfDay == 0) { + hourOfDay = HOURS_IN_HALF_DAY; + } + } + updateAmPmControl(); + } + mHourSpinner.setValue(hourOfDay); + onDateTimeChanged(); + } + + // 获取/设置分钟 + public int getCurrentMinute() { return mDate.get(Calendar.MINUTE); } + public void setCurrentMinute(int minute) { + if (!mInitialising && minute == getCurrentMinute()) return; + mMinuteSpinner.setValue(minute); + mDate.set(Calendar.MINUTE, minute); + onDateTimeChanged(); + } + + // 获取/设置24小时制模式 + public boolean is24HourView() { return mIs24HourView; } + public void set24HourView(boolean is24HourView) { + if (mIs24HourView == is24HourView) return; + mIs24HourView = is24HourView; + + // 更新AM/PM选择器可见性 + mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE); + + // 更新小时选择器范围 + updateHourControl(); + + // 设置当前小时(保持实际时间不变) + setCurrentHour(getCurrentHourOfDay()); + + // 更新AM/PM显示 + updateAmPmControl(); + } + + // 更新日期控件显示值(一周日期范围) + private void updateDateControl() { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(mDate.getTimeInMillis()); + 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); + // 格式化日期为"MM.dd EEEE"格式(如"07.15 星期一") + mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal); + } + + mDateSpinner.setDisplayedValues(mDateDisplayValues); + mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); // 中间值(当前日) + mDateSpinner.invalidate(); // 强制重绘 + } + + // 更新AM/PM控件状态 + private void updateAmPmControl() { + if (mIs24HourView) { + mAmPmSpinner.setVisibility(View.GONE); + } else { + int index = mIsAm ? Calendar.AM : Calendar.PM; + mAmPmSpinner.setValue(index); + mAmPmSpinner.setVisibility(View.VISIBLE); + } + } + + // 更新小时控件范围(12/24小时制) + private void updateHourControl() { + if (mIs24HourView) { + mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW); + mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW); + } else { + mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW); + mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW); + } + } + + // 设置日期时间变化监听器 + public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) { + mOnDateTimeChangedListener = callback; + } + + // 触发日期时间变化回调 + private void onDateTimeChanged() { + if (mOnDateTimeChangedListener != null) { + mOnDateTimeChangedListener.onDateTimeChanged(this, + getCurrentYear(), getCurrentMonth(), getCurrentDay(), + getCurrentHourOfDay(), getCurrentMinute()); + } + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/DateTimePickerDialog.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/DateTimePickerDialog.java new file mode 100644 index 0000000..bf37351 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/DateTimePickerDialog.java @@ -0,0 +1,145 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.ui; + +// 导入Java工具类 +import java.util.Calendar; // 日期时间处理类 + +// 导入项目资源 +import net.micode.notes.R; // 资源ID引用 + +// 导入自定义组件 +// 日期时间选择器 +// 时间变化监听器 + +// 导入Android相关类 +import android.app.AlertDialog; // 对话框基类 +import android.content.Context; // 上下文对象 +import android.content.DialogInterface; // 对话框接口 +import android.content.DialogInterface.OnClickListener; // 点击监听器 +import android.text.format.DateFormat; // 日期格式化工具 +import android.text.format.DateUtils; // 日期工具类 + +/** + * 日期时间选择对话框 + * 将DateTimePicker封装在AlertDialog中,提供完整的日期时间选择功能 + */ +public class DateTimePickerDialog extends AlertDialog implements OnClickListener { + + // 成员变量 + private final Calendar mDate = Calendar.getInstance(); // 当前选择的日期时间 + private boolean mIs24HourView; // 是否为24小时制 + private OnDateTimeSetListener mOnDateTimeSetListener; // 回调监听器 + + /** + * 日期时间设置回调接口 + */ + public interface OnDateTimeSetListener { + void OnDateTimeSet(AlertDialog dialog, long date); // 点击确定时回调 + } + + /** + * 构造函数 + * @param context 上下文对象 + * @param date 初始时间(时间戳) + */ + public DateTimePickerDialog(Context context, long date) { + super(context); + + // 初始化日期时间选择器 + // 内嵌的日期时间选择器 + DateTimePicker mDateTimePicker = new DateTimePicker(context); + setView(mDateTimePicker); // 将选择器添加到对话框 + + // 设置时间变化监听器 + mDateTimePicker.setOnDateTimeChangedListener((view, year, month, dayOfMonth, hourOfDay, minute) -> { + // 更新内部Calendar对象 + mDate.set(Calendar.YEAR, year); + mDate.set(Calendar.MONTH, month); + mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); + mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); + mDate.set(Calendar.MINUTE, minute); + + // 更新对话框标题显示 + updateTitle(mDate.getTimeInMillis()); + }); + + // 设置初始时间 + mDate.setTimeInMillis(date); + mDate.set(Calendar.SECOND, 0); // 秒数归零 + mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); + + // 添加确定/取消按钮 + setButton(DialogInterface.BUTTON_POSITIVE, + context.getString(R.string.datetime_dialog_ok), this); + setButton(DialogInterface.BUTTON_NEGATIVE, + context.getString(R.string.datetime_dialog_cancel), + (OnClickListener)null); + + // 根据系统设置初始化24小时制显示 + set24HourView(DateFormat.is24HourFormat(this.getContext())); + + // 初始化标题显示 + updateTitle(mDate.getTimeInMillis()); + } + + /** + * 设置24小时制显示模式 + * @param is24HourView true为24小时制,false为12小时制 + */ + public void set24HourView(boolean is24HourView) { + mIs24HourView = is24HourView; + // 注意:这里未实际传递给DateTimePicker,可能需要额外处理 + } + + /** + * 设置日期时间确定回调 + * @param callBack 回调接口 + */ + public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) { + mOnDateTimeSetListener = callBack; + } + + /** + * 更新对话框标题 + * @param date 时间戳 + */ + private void updateTitle(long date) { + // 设置日期时间显示格式 + int flag = DateUtils.FORMAT_SHOW_YEAR | // 显示年份 + DateUtils.FORMAT_SHOW_DATE | // 显示日期 + DateUtils.FORMAT_SHOW_TIME; // 显示时间 + + // 添加24小时制标志(注意:原代码有错误,应该是FORMAT_24HOUR或FORMAT_12HOUR) + flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR; + + // 设置格式化后的标题 + setTitle(DateUtils.formatDateTime( + this.getContext(), // 上下文 + date, // 时间戳 + flag)); // 格式标志 + } + + /** + * 确定按钮点击回调 + */ + @Override + public void onClick(DialogInterface arg0, int arg1) { + // 触发回调事件 + if (mOnDateTimeSetListener != null) { + mOnDateTimeSetListener.OnDateTimeSet( + this, // 当前对话框实例 + mDate.getTimeInMillis()); // 最终选择的时间戳 + } + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/DropdownMenu.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/DropdownMenu.java new file mode 100644 index 0000000..271056c --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/DropdownMenu.java @@ -0,0 +1,89 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.ui; + +// 导入Android相关类库 +import android.content.Context; // 上下文对象,用于获取系统服务 +import android.view.Menu; // 菜单接口 +import android.view.MenuItem; // 菜单项接口 +// 基础视图类 +// 点击监听接口 +import android.widget.Button; // 按钮控件 +import android.widget.PopupMenu; // 弹出式菜单 +import android.widget.PopupMenu.OnMenuItemClickListener; // 菜单项点击监听 + +// 导入项目资源 +import net.micode.notes.R; // 资源ID引用 + +/** + * 下拉菜单封装类 + * 将PopupMenu与Button绑定,简化下拉菜单的实现 + */ +public class DropdownMenu { + // 成员变量 + private final Button mButton; // 触发菜单的按钮 + private final PopupMenu mPopupMenu; // 弹出菜单实例 + private final Menu mMenu; // 菜单对象 + + /** + * 构造函数 + * @param context 上下文对象 + * @param button 绑定的按钮控件 + * @param menuId 菜单资源ID(R.menu.xxx ) + */ + public DropdownMenu(Context context, Button button, int menuId) { + // 初始化按钮引用 + mButton = button; + // 设置按钮背景(下拉箭头图标) + mButton.setBackgroundResource(R.drawable.dropdown_icon); + + // 创建PopupMenu并绑定到按钮 + mPopupMenu = new PopupMenu(context, mButton); + // 获取菜单对象 + mMenu = mPopupMenu.getMenu(); + // 加载菜单布局 + mPopupMenu.getMenuInflater().inflate(menuId, mMenu); + + // 设置按钮点击事件:显示弹出菜单 + mButton.setOnClickListener(v -> { + mPopupMenu.show(); // 显示弹出菜单 + }); + } + + /** + * 设置菜单项点击监听器 + * @param listener 菜单项点击监听接口实现 + */ + public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) { + if (mPopupMenu != null) { + mPopupMenu.setOnMenuItemClickListener(listener); + } + } + + /** + * 查找菜单项 + * @param id 菜单项ID(R.id.xxx ) + * @return 找到的MenuItem对象 + */ + public MenuItem findItem(int id) { + return mMenu.findItem(id); + } + + /** + * 设置按钮文本 + * @param title 要显示的标题文本 + */ + public void setTitle(CharSequence title) { + mButton.setText(title); + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/FoldersListAdapter.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/FoldersListAdapter.java new file mode 100644 index 0000000..672a0a5 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/FoldersListAdapter.java @@ -0,0 +1,131 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.ui; + +// 导入Android相关类库 +import android.content.Context; // 上下文对象 +import android.database.Cursor; // 数据库游标 +import android.view.View; // 基础视图类 +import android.view.ViewGroup; // 视图组 +import android.widget.CursorAdapter; // 游标适配器基类 +import android.widget.LinearLayout; // 线性布局 +import android.widget.TextView; // 文本视图 + +// 导入项目资源 +import net.micode.notes.R; // 资源ID引用 +// 导入数据类 +import net.micode.notes.data.Notes; // 笔记常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记表列名 + +/** + * 文件夹列表适配器 + * 继承自CursorAdapter,用于将文件夹数据绑定到列表视图 + */ +public class FoldersListAdapter extends CursorAdapter { + // 查询投影列(需要获取的字段) + public static final String [] PROJECTION = { + NoteColumns.ID, // 笔记ID + NoteColumns.SNIPPET // 文件夹名称(使用SNIPPET字段存储) + }; + + // 列索引常量 + public static final int ID_COLUMN = 0; // ID列索引 + public static final int NAME_COLUMN = 1; // 名称列索引 + + /** + * 构造函数 + * @param context 上下文对象 + * @param c 数据库游标 + */ + public FoldersListAdapter(Context context, Cursor c) { + super(context, c); + // TODO: 可在此处添加额外初始化代码 + } + + /** + * 创建新列表项视图 + * @param context 上下文 + * @param cursor 数据游标 + * @param parent 父视图组 + * @return 创建好的列表项视图 + */ + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + // 创建自定义的文件夹列表项视图 + return new FolderListItem(context); + } + + /** + * 绑定数据到已有视图 + * @param view 要绑定的视图 + * @param context 上下文 + * @param cursor 数据游标 + */ + @Override + public void bindView(View view, Context context, Cursor cursor) { + // 确保视图是FolderListItem类型 + if (view instanceof FolderListItem) { + String folderName; + // 判断是否为根文件夹(特殊处理) + if (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) { + folderName = context.getString(R.string.menu_move_parent_folder); + } else { + folderName = cursor.getString(NAME_COLUMN); + } + // 调用视图的bind方法设置名称 + ((FolderListItem) view).bind(folderName); + } + } + + /** + * 获取指定位置的文件夹名称 + * @param context 上下文 + * @param position 位置索引 + * @return 文件夹名称 + */ + public String getFolderName(Context context, int position) { + // 获取对应位置的游标数据 + Cursor cursor = (Cursor) getItem(position); + // 判断是否为根文件夹(特殊处理) + return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? + context.getString(R.string.menu_move_parent_folder) : + cursor.getString(NAME_COLUMN); + } + + /** + * 自定义文件夹列表项视图(内部类) + */ + private static class FolderListItem extends LinearLayout { + private final TextView mName; // 显示文件夹名称的文本视图 + + /** + * 构造函数 + * @param context 上下文 + */ + public FolderListItem(Context context) { + super(context); + // 加载布局文件 + inflate(context, R.layout.folder_list_item, this); + // 获取名称文本视图 + mName = findViewById(R.id.tv_folder_name); + } + + /** + * 绑定文件夹名称 + * @param name 文件夹名称 + */ + public void bind(String name) { + mName.setText(name); // 设置文本内容 + } + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java new file mode 100644 index 0000000..c904b35 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java @@ -0,0 +1,1257 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义NoteEditActivity所在的包路径 +package net.micode.notes.ui; + +// 导入各类Android框架类和自定义类 +import android.annotation.SuppressLint; +import android.app.Activity; // Activity基类 +import android.app.AlarmManager; // 闹钟管理服务,用于提醒功能 +import android.app.AlertDialog; // 警告对话框 +import android.app.PendingIntent; // 延时意图 +import android.app.SearchManager; // 搜索服务管理 +import android.appwidget.AppWidgetManager; // 小部件管理 +import android.content.ContentResolver; +import android.content.ContentUris; // 内容URI工具 +import android.content.ContentValues; +import android.content.Context; // 应用上下文 +// 对话框接口 +import android.content.Intent; // 意图相关 +import android.content.SharedPreferences; // 共享首选项 +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Paint; // 绘图相关 +import android.net.Uri; +import android.os.Bundle; // 保存Activity状态 +import android.preference.PreferenceManager; // 首选项管理 +import android.provider.DocumentsContract; +import android.provider.MediaStore; +import android.text.Editable; +import android.text.Spannable; // 可样式化文本 +import android.text.SpannableString; // 可样式化字符串 +import android.text.TextUtils; // 文本工具类 +import android.text.format.DateUtils; // 日期时间格式化 +import android.text.style.BackgroundColorSpan; // 文本背景色样式 +import android.text.style.ImageSpan; +import android.util.Log; // 日志工具 +import android.view.LayoutInflater; // 布局加载器 +import android.view.Menu; // 菜单相关 +import android.view.MenuItem; // 菜单项 +import android.view.MotionEvent; // 触摸事件 +import android.view.View; // 视图基类 +import android.view.View.OnClickListener; // 点击监听器 +import android.view.WindowManager; // 窗口管理 +import android.widget.CheckBox; // 复选框控件 +// 复合按钮 +// 复选框状态监听 +import android.widget.EditText; // 编辑文本框 +import android.widget.ImageButton; +import android.widget.ImageView; // 图片视图 +import android.widget.LinearLayout; // 线性布局 +import android.widget.TextView; // 文本视图 +import android.widget.Toast; // 提示消息 + +import androidx.annotation.NonNull; // AndroidX非空注解 + +// 导入项目自定义类和资源 +import net.micode.notes.R; // 项目资源文件 +import net.micode.notes.data.Notes; // 笔记数据相关 +import net.micode.notes.data.Notes.TextNote; // 文本笔记数据 +import net.micode.notes.model.WorkingNote; // 工作笔记模型 +import net.micode.notes.model.WorkingNote.NoteSettingChangedListener; // 笔记设置变更监听 +import net.micode.notes.tool.DataUtils; // 数据工具类 +import net.micode.notes.tool.ResourceParser; // 资源解析器 +import net.micode.notes.tool.ResourceParser.TextAppearanceResources; // 文本外观资源 +// 日期时间选择监听 +import net.micode.notes.ui.NoteEditText.OnTextViewChangeListener; // 文本变更监听 +import net.micode.notes.widget.NoteWidgetProvider_2x; // 2x尺寸小部件 +import net.micode.notes.widget.NoteWidgetProvider_4x; // 4x尺寸小部件 + +// 导入Java工具类 +import java.io.FileNotFoundException; +import java.util.HashMap; // 哈希映射 +import java.util.HashSet; // 哈希集合 +import java.util.Map; // 映射接口 +import java.util.Objects; // 对象工具 +import java.util.regex.Matcher; // 正则匹配器 +import java.util.regex.Pattern; // 正则表达式 + +// 笔记编辑Activity,继承Activity并实现多个接口 +public class NoteEditActivity extends Activity implements OnClickListener, + NoteSettingChangedListener, OnTextViewChangeListener { + // 头部视图Holder静态内部类 + private static class HeadViewHolder { + public TextView tvModified; // 修改时间显示文本 + public ImageView ivAlertIcon; // 提醒图标 + public TextView tvAlertDate; // 提醒日期文本 + public ImageView ibSetBgColor; // 设置背景色按钮 + } + + // 背景颜色选择按钮映射表(按钮ID->颜色值) + private static final Map sBgSelectorBtnsMap = new HashMap<>(); + static { // 静态初始化块 + sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW); + sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED); + sBgSelectorBtnsMap.put(R.id.iv_bg_blue, ResourceParser.BLUE); + sBgSelectorBtnsMap.put(R.id.iv_bg_green, ResourceParser.GREEN); + sBgSelectorBtnsMap.put(R.id.iv_bg_white, ResourceParser.WHITE); + } + + // 背景颜色选择状态映射表(颜色值->选中状态视图ID) + private static final Map sBgSelectorSelectionMap = new HashMap<>(); + static { + sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select); + sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select); + sBgSelectorSelectionMap.put(ResourceParser.BLUE, R.id.iv_bg_blue_select); + sBgSelectorSelectionMap.put(ResourceParser.GREEN, R.id.iv_bg_green_select); + sBgSelectorSelectionMap.put(ResourceParser.WHITE, R.id.iv_bg_white_select); + } + + // 字体大小选择按钮映射表(布局ID->字体大小值) + private static final Map sFontSizeBtnsMap = new HashMap<>(); + static { + sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE); + sFontSizeBtnsMap.put(R.id.ll_font_small, ResourceParser.TEXT_SMALL); + sFontSizeBtnsMap.put(R.id.ll_font_normal, ResourceParser.TEXT_MEDIUM); + sFontSizeBtnsMap.put(R.id.ll_font_super, ResourceParser.TEXT_SUPER); + } + + // 字体大小选择状态映射表(字体大小值->选中状态视图ID) + private static final Map sFontSelectorSelectionMap = new HashMap<>(); + static { + sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select); + sFontSelectorSelectionMap.put(ResourceParser.TEXT_SMALL, R.id.iv_small_select); + sFontSelectorSelectionMap.put(ResourceParser.TEXT_MEDIUM, R.id.iv_medium_select); + sFontSelectorSelectionMap.put(ResourceParser.TEXT_SUPER, R.id.iv_super_select); + } + + private static final String TAG = "NoteEditActivity"; // 日志标签 + + // 视图成员变量 + private HeadViewHolder mNoteHeaderHolder; // 头部视图Holder实例 + private View mHeadViewPanel; // 头部面板视图 + private View mNoteBgColorSelector; // 背景颜色选择器 + private View mFontSizeSelector; // 字体大小选择器 + private EditText mNoteEditor; // 笔记内容编辑器 + private View mNoteEditorPanel; // 编辑器面板 + private WorkingNote mWorkingNote; // 当前工作笔记对象 + + // 偏好设置相关 + private SharedPreferences mSharedPrefs; // 共享偏好设置 + private int mFontSizeId; // 当前字体大小ID + private static final String PREFERENCE_FONT_SIZE = "pref_font_size"; // 字体大小偏好键名 + + // 快捷方式相关常量 + private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10; // 快捷方式标题最大长度 + + // 复选框标记常量 + public static final String TAG_CHECKED = String.valueOf('√'); // 选中标记 + public static final String TAG_UNCHECKED = String.valueOf('□'); // 未选中标记 + + // 其他成员变量 + private LinearLayout mEditTextList; // 编辑文本列表布局 + private String mUserQuery; // 用户搜索查询词 + private static final int PHOTO_REQUEST = 1; // 图片请求码 + + private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { + String column = "_data"; + String[] projection = { column }; + try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) { + if (cursor != null && cursor.moveToFirst()) { + int column_index = cursor.getColumnIndexOrThrow(column); + return cursor.getString(column_index); + } + } + return null; + } + + private static boolean isMediaDocument(Uri uri) { + return "com.android.providers.media.documents".equals(uri.getAuthority()); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + this.setContentView(R.layout.note_edit); // 设置布局 + + // 初始化Activity状态,如果没有有效状态则结束Activity + if (savedInstanceState == null && !initActivityState(getIntent())) { + finish(); + return; + } + initResources(); // 初始化资源 + //根据id获取添加图片按钮 + final ImageButton add_img_btn = findViewById(R.id.add_img_btn); + //为点击图片按钮设置监听器 + add_img_btn.setOnClickListener(view -> { + Log.d(TAG, "onClick: click add image button"); + //ACTION_GET_CONTENT: 允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音) + Intent loadImage = new Intent(Intent.ACTION_GET_CONTENT); + //Category属性用于指定当前动作(Action)被执行的环境. + //CATEGORY_OPENABLE; 用来指示一个ACTION_GET_CONTENT的intent + loadImage.addCategory(Intent.CATEGORY_OPENABLE); + loadImage.setType("image/*"); + startActivityForResult(loadImage, PHOTO_REQUEST); + }); + } + + /** + * 当内存不足时Activity可能被杀死,恢复时需要重建状态 + */ + @Override + protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + // 从保存状态恢复笔记ID + if (savedInstanceState.containsKey(Intent.EXTRA_UID)) { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.putExtra(Intent.EXTRA_UID, savedInstanceState.getLong(Intent.EXTRA_UID)); + if (!initActivityState(intent)) { + finish(); + return; + } + Log.d(TAG, "Restoring from killed activity"); + } + } + + /** + * 初始化Activity状态 + * @param intent 传入的Intent + * @return 是否初始化成功 + */ + private boolean initActivityState(Intent intent) { + mWorkingNote = null; + // 处理查看笔记的Intent + if (TextUtils.equals(Intent.ACTION_VIEW, intent.getAction())) { + long noteId = intent.getLongExtra(Intent.EXTRA_UID, 0); + mUserQuery = ""; + + // 处理来自搜索的结果 + if (intent.hasExtra(SearchManager.EXTRA_DATA_KEY)) { + noteId = Long.parseLong(Objects.requireNonNull(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY))); + mUserQuery = intent.getStringExtra(SearchManager.USER_QUERY); + } + + // 检查笔记是否存在 + if (!DataUtils.visibleInNoteDatabase(getContentResolver(), noteId, Notes.TYPE_NOTE)) { + Intent jump = new Intent(this, NotesListActivity.class); + startActivity(jump); + showToast(R.string.error_note_not_exist); + finish(); + return false; + } else { + // 加载笔记 + mWorkingNote = WorkingNote.load(this, noteId); + } + // 设置软键盘模式 + getWindow().setSoftInputMode( + WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN + | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); + } + // 处理新建或编辑笔记的Intent + else if(TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) { + // 获取参数 + long folderId = intent.getLongExtra(Notes.INTENT_EXTRA_FOLDER_ID, 0); + int widgetId = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_ID, + AppWidgetManager.INVALID_APPWIDGET_ID); + int widgetType = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, + Notes.TYPE_WIDGET_INVALIDE); + int bgResId = intent.getIntExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, + ResourceParser.getDefaultBgId(this)); + + // 处理通话记录笔记 + String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); + long callDate = intent.getLongExtra(Notes.INTENT_EXTRA_CALL_DATE, 0); + if (callDate != 0 && phoneNumber != null) { + if (TextUtils.isEmpty(phoneNumber)) { + Log.w(TAG, "The call record number is null"); + } + long noteId; + // 查找是否已存在对应通话记录 + if ((noteId = DataUtils.getNoteIdByPhoneNumberAndCallDate(getContentResolver(), + phoneNumber, callDate)) > 0) { + mWorkingNote = WorkingNote.load(this, noteId); + } else { + // 新建通话记录笔记 + mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, + widgetType, bgResId); + mWorkingNote.convertToCallNote(phoneNumber, callDate); + } + } else { + // 创建普通空笔记 + mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, widgetType, + bgResId); + } + + // 设置软键盘模式 + getWindow().setSoftInputMode( + WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE + | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); + } else { + Log.e(TAG, "Intent not specified action, should not support"); + finish(); + return false; + } + // 设置笔记变更监听器 + mWorkingNote.setOnSettingStatusChangedListener(this); + return true; + } + + @Override + protected void onResume() { + super.onResume(); + initNoteScreen(); // 初始化笔记显示 + } + + /** + * 初始化笔记显示界面 + */ + private void initNoteScreen() { + // 设置编辑器字体大小 + mNoteEditor.setTextAppearance(this, TextAppearanceResources + .getTexAppearanceResource(mFontSizeId)); + + // 根据笔记模式设置内容 + if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) { + switchToListMode(mWorkingNote.getContent()); // 列表模式 + } else { + // 普通模式,高亮显示搜索词 + mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery)); + mNoteEditor.setSelection(mNoteEditor.getText().length()); // 光标移至末尾 + } + + // 初始化背景选择器状态 + for (Integer id : sBgSelectorSelectionMap.keySet()) { + findViewById(sBgSelectorSelectionMap.get(id)).setVisibility(View.GONE); + } + + // 设置背景和标题样式 + mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId()); + mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId()); + + // 设置修改时间显示 + mNoteHeaderHolder.tvModified.setText(DateUtils.formatDateTime(this, + mWorkingNote.getModifiedDate(), DateUtils.FORMAT_SHOW_DATE + | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME + | DateUtils.FORMAT_SHOW_YEAR)); + + // 显示提醒头部(如果有设置提醒) + showAlertHeader(); + } + + /** + * 显示提醒头部信息 + */ + private void showAlertHeader() { + if (mWorkingNote.hasClockAlert()) { + long time = System.currentTimeMillis(); + // 判断提醒是否过期 + if (time > mWorkingNote.getAlertDate()) { + mNoteHeaderHolder.tvAlertDate.setText(R.string.note_alert_expired); + } else { + // 显示相对时间 + mNoteHeaderHolder.tvAlertDate.setText(DateUtils.getRelativeTimeSpanString( + mWorkingNote.getAlertDate(), time, DateUtils.MINUTE_IN_MILLIS)); + } + mNoteHeaderHolder.tvAlertDate.setVisibility(View.VISIBLE); + mNoteHeaderHolder.ivAlertIcon.setVisibility(View.VISIBLE); + } else { + // 没有提醒时隐藏相关视图 + mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE); + mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE); + } + } + + // 处理新的Intent到来(例如从最近任务重新打开) + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); // 调用父类方法 + initActivityState(intent); // 用新intent重新初始化Activity状态 + } + + // 保存Activity状态(例如屏幕旋转时) + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); // 调用父类方法 + + /* + 对于尚未保存的新笔记,需要先保存以生成ID + 如果笔记不值得保存,则不会有ID等同于创建新笔记 + */ + if (!mWorkingNote.existInDatabase()) { + saveNote(); // 保存笔记到数据库 + } + + // 将当前笔记ID保存到Bundle中 + outState.putLong(Intent.EXTRA_UID, mWorkingNote.getNoteId()); + Log.d(TAG, "Save working note id: " + mWorkingNote.getNoteId() + " onSaveInstanceState"); + } + + // 处理触摸事件分发 + @Override + public boolean dispatchTouchEvent(MotionEvent ev) { + // 如果背景颜色选择器可见且触摸在区域外,则隐藏它 + if (mNoteBgColorSelector.getVisibility() == View.VISIBLE + && !inRangeOfView(mNoteBgColorSelector, ev)) { + mNoteBgColorSelector.setVisibility(View.GONE); + return true; // 事件已消费 + } + + // 如果字体大小选择器可见且触摸在区域外,则隐藏它 + if (mFontSizeSelector.getVisibility() == View.VISIBLE + && !inRangeOfView(mFontSizeSelector, ev)) { + mFontSizeSelector.setVisibility(View.GONE); + return true; // 事件已消费 + } + + return super.dispatchTouchEvent(ev); // 其他情况交由父类处理 + } + + // 检查触摸事件是否在视图范围内 + private boolean inRangeOfView(View view, MotionEvent ev) { + int []location = new int[2]; // 存储视图坐标的数组 + view.getLocationOnScreen(location); // 获取视图在屏幕上的坐标 + int x = location[0]; // 视图左上角x坐标 + int y = location[1]; // 视图左上角y坐标 + + // 判断触摸点是否在视图范围外 + return !(ev.getX() < x) + && !(ev.getX() > (x + view.getWidth())) + && !(ev.getY() < y) + && !(ev.getY() > (y + view.getHeight())); // 不在范围内 +// 在范围内 + } + + // 初始化视图资源和数据 + private void initResources() { + // 初始化头部视图 + mHeadViewPanel = findViewById(R.id.note_title); + mNoteHeaderHolder = new HeadViewHolder(); + + // 绑定头部视图控件 + mNoteHeaderHolder.tvModified = findViewById(R.id.tv_modified_date); + mNoteHeaderHolder.ivAlertIcon = findViewById(R.id.iv_alert_icon); + mNoteHeaderHolder.tvAlertDate = findViewById(R.id.tv_alert_date); + mNoteHeaderHolder.ibSetBgColor = findViewById(R.id.btn_set_bg_color); + mNoteHeaderHolder.ibSetBgColor.setOnClickListener(this); // 设置背景颜色按钮点击监听 + + // 初始化编辑器相关视图 + mNoteEditor = findViewById(R.id.note_edit_view); + mNoteEditorPanel = findViewById(R.id.sv_note_edit); + + // 初始化背景颜色选择器 + mNoteBgColorSelector = findViewById(R.id.note_bg_color_selector); + // 为所有背景颜色按钮设置点击监听 + for (int id : sBgSelectorBtnsMap.keySet()) { + ImageView iv = findViewById(id); + iv.setOnClickListener(this); + } + + // 初始化字体大小选择器 + mFontSizeSelector = findViewById(R.id.font_size_selector); + // 为所有字体大小选项设置点击监听 + for (int id : sFontSizeBtnsMap.keySet()) { + View view = findViewById(id); + view.setOnClickListener(this); + }; + + // 初始化偏好设置 + mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + // 获取保存的字体大小设置,默认为默认字体大小 + mFontSizeId = mSharedPrefs.getInt(PREFERENCE_FONT_SIZE, ResourceParser.BG_DEFAULT_FONT_SIZE); + + /* + HACKME: 修复在shared preference中存储资源id的bug。 + id可能大于资源数量,这种情况返回默认字体大小 + */ + if(mFontSizeId >= TextAppearanceResources.getResourcesSize()) { + mFontSizeId = ResourceParser.BG_DEFAULT_FONT_SIZE; + } + + // 初始化编辑文本列表布局 + mEditTextList = findViewById(R.id.note_edit_list); + } + + // Activity暂停时调用 + @Override + protected void onPause() { + super.onPause(); + // 保存笔记并记录日志 + if(saveNote()) { + Log.d(TAG, "Note data was saved with length:" + mWorkingNote.getContent().length()); + } + clearSettingState(); // 清除设置状态(如隐藏选择器) + } + + // 更新关联的小部件 + private void updateWidget() { + Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); + + // 根据小部件类型设置不同的Provider + if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_2X) { + intent.setClass(this, NoteWidgetProvider_2x.class); + } else if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_4X) { + intent.setClass(this, NoteWidgetProvider_4x.class); + } else { + Log.e(TAG, "Unspported widget type"); + return; + } + + // 设置要更新的小部件ID + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { + mWorkingNote.getWidgetId() + }); + + sendBroadcast(intent); // 发送广播更新小部件 + setResult(RESULT_OK, intent); // 设置结果 + } + + // 点击事件处理 + public void onClick(View v) { + int id = v.getId(); + + // 背景颜色设置按钮点击 + if (id == R.id.btn_set_bg_color) { + mNoteBgColorSelector.setVisibility(View.VISIBLE); // 显示背景选择器 + // 显示当前选中背景的标记 + findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(View.VISIBLE); + } + // 背景颜色选项点击 + else if (sBgSelectorBtnsMap.containsKey(id)) { + // 隐藏之前选中的背景标记 + findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(View.GONE); + // 设置新背景颜色 + mWorkingNote.setBgColorId(sBgSelectorBtnsMap.get(id)); + mNoteBgColorSelector.setVisibility(View.GONE); // 隐藏背景选择器 + } + // 字体大小选项点击 + else if (sFontSizeBtnsMap.containsKey(id)) { + // 隐藏之前选中的字体大小标记 + findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.GONE); + // 设置新字体大小 + mFontSizeId = sFontSizeBtnsMap.get(id); + // 保存到偏好设置 + mSharedPrefs.edit().putInt(PREFERENCE_FONT_SIZE, mFontSizeId).apply(); + // 显示新选中的字体大小标记 + findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.VISIBLE); + + // 根据当前模式应用字体设置 + if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) { + getWorkingText(); + switchToListMode(mWorkingNote.getContent()); + } else { + // 设置编辑器文本外观 + mNoteEditor.setTextAppearance(this, + TextAppearanceResources.getTexAppearanceResource(mFontSizeId)); + } + mFontSizeSelector.setVisibility(View.GONE); // 隐藏字体大小选择器 + } + } + + // 返回键处理 + @Override + public void onBackPressed() { + if(clearSettingState()) { // 如果有设置面板显示,先关闭它们 + return; + } + + saveNote(); // 保存笔记 + super.onBackPressed(); // 执行默认返回操作 + } + + // 清除设置状态(隐藏所有选择器) + private boolean clearSettingState() { + if (mNoteBgColorSelector.getVisibility() == View.VISIBLE) { + mNoteBgColorSelector.setVisibility(View.GONE); + return true; + } else if (mFontSizeSelector.getVisibility() == View.VISIBLE) { + mFontSizeSelector.setVisibility(View.GONE); + return true; + } + return false; + } + + // 背景颜色变化回调 + public void onBackgroundColorChanged() { + // 更新背景选择标记 + findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(View.VISIBLE); + // 应用新背景颜色 + mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId()); + mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId()); + } + + // 准备选项菜单 + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + if (isFinishing()) { // 如果Activity正在结束,不处理菜单 + return true; + } + + clearSettingState(); // 清除设置状态 + menu.clear(); // 清除原有菜单项 + + // 根据笔记类型加载不同菜单 + if (mWorkingNote.getFolderId() == Notes.ID_CALL_RECORD_FOLDER) { + getMenuInflater().inflate(R.menu.call_note_edit, menu); + } else { + getMenuInflater().inflate(R.menu.note_edit, menu); + } + + // 设置列表模式/普通模式菜单项标题 + if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) { + menu.findItem(R.id.menu_list_mode).setTitle(R.string.menu_normal_mode); + } else { + menu.findItem(R.id.menu_list_mode).setTitle(R.string.menu_list_mode); + } + + // 根据是否有提醒设置显示不同的菜单项 + if (mWorkingNote.hasClockAlert()) { + menu.findItem(R.id.menu_alert).setVisible(false); // 隐藏设置提醒 + menu.findItem(R.id.menu_delete_remind).setVisible(true); // 显示删除提醒 + } else { + menu.findItem(R.id.menu_alert).setVisible(true); // 显示设置提醒 + menu.findItem(R.id.menu_delete_remind).setVisible(false); // 隐藏删除提醒 + } + return true; + } + + // 菜单项选择处理 + @SuppressLint("NonConstantResourceId") + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_new_note: // 新建笔记 + createNewNote(); + break; + case R.id.menu_delete: // 删除笔记 + showDeleteNoteDialog(); + break; + case R.id.menu_font_size: // 字体大小设置 + mFontSizeSelector.setVisibility(View.VISIBLE); + findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.VISIBLE); + break; + case R.id.menu_list_mode: // 切换列表模式/普通模式 + toggleListMode(); + break; + case R.id.menu_share: // 分享笔记内容 + shareNoteContent(); + break; + case R.id.menu_send_to_desktop: // 发送到桌面 + sendToDesktop(); + break; + case R.id.menu_alert: // 设置提醒 + setReminder(); + break; + case R.id.menu_delete_remind: // 删除提醒 + mWorkingNote.setAlertDate(0, false); + break; + default: + break; + } + return true; + } + + // 显示删除笔记确认对话框 + private void showDeleteNoteDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getString(R.string.alert_title_delete)); + builder.setIcon(android.R.drawable.ic_dialog_alert); + builder.setMessage(getString(R.string.alert_message_delete_note)); + builder.setPositiveButton(android.R.string.ok, + (dialog, which) -> { + deleteCurrentNote(); // 删除当前笔记 + finish(); // 结束Activity + }); + builder.setNegativeButton(android.R.string.cancel, null); + builder.show(); + } + + // 切换列表模式/普通模式 + private void toggleListMode() { + int newMode = mWorkingNote.getCheckListMode() == 0 ? TextNote.MODE_CHECK_LIST : 0; + mWorkingNote.setCheckListMode(newMode); + } + + // 分享笔记内容 + private void shareNoteContent() { + getWorkingText(); + sendTo(this, mWorkingNote.getContent()); + } + + // 设置笔记提醒时间 + private void setReminder() { + // 创建日期时间选择对话框,传入当前时间作为初始值 + DateTimePickerDialog d = new DateTimePickerDialog(this, System.currentTimeMillis()); + + // 设置日期时间选择监听器 + d.setOnDateTimeSetListener((dialog, date) -> { + // 设置笔记的提醒时间 + mWorkingNote.setAlertDate(date, true); + }); + d.show(); // 显示对话框 + } + + /** + * 分享笔记内容到支持分享功能的应用 + * @param context 上下文对象 + * @param info 要分享的文本内容 + */ + private void sendTo(Context context, String info) { + // 创建分享意图 + Intent intent = new Intent(Intent.ACTION_SEND); + // 添加要分享的文本内容 + intent.putExtra(Intent.EXTRA_TEXT, info); + // 设置分享类型为纯文本 + intent.setType("text/plain"); + // 启动分享活动 + context.startActivity(intent); + } + + // 创建新笔记 + private void createNewNote() { + // 首先保存当前正在编辑的笔记 + saveNote(); + + // 结束当前Activity,确保安全性 + finish(); + + // 创建启动新笔记编辑Activity的意图 + Intent intent = new Intent(this, NoteEditActivity.class); + // 设置操作为插入或编辑 + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); + // 传递当前文件夹ID + intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mWorkingNote.getFolderId()); + // 启动新的笔记编辑Activity + startActivity(intent); + } + + // 删除当前笔记 + private void deleteCurrentNote() { + // 检查笔记是否存在于数据库中 + if (mWorkingNote.existInDatabase()) { + // 创建笔记ID集合 + HashSet ids = new HashSet<>(); + long id = mWorkingNote.getNoteId(); + + // 检查笔记ID是否有效 + if (id != Notes.ID_ROOT_FOLDER) { + ids.add(id); // 添加要删除的笔记ID + } else { + Log.d(TAG, "Wrong note id, should not happen"); + } + + // 根据同步模式选择删除方式 + if (!isSyncMode()) { + // 直接批量删除笔记 + if (!DataUtils.batchDeleteNotes(getContentResolver(), ids)) { + Log.e(TAG, "Delete Note error"); + } + } else { + // 同步模式下将笔记移动到回收站 + if (!DataUtils.batchMoveToFolder(getContentResolver(), ids, Notes.ID_TRASH_FOLER)) { + Log.e(TAG, "Move notes to trash folder error, should not happens"); + } + } + } + // 标记笔记为已删除状态 + mWorkingNote.markDeleted(true); + } + + // 检查是否处于同步模式 + private boolean isSyncMode() { + // 通过偏好设置获取同步账号名称,长度大于0表示处于同步模式 + return !NotesPreferenceActivity.getSyncAccountName(this).trim().isEmpty(); + } + + // 当提醒时间发生变化时调用 + public void onClockAlertChanged(long date, boolean set) { + /* + 用户可以为未保存的笔记设置提醒,因此在设置提醒前需要先保存笔记 + */ + if (!mWorkingNote.existInDatabase()) { + saveNote(); // 保存笔记到数据库 + } + + // 检查笔记ID是否有效 + if (mWorkingNote.getNoteId() > 0) { + // 创建闹钟接收器意图 + Intent intent = new Intent(this, AlarmReceiver.class); + // 设置笔记URI + intent.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mWorkingNote.getNoteId())); + // 创建待定意图 + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); + // 获取闹钟管理器服务 + AlarmManager alarmManager = ((AlarmManager) getSystemService(ALARM_SERVICE)); + + // 更新提醒头部显示 + showAlertHeader(); + + // 根据设置状态取消或设置闹钟 + if(!set) { + alarmManager.cancel(pendingIntent); // 取消闹钟 + } else { + alarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent); // 设置闹钟 + } + } else { + /* + 用户未输入任何内容(笔记不值得保存),我们没有笔记ID, + 提醒用户需要输入内容 + */ + Log.e(TAG, "Clock alert setting error"); + showToast(R.string.error_note_empty_for_clock); + } + } + + // 当关联的小部件发生变化时调用 + public void onWidgetChanged() { + updateWidget(); // 更新小部件显示 + } + + // 当编辑文本被删除时调用 + public void onEditTextDelete(int index, String text) { + int childCount = mEditTextList.getChildCount(); + if (childCount == 1) { + return; // 只有一个子项时不处理 + } + + // 更新后续子项的索引 + for (int i = index + 1; i < childCount; i++) { + ((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text)) + .setIndex(i - 1); + } + + // 移除指定位置的视图 + mEditTextList.removeViewAt(index); + + // 获取要处理焦点的编辑框 + NoteEditText edit; + if(index == 0) { + edit = mEditTextList.getChildAt(0).findViewById(R.id.et_edit_text); + } else { + edit = mEditTextList.getChildAt(index - 1).findViewById(R.id.et_edit_text); + } + + // 处理被删除的文本 + int length = edit.length(); + edit.append(text); // 追加文本 + edit.requestFocus(); // 获取焦点 + edit.setSelection(length); // 设置光标位置 + } + + // 当在编辑文本中按下回车时调用 + public void onEditTextEnter(int index, String text) { + /* + 不应发生的情况,用于调试检查 + */ + if(index > mEditTextList.getChildCount()) { + Log.e(TAG, "Index out of mEditTextList boundrary, should not happen"); + } + + // 创建新列表项并添加到指定位置 + View view = getListItem(text, index); + mEditTextList.addView(view, index); + + // 设置新添加的编辑框获取焦点 + NoteEditText edit = view.findViewById(R.id.et_edit_text); + edit.requestFocus(); + edit.setSelection(0); + + // 更新后续子项的索引 + for (int i = index + 1; i < mEditTextList.getChildCount(); i++) { + ((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text)) + .setIndex(i); + } + } + + // 切换到列表模式 + private void switchToListMode(String text) { + // 清除现有所有子视图 + mEditTextList.removeAllViews(); + + // 按换行符拆分文本 + String[] items = text.split("\n"); + int index = 0; + + // 为每个非空行创建列表项 + for (String item : items) { + if(!TextUtils.isEmpty(item)) { + mEditTextList.addView(getListItem(item, index)); + index++; + } + } + + // 添加一个空列表项用于输入新内容 + mEditTextList.addView(getListItem("", index)); + mEditTextList.getChildAt(index).findViewById(R.id.et_edit_text).requestFocus(); + + // 切换视图显示状态 + mNoteEditor.setVisibility(View.GONE); + mEditTextList.setVisibility(View.VISIBLE); + } + + // 获取高亮显示查询结果的文本 + private Spannable getHighlightQueryResult(String fullText, String userQuery) { + // 创建可样式化的字符串 + SpannableString spannable = new SpannableString(fullText == null ? "" : fullText); + + // 如果用户查询词不为空,则处理高亮 + if (!TextUtils.isEmpty(userQuery)) { + // 创建匹配用户查询词的正则表达式 + var mPattern = Pattern.compile(userQuery); + Matcher m = null; + if (fullText != null) { + m = mPattern.matcher(fullText); + } + int start = 0; + + // 查找所有匹配项并设置背景色高亮 + if (m != null) { + while (m.find(start)) { + spannable.setSpan( + new BackgroundColorSpan(this.getResources().getColor( + R.color.user_query_highlight)), m.start(), m.end(), + Spannable.SPAN_INCLUSIVE_EXCLUSIVE); + start = m.end(); + } + } + } + return spannable; + } + + // 获取列表项视图 + private View getListItem(String item, int index) { + // 加载列表项布局 + View view = LayoutInflater.from(this).inflate(R.layout.note_edit_list_item, null); + final NoteEditText edit = view.findViewById(R.id.et_edit_text); + + // 设置编辑框文本外观 + edit.setTextAppearance(this, TextAppearanceResources.getTexAppearanceResource(mFontSizeId)); + + // 设置复选框状态变化监听器 + CheckBox cb = view.findViewById(R.id.cb_edit_item); + cb.setOnCheckedChangeListener((buttonView, isChecked) -> { + // 根据复选框状态设置文本删除线 + if (isChecked) { + edit.setPaintFlags(edit.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); + } else { + edit.setPaintFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); + } + }); + + // 处理复选框初始状态 + if (item.startsWith(TAG_CHECKED)) { + cb.setChecked(true); // 设置为选中状态 + edit.setPaintFlags(edit.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); // 添加删除线 + item = item.substring(TAG_CHECKED.length()).trim(); // 去除标记前缀 + } else if (item.startsWith(TAG_UNCHECKED)) { + cb.setChecked(false); // 设置为未选中状态 + edit.setPaintFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); // 清除删除线 + item = item.substring(TAG_UNCHECKED.length()).trim(); // 去除标记前缀 + } + + // 设置编辑框变化监听器和索引 + edit.setOnTextViewChangeListener(this); + edit.setIndex(index); + // 设置编辑框文本(处理高亮查询结果) + edit.setText(getHighlightQueryResult(item, mUserQuery)); + return view; + } + + // 当文本内容发生变化时调用 + public void onTextChange(int index, boolean hasText) { + // 检查索引是否有效 + if (index >= mEditTextList.getChildCount()) { + Log.e(TAG, "Wrong index, should not happen"); + return; + } + + // 根据是否有文本设置复选框可见性 + if(hasText) { + mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.VISIBLE); + } else { + mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.GONE); + } + } + + // 当笔记的列表模式发生变化时调用 + public void onCheckListModeChanged(int oldMode, int newMode) { + // 切换到列表模式 + if (newMode == TextNote.MODE_CHECK_LIST) { + switchToListMode(mNoteEditor.getText().toString()); + } else { + // 切换回普通模式 + if (!getWorkingText()) { + // 去除未选中标记 + mWorkingNote.setWorkingText(mWorkingNote.getContent().replace(TAG_UNCHECKED + " ", "")); + } + // 设置编辑器文本(处理高亮查询结果) + mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery)); + // 切换视图显示状态 + mEditTextList.setVisibility(View.GONE); + mNoteEditor.setVisibility(View.VISIBLE); + } + } + + // 获取当前工作文本 + private boolean getWorkingText() { + boolean hasChecked = false; + // 如果是列表模式,处理所有列表项 + if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) { + StringBuilder sb = new StringBuilder(); + // 遍历所有列表项 + for (int i = 0; i < mEditTextList.getChildCount(); i++) { + View view = mEditTextList.getChildAt(i); + NoteEditText edit = view.findViewById(R.id.et_edit_text); + // 如果编辑框有文本内容 + if (!TextUtils.isEmpty(edit.getText())) { + // 根据复选框状态添加相应标记 + if (((CheckBox) view.findViewById(R.id.cb_edit_item)).isChecked()) { + sb.append(TAG_CHECKED).append(" ").append(edit.getText()).append("\n"); + hasChecked = true; + } else { + sb.append(TAG_UNCHECKED).append(" ").append(edit.getText()).append("\n"); + } + } + } + // 设置工作文本 + mWorkingNote.setWorkingText(sb.toString()); + } else { + // 普通模式直接获取编辑框文本 + mWorkingNote.setWorkingText(mNoteEditor.getText().toString()); + } + return hasChecked; // 返回是否有选中的项 + } + + // 保存笔记 + private boolean saveNote() { + getWorkingText(); // 获取当前工作文本 + boolean saved = mWorkingNote.saveNote(); // 保存笔记 + if (saved) { + /* + 从列表视图到编辑视图有两种模式:打开一个笔记,创建/编辑一个笔记。 + 打开笔记需要返回时保持原来的列表位置,而创建新笔记需要返回列表顶部。 + 这里的RESULT_OK用于标识创建/编辑状态 + */ + setResult(RESULT_OK); // 设置结果码 + } + return saved; // 返回保存结果 + } + + // 发送笔记到桌面(创建快捷方式) + private void sendToDesktop() { + /* + 在发送消息到桌面之前,应确保当前编辑的笔记已存在于数据库中。 + 因此,对于新笔记,首先需要保存它 + */ + if (!mWorkingNote.existInDatabase()) { + saveNote(); // 保存笔记到数据库 + } + + // 检查笔记ID是否有效 + if (mWorkingNote.getNoteId() > 0) { + Intent sender = new Intent(); + // 创建快捷方式点击时启动的意图 + Intent shortcutIntent = new Intent(this, NoteEditActivity.class); + shortcutIntent.setAction(Intent.ACTION_VIEW); + shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId()); + + // 设置快捷方式参数 + sender.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); + // 设置快捷方式名称(使用笔记内容前10个字符) + sender.putExtra(Intent.EXTRA_SHORTCUT_NAME, + makeShortcutIconTitle(mWorkingNote.getContent())); + // 设置快捷方式图标 + sender.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, + Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_app)); + sender.putExtra("duplicate", true); // 允许重复创建 + sender.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); // 设置安装快捷方式动作 + + showToast(R.string.info_note_enter_desktop); // 显示提示消息 + sendBroadcast(sender); // 发送广播创建快捷方式 + } else { + /* + 用户未输入任何内容(笔记不值得保存),我们没有笔记ID, + 提醒用户需要输入内容 + */ + Log.e(TAG, "Send to desktop error"); + showToast(R.string.error_note_empty_for_send_to_desktop); + } + } + + // 生成快捷方式标题 + private String makeShortcutIconTitle(String content) { + // 去除复选框标记 + content = content.replace(TAG_CHECKED, ""); + content = content.replace(TAG_UNCHECKED, ""); + // 截取前10个字符作为标题 + return content.length() > SHORTCUT_ICON_TITLE_MAX_LEN ? content.substring(0, + SHORTCUT_ICON_TITLE_MAX_LEN) : content; + } + + // 显示短时Toast提示 + private void showToast(int resId) { + showToast(resId, Toast.LENGTH_SHORT); // 默认显示短时Toast + } + + // 显示Toast提示 + private void showToast(int resId, int duration) { + Toast.makeText(this, resId, duration).show(); // 创建并显示Toast + } + //路径字符串格式 转换为 图片image格式 + private void convertToImage() { + NoteEditText noteEditText = findViewById(R.id.note_edit_view); //获取当前的edit + Editable editable = noteEditText.getText();//1.获取text + String noteText = null; //2.将note内容转换为字符串 + if (editable != null) { + noteText = editable.toString(); + } + int length = 0; //内容的长度 + if (editable != null) { + length = editable.length(); + } + //3.截取img片段 [local]+uri+[local],提取uri + for(int i = 0; i < length; i++) { + for(int j = i; j < length; j++) { + String img_fragment = noteText.substring(i, j+1); //img_fragment:关于图片路径的片段 + if(img_fragment.length() > 15 && img_fragment.endsWith("[/local]") && img_fragment.startsWith("[local]")){ + int limit = 7; //[local]为7个字符 + //[local][/local]共15个字符,剩下的为真正的path长度 + int len = img_fragment.length()-15; + //从[local]之后的len个字符就是path + String path = img_fragment.substring(limit,limit+len);//获取到了图片路径 + Bitmap bitmap = null; + Log.d(TAG, "图片的路径是:"+path); + try { + bitmap = BitmapFactory.decodeFile(path);//将图片路径解码为图片格式 + } catch (Exception e) { + e.printStackTrace(); + } + if(bitmap!=null){ //若图片存在 + Log.d(TAG, "图片不为null"); + ImageSpan imageSpan = new ImageSpan(NoteEditActivity.this, bitmap); + //4.创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像 + String ss = "[local]" + path + "[/local]"; + SpannableString spannableString = new SpannableString(ss); + //5.将指定的标记对象附加到文本的开始...结束范围 + spannableString.setSpan(imageSpan, 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + Log.d(TAG, "Create spannable string success!"); + Editable edit_text = noteEditText.getEditableText(); + edit_text.delete(i,i+len+15); //6.删掉图片路径的文字 + edit_text.insert(i, spannableString); //7.在路径的起始位置插入图片 + } + } + } + } + } + //重写onActivityResult()来处理返回的数据 + protected void onActivityResult(int requestCode, int resultCode, Intent intent) { + super.onActivityResult(requestCode, resultCode, intent); + ContentResolver resolver = getContentResolver(); + if (requestCode == PHOTO_REQUEST) { + Uri originalUri = intent.getData(); //1.获得图片的真实路径 + Bitmap bitmap = null; + try { + if (originalUri != null) { + bitmap = BitmapFactory.decodeStream(resolver.openInputStream(originalUri));//2.解码图片 + } + } catch (FileNotFoundException e) { + Log.d(TAG, "onActivityResult: get file_exception"); + e.printStackTrace(); + } + + if (bitmap != null) { + //3.根据Bitmap对象创建ImageSpan对象 + Log.d(TAG, "onActivityResult: bitmap is not null"); + ImageSpan imageSpan = new ImageSpan(NoteEditActivity.this, bitmap); + String path = getPath(this, originalUri); + //4.使用[local][/local]将path括起来,用于之后方便识别图片路径在note中的位置 + String img_fragment = "[local]" + path + "[/local]"; + //创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像 + SpannableString spannableString = new SpannableString(img_fragment); + spannableString.setSpan(imageSpan, 0, img_fragment.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + //5.将选择的图片追加到EditText中光标所在位置 + NoteEditText e = findViewById(R.id.note_edit_view); + int index = e.getSelectionStart(); //获取光标所在位置 + Log.d(TAG, "Index是: " + index); + Editable edit_text = e.getEditableText(); + edit_text.insert(index, spannableString); //将图片插入到光标所在位置 + + mWorkingNote.mContent = Objects.requireNonNull(e.getText()).toString(); + //6.把改动提交到数据库中,两个数据库表都要改的 + ContentResolver contentResolver = getContentResolver(); + ContentValues contentValues = new ContentValues(); + final long id = mWorkingNote.getNoteId(); + contentValues.put("snippet", mWorkingNote.mContent); + contentResolver.update(Uri.parse("content://micode_notes/note"), contentValues, "_id=?", new String[]{"" + id}); + ContentValues contentValues1 = new ContentValues(); + contentValues1.put("content", mWorkingNote.mContent); + contentResolver.update(Uri.parse("content://micode_notes/data"), contentValues1, "mime_type=? and note_id=?", new String[]{"vnd.android.cursor.item/text_note", "" + id}); + + } else { + Toast.makeText(NoteEditActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show(); + } + } + } + //获取文件的real path + public String getPath(final Context context, final Uri uri) { + + final boolean isKitKat = true; + + // DocumentProvider + if (DocumentsContract.isDocumentUri(context, uri)) { + // ExternalStorageProvider +// if (isExternalStorageDocument(uri)) { +// final String docId = DocumentsContract.getDocumentId(uri); +// final String[] split = docId.split(":"); +// final String type = split[0]; +// +// if ("primary".equalsIgnoreCase(type)) { +// return Environment.getExternalStorageDirectory() + "/" + split[1]; +// } +// } +// // DownloadsProvider +// else if (isDownloadsDocument(uri)) { +// final String id = DocumentsContract.getDocumentId(uri); +// final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); +// return getDataColumn(context, contentUri, null, null); +// } + // MediaProvider +// else + if (isMediaDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + + Uri contentUri = null; + if ("image".equals(type)) { + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + } + + final String selection = "_id=?"; + final String[] selectionArgs = new String[]{split[1]}; + + return getDataColumn(context, contentUri, selection, selectionArgs); + } + } + // Media + else if ("content".equalsIgnoreCase(uri.getScheme())) { + return getDataColumn(context, uri, null, null); + } + // File + else if ("file".equalsIgnoreCase(uri.getScheme())) { + return uri.getPath(); + } + return null; + } + +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditText.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditText.java new file mode 100644 index 0000000..42c075d --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditText.java @@ -0,0 +1,256 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义当前类的包路径,指明该类属于net.micode.notes.ui 包 +package net.micode.notes.ui; + +/* 导入Android基础类 */ +// Context类提供应用环境信息访问接口 +import android.content.Context; +// Rect类表示一个矩形区域,用于处理视图边界 +import android.graphics.Rect; +// Layout类用于管理文本布局和测量 +import android.text.Layout; +// Selection类用于处理文本选择操作 +import android.text.Selection; +// Spanned接口表示带有样式的文本 +// TextUtils类提供文本处理工具方法 +import android.text.TextUtils; +// URLSpan类用于标记文本中的超链接 +import android.text.style.URLSpan; +// AttributeSet接口表示XML属性集合 +import android.util.AttributeSet; +// Log类提供日志记录功能 +import android.util.Log; + +/* 导入Android视图相关类 */ +// ContextMenu类表示上下文菜单 +import android.view.ContextMenu; +// KeyEvent类表示按键事件 +import android.view.KeyEvent; +// MenuItem类表示菜单项 +// OnMenuItemClickListener接口定义菜单项点击回调 +// MotionEvent类表示触摸事件 +import android.view.MotionEvent; +// EditText类是基础文本编辑框 + +/* 导入项目资源 */ +// 导入项目R类,用于访问资源ID +import net.micode.notes.R; + +/* 导入Java集合类 */ +// HashMap类实现哈希表结构的Map接口 +import java.util.HashMap; +// Map接口表示键值对映射集合 +import java.util.Map; +import java.util.Objects; + +// 继承自AppCompatEditText的支持兼容的编辑文本框 +public class NoteEditText extends androidx.appcompat.widget.AppCompatEditText { + private static final String TAG = "NoteEditText"; // 日志标签 + private int mIndex; // 当前编辑框在列表中的索引 + private int mSelectionStartBeforeDelete; // 删除操作前的选择开始位置 + + // 定义支持的URL协议常量 + private static final String SCHEME_TEL = "tel:"; + private static final String SCHEME_HTTP = "http:"; + private static final String SCHEME_EMAIL = "mailto:"; + + // 创建协议到资源ID的映射表 + private static final Map sSchemaActionResMap = new HashMap<>(); + static { + // 初始化协议对应的字符串资源 + sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel); // 电话协议 + sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web); // 网页协议 + sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email);// 邮件协议 + } + + /** + * 定义文本变化监听接口,由NoteEditActivity实现 + * 用于处理编辑文本框的删除和新增事件 + */ + public interface OnTextViewChangeListener { + /** + * 当删除键按下且文本为空时删除当前编辑框 + * @param index 当前编辑框索引 + * @param text 当前编辑框文本内容 + */ + void onEditTextDelete(int index, String text); + + /** + * 当回车键按下时在当前编辑框后添加新编辑框 + * @param index 新编辑框的索引 + * @param text 新编辑框的初始文本 + */ + void onEditTextEnter(int index, String text); + + /** + * 当文本变化时隐藏或显示选项 + * @param index 当前编辑框索引 + * @param hasText 是否有文本内容 + */ + void onTextChange(int index, boolean hasText); + } + + private OnTextViewChangeListener mOnTextViewChangeListener; // 文本变化监听器实例 + + // 第一个构造方法 + public NoteEditText(Context context) { + super(context, null); + mIndex = 0; // 默认索引为0 + } + + // 设置当前编辑框索引 + public void setIndex(int index) { + mIndex = index; + } + + // 设置文本变化监听器 + public void setOnTextViewChangeListener(OnTextViewChangeListener listener) { + mOnTextViewChangeListener = listener; + } + + // 第二个构造方法 + public NoteEditText(Context context, AttributeSet attrs) { + super(context, attrs, android.R.attr.editTextStyle); // 调用父类构造方法 + } + + // 第三个构造方法 + public NoteEditText(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + // TODO: 待实现的构造函数 + } + + // 触摸事件处理 + @Override + public boolean onTouchEvent(MotionEvent event) { + // 按下事件处理 + if (event.getAction() == MotionEvent.ACTION_DOWN) { + // 计算触摸点的准确位置 + int x = (int) event.getX(); + int y = (int) event.getY(); + x -= getTotalPaddingLeft(); // 减去左边距 + y -= getTotalPaddingTop(); // 减去上边距 + x += getScrollX(); // 加上水平滚动量 + y += getScrollY(); // 加上垂直滚动量 + + // 获取布局并确定触摸点对应的文本偏移位置 + Layout layout = getLayout(); + int line = layout.getLineForVertical(y); // 获取垂直方向的行号 + int off = layout.getOffsetForHorizontal(line, x); // 获取水平方向的偏移量 + Selection.setSelection(getText(), off); // 设置文本选择位置 + } + + return super.onTouchEvent(event); // 调用父类处理方法 + } + + // 按键按下事件处理 + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_ENTER: // 回车键 + if (mOnTextViewChangeListener != null) { + return false; // 交给onKeyUp处理 + } + break; + case KeyEvent.KEYCODE_DEL: // 删除键 + mSelectionStartBeforeDelete = getSelectionStart(); // 记录删除前的位置 + break; + default: + break; + } + return super.onKeyDown(keyCode, event); // 调用父类处理方法 + } + + // 按键释放事件处理 + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + switch(keyCode) { + case KeyEvent.KEYCODE_DEL: // 删除键 + if (mOnTextViewChangeListener != null) { + // 如果在起始位置删除且不是第一个编辑框,则触发删除回调 + if (0 == mSelectionStartBeforeDelete && mIndex != 0) { + mOnTextViewChangeListener.onEditTextDelete(mIndex, Objects.requireNonNull(getText()).toString()); + return true; // 消费事件 + } + } else { + Log.d(TAG, "OnTextViewChangeListener was not seted"); + } + break; + case KeyEvent.KEYCODE_ENTER: // 回车键 + if (mOnTextViewChangeListener != null) { + // 分割文本:光标后的内容放入新编辑框 + int selectionStart = getSelectionStart(); + String text = Objects.requireNonNull(getText()).subSequence(selectionStart, length()).toString(); + setText(getText().subSequence(0, selectionStart)); // 保留光标前的内容 + mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text); // 触发添加回调 + } else { + Log.d(TAG, "OnTextViewChangeListener was not seted"); + } + break; + default: + break; + } + return super.onKeyUp(keyCode, event); // 调用父类处理方法 + } + + // 焦点变化处理 + @Override + protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { + // 通知监听器文本变化状态 + if (mOnTextViewChangeListener != null) { + mOnTextViewChangeListener.onTextChange(mIndex, focused || !TextUtils.isEmpty(getText())); + } + super.onFocusChanged(focused, direction, previouslyFocusedRect); // 调用父类方法 + } + + // 创建上下文菜单 + @Override + protected void onCreateContextMenu(ContextMenu menu) { + if (getText() != null) { + // 获取当前选择的起止位置 + int selStart = getSelectionStart(); + int selEnd = getSelectionEnd(); + + // 计算最小和最大位置 + int min = Math.min(selStart, selEnd); + int max = Math.max(selStart, selEnd); + + // 获取选择范围内的URLSpan对象 + final URLSpan[] urls = getText().getSpans(min, max, URLSpan.class); + if (urls.length == 1) { // 如果选中了一个URL + int defaultResId = 0; + // 根据URL协议类型获取对应的资源ID + for(String schema: sSchemaActionResMap.keySet()) { + if(urls[0].getURL().contains(schema)) { + defaultResId = sSchemaActionResMap.get(schema); + break; + } + } + + // 如果没有匹配的协议,使用默认资源 + if (defaultResId == 0) { + defaultResId = R.string.note_link_other; + } + + // 添加上下文菜单项并设置点击监听器 + menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener( + item -> { + // 点击时执行URL跳转 + urls[0].onClick(NoteEditText.this); + return true; + }); + } + } + super.onCreateContextMenu(menu); // 调用父类方法 + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteItemData.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteItemData.java new file mode 100644 index 0000000..bf120a7 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteItemData.java @@ -0,0 +1,261 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义当前类的包路径,属于net.micode.notes.ui 包 +package net.micode.notes.ui; + +// 导入Android基础类 +import android.content.Context; // 上下文对象,提供应用环境信息 +import android.database.Cursor; // 数据库查询结果游标 +import android.text.TextUtils; // 文本处理工具类 + +// 导入项目自定义类 +import net.micode.notes.data.Contact; // 联系人数据类 +import net.micode.notes.data.Notes; // 笔记数据常量定义 +import net.micode.notes.data.Notes.NoteColumns; // 笔记表列名定义 +import net.micode.notes.tool.DataUtils; // 数据工具类 + +/** + * 笔记项数据类,封装单条笔记的所有数据 + */ +public class NoteItemData { + // 数据库查询列名投影(指定需要查询的列) + static final String[] PROJECTION = new String[] { + NoteColumns.ID, // 笔记ID + NoteColumns.ALERTED_DATE, // 提醒日期 + NoteColumns.BG_COLOR_ID, // 背景颜色ID + NoteColumns.CREATED_DATE, // 创建日期 + NoteColumns.HAS_ATTACHMENT, // 是否有附件 + NoteColumns.MODIFIED_DATE, // 修改日期 + NoteColumns.NOTES_COUNT, // 笔记数量(针对文件夹) + NoteColumns.PARENT_ID, // 父项ID + NoteColumns.SNIPPET, // 内容摘要 + NoteColumns.TYPE, // 笔记类型 + NoteColumns.WIDGET_ID, // 关联小部件ID + NoteColumns.WIDGET_TYPE, // 小部件类型 + }; + + // 列索引常量定义(对应PROJECTION顺序) + private static final int ID_COLUMN = 0; // ID列索引 + private static final int ALERTED_DATE_COLUMN = 1; // 提醒日期列索引 + private static final int BG_COLOR_ID_COLUMN = 2; // 背景颜色列索引 + private static final int CREATED_DATE_COLUMN = 3; // 创建日期列索引 + private static final int HAS_ATTACHMENT_COLUMN = 4; // 附件列索引 + private static final int MODIFIED_DATE_COLUMN = 5; // 修改日期列索引 + private static final int NOTES_COUNT_COLUMN = 6; // 笔记数量列索引 + private static final int PARENT_ID_COLUMN = 7; // 父项ID列索引 + private static final int SNIPPET_COLUMN = 8; // 摘要列索引 + private static final int TYPE_COLUMN = 9; // 类型列索引 + private static final int WIDGET_ID_COLUMN = 10; // 小部件ID列索引 + private static final int WIDGET_TYPE_COLUMN = 11; // 小部件类型列索引 + + /* 笔记数据字段 */ + private final long mId; // 笔记唯一ID + private final long mAlertDate; // 提醒时间戳 + private final int mBgColorId; // 背景颜色资源ID + private final long mCreatedDate; // 创建时间戳 + private final boolean mHasAttachment; // 是否有附件 + private final long mModifiedDate; // 最后修改时间戳 + private final int mNotesCount; // 包含的笔记数量(文件夹用) + private final long mParentId; // 父文件夹ID + private String mSnippet; // 笔记内容摘要 + private final int mType; // 笔记类型(笔记/文件夹等) + private final int mWidgetId; // 关联的小部件ID + private final int mWidgetType; // 小部件类型 + + /* 联系人相关字段 */ + private String mName; // 联系人姓名(通话记录用) + private String mPhoneNumber; // 电话号码(通话记录用) + + /* 位置状态标识 */ + private boolean mIsLastItem; // 是否是列表最后一项 + private boolean mIsFirstItem; // 是否是列表第一项 + private boolean mIsOnlyOneItem; // 是否是唯一一项 + private boolean mIsOneNoteFollowingFolder; // 是否是单个笔记跟随文件夹 + private boolean mIsMultiNotesFollowingFolder; // 是否是多个笔记跟随文件夹 + + /** + * 构造函数,从游标初始化笔记数据 + * @param context 上下文对象 + * @param cursor 数据库查询结果游标 + */ + public NoteItemData(Context context, Cursor cursor) { + // 从游标读取各列数据 + mId = cursor.getLong(ID_COLUMN); + mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); + mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); + mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); + // 将整型转换为布尔值 + mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0); + mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); + mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); + mParentId = cursor.getLong(PARENT_ID_COLUMN); + // 处理内容摘要,移除复选框标记 + mSnippet = cursor.getString(SNIPPET_COLUMN); + mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( + NoteEditActivity.TAG_UNCHECKED, ""); + mType = cursor.getInt(TYPE_COLUMN); + mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); + mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); + + // 初始化通话记录相关信息 + mPhoneNumber = ""; + if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { + // 如果是通话记录文件夹中的笔记,获取电话号码 + mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); + if (!TextUtils.isEmpty(mPhoneNumber)) { + // 根据电话号码获取联系人姓名 + mName = Contact.getContact(context, mPhoneNumber); + if (mName == null) { + mName = mPhoneNumber; // 如果没有联系人信息,显示号码 + } + } + } + + // 确保姓名不为null + if (mName == null) { + mName = ""; + } + + // 检查当前项在列表中的位置状态 + checkPostion(cursor); + } + + /** + * 检查当前项在列表中的位置状态 + * @param cursor 数据库游标 + */ + private void checkPostion(Cursor cursor) { + mIsLastItem = cursor.isLast(); // 是否是最后一项 + mIsFirstItem = cursor.isFirst(); // 是否是第一项 + mIsOnlyOneItem = (cursor.getCount() == 1); // 是否是唯一一项 + mIsMultiNotesFollowingFolder = false; + mIsOneNoteFollowingFolder = false; + + // 如果是笔记类型且不是第一项,检查前一项是否是文件夹 + if (mType == Notes.TYPE_NOTE && !mIsFirstItem) { + int position = cursor.getPosition(); + if (cursor.moveToPrevious()) { // 移动到前一项 + int prevType = cursor.getInt(TYPE_COLUMN); + // 前一项是文件夹或系统文件夹 + if (prevType == Notes.TYPE_FOLDER || prevType == Notes.TYPE_SYSTEM) { + // 判断是多个还是一个笔记跟随文件夹 + if (cursor.getCount() > (position + 1)) { + mIsMultiNotesFollowingFolder = true; + } else { + mIsOneNoteFollowingFolder = true; + } + } + // 移动回原来的位置 + if (!cursor.moveToNext()) { + throw new IllegalStateException("cursor move to previous but can't move back"); + } + } + } + } + + /* ========== 以下是各种访问方法 ========== */ + + public boolean isOneFollowingFolder() { + return mIsOneNoteFollowingFolder; // 是否是单个笔记跟随文件夹 + } + + public boolean isMultiFollowingFolder() { + return mIsMultiNotesFollowingFolder; // 是否是多个笔记跟随文件夹 + } + + public boolean isLast() { + return mIsLastItem; // 是否是最后一项 + } + + public String getCallName() { + return mName; // 获取联系人姓名 + } + + public boolean isFirst() { + return mIsFirstItem; // 是否是第一项 + } + + public boolean isSingle() { + return mIsOnlyOneItem; // 是否是唯一一项 + } + + public long getId() { + return mId; // 获取笔记ID + } + + public long getAlertDate() { + return mAlertDate; // 获取提醒时间 + } + + public long getCreatedDate() { + return mCreatedDate; // 获取创建时间 + } + + public boolean hasAttachment() { + return mHasAttachment; // 是否有附件 + } + + public long getModifiedDate() { + return mModifiedDate; // 获取修改时间 + } + + public int getBgColorId() { + return mBgColorId; // 获取背景颜色ID + } + + public long getParentId() { + return mParentId; // 获取父文件夹ID + } + + public int getNotesCount() { + return mNotesCount; // 获取包含的笔记数量 + } + + public long getFolderId() { + return mParentId; // 获取文件夹ID(与getParentId相同) + } + + public int getType() { + return mType; // 获取笔记类型 + } + + public int getWidgetType() { + return mWidgetType; // 获取小部件类型 + } + + public int getWidgetId() { + return mWidgetId; // 获取小部件ID + } + + public String getSnippet() { + return mSnippet; // 获取内容摘要 + } + + public boolean hasAlert() { + return (mAlertDate > 0); // 是否有提醒 + } + + public boolean isCallRecord() { + // 是否是通话记录(在通话记录文件夹中且有号码) + return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber)); + } + + /** + * 静态方法:从游标获取笔记类型 + * @param cursor 数据库游标 + * @return 笔记类型 + */ + public static int getNoteType(Cursor cursor) { + return cursor.getInt(TYPE_COLUMN); + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListActivity.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListActivity.java new file mode 100644 index 0000000..4de9b0a --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListActivity.java @@ -0,0 +1,1358 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 定义当前包名为net.micode.notes.ui +package net.micode.notes.ui; + +/* 导入Android基础包 */ +import android.annotation.SuppressLint; +import android.app.Activity; // 基础Activity类 +import android.app.AlertDialog; // 警告对话框类 +import android.app.Dialog; // 对话框基类 +import android.appwidget.AppWidgetManager; // 小部件管理类 +import android.content.AsyncQueryHandler; // 异步查询处理器 +import android.content.ContentResolver; // 内容解析器 +import android.content.ContentValues; // 内容值对象 +import android.content.Context; // 上下文对象 +import android.content.Intent; // Intent类 +import android.content.SharedPreferences; // 共享偏好设置 +import android.database.Cursor; // 数据库游标 +import android.graphics.Point; +// 异步任务类 +import android.os.Build; +import android.os.Bundle; // Bundle数据包 +// 偏好设置管理器 +import android.text.Editable; // 可编辑文本接口 +import android.text.TextUtils; // 文本工具类 +import android.text.TextWatcher; // 文本变化监听器 +import android.util.Log; // 日志工具 +import android.view.ActionMode; // 上下文操作模式 +import android.view.ContextMenu; // 上下文菜单 +import android.view.ContextMenu.ContextMenuInfo; // 上下文菜单信息 +import android.view.Display; // 显示设备信息 +import android.view.HapticFeedbackConstants; // 触觉反馈常量 +import android.view.LayoutInflater; // 布局加载器 +import android.view.Menu; // 菜单类 +import android.view.MenuItem; // 菜单项 +import android.view.MenuItem.OnMenuItemClickListener; // 菜单项点击监听器 +import android.view.MotionEvent; // 触摸事件 +import android.view.View; // 视图基类 +import android.view.View.OnClickListener; // 点击监听器 +import android.view.View.OnCreateContextMenuListener; // 上下文菜单创建监听器 +import android.view.View.OnTouchListener; // 触摸监听器 +import android.view.inputmethod.InputMethodManager; // 输入法管理器 +import android.widget.AdapterView; // 适配器视图基类 +import android.widget.AdapterView.OnItemClickListener; // 列表项点击监听器 +import android.widget.AdapterView.OnItemLongClickListener; // 列表项长按监听器 +import android.widget.Button; // 按钮控件 +import android.widget.EditText; // 编辑文本框 +import android.widget.ListView; // 列表视图 +import android.widget.TextView; // 文本视图 +import android.widget.Toast; // 提示信息控件 + +/* 导入AndroidX支持库 */ +import androidx.annotation.NonNull; // 非空注解 + +/* 导入项目资源 */ +import net.micode.notes.R; // 自动生成的R资源类 + +/* 导入项目数据相关类 */ +import net.micode.notes.data.Notes; // 笔记数据库常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列名定义 + +/* 导入Google任务同步服务 */ +import net.micode.notes.gtask.remote.GTaskSyncService; // Google任务同步服务 + +/* 导入项目模型类 */ +import net.micode.notes.model.WorkingNote; // 工作笔记模型 + +/* 导入项目工具类 */ +import net.micode.notes.tool.BackupUtils; // 备份工具 +import net.micode.notes.tool.DataUtils; // 数据工具 +import net.micode.notes.tool.ResourceParser; // 资源解析器 + +/* 导入项目UI适配器 */ +import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; // 小部件属性定义 + +/* 导入项目小部件相关 */ +import net.micode.notes.widget.NoteWidgetProvider_2x; // 2x尺寸小部件 +import net.micode.notes.widget.NoteWidgetProvider_4x; // 4x尺寸小部件 + +/* 导入Java IO类 */ +import java.io.BufferedReader; // 缓冲读取器 +import java.io.IOException; // IO异常 +import java.io.InputStream; // 输入流 +import java.io.InputStreamReader; // 输入流读取器 + +/* 导入Java集合类 */ +import java.util.HashSet; // 哈希集合 +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +// 定义笔记列表Activity,实现点击和长按监听接口 +public class NotesListActivity extends Activity implements OnClickListener, OnItemLongClickListener { + // 查询令牌常量 + private static final int FOLDER_NOTE_LIST_QUERY_TOKEN = 0; // 文件夹笔记列表查询标识 + private static final int FOLDER_LIST_QUERY_TOKEN = 1; // 文件夹列表查询标识 + + // 上下文菜单项ID + private static final int MENU_FOLDER_DELETE = 0; // 删除文件夹菜单ID + private static final int MENU_FOLDER_VIEW = 1; // 查看文件夹菜单ID + private static final int MENU_FOLDER_CHANGE_NAME = 2; // 重命名文件夹菜单ID + + // 偏好设置键(是否已添加引导说明) + private static final String PREFERENCE_ADD_INTRODUCTION = "net.micode.notes.introduction"; + + // 列表编辑状态枚举 + private enum ListEditState { + NOTE_LIST, // 普通笔记列表模式 + SUB_FOLDER, // 子文件夹模式 + CALL_RECORD_FOLDER // 通话记录文件夹模式 + } + + // 成员变量声明 + private ListEditState mState; // 当前列表状态 + private BackgroundQueryHandler mBackgroundQueryHandler; // 后台查询处理器 + private NotesListAdapter mNotesListAdapter; // 笔记列表适配器 + private ListView mNotesListView; // 笔记列表视图 + private Button mAddNewNote; // 新建笔记按钮 + private boolean mDispatch; // 触摸事件分发标志 + private int mOriginY; // 触摸起始Y坐标 + private int mDispatchY; // 触摸分发Y坐标 + private TextView mTitleBar; // 标题栏文本 + private long mCurrentFolderId; // 当前文件夹ID + private ContentResolver mContentResolver; // 内容解析器 + private ModeCallback mModeCallBack; // 多选模式回调 + private static final String TAG = "NotesListActivity"; // 日志标签 + public static final int NOTES_LISTVIEW_SCROLL_RATE = 30; // 列表滚动速率 + private NoteItemData mFocusNoteDataItem; // 当前焦点笔记数据 + private int mode = -1; + // SQL查询条件 + private static final String NORMAL_SELECTION = NoteColumns.PARENT_ID + "=?"; + private static final String ROOT_FOLDER_SELECTION = "(" + NoteColumns.TYPE + "<>" + + Notes.TYPE_SYSTEM + " AND " + NoteColumns.PARENT_ID + "=?)" + " OR (" + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER + " AND " + + NoteColumns.NOTES_COUNT + ">0)"; + + // 请求码常量 + private final static int REQUEST_CODE_OPEN_NODE = 102; // 打开笔记请求码 + private final static int REQUEST_CODE_NEW_NODE = 103; // 新建笔记请求码 + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + this.setContentView(R.layout.note_list); // 设置布局文件 + + initResources(); // 初始化资源 + + // 首次使用时添加引导说明 + setAppInfoFromRawRes(); + getWindow().setBackgroundDrawableResource(R.drawable.anqila); + getWindow().setBackgroundDrawableResource(R.drawable.aguduo); + getWindow().setBackgroundDrawableResource(R.drawable.yao); + initResources(); + setAppInfoFromRawRes(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // 处理笔记打开/新建返回结果 + if (resultCode == RESULT_OK + && (requestCode == REQUEST_CODE_OPEN_NODE || requestCode == REQUEST_CODE_NEW_NODE)) { + mNotesListAdapter.changeCursor(null); // 清空当前游标触发刷新 + } else { + super.onActivityResult(requestCode, resultCode, data); + } + } + + // 从raw资源读取并设置应用引导信息 + private void setAppInfoFromRawRes() { + SharedPreferences sp = getSharedPreferences( + getPackageName() + "_preferences", // 与原默认文件名一致 + Context.MODE_PRIVATE + ); + // 检查是否已经添加过引导说明 + if (!sp.getBoolean(PREFERENCE_ADD_INTRODUCTION, false)) { + StringBuilder sb = new StringBuilder(); + InputStream in = null; + try { + // 从raw资源读取引导文本 + in = getResources().openRawResource(R.raw.introduction); + InputStreamReader isr = new InputStreamReader(in); + BufferedReader br = new BufferedReader(isr); + char[] buf = new char[1024]; + int len; + while ((len = br.read(buf)) > 0) { + sb.append(buf, 0, len); + } + } catch (IOException e) { + Log.e(TAG, "Batch delete error", e); + return; + } finally { + // 关闭输入流 + if (in != null) { + try { + in.close(); + } catch (IOException e) { + Log.e(TAG, "Save introduction note error"); + } + } + } + + // 创建并保存引导笔记 + WorkingNote note = WorkingNote.createEmptyNote(this, Notes.ID_ROOT_FOLDER, + AppWidgetManager.INVALID_APPWIDGET_ID, Notes.TYPE_WIDGET_INVALIDE, + ResourceParser.RED); + note.setWorkingText(sb.toString()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + if (note.saveNote()) { + sp.edit().putBoolean(PREFERENCE_ADD_INTRODUCTION, true).apply(); + } else { + Log.e(TAG, "Save introduction note error"); + } + } + } + } + + @Override + protected void onStart() { + super.onStart(); + startAsyncNotesListQuery(); // 启动异步笔记列表查询 + } + + // 初始化视图资源和变量 + @SuppressLint("ClickableViewAccessibility") + private void initResources() { + mContentResolver = this.getContentResolver(); + mBackgroundQueryHandler = new BackgroundQueryHandler(this.getContentResolver()); + mCurrentFolderId = Notes.ID_ROOT_FOLDER; // 默认设为根文件夹 + + // 初始化列表视图 + mNotesListView = findViewById(R.id.notes_list); + mNotesListView.addFooterView(LayoutInflater.from(this).inflate( + R.layout.note_list_footer, null), null, false); + mNotesListView.setOnItemClickListener(new OnListItemClickListener()); + mNotesListView.setOnItemLongClickListener(this); + + // 设置列表适配器 + mNotesListAdapter = new NotesListAdapter(this); + mNotesListView.setAdapter(mNotesListAdapter); + + // 初始化新建笔记按钮 + mAddNewNote = findViewById(R.id.btn_new_note); + mAddNewNote.setOnClickListener(this); + mAddNewNote.setOnTouchListener(new NewNoteOnTouchListener()); + + // 初始化触摸相关变量 + mDispatch = false; + mDispatchY = 0; + mOriginY = 0; + + // 初始化标题栏 + mTitleBar = findViewById(R.id.tv_title_bar); + mState = ListEditState.NOTE_LIST; // 初始状态为普通笔记列表 + + // 初始化多选模式回调 + mModeCallBack = new ModeCallback(); + } + + // 多选模式回调实现类 + private class ModeCallback implements ListView.MultiChoiceModeListener, OnMenuItemClickListener { + private DropdownMenu mDropDownMenu; // 下拉菜单 + private ActionMode mActionMode; // 操作模式 + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + // 加载多选操作菜单 + getMenuInflater().inflate(R.menu.note_list_options, menu); + menu.findItem(R.id.delete).setOnMenuItemClickListener(this); + + // 初始化移动菜单 + // 移动菜单项 + MenuItem mMoveMenu = menu.findItem(R.id.move); + if (mFocusNoteDataItem.getParentId() == Notes.ID_CALL_RECORD_FOLDER + || DataUtils.getUserFolderCount(mContentResolver) == 0) { + mMoveMenu.setVisible(false); // 通话记录或无文件夹时隐藏移动选项 + } else { + mMoveMenu.setVisible(true); + mMoveMenu.setOnMenuItemClickListener(this); + } + + // 设置多选模式状态 + mActionMode = mode; + mNotesListAdapter.setChoiceMode(true); + mNotesListView.setLongClickable(false); + mAddNewNote.setVisibility(View.GONE); // 隐藏新建按钮 + + // 设置自定义多选操作栏 + View customView = LayoutInflater.from(NotesListActivity.this).inflate( + R.layout.note_list_dropdown_menu, null); + mode.setCustomView(customView); + + // 初始化下拉选择菜单 + mDropDownMenu = new DropdownMenu(NotesListActivity.this, + customView.findViewById(R.id.selection_menu), + R.menu.note_list_dropdown); + mDropDownMenu.setOnDropdownMenuItemClickListener(item -> { + // 全选/反选处理 + mNotesListAdapter.selectAll(!mNotesListAdapter.isAllSelected()); + updateMenu(); + return true; + }); + return true; + } + + /** + * 更新多选操作菜单的状态显示 + * 1. 刷新选中项数量显示 + * 2. 更新全选/反选按钮状态 + */ + private void updateMenu() { + // 获取当前选中的笔记项数量(从列表适配器中获取) + int selectedCount = mNotesListAdapter.getSelectedCount(); + + /* + 设置下拉菜单标题文本 + 使用字符串资源 R.string.menu_select_title 作为模板 + 将选中数量 selectedCount 作为参数插入字符串 + 示例:当选中3项时显示"已选3项" + */ + String format = getResources().getString(R.string.menu_select_title, selectedCount); + mDropDownMenu.setTitle(format); // 将格式化后的字符串设置为菜单标题 + + // 从下拉菜单中获取全选功能菜单项(通过资源ID R.id.action_select_all ) + MenuItem item = mDropDownMenu.findItem(R.id.action_select_all); + + // 确保菜单项存在(防御性编程) + if (item != null) { + /* + 检查是否所有笔记项都被选中 + 根据状态更新菜单项: + 1. 已全选:显示选中状态和"取消全选"文本 + 2. 未全选:显示未选中状态和"全选"文本 + */ + if (mNotesListAdapter.isAllSelected()) { + item.setChecked(true); // 显示复选框选中状态 + item.setTitle(R.string.menu_deselect_all); // 设置文本为"取消全选" + } else { + item.setChecked(false); // 显示复选框未选中状态 + item.setTitle(R.string.menu_select_all); // 设置文本为"全选" + } + } + } + + /** + * ActionMode准备时的回调方法 + * + * @param mode 当前的ActionMode对象 + * @param menu 要显示的菜单 + * @return boolean 返回false表示不需要重新创建菜单 + */ + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + // TODO: 需要在此处添加菜单项的动态更新逻辑 + return false; // 默认返回false + } + + /** + * ActionMode菜单项点击事件处理 + * + * @param mode 当前的ActionMode对象 + * @param item 被点击的菜单项 + * @return boolean 返回false表示事件未处理,会继续传递 + */ + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + // TODO: 需要添加其他菜单项的点击处理逻辑 + return false; // 默认返回false + } + + /** + * 销毁ActionMode时的清理操作 + * + * @param mode 要销毁的ActionMode对象 + */ + public void onDestroyActionMode(ActionMode mode) { + mNotesListAdapter.setChoiceMode(false); // 禁用适配器的多选模式 + mNotesListView.setLongClickable(true); // 恢复列表视图的长按功能 + mAddNewNote.setVisibility(View.VISIBLE); // 显示新建笔记按钮 + } + + /** + * 主动结束ActionMode + */ + public void finishActionMode() { + mActionMode.finish(); // 调用当前ActionMode的finish方法结束多选模式 + } + + /** + * 列表项选中状态变化回调 + * + * @param mode 当前的ActionMode对象 + * @param position 发生变化的项位置 + * @param id 项ID + * @param checked 新的选中状态 + */ + public void onItemCheckedStateChanged(ActionMode mode, int position, long id, + boolean checked) { + mNotesListAdapter.setCheckedItem(position, checked); // 更新适配器中对应项的选中状态 + updateMenu(); // 刷新菜单显示 + } + + /** + * 处理上下文菜单项点击事件 + * + * @param item 被点击的菜单项 + * @return boolean 返回true表示事件已处理 + */ + @SuppressLint("NonConstantResourceId") + public boolean onMenuItemClick(@NonNull MenuItem item) { + // 检查当前是否有选中的笔记 + if (mNotesListAdapter.getSelectedCount() == 0) { + // 显示"未选中任何项"的提示 + Toast.makeText(NotesListActivity.this, + getString(R.string.menu_select_none), + Toast.LENGTH_SHORT).show(); + return true; // 事件已处理 + } + + // 根据点击的菜单项ID执行不同操作 + switch (item.getItemId()) { + case R.id.delete: // 处理删除操作 + // 创建删除确认对话框 + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); + builder.setTitle(getString(R.string.alert_title_delete)); // 设置标题 + builder.setIcon(android.R.drawable.ic_dialog_alert); // 设置警告图标 + + // 设置提示消息,显示要删除的笔记数量 + builder.setMessage(getString(R.string.alert_message_delete_notes, + mNotesListAdapter.getSelectedCount())); + + // 设置确认按钮,点击后执行批量删除 + builder.setPositiveButton(android.R.string.ok, + (dialog, which) -> batchDelete()); + + // 设置取消按钮,点击后不执行任何操作 + builder.setNegativeButton(android.R.string.cancel, null); + + builder.show(); // 显示对话框 + break; + + case R.id.move: // 处理移动操作 + startQueryDestinationFolders(); // 启动目标文件夹查询 + break; + + default: // 其他菜单项 + return false; // 返回false表示不处理 + } + return true; // 返回true表示事件已处理 + } + } + + /** + * 新建笔记按钮的触摸事件监听器 + * 处理透明区域的特殊点击事件分发 + */ + private class NewNoteOnTouchListener implements OnTouchListener { + + /** + * 处理触摸事件 + * + * @param v 被触摸的视图(新建笔记按钮) + * @param event 触摸事件对象 + * @return boolean 返回true表示事件已处理,false继续传递 + */ + public boolean onTouch(View v, MotionEvent event) { + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: { // 按下事件 + // 获取屏幕显示信息 + Display display = getWindowManager().getDefaultDisplay(); + //屏幕高度 + Point screenSize = new Point(); + display.getSize(screenSize); + int screenHeight = screenSize.y; + int newNoteViewHeight = mAddNewNote.getHeight(); // 按钮高度 + + // 计算按钮在屏幕中的起始Y坐标(屏幕底部向上偏移按钮高度) + int start = screenHeight - newNoteViewHeight; + // 计算触摸事件的绝对Y坐标(相对于屏幕) + int eventY = start + (int) event.getY(); + + // 如果是子文件夹模式,需要减去标题栏高度 + if (mState == ListEditState.SUB_FOLDER) { + eventY -= mTitleBar.getHeight(); + start -= mTitleBar.getHeight(); + } + + /* + HACK: 处理按钮透明区域的点击事件分发 + 透明区域定义为:y < -0.12x + 94(基于按钮局部坐标系) + 94表示透明区域的最大高度(像素) + 注意:如果按钮背景改变,这个公式需要相应调整 + */ + if (event.getY() < (event.getX() * (-0.12) + 94)) { + // 获取列表最后一个可见项(排除页脚视图) + View view = mNotesListView.getChildAt(mNotesListView.getChildCount() - 1 + - mNotesListView.getFooterViewsCount()); + + // 检查触摸位置是否与列表项重叠 + if (view != null && view.getBottom() > start + && (view.getTop() < (start + 94))) { + mOriginY = (int) event.getY(); // 记录原始Y坐标(局部) + mDispatchY = eventY; // 记录分发Y坐标(绝对) + event.setLocation(event.getX(), mDispatchY); // 修改事件坐标 + mDispatch = true; // 设置分发标志 + return mNotesListView.dispatchTouchEvent(event); // 分发事件给列表 + } + } + break; + } + case MotionEvent.ACTION_MOVE: { // 移动事件 + if (mDispatch) { + // 计算Y坐标偏移量并更新分发坐标 + mDispatchY += (int) event.getY() - mOriginY; + event.setLocation(event.getX(), mDispatchY); + return mNotesListView.dispatchTouchEvent(event); // 继续分发移动事件 + } + break; + } + default: { // 处理ACTION_UP和ACTION_CANCEL + if (mDispatch) { + event.setLocation(event.getX(), mDispatchY); // 保持坐标一致性 + mDispatch = false; // 重置分发标志 + return mNotesListView.dispatchTouchEvent(event); // 分发抬起/取消事件 + } + break; + } + } + return false; // 不处理事件,继续传递 + } + } + + /** + * 启动异步查询笔记列表 + * 根据当前文件夹ID决定查询条件 + */ + private void startAsyncNotesListQuery() { + // 选择查询条件:如果是根文件夹使用特殊查询,否则使用普通查询 + String selection = (mCurrentFolderId == Notes.ID_ROOT_FOLDER) ? ROOT_FOLDER_SELECTION + : NORMAL_SELECTION; + + // 执行异步查询 + mBackgroundQueryHandler.startQuery( + FOLDER_NOTE_LIST_QUERY_TOKEN, // 查询标识 + null, // cookie对象 + Notes.CONTENT_NOTE_URI, // 内容URI + NoteItemData.PROJECTION, // 查询列 + selection, // WHERE条件 + new String[]{String.valueOf(mCurrentFolderId)}, // WHERE参数 + NoteColumns.TYPE + " DESC," + NoteColumns.MODIFIED_DATE + " DESC" // 排序 + ); + } + + /** + * 后台查询处理器 + * 继承AsyncQueryHandler实现异步数据库操作 + */ + private final class BackgroundQueryHandler extends AsyncQueryHandler { + + /** + * 构造函数 + * + * @param contentResolver 内容解析器 + */ + public BackgroundQueryHandler(ContentResolver contentResolver) { + super(contentResolver); // 调用父类构造函数 + } + + /** + * 查询完成回调 + * + * @param token 查询标识 + * @param cookie 附加数据 + * @param cursor 查询结果游标 + */ + @Override + protected void onQueryComplete(int token, Object cookie, Cursor cursor) { + switch (token) { + case FOLDER_NOTE_LIST_QUERY_TOKEN: // 笔记列表查询完成 + mNotesListAdapter.changeCursor(cursor); // 更新适配器数据 + break; + case FOLDER_LIST_QUERY_TOKEN: // 文件夹列表查询完成 + if (cursor != null && cursor.getCount() > 0) { + showFolderListMenu(cursor); // 显示文件夹选择菜单 + } else { + Log.e(TAG, "Query folder failed"); // 记录查询失败 + } + break; + default: + // 其他查询类型不处理 + } + } + } + + /** + * 显示文件夹选择菜单 + * + * @param cursor 包含文件夹数据的游标 + */ + private void showFolderListMenu(Cursor cursor) { + // 创建对话框构建器 + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); + builder.setTitle(R.string.menu_title_select_folder); // 设置标题 + + // 创建文件夹列表适配器 + final FoldersListAdapter adapter = new FoldersListAdapter(this, cursor); + + // 设置列表项点击监听器 + builder.setAdapter(adapter, (dialog, which) -> { + // 执行批量移动操作 + DataUtils.batchMoveToFolder( + mContentResolver, + mNotesListAdapter.getSelectedItemIds(), // 选中笔记ID集合 + adapter.getItemId(which) // 目标文件夹ID + ); + + // 显示操作结果提示 + Toast.makeText( + NotesListActivity.this, + getString(R.string.format_move_notes_to_folder, + mNotesListAdapter.getSelectedCount(), // 移动的笔记数量 + adapter.getFolderName(NotesListActivity.this, which)), // 目标文件夹名 + Toast.LENGTH_SHORT + ).show(); + + mModeCallBack.finishActionMode(); // 结束多选模式 + }); + + builder.show(); // 显示对话框 + } + + /** + * 创建新笔记 + * 启动笔记编辑Activity并传递当前文件夹ID + */ + private void createNewNote() { + // 创建编辑意图 + Intent intent = new Intent(this, NoteEditActivity.class); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // 设置操作为插入或编辑 + intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mCurrentFolderId); // 传递当前文件夹ID + + // 启动Activity并等待结果 + this.startActivityForResult(intent, REQUEST_CODE_NEW_NODE); + } + + /** + * 批量删除选中的笔记项 + * 根据同步模式决定直接删除还是移动到回收站 + * 使用ExecutorService替代AsyncTask + */ + private void batchDelete() { + // 创建单线程线程池 + ExecutorService executor = Executors.newSingleThreadExecutor(); + + // 提交任务到线程池 + executor.execute(() -> { + try { + // 1. 获取选中笔记关联的小部件集合 + HashSet widgets = mNotesListAdapter.getSelectedWidget(); + + // 2. 根据同步模式选择删除策略 + if (!isSyncMode()) { + // 非同步模式:直接删除 + if (!DataUtils.batchDeleteNotes(mContentResolver, + mNotesListAdapter.getSelectedItemIds())) { + Log.e(TAG, "Delete notes error, should not happen"); + } + } else { + // 同步模式:移动到回收站 + if (!DataUtils.batchMoveToFolder(mContentResolver, + mNotesListAdapter.getSelectedItemIds(), Notes.ID_TRASH_FOLER)) { + Log.e(TAG, "Move notes to trash folder error, should not happen"); + } + } + + // 3. 切换到主线程更新UI + runOnUiThread(() -> { + // 更新关联的小部件 + if (widgets != null) { + for (AppWidgetAttribute widget : widgets) { + if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID && + widget.widgetType.get() != Notes.TYPE_WIDGET_INVALIDE) { + updateWidget(widget.widgetId, widget.widgetType.get()); + } + } + } + mModeCallBack.finishActionMode(); + }); + + } catch (Exception e) { + Log.e(TAG, "Batch delete error", e); + } finally { + executor.shutdown(); // 关闭线程池 + } + }); + } + + /** + * 删除指定文件夹 + * + * @param folderId 要删除的文件夹ID + */ + private void deleteFolder(long folderId) { + // 检查是否是根文件夹(不允许删除) + if (folderId == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "Wrong folder id, should not happen " + folderId); + return; + } + + // 1. 准备要删除的文件夹ID集合 + HashSet ids = new HashSet<>(); + ids.add(folderId); + + // 2. 获取文件夹关联的小部件 + HashSet widgets = DataUtils.getFolderNoteWidget(mContentResolver, folderId); + + // 3. 根据同步模式选择删除策略 + if (!isSyncMode()) { + DataUtils.batchDeleteNotes(mContentResolver, ids); // 直接删除 + } else { + DataUtils.batchMoveToFolder(mContentResolver, ids, Notes.ID_TRASH_FOLER); // 移动到回收站 + } + + // 4. 更新关联的小部件 + if (widgets != null) { + for (AppWidgetAttribute widget : widgets) { + if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID && + widget.widgetType.get() != Notes.TYPE_WIDGET_INVALIDE) { + updateWidget(widget.widgetId, widget.widgetType.get()); + } + } + } + } + + /** + * 打开笔记查看/编辑界面 + * + * @param data 要打开的笔记数据 + */ + private void openNode(NoteItemData data) { + Intent intent = new Intent(this, NoteEditActivity.class); + intent.setAction(Intent.ACTION_VIEW); // 设置为查看模式 + intent.putExtra(Intent.EXTRA_UID, data.getId()); // 传递笔记ID + startActivityForResult(intent, REQUEST_CODE_OPEN_NODE); // 启动编辑界面 + } + + /** + * 打开文件夹显示其内容 + * + * @param data 要打开的文件夹数据 + */ + private void openFolder(NoteItemData data) { + // 1. 更新当前文件夹ID并查询内容 + mCurrentFolderId = data.getId(); + startAsyncNotesListQuery(); + + // 2. 根据文件夹类型设置状态 + if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { + mState = ListEditState.CALL_RECORD_FOLDER; + mAddNewNote.setVisibility(View.GONE); // 通话记录文件夹隐藏新建按钮 + } else { + mState = ListEditState.SUB_FOLDER; + } + + // 3. 更新标题栏显示 + if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { + mTitleBar.setText(R.string.call_record_folder_name); // 固定标题 + } else { + mTitleBar.setText(data.getSnippet()); // 使用文件夹名作为标题 + } + mTitleBar.setVisibility(View.VISIBLE); // 显示标题栏 + } + + // 处理新建笔记按钮点击 + public void onClick(View v) { + if (v.getId() == R.id.btn_new_note) { + createNewNote(); // 调用新建笔记方法 + } + } + + /** + * 强制显示软键盘 + */ + private void showSoftInput() { + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm != null) { + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // 强制显示 + } + } + + /** + * 隐藏软键盘 + * + * @param view 当前焦点视图 + */ + private void hideSoftInput(View view) { + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); // 根据窗口token隐藏 + } + + /** + * 显示创建/修改文件夹对话框 + * + * @param create true表示创建,false表示修改 + */ + private void showCreateOrModifyFolderDialog(final boolean create) { + // 1. 初始化对话框 + final AlertDialog.Builder builder = new AlertDialog.Builder(this); + View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null); + final EditText etName = view.findViewById(R.id.et_foler_name); + + // 2. 自动弹出软键盘 + showSoftInput(); + + // 3. 根据模式设置初始文本 + if (!create) { + if (mFocusNoteDataItem != null) { + etName.setText(mFocusNoteDataItem.getSnippet()); // 修改模式:预填原名称 + builder.setTitle(getString(R.string.menu_folder_change_name)); + } else { + Log.e(TAG, "The long click data item is null"); + return; + } + } else { + etName.setText(""); // 创建模式:清空输入框 + builder.setTitle(this.getString(R.string.menu_create_folder)); + } + + // 4. 设置对话框按钮 + builder.setPositiveButton(android.R.string.ok, null); // 先设为null后自定义点击 + builder.setNegativeButton(android.R.string.cancel, (dialog, which) -> hideSoftInput(etName)); + + // 5. 显示对话框 + final Dialog dialog = builder.setView(view).show(); + final Button positive = dialog.findViewById(android.R.id.button1); + + // 6. 自定义确认按钮点击逻辑 + positive.setOnClickListener(v -> { + hideSoftInput(etName); + String name = etName.getText().toString(); + + // 检查文件夹名是否已存在 + if (DataUtils.checkVisibleFolderName(mContentResolver, name)) { + Toast.makeText(this, getString(R.string.folder_exist, name), Toast.LENGTH_LONG).show(); + etName.setSelection(0, etName.length()); // 全选文本方便修改 + return; + } + + // 执行创建或修改操作 + if (!create) { + // 修改现有文件夹 + if (!TextUtils.isEmpty(name)) { + ContentValues values = new ContentValues(); + values.put(NoteColumns.SNIPPET, name); // 新名称 + values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为已修改 + mContentResolver.update(Notes.CONTENT_NOTE_URI, values, + NoteColumns.ID + "=?", + new String[]{String.valueOf(mFocusNoteDataItem.getId())}); + } + } else if (!TextUtils.isEmpty(name)) { + // 创建新文件夹 + ContentValues values = new ContentValues(); + values.put(NoteColumns.SNIPPET, name); + values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); + mContentResolver.insert(Notes.CONTENT_NOTE_URI, values); + } + dialog.dismiss(); // 关闭对话框 + }); + + // 7. 初始禁用确认按钮(当名称为空时) + if (TextUtils.isEmpty(etName.getText())) { + positive.setEnabled(false); + } + + // 8. 添加文本变化监听 + etName.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // 文本变化前(无需实现) + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + // 文本变化时启用/禁用确认按钮 + positive.setEnabled(!TextUtils.isEmpty(etName.getText())); + } + + @Override + public void afterTextChanged(Editable s) { + // 文本变化后(无需实现) + } + }); + } + + // 处理返回键按下事件 + @Override + public void onBackPressed() { + switch (mState) { + case SUB_FOLDER: + // 从子文件夹返回根目录 + mCurrentFolderId = Notes.ID_ROOT_FOLDER; + mState = ListEditState.NOTE_LIST; + startAsyncNotesListQuery(); // 重新加载根目录列表 + mTitleBar.setVisibility(View.GONE); // 隐藏标题栏 + break; + case CALL_RECORD_FOLDER: + // 从通话记录文件夹返回根目录 + mCurrentFolderId = Notes.ID_ROOT_FOLDER; + mState = ListEditState.NOTE_LIST; + mAddNewNote.setVisibility(View.VISIBLE); // 显示新建笔记按钮 + mTitleBar.setVisibility(View.GONE); // 隐藏标题栏 + startAsyncNotesListQuery(); // 重新加载根目录列表 + break; + case NOTE_LIST: + super.onBackPressed(); // 默认返回行为 + break; + default: + break; + } + } + + // 更新指定的小部件 + private void updateWidget(int appWidgetId, int appWidgetType) { + // 创建小部件更新Intent + Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); + + // 根据小部件类型设置对应的Provider类 + if (appWidgetType == Notes.TYPE_WIDGET_2X) { + intent.setClass(this, NoteWidgetProvider_2x.class); + } else if (appWidgetType == Notes.TYPE_WIDGET_4X) { + intent.setClass(this, NoteWidgetProvider_4x.class); + } else { + Log.e(TAG, "Unsupported widget type"); + return; + } + + // 设置要更新的小部件ID数组 + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{appWidgetId}); + + sendBroadcast(intent); // 发送广播更新小部件 + setResult(RESULT_OK, intent); // 设置操作结果为成功 + } + + // 文件夹长按菜单创建监听器 + private final OnCreateContextMenuListener mFolderOnCreateContextMenuListener = + new OnCreateContextMenuListener() { + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { + if (mFocusNoteDataItem != null) { + menu.setHeaderTitle(mFocusNoteDataItem.getSnippet()); // 设置菜单标题为文件夹名 + menu.add(0, MENU_FOLDER_VIEW, 0, R.string.menu_folder_view); // 添加查看菜单项 + menu.add(0, MENU_FOLDER_DELETE, 0, R.string.menu_folder_delete); // 添加删除菜单项 + menu.add(0, MENU_FOLDER_CHANGE_NAME, 0, R.string.menu_folder_change_name); // 添加重命名菜单项 + } + } + }; + + // 上下文菜单关闭时调用 + @Override + public void onContextMenuClosed(@NonNull Menu menu) { + if (mNotesListView != null) { + mNotesListView.setOnCreateContextMenuListener(null); // 移除菜单监听器 + } + super.onContextMenuClosed(menu); + } + + // 处理上下文菜单项选择 + @Override + public boolean onContextItemSelected(@NonNull MenuItem item) { + if (mFocusNoteDataItem == null) { + Log.e(TAG, "The long click data item is null"); + return false; + } + + switch (item.getItemId()) { + case MENU_FOLDER_VIEW: // 查看文件夹 + openFolder(mFocusNoteDataItem); + break; + case MENU_FOLDER_DELETE: // 删除文件夹 + // 显示删除确认对话框 + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getString(R.string.alert_title_delete)); + builder.setIcon(android.R.drawable.ic_dialog_alert); + builder.setMessage(getString(R.string.alert_message_delete_folder)); + builder.setPositiveButton(android.R.string.ok, + (dialog, which) -> deleteFolder(mFocusNoteDataItem.getId())); + builder.setNegativeButton(android.R.string.cancel, null); + builder.show(); + break; + case MENU_FOLDER_CHANGE_NAME: // 重命名文件夹 + showCreateOrModifyFolderDialog(false); + break; + default: + break; + } + return true; + } + + // 准备选项菜单 + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + menu.clear(); // 清空现有菜单 + + // 根据当前状态加载不同的菜单布局 + if (mState == ListEditState.NOTE_LIST) { + getMenuInflater().inflate(R.menu.note_list, menu); + // 根据同步状态设置同步/取消同步菜单项 + menu.findItem(R.id.menu_sync).setTitle( + GTaskSyncService.isSyncing() ? "取消同步":"同步"); + } else if (mState == ListEditState.SUB_FOLDER) { + getMenuInflater().inflate(R.menu.sub_folder, menu); + } else if (mState == ListEditState.CALL_RECORD_FOLDER) { + getMenuInflater().inflate(R.menu.call_record_folder, menu); + } else { + Log.e(TAG, "Wrong state:" + mState); + } + if(mode==-1) + menu.findItem(R.id.menu_aguduo).setVisible(false); + else if(mode==0) + menu.findItem(R.id.menu_anqila).setVisible(false); + else if(mode==1) + menu.findItem(R.id.menu_yao).setVisible(false); + return true; + } + + // 处理选项菜单项选择 + @SuppressLint("NonConstantResourceId") + @Override + public boolean onOptionsItemSelected(MenuItem item) { + SharedPreferences.Editor editor = getSharedPreferences("note_prefs", MODE_PRIVATE).edit(); + + switch (item.getItemId()) { + case R.id.menu_aguduo:{ + mode=-1; + getWindow().setBackgroundDrawableResource(R.drawable.aguduo); + break; + } + case R.id.menu_anqila:{ + mode=0; + getWindow().setBackgroundDrawableResource(R.drawable.anqila); + break; + } + case R.id.menu_yao:{ + mode=1; + getWindow().setBackgroundDrawableResource(R.drawable.yao); + break; + } + case R.id.menu_new_folder: // 新建文件夹 + showCreateOrModifyFolderDialog(true); + break; + case R.id.menu_export_text: // 导出为文本 + exportNoteToText(); + break; + case R.id.menu_sync: // 同步操作 + if (isSyncMode()) { + // 根据当前状态切换同步/取消同步 + if (TextUtils.equals(item.getTitle(), getString(R.string.menu_sync))) { + GTaskSyncService.startSync(this); // 开始同步 + } else { + GTaskSyncService.cancelSync(this); // 取消同步 + } + } else { + startPreferenceActivity(); // 未设置同步账号时打开设置 + } + break; + case R.id.menu_setting: // 设置 + // 启动设置Activity + startPreferenceActivity(); + break; + case R.id.menu_new_note: // 新建笔记 + createNewNote(); + break; + case R.id.menu_search: // 搜索 + onSearchRequested(); + break; + + default: + return super.onOptionsItemSelected(item); + } + return super.onOptionsItemSelected(item); + } + + /** + * 处理同步逻辑 + */ + private void handleSync() { + if (!DataUtils.externalStorageAvailable()) { + Toast.makeText(this, R.string.error_sdcard_unmounted, Toast.LENGTH_SHORT).show(); + return; + } + + SharedPreferences pref = getSharedPreferences( + "${applicationId}_preferences", + Context.MODE_PRIVATE + ); + String syncAccount = pref.getString(NotesPreferenceActivity.PREFERENCE_SYNC_ACCOUNT_NAME, null); + if (TextUtils.isEmpty(syncAccount)) { + Toast.makeText(this, R.string.error_no_sync_account, Toast.LENGTH_SHORT).show(); + startPreferenceActivity(); + } else { + if (!GTaskSyncService.isSyncing()) { + GTaskSyncService.startSync(this); + Toast.makeText(this, R.string.ticker_syncing, Toast.LENGTH_SHORT).show(); + } else { + GTaskSyncService.cancelSync(this); + Toast.makeText(this, R.string.ticker_cancel, Toast.LENGTH_SHORT).show(); + } + } + } + // 处理搜索请求 + @Override + public boolean onSearchRequested() { + startSearch(null, false, null /* appData */, false); + return true; + } + + /* + 将笔记导出为文本文件的后台操作方法 + 1. 使用异步任务在后台执行导出操作 + 2. 根据导出结果显示相应的提示对话框 + */ + /** + * 将笔记导出为文本文件的后台操作方法 + * 使用ExecutorService替代已弃用的AsyncTask + */ + private void exportNoteToText() { + // 获取BackupUtils单例实例 + final BackupUtils backup = BackupUtils.getInstance(NotesListActivity.this); + + // 创建单线程线程池 + ExecutorService executor = Executors.newSingleThreadExecutor(); + + // 提交任务到线程池 + executor.execute(() -> { + // 在后台线程执行导出操作 + final int result = backup.exportToText(); + + // 切换到主线程更新UI + runOnUiThread(() -> { + // 处理导出结果 + switch (result) { + case BackupUtils.STATE_SD_CARD_UNMOUNTED: + showExportDialog( + R.string.failed_sdcard_export, + R.string.error_sdcard_unmounted + ); + break; + case BackupUtils.STATE_SUCCESS: + showExportDialog( + R.string.success_sdcard_export, + R.string.format_exported_file_location, + backup.getExportedTextFileName(), + backup.getExportedTextFileDir() + ); + break; + case BackupUtils.STATE_SYSTEM_ERROR: + showExportDialog( + R.string.failed_sdcard_export, + R.string.error_sdcard_export + ); + break; + default: + showExportDialog( + R.string.failed_sdcard_export, + R.string.error_unknown + ); + } + }); + }); + + // 关闭线程池(实际应用中可以考虑复用线程池) + executor.shutdown(); + } + + /** + * 显示导出结果对话框 + */ + private void showExportDialog(int titleResId, int messageResId, Object... formatArgs) { + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); + builder.setTitle(titleResId) + .setIcon(android.R.drawable.ic_dialog_info); + + // 处理带格式化参数的提示消息 + String message = formatArgs.length > 0 ? + getString(messageResId, formatArgs) : + getString(messageResId); + + builder.setMessage(message) + .setPositiveButton(android.R.string.ok, null) + .show(); + } + + + /** + * 检查当前是否处于同步模式 + * + * @return true表示已设置同步账户(同步模式),false表示未设置(本地模式) + */ + private boolean isSyncMode() { + // 获取同步账户名并检查是否为空(去除前后空格) + return !NotesPreferenceActivity.getSyncAccountName(this).trim().isEmpty(); + } + + /** + * 启动设置Activity + * 优先使用父Activity启动(如果存在),否则使用当前Activity + */ + private void startPreferenceActivity() { + // 获取父Activity(FragmentActivity情况下),不存在则使用当前Activity + Activity from = getParent() != null ? getParent() : this; + // 创建跳转到设置页面的Intent + Intent intent = new Intent(from, NotesPreferenceActivity.class); + // 启动Activity(startActivityIfNeeded保证单例) + from.startActivityIfNeeded(intent, -1); + } + + /** + * 列表项点击事件监听器 + * 处理笔记/文件夹的点击逻辑 + */ + private class OnListItemClickListener implements OnItemClickListener { + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + // 检查是否为笔记列表项视图 + if (view instanceof NotesListItem) { + NoteItemData item = ((NotesListItem) view).getItemData(); + + // 多选模式下的处理 + if (mNotesListAdapter.isInChoiceMode()) { + // 仅处理笔记类型的项 + if (item.getType() == Notes.TYPE_NOTE) { + // 调整位置(考虑HeaderViews的影响) + position = position - mNotesListView.getHeaderViewsCount(); + // 切换当前项的选中状态 + mModeCallBack.onItemCheckedStateChanged( + null, position, id, !mNotesListAdapter.isSelectedItem(position)); + } + return; // 多选模式下不执行后续操作 + } + + // 根据当前状态处理点击 + switch (mState) { + case NOTE_LIST: // 根目录状态 + if (item.getType() == Notes.TYPE_FOLDER + || item.getType() == Notes.TYPE_SYSTEM) { + openFolder(item); // 打开文件夹或系统文件夹 + } else if (item.getType() == Notes.TYPE_NOTE) { + openNode(item); // 打开笔记 + } else { + Log.e(TAG, "Wrong note type in NOTE_LIST"); + } + break; + + case SUB_FOLDER: // 子文件夹状态 + case CALL_RECORD_FOLDER: // 通话记录文件夹状态 + if (item.getType() == Notes.TYPE_NOTE) { + openNode(item); // 打开笔记 + } else { + Log.e(TAG, "Wrong note type in SUB_FOLDER"); + } + break; + default: + break; + } + } + } + } + + /** + * 查询可用目标文件夹(用于移动笔记操作) + * 查询条件: + * 1. 文件夹类型 + * 2. 非回收站文件夹 + * 3. 非当前所在文件夹 + * 特殊处理:在根目录状态下包含根文件夹选项 + */ + private void startQueryDestinationFolders() { + // 基础查询条件(类型是文件夹,且不是回收站,且不是当前文件夹) + String selection = NoteColumns.TYPE + "=? AND " + + NoteColumns.PARENT_ID + "<>? AND " + + NoteColumns.ID + "<>?"; + + // 在根目录状态时,额外包含根文件夹选项 + selection = (mState == ListEditState.NOTE_LIST) ? selection : + "(" + selection + ") OR (" + NoteColumns.ID + "=" + Notes.ID_ROOT_FOLDER + ")"; + + // 执行异步查询 + mBackgroundQueryHandler.startQuery( + FOLDER_LIST_QUERY_TOKEN, // 查询标识 + null, // 附加数据 + Notes.CONTENT_NOTE_URI, // 内容URI + FoldersListAdapter.PROJECTION, // 查询列 + selection, // 查询条件 + new String[]{ // 查询参数值 + String.valueOf(Notes.TYPE_FOLDER), // 文件夹类型 + String.valueOf(Notes.ID_TRASH_FOLER), // 排除回收站 + String.valueOf(mCurrentFolderId) // 排除当前文件夹 + }, + NoteColumns.MODIFIED_DATE + " DESC" // 按修改时间降序 + ); + } + + /** + * 列表项长按事件处理 + * + * @return true表示已处理事件,false继续传递事件 + */ + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + // 检查是否为笔记列表项视图 + if (view instanceof NotesListItem) { + // 保存当前长按的数据项 + mFocusNoteDataItem = ((NotesListItem) view).getItemData(); + + // 笔记类型的长按处理(非多选模式下) + if (mFocusNoteDataItem.getType() == Notes.TYPE_NOTE && + !mNotesListAdapter.isInChoiceMode()) { + // 启动多选模式 + if (mNotesListView.startActionMode(mModeCallBack) != null) { + // 自动选中当前长按的项 + mModeCallBack.onItemCheckedStateChanged(null, position, id, true); + // 触觉反馈(长按震动) + mNotesListView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); + } else { + Log.e(TAG, "startActionMode fails"); + } + } + // 文件夹类型的长按处理 + else if (mFocusNoteDataItem.getType() == Notes.TYPE_FOLDER) { + // 设置上下文菜单监听器 + mNotesListView.setOnCreateContextMenuListener(mFolderOnCreateContextMenuListener); + } + } + return false; // 允许事件继续传递 + } + private void updateBackground() { + switch (mode) { + case -1: // 阿古朵 + getWindow().setBackgroundDrawableResource(R.drawable.aguduo); + break; + case 0: // 安琪拉 + getWindow().setBackgroundDrawableResource(R.drawable.anqila); + break; + case 1: // 瑶 + getWindow().setBackgroundDrawableResource(R.drawable.yao); + break; + default: + getWindow().setBackgroundDrawableResource(R.drawable.aguduo); + } + } + +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java new file mode 100644 index 0000000..ca18491 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java @@ -0,0 +1,276 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 声明当前Java文件的包路径(表明该类所在的位置) +package net.micode.notes.ui; + +// 导入Android相关的Context类(用于访问应用资源和系统服务) +import android.content.Context; +// 导入Cursor类(用于数据库查询结果的遍历) +import android.database.Cursor; +// 导入Android日志工具类 +import android.util.Log; +// 导入Android基础视图类 +import android.view.View; +// 导入视图组类(用于视图的容器管理) +import android.view.ViewGroup; +// 导入CursorAdapter类(用于将Cursor数据适配到ListView等控件) +import android.widget.CursorAdapter; + +// 导入当前应用的数据模型包 +import net.micode.notes.data.Notes; + +// 导入Java集合框架相关类 +import java.util.Collection; // 集合框架的根接口 +import java.util.HashMap; // 哈希映射表实现 +import java.util.HashSet; // 哈希集合实现 +import java.util.Iterator; // 迭代器接口 + +/** + * 笔记列表适配器 + * 功能: + * 1. 管理笔记列表数据显示 + * 2. 处理多选模式逻辑 + * 3. 跟踪选中状态 + */ +public class NotesListAdapter extends CursorAdapter { + private static final String TAG = "NotesListAdapter"; // 日志标签 + private final Context mContext; // 上下文对象 + private final HashMap mSelectedIndex; // 存储选中状态的映射表(位置 -> 是否选中) + private int mNotesCount; // 笔记总数(不含文件夹) + private boolean mChoiceMode; // 是否处于多选模式 + + /** + * 小部件属性封装类 + */ + public static class AppWidgetAttribute { + public int widgetId; // 小部件ID + public final ThreadLocal widgetType = new ThreadLocal<>() { + @Override + protected Integer initialValue() { + return 0; + } + }; // 小部件类型 + } + + /** + * 构造函数 + * @param context 上下文环境 + */ + public NotesListAdapter(Context context) { + super(context, null); // 初始化CursorAdapter,初始cursor为null + mSelectedIndex = new HashMap<>(); // 初始化选中状态集合 + mContext = context; // 保存上下文引用 + mNotesCount = 0; // 初始化笔记数量 + } + + /** + * 创建新列表项视图 + * @param context 上下文 + * @param cursor 数据游标 + * @param parent 父视图 + * @return 新建的NotesListItem视图 + */ + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + return new NotesListItem(context); // 创建新的笔记列表项 + } + + /** + * 绑定数据到列表项视图 + * @param view 要绑定的视图 + * @param context 上下文 + * @param cursor 数据游标 + */ + @Override + public void bindView(View view, Context context, Cursor cursor) { + if (view instanceof NotesListItem) { + // 创建笔记数据对象 + NoteItemData itemData = new NoteItemData(context, cursor); + // 绑定数据到列表项视图 + ((NotesListItem) view).bind(context, itemData, mChoiceMode, + isSelectedItem(cursor.getPosition())); + } + } + + /** + * 设置指定位置的选中状态 + * @param position 列表位置 + * @param checked 是否选中 + */ + public void setCheckedItem(final int position, final boolean checked) { + mSelectedIndex.put(position, checked); // 更新选中状态 + notifyDataSetChanged(); // 通知视图更新 + } + + /** + * 检查是否处于多选模式 + * @return true表示正在多选模式 + */ + public boolean isInChoiceMode() { + return mChoiceMode; + } + + /** + * 设置多选模式状态 + * @param mode true进入多选模式,false退出 + */ + public void setChoiceMode(boolean mode) { + mSelectedIndex.clear(); // 清除所有选中状态 + mChoiceMode = mode; // 更新模式状态 + } + + /** + * 全选/取消全选所有笔记项 + * @param checked true全选,false取消全选 + */ + public void selectAll(boolean checked) { + Cursor cursor = getCursor(); + // 遍历所有项 + for (int i = 0; i < getCount(); i++) { + if (cursor.moveToPosition(i)) { + // 只处理笔记类型(不包含文件夹) + if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { + setCheckedItem(i, checked); // 设置选中状态 + } + } + } + } + + /** + * 获取所有选中项的ID集合 + * @return 选中项的ID集合 + */ + public HashSet getSelectedItemIds() { + HashSet itemSet = new HashSet<>(); + // 遍历所有选中项 + for (Integer position : mSelectedIndex.keySet()) { + if (Boolean.TRUE.equals(mSelectedIndex.get(position))) { + long id = getItemId(position); // 获取项ID + // 检查是否为根文件夹(不应该被选中) + if (id == Notes.ID_ROOT_FOLDER) { + Log.d(TAG, "Wrong item id, should not happen"); + } else { + itemSet.add(id); // 添加到结果集 + } + } + } + return itemSet; + } + + /** + * 获取选中项关联的小部件属性集合 + * @return 小部件属性集合,可能为null + */ + public HashSet getSelectedWidget() { + HashSet itemSet = new HashSet<>(); + // 遍历所有选中项 + for (Integer position : mSelectedIndex.keySet()) { + if (Boolean.TRUE.equals(mSelectedIndex.get(position))) { + Cursor c = (Cursor) getItem(position); // 获取项数据 + if (c != null) { + AppWidgetAttribute widget = new AppWidgetAttribute(); + NoteItemData item = new NoteItemData(mContext, c); + widget.widgetId = item.getWidgetId(); // 设置小部件ID + widget.widgetType.set(item.getWidgetType()); // 设置小部件类型 + itemSet.add(widget); // 添加到结果集 + /* + * 注意:不要在此处关闭cursor,只有适配器可以关闭它 + */ + } else { + Log.e(TAG, "Invalid cursor"); + return null; // 遇到错误返回null + } + } + } + return itemSet; + } + + /** + * 获取当前选中项数量 + * @return 选中项计数 + */ + public int getSelectedCount() { + Collection values = mSelectedIndex.values(); + Iterator iter = values.iterator(); + int count = 0; + // 统计选中状态为true的数量 + while (iter.hasNext()) { + if (iter.next()) { + count++; + } + } + return count; + } + + /** + * 检查是否所有项都被选中 + * @return true表示全部选中 + */ + public boolean isAllSelected() { + int checkedCount = getSelectedCount(); + // 选中数量不为0且等于总笔记数量 + return (checkedCount != 0 && checkedCount == mNotesCount); + } + + /** + * 检查指定位置是否被选中 + * @param position 要检查的位置 + * @return true表示该位置被选中 + */ + public boolean isSelectedItem(final int position) { + if (null == mSelectedIndex.get(position)) { + return false; // 未记录的位置默认为未选中 + } + return Boolean.TRUE.equals(mSelectedIndex.get(position)); + } + + /** + * 当内容变化时的回调 + * 重新计算笔记数量 + */ + @Override + protected void onContentChanged() { + super.onContentChanged(); + calcNotesCount(); // 更新笔记计数 + } + + /** + * 切换游标时的回调 + * @param cursor 新游标 + */ + @Override + public void changeCursor(Cursor cursor) { + super.changeCursor(cursor); + calcNotesCount(); // 更新笔记计数 + } + + /** + * 计算笔记数量(不包括文件夹) + */ + private void calcNotesCount() { + mNotesCount = 0; + // 遍历所有项 + for (int i = 0; i < getCount(); i++) { + Cursor c = (Cursor) getItem(i); + if (c != null) { + // 只统计笔记类型(TYPE_NOTE) + if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { + mNotesCount++; // 计数增加 + } + } else { + Log.e(TAG, "Invalid cursor"); + return; // 遇到错误提前退出 + } + } + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListItem.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListItem.java new file mode 100644 index 0000000..01b7cf4 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesListItem.java @@ -0,0 +1,183 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 声明当前Java文件所在的包路径(net.micode.notes.ui 表示笔记应用的UI层) +package net.micode.notes.ui; +// Android注解库,@SuppressLint用于忽略特定Lint警告 +import android.annotation.SuppressLint; +// Android上下文类,提供应用环境信息和访问系统服务 +import android.content.Context; +// Android日期/时间格式化工具类,用于友好显示时间 +import android.text.format.DateUtils; +// Android基础视图类,所有UI组件的基类 +import android.view.View; +// 复选框控件,用于多选操作 +import android.widget.CheckBox; +// 图片视图控件,用于显示图标 +import android.widget.ImageView; +// 线性布局容器,用于水平或垂直排列子视图 +import android.widget.LinearLayout; +// 文本视图控件,用于显示文字内容 +import android.widget.TextView; +// 导入应用R资源文件(自动生成,包含所有资源ID) +import net.micode.notes.R; +// 导入笔记数据模型和常量定义 +import net.micode.notes.data.Notes; +// 导入数据工具类,提供数据处理方法 +import net.micode.notes.tool.DataUtils; +// 导入笔记项背景资源定义类 +import net.micode.notes.tool.ResourceParser.NoteItemBgResources; + +/** + * 自定义笔记列表项视图组件 + * 继承自LinearLayout,展示单条笔记/文件夹的完整信息 + */ +public class NotesListItem extends LinearLayout { + // 提醒图标(显示时钟或通话记录图标) + private final ImageView mAlert; + // 标题/内容文本视图 + private final TextView mTitle; + // 时间文本视图(显示相对时间) + private final TextView mTime; + // 通话记录联系人名称(仅通话记录可见) + private final TextView mCallName; + // 当前绑定的笔记数据对象 + private NoteItemData mItemData; + // 多选模式下的复选框 + private final CheckBox mCheckBox; + + /** + * 构造函数 + * @param context 上下文环境 + */ + public NotesListItem(Context context) { + super(context); + // 加载布局文件(R.layout.note_item )到当前视图 + inflate(context, R.layout.note_item, this); + + // 初始化视图组件 + mAlert = findViewById(R.id.iv_alert_icon); // 提醒图标 + mTitle = findViewById(R.id.tv_title); // 标题文本 + mTime = findViewById(R.id.tv_time); // 时间文本 + mCallName = findViewById(R.id.tv_name); // 联系人名称 + mCheckBox = findViewById(android.R.id.checkbox);// 多选复选框 + } + + /** + * 绑定数据到视图 + * @param context 上下文 + * @param data 笔记数据对象 + * @param choiceMode 是否处于多选模式 + * @param checked 是否被选中 + */ + @SuppressLint("SetTextI18n") // 忽略国际化的Lint警告 + public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { + // 处理多选模式显示逻辑 + if (choiceMode && data.getType() == Notes.TYPE_NOTE) { + mCheckBox.setVisibility(View.VISIBLE); // 显示复选框 + mCheckBox.setChecked(checked); // 设置选中状态 + } else { + mCheckBox.setVisibility(View.GONE); // 隐藏复选框 + } + + mItemData = data; // 保存数据引用 + + // 判断笔记类型并设置不同显示样式 + if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { + // 通话记录文件夹的特殊处理 + mCallName.setVisibility(View.GONE); // 隐藏联系人名称 + mAlert.setVisibility(View.VISIBLE); // 显示提醒图标 + // 设置标题样式和内容 + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); + mTitle.setText(context.getString(R.string.call_record_folder_name) + + context.getString(R.string.format_folder_files_count, data.getNotesCount())); + mAlert.setImageResource(R.drawable.call_record); // 设置通话记录图标 + } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { + // 通话记录笔记的特殊处理 + mCallName.setVisibility(View.VISIBLE); // 显示联系人名称 + mCallName.setText(data.getCallName()); // 设置联系人名称 + // 设置次级标题样式 + mTitle.setTextAppearance(context, R.style.TextAppearanceSecondaryItem); + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); // 设置内容摘要 + // 处理提醒图标显示 + if (data.hasAlert()) { + mAlert.setImageResource(R.drawable.clock); // 设置时钟图标 + mAlert.setVisibility(View.VISIBLE); + } else { + mAlert.setVisibility(View.GONE); + } + } else { + // 普通笔记/文件夹的处理 + mCallName.setVisibility(View.GONE); // 隐藏联系人名称 + // 设置主标题样式 + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); + + if (data.getType() == Notes.TYPE_FOLDER) { + // 文件夹的特殊处理 + mTitle.setText(data.getSnippet() + context.getString(R.string.format_folder_files_count, + data.getNotesCount())); // 显示文件夹名+笔记数 + mAlert.setVisibility(View.GONE); // 隐藏提醒图标 + } else { + // 普通笔记的处理 + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); // 设置内容摘要 + // 处理提醒图标显示 + if (data.hasAlert()) { + mAlert.setImageResource(R.drawable.clock); // 设置时钟图标 + mAlert.setVisibility(View.VISIBLE); + } else { + mAlert.setVisibility(View.GONE); + } + } + } + + // 设置相对时间显示(如"2分钟前") + mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); + + // 设置背景样式 + setBackground(data); + } + + /** + * 根据笔记位置设置不同背景样式 + * @param data 笔记数据对象 + */ + private void setBackground(NoteItemData data) { + int id = data.getBgColorId(); // 获取背景颜色ID + if (data.getType() == Notes.TYPE_NOTE) { + // 笔记项的背景处理 + if (data.isSingle() || data.isOneFollowingFolder()) { + // 单一项或紧接文件夹的项 + setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); + } else if (data.isLast()) { + // 最后一项 + setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); + } else if (data.isFirst() || data.isMultiFollowingFolder()) { + // 第一项或跟随多文件夹的项 + setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); + } else { + // 普通中间项 + setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); + } + } else { + // 文件夹项的背景 + setBackgroundResource(NoteItemBgResources.getFolderBgRes()); + } + } + + /** + * 获取当前绑定的笔记数据 + * @return 笔记数据对象 + */ + public NoteItemData getItemData() { + return mItemData; + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesPreferenceActivity.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesPreferenceActivity.java new file mode 100644 index 0000000..e2c529f --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NotesPreferenceActivity.java @@ -0,0 +1,452 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ + +// 声明当前Java文件的包路径(该Activity位于notes应用的ui包下) +package net.micode.notes.ui; + +// Android账户管理相关类 +import android.accounts.Account; // 账户基本类 +import android.accounts.AccountManager; // 账户管理系统服务 + +// Android基础UI组件 +// 顶部操作栏 +import android.app.AlertDialog; // 提示对话框 +import android.content.BroadcastReceiver; // 广播接收器基类 +import android.content.ContentValues; // 键值对存储(用于数据库操作) + +// Android核心组件类 +import android.content.Context; // 应用上下文 +// 对话框按钮回调接口 +import android.content.Intent; // 意图(用于Activity跳转) +import android.content.IntentFilter; // 意图过滤器(广播相关) +import android.content.SharedPreferences; // 轻量级数据存储 + +// Android基础框架类 +import android.os.Bundle; // 数据存储Bundle + +// 设置相关类(Preference) +import android.preference.Preference; // 设置项基类 +// 设置项点击监听 +import android.preference.PreferenceActivity; // 设置Activity基类 +import android.preference.PreferenceCategory; // 设置分组容器 + +// Android文本工具类 +import android.text.TextUtils; // 文本处理工具 +import android.text.format.DateFormat; // 日期格式化 + +// Android视图相关类 +import android.view.LayoutInflater; // 布局加载器 +// 菜单 +import android.view.MenuItem; // 菜单项 +import android.view.View; // 基础视图类 +import android.widget.Button; // 按钮控件 +import android.widget.TextView; // 文本控件 +import android.widget.Toast; // Toast提示 + +// 应用资源类 +import net.micode.notes.R; // 自动生成的资源ID类 + +// 本地数据模型 +import net.micode.notes.data.Notes; // 笔记数据常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记数据列名 + +// 同步服务相关 +import net.micode.notes.gtask.remote.GTaskSyncService; // 谷歌任务同步服务 + +// Java工具类 +import java.util.Objects; // 对象工具类(判空等) + +/** + * 笔记应用的设置Activity,继承自PreferenceActivity + * 功能: + * 1. 管理Google账户同步设置 + * 2. 处理背景颜色偏好设置 + * 3. 控制立即同步/取消同步操作 + */ +public class NotesPreferenceActivity extends PreferenceActivity { + // 偏好设置文件名 + public static final String PREFERENCE_NAME = "notes_preferences"; + // 同步账户名的偏好设置键 + public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name"; + // 最后同步时间的偏好设置键 + public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time"; + // 背景颜色设置的偏好设置键 + public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear"; + // 同步账户设置项的键 + private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key"; + // 账户权限过滤键 + private static final String AUTHORITIES_FILTER_KEY = "authorities"; + + // 账户设置分类 + private PreferenceCategory mAccountCategory; + // 同步服务广播接收器 + private GTaskReceiver mReceiver; + // 原始账户数组(用于检测新增账户) + private Account[] mOriAccounts; + // 是否添加了新账户的标志 + private boolean mHasAddedAccount; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + + // 设置ActionBar显示返回箭头 + Objects.requireNonNull(getActionBar()).setDisplayHomeAsUpEnabled(true); + + // 从XML加载偏好设置 + addPreferencesFromResource(R.xml.preferences); + // 获取账户设置分类 + mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); + // 初始化广播接收器 + mReceiver = new GTaskReceiver(); + // 创建意图过滤器,监听同步服务广播 + IntentFilter filter = new IntentFilter(); + filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); + registerReceiver(mReceiver, filter); + + // 初始化原始账户数组 + mOriAccounts = null; + // 加载设置页面的顶部自定义布局 + View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); + + } + + @Override + protected void onResume() { + super.onResume(); + + // 如果用户添加了新账户,需要自动设置同步账户 + if (mHasAddedAccount) { + Account[] accounts = getGoogleAccounts(); + // 比较新旧账户数量 + if (mOriAccounts != null && accounts.length > mOriAccounts.length) { + // 查找新添加的账户 + for (Account accountNew : accounts) { + boolean found = false; + for (Account accountOld : mOriAccounts) { + if (TextUtils.equals(accountOld.name, accountNew.name)) { + found = true; + break; + } + } + // 找到新账户后设置同步账户 + if (!found) { + setSyncAccount(accountNew.name); + break; + } + } + } + } + + // 刷新UI状态 + refreshUI(); + } + + @Override + protected void onDestroy() { + // 注销广播接收器 + if (mReceiver != null) { + unregisterReceiver(mReceiver); + } + super.onDestroy(); + } + + /** + * 加载账户偏好设置项 + */ + private void loadAccountPreference() { + // 清空现有设置项 + mAccountCategory.removeAll(); + + // 创建账户设置项 + Preference accountPref = new Preference(this); + final String defaultAccount = getSyncAccountName(this); + // 设置标题和摘要 + accountPref.setTitle(getString(R.string.preferences_account_title)); + accountPref.setSummary(getString(R.string.preferences_account_summary)); + // 设置点击监听器 + accountPref.setOnPreferenceClickListener(preference -> { + if (!GTaskSyncService.isSyncing()) { + // 首次设置账户 + if (TextUtils.isEmpty(defaultAccount)) { + showSelectAccountAlertDialog(); + } else { + // 已有账户时显示确认对话框 + showChangeAccountConfirmAlertDialog(); + } + } else { + // 同步进行中不能更改账户 + Toast.makeText(NotesPreferenceActivity.this, + R.string.preferences_toast_cannot_change_account, + Toast.LENGTH_SHORT).show(); + } + return true; + }); + + // 添加设置项到分类 + mAccountCategory.addPreference(accountPref); + } + + /** + * 加载同步按钮状态 + */ + private void loadSyncButton() { + Button syncButton = findViewById(R.id.preference_sync_button); + TextView lastSyncTimeView = findViewById(R.id.prefenerece_sync_status_textview); + + // 根据同步状态设置按钮文本和点击事件 + if (GTaskSyncService.isSyncing()) { + syncButton.setText(getString(R.string.preferences_button_sync_cancel)); + syncButton.setOnClickListener(v -> GTaskSyncService.cancelSync(NotesPreferenceActivity.this)); + } else { + syncButton.setText(getString(R.string.preferences_button_sync_immediately)); + syncButton.setOnClickListener(v -> GTaskSyncService.startSync(NotesPreferenceActivity.this)); + } + // 只有设置了同步账户才能点击同步按钮 + syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this))); + + // 设置最后同步时间显示 + if (GTaskSyncService.isSyncing()) { + lastSyncTimeView.setText(GTaskSyncService.getProgressString()); + lastSyncTimeView.setVisibility(View.VISIBLE); + } else { + long lastSyncTime = getLastSyncTime(this); + if (lastSyncTime != 0) { + lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time, + DateFormat.format(getString(R.string.preferences_last_sync_time_format), + lastSyncTime))); + lastSyncTimeView.setVisibility(View.VISIBLE); + } else { + lastSyncTimeView.setVisibility(View.GONE); + } + } + } + + /** + * 刷新UI状态 + */ + private void refreshUI() { + loadAccountPreference(); + loadSyncButton(); + } + + /** + * 显示选择账户对话框 + */ + private void showSelectAccountAlertDialog() { + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); + + // 加载自定义标题布局 + View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); + TextView titleTextView = titleView.findViewById(R.id.account_dialog_title); + titleTextView.setText(getString(R.string.preferences_dialog_select_account_title)); + TextView subtitleTextView = titleView.findViewById(R.id.account_dialog_subtitle); + subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); + + dialogBuilder.setCustomTitle(titleView); + dialogBuilder.setPositiveButton(null, null); + + // 获取当前所有Google账户 + Account[] accounts = getGoogleAccounts(); + String defAccount = getSyncAccountName(this); + + mOriAccounts = accounts; + mHasAddedAccount = false; + + if (accounts.length > 0) { + // 构建账户列表项 + CharSequence[] items = new CharSequence[accounts.length]; + final CharSequence[] itemMapping = items; + int checkedItem = -1; + int index = 0; + for (Account account : accounts) { + // 标记当前已选账户 + if (TextUtils.equals(account.name, defAccount)) { + checkedItem = index; + } + items[index++] = account.name; + } + // 设置单选列表 + dialogBuilder.setSingleChoiceItems(items, checkedItem, + (dialog, which) -> { + setSyncAccount(itemMapping[which].toString()); + dialog.dismiss(); + refreshUI(); + }); + } + + // 添加"添加账户"视图 + View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null); + dialogBuilder.setView(addAccountView); + + final AlertDialog dialog = dialogBuilder.show(); + addAccountView.setOnClickListener(v -> { + mHasAddedAccount = true; + // 启动添加账户设置界面 + Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); + intent.putExtra(AUTHORITIES_FILTER_KEY, new String[]{"gmail-ls"}); + startActivityForResult(intent, -1); + dialog.dismiss(); + }); + } + + /** + * 显示更改账户确认对话框 + */ + private void showChangeAccountConfirmAlertDialog() { + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); + + // 加载自定义标题布局 + View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); + TextView titleTextView = titleView.findViewById(R.id.account_dialog_title); + titleTextView.setText(getString(R.string.preferences_dialog_change_account_title, + getSyncAccountName(this))); + TextView subtitleTextView = titleView.findViewById(R.id.account_dialog_subtitle); + subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg)); + dialogBuilder.setCustomTitle(titleView); + + // 设置操作菜单项 + CharSequence[] menuItemArray = new CharSequence[]{ + getString(R.string.preferences_menu_change_account), + getString(R.string.preferences_menu_remove_account), + getString(R.string.preferences_menu_cancel) + }; + dialogBuilder.setItems(menuItemArray, (dialog, which) -> { + if (which == 0) { + showSelectAccountAlertDialog(); + } else if (which == 1) { + removeSyncAccount(); + refreshUI(); + } + }); + dialogBuilder.show(); + } + + /** + * 获取所有Google账户 + */ + private Account[] getGoogleAccounts() { + AccountManager accountManager = AccountManager.get(this); + return accountManager.getAccountsByType("com.google"); + } + + /** + * 设置同步账户 + */ + private void setSyncAccount(String account) { + if (!getSyncAccountName(this).equals(account)) { + // 保存账户到偏好设置 + SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = settings.edit(); + editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, Objects.requireNonNullElse(account, "")); + editor.apply(); + + // 重置最后同步时间 + setLastSyncTime(this, 0); + + // 在新线程中清除本地同步信息 + new Thread(() -> { + ContentValues values = new ContentValues(); + values.put(NoteColumns.GTASK_ID, ""); + values.put(NoteColumns.SYNC_ID, 0); + getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); + }).start(); + + Toast.makeText(this, + getString(R.string.preferences_toast_success_set_accout, account), + Toast.LENGTH_SHORT).show(); + } + } + + /** + * 移除同步账户 + */ + private void removeSyncAccount() { + SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = settings.edit(); + // 移除账户相关设置 + if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) { + editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME); + } + if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) { + editor.remove(PREFERENCE_LAST_SYNC_TIME); + } + editor.apply(); + + // 在新线程中清除本地同步信息 + new Thread(() -> { + ContentValues values = new ContentValues(); + values.put(NoteColumns.GTASK_ID, ""); + values.put(NoteColumns.SYNC_ID, 0); + getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); + }).start(); + } + + /** + * 静态方法:获取当前同步账户名 + */ + public static String getSyncAccountName(Context context) { + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, + Context.MODE_PRIVATE); + return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); + } + + /** + * 静态方法:设置最后同步时间 + */ + public static void setLastSyncTime(Context context, long time) { + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = settings.edit(); + editor.putLong(PREFERENCE_LAST_SYNC_TIME, time); + editor.apply(); + } + + /** + * 静态方法:获取最后同步时间 + */ + public static long getLastSyncTime(Context context) { + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, + Context.MODE_PRIVATE); + return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0); + } + + /** + * 同步服务广播接收器 + */ + private class GTaskReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + // 收到同步状态变化时刷新UI + refreshUI(); + if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) { + TextView syncStatus = findViewById(R.id.prefenerece_sync_status_textview); + syncStatus.setText(intent + .getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); + } + } + } + + /** + * 处理选项菜单项点击 + */ + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + // 点击返回箭头时回到笔记列表 + Intent intent = new Intent(this, NotesListActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + return true; + } + return false; + } +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider.java b/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider.java new file mode 100644 index 0000000..1d46efd --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider.java @@ -0,0 +1,208 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.widget; + +// 导入Android相关类 +import android.app.PendingIntent; // 延时意图 +import android.appwidget.AppWidgetManager; // 小部件管理器 +import android.appwidget.AppWidgetProvider; // 小部件提供者基类 +import android.content.ContentValues; // 内容值对象 +import android.content.Context; // 上下文对象 +import android.content.Intent; // 意图对象 +import android.database.Cursor; // 数据库游标 +import android.util.Log; // 日志工具 +import android.widget.RemoteViews; // 远程视图(用于小部件UI) + +// 导入项目资源 +import net.micode.notes.R; // 项目资源文件 +// 导入项目数据类 +import net.micode.notes.data.Notes; // 笔记常量 +import net.micode.notes.data.Notes.NoteColumns; // 笔记列定义 +// 导入工具类 +import net.micode.notes.tool.ResourceParser; // 资源解析工具 +// 导入Activity类 +import net.micode.notes.ui.NoteEditActivity; // 笔记编辑界面 +import net.micode.notes.ui.NotesListActivity; // 笔记列表界面 + +/** + * 抽象笔记小部件提供者类 + * 提供笔记小部件的基本功能,具体样式由子类实现 + */ +public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 笔记查询投影列定义 + public static final String [] PROJECTION = new String [] { + NoteColumns.ID, // 笔记ID + NoteColumns.BG_COLOR_ID, // 背景颜色ID + NoteColumns.SNIPPET // 笔记摘要 + }; + + // 投影列索引常量 + public static final int COLUMN_ID = 0; // ID列索引 + public static final int COLUMN_BG_COLOR_ID = 1; // 背景颜色列索引 + public static final int COLUMN_SNIPPET = 2; // 摘要列索引 + + private static final String TAG = "NoteWidgetProvider"; // 日志标签 + + /** + * 当小部件被删除时调用 + * @param context 上下文对象 + * @param appWidgetIds 被删除的小部件ID数组 + */ + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + // 创建内容值对象,用于更新数据库 + ContentValues values = new ContentValues(); + // 将笔记的小部件ID设为无效值 + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + + // 遍历所有被删除的小部件ID + for (int appWidgetId : appWidgetIds) { + // 更新数据库,解除笔记与小部件的关联 + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", // 更新条件 + new String[]{String.valueOf(appWidgetId)}); // 参数值 + } + } + + /** + * 获取与小部件关联的笔记信息 + * @param context 上下文对象 + * @param widgetId 小部件ID + * @return 包含笔记信息的游标对象 + */ + private Cursor getNoteWidgetInfo(Context context, int widgetId) { + // 查询数据库获取关联的笔记信息 + return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, + PROJECTION, // 查询列 + // 查询条件:匹配小部件ID且不在回收站中 + NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", + new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) }, + null); // 无排序 + } + + /** + * 更新小部件(公开方法) + * @param context 上下文对象 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 需要更新的小部件ID数组 + */ + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + update(context, appWidgetManager, appWidgetIds, false); // 默认非隐私模式 + } + + /** + * 更新小部件(私有方法) + * @param context 上下文对象 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 需要更新的小部件ID数组 + * @param privacyMode 是否为隐私模式 + */ + private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, + boolean privacyMode) { + // 遍历所有需要更新的小部件 + /* + 生成启动宿主Activity的延时意图 + */ + for (int appWidgetId : appWidgetIds) { + // 检查小部件ID是否有效 + if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { + int bgId = ResourceParser.getDefaultBgId(context); // 获取默认背景ID + String snippet; // 笔记摘要 + + // 创建编辑笔记的Intent + Intent intent = new Intent(context, NoteEditActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 设置启动模式 + // 添加小部件ID到Intent + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetId); + // 添加小部件类型到Intent + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); + + // 获取关联的笔记信息 + Cursor c = getNoteWidgetInfo(context, appWidgetId); + if (c != null && c.moveToFirst()) { + // 检查是否有多条笔记关联同一个小部件(异常情况) + if (c.getCount() > 1) { + Log.e(TAG, "Multiple message with same widget id:" + appWidgetId); + c.close(); + return; + } + // 从游标获取笔记信息 + snippet = c.getString(COLUMN_SNIPPET); // 获取摘要 + bgId = c.getInt(COLUMN_BG_COLOR_ID); // 获取背景ID + intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); // 添加笔记ID + intent.setAction(Intent.ACTION_VIEW); // 设置操作为查看 + } else { + // 如果没有关联的笔记,显示默认文本 + snippet = context.getResources().getString(R.string.widget_havenot_content); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // 设置操作为新建 + } + + // 关闭游标 + if (c != null) { + c.close(); + } + + // 创建远程视图 + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); + // 设置背景图片 + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); + // 添加背景ID到Intent + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); + + /* + 生成启动宿主Activity的延时意图 + */ + PendingIntent pendingIntent; + if (privacyMode) { + // 隐私模式下显示特殊文本 + rv.setTextViewText(R.id.widget_text, + context.getString(R.string.widget_under_visit_mode)); + // 点击跳转到笔记列表 + pendingIntent = PendingIntent.getActivity(context, appWidgetId, new Intent( + context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); + } else { + // 正常模式显示笔记摘要 + rv.setTextViewText(R.id.widget_text, snippet); + // 点击跳转到笔记编辑界面 + pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + + // 设置点击事件 + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); + // 更新小部件 + appWidgetManager.updateAppWidget(appWidgetId, rv); + } + } + } + + /** + * 抽象方法:获取背景资源ID + * @param bgId 背景颜色ID + * @return 对应的背景资源ID + */ + protected abstract int getBgResourceId(int bgId); + + /** + * 抽象方法:获取布局ID + * @return 小部件布局资源ID + */ + protected abstract int getLayoutId(); + + /** + * 抽象方法:获取小部件类型 + * @return 小部件类型标识 + */ + protected abstract int getWidgetType(); +} diff --git a/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_2x.java b/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..9dccd4f --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_2x.java @@ -0,0 +1,77 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径 +package net.micode.notes.widget; + +// 导入Android相关类 +import android.appwidget.AppWidgetManager; // 小部件管理类 +import android.content.Context; // 上下文对象 + +// 导入项目资源 +import net.micode.notes.R; // 项目资源文件 +// 导入数据常量 +import net.micode.notes.data.Notes; // 笔记相关常量 +// 导入资源解析工具 +import net.micode.notes.tool.ResourceParser; // 资源解析工具类 + +/** + * 2x大小笔记小部件提供者类 + * 继承自NoteWidgetProvider,实现2x尺寸小部件的具体功能 + */ +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + + /** + * 小部件更新回调方法 + * 当小部件需要更新时由系统调用 + * + * @param context 上下文对象 + * @param appWidgetManager 小部件管理器 + * @param appWidgetIds 需要更新的小部件ID数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的update方法进行基础更新 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取小部件布局资源ID + * + * @return 返回2x小部件的布局资源ID + */ + @Override + protected int getLayoutId() { + return R.layout.widget_2x; // 使用widget_2x布局文件 + } + + /** + * 根据背景ID获取对应的2x小部件背景资源 + * + * @param bgId 背景颜色ID(如ResourceParser.YELLOW等) + * @return 对应的2x小部件背景资源ID + */ + @Override + protected int getBgResourceId(int bgId) { + // 通过ResourceParser获取2x尺寸的小部件背景资源 + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + /** + * 获取小部件类型标识 + * + * @return 返回2x小部件的类型常量 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; // 返回2x小部件类型常量 + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_4x.java b/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..28a49a8 --- /dev/null +++ b/src/mi_note/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_4x.java @@ -0,0 +1,82 @@ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net) + * 根据 Apache 许可证 2.0 版本("许可证")授权; + * 除非符合许可证的规定,否则不得使用本文件。 + * 您可以从以下网址获取许可证副本: + * http://www.apache.org/licenses/LICENSE-2.0 + * 除非适用法律要求或书面同意,本软件按"原样"分发, + * 没有任何明示或暗示的保证或条件。 + * 详见许可证中规定的权限和限制。 + * (注:这是一份标准的Apache许可证2.0版本的开源声明) + */ +// 定义包路径(说明该类所属的Java包) +package net.micode.notes.widget; + +// 导入Android系统类库 +import android.appwidget.AppWidgetManager; // 小部件管理类,用于管理App Widgets +import android.content.Context; // 上下文类,提供应用环境信息 + +// 导入项目资源 +import net.micode.notes.R; // 自动生成的R.java 文件,包含所有资源ID +// 导入项目数据类 +import net.micode.notes.data.Notes; // 笔记应用的常量定义 +// 导入工具类 +import net.micode.notes.tool.ResourceParser; // 资源解析工具类 + +/** + * 4x尺寸笔记小部件提供者类 + * 继承自NoteWidgetProvider抽象基类,专门用于4x大小的小部件实现 + */ +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + + /** + * 当小部件需要更新时调用的方法 + * 覆盖父类方法,提供4x小部件的更新逻辑 + * + * @param context 上下文对象,提供应用环境信息 + * @param appWidgetManager 小部件管理器,用于更新小部件 + * @param appWidgetIds 需要更新的小部件ID数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 直接调用父类的update方法完成基础更新操作 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取4x小部件的布局资源ID + * 实现父类抽象方法,返回4x小部件特有的布局文件 + * + * @return int 返回4x小部件的布局资源ID(R.layout.widget_4x ) + */ + @Override + protected int getLayoutId() { + // 返回4x小部件专用的布局文件ID + return R.layout.widget_4x; + } + + /** + * 根据背景颜色ID获取对应的4x小部件背景资源 + * 实现父类抽象方法,返回4x尺寸的背景资源 + * + * @param bgId 背景颜色ID(来自ResourceParser的颜色常量) + * @return int 返回对应的4x尺寸背景图片资源ID + */ + @Override + protected int getBgResourceId(int bgId) { + // 通过ResourceParser工具类获取4x尺寸的背景资源 + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + /** + * 获取小部件类型标识 + * 实现父类抽象方法,返回4x小部件的类型常量 + * + * @return int 返回4x小部件的类型常量(Notes.TYPE_WIDGET_4X) + */ + @Override + protected int getWidgetType() { + // 返回4x小部件的类型定义常量 + return Notes.TYPE_WIDGET_4X; + } +} \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/color/primary_text_dark.xml b/src/mi_note/app/src/main/res/color/primary_text_dark.xml new file mode 100644 index 0000000..7c85459 --- /dev/null +++ b/src/mi_note/app/src/main/res/color/primary_text_dark.xml @@ -0,0 +1,22 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/color/secondary_text_dark.xml b/src/mi_note/app/src/main/res/color/secondary_text_dark.xml new file mode 100644 index 0000000..c1c2384 --- /dev/null +++ b/src/mi_note/app/src/main/res/color/secondary_text_dark.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/bg_btn_set_color.png b/src/mi_note/app/src/main/res/drawable-hdpi/bg_btn_set_color.png new file mode 100644 index 0000000..5eb5d44 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/bg_btn_set_color.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/bg_color_btn_mask.png b/src/mi_note/app/src/main/res/drawable-hdpi/bg_color_btn_mask.png new file mode 100644 index 0000000..100db77 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/bg_color_btn_mask.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/call_record.png b/src/mi_note/app/src/main/res/drawable-hdpi/call_record.png new file mode 100644 index 0000000..fb88ca4 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/call_record.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/clock.png b/src/mi_note/app/src/main/res/drawable-hdpi/clock.png new file mode 100644 index 0000000..5f2ae9a Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/clock.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/delete.png b/src/mi_note/app/src/main/res/drawable-hdpi/delete.png new file mode 100644 index 0000000..643de3e Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/delete.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/dropdown_icon.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/dropdown_icon.9.png new file mode 100644 index 0000000..5525025 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/dropdown_icon.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_blue.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_blue.9.png new file mode 100644 index 0000000..55a1856 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_blue.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_green.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_green.9.png new file mode 100644 index 0000000..2cb2d60 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_green.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_red.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_red.9.png new file mode 100644 index 0000000..bae944a Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_red.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_blue.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_blue.9.png new file mode 100644 index 0000000..96e6092 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_blue.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_green.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_green.9.png new file mode 100644 index 0000000..08d8644 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_green.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_red.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_red.9.png new file mode 100644 index 0000000..9c430e5 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_red.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_white.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_white.9.png new file mode 100644 index 0000000..19e8d95 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_white.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_yellow.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_yellow.9.png new file mode 100644 index 0000000..bf8f580 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_title_yellow.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_white.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_white.9.png new file mode 100644 index 0000000..918f7a6 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_white.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/edit_yellow.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/edit_yellow.9.png new file mode 100644 index 0000000..10cb642 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/edit_yellow.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/font_large.png b/src/mi_note/app/src/main/res/drawable-hdpi/font_large.png new file mode 100644 index 0000000..78cf2e6 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/font_large.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/font_normal.png b/src/mi_note/app/src/main/res/drawable-hdpi/font_normal.png new file mode 100644 index 0000000..9de7ced Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/font_normal.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/font_size_selector_bg.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/font_size_selector_bg.9.png new file mode 100644 index 0000000..be8e64c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/font_size_selector_bg.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/font_small.png b/src/mi_note/app/src/main/res/drawable-hdpi/font_small.png new file mode 100644 index 0000000..d3ff104 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/font_small.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/font_super.png b/src/mi_note/app/src/main/res/drawable-hdpi/font_super.png new file mode 100644 index 0000000..85b13a1 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/font_super.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/icon_app.png b/src/mi_note/app/src/main/res/drawable-hdpi/icon_app.png new file mode 100644 index 0000000..418aadc Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/icon_app.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_background.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_background.png new file mode 100644 index 0000000..087e1f9 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_background.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_down.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_down.9.png new file mode 100644 index 0000000..b88eebf Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_down.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_middle.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_middle.9.png new file mode 100644 index 0000000..96b1c8b Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_middle.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_single.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_single.9.png new file mode 100644 index 0000000..d7e7206 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_single.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_up.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_up.9.png new file mode 100644 index 0000000..632e88c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_blue_up.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_folder.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_folder.9.png new file mode 100644 index 0000000..829f61b Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_folder.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_footer_bg.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_footer_bg.9.png new file mode 100644 index 0000000..5325c25 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_footer_bg.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_green_down.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_down.9.png new file mode 100644 index 0000000..64a39d9 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_down.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_green_middle.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_middle.9.png new file mode 100644 index 0000000..897325a Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_middle.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_green_single.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_single.9.png new file mode 100644 index 0000000..c83405f Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_single.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_green_up.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_up.9.png new file mode 100644 index 0000000..141f9e1 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_green_up.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_red_down.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_down.9.png new file mode 100644 index 0000000..4224309 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_down.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_red_middle.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_middle.9.png new file mode 100644 index 0000000..9988f17 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_middle.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_red_single.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_single.9.png new file mode 100644 index 0000000..587c348 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_single.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_red_up.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_up.9.png new file mode 100644 index 0000000..46b4757 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_red_up.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_white_down.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_down.9.png new file mode 100644 index 0000000..29f9d8c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_down.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_white_middle.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_middle.9.png new file mode 100644 index 0000000..77a4ab4 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_middle.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_white_single.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_single.9.png new file mode 100644 index 0000000..3e79189 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_single.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_white_up.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_up.9.png new file mode 100644 index 0000000..e23cd5c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_white_up.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_down.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_down.9.png new file mode 100644 index 0000000..31cfc1e Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_down.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_middle.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_middle.9.png new file mode 100644 index 0000000..b6549b2 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_middle.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_single.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_single.9.png new file mode 100644 index 0000000..3faf507 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_single.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_up.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_up.9.png new file mode 100644 index 0000000..4ae791c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/list_yellow_up.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/menu_delete.png b/src/mi_note/app/src/main/res/drawable-hdpi/menu_delete.png new file mode 100644 index 0000000..ccdfc4b Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/menu_delete.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/menu_move.png b/src/mi_note/app/src/main/res/drawable-hdpi/menu_move.png new file mode 100644 index 0000000..1140b71 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/menu_move.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/new_note_normal.png b/src/mi_note/app/src/main/res/drawable-hdpi/new_note_normal.png new file mode 100644 index 0000000..e24e0d1 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/new_note_normal.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/new_note_pressed.png b/src/mi_note/app/src/main/res/drawable-hdpi/new_note_pressed.png new file mode 100644 index 0000000..c748936 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/new_note_pressed.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/note_edit_color_selector_panel.png b/src/mi_note/app/src/main/res/drawable-hdpi/note_edit_color_selector_panel.png new file mode 100644 index 0000000..fc49552 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/note_edit_color_selector_panel.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/notification.png b/src/mi_note/app/src/main/res/drawable-hdpi/notification.png new file mode 100644 index 0000000..b13ab4a Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/notification.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/search_result.png b/src/mi_note/app/src/main/res/drawable-hdpi/search_result.png new file mode 100644 index 0000000..ff2befd Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/search_result.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/selected.png b/src/mi_note/app/src/main/res/drawable-hdpi/selected.png new file mode 100644 index 0000000..b889bef Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/selected.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/title_alert.png b/src/mi_note/app/src/main/res/drawable-hdpi/title_alert.png new file mode 100644 index 0000000..544ee9c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/title_alert.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/title_bar_bg.9.png b/src/mi_note/app/src/main/res/drawable-hdpi/title_bar_bg.9.png new file mode 100644 index 0000000..eb6bff0 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/title_bar_bg.9.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_blue.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_blue.png new file mode 100644 index 0000000..a1707f4 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_blue.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_green.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_green.png new file mode 100644 index 0000000..f86886c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_green.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_red.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_red.png new file mode 100644 index 0000000..0e66c29 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_red.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_white.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_white.png new file mode 100644 index 0000000..5f0619a Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_white.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_yellow.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_yellow.png new file mode 100644 index 0000000..12d1c2b Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_2x_yellow.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_blue.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_blue.png new file mode 100644 index 0000000..9183738 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_blue.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_green.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_green.png new file mode 100644 index 0000000..fa8b452 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_green.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_red.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_red.png new file mode 100644 index 0000000..62de074 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_red.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_white.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_white.png new file mode 100644 index 0000000..a37d67c Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_white.png differ diff --git a/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_yellow.png b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_yellow.png new file mode 100644 index 0000000..d7c5fa4 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable-hdpi/widget_4x_yellow.png differ diff --git a/src/mi_note/app/src/main/res/drawable/aguduo.png b/src/mi_note/app/src/main/res/drawable/aguduo.png new file mode 100644 index 0000000..5c4e647 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable/aguduo.png differ diff --git a/src/mi_note/app/src/main/res/drawable/anqila.png b/src/mi_note/app/src/main/res/drawable/anqila.png new file mode 100644 index 0000000..da59a49 Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable/anqila.png differ diff --git a/src/mi_note/app/src/main/res/drawable/ic_launcher_background.xml b/src/mi_note/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/src/mi_note/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mi_note/app/src/main/res/drawable/ic_launcher_foreground.xml b/src/mi_note/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/src/mi_note/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/drawable/new_note.xml b/src/mi_note/app/src/main/res/drawable/new_note.xml new file mode 100644 index 0000000..2154ebc --- /dev/null +++ b/src/mi_note/app/src/main/res/drawable/new_note.xml @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/src/mi_note/app/src/main/res/drawable/yao.png b/src/mi_note/app/src/main/res/drawable/yao.png new file mode 100644 index 0000000..a12c39e Binary files /dev/null and b/src/mi_note/app/src/main/res/drawable/yao.png differ diff --git a/src/mi_note/app/src/main/res/layout/account_dialog_title.xml b/src/mi_note/app/src/main/res/layout/account_dialog_title.xml new file mode 100644 index 0000000..7717112 --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/account_dialog_title.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/layout/activity_main.xml b/src/mi_note/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..86a5d97 --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/layout/add_account_text.xml b/src/mi_note/app/src/main/res/layout/add_account_text.xml new file mode 100644 index 0000000..c799178 --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/add_account_text.xml @@ -0,0 +1,32 @@ + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/layout/datetime_picker.xml b/src/mi_note/app/src/main/res/layout/datetime_picker.xml new file mode 100644 index 0000000..f10d592 --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/datetime_picker.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/layout/dialog_edit_text.xml b/src/mi_note/app/src/main/res/layout/dialog_edit_text.xml new file mode 100644 index 0000000..361b39a --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/dialog_edit_text.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/layout/folder_list_item.xml b/src/mi_note/app/src/main/res/layout/folder_list_item.xml new file mode 100644 index 0000000..77e8148 --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/folder_list_item.xml @@ -0,0 +1,29 @@ + + + + + + + + \ No newline at end of file diff --git a/src/mi_note/app/src/main/res/layout/note_edit.xml b/src/mi_note/app/src/main/res/layout/note_edit.xml new file mode 100644 index 0000000..94c5a1b --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/note_edit.xml @@ -0,0 +1,405 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mi_note/app/src/main/res/layout/note_edit_list_item.xml b/src/mi_note/app/src/main/res/layout/note_edit_list_item.xml new file mode 100644 index 0000000..a885f9c --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/note_edit_list_item.xml @@ -0,0 +1,39 @@ + + + + + + + + + + diff --git a/src/mi_note/app/src/main/res/layout/note_item.xml b/src/mi_note/app/src/main/res/layout/note_item.xml new file mode 100644 index 0000000..d541f6a --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/note_item.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mi_note/app/src/main/res/layout/note_list.xml b/src/mi_note/app/src/main/res/layout/note_list.xml new file mode 100644 index 0000000..e59734c --- /dev/null +++ b/src/mi_note/app/src/main/res/layout/note_list.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + +