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

Commit 40177aec authored by Mill Chen's avatar Mill Chen
Browse files

Fix ArrayIndexOutOfBoundsException

The crash occurred when drawing the rounded background. To manage this
error, we can fix it by checking the array's length before accessing an
index.

Fix: 434260825
Test: manual test
Flag: EXEMPT bugfix
Change-Id: I6465f7275e73ce9a7d6c14b2bf09c734d7aa2d2f
parent 7edf7c84
Loading
Loading
Loading
Loading
+7 −1
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.SuppressLint
import android.graphics.Rect
import android.graphics.Rect
import android.os.Handler
import android.os.Handler
import android.os.Looper
import android.os.Looper
import android.util.Log
import android.util.TypedValue
import android.util.TypedValue
import android.view.View
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.DrawableRes
@@ -203,7 +204,7 @@ open class SettingsPreferenceGroupAdapter(preferenceGroup: PreferenceGroup) :
            val hasIconSpace = iconFrame != null && iconFrame.visibility != View.GONE
            val hasIconSpace = iconFrame != null && iconFrame.visibility != View.GONE
            drawableStateLayout.extraDrawableState =
            drawableStateLayout.extraDrawableState =
                stateSetOf(mItemPositionStates[position], hasIconSpace)
                stateSetOf(mItemPositionStates[position], hasIconSpace)
        } else {
        } else { // Handle the background of the preferences that are group divider
            val backgroundRes = getRoundCornerDrawableRes(position, isSelected = false)
            val backgroundRes = getRoundCornerDrawableRes(position, isSelected = false)
            val (paddingStart, paddingEnd) = getStartEndPadding(position)
            val (paddingStart, paddingEnd) = getStartEndPadding(position)
            v.setPaddingRelative(paddingStart, v.paddingTop, paddingEnd, v.paddingBottom)
            v.setPaddingRelative(paddingStart, v.paddingTop, paddingEnd, v.paddingBottom)
@@ -214,6 +215,10 @@ open class SettingsPreferenceGroupAdapter(preferenceGroup: PreferenceGroup) :


    private fun getStartEndPadding(position: Int): Pair<Int, Int> {
    private fun getStartEndPadding(position: Int): Pair<Int, Int> {
        val item = getItem(position)
        val item = getItem(position)
        if (position >= mItemPositionStates.size) {
            Log.e(TAG, "IndexOutOfBounds: ${item?.title} in $position")
            return 0 to 0
        }
        val positionState = mItemPositionStates[position]
        val positionState = mItemPositionStates[position]
        return when {
        return when {
            // This item handles edge to edge itself
            // This item handles edge to edge itself
@@ -283,6 +288,7 @@ open class SettingsPreferenceGroupAdapter(preferenceGroup: PreferenceGroup) :
                || preference is SpacePreference
                || preference is SpacePreference


    companion object {
    companion object {
        private val TAG = "SettingsPrefGroupAdapter"
        private val STATE_SET_NONE = intArrayOf()
        private val STATE_SET_NONE = intArrayOf()
        private val STATE_SET_SINGLE = intArrayOf(android.R.attr.state_single)
        private val STATE_SET_SINGLE = intArrayOf(android.R.attr.state_single)
        private val STATE_SET_FIRST = intArrayOf(android.R.attr.state_first)
        private val STATE_SET_FIRST = intArrayOf(android.R.attr.state_first)