• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 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 
17 package android.platform.systemui_tapl.volume.panel.ui
18 
19 import android.platform.systemui_tapl.utils.DeviceUtils
20 import android.platform.uiautomatorhelpers.DeviceHelpers.waitForObj
21 import android.view.accessibility.AccessibilityNodeInfo
22 import androidx.test.uiautomator.Direction
23 import androidx.test.uiautomator.UiObject2
24 
25 /**
26  * An access point for the Volume Panel slider.
27  *
28  * @see VolumePanel.slider
29  */
30 interface VolumePanelSlider {
31 
32     /** Scrolls until the [targetProgress] is met. */
33     fun scrollToProgress(targetProgress: Float)
34 }
35 
36 internal class VolumePanelSliderImpl(container: UiObject2, @SliderStream streamType: Int) :
37     VolumePanelSlider {
38 
39     private val sliderObject: UiObject2 =
<lambda>null40         container.waitForObj(DeviceUtils.sysuiResSelector(SliderStream.getResourceId(streamType))) {
41             "Can't find slider=$streamType"
42         }
43 
scrollToProgressnull44     override fun scrollToProgress(targetProgress: Float) {
45         val currentProgress = sliderObject.getProgress()
46         val direction =
47             when {
48                 targetProgress > currentProgress -> Direction.LEFT
49                 targetProgress < currentProgress -> Direction.RIGHT
50                 else -> return
51             }
52         sliderObject.scrollUntil(direction) { obj -> obj.getProgress() == targetProgress }
53     }
54 
55     // TODO: b/349817024 - Remove this method in favour of the new API
getProgressnull56     private fun UiObject2.getProgress(): Float =
57         with(javaClass.getDeclaredMethod("getAccessibilityNodeInfo")) {
58             val originalIsAccessible = isAccessible
59             return try {
60                 isAccessible = true
61                 val rangeInfo =
62                     (invoke(this@getProgress) as AccessibilityNodeInfo).rangeInfo
63                         ?: throw RuntimeException(
64                             "No RangeInfo found for the object=[$contentDescription, $resourceName]"
65                         )
66                 rangeInfo.current
67             } finally {
68                 isAccessible = originalIsAccessible
69             }
70         }
71 }
72