tasker: kind of working plugin

This commit is contained in:
Alexander Capehart 2024-05-18 17:24:08 -06:00
parent b955e2f3ab
commit 8bc7418887
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
11 changed files with 269 additions and 37 deletions

View file

@ -147,5 +147,24 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".tasker.ShuffleAllConfigBasicAction"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:label="Shuffle All Songs">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>
<activity
android:name=".tasker.RestoreStateConfigBasicAction"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:label="Restore State">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>
</application> </application>
</manifest> </manifest>

View file

@ -28,8 +28,7 @@ import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import javax.inject.Inject
import org.oxycblt.auxio.music.service.IndexerServiceFragment import org.oxycblt.auxio.music.service.IndexerServiceFragment
import org.oxycblt.auxio.playback.service.MediaSessionServiceFragment import org.oxycblt.auxio.playback.service.MediaSessionServiceFragment
import org.oxycblt.auxio.tasker.indicateServiceRunning import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.tasker.indicateServiceStopped
@AndroidEntryPoint @AndroidEntryPoint
class AuxioService : MediaLibraryService(), ForegroundListener { class AuxioService : MediaLibraryService(), ForegroundListener {
@ -37,16 +36,17 @@ class AuxioService : MediaLibraryService(), ForegroundListener {
@Inject lateinit var indexingFragment: IndexerServiceFragment @Inject lateinit var indexingFragment: IndexerServiceFragment
private var nativeStart = false
@SuppressLint("WrongConstant") @SuppressLint("WrongConstant")
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
mediaSessionFragment.attach(this, this) mediaSessionFragment.attach(this, this)
indexingFragment.attach(this) indexingFragment.attach(this)
indicateServiceRunning()
} }
override fun onBind(intent: Intent?): IBinder? { override fun onBind(intent: Intent?): IBinder? {
handleIntent(intent) // handleIntent(intent)
return super.onBind(intent) return super.onBind(intent)
} }
@ -58,7 +58,8 @@ class AuxioService : MediaLibraryService(), ForegroundListener {
} }
private fun handleIntent(intent: Intent?) { private fun handleIntent(intent: Intent?) {
val nativeStart = intent?.getBooleanExtra(INTENT_KEY_NATIVE_START, false) ?: false nativeStart = intent?.getBooleanExtra(INTENT_KEY_INTERNAL_START, false) ?: false
logD("${intent} $nativeStart")
if (!nativeStart) { if (!nativeStart) {
// Some foreign code started us, no guarantees about foreground stability. Figure // Some foreign code started us, no guarantees about foreground stability. Figure
// out what to do. // out what to do.
@ -73,7 +74,6 @@ class AuxioService : MediaLibraryService(), ForegroundListener {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
indicateServiceStopped()
indexingFragment.release() indexingFragment.release()
mediaSessionFragment.release() mediaSessionFragment.release()
} }
@ -86,7 +86,9 @@ class AuxioService : MediaLibraryService(), ForegroundListener {
} }
override fun updateForeground(change: ForegroundListener.Change) { override fun updateForeground(change: ForegroundListener.Change) {
if (mediaSessionFragment.hasNotification()) { val state = mediaSessionFragment.hasNotification()
if (state == MediaSessionServiceFragment.NotificationState.RUNNING) {
if (change == ForegroundListener.Change.MEDIA_SESSION) { if (change == ForegroundListener.Change.MEDIA_SESSION) {
mediaSessionFragment.createNotification { mediaSessionFragment.createNotification {
startForeground(it.notificationId, it.notification) startForeground(it.notificationId, it.notification)
@ -98,7 +100,7 @@ class AuxioService : MediaLibraryService(), ForegroundListener {
indexingFragment.createNotification { indexingFragment.createNotification {
if (it != null) { if (it != null) {
startForeground(it.code, it.build()) startForeground(it.code, it.build())
} else { } else if (state == MediaSessionServiceFragment.NotificationState.NOT_RUNNING) {
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
} }
} }
@ -107,7 +109,7 @@ class AuxioService : MediaLibraryService(), ForegroundListener {
companion object { companion object {
// This is only meant for Auxio to internally ensure that it's state management will work. // This is only meant for Auxio to internally ensure that it's state management will work.
const val INTENT_KEY_NATIVE_START = BuildConfig.APPLICATION_ID + ".service.NATIVE_START" const val INTENT_KEY_INTERNAL_START = BuildConfig.APPLICATION_ID + ".service.INTERNAL_START"
} }
} }

View file

@ -71,11 +71,11 @@ class MainActivity : AppCompatActivity() {
startService( startService(
Intent(this, AuxioService::class.java) Intent(this, AuxioService::class.java)
.putExtra(AuxioService.INTENT_KEY_NATIVE_START, true)) .putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true))
if (!startIntentAction(intent)) { if (!startIntentAction(intent)) {
// No intent action to do, just restore the previously saved state. // No intent action to do, just restore the previously saved state.
playbackModel.playDeferred(DeferredPlayback.RestoreState) playbackModel.playDeferred(DeferredPlayback.RestoreState(sessionRequired = false))
} }
} }

View file

@ -85,7 +85,7 @@ class ExoPlaybackStateHolder(
private var currentSaveJob: Job? = null private var currentSaveJob: Job? = null
private var openAudioEffectSession = false private var openAudioEffectSession = false
var sessionOngoing = false override var sessionOngoing = false
private set private set
fun attach() { fun attach() {
@ -157,16 +157,24 @@ class ExoPlaybackStateHolder(
musicRepository.deviceLibrary musicRepository.deviceLibrary
// No library, cannot do anything. // No library, cannot do anything.
?: return false ?: return false
logD((Exception().stackTraceToString()))
when (action) { when (action) {
// Restore state -> Start a new restoreState job // Restore state -> Start a new restoreState job
is DeferredPlayback.RestoreState -> { is DeferredPlayback.RestoreState -> {
logD("Restoring playback state") logD("Restoring playback state")
restoreScope.launch { restoreScope.launch {
persistenceRepository.readState()?.let { val state = persistenceRepository.readState()
if (state != null) {
// Apply the saved state on the main thread to prevent code expecting // Apply the saved state on the main thread to prevent code expecting
// state updates on the main thread from crashing. // state updates on the main thread from crashing.
withContext(Dispatchers.Main) { playbackManager.applySavedState(it, false) } withContext(Dispatchers.Main) {
if (action.sessionRequired) {
sessionOngoing = true
}
playbackManager.applySavedState(state, false)
}
} else if (action.sessionRequired) {
error("No playback state to restore, but need to start session")
} }
} }
} }

View file

@ -51,7 +51,6 @@ import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.service.MediaItemBrowser import org.oxycblt.auxio.music.service.MediaItemBrowser
import org.oxycblt.auxio.playback.state.DeferredPlayback import org.oxycblt.auxio.playback.state.DeferredPlayback
import org.oxycblt.auxio.playback.state.PlaybackStateManager import org.oxycblt.auxio.playback.state.PlaybackStateManager
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.newMainPendingIntent import org.oxycblt.auxio.util.newMainPendingIntent
class MediaSessionServiceFragment class MediaSessionServiceFragment
@ -112,11 +111,25 @@ constructor(
fun handleNonNativeStart() { fun handleNonNativeStart() {
// At minimum we want to ensure an active playback state. // At minimum we want to ensure an active playback state.
// TODO: Possibly also force to go foreground? // TODO: Possibly also force to go foreground?
logD("Handling non-native start.") // We assume that all non-native starts are from media controllers that should know
playbackManager.playDeferred(DeferredPlayback.RestoreState) // what they are doing and have their own commands they want to execute.
playbackManager.playDeferred(DeferredPlayback.RestoreState(sessionRequired = true))
} }
fun hasNotification(): Boolean = exoHolder.sessionOngoing enum class NotificationState {
RUNNING,
NOT_RUNNING,
MAYBE_LATER
}
fun hasNotification(): NotificationState =
if (exoHolder.sessionOngoing) {
NotificationState.RUNNING
} else if (playbackManager.hasDeferredPlayback()) {
NotificationState.MAYBE_LATER
} else {
NotificationState.NOT_RUNNING
}
fun createNotification(post: (MediaNotification) -> Unit) { fun createNotification(post: (MediaNotification) -> Unit) {
val notification = val notification =

View file

@ -41,6 +41,9 @@ interface PlaybackStateHolder {
/** The current [MusicParent] being played from. Null if playing from all songs. */ /** The current [MusicParent] being played from. Null if playing from all songs. */
val parent: MusicParent? val parent: MusicParent?
/** Whether the player is in an active playback session. */
val sessionOngoing: Boolean
/** /**
* Resolve the current queue state as a [RawQueue]. * Resolve the current queue state as a [RawQueue].
* *
@ -275,8 +278,12 @@ data class QueueChange(val type: Type, val instructions: UpdateInstructions) {
/** Possible long-running background tasks handled by the background playback task. */ /** Possible long-running background tasks handled by the background playback task. */
sealed interface DeferredPlayback { sealed interface DeferredPlayback {
/** Restore the previously saved playback state. */ /**
data object RestoreState : DeferredPlayback * Restore the previously saved playback state.
*
* @param sessionRequired Whether a playback session must be started after restoration.
*/
data class RestoreState(val sessionRequired: Boolean) : DeferredPlayback
/** /**
* Start shuffled playback of the entire music library. Analogous to the "Shuffle All" shortcut. * Start shuffled playback of the entire music library. Analogous to the "Shuffle All" shortcut.

View file

@ -66,6 +66,9 @@ interface PlaybackStateManager {
/** Whether the queue is shuffled or not. */ /** Whether the queue is shuffled or not. */
val isShuffled: Boolean val isShuffled: Boolean
/** Whether there is an ongoing playback session or not. */
val sessionOngoing: Boolean
/** The audio session ID of the internal player. Null if no internal player exists. */ /** The audio session ID of the internal player. Null if no internal player exists. */
val currentAudioSessionId: Int? val currentAudioSessionId: Int?
@ -195,6 +198,13 @@ interface PlaybackStateManager {
*/ */
fun ack(stateHolder: PlaybackStateHolder, ack: StateAck) fun ack(stateHolder: PlaybackStateHolder, ack: StateAck)
/**
* Check if there is a pending [DeferredPlayback] to handle.
*
* @return Whether there is a pending [DeferredPlayback] to handle.
*/
fun hasDeferredPlayback(): Boolean
/** /**
* Start a [DeferredPlayback] for the current [PlaybackStateHolder] to handle eventually. * Start a [DeferredPlayback] for the current [PlaybackStateHolder] to handle eventually.
* *
@ -382,6 +392,9 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
override val isShuffled override val isShuffled
get() = stateMirror.isShuffled get() = stateMirror.isShuffled
override val sessionOngoing
get() = stateHolder?.sessionOngoing ?: false
override val currentAudioSessionId: Int? override val currentAudioSessionId: Int?
get() = stateHolder?.audioSessionId get() = stateHolder?.audioSessionId
@ -522,6 +535,8 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
// --- INTERNAL PLAYER FUNCTIONS --- // --- INTERNAL PLAYER FUNCTIONS ---
@Synchronized override fun hasDeferredPlayback(): Boolean = pendingDeferredPlayback != null
@Synchronized @Synchronized
override fun playDeferred(action: DeferredPlayback) { override fun playDeferred(action: DeferredPlayback) {
val stateHolder = stateHolder val stateHolder = stateHolder

View file

@ -0,0 +1,75 @@
/*
* Copyright (c) 2024 Auxio Project
* RestoreState.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.tasker
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.joaomgcd.taskerpluginlibrary.action.TaskerPluginRunnerActionNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfig
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigHelperNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigNoInput
import com.joaomgcd.taskerpluginlibrary.input.TaskerInput
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess
import dagger.hilt.EntryPoints
import kotlinx.coroutines.runBlocking
import org.oxycblt.auxio.AuxioService
import org.oxycblt.auxio.playback.state.DeferredPlayback
class RestoreStateHelper(config: TaskerPluginConfig<Unit>) :
TaskerPluginConfigHelperNoOutputOrInput<RestoreStateRunner>(config) {
override val runnerClass: Class<RestoreStateRunner>
get() = RestoreStateRunner::class.java
override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) {
blurbBuilder.append("Shuffles All Songs Once the Service is Available")
}
}
class RestoreStateConfigBasicAction : Activity(), TaskerPluginConfigNoInput {
override val context: Context
get() = applicationContext
private val taskerHelper by lazy { RestoreStateHelper(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
taskerHelper.finishForTasker()
}
}
class RestoreStateRunner : TaskerPluginRunnerActionNoOutputOrInput() {
override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> {
ContextCompat.startForegroundService(
context,
Intent(context, AuxioService::class.java)
.putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true))
val entryPoint = EntryPoints.get(context.applicationContext, TaskerEntryPoint::class.java)
val playbackManager = entryPoint.playbackManager()
runBlocking {
playbackManager.playDeferred(DeferredPlayback.RestoreState(sessionRequired = true))
}
while (!playbackManager.sessionOngoing) {}
Thread.sleep(100)
return TaskerPluginResultSucess()
}
}

View file

@ -0,0 +1,74 @@
/*
* Copyright (c) 2024 Auxio Project
* ShuffleAll.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.tasker
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.joaomgcd.taskerpluginlibrary.action.TaskerPluginRunnerActionNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfig
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigHelperNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigNoInput
import com.joaomgcd.taskerpluginlibrary.input.TaskerInput
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess
import dagger.hilt.EntryPoints
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.oxycblt.auxio.AuxioService
import org.oxycblt.auxio.playback.state.DeferredPlayback
class ShuffleAllHelper(config: TaskerPluginConfig<Unit>) :
TaskerPluginConfigHelperNoOutputOrInput<ShuffleAllRunner>(config) {
override val runnerClass: Class<ShuffleAllRunner>
get() = ShuffleAllRunner::class.java
override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) {
blurbBuilder.append("Shuffles All Songs Once the Service is Available")
}
}
class ShuffleAllConfigBasicAction : Activity(), TaskerPluginConfigNoInput {
override val context: Context
get() = applicationContext
private val taskerHelper by lazy { ShuffleAllHelper(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
taskerHelper.finishForTasker()
}
}
class ShuffleAllRunner : TaskerPluginRunnerActionNoOutputOrInput() {
override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> {
ContextCompat.startForegroundService(
context,
Intent(context, AuxioService::class.java)
.putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true))
val entryPoint = EntryPoints.get(context.applicationContext, TaskerEntryPoint::class.java)
val playbackManager = entryPoint.playbackManager()
runBlocking(Dispatchers.Main) { playbackManager.playDeferred(DeferredPlayback.ShuffleAll) }
while (!playbackManager.sessionOngoing) {}
Thread.sleep(100)
return TaskerPluginResultSucess()
}
}

View file

@ -32,26 +32,13 @@ import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess
import org.oxycblt.auxio.AuxioService import org.oxycblt.auxio.AuxioService
private var serviceRunning = false
fun indicateServiceRunning() {
serviceRunning = true
}
fun indicateServiceStopped() {
serviceRunning = false
}
class StartActionHelper(config: TaskerPluginConfig<Unit>) : class StartActionHelper(config: TaskerPluginConfig<Unit>) :
TaskerPluginConfigHelperNoOutputOrInput<StartActionRunner>(config) { TaskerPluginConfigHelperNoOutputOrInput<StartActionRunner>(config) {
override val runnerClass: Class<StartActionRunner> override val runnerClass: Class<StartActionRunner>
get() = StartActionRunner::class.java get() = StartActionRunner::class.java
override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) { override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) {
blurbBuilder.append( blurbBuilder.append("Starts the Auxio Service. You MUST apply an action after this.")
"Starts the Auxio Service. This will block until the service is fully initialized." +
"You must start active playback/foreground state after this or Auxio may" +
"crash.")
} }
} }
@ -69,8 +56,10 @@ class StartConfigBasicAction : Activity(), TaskerPluginConfigNoInput {
class StartActionRunner : TaskerPluginRunnerActionNoOutputOrInput() { class StartActionRunner : TaskerPluginRunnerActionNoOutputOrInput() {
override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> { override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> {
ContextCompat.startForegroundService(context, Intent(context, AuxioService::class.java)) ContextCompat.startForegroundService(
while (!serviceRunning) {} context,
Intent(context, AuxioService::class.java)
.putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true))
return TaskerPluginResultSucess() return TaskerPluginResultSucess()
} }
} }

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Auxio Project
* TaskerEntryPoint.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.tasker
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.oxycblt.auxio.playback.state.PlaybackStateManager
@EntryPoint
@InstallIn(SingletonComponent::class)
interface TaskerEntryPoint {
fun playbackManager(): PlaybackStateManager
}