Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0451f444 authored by Luca Stefani's avatar Luca Stefani
Browse files

Handle all exceptions when loading artwork bitmap

Google "fixed" it in 16.

Fixes: https://gitlab.com/LineageOS/issues/android/-/issues/8313
Change-Id: I4d58ad6f369eb37e533c98795dc8f341309e486c
parent ee9b7366
Loading
Loading
Loading
Loading
+10 −37
Original line number Diff line number Diff line
@@ -24,10 +24,8 @@ import android.annotation.WorkerThread
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources
import android.content.res.Resources.NotFoundException
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.graphics.ImageDecoder.DecodeException
import android.graphics.drawable.AdaptiveIconDrawable
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
@@ -39,7 +37,6 @@ import com.android.app.tracing.traceSection
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import java.io.IOException
import javax.inject.Inject
import kotlin.math.min
import kotlinx.coroutines.CoroutineDispatcher
@@ -129,17 +126,14 @@ constructor(
        @Px maxHeight: Int = DEFAULT_MAX_SAFE_BITMAP_SIZE_PX,
        allocator: Int = ImageDecoder.ALLOCATOR_DEFAULT
    ): Bitmap? {
        return try {
        return runCatching {
            loadBitmapSync(
                toImageDecoderSource(source, defaultContext),
                maxWidth,
                maxHeight,
                allocator
            )
        } catch (e: NotFoundException) {
            Log.w(TAG, "Couldn't load resource $source", e)
            null
        }
        }.getOrNull()
    }

    /**
@@ -165,18 +159,12 @@ constructor(
        allocator: Int = ImageDecoder.ALLOCATOR_DEFAULT
    ): Bitmap? =
        traceSection("ImageLoader#loadBitmap") {
            return try {
            return runCatching {
                ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
                    configureDecoderForMaximumSize(decoder, info.size, maxWidth, maxHeight)
                    decoder.allocator = allocator
                }
            } catch (e: IOException) {
                Log.w(TAG, "Failed to load source $source", e)
                return null
            } catch (e: DecodeException) {
                Log.w(TAG, "Failed to decode source $source", e)
                return null
            }
            }.getOrNull()
        }

    /**
@@ -257,7 +245,7 @@ constructor(
        allocator: Int = ImageDecoder.ALLOCATOR_DEFAULT
    ): Drawable? =
        traceSection("ImageLoader#loadDrawable") {
            return try {
            return runCatching {
                loadDrawableSync(
                    toImageDecoderSource(source, defaultContext),
                    maxWidth,
@@ -275,10 +263,7 @@ constructor(
                    } else {
                        null
                    }
            } catch (e: NotFoundException) {
                Log.w(TAG, "Couldn't load resource $source", e)
                null
            }
            }.getOrNull()
        }

    /**
@@ -304,18 +289,12 @@ constructor(
        allocator: Int = ImageDecoder.ALLOCATOR_DEFAULT
    ): Drawable? =
        traceSection("ImageLoader#loadDrawable") {
            return try {
            return runCatching {
                ImageDecoder.decodeDrawable(source) { decoder, info, _ ->
                    configureDecoderForMaximumSize(decoder, info.size, maxWidth, maxHeight)
                    decoder.allocator = allocator
                }
            } catch (e: IOException) {
                Log.w(TAG, "Failed to load source $source", e)
                return null
            } catch (e: DecodeException) {
                Log.w(TAG, "Failed to decode source $source", e)
                return null
            }
            }.getOrNull()
        }

    /** Loads icon drawable while attempting to size restrict the drawable. */
@@ -419,15 +398,9 @@ constructor(
     */
    @WorkerThread
    fun loadSizeSync(source: ImageDecoder.Source): Size? {
        return try {
        return runCatching {
            ImageDecoder.decodeHeader(source).size
        } catch (e: IOException) {
            Log.w(TAG, "Failed to load source $source", e)
            return null
        } catch (e: DecodeException) {
            Log.w(TAG, "Failed to decode source $source", e)
            return null
        }
        }.getOrNull()
    }

    companion object {