Browse Source

feat(location_service): 添加系统位置服务状态检查功能

添加检查系统位置服务是否开启的功能,并在定位前进行校验
修改定位函数名以更准确描述功能
修复中断响应逻辑,优先停止TTS
更新Azure语音识别支持的语言列表
配置gradle构建参数以支持旧版打包方式
newdev_shunjiawei
liwei1dao 9 months ago
parent
commit
8c3ae0c987
  1. 7
      android/app/build.gradle.kts
  2. 1
      android/app/src/main/AndroidManifest.xml
  3. 3
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  4. 2
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt
  5. 4
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt
  6. 23
      local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt
  7. 14
      local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationServicePlugin.kt
  8. 17
      local_plugins/location_service/lib/location_service.dart

7
android/app/build.gradle.kts

@ -55,6 +55,9 @@ android {
excludes.add("META-INF/notice.txt")
excludes.add("META-INF/*.kotlin_module")
}
jniLibs {
useLegacyPackaging = true
}
}
defaultConfig {
@ -83,6 +86,8 @@ android {
storePassword = keystoreProperties.getProperty("storePassword")
enableV1Signing = true
enableV2Signing = true
enableV3Signing = false
enableV4Signing = false
}
getByName("debug") {
// debug 使用与 release 相同的签名配置
@ -92,6 +97,8 @@ android {
storePassword = keystoreProperties.getProperty("storePassword")
enableV1Signing = true
enableV2Signing = true
enableV3Signing = false
enableV4Signing = false
}
}

1
android/app/src/main/AndroidManifest.xml

@ -72,6 +72,7 @@
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
android:extractNativeLibs="true"
android:enableOnBackInvokedCallback="true"
tools:replace="android:label">

3
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -746,7 +746,8 @@ object AgentService : CoroutineScope {
// 执行打断
lastInterruptTime = currentTime
interruptCurrentResponse()
stopTts()
// interruptCurrentResponse()
}
}
}

2
local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt

@ -130,7 +130,7 @@ class AzureAsrHelper(private val context: Context) {
val xunfeiConfigReady = this.xunfeiAppId.isNotBlank() &&
this.xunfeiAccessKeyId.isNotBlank() &&
this.xunfeiAccessKeySecret.isNotBlank()
this.useXunfei = supportedLanguages.all { it == "zh-CN" || it == "en-US" } && xunfeiConfigReady
this.useXunfei = supportedLanguages.all { it == "zh-CN" || it == "en-US" || it == "zh-HK"|| it == "en-TW" } && xunfeiConfigReady
// 根据支持的语言数量决定是否启用自动语言检测
this.isAutoDetectLanguage = supportedLanguages.size >= 2

4
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt

@ -152,8 +152,8 @@ class SystemFunctionHandler(private val context: Context? = null) {
// 注册获取当前位置函数
client.registerLocalFunction(
name = "get_current_location",
description = "获取当前地理位置",
name = "get_user_current_location",
description = "获取用户当前地理位置",
parameters = mapOf(
"type" to "object",
"properties" to emptyMap<String, Any>(),

23
local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt

@ -5,6 +5,7 @@ import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Build
import android.os.Handler
import android.os.Looper
@ -150,6 +151,10 @@ object LocationService {
callback.onLocationError(-2, "后台定位权限需要打开始终定位权限才可,请打开app重新设置权限后再使用")
return
}
if (!isLocationServiceEnabled(ctx)) {
callback.onLocationError(-6, "定位服务未开启,请在系统设置中打开位置服务开关")
return
}
if (!initialized) {
callback.onLocationError(-3, "定位服务未初始化")
return
@ -287,6 +292,24 @@ object LocationService {
return 0
}
/**
* 检查系统位置服务开关状态
* @param context 应用上下文
* @return true:位置服务已开启;false:位置服务关闭或检测失败
* @throws 无显式抛出异常(内部捕获并记录日志)
*/
fun isLocationServiceEnabled(context: Context): Boolean {
try {
val lm = context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager
val gpsOn = lm?.isProviderEnabled(LocationManager.GPS_PROVIDER) == true
val networkOn = lm?.isProviderEnabled(LocationManager.NETWORK_PROVIDER) == true
return gpsOn || networkOn
} catch (e: Exception) {
Log.e(TAG, "检测位置服务开关失败: ${e.message}", e)
return false
}
}
private fun hasForegroundLocationPermission(context: Context): Boolean {
return ContextCompat.checkSelfPermission(
context,

14
local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationServicePlugin.kt

@ -25,7 +25,19 @@ class LocationServicePlugin: FlutterPlugin, MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
Log.d(TAG, "Method call: ${call.method}")
when (call.method) {
"isLocationServiceEnabled" -> {
try {
val enabled = LocationService.isLocationServiceEnabled(context)
result.success(enabled)
} catch (e: Exception) {
result.error("CHECK_SERVICE_FAILED", e.message, null)
}
}
else -> {
result.notImplemented()
}
}
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {

17
local_plugins/location_service/lib/location_service.dart

@ -12,7 +12,7 @@ class LocationService {
);
// 内部构造函数
LocationService._internal() {}
LocationService._internal();
/// 检查定位权限
Future<Map<String, bool>> checkPermission() async {
@ -40,6 +40,21 @@ class LocationService {
}
}
/// 检查系统位置服务是否开启
/// 参数:无
/// 返回值:true 表示已开启;false 表示未开启或检测失败
/// 异常:抛出 Exception,表示方法通道调用失败或平台异常
Future<bool> isLocationServiceEnabled() async {
try {
final enabled = await _methodChannel.invokeMethod<bool>(
'isLocationServiceEnabled',
);
return enabled ?? false;
} catch (e) {
throw Exception('检查位置服务失败: $e');
}
}
/// 获取当前位置(按需获取)
Future<Map<String, dynamic>> location() async {
try {

Loading…
Cancel
Save