1 /* <lambda>null2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.platform.systemui_tapl.ui 17 18 import android.platform.systemui_tapl.controller.VolumeController.RingerMode 19 import android.platform.systemui_tapl.utils.DeviceUtils.sysuiResSelector 20 import android.platform.uiautomatorhelpers.DeviceHelpers.waitForObj 21 import androidx.test.uiautomator.BySelector 22 import androidx.test.uiautomator.UiObject2 23 import com.android.systemui.Flags 24 25 interface VolumeRingerDrawer { 26 27 val selectedMode: RingerMode 28 29 fun selectRingerMode(mode: RingerMode) 30 31 companion object { 32 33 fun get(): VolumeRingerDrawer { 34 return if (Flags.volumeRedesign()) { 35 VolumeRingerDrawerImpl() 36 } else { 37 VolumeRingerDrawerLegacy() 38 } 39 } 40 } 41 } 42 43 /** 44 * Ringer drawer is a container which is opened after clicking ringer mode button. 45 * 46 * Volume dialog(drawer closed): https://hsv.googleplex.com/4762218357850112 47 * 48 * Ringer drawer: https://hsv.googleplex.com/5102770609717248 49 */ 50 private class VolumeRingerDrawerLegacy : VolumeRingerDrawer { 51 52 private val container: UiObject2 53 54 init { 55 val containerSelector = sysuiResSelector("volume_drawer_container") <lambda>null56 this.container = waitForObj(containerSelector) { "Can't find the ringer drawer." } 57 } 58 59 /** 60 * Detect the current selected mode by checking the highlighted ringer icon. The highlighted 61 * icon is the one with the active icon container on the top. 62 */ 63 override val selectedMode: RingerMode 64 get() { 65 val activeIconSel = sysuiResSelector("volume_new_ringer_active_icon_container") 66 val activeIcon = <lambda>null67 waitForObj(activeIconSel) { "Can't find any active icon on the drawer." } 68 val center = activeIcon.visibleCenter 69 return RingerMode.entries <lambda>null70 .filter { it.isAvailable } <lambda>null71 .first { 72 val icon = 73 container.waitForObj(it.getIconSelector()) { 74 "Can't find $it icon on the drawer." 75 } 76 icon.visibleBounds.contains(center.x, center.y) 77 } 78 } 79 80 /** Click the given ringer icon in the drawer. */ selectRingerModenull81 override fun selectRingerMode(mode: RingerMode) { 82 waitForObj(mode.getIconSelector()) { "$mode icon not found" }.click() 83 } 84 getIconSelectornull85 private fun RingerMode.getIconSelector(): BySelector { 86 return when (this) { 87 RingerMode.NORMAL -> "volume_drawer_normal" 88 RingerMode.SILENT -> "volume_drawer_mute" 89 RingerMode.VIBRATE -> "volume_drawer_vibrate" 90 }.let { sysuiResSelector(it) } 91 } 92 } 93 94 class VolumeRingerDrawerImpl : VolumeRingerDrawer { 95 96 private val container: UiObject2 = <lambda>null97 waitForObj(sysuiResSelector("volume_ringer_drawer")) { "Can't find the ringer drawer." } 98 99 /** 100 * Detect the current selected mode by checking the highlighted ringer icon. The highlighted 101 * icon is the one with the active icon container on the top. 102 */ 103 override val selectedMode: RingerMode 104 get() { 105 val selectedIndex = 106 container.children <lambda>null107 .mapIndexedNotNull { index, uiObject2 -> index.takeIf { uiObject2.isSelected } } 108 .single() <lambda>null109 return RingerMode.entries.single { it.getIndex() == selectedIndex } 110 } 111 112 /** Click the given ringer icon in the drawer. */ selectRingerModenull113 override fun selectRingerMode(mode: RingerMode) { 114 val index = mode.getIndex() ?: error("ringer mode is unavailable") 115 container.children[index].click() 116 } 117 getIndexnull118 private fun RingerMode.getIndex(): Int? { 119 return if (!RingerMode.VIBRATE.isAvailable) { 120 when (this) { 121 RingerMode.NORMAL -> 2 122 RingerMode.SILENT -> 1 123 RingerMode.VIBRATE -> null 124 } 125 } else { 126 when (this) { 127 RingerMode.NORMAL -> 3 128 RingerMode.SILENT -> 2 129 RingerMode.VIBRATE -> 1 130 } 131 } 132 } 133 } 134