|
|
|
@ -2,6 +2,7 @@ package com.yunqiinnovation.agent_service |
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
import android.content.Intent |
|
|
|
import android.content.SharedPreferences |
|
|
|
import android.os.Handler |
|
|
|
import android.os.Looper |
|
|
|
import org.json.JSONArray |
|
|
|
@ -62,9 +63,12 @@ interface AgentServiceListener { |
|
|
|
*/ |
|
|
|
object AgentService : CoroutineScope { |
|
|
|
private val TAG = "AgentService" |
|
|
|
|
|
|
|
// SharedPreferences 相关常量 |
|
|
|
private const val PREF_NAME = "agent_service_prefs" |
|
|
|
private const val KEY_IS_INTERRUPT = "is_interrupt" |
|
|
|
|
|
|
|
|
|
|
|
private var _isInterrupt = AtomicBoolean(true) |
|
|
|
private var _isInterrupt = AtomicBoolean(false) |
|
|
|
|
|
|
|
private var azureSpeechKey: String = "" |
|
|
|
private var azureSpeechRegion: String = "" |
|
|
|
@ -201,6 +205,10 @@ object AgentService : CoroutineScope { |
|
|
|
callingTool = config["callingTool"]?.toString() ?: ""; |
|
|
|
// 初始化上下文和服务 |
|
|
|
this.context = appContext.applicationContext |
|
|
|
|
|
|
|
// 从 SharedPreferences 读取 _isInterrupt 状态 |
|
|
|
loadIsInterruptFromCache() |
|
|
|
|
|
|
|
this.chatApiService = ChatApiService(context) |
|
|
|
this.chatStorageHelper = ChatStorageHelper(context) |
|
|
|
this.audioPlayer = AudioPlayer(context) |
|
|
|
@ -535,11 +543,15 @@ object AgentService : CoroutineScope { |
|
|
|
* 如果当前为false,则设置为true;如果当前为true,则设置为false |
|
|
|
* @return 切换后的状态值 |
|
|
|
*/ |
|
|
|
fun isInterrupt(): Boolean { |
|
|
|
val currentValue = _isInterrupt.get() |
|
|
|
val newValue = !currentValue |
|
|
|
_isInterrupt.set(newValue) |
|
|
|
return newValue |
|
|
|
fun isInterrupt(isInterrupt: Boolean): Boolean { |
|
|
|
// val currentValue = _isInterrupt.get() |
|
|
|
// val newValue = !currentValue |
|
|
|
_isInterrupt.set(isInterrupt) |
|
|
|
|
|
|
|
// 保存到 SharedPreferences |
|
|
|
saveIsInterruptToCache(isInterrupt) |
|
|
|
|
|
|
|
return isInterrupt |
|
|
|
} |
|
|
|
/** |
|
|
|
* 开始语音识别 |
|
|
|
@ -2565,4 +2577,35 @@ object AgentService : CoroutineScope { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从 SharedPreferences 加载 _isInterrupt 状态 |
|
|
|
*/ |
|
|
|
private fun loadIsInterruptFromCache() { |
|
|
|
try { |
|
|
|
if (::context.isInitialized) { |
|
|
|
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) |
|
|
|
val cachedValue = prefs.getBoolean(KEY_IS_INTERRUPT, false) // 默认值为 false |
|
|
|
_isInterrupt.set(cachedValue) |
|
|
|
Log.d(TAG, "从缓存加载 _isInterrupt 状态: $cachedValue") |
|
|
|
} |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "加载 _isInterrupt 缓存状态时发生异常: ${e.message}", e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 保存 _isInterrupt 状态到 SharedPreferences |
|
|
|
*/ |
|
|
|
private fun saveIsInterruptToCache(isInterrupt: Boolean) { |
|
|
|
try { |
|
|
|
if (::context.isInitialized) { |
|
|
|
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) |
|
|
|
prefs.edit().putBoolean(KEY_IS_INTERRUPT, isInterrupt).apply() |
|
|
|
Log.d(TAG, "保存 _isInterrupt 状态到缓存: $isInterrupt") |
|
|
|
} |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "保存 _isInterrupt 缓存状态时发生异常: ${e.message}", e) |
|
|
|
} |
|
|
|
} |
|
|
|
} |