1 /*
2  * Copyright 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 androidx.compose.ui.autofill
18 
19 import android.view.View
20 import android.view.ViewStructure
21 import android.view.autofill.AutofillId
22 import android.view.autofill.AutofillManager
23 import android.view.autofill.AutofillValue
24 import androidx.annotation.RequiresApi
25 
26 /**
27  * This class is here to ensure that the classes that use this API will get verified and can be AOT
28  * compiled. It is expected that this class will soft-fail verification, but the classes which use
29  * this method will pass.
30  */
31 @RequiresApi(28)
32 internal object AutofillApi28Helper {
33     @RequiresApi(28)
setMaxTextLengthnull34     fun setMaxTextLength(structure: ViewStructure, length: Int) = structure.setMaxTextLength(length)
35 }
36 
37 /**
38  * This class is here to ensure that the classes that use this API will get verified and can be AOT
39  * compiled. It is expected that this class will soft-fail verification, but the classes which use
40  * this method will pass.
41  */
42 @RequiresApi(27)
43 internal object AutofillApi27Helper {
44     @RequiresApi(27)
45     fun notifyViewVisibilityChanged(
46         view: View,
47         autofillManager: AutofillManager,
48         semanticsId: Int,
49         isVisible: Boolean
50     ) {
51         autofillManager.notifyViewVisibilityChanged(view, semanticsId, isVisible)
52     }
53 }
54 
55 /**
56  * This class is here to ensure that the classes that use this API will get verified and can be AOT
57  * compiled. It is expected that this class will soft-fail verification, but the classes which use
58  * this method will pass.
59  */
60 @RequiresApi(26)
61 internal object AutofillApi26Helper {
62     @RequiresApi(26)
newChildnull63     fun newChild(structure: ViewStructure, index: Int): ViewStructure = structure.newChild(index)
64 
65     @RequiresApi(26)
66     fun addChildCount(structure: ViewStructure, num: Int) = structure.addChildCount(num)
67 
68     @RequiresApi(26)
69     fun setId(
70         structure: ViewStructure,
71         id: Int,
72         packageName: String?,
73         typeName: String?,
74         entryName: String?
75     ) = structure.setId(id, packageName, typeName, entryName)
76 
77     @RequiresApi(26)
78     fun setDimens(
79         structure: ViewStructure,
80         left: Int,
81         top: Int,
82         scrollX: Int,
83         scrollY: Int,
84         width: Int,
85         height: Int
86     ) = structure.setDimens(left, top, scrollX, scrollY, width, height)
87 
88     @RequiresApi(26) fun getAutofillId(structure: ViewStructure) = structure.autofillId
89 
90     @RequiresApi(26) fun isDate(value: AutofillValue) = value.isDate
91 
92     @RequiresApi(26) fun isList(value: AutofillValue) = value.isList
93 
94     @RequiresApi(26) fun isText(value: AutofillValue) = value.isText
95 
96     @RequiresApi(26) fun isToggle(value: AutofillValue) = value.isToggle
97 
98     @RequiresApi(26)
99     fun setContentDescription(structure: ViewStructure, contentDescription: CharSequence) =
100         structure.setContentDescription(contentDescription)
101 
102     @RequiresApi(26)
103     fun setAutofillHints(structure: ViewStructure, hints: Array<String>) =
104         structure.setAutofillHints(hints)
105 
106     @RequiresApi(26)
107     fun setAutofillId(structure: ViewStructure, parent: AutofillId, virtualId: Int) =
108         structure.setAutofillId(parent, virtualId)
109 
110     @RequiresApi(26)
111     fun setAutofillType(structure: ViewStructure, type: Int) = structure.setAutofillType(type)
112 
113     @RequiresApi(26)
114     fun setAutofillValue(structure: ViewStructure, value: AutofillValue) =
115         structure.setAutofillValue(value)
116 
117     @RequiresApi(26)
118     fun setCheckable(structure: ViewStructure, checkable: Boolean) =
119         structure.setCheckable(checkable)
120 
121     @RequiresApi(26)
122     fun setChecked(structure: ViewStructure, checked: Boolean) = structure.setChecked(checked)
123 
124     @RequiresApi(26)
125     fun setChildCount(structure: ViewStructure, numChildren: Int) {
126         structure.childCount = numChildren
127     }
128 
129     @RequiresApi(26)
setClassNamenull130     fun setClassName(structure: ViewStructure, classname: String) =
131         structure.setClassName(classname)
132 
133     @RequiresApi(26)
134     fun setClickable(structure: ViewStructure, clickable: Boolean) =
135         structure.setClickable(clickable)
136 
137     @RequiresApi(26)
138     fun setDataIsSensitive(structure: ViewStructure, isSensitive: Boolean) =
139         structure.setDataIsSensitive(isSensitive)
140 
141     @RequiresApi(26)
142     fun setEnabled(structure: ViewStructure, enabled: Boolean) = structure.setEnabled(enabled)
143 
144     @RequiresApi(26)
145     fun setFocusable(structure: ViewStructure, focusable: Boolean) =
146         structure.setFocusable(focusable)
147 
148     @RequiresApi(26)
149     fun setFocused(structure: ViewStructure, focused: Boolean) = structure.setFocused(focused)
150 
151     @RequiresApi(26)
152     fun setInputType(structure: ViewStructure, type: Int) = structure.setInputType(type)
153 
154     @RequiresApi(26)
155     fun setLongClickable(structure: ViewStructure, longClickable: Boolean) =
156         structure.setLongClickable(longClickable)
157 
158     @RequiresApi(26)
159     fun setOpaque(structure: ViewStructure, isOpaque: Boolean) = structure.setOpaque(isOpaque)
160 
161     @RequiresApi(26)
162     fun setSelected(structure: ViewStructure, isSelected: Boolean) =
163         structure.setSelected(isSelected)
164 
165     @RequiresApi(26)
166     fun setText(structure: ViewStructure, text: CharSequence) {
167         structure.text = text
168     }
169 
170     @RequiresApi(26)
setVisibilitynull171     fun setVisibility(structure: ViewStructure, visibility: Int) =
172         structure.setVisibility(visibility)
173 
174     @RequiresApi(26) fun textValue(value: AutofillValue): CharSequence = value.textValue
175 
176     @RequiresApi(26) fun booleanValue(value: AutofillValue): Boolean = value.toggleValue
177 
178     @RequiresApi(26) fun listValue(value: AutofillValue): Int = value.listValue
179 
180     @RequiresApi(26)
181     fun getAutofillTextValue(value: String): AutofillValue {
182         return AutofillValue.forText(value)
183     }
184 }
185