Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop Absorb starting playback by itself when it's opened from the widget cover or right after an update

Barnabas6ba92f9e0749
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
index e1774c0b..8eaf9593 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
@@ -14,6 +14,7 @@ import android.media.audiofx.Equalizer
import android.media.audiofx.LoudnessEnhancer
import android.media.audiofx.Virtualizer
import android.os.Build
+import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Looper
@@ -26,6 +27,7 @@ import com.ryanheise.audioservice.AudioService
import com.ryanheise.audioservice.AudioServiceActivity
import com.ryanheise.audioservice.AudioServicePlugin
import com.ryanheise.just_audio.MonoController
+import es.antonborri.home_widget.HomeWidgetLaunchIntent
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.plugin.common.MethodCall
@@ -53,6 +55,22 @@ class MainActivity : AudioServiceActivity() {
// init proves the engine is unavailable, skip attaching native effects.
private var effectsAvailable: Boolean = true
+ // Android brings a task back from a dead process by recreating its root
+ // activity with the intent that first created it. When that was the
+ // widget's play button, every later plain open (the widget cover, the
+ // installer's Open button, Recents) replayed the play deep link and the
+ // app started playing on its own. A real tap always creates the activity
+ // fresh, so only a recreated one can be carrying a replay.
+ override fun onCreate(savedInstanceState: Bundle?) {
+ if (savedInstanceState != null &&
+ intent?.action == HomeWidgetLaunchIntent.HOME_WIDGET_LAUNCH_ACTION) {
+ Log.d(TAG, "Dropping replayed widget launch ${intent.data}")
+ intent.action = Intent.ACTION_MAIN
+ intent.data = null
+ }
+ super.onCreate(savedInstanceState)
+ }
+
override fun provideFlutterEngine(context: Context): FlutterEngine? {
// Headless service starts (Android Auto binds, media buttons on a
// dead process) boot the engine with no activity so the browse tree
@@ -547,6 +565,12 @@ class MainActivity : AudioServiceActivity() {
Log.d(TAG, "Discarding search intent: $action")
return
}
+ // FlutterActivity never adopts a new intent, so the widget's "launched
+ // from" query kept answering with whatever intent created the activity.
+ // On a task restored from a dead process that lost a fresh play-button
+ // tap and replayed an old one. Keep the latest so the query sees the
+ // tap that actually opened the app this time.
+ setIntent(intent)
super.onNewIntent(intent)
}
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt
index 5ce9e47c..a33204aa 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt
@@ -394,12 +394,14 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {
views.setViewVisibility(R.id.widget_play_pending_tile, View.GONE)
}
+ // With the app dead the tile shows no play icon, so a tap reads
+ // as "open", not "play" - wired to play it opened the app and
+ // started playback on its own. The larger widgets keep
+ // open-and-play on their actual play button.
val tapIntent = if (canControl) {
togglePendingIntent(context)
} else {
- HomeWidgetLaunchIntent.getActivity(
- context, MainActivity::class.java, Uri.parse("absorb://widget/play_pause")
- )
+ openAppPendingIntent(context)
}
views.setOnClickPendingIntent(R.id.widget_root, tapIntent)
}