feat: 添加语音助手设置及识别服务

This commit is contained in:
2026-07-27 10:08:21 +08:00
parent 8783f95307
commit 6bb9f25600
8 changed files with 107 additions and 3 deletions

View File

@@ -5,6 +5,8 @@
cmake_minimum_required(VERSION 3.4.1)
project("ttstd")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
@@ -41,4 +43,6 @@ target_link_libraries( # Specifies the target library.
# Links the target library to the log library
# included in the NDK.
${log-lib})
${log-lib})
set_target_properties(ttstd PROPERTIES LINK_FLAGS "-Wl,-z,max-page-size=16384")

View File

@@ -187,6 +187,16 @@
android:exported="true"
android:permission="android.permission.BIND_VOICE_INTERACTION" />
<service
android:name=".service.MyRecognitionService"
android:exported="true"
android:label="拨号助手识别"
android:permission="android.permission.BIND_RECOGNITION_SERVICE">
<intent-filter>
<action android:name="android.speech.RecognitionService" />
</intent-filter>
</service>
<!-- <receiver-->
<!-- android:name=".receiver.AppChangedReceiver"-->
<!-- android:enabled="true"-->

View File

@@ -3,8 +3,10 @@ package com.ttstd.dialer.activity.settings.utils;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.Settings;
import android.view.View;
import com.ttstd.dialer.service.MyVoiceInteractionService;
import com.ttstd.dialer.view.SwitchButton;
import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.R;
@@ -115,6 +117,26 @@ public class SettingsUtilsActivity extends BaseMvvmActivity<SettingsUtilsViewMod
}
});
mViewDataBinding.siAssistant.setOnToggleChanged(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if (isChecked) {
if (!SystemUtils.isAssistantServiceEnabled(SettingsUtilsActivity.this, MyVoiceInteractionService.class.getName())) {
boolean success = SystemUtils.setAssistantService(SettingsUtilsActivity.this, MyVoiceInteractionService.class.getName());
if (!success) {
Intent intent = new Intent(Settings.ACTION_VOICE_INPUT_SETTINGS);
startActivity(intent);
}
}
} else {
if (SystemUtils.isAssistantServiceEnabled(SettingsUtilsActivity.this, MyVoiceInteractionService.class.getName())) {
Intent intent = new Intent(Settings.ACTION_VOICE_INPUT_SETTINGS);
startActivity(intent);
}
}
}
});
}
@Override
@@ -140,6 +162,10 @@ public class SettingsUtilsActivity extends BaseMvvmActivity<SettingsUtilsViewMod
int enableHourlyChime = mMMKV.decodeInt(CommonConfig.HOURLY_CHIME_ENABLE, 0);
Logger.e(TAG, "initView: enableHourlyChime = " + enableHourlyChime);
mViewDataBinding.siHourlyChime.setToggleStatu(enableHourlyChime == 1);
boolean assistantEnabled = SystemUtils.isAssistantServiceEnabled(this, MyVoiceInteractionService.class.getName());
Logger.e(TAG, "initView: assistantEnabled = " + assistantEnabled);
mViewDataBinding.siAssistant.setToggleStatu(assistantEnabled);
}
public class BtnClick {

View File

@@ -0,0 +1,22 @@
package com.ttstd.dialer.service;
import android.content.Intent;
import android.speech.RecognitionService;
import com.ttstd.dialer.utils.Logger;
public class MyRecognitionService extends RecognitionService {
@Override
protected void onStartListening(Intent recognizerIntent, Callback listener) {
Logger.d("MyRecognitionService", "onStartListening");
}
@Override
protected void onCancel(Callback listener) {
Logger.d("MyRecognitionService", "onCancel");
}
@Override
protected void onStopListening(Callback listener) {
Logger.d("MyRecognitionService", "onStopListening");
}
}

View File

@@ -805,4 +805,34 @@ public class SystemUtils {
}).subscribe(observer);
}
/**
* 检查指定的 VoiceInteractionService 是否被设置为系统默认助手
*/
public static boolean isAssistantServiceEnabled(Context context, String serviceName) {
String setting = Settings.Secure.getString(context.getContentResolver(), "assistant");
if (setting != null) {
ComponentName componentName = ComponentName.unflattenFromString(setting);
if (componentName != null) {
return componentName.getPackageName().equals(context.getPackageName()) &&
componentName.getClassName().equals(serviceName);
}
}
return false;
}
/**
* 尝试将当前应用的指定服务设置为系统默认助手(通常需要系统签名或 WRITE_SECURE_SETTINGS 权限)
*/
public static boolean setAssistantService(Context context, String serviceName) {
try {
String value = context.getPackageName() + "/" + serviceName;
boolean success1 = Settings.Secure.putString(context.getContentResolver(), "assistant", value);
boolean success2 = Settings.Secure.putString(context.getContentResolver(), "voice_interaction_service", value);
return success1 && success2;
} catch (Exception e) {
Logger.e(TAG, "setAssistantService error: " + e.getMessage());
return false;
}
}
}

View File

@@ -114,6 +114,14 @@
app:enableText="@string/enable_text_hourly_chime"
app:optionsText="@string/options_text_hourly_chime" />
<com.ttstd.dialer.view.SettingItem
android:id="@+id/si_assistant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:disableText="@string/disable_text_assistant"
app:enableText="@string/enable_text_assistant"
app:optionsText="@string/options_text_assistant" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_100">

View File

@@ -48,6 +48,10 @@
<string name="options_text_hourly_chime">开启整点报时</string>
<string name="enable_text_hourly_chime">已开启,整点时将自动语音报时</string>
<string name="disable_text_hourly_chime">未开启,整点时将自动语音报时</string>
<string name="options_text_assistant">系统辅助助理</string>
<string name="enable_text_assistant">已开启,可识别屏幕内容和提供语音建议</string>
<string name="disable_text_assistant">未开启,点击开启设置</string>
<!--utils end-->
</resources>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<voice-interaction-service xmlns:android="http://schemas.android.com/apk/res/android"
android:sessionService="com.ttstd.dialer.service.MyVoiceInteractionSessionService"
android:recognitionService="com.ttstd.dialer.service.MyVoiceInteractionService"
android:recognitionService="com.ttstd.dialer.service.MyRecognitionService"
android:supportsAssist="true"
android:supportsLaunchFromKeyguard="true"
android:supportsLaunchVoiceAssistFromKeyguard="true"
android:supportsLocalInteraction="true" />