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>
|
</intent-filter>
|
||||||
</receiver>
|
</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
|
<service
|
||||||
android:name=".Main" />
|
android:name=".Main" />
|
||||||
</application>
|
</application>
|
||||||
|
@ -5,5 +5,20 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
|
||||||
class AutomationReceiver : BroadcastReceiver() {
|
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.common.util.UnstableApi;
|
||||||
import androidx.media3.session.MediaSession;
|
import androidx.media3.session.MediaSession;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.musicpd.data.LoggingRepository;
|
import org.musicpd.data.LoggingRepository;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@ -35,8 +37,10 @@ import dagger.hilt.android.AndroidEntryPoint;
|
|||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
public class Main extends Service implements Runnable {
|
public class Main extends Service implements Runnable {
|
||||||
|
|
||||||
private static final String TAG = "Main";
|
private static final String TAG = "Main";
|
||||||
private static final String WAKELOCK_TAG = "mpd:wakelockmain";
|
private static final String WAKELOCK_TAG = "mpd:wakelockmain";
|
||||||
|
|
||||||
private static final int MAIN_STATUS_ERROR = -1;
|
private static final int MAIN_STATUS_ERROR = -1;
|
||||||
private static final int MAIN_STATUS_STOPPED = 0;
|
private static final int MAIN_STATUS_STOPPED = 0;
|
||||||
private static final int MAIN_STATUS_STARTED = 1;
|
private static final int MAIN_STATUS_STARTED = 1;
|
||||||
@ -57,6 +61,9 @@ public class Main extends Service implements Runnable {
|
|||||||
@Inject
|
@Inject
|
||||||
LoggingRepository logging;
|
LoggingRepository logging;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static final String SHUTDOWN_ACTION = "org.musicpd.action.ShutdownMPD";
|
||||||
|
|
||||||
static class MainStub extends IMain.Stub {
|
static class MainStub extends IMain.Stub {
|
||||||
private Main mService;
|
private Main mService;
|
||||||
MainStub(Main service) {
|
MainStub(Main service) {
|
||||||
@ -126,9 +133,13 @@ public class Main extends Service implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
start();
|
if (Objects.equals(intent.getAction(), SHUTDOWN_ACTION)) {
|
||||||
if (intent != null && intent.getBooleanExtra("wakelock", false))
|
stop();
|
||||||
setWakelockEnabled(true);
|
} else {
|
||||||
|
start();
|
||||||
|
if (intent.getBooleanExtra("wakelock", false))
|
||||||
|
setWakelockEnabled(true);
|
||||||
|
}
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,4 +330,9 @@ public class Main extends Service implements Runnable {
|
|||||||
else
|
else
|
||||||
context.startService(intent);
|
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