From 8ada758ea3ef5df5189b4338c941467588fab2ad Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Fri, 18 Jun 2021 14:30:06 -0400 Subject: [PATCH] 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 --- packages/SystemUI/docs/qs-tiles.md | 1 + .../systemui/qs/tileimpl/QSTileViewImpl.kt | 41 ++++++++++++++++--- .../qs/tileimpl/TilesStatesTextTest.kt | 29 ++++++++++++- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/packages/SystemUI/docs/qs-tiles.md b/packages/SystemUI/docs/qs-tiles.md index 89c28a074c76e..efcb2de0f6c30 100644 --- a/packages/SystemUI/docs/qs-tiles.md +++ b/packages/SystemUI/docs/qs-tiles.md @@ -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. 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_`. 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 diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt index 8aed0a88fa35d..5e70b453501e3 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt @@ -492,11 +492,7 @@ open class QSTileViewImpl @JvmOverloads constructor( } return if (state.state == Tile.STATE_UNAVAILABLE || state is BooleanState) { - val resName = "$TILE_STATE_RES_PREFIX${state.spec}" - var arrayResId = resources.getIdentifier(resName, "array", context.packageName) - if (arrayResId == 0) { - arrayResId = R.array.tile_states_default - } + var arrayResId = SubtitleArrayMapping.getSubtitleId(state.spec) val array = resources.getStringArray(arrayResId) array[state.state] } else { @@ -557,6 +553,41 @@ open class QSTileViewImpl @JvmOverloads constructor( private fun getChevronColorForState(state: Int): Int = getSecondaryLabelColorForState(state) } +@VisibleForTesting +internal object SubtitleArrayMapping { + private val subtitleIdsMap = mapOf( + "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 { return PropertyValuesHolder.ofInt(name, *values).apply { setEvaluator(ArgbEvaluator.getInstance()) diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tileimpl/TilesStatesTextTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/tileimpl/TilesStatesTextTest.kt index 19ffa4991eaf4..b8d018f5f8503 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/qs/tileimpl/TilesStatesTextTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tileimpl/TilesStatesTextTest.kt @@ -17,7 +17,7 @@ package com.android.systemui.qs.tileimpl import android.testing.AndroidTestingRunner -import androidx.test.filters.SmallTest +import androidx.test.filters.MediumTest import com.android.systemui.R import com.android.systemui.SysuiTestCase import com.google.common.truth.Truth.assertThat @@ -27,7 +27,7 @@ import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidTestingRunner::class) -@SmallTest +@MediumTest class TilesStatesTextTest : SysuiTestCase() { @Test @@ -51,4 +51,29 @@ class TilesStatesTextTest : SysuiTestCase() { 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