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

Commit 8ada758e authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Statically map tile spec to array res id

That way, we don't need to use Resources#getIdentifier

Test: atest QSTileViewImpl TilesStatesTextTest
Test: microbenchmark jank test
Fixes: 191483200
Change-Id: I0d92c6e5b9861e5001df5e2ffb7f80734b2b9ff8
parent bd1d8412
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -306,6 +306,7 @@ This section describes necessary and recommended steps when implementing a Quick
    * Add a case to the `switch` with a unique String spec for the chosen tile.
    * Add a case to the `switch` with a unique String spec for the chosen tile.
5. In [SystemUI/res/values/config.xml](/packages/SystemUI/res/values/config.xml), modify `quick_settings_tiles_stock` and add the spec defined in the previous step. If necessary, add it also to `quick_settings_tiles_default`. The first one contains a list of all the tiles that SystemUI knows how to create (to show to the user in the customization screen). The second one contains only the default tiles that the user will experience on a fresh boot or after they reset their tiles.
5. In [SystemUI/res/values/config.xml](/packages/SystemUI/res/values/config.xml), modify `quick_settings_tiles_stock` and add the spec defined in the previous step. If necessary, add it also to `quick_settings_tiles_default`. The first one contains a list of all the tiles that SystemUI knows how to create (to show to the user in the customization screen). The second one contains only the default tiles that the user will experience on a fresh boot or after they reset their tiles.
6. In [SystemUI/res/values/tiles_states_strings.xml](/packages/SystemUI/res/values/tiles_states_strings.xml), add a new array for your tile. The name has to be `tile_states_<spec>`. Use a good description to help the translators.
6. In [SystemUI/res/values/tiles_states_strings.xml](/packages/SystemUI/res/values/tiles_states_strings.xml), add a new array for your tile. The name has to be `tile_states_<spec>`. Use a good description to help the translators.
7. In [`SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt`](/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt), add a new element to the map in `SubtitleArrayMapping` corresponding to the resource created in the previous step.


#### Abstract methods in QSTileImpl
#### Abstract methods in QSTileImpl


+36 −5
Original line number Original line Diff line number Diff line
@@ -492,11 +492,7 @@ open class QSTileViewImpl @JvmOverloads constructor(
        }
        }


        return if (state.state == Tile.STATE_UNAVAILABLE || state is BooleanState) {
        return if (state.state == Tile.STATE_UNAVAILABLE || state is BooleanState) {
            val resName = "$TILE_STATE_RES_PREFIX${state.spec}"
            var arrayResId = SubtitleArrayMapping.getSubtitleId(state.spec)
            var arrayResId = resources.getIdentifier(resName, "array", context.packageName)
            if (arrayResId == 0) {
                arrayResId = R.array.tile_states_default
            }
            val array = resources.getStringArray(arrayResId)
            val array = resources.getStringArray(arrayResId)
            array[state.state]
            array[state.state]
        } else {
        } else {
@@ -557,6 +553,41 @@ open class QSTileViewImpl @JvmOverloads constructor(
    private fun getChevronColorForState(state: Int): Int = getSecondaryLabelColorForState(state)
    private fun getChevronColorForState(state: Int): Int = getSecondaryLabelColorForState(state)
}
}


@VisibleForTesting
internal object SubtitleArrayMapping {
    private val subtitleIdsMap = mapOf<String?, Int>(
        "internet" to R.array.tile_states_internet,
        "wifi" to R.array.tile_states_wifi,
        "cell" to R.array.tile_states_cell,
        "battery" to R.array.tile_states_battery,
        "dnd" to R.array.tile_states_dnd,
        "flashlight" to R.array.tile_states_flashlight,
        "rotation" to R.array.tile_states_rotation,
        "bt" to R.array.tile_states_bt,
        "airplane" to R.array.tile_states_airplane,
        "location" to R.array.tile_states_location,
        "hotspot" to R.array.tile_states_hotspot,
        "inversion" to R.array.tile_states_inversion,
        "saver" to R.array.tile_states_saver,
        "dark" to R.array.tile_states_dark,
        "work" to R.array.tile_states_work,
        "cast" to R.array.tile_states_cast,
        "night" to R.array.tile_states_night,
        "screenrecord" to R.array.tile_states_screenrecord,
        "reverse" to R.array.tile_states_reverse,
        "reduce_brightness" to R.array.tile_states_reduce_brightness,
        "cameratoggle" to R.array.tile_states_cameratoggle,
        "mictoggle" to R.array.tile_states_mictoggle,
        "controls" to R.array.tile_states_controls,
        "wallet" to R.array.tile_states_wallet,
        "alarm" to R.array.tile_states_alarm
    )

    fun getSubtitleId(spec: String?): Int {
        return subtitleIdsMap.getOrDefault(spec, R.array.tile_states_default)
    }
}

private fun colorValuesHolder(name: String, vararg values: Int): PropertyValuesHolder {
private fun colorValuesHolder(name: String, vararg values: Int): PropertyValuesHolder {
    return PropertyValuesHolder.ofInt(name, *values).apply {
    return PropertyValuesHolder.ofInt(name, *values).apply {
        setEvaluator(ArgbEvaluator.getInstance())
        setEvaluator(ArgbEvaluator.getInstance())
+27 −2
Original line number Original line Diff line number Diff line
@@ -17,7 +17,7 @@
package com.android.systemui.qs.tileimpl
package com.android.systemui.qs.tileimpl


import android.testing.AndroidTestingRunner
import android.testing.AndroidTestingRunner
import androidx.test.filters.SmallTest
import androidx.test.filters.MediumTest
import com.android.systemui.R
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.SysuiTestCase
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertThat
@@ -27,7 +27,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runner.RunWith


@RunWith(AndroidTestingRunner::class)
@RunWith(AndroidTestingRunner::class)
@SmallTest
@MediumTest
class TilesStatesTextTest : SysuiTestCase() {
class TilesStatesTextTest : SysuiTestCase() {


    @Test
    @Test
@@ -51,4 +51,29 @@ class TilesStatesTextTest : SysuiTestCase() {


        assertThat(array.size).isEqualTo(3)
        assertThat(array.size).isEqualTo(3)
    }
    }

    @Test
    fun testStockTilesSubtitlesMap() {
        val tiles = mContext.getString(R.string.quick_settings_tiles_stock).split(",")
        tiles.forEach { spec ->
            val resName = "${QSTileViewImpl.TILE_STATE_RES_PREFIX}$spec"
            val resId = mContext.resources.getIdentifier(resName, "array", mContext.packageName)

            assertNotEquals("Missing resource for $resName", 0, resId)

            assertThat(SubtitleArrayMapping.getSubtitleId(spec)).isEqualTo(resId)
        }
    }

    @Test
    fun testStockTilesSubtitlesReturnsDefault_unknown() {
        assertThat(SubtitleArrayMapping.getSubtitleId("unknown"))
            .isEqualTo(R.array.tile_states_default)
    }

    @Test
    fun testStockTilesSubtitlesReturnsDefault_null() {
        assertThat(SubtitleArrayMapping.getSubtitleId(null))
            .isEqualTo(R.array.tile_states_default)
    }
}
}
 No newline at end of file