widgets: backport to <v31

Modify the minimal wdiget so that it can work on Android 11 and below.
Was not actually that hard, mostly changing layouts around all things
considered. This also splits up the default view and the main widget
views, which makes managing UI state much easier.
This commit is contained in:
OxygenCobalt
2021-08-02 08:57:35 -06:00
parent ffebbc2839
commit 8673995630
18 changed files with 136 additions and 82 deletions
@@ -20,7 +20,6 @@ import org.oxycblt.auxio.ui.isNight
/**
* The single [AppCompatActivity] for Auxio.
* TODO: Port widgets to non-12 android
* TODO: Fix intent issues
*/
class MainActivity : AppCompatActivity() {
private val playbackModel: PlaybackViewModel by viewModels()
@@ -78,7 +78,7 @@ class PlaybackService : Service(), Player.Listener, PlaybackStateManager.Callbac
private val settingsManager = SettingsManager.getInstance()
// Widgets
private val minimalWidget = MinimalWidgetProvider.new()
private val minimalWidget = MinimalWidgetProvider()
// State
private var isForeground = false
@@ -179,6 +179,10 @@ class PlaybackService : Service(), Player.Listener, PlaybackStateManager.Callbac
audioReactor.release()
releaseWakelock()
// Technically the widgets don't *have* to be reset, but any commands from them
// won't work if the service is dead, so we do it anyway
minimalWidget.stop(this)
playbackManager.removeCallback(this)
settingsManager.removeCallback(this)
@@ -246,14 +250,14 @@ class PlaybackService : Service(), Player.Listener, PlaybackStateManager.Callbac
this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify
)
minimalWidget?.update(this, playbackManager)
minimalWidget.update(this, playbackManager)
return
}
// Clear if there's nothing to play.
player.stop()
minimalWidget?.stop(this)
minimalWidget.stop(this)
stopForegroundAndNotification()
}
@@ -11,6 +11,7 @@ import android.widget.RemoteViews
import androidx.annotation.LayoutRes
import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.MainActivity
import org.oxycblt.auxio.R
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.playback.state.PlaybackStateManager
@@ -112,11 +113,3 @@ abstract class BaseWidget : AppWidgetProvider() {
const val KEY_WIDGET_TYPE = BuildConfig.APPLICATION_ID + ".key.WIDGET_TYPE"
}
}
// PLAN:
// - Service calls methods on the widgets, think a subset of PlaybackStateManager.Callback
// - Widgets send BACK broadcasts to control playback, as other parts of the code do
// - Since widgets are broadcastrecievers, this is okay, shouldn't cause memory leaks
// - Can't use playbackstatemanager here since that would make the app unkillable
// - Callbacks also need to handle PlaybackService dying and being unable to send any
// more updates, and PlaybackService starting up as well
@@ -2,7 +2,6 @@ package org.oxycblt.auxio.widgets
import android.content.Context
import android.os.Build
import android.view.View
import android.widget.RemoteViews
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.loadBitmap
@@ -17,14 +16,7 @@ class MinimalWidgetProvider : BaseWidget() {
override val type: Int get() = TYPE
override fun getDefaultViews(context: Context): RemoteViews {
val views = getRemoteViews(context, LAYOUT)
views.setInt(R.id.widget_cover, "setImageResource", R.drawable.ic_song_clear)
views.setInt(R.id.widget_cover, "setVisibility", View.VISIBLE)
views.setInt(R.id.widget_placeholder_msg, "setVisibility", View.VISIBLE)
views.setInt(R.id.widget_meta, "setVisibility", View.GONE)
return views
return getRemoteViews(context, R.layout.widget_default)
}
override fun updateViews(
@@ -32,15 +24,12 @@ class MinimalWidgetProvider : BaseWidget() {
playbackManager: PlaybackStateManager,
onDone: (RemoteViews) -> Unit
) {
val views = getRemoteViews(context, LAYOUT)
val song = playbackManager.song
if (song != null) {
logD("updating view to ${song.name}")
// Show the proper widget views
views.setInt(R.id.widget_placeholder_msg, "setVisibility", View.GONE)
views.setInt(R.id.widget_meta, "setVisibility", View.VISIBLE)
val views = getRemoteViews(context, R.layout.widget_minimal)
// Update the metadata
views.setTextViewText(R.id.widget_song, song.name)
@@ -51,24 +40,18 @@ class MinimalWidgetProvider : BaseWidget() {
if (bitmap != null) {
views.setBitmap(R.id.widget_cover, "setImageBitmap", bitmap)
} else {
views.setInt(R.id.widget_cover, "setImageResource", R.drawable.ic_song_clear)
views.setInt(R.id.widget_cover, "setImageResource", R.drawable.ic_song)
}
onDone(views)
}
} else {
views.setInt(R.id.widget_cover, "setImageResource", R.drawable.ic_song_clear)
onDone(views)
onDone(getDefaultViews(context))
}
}
companion object {
const val TYPE = 0xA0D0
// Workaround to make studio shut up about perfectly valid layouts somehow
// being invalid for remote views.
const val LAYOUT = R.layout.widget_minimal
fun new(): MinimalWidgetProvider? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MinimalWidgetProvider()