Nothing: add option to read aloud incoming notifications.

Also fixes a bug where the call would be accepted twice if the tts was not bound.
This commit is contained in:
Daniele Gobbetti 2024-04-12 16:51:49 +02:00
parent 1a8689d4bf
commit ff9207cd9a
5 changed files with 49 additions and 9 deletions

View File

@ -440,4 +440,5 @@ public class DeviceSettingsPreferenceConst {
public static final String PREF_AUTO_REPLY_INCOMING_CALL = "pref_auto_reply_phonecall";
public static final String PREF_AUTO_REPLY_INCOMING_CALL_DELAY = "pref_auto_reply_phonecall_delay";
public static final String PREF_SPEAK_NOTIFICATIONS_ALOUD = "pref_speak_notifications_aloud";
}

View File

@ -27,6 +27,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.service.serial.AbstractSerialDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
@ -57,6 +58,8 @@ public class Ear1Support extends AbstractSerialDeviceSupport {
callCmd.event = GBDeviceEventCallControl.Event.ACCEPT;
evaluateGBDeviceEvent(callCmd);
}, delayMillis); //15s
return;
}
String speechText = callSpec.name;
if (callSpec.name.equals(callSpec.number)) {
@ -70,6 +73,26 @@ public class Ear1Support extends AbstractSerialDeviceSupport {
}
@Override
public void onNotification(NotificationSpec notificationSpec) {
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
if (!prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SPEAK_NOTIFICATIONS_ALOUD, false))
return;
if (gbTextToSpeech.isConnected()) {
String notificationSpeller = new StringBuilder()
.append(notificationSpec.sourceName == null ? "" : notificationSpec.sourceName).append(". ")
.append(notificationSpec.title == null ? "" : notificationSpec.title).append(": ")
.append(notificationSpec.body == null ? "" : notificationSpec.body).toString();
gbTextToSpeech.speakNotification(notificationSpeller);
}
}
@Override
public void onSendConfiguration(String config) {
super.onSendConfiguration(config);
@ -122,16 +145,18 @@ public class Ear1Support extends AbstractSerialDeviceSupport {
@Override
public void onDone(String utteranceId) {
// LOG.debug("UtteranceProgressListener onDone.");
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
final int delayMillis = Integer.parseInt(prefs.getString(DeviceSettingsPreferenceConst.PREF_AUTO_REPLY_INCOMING_CALL_DELAY, "15")) * 1000;
if (utteranceId.equals("call")) {
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
final int delayMillis = Integer.parseInt(prefs.getString(DeviceSettingsPreferenceConst.PREF_AUTO_REPLY_INCOMING_CALL_DELAY, "15")) * 1000;
Looper mainLooper = Looper.getMainLooper();
new Handler(mainLooper).postDelayed(() -> {
GBDeviceEventCallControl callCmd = new GBDeviceEventCallControl();
callCmd.event = GBDeviceEventCallControl.Event.ACCEPT;
evaluateGBDeviceEvent(callCmd);
}, delayMillis); //15s
Looper mainLooper = Looper.getMainLooper();
new Handler(mainLooper).postDelayed(() -> {
GBDeviceEventCallControl callCmd = new GBDeviceEventCallControl();
callCmd.event = GBDeviceEventCallControl.Event.ACCEPT;
evaluateGBDeviceEvent(callCmd);
}, delayMillis); //15s
}
}
@Override

View File

@ -47,7 +47,13 @@ public class GBTextToSpeech {
Bundle params = new Bundle();
// Put the audio stream type into the Bundle
params.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_RING);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, "utteranceId");
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, "call");
}
public void speakNotification(String text) {
Bundle params = new Bundle();
params.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_MUSIC);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, "notification");
}
public void shutdown() {

View File

@ -2782,4 +2782,6 @@
<string name="pref_auto_reply_calls_title">Automatically answer phone calls</string>
<string name="pref_auto_reply_calls_delay_summary">Number of seconds after which the call is automatically picked up</string>
<string name="pref_auto_reply_calls_delay_title">Automatic Answer Delay</string>
<string name="pref_speak_notifications_aloud_summary">Notifications will be read aloud through the headphones</string>
<string name="pref_speak_notifications_aloud_title">Speak Notifications Aloud</string>
</resources>

View File

@ -29,4 +29,10 @@
android:summary="@string/pref_auto_reply_calls_delay_summary"
android:title="@string/pref_auto_reply_calls_delay_title"
app:defaultValue="15" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_voice"
android:key="pref_speak_notifications_aloud"
android:summary="@string/pref_speak_notifications_aloud_summary"
android:title="@string/pref_speak_notifications_aloud_title" />
</androidx.preference.PreferenceScreen>