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
This commit is contained in:
parent
4bcbeae1e0
commit
fff9ceccc2
@ -53,6 +53,16 @@
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<receiver android:name=".AutomationReceiver"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="org.musicpd.action.StartService" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="org.musicpd.action.StopService" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<service
|
||||
android:name=".Main" />
|
||||
</application>
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user