From 2659a23421b17f5133a1e8ffe28bfc9732c4e950 Mon Sep 17 00:00:00 2001 From: octospacc Date: Mon, 29 May 2023 22:57:34 +0200 Subject: [PATCH] Intent API: Add allow_debug_commands option and receivers for SEND, INCOMING_CALL --- .../externalevents/IntentApiReceiver.java | 76 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/preferences.xml | 5 ++ 3 files changed, 83 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/IntentApiReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/IntentApiReceiver.java index df079e474..5fdbb2798 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/IntentApiReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/IntentApiReceiver.java @@ -24,20 +24,29 @@ import android.content.IntentFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; +import nodomain.freeyourgadget.gadgetbridge.BuildConfig; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.database.PeriodicExporter; +import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; +import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; +import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class IntentApiReceiver extends BroadcastReceiver { private static final Logger LOG = LoggerFactory.getLogger(IntentApiReceiver.class); + + private static final String msgDebugNotAllowed = "Intent API Allow Debug Commands not allowed"; public static final String COMMAND_ACTIVITY_SYNC = "nodomain.freeyourgadget.gadgetbridge.command.ACTIVITY_SYNC"; public static final String COMMAND_TRIGGER_EXPORT = "nodomain.freeyourgadget.gadgetbridge.command.TRIGGER_EXPORT"; + public static final String COMMAND_DEBUG_SEND = "nodomain.freeyourgadget.gadgetbridge.command.DEBUG_SEND"; + public static final String COMMAND_DEBUG_INCOMING_CALL = "nodomain.freeyourgadget.gadgetbridge.command.DEBUG_INCOMING_CALL"; @Override public void onReceive(final Context context, final Intent intent) { @@ -72,6 +81,7 @@ public class IntentApiReceiver extends BroadcastReceiver { GBApplication.deviceService().onFetchRecordedData(dataTypes); break; + case COMMAND_TRIGGER_EXPORT: if (!prefs.getBoolean("intent_api_allow_trigger_export", false)) { LOG.warn("Intent API export trigger not allowed"); @@ -83,6 +93,70 @@ public class IntentApiReceiver extends BroadcastReceiver { final Intent exportIntent = new Intent(context, PeriodicExporter.class); context.sendBroadcast(exportIntent); break; + + case COMMAND_DEBUG_SEND: + if (!prefs.getBoolean("intent_api_allow_debug_commands", false)) { + LOG.warn(msgDebugNotAllowed); + return; + } + LOG.info("Triggering Debug Send notification message"); + NotificationSpec notificationSpec = new NotificationSpec(); + notificationSpec.sender = intent.getStringExtra("sender"); + if (notificationSpec.sender == null) { + notificationSpec.sender = "DEBUG_SEND"; + } + notificationSpec.phoneNumber = intent.getStringExtra("phoneNumber"); + if (notificationSpec.phoneNumber == null) { + notificationSpec.phoneNumber = "DEBUG_SEND"; + } + notificationSpec.subject = intent.getStringExtra("subject"); + if (notificationSpec.subject == null) { + notificationSpec.subject = "DEBUG_SEND"; + } + notificationSpec.body = intent.getStringExtra("body"); + if (notificationSpec.body == null) { + notificationSpec.body = "DEBUG_SEND"; + } + notificationSpec.type = NotificationType.GENERIC_SMS; + if (intent.getStringExtra("type") != null) { + try { + notificationSpec.type = NotificationType.valueOf(intent.getStringExtra("type")); + } catch(IllegalArgumentException e) {} + } + if (notificationSpec.type != NotificationType.GENERIC_SMS) { + // SMS notifications don't have a source app ID when sent by the SMSReceiver, + // so let's not set it here as well for consistency + notificationSpec.sourceAppId = BuildConfig.APPLICATION_ID; + } + notificationSpec.sourceName = context.getApplicationInfo() + .loadLabel(context.getPackageManager()) + .toString(); + notificationSpec.pebbleColor = notificationSpec.type.color; + notificationSpec.attachedActions = new ArrayList<>(); + if (notificationSpec.type == NotificationType.GENERIC_SMS) { + // REPLY action + NotificationSpec.Action replyAction = new NotificationSpec.Action(); + replyAction.title = "Reply"; + replyAction.type = NotificationSpec.Action.TYPE_SYNTECTIC_REPLY_PHONENR; + notificationSpec.attachedActions.add(replyAction); + } + GBApplication.deviceService().onNotification(notificationSpec); + break; + + case COMMAND_DEBUG_INCOMING_CALL: + if (!prefs.getBoolean("intent_api_allow_debug_commands", false)) { + LOG.warn(msgDebugNotAllowed); + return; + } + LOG.info("Triggering Debug Incoming Call"); + CallSpec callSpec = new CallSpec(); + callSpec.command = CallSpec.CALL_INCOMING; + callSpec.number = intent.getStringExtra("caller"); + if (callSpec.number == null) { + callSpec.number = "DEBUG_INCOMING_CALL"; + } + GBApplication.deviceService().onSetCallState(callSpec); + break; } } @@ -90,6 +164,8 @@ public class IntentApiReceiver extends BroadcastReceiver { final IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(COMMAND_ACTIVITY_SYNC); intentFilter.addAction(COMMAND_TRIGGER_EXPORT); + intentFilter.addAction(COMMAND_DEBUG_SEND); + intentFilter.addAction(COMMAND_DEBUG_INCOMING_CALL); return intentFilter; } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bd406e5e3..c61ed901b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2079,6 +2079,8 @@ Allow triggering database export via Intent API Broadcast on database export Broadcast an intent when database export finishes + Allow Debug Commands + Allow triggering debug menu commands via Intent API Shell Racing Turbo Speed Lights diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 89004c9ab..bde849ae3 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -423,5 +423,10 @@ android:key="intent_api_broadcast_export" android:title="@string/intent_api_broadcast_export_title" android:summary="@string/intent_api_broadcast_export_summary" /> + +