From d3e775d5275184228f05778649ef4957ede49297 Mon Sep 17 00:00:00 2001 From: wolfplus Date: Tue, 6 May 2025 19:07:42 +0100 Subject: [PATCH] add --- android/app/build.gradle.kts | 4 +- .../yunqiinnovation/deepsound/MainActivity.kt | 44 +----- .../com/yunqiinnovation/deepsound/utils.kt | 144 ------------------ android/settings.gradle.kts | 5 +- 4 files changed, 7 insertions(+), 190 deletions(-) delete mode 100644 android/app/src/main/kotlin/com/yunqiinnovation/deepsound/utils.kt diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 8c29dfd84..fc2da7300 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -140,9 +140,7 @@ dependencies { // 添加legacy-support-v4依赖 implementation("androidx.legacy:legacy-support-v4:1.0.0") - // 添加 AndroidX Security 加密 SharedPreferences 依赖 - implementation("androidx.security:security-crypto:1.1.0-alpha06") - + } flutter { diff --git a/android/app/src/main/kotlin/com/yunqiinnovation/deepsound/MainActivity.kt b/android/app/src/main/kotlin/com/yunqiinnovation/deepsound/MainActivity.kt index d63ac7dbe..27f8d8498 100644 --- a/android/app/src/main/kotlin/com/yunqiinnovation/deepsound/MainActivity.kt +++ b/android/app/src/main/kotlin/com/yunqiinnovation/deepsound/MainActivity.kt @@ -13,56 +13,16 @@ import android.content.Context import android.os.Handler import android.os.Looper import com.yunqiinnovation.deepvoice_utils.FileLogger -import com.yunqiinnovation.ble_service.BleForegroundService -import com.yunqiinnovation.ble_service.BleManagerWrapper +import com.yunqiinnovation.ble_service.BleMediaService class MainActivity: FlutterActivity() { - private val TAG = "MainActivity" - - // 使用伴生对象存储配置信息,使其在整个应用中可访问 - companion object { - // 配置字典,替代原来的单独配置变量 - val configMap: MutableMap = mutableMapOf() - - // 安全存储相关常量 - const val KEY_AZURE_SPEECH_KEY = "azure_speech_key" - const val KEY_AZURE_SPEECH_REGION = "azure_speech_region" - const val KEY_VOLCANO_AI_API_KEY = "volcano_ai_api_key" - const val KEY_OPENAI_API_KEY = "openai_api_key" - const val KEY_OPENAI_BASE_URL = "openai_base_url" - const val KEY_OPENAI_MODEL = "openai_model" - const val KEY_MCP_SERVER_ENDPOINT = "mcp_server_endpoint" - const val KEY_VOLCANO_SPEECH_APP_ID = "volcano_speech_app_id" - const val KEY_VOLCANO_SPEECH_APP_TOKEN = "volcano_speech_app_token" - const val KEY_MCP_SERVER = "mcp_server" - const val KEY_SPOTIFY_CLIENT_ID = "spotify_client_id" - const val KEY_SPOTIFY_REDIRECT_URL = "spotify_redirect_url" - - // 获取配置的便捷方法 - fun getConfig(key: String, defaultValue: String = ""): String { - return configMap[key] ?: defaultValue - } - } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - - // 初始化FileLogger - FileLogger.init(applicationContext) - - FileLogger.d(TAG, "初始化完成") + } - /** - * 从安全存储中加载配置 - */ - private fun loadConfigFromSecureStorage() { - val data = Utils.loadAllSecureData(applicationContext) - configMap.clear() - configMap.putAll(data) - } - override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) diff --git a/android/app/src/main/kotlin/com/yunqiinnovation/deepsound/utils.kt b/android/app/src/main/kotlin/com/yunqiinnovation/deepsound/utils.kt deleted file mode 100644 index cf8d20831..000000000 --- a/android/app/src/main/kotlin/com/yunqiinnovation/deepsound/utils.kt +++ /dev/null @@ -1,144 +0,0 @@ -package com.yunqiinnovation.deepsound - -import android.content.Context -import androidx.security.crypto.EncryptedSharedPreferences -import androidx.security.crypto.MasterKey -import com.yunqiinnovation.deepvoice_utils.FileLogger - -/** - * 通用工具类,包含安全存储等功能 - */ -object Utils { - private const val TAG = "Utils" - private const val SECURE_PREFS_FILENAME = "deep_voice_secure_prefs" - private const val KEY_SESSION_ID = "session_id" - - /** - * 安全存储数据 - * @param context 上下文 - * @param dataMap 要存储的键值对字典 - * @return 是否成功存储 - */ - fun saveSecureData(context: Context, dataMap: Map): Boolean { - return try { - // 创建或获取主密钥 - val masterKey = MasterKey.Builder(context) - .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) - .build() - - // 创建加密SharedPreferences - val sharedPreferences = EncryptedSharedPreferences.create( - context, - SECURE_PREFS_FILENAME, - masterKey, - EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, - EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM - ) - - // 获取或生成会话ID - var sessionId = sharedPreferences.getString(KEY_SESSION_ID, "") ?: "" - if (sessionId.isEmpty()) { - sessionId = java.util.UUID.randomUUID().toString() - } - - // 开始编辑会话 - val editor = sharedPreferences.edit() - - // 保存会话ID - editor.putString(KEY_SESSION_ID, sessionId) - - // 保存所有键值对 - for ((key, value) in dataMap) { - if (value != null) { - editor.putString(key, value) - } else { - editor.remove(key) - } - } - - // 提交更改 - editor.apply() - - FileLogger.d(TAG, "数据已安全保存到加密存储中") - true - } catch (e: Exception) { - FileLogger.e(TAG, "保存数据到加密存储失败", e) - false - } - } - - /** - * 从安全存储中读取所有数据 - * @param context 上下文 - * @return 包含所有数据的键值对字典 - */ - fun loadAllSecureData(context: Context): Map { - return try { - val resultMap = mutableMapOf() - - // 创建或获取主密钥 - val masterKey = MasterKey.Builder(context) - .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) - .build() - - // 创建加密SharedPreferences - val sharedPreferences = EncryptedSharedPreferences.create( - context, - SECURE_PREFS_FILENAME, - masterKey, - EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, - EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM - ) - - // 读取所有数据 - for ((key, value) in sharedPreferences.all) { - if (value is String) { - resultMap[key] = value - } - } - - FileLogger.d(TAG, "已从加密存储加载所有数据") - resultMap - } catch (e: Exception) { - FileLogger.e(TAG, "从加密存储加载所有数据失败", e) - emptyMap() - } - } - - /** - * 清除所有安全存储的数据 - * @param context 上下文 - * @return 是否成功清除 - */ - fun clearAllSecureData(context: Context): Boolean { - return try { - // 创建或获取主密钥 - val masterKey = MasterKey.Builder(context) - .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) - .build() - - // 创建加密SharedPreferences - val sharedPreferences = EncryptedSharedPreferences.create( - context, - SECURE_PREFS_FILENAME, - masterKey, - EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, - EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM - ) - - // 生成新的会话ID - val sessionId = java.util.UUID.randomUUID().toString() - - // 清除所有数据 - sharedPreferences.edit().clear() - .putString(KEY_SESSION_ID, sessionId) - .apply() - - FileLogger.d(TAG, "已清除所有安全存储的数据") - true - } catch (e: Exception) { - FileLogger.e(TAG, "清除安全存储数据失败", e) - false - } - } -} \ No newline at end of file diff --git a/android/settings.gradle.kts b/android/settings.gradle.kts index 5f9c97be6..d350a0017 100644 --- a/android/settings.gradle.kts +++ b/android/settings.gradle.kts @@ -39,6 +39,7 @@ include(":spotify_sdk") include(":ble_service") include(":deepvoice_utils") include(":classic_bluetooth") +include(":agent_service") // 设置azure_speech项目的路径 project(":azure_speech").projectDir = file("../local_plugins/azure_speech/android") project(":open_ai_service").projectDir = file("../local_plugins/open_ai_service/android") @@ -47,4 +48,6 @@ project(":jieli").projectDir = file("../local_plugins/jieli/android") project(":spotify_sdk").projectDir = file("../local_plugins/spotify_sdk/android") project(":ble_service").projectDir = file("../local_plugins/ble_service/android") project(":deepvoice_utils").projectDir = file("../local_plugins/deepvoice_utils/android") -project(":classic_bluetooth").projectDir = file("../local_plugins/classic_bluetooth/android") \ No newline at end of file +project(":classic_bluetooth").projectDir = file("../local_plugins/classic_bluetooth/android") +project(":agent_service").projectDir = file("../local_plugins/agent_service/android") +