feat: 添加语音助手设置及识别服务
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user