From fff9ceccc2cdf85df4e3441b710a1a5211c63f95 Mon Sep 17 00:00:00 2001 From: Colin Edwards Date: Fri, 5 Apr 2024 23:46:14 -0500 Subject: [PATCH] android: Add intents for service start and stop org.musicpd.action.StartService org.musicpd.action.StopService You can test these actions like: adb shell am broadcast -a org.musicpd.action.StartService org.musicpd Calling these from an app like tasker should allow for automation --- android/app/src/main/AndroidManifest.xml | 10 +++++++++ .../java/org/musicpd/AutomationReceiver.kt | 17 +++++++++++++- .../app/src/main/java/org/musicpd/Main.java | 22 ++++++++++++++++--- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 55ad59448..4df7ded3e 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -53,6 +53,16 @@ + + + + + + + + + diff --git a/android/app/src/main/java/org/musicpd/AutomationReceiver.kt b/android/app/src/main/java/org/musicpd/AutomationReceiver.kt index bb7f42a91..c1a31dcc1 100644 --- a/android/app/src/main/java/org/musicpd/AutomationReceiver.kt +++ b/android/app/src/main/java/org/musicpd/AutomationReceiver.kt @@ -5,5 +5,20 @@ import android.content.Context import android.content.Intent class AutomationReceiver : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) {} + override fun onReceive(context: Context, intent: Intent) { + + when(intent.action) { + "org.musicpd.action.StartService" -> { + val wakelock = Preferences.getBoolean( + context, + Preferences.KEY_WAKELOCK, false + ) + Main.startService(context, wakelock) + } + "org.musicpd.action.StopService" -> { + context.startService(Intent(context, Main::class.java) + .setAction(Main.SHUTDOWN_ACTION)) + } + } + } } diff --git a/android/app/src/main/java/org/musicpd/Main.java b/android/app/src/main/java/org/musicpd/Main.java index cc5a52837..3c4ad5464 100644 --- a/android/app/src/main/java/org/musicpd/Main.java +++ b/android/app/src/main/java/org/musicpd/Main.java @@ -24,10 +24,12 @@ import androidx.annotation.OptIn; import androidx.media3.common.util.UnstableApi; import androidx.media3.session.MediaSession; +import org.jetbrains.annotations.NotNull; import org.musicpd.data.LoggingRepository; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.Objects; import javax.inject.Inject; @@ -35,8 +37,10 @@ import dagger.hilt.android.AndroidEntryPoint; @AndroidEntryPoint public class Main extends Service implements Runnable { + private static final String TAG = "Main"; private static final String WAKELOCK_TAG = "mpd:wakelockmain"; + private static final int MAIN_STATUS_ERROR = -1; private static final int MAIN_STATUS_STOPPED = 0; private static final int MAIN_STATUS_STARTED = 1; @@ -57,6 +61,9 @@ public class Main extends Service implements Runnable { @Inject LoggingRepository logging; + @NotNull + public static final String SHUTDOWN_ACTION = "org.musicpd.action.ShutdownMPD"; + static class MainStub extends IMain.Stub { private Main mService; MainStub(Main service) { @@ -126,9 +133,13 @@ public class Main extends Service implements Runnable { @Override public int onStartCommand(Intent intent, int flags, int startId) { - start(); - if (intent != null && intent.getBooleanExtra("wakelock", false)) - setWakelockEnabled(true); + if (Objects.equals(intent.getAction(), SHUTDOWN_ACTION)) { + stop(); + } else { + start(); + if (intent.getBooleanExtra("wakelock", false)) + setWakelockEnabled(true); + } return START_STICKY; } @@ -319,4 +330,9 @@ public class Main extends Service implements Runnable { else context.startService(intent); } + + public static void stopService(Context context) { + Intent intent = new Intent(context, Main.class); + context.stopService(intent); + } }