1 /*
2  * Copyright (C) 2011 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.view;
18 
19 import static com.android.window.flags.Flags.surfaceTrustedOverlay;
20 
21 import android.annotation.IntDef;
22 import android.annotation.Nullable;
23 import android.graphics.Matrix;
24 import android.graphics.Rect;
25 import android.graphics.Region;
26 import android.gui.TouchOcclusionMode;
27 import android.os.IBinder;
28 import android.os.InputConfig;
29 import android.util.Size;
30 
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 import java.lang.ref.WeakReference;
34 
35 /**
36  * Functions as a handle for a window that can receive input, and allows for the behavior of the
37  * input window to be configured.
38  * @hide
39  */
40 public final class InputWindowHandle {
41     /**
42      * An internal annotation for all the {@link android.os.InputConfig} flags that can be
43      * specified to {@link #inputConfig} to control the behavior of an input window. Only the
44      * flags listed here are valid for use in Java.
45      *
46      * The default flag value is 0, which is what we expect for a normal application window. Adding
47      * a flag indicates that the window's behavior deviates from that of a normal application
48      * window.
49      *
50      * The flags are defined as an AIDL enum to keep it in sync with native code.
51      * {@link android.os.InputConfig} flags that are not listed here should not be used in Java, and
52      * are only meant to be used in native code.
53      */
54     @Retention(RetentionPolicy.SOURCE)
55     @IntDef(flag = true, value = {
56             InputConfig.DEFAULT,
57             InputConfig.NO_INPUT_CHANNEL,
58             InputConfig.NOT_FOCUSABLE,
59             InputConfig.NOT_TOUCHABLE,
60             InputConfig.PREVENT_SPLITTING,
61             InputConfig.DUPLICATE_TOUCH_TO_WALLPAPER,
62             InputConfig.IS_WALLPAPER,
63             InputConfig.PAUSE_DISPATCHING,
64             InputConfig.WATCH_OUTSIDE_TOUCH,
65             InputConfig.SLIPPERY,
66             InputConfig.DISABLE_USER_ACTIVITY,
67             InputConfig.SPY,
68             InputConfig.INTERCEPTS_STYLUS,
69             InputConfig.CLONE,
70             InputConfig.SENSITIVE_FOR_PRIVACY,
71     })
72     public @interface InputConfigFlags {}
73 
74     // Pointer to the native input window handle.
75     // This field is lazily initialized via JNI.
76     @SuppressWarnings("unused")
77     private long ptr;
78 
79     // The input application handle.
80     public InputApplicationHandle inputApplicationHandle;
81 
82     // The token associates input data with a window and its input channel. The client input
83     // channel and the server input channel will both contain this token.
84     public IBinder token;
85 
86     /**
87      * The {@link IBinder} handle if InputWindowHandle is associated with a client token,
88      * normally the IWindow token, null otherwise.
89      */
90     @Nullable
91     private IBinder windowToken;
92 
93     // The window name.
94     public String name;
95 
96     // Window layout params attributes. (WindowManager.LayoutParams)
97     // These values do not affect any input configurations. Use {@link #inputConfig} instead.
98     public int layoutParamsFlags;
99     public int layoutParamsType;
100 
101     // Dispatching timeout.
102     public long dispatchingTimeoutMillis;
103 
104     // Window frame.
105     public final Rect frame = new Rect();
106 
107     // The real size of the content, excluding any crop. If no buffer is rendered, this is 0,0
108     public Size contentSize = new Size(0, 0);
109 
110     public int surfaceInset;
111 
112     // Global scaling factor applied to touch events when they are dispatched
113     // to the window
114     public float scaleFactor;
115 
116     // Window touchable region.
117     public final Region touchableRegion = new Region();
118 
119     // Flags that specify the behavior of this input window. See {@link #InputConfigFlags}.
120     @InputConfigFlags
121     public int inputConfig;
122 
123     // What effect this window has on touch occlusion if it lets touches pass through
124     // By default windows will block touches if they are untrusted and from a different UID due to
125     // security concerns
126     public int touchOcclusionMode = TouchOcclusionMode.BLOCK_UNTRUSTED;
127 
128     // Id of process and user that owns the window.
129     public int ownerPid;
130     public int ownerUid;
131 
132     // Owner package of the window
133     public String packageName;
134 
135     // Display this input window is on.
136     public int displayId;
137 
138     /**
139      * Crops the {@link #touchableRegion} to the bounds of the surface provided.
140      *
141      * This can be used in cases where the window should be constrained to the bounds of a parent
142      * window. That is, the window should receive touch events outside its window frame, but be
143      * limited to its stack bounds, such as in the case of split screen.
144      */
145     public WeakReference<SurfaceControl> touchableRegionSurfaceControl = new WeakReference<>(null);
146 
147     /**
148      * Replace {@link #touchableRegion} with the bounds of {@link #touchableRegionSurfaceControl}.
149      * If the handle is {@code null}, the bounds of the surface associated with this window is used
150      * as the touchable region.
151      */
152     public boolean replaceTouchableRegionWithCrop;
153 
154     /**
155      * The transform that should be applied to the Window to get it from screen coordinates to
156      * window coordinates
157      */
158     public Matrix transform;
159 
160     /**
161      * The alpha value returned from SurfaceFlinger. This will be ignored if passed as input data.
162      */
163     public float alpha;
164 
165     /**
166      * Sets a property on this window indicating that its visible region should be considered when
167      * computing TrustedPresentation Thresholds.
168      */
169     public boolean canOccludePresentation;
170 
171     /**
172      * The input token for the window to which focus should be transferred when this input window
173      * can be successfully focused. If null, this input window will not transfer its focus to
174      * any other window.
175      */
176     @Nullable
177     public IBinder focusTransferTarget;
178 
nativeDispose()179     private native void nativeDispose();
180 
InputWindowHandle(InputApplicationHandle inputApplicationHandle, int displayId)181     public InputWindowHandle(InputApplicationHandle inputApplicationHandle, int displayId) {
182         this.inputApplicationHandle = inputApplicationHandle;
183         this.displayId = displayId;
184     }
185 
InputWindowHandle(InputWindowHandle other)186     public InputWindowHandle(InputWindowHandle other) {
187         // Do not copy ptr to prevent this copy from sharing the same native object.
188         ptr = 0;
189         inputApplicationHandle = new InputApplicationHandle(other.inputApplicationHandle);
190         token = other.token;
191         windowToken = other.windowToken;
192         name = other.name;
193         layoutParamsFlags = other.layoutParamsFlags;
194         layoutParamsType = other.layoutParamsType;
195         dispatchingTimeoutMillis = other.dispatchingTimeoutMillis;
196         frame.set(other.frame);
197         surfaceInset = other.surfaceInset;
198         scaleFactor = other.scaleFactor;
199         touchableRegion.set(other.touchableRegion);
200         inputConfig = other.inputConfig;
201         touchOcclusionMode = other.touchOcclusionMode;
202         ownerPid = other.ownerPid;
203         ownerUid = other.ownerUid;
204         packageName = other.packageName;
205         displayId = other.displayId;
206         touchableRegionSurfaceControl = other.touchableRegionSurfaceControl;
207         replaceTouchableRegionWithCrop = other.replaceTouchableRegionWithCrop;
208         if (other.transform != null) {
209             transform = new Matrix();
210             transform.set(other.transform);
211         }
212         focusTransferTarget = other.focusTransferTarget;
213         contentSize = new Size(other.contentSize.getWidth(), other.contentSize.getHeight());
214         alpha = other.alpha;
215         canOccludePresentation = other.canOccludePresentation;
216     }
217 
218     @Override
toString()219     public String toString() {
220         return new StringBuilder(name != null ? name : "")
221                 .append(", frame=[").append(frame).append("]")
222                 .append(", touchableRegion=").append(touchableRegion)
223                 .append(", scaleFactor=").append(scaleFactor)
224                 .append(", transform=").append(transform)
225                 .append(", windowToken=").append(windowToken)
226                 .append(", displayId=").append(displayId)
227                 .append(", isClone=").append((inputConfig & InputConfig.CLONE) != 0)
228                 .append(", contentSize=").append(contentSize)
229                 .append(", alpha=").append(alpha)
230                 .append(", canOccludePresentation=").append(canOccludePresentation)
231                 .toString();
232 
233     }
234 
235     @Override
finalize()236     protected void finalize() throws Throwable {
237         try {
238             nativeDispose();
239         } finally {
240             super.finalize();
241         }
242     }
243 
244     /**
245      * Set the window's touchable region to the bounds of {@link #touchableRegionSurfaceControl}
246      * and ignore the value of {@link #touchableRegion}.
247      *
248      * @param bounds surface to set the touchable region to. Set to {@code null} to set the
249      *               touchable region as the current surface bounds.
250      */
replaceTouchableRegionWithCrop(@ullable SurfaceControl bounds)251     public void replaceTouchableRegionWithCrop(@Nullable SurfaceControl bounds) {
252         setTouchableRegionCrop(bounds);
253         replaceTouchableRegionWithCrop = true;
254     }
255 
256     /**
257      * Crop the window touchable region to the bounds of the surface provided.
258      */
setTouchableRegionCrop(@ullable SurfaceControl bounds)259     public void setTouchableRegionCrop(@Nullable SurfaceControl bounds) {
260         touchableRegionSurfaceControl = new WeakReference<>(bounds);
261     }
262 
setWindowToken(IBinder iwindow)263     public void setWindowToken(IBinder iwindow) {
264         windowToken = iwindow;
265     }
266 
getWindowToken()267     public @Nullable IBinder getWindowToken() {
268         return windowToken;
269     }
270 
271     /**
272      * Set the provided inputConfig flag values.
273      * @param inputConfig the flag values to change
274      * @param value the provided flag values are set when true, and cleared when false
275      */
setInputConfig(@nputConfigFlags int inputConfig, boolean value)276     public void setInputConfig(@InputConfigFlags int inputConfig, boolean value) {
277         if (value) {
278             this.inputConfig |= inputConfig;
279             return;
280         }
281         this.inputConfig &= ~inputConfig;
282     }
283 
setTrustedOverlay(SurfaceControl.Transaction t, SurfaceControl sc, boolean isTrusted)284     public void setTrustedOverlay(SurfaceControl.Transaction t, SurfaceControl sc,
285             boolean isTrusted) {
286         if (surfaceTrustedOverlay()) {
287             t.setTrustedOverlay(sc, isTrusted);
288         } else if (isTrusted) {
289             inputConfig |= InputConfig.TRUSTED_OVERLAY;
290         }
291     }
292 }
293