1 /*
2  * Copyright 2023 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.core.view;
18 
19 import static android.os.Build.VERSION.SDK_INT;
20 
21 import android.view.ViewStructure;
22 
23 import androidx.annotation.RequiresApi;
24 
25 import org.jspecify.annotations.NonNull;
26 
27 /**
28  * Helper for accessing features in {@link ViewStructure}.
29  * <p>
30  * Currently this helper class only has features for content capture usage. Other features for
31  * Autofill are not available.
32  */
33 public class ViewStructureCompat {
34 
35     // Only guaranteed to be non-null on SDK_INT >= 23.
36     private final Object mWrappedObj;
37 
38     /**
39      * Provides a backward-compatible wrapper for {@link ViewStructure}.
40      * <p>
41      * This method is not supported on devices running SDK < 23 since the platform
42      * class will not be available.
43      *
44      * @param contentCaptureSession platform class to wrap
45      * @return wrapped class
46      */
47     @RequiresApi(23)
toViewStructureCompat( @onNull ViewStructure contentCaptureSession)48     public static @NonNull ViewStructureCompat toViewStructureCompat(
49             @NonNull ViewStructure contentCaptureSession) {
50         return new ViewStructureCompat(contentCaptureSession);
51     }
52 
53     /**
54      * Provides the {@link ViewStructure} represented by this object.
55      * <p>
56      * This method is not supported on devices running SDK < 23 since the platform
57      * class will not be available.
58      *
59      * @return platform class object
60      * @see ViewStructureCompat#toViewStructureCompat(ViewStructure)
61      */
62     @RequiresApi(23)
toViewStructure()63     public @NonNull ViewStructure toViewStructure() {
64         return (ViewStructure) mWrappedObj;
65     }
66 
ViewStructureCompat(@onNull ViewStructure viewStructure)67     private ViewStructureCompat(@NonNull ViewStructure viewStructure) {
68         this.mWrappedObj = viewStructure;
69     }
70 
71     /**
72      * Set the text that is associated with this view.  There is no selection
73      * associated with the text.  The text may have style spans to supply additional
74      * display and semantic information.
75      *
76      * Compatibility behavior:
77      * <ul>
78      * <li>SDK 23 and above, this method matches platform behavior.
79      * <li>SDK 22 and below, this method does nothing.
80      * </ul>
81      */
setText(@onNull CharSequence charSequence)82     public void setText(@NonNull CharSequence charSequence) {
83         if (SDK_INT >= 23) {
84             Api23Impl.setText((ViewStructure) mWrappedObj, charSequence);
85         }
86     }
87 
88     /**
89      * Set the class name of the view, as per
90      * {@link android.view.View#getAccessibilityClassName View.getAccessibilityClassName()}.
91      *
92      * Compatibility behavior:
93      * <ul>
94      * <li>SDK 23 and above, this method matches platform behavior.
95      * <li>SDK 22 and below, this method does nothing.
96      * </ul>
97      */
setClassName(@onNull String string)98     public void setClassName(@NonNull String string) {
99         if (SDK_INT >= 23) {
100             Api23Impl.setClassName((ViewStructure) mWrappedObj, string);
101         }
102     }
103 
104     /**
105      * Set the content description of the view, as per
106      * {@link android.view.View#getContentDescription View.getContentDescription()}.
107      *
108      * Compatibility behavior:
109      * <ul>
110      * <li>SDK 23 and above, this method matches platform behavior.
111      * <li>SDK 22 and below, this method does nothing.
112      * </ul>
113      */
setContentDescription(@onNull CharSequence charSequence)114     public void setContentDescription(@NonNull CharSequence charSequence) {
115         if (SDK_INT >= 23) {
116             Api23Impl.setContentDescription((ViewStructure) mWrappedObj, charSequence);
117         }
118     }
119 
120     /**
121      * Set the basic dimensions of this view.
122      *
123      * @param left The view's left position, in pixels relative to its parent's left edge.
124      * @param top The view's top position, in pixels relative to its parent's top edge.
125      * @param scrollX How much the view's x coordinate space has been scrolled, in pixels.
126      * @param scrollY How much the view's y coordinate space has been scrolled, in pixels.
127      * @param width The view's visible width, in pixels.  This is the width visible on screen,
128      * not the total data width of a scrollable view.
129      * @param height The view's visible height, in pixels.  This is the height visible on
130      * screen, not the total data height of a scrollable view.
131      *
132      * Compatibility behavior:
133      * <ul>
134      * <li>SDK 23 and above, this method matches platform behavior.
135      * <li>SDK 22 and below, this method does nothing.
136      * </ul>
137      */
setDimens(int left, int top, int scrollX, int scrollY, int width, int height)138     public void setDimens(int left, int top, int scrollX, int scrollY, int width, int height) {
139         if (SDK_INT >= 23) {
140             Api23Impl.setDimens(
141                     (ViewStructure) mWrappedObj, left, top, scrollX, scrollY, width, height);
142         }
143     }
144 
145     @RequiresApi(23)
146     private static class Api23Impl {
Api23Impl()147         private Api23Impl() {
148             // This class is not instantiable.
149         }
150 
setDimens(ViewStructure viewStructure, int left, int top, int scrollX, int scrollY, int width, int height)151         static void setDimens(ViewStructure viewStructure, int left, int top, int scrollX,
152                 int scrollY, int width, int height) {
153             viewStructure.setDimens(left, top, scrollX, scrollY, width, height);
154         }
155 
setText(ViewStructure viewStructure, CharSequence charSequence)156         static void setText(ViewStructure viewStructure, CharSequence charSequence) {
157             viewStructure.setText(charSequence);
158         }
159 
setClassName(ViewStructure viewStructure, String string)160         static void setClassName(ViewStructure viewStructure, String string) {
161             viewStructure.setClassName(string);
162         }
163 
setContentDescription(ViewStructure viewStructure, CharSequence charSequence)164         static void setContentDescription(ViewStructure viewStructure, CharSequence charSequence) {
165             viewStructure.setContentDescription(charSequence);
166         }
167     }
168 }
169