all: hack around more lollipop issues

Hack around more insane lollipop bugs, such as:

- The angular auxio icon crashing the system UI
- Optimized icons being corrupted
- Setting image alpha not working properly

I really wish I could drop support for this horrible version, but I
either have to wait for a major library to drop support or for the
usage numbers to reach 1%.
This commit is contained in:
OxygenCobalt
2022-05-22 17:12:49 -06:00
parent 594fa3597e
commit 485c35d74c
78 changed files with 231 additions and 214 deletions
@@ -80,6 +80,7 @@ class MainActivity : AppCompatActivity() {
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
// See if the new intent is a file intent. If so, open it.
val uri = retrieveViewUri(intent)
if (uri != null) {
playbackModel.performAction(this, PlaybackViewModel.DelayedAction.Open(uri))
@@ -183,7 +183,9 @@ class MainFragment : ViewBindingFragment<FragmentMainBinding>() {
/**
* A back press callback that handles how to respond to backwards navigation in the detail
* fragments and the playback panel. TODO: Migrate to new predictive API
* fragments and the playback panel.
*
* TODO: Migrate to new predictive API
*/
inner class DynamicBackPressedCallback : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
@@ -31,6 +31,7 @@ import org.oxycblt.auxio.ui.BindingViewHolder
import org.oxycblt.auxio.ui.Item
import org.oxycblt.auxio.ui.MenuItemListener
import org.oxycblt.auxio.ui.SimpleItemCallback
import org.oxycblt.auxio.ui.StyledImageView
import org.oxycblt.auxio.util.context
import org.oxycblt.auxio.util.formatDuration
import org.oxycblt.auxio.util.getPluralSafe
@@ -202,7 +203,7 @@ private class AlbumSongViewHolder private constructor(private val binding: ItemA
contentDescription = context.getString(R.string.desc_track_number, item.track)
}
binding.songTrackBg.imageAlpha = 0
binding.songTrackBg.setImageDrawable(null)
} else {
binding.songTrack.apply {
textSafe = ""
@@ -210,7 +211,12 @@ private class AlbumSongViewHolder private constructor(private val binding: ItemA
contentDescription = context.getString(R.string.def_track)
}
binding.songTrackBg.imageAlpha = 255
// Normally we would not re-load the drawable each time and instead
// change the alpha value, but Lollipop gets in our way yet again
// and does some stupid insanity with the alpha value that results
// in such branching.
binding.songTrackBg.setImageDrawable(
StyledImageView.StyledDrawable(binding.context, R.drawable.ic_song))
}
binding.songName.textSafe = item.resolveName(binding.context)
@@ -17,7 +17,6 @@
package org.oxycblt.auxio.music
import android.content.ContentResolver
import android.content.ContentUris
import android.content.Context
import android.net.Uri
@@ -26,6 +25,7 @@ import androidx.core.database.getIntOrNull
import androidx.core.database.getStringOrNull
import org.oxycblt.auxio.music.excluded.ExcludedDatabase
import org.oxycblt.auxio.ui.Sort
import org.oxycblt.auxio.util.contentResolverSafe
import org.oxycblt.auxio.util.logD
/**
@@ -99,13 +99,6 @@ object Indexer {
@Suppress("InlinedApi")
private const val AUDIO_COLUMN_ALBUM_ARTIST = MediaStore.Audio.AudioColumns.ALBUM_ARTIST
/**
* Gets a content resolver in a way that does not mangle metadata on certain OEM skins. See
* https://github.com/OxygenCobalt/Auxio/issues/50 for more info.
*/
private val Context.contentResolverSafe: ContentResolver
get() = applicationContext.contentResolver
fun index(context: Context): MusicStore.Library? {
val songs = loadSongs(context)
if (songs.isEmpty()) return null
@@ -18,7 +18,6 @@
package org.oxycblt.auxio.music
import android.Manifest
import android.content.ContentResolver
import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
@@ -27,6 +26,7 @@ import androidx.core.content.ContextCompat
import java.lang.Exception
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.oxycblt.auxio.util.contentResolverSafe
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.logE
@@ -115,15 +115,16 @@ class MusicStore private constructor() {
* uri.
* @return The corresponding [Song] for this [uri], null if there isn't one.
*/
fun findSongForUri(uri: Uri, resolver: ContentResolver): Song? {
resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use {
cursor ->
cursor.moveToFirst()
val fileName =
cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
fun findSongForUri(context: Context, uri: Uri): Song? {
context.contentResolverSafe.query(
uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)
?.use { cursor ->
cursor.moveToFirst()
val fileName =
cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
return songs.find { it.fileName == fileName }
}
return songs.find { it.fileName == fileName }
}
return null
}
@@ -23,14 +23,13 @@ import android.view.LayoutInflater
import android.view.WindowInsets
import androidx.core.view.updatePadding
import androidx.fragment.app.activityViewModels
import com.google.android.material.color.MaterialColors
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentPlaybackBarBinding
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.ui.MainNavigationAction
import org.oxycblt.auxio.ui.NavigationViewModel
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.getAttrColorSafe
import org.oxycblt.auxio.util.getColorStateListSafe
import org.oxycblt.auxio.util.systemBarInsetsCompat
import org.oxycblt.auxio.util.textSafe
@@ -82,20 +81,17 @@ class PlaybackBarFragment : ViewBindingFragment<FragmentPlaybackBarBinding>() {
}
}
// Load the track color in manually as it's unclear whether the track actually supports
// using a ColorStateList in the resources
binding.playbackProgressBar.trackColor =
requireContext().getColorStateListSafe(R.color.sel_track).defaultColor
binding.playbackSkipPrev?.setOnClickListener { playbackModel.prev() }
binding.playbackPlayPause.setOnClickListener { playbackModel.invertPlaying() }
binding.playbackSkipNext?.setOnClickListener { playbackModel.next() }
// Deliberately override the progress bar color [in a Lollipop-friendly way] so that
// we use colorSecondary instead of colorSurfaceVariant. This is because
// colorSurfaceVariant is used with the assumption that the view that is using it is
// not elevated and is therefore not colored. This view is elevated.
binding.playbackProgressBar.trackColor =
MaterialColors.compositeARGBWithAlpha(
requireContext().getAttrColorSafe(R.attr.colorSecondary), (255 * 0.2).toInt())
// -- VIEWMODEL SETUP ---
playbackModel.song.observe(viewLifecycleOwner, ::updateSong)
@@ -24,7 +24,6 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.google.android.material.color.MaterialColors
import com.google.android.material.slider.Slider
import kotlin.math.max
import org.oxycblt.auxio.R
@@ -36,9 +35,7 @@ import org.oxycblt.auxio.ui.MainNavigationAction
import org.oxycblt.auxio.ui.NavigationViewModel
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.formatDuration
import org.oxycblt.auxio.util.getAttrColorSafe
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.stateList
import org.oxycblt.auxio.util.systemBarInsetsCompat
import org.oxycblt.auxio.util.textSafe
@@ -98,12 +95,6 @@ class PlaybackPanelFragment :
binding.playbackSeekBar.apply {
addOnChangeListener(this@PlaybackPanelFragment)
addOnSliderTouchListener(this@PlaybackPanelFragment)
// Composite a tint list based on the active/inactive colors
trackInactiveTintList =
MaterialColors.compositeARGBWithAlpha(
context.getAttrColorSafe(R.attr.colorSecondary), (255 * 0.2).toInt())
.stateList
}
binding.playbackRepeat.setOnClickListener { playbackModel.incrementRepeatMode() }
@@ -99,43 +99,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback, MusicStore
// --- PLAYING FUNCTIONS ---
/**
* Perform the given [DelayedAction].
*
* A "delayed action" is a class of playback actions that must have music present to function,
* usually alongside a context too. Examples include:
* - Opening files
* - Restoring the playback state
* - Future app shortcuts
*
* We would normally want to put this kind of functionality into PlaybackService, but it's
* lifecycle makes that more or less impossible.
*/
fun performAction(context: Context, action: DelayedAction) {
val library = musicStore.library
val actionImpl = DelayedActionImpl(context.applicationContext, action)
if (library != null) {
performActionImpl(actionImpl, library)
} else {
pendingDelayedAction = actionImpl
}
}
private fun performActionImpl(action: DelayedActionImpl, library: MusicStore.Library) {
when (action.inner) {
is DelayedAction.RestoreState -> {
if (!playbackManager.isInitialized) {
viewModelScope.launch { playbackManager.restoreState(action.context) }
}
}
is DelayedAction.Open -> {
library
.findSongForUri(action.inner.uri, action.context.contentResolver)
?.let(::play)
}
}
}
/**
* Play a [song] with the [mode] specified. [mode] will default to the preferred song playback
* mode of the user if not specified.
@@ -188,6 +151,41 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback, MusicStore
playbackManager.shuffleAll()
}
/**
* Perform the given [DelayedAction].
*
* A "delayed action" is a class of playback actions that must have music present to function,
* usually alongside a context too. Examples include:
* - Opening files
* - Restoring the playback state
* - Future app shortcuts
*
* We would normally want to put this kind of functionality into PlaybackService, but it's
* lifecycle makes that more or less impossible.
*/
fun performAction(context: Context, action: DelayedAction) {
val library = musicStore.library
val actionImpl = DelayedActionImpl(context.applicationContext, action)
if (library != null) {
performActionImpl(actionImpl, library)
} else {
pendingDelayedAction = actionImpl
}
}
private fun performActionImpl(action: DelayedActionImpl, library: MusicStore.Library) {
when (action.inner) {
is DelayedAction.RestoreState -> {
if (!playbackManager.isInitialized) {
viewModelScope.launch { playbackManager.restoreState(action.context) }
}
}
is DelayedAction.Open -> {
library.findSongForUri(action.context, action.inner.uri)?.let(::play)
}
}
}
// --- POSITION FUNCTIONS ---
/** Update the position and push it to [PlaybackStateManager] */
@@ -64,7 +64,7 @@ class NotificationComponent(
notificationManager.createNotificationChannel(channel)
}
setSmallIcon(R.drawable.ic_auxio)
setSmallIcon(R.drawable.ic_song)
setCategory(NotificationCompat.CATEGORY_SERVICE)
setShowWhen(false)
setSilent(true)
@@ -75,7 +75,7 @@ class NotificationComponent(
addAction(buildAction(context, PlaybackService.ACTION_SKIP_PREV, R.drawable.ic_skip_prev))
addAction(buildPlayPauseAction(context, true))
addAction(buildAction(context, PlaybackService.ACTION_SKIP_NEXT, R.drawable.ic_skip_next))
addAction(buildAction(context, PlaybackService.ACTION_EXIT, R.drawable.ic_exit))
addAction(buildAction(context, PlaybackService.ACTION_EXIT, R.drawable.ic_close))
setStyle(MediaStyle().setMediaSession(sessionToken).setShowActionsInCompactView(1, 2, 3))
}
@@ -100,7 +100,7 @@ class NotificationComponent(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSubText(parent?.resolveName(context) ?: context.getString(R.string.lbl_all_songs))
} else {
setSubText(song.resolveName(context))
setSubText(song.album.resolveName(context))
}
provider.load(
@@ -108,7 +108,6 @@ class NotificationComponent(
object : BitmapProvider.Target {
override fun onCompleted(bitmap: Bitmap?) {
setLargeIcon(bitmap)
setLargeIcon(null)
callback.onNotificationChanged(this@NotificationComponent)
}
})
@@ -171,7 +170,7 @@ class NotificationComponent(
isShuffled: Boolean
): NotificationCompat.Action {
val drawableRes =
if (isShuffled) R.drawable.ic_shuffle else R.drawable.ic_remote_shuffle_off
if (isShuffled) R.drawable.ic_shuffle_state else R.drawable.ic_remote_shuffle_off
return buildAction(context, PlaybackService.ACTION_INVERT_SHUFFLE, drawableRes)
}
@@ -209,8 +209,8 @@ class SettingsListFragment : PreferenceFragmentCompat() {
private fun Int.toThemeIcon(): Int {
return when (this) {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> R.drawable.ic_auto
AppCompatDelegate.MODE_NIGHT_NO -> R.drawable.ic_day
AppCompatDelegate.MODE_NIGHT_YES -> R.drawable.ic_night
AppCompatDelegate.MODE_NIGHT_NO -> R.drawable.ic_light
AppCompatDelegate.MODE_NIGHT_YES -> R.drawable.ic_dark
else -> R.drawable.ic_auto
}
}
@@ -33,9 +33,10 @@ import org.oxycblt.auxio.util.systemBarInsetsCompat
/**
* An [AppBarLayout] that fixes a bug with the default implementation where the lifted state will
* not properly respond to RecyclerView events. **Note:** This layout relies on
* [AppBarLayout.liftOnScrollTargetViewId] to figure out what scrolling view to use. Failure to
* specify this will result in the layout not working.
* not properly respond to RecyclerView events.
*
* **Note:** This layout relies on [AppBarLayout.liftOnScrollTargetViewId] to figure out what
* scrolling view to use. Failure to specify this will result in the layout not working.
*/
open class EdgeAppBarLayout
@JvmOverloads
@@ -27,6 +27,7 @@ import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.graphics.drawable.DrawableCompat
import coil.dispose
import coil.load
import com.google.android.material.shape.MaterialShapeDrawable
@@ -40,6 +41,7 @@ import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.util.getColorStateListSafe
import org.oxycblt.auxio.util.getDrawableSafe
import org.oxycblt.auxio.util.logD
/**
* An [AppCompatImageView] that applies many of the stylistic choices that Auxio uses regarding
@@ -71,7 +73,7 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
// If we have a pre-set drawable, ensure that it's half-size.
val drawable = drawable
if (drawable != null) {
setImageDrawable(HalfSizeDrawable(drawable))
setImageDrawable(StyledDrawable(context, drawable))
}
}
@@ -106,16 +108,29 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
private fun <T : Music> load(music: T, @DrawableRes error: Int, @StringRes desc: Int) {
dispose()
load(music) {
error(HalfSizeDrawable(context, error))
error(StyledDrawable(context, error))
transformations(SquareFrameTransform.INSTANCE)
}
contentDescription = context.getString(desc, music.resolveName(context))
}
private class HalfSizeDrawable(private val src: Drawable) : Drawable() {
constructor(context: Context, @DrawableRes res: Int) : this(context.getDrawableSafe(res))
/**
* A companion drawable that can be used with the style that [StyledImageView] provides.
* @author OxygenCobalt
*/
class StyledDrawable(context: Context, private val src: Drawable) : Drawable() {
constructor(
context: Context,
@DrawableRes res: Int
) : this(context, context.getDrawableSafe(res))
init {
// Re-tint the drawable to something that will play along with the background
DrawableCompat.setTintList(src, context.getColorStateListSafe(R.color.sel_on_cover_bg))
}
override fun draw(canvas: Canvas) {
logD(src.alpha)
src.bounds.set(canvas.clipBounds)
val adjustWidth = src.bounds.width() / 4
val adjustHeight = src.bounds.height() / 4
@@ -18,6 +18,7 @@
package org.oxycblt.auxio.util
import android.app.PendingIntent
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
@@ -60,6 +61,13 @@ val Context.isNight: Boolean
val Context.isLandscape
get() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
/**
* Gets a content resolver in a way that does not mangle metadata on certain OEM skins. See
* https://github.com/OxygenCobalt/Auxio/issues/50 for more info.
*/
val Context.contentResolverSafe: ContentResolver
get() = applicationContext.contentResolver
/**
* Convenience method for getting a plural.
* @param pluralsRes Resource for the plural
@@ -178,6 +186,11 @@ fun Context.getDimenOffsetSafe(@DimenRes dimen: Int): Int {
}
}
/**
* Calculates the pixels of the given dimension [dp].
* @param dp the dimension value
* @return The equivalent amount of pixels for [dp].
*/
@Px
fun Context.pxOfDp(@Dimension dp: Float): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics)