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

Commit 186667d1 authored by Fabian Kozynski's avatar Fabian Kozynski Committed by Automerger Merge Worker
Browse files

Merge "Statically map tile spec to array res id" into sc-dev am: 648711da

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15028271

Change-Id: I1270009817d711b34f79dd5cfaabb9f976174cb8
parents f0ce1e1e 648711da
Loading
Loading
Loading
Loading
+1 −0
Original line number 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.
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.
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

+36 −5
Original line number Diff line number Diff line
@@ -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<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 {
    return PropertyValuesHolder.ofInt(name, *values).apply {
        setEvaluator(ArgbEvaluator.getInstance())
+27 −2
Original line number Diff line number Diff line
@@ -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