From 00e7af8f3d6da34bc43b05f186427b9b51336642 Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Sat, 20 Feb 2021 10:05:20 -0700 Subject: [PATCH] Fix another file intent bug Fix another bug where PlaybackService wouldn't show its notification/go into foreground if a song was played by a file intent. --- .../java/org/oxycblt/auxio/MainActivity.kt | 11 ++-------- .../java/org/oxycblt/auxio/MainFragment.kt | 11 ++-------- .../auxio/playback/PlaybackFragment.kt | 9 ++------- .../auxio/playback/queue/QueueFragment.kt | 9 ++------- .../auxio/playback/system/PlaybackService.kt | 10 +++++----- .../org/oxycblt/auxio/ui/InterfaceUtils.kt | 20 ++++++++++++++++++- 6 files changed, 32 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/MainActivity.kt b/app/src/main/java/org/oxycblt/auxio/MainActivity.kt index a12010c79..b385994d9 100644 --- a/app/src/main/java/org/oxycblt/auxio/MainActivity.kt +++ b/app/src/main/java/org/oxycblt/auxio/MainActivity.kt @@ -50,17 +50,10 @@ class MainActivity : AppCompatActivity() { super.onNewIntent(intent) // Since the activity is set to singleInstance [Given that there's only MainActivity] - // We have to manually push the intent whenever we get one so that MainFragment + // We have to manually push the intent whenever we get one so that the fragments // can catch any file intents + // FIXME: Centralize the file intent code in MainActivity, if thats even possible setIntent(intent) - - // TODO: Idea Needed - // Move to all non-loading fragments - // Use ext fun - // Apply bool to intent to make sure it doesnt fire again??? - // Need it to - // - Run everywhere except loading - // - Dont fire after the first go } @Suppress("DEPRECATION") diff --git a/app/src/main/java/org/oxycblt/auxio/MainFragment.kt b/app/src/main/java/org/oxycblt/auxio/MainFragment.kt index 6d99bf706..f913fbf40 100644 --- a/app/src/main/java/org/oxycblt/auxio/MainFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/MainFragment.kt @@ -1,6 +1,5 @@ package org.oxycblt.auxio -import android.content.Intent import android.content.res.ColorStateList import android.os.Bundle import android.view.LayoutInflater @@ -22,6 +21,7 @@ import org.oxycblt.auxio.music.Song import org.oxycblt.auxio.playback.PlaybackViewModel import org.oxycblt.auxio.ui.Accent import org.oxycblt.auxio.ui.fixAnimInfoLeak +import org.oxycblt.auxio.ui.handleFileIntent import org.oxycblt.auxio.ui.isLandscape import org.oxycblt.auxio.ui.isTablet import org.oxycblt.auxio.ui.toColor @@ -119,14 +119,7 @@ class MainFragment : Fragment() { override fun onResume() { super.onResume() - val activity = requireActivity() - val intent = activity.intent - - // If the intent of the activity is a file intent, then play it. - // TODO?: Add an option to view it instead of play it if this becomes too annoying - if (intent != null && intent.action == Intent.ACTION_VIEW) { - playbackModel.playWithIntent(intent, requireContext()) - } else { + if (!handleFileIntent(playbackModel)) { playbackModel.restorePlaybackIfNeeded(requireContext()) } } diff --git a/app/src/main/java/org/oxycblt/auxio/playback/PlaybackFragment.kt b/app/src/main/java/org/oxycblt/auxio/playback/PlaybackFragment.kt index f5573f02f..c495d88cb 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/PlaybackFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/PlaybackFragment.kt @@ -1,6 +1,5 @@ package org.oxycblt.auxio.playback -import android.content.Intent import android.content.res.ColorStateList import android.os.Bundle import android.view.LayoutInflater @@ -18,6 +17,7 @@ import org.oxycblt.auxio.detail.DetailViewModel import org.oxycblt.auxio.logD import org.oxycblt.auxio.playback.state.LoopMode import org.oxycblt.auxio.ui.Accent +import org.oxycblt.auxio.ui.handleFileIntent import org.oxycblt.auxio.ui.memberBinding import org.oxycblt.auxio.ui.toStateList @@ -202,12 +202,7 @@ class PlaybackFragment : Fragment(), SeekBar.OnSeekBarChangeListener { playbackModel.enableAnimation() } - val intent = requireActivity().intent - - // If the intent of the activity is a file intent, then play it. - if (intent != null && intent.action == Intent.ACTION_VIEW) { - playbackModel.playWithIntent(intent, requireContext()) - } + handleFileIntent(playbackModel) } // --- SEEK CALLBACKS --- diff --git a/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueFragment.kt b/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueFragment.kt index 644b0b6e8..3187b70d4 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueFragment.kt @@ -1,6 +1,5 @@ package org.oxycblt.auxio.playback.queue -import android.content.Intent import android.os.Build import android.os.Bundle import android.view.LayoutInflater @@ -19,6 +18,7 @@ import org.oxycblt.auxio.music.Genre import org.oxycblt.auxio.music.Header import org.oxycblt.auxio.playback.PlaybackViewModel import org.oxycblt.auxio.playback.state.PlaybackMode +import org.oxycblt.auxio.ui.handleFileIntent import org.oxycblt.auxio.ui.isEdgeOn import org.oxycblt.auxio.ui.isIrregularLandscape @@ -102,12 +102,7 @@ class QueueFragment : Fragment() { override fun onResume() { super.onResume() - val intent = requireActivity().intent - - // If the intent of the activity is a file intent, then play it. - if (intent != null && intent.action == Intent.ACTION_VIEW) { - playbackModel.playWithIntent(intent, requireContext()) - } + handleFileIntent(playbackModel) } /** diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt index e62620939..489e6b2aa 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt @@ -213,7 +213,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca pushMetadataToSession(song) notification.setMetadata( - this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify + this, song, settingsManager.colorizeNotif, { startForegroundOrNotify() } ) return @@ -270,7 +270,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca override fun onColorizeNotifUpdate(doColorize: Boolean) { playbackManager.song?.let { song -> notification.setMetadata( - this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify + this, song, settingsManager.colorizeNotif, {} ) } } @@ -288,7 +288,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca override fun onShowCoverUpdate(showCovers: Boolean) { playbackManager.song?.let { song -> notification.setMetadata( - this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify + this, song, settingsManager.colorizeNotif, { startForegroundOrNotify() } ) } } @@ -296,7 +296,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca override fun onQualityCoverUpdate(doQualityCovers: Boolean) { playbackManager.song?.let { song -> notification.setMetadata( - this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify + this, song, settingsManager.colorizeNotif, { startForegroundOrNotify() } ) } } @@ -403,7 +403,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca * Bring the service into the foreground and show the notification, or refresh the notification. */ private fun startForegroundOrNotify() { - if (playbackManager.hasPlayed && playbackManager.isRestored && playbackManager.song != null) { + if (playbackManager.hasPlayed && playbackManager.song != null) { logD("Starting foreground/notifying") if (!isForeground) { diff --git a/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt b/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt index 72cf116d3..c0341ddcf 100644 --- a/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt +++ b/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt @@ -2,6 +2,7 @@ package org.oxycblt.auxio.ui import android.app.Activity import android.content.Context +import android.content.Intent import android.content.res.ColorStateList import android.content.res.Configuration import android.content.res.Resources @@ -26,6 +27,7 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.button.MaterialButton import org.oxycblt.auxio.R import org.oxycblt.auxio.logE +import org.oxycblt.auxio.playback.PlaybackViewModel // --- VIEW CONFIGURATION --- @@ -225,7 +227,7 @@ private fun isSystemBarOnBottom(activity: Activity): Boolean { return (!canMove || width < height) } -// --- HACKY NIGHTMARES --- +// --- FRAGMENT NONSENSE --- /** * Use reflection to fix a memory leak in the [Fragment] source code where the focused view will @@ -241,3 +243,19 @@ fun Fragment.fixAnimInfoLeak() { logE("mAnimationInfo leak fix failed.") } } + +/** + * Shortcut for handling a file intent. + * @return True if the file intent was pushed to [playbackModel], false if not + */ +fun Fragment.handleFileIntent(playbackModel: PlaybackViewModel): Boolean { + val intent = requireActivity().intent + + if (intent != null && intent.action == Intent.ACTION_VIEW) { + playbackModel.playWithIntent(intent, requireContext()) + + return true + } + + return false +}