|
|
|
@ -8,6 +8,7 @@ import android.content.Context |
|
|
|
import android.content.Intent |
|
|
|
import android.content.pm.PackageManager |
|
|
|
import android.location.LocationManager |
|
|
|
import android.media.AudioManager |
|
|
|
import android.net.Uri |
|
|
|
import android.os.Build |
|
|
|
import android.provider.AlarmClock |
|
|
|
@ -232,6 +233,42 @@ class SystemFunctionHandler(private val context: Context? = null) { |
|
|
|
), |
|
|
|
handler = ExitNavigationHandler(context) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// 增加音量 |
|
|
|
client.registerLocalFunction( |
|
|
|
name = "add_volume", |
|
|
|
description = "增大音量", |
|
|
|
parameters = mapOf( |
|
|
|
"type" to "object", |
|
|
|
"properties" to emptyMap<String, Any>(), |
|
|
|
"required" to emptyList<String>() |
|
|
|
), |
|
|
|
handler = AddVolumeHandler(context) |
|
|
|
) |
|
|
|
|
|
|
|
// 增加音量 |
|
|
|
client.registerLocalFunction( |
|
|
|
name = "reduce_volume", |
|
|
|
description = "降低音量", |
|
|
|
parameters = mapOf( |
|
|
|
"type" to "object", |
|
|
|
"properties" to emptyMap<String, Any>(), |
|
|
|
"required" to emptyList<String>() |
|
|
|
), |
|
|
|
handler = ReduceVolumeHandler(context) |
|
|
|
) |
|
|
|
// 设置静音 |
|
|
|
client.registerLocalFunction( |
|
|
|
name = "mute_volume", |
|
|
|
description = "设置静音", |
|
|
|
parameters = mapOf( |
|
|
|
"type" to "object", |
|
|
|
"properties" to emptyMap<String, Any>(), |
|
|
|
"required" to emptyList<String>() |
|
|
|
), |
|
|
|
handler = MuteVolumeHandler(context) |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -1320,4 +1357,109 @@ private class OpenRecorderHandler(private val context: Context?) : FunctionHandl |
|
|
|
}.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 添加音量处理类 |
|
|
|
*/ |
|
|
|
private class AddVolumeHandler(private val context: Context?) : FunctionHandler { |
|
|
|
|
|
|
|
//设置音量 |
|
|
|
fun setVolume(context: Context, streamType: Int, volume: Int) { |
|
|
|
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager |
|
|
|
val maxVolume = audioManager.getStreamMaxVolume(streamType) |
|
|
|
val safeVolume = volume.coerceIn(0, maxVolume) |
|
|
|
audioManager.setStreamVolume(streamType, safeVolume, AudioManager.FLAG_SHOW_UI) |
|
|
|
} |
|
|
|
fun getCurrentVolume(context: Context, streamType: Int): Int { |
|
|
|
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager |
|
|
|
return audioManager.getStreamVolume(streamType) |
|
|
|
} |
|
|
|
fun getMaxVolume(context: Context, streamType: Int): Int { |
|
|
|
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager |
|
|
|
return audioManager.getStreamMaxVolume(streamType) |
|
|
|
} |
|
|
|
//增加音量/减少音量 |
|
|
|
fun adjustVolume(context: Context, direction: Int) { |
|
|
|
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager |
|
|
|
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, direction, AudioManager.FLAG_SHOW_UI) |
|
|
|
} |
|
|
|
|
|
|
|
override suspend fun handle(arguments: Map<String, Any>): String { |
|
|
|
if (context == null) { |
|
|
|
return JSONObject().apply { |
|
|
|
put("error", JSONObject().apply { |
|
|
|
put("code", "context_error") |
|
|
|
put("message", "上下文未初始化") |
|
|
|
}) |
|
|
|
}.toString() |
|
|
|
} |
|
|
|
|
|
|
|
// 发送广播通知退出交互 |
|
|
|
val result = adjustVolume(context, AudioManager.ADJUST_RAISE) |
|
|
|
|
|
|
|
return JSONObject().apply { |
|
|
|
put("result", "增加音量成功!") |
|
|
|
put("broadcast", false) |
|
|
|
}.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
/** |
|
|
|
* 添加音量处理类 |
|
|
|
*/ |
|
|
|
private class ReduceVolumeHandler(private val context: Context?) : FunctionHandler { |
|
|
|
//增加音量/减少音量 |
|
|
|
fun adjustVolume(context: Context, direction: Int) { |
|
|
|
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager |
|
|
|
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, direction, AudioManager.FLAG_SHOW_UI) |
|
|
|
} |
|
|
|
|
|
|
|
override suspend fun handle(arguments: Map<String, Any>): String { |
|
|
|
if (context == null) { |
|
|
|
return JSONObject().apply { |
|
|
|
put("error", JSONObject().apply { |
|
|
|
put("code", "context_error") |
|
|
|
put("message", "上下文未初始化") |
|
|
|
}) |
|
|
|
}.toString() |
|
|
|
} |
|
|
|
|
|
|
|
// 发送广播通知退出交互 |
|
|
|
adjustVolume(context, AudioManager.ADJUST_LOWER) |
|
|
|
return JSONObject().apply { |
|
|
|
put("result", "降低音量成功!") |
|
|
|
put("broadcast", false) |
|
|
|
}.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
/** |
|
|
|
* 添加音量处理类 |
|
|
|
*/ |
|
|
|
private class MuteVolumeHandler(private val context: Context?) : FunctionHandler { |
|
|
|
|
|
|
|
//设置音量 |
|
|
|
fun setVolume(context: Context, streamType: Int, volume: Int) { |
|
|
|
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager |
|
|
|
val maxVolume = audioManager.getStreamMaxVolume(streamType) |
|
|
|
val safeVolume = volume.coerceIn(0, maxVolume) |
|
|
|
audioManager.setStreamVolume(streamType, safeVolume, AudioManager.FLAG_SHOW_UI) |
|
|
|
} |
|
|
|
|
|
|
|
override suspend fun handle(arguments: Map<String, Any>): String { |
|
|
|
if (context == null) { |
|
|
|
return JSONObject().apply { |
|
|
|
put("error", JSONObject().apply { |
|
|
|
put("code", "context_error") |
|
|
|
put("message", "上下文未初始化") |
|
|
|
}) |
|
|
|
}.toString() |
|
|
|
} |
|
|
|
|
|
|
|
// 发送广播通知退出交互 |
|
|
|
setVolume(context, AudioManager.STREAM_MUSIC, 0) |
|
|
|
return JSONObject().apply { |
|
|
|
put("result", "设置静音成功!") |
|
|
|
put("broadcast", false) |
|
|
|
}.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
|