[tile] Consolidate some functions to be reuse by LauncherTileService.

Bug: 262443413
Test: manual
Change-Id: Idb8c8e17623b6dddc22aa5bf69834dcbb87a3626
diff --git a/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java b/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java
index 2b16999..1d28c63 100644
--- a/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java
+++ b/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java
@@ -16,9 +16,11 @@
 
 import android.annotation.NonNull;
 import android.content.Context;
+import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.metrics.LogMaker;
 import android.service.quicksettings.Tile;
+import android.text.TextUtils;
 import android.view.View;
 
 import androidx.annotation.Nullable;
@@ -175,6 +177,24 @@
         public Drawable sideViewCustomDrawable;
         public String spec;
 
+        /** Get the state text. */
+        public String getStateText(int arrayResId, Resources resources) {
+            if (state == Tile.STATE_UNAVAILABLE || this instanceof QSTile.BooleanState) {
+                String[] array = resources.getStringArray(arrayResId);
+                return array[state];
+            } else {
+                return "";
+            }
+        }
+
+        /** Get the text for secondaryLabel. */
+        public String getSecondaryLabel(String stateText) {
+            if (TextUtils.isEmpty(secondaryLabel)) {
+                return stateText;
+            }
+            return secondaryLabel.toString();
+        }
+
         public boolean copyTo(State other) {
             if (other == null) throw new IllegalArgumentException();
             if (!other.getClass().equals(getClass())) throw new IllegalArgumentException();
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java
index 2cffe89..49ba508 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java
@@ -712,6 +712,10 @@
             return context.getDrawable(mResId);
         }
 
+        public int getResId() {
+            return mResId;
+        }
+
         @Override
         public boolean equals(Object o) {
             return o instanceof ResourceIcon && ((ResourceIcon) o).mResId == mResId;
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 d0b04c9..de1137e 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt
+++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt
@@ -440,12 +440,11 @@
 
         // State handling and description
         val stateDescription = StringBuilder()
-        val stateText = getStateText(state)
+        val arrayResId = SubtitleArrayMapping.getSubtitleId(state.spec)
+        val stateText = state.getStateText(arrayResId, resources)
+        state.secondaryLabel = state.getSecondaryLabel(stateText)
         if (!TextUtils.isEmpty(stateText)) {
             stateDescription.append(stateText)
-            if (TextUtils.isEmpty(state.secondaryLabel)) {
-                state.secondaryLabel = stateText
-            }
         }
         if (state.disabledByPolicy && state.state != Tile.STATE_UNAVAILABLE) {
             stateDescription.append(", ")
@@ -591,16 +590,6 @@
         return resources.getStringArray(arrayResId)[Tile.STATE_UNAVAILABLE]
     }
 
-    private fun getStateText(state: QSTile.State): String {
-        return if (state.state == Tile.STATE_UNAVAILABLE || state is BooleanState) {
-            val arrayResId = SubtitleArrayMapping.getSubtitleId(state.spec)
-            val array = resources.getStringArray(arrayResId)
-            array[state.state]
-        } else {
-            ""
-        }
-    }
-
     /*
      * The view should not be animated if it's not on screen and no part of it is visible.
      */
@@ -663,46 +652,6 @@
     )
 }
 
-@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,
-        "qr_code_scanner" to R.array.tile_states_qr_code_scanner,
-        "alarm" to R.array.tile_states_alarm,
-        "onehanded" to R.array.tile_states_onehanded,
-        "color_correction" to R.array.tile_states_color_correction,
-        "dream" to R.array.tile_states_dream,
-        "font_scaling" to R.array.tile_states_font_scaling
-    )
-
-    fun getSubtitleId(spec: String?): Int {
-        return subtitleIdsMap.getOrDefault(spec, R.array.tile_states_default)
-    }
-}
-
 fun constrainSquishiness(squish: Float): Float {
     return 0.1f + squish * 0.9f
 }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/SubtitleArrayMapping.kt b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/SubtitleArrayMapping.kt
new file mode 100644
index 0000000..f672e51
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/SubtitleArrayMapping.kt
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.systemui.qs.tileimpl
+
+import com.android.systemui.R
+
+/** Return the subtitle resource Id of the given tile. */
+object SubtitleArrayMapping {
+    private val subtitleIdsMap: HashMap<String, Int> = HashMap()
+    init {
+        subtitleIdsMap["internet"] = R.array.tile_states_internet
+        subtitleIdsMap["wifi"] = R.array.tile_states_wifi
+        subtitleIdsMap["cell"] = R.array.tile_states_cell
+        subtitleIdsMap["battery"] = R.array.tile_states_battery
+        subtitleIdsMap["dnd"] = R.array.tile_states_dnd
+        subtitleIdsMap["flashlight"] = R.array.tile_states_flashlight
+        subtitleIdsMap["rotation"] = R.array.tile_states_rotation
+        subtitleIdsMap["bt"] = R.array.tile_states_bt
+        subtitleIdsMap["airplane"] = R.array.tile_states_airplane
+        subtitleIdsMap["location"] = R.array.tile_states_location
+        subtitleIdsMap["hotspot"] = R.array.tile_states_hotspot
+        subtitleIdsMap["inversion"] = R.array.tile_states_inversion
+        subtitleIdsMap["saver"] = R.array.tile_states_saver
+        subtitleIdsMap["dark"] = R.array.tile_states_dark
+        subtitleIdsMap["work"] = R.array.tile_states_work
+        subtitleIdsMap["cast"] = R.array.tile_states_cast
+        subtitleIdsMap["night"] = R.array.tile_states_night
+        subtitleIdsMap["screenrecord"] = R.array.tile_states_screenrecord
+        subtitleIdsMap["reverse"] = R.array.tile_states_reverse
+        subtitleIdsMap["reduce_brightness"] = R.array.tile_states_reduce_brightness
+        subtitleIdsMap["cameratoggle"] = R.array.tile_states_cameratoggle
+        subtitleIdsMap["mictoggle"] = R.array.tile_states_mictoggle
+        subtitleIdsMap["controls"] = R.array.tile_states_controls
+        subtitleIdsMap["wallet"] = R.array.tile_states_wallet
+        subtitleIdsMap["qr_code_scanner"] = R.array.tile_states_qr_code_scanner
+        subtitleIdsMap["alarm"] = R.array.tile_states_alarm
+        subtitleIdsMap["onehanded"] = R.array.tile_states_onehanded
+        subtitleIdsMap["color_correction"] = R.array.tile_states_color_correction
+        subtitleIdsMap["dream"] = R.array.tile_states_dream
+        subtitleIdsMap["font_scaling"] = R.array.tile_states_font_scaling
+    }
+
+    /** Get the subtitle resource id of the given tile */
+    fun getSubtitleId(spec: String?): Int {
+        return if (spec == null) {
+            R.array.tile_states_default
+        } else subtitleIdsMap[spec] ?: R.array.tile_states_default
+    }
+}