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 android.view.Display.INVALID_DISPLAY;
20 
21 import android.annotation.Nullable;
22 import android.graphics.Matrix;
23 import android.graphics.Region;
24 import android.gui.TouchOcclusionMode;
25 import android.os.IBinder;
26 
27 import java.lang.ref.WeakReference;
28 
29 /**
30  * Functions as a handle for a window that can receive input.
31  * Enables the native input dispatcher to refer indirectly to the window manager's window state.
32  * @hide
33  */
34 public final class InputWindowHandle {
35     // Pointer to the native input window handle.
36     // This field is lazily initialized via JNI.
37     @SuppressWarnings("unused")
38     private long ptr;
39 
40     // The input application handle.
41     public InputApplicationHandle inputApplicationHandle;
42 
43     // The token associates input data with a window and its input channel. The client input
44     // channel and the server input channel will both contain this token.
45     public IBinder token;
46 
47     /**
48      * The {@link IWindow} handle if InputWindowHandle is associated with a window, null otherwise.
49      */
50     @Nullable
51     private IBinder windowToken;
52     /**
53      * Used to cache IWindow from the windowToken so we don't need to convert every time getWindow
54      * is called.
55      */
56     private IWindow window;
57 
58     // The window name.
59     public String name;
60 
61     // Window layout params attributes.  (WindowManager.LayoutParams)
62     public int layoutParamsFlags;
63     public int layoutParamsType;
64 
65     // Dispatching timeout.
66     public long dispatchingTimeoutMillis;
67 
68     // Window frame.
69     public int frameLeft;
70     public int frameTop;
71     public int frameRight;
72     public int frameBottom;
73 
74     public int surfaceInset;
75 
76     // Global scaling factor applied to touch events when they are dispatched
77     // to the window
78     public float scaleFactor;
79 
80     // Window touchable region.
81     public final Region touchableRegion = new Region();
82 
83     // Window is visible.
84     public boolean visible;
85 
86     // Window can be focused.
87     public boolean focusable;
88 
89     // Window has wallpaper.  (window is the current wallpaper target)
90     public boolean hasWallpaper;
91 
92     // Input event dispatching is paused.
93     public boolean paused;
94 
95     // Window is trusted overlay.
96     public boolean trustedOverlay;
97 
98     // What effect this window has on touch occlusion if it lets touches pass through
99     // By default windows will block touches if they are untrusted and from a different UID due to
100     // security concerns
101     public int touchOcclusionMode = TouchOcclusionMode.BLOCK_UNTRUSTED;
102 
103     // Id of process and user that owns the window.
104     public int ownerPid;
105     public int ownerUid;
106 
107     // Owner package of the window
108     public String packageName;
109 
110     // Window input features.
111     public int inputFeatures;
112 
113     // Display this input is on.
114     public int displayId;
115 
116     // If this value is set to a valid display ID, it indicates this window is a portal which
117     // transports the touch of this window to the display indicated by portalToDisplayId.
118     public int portalToDisplayId = INVALID_DISPLAY;
119 
120     /**
121      * Crops the touchable region to the bounds of the surface provided.
122      *
123      * This can be used in cases where the window is not
124      * {@link android.view.WindowManager#FLAG_NOT_TOUCH_MODAL} but should be constrained to the
125      * bounds of a parent window. That is the window should receive touch events outside its
126      * window but be limited to its stack bounds, such as in the case of split screen.
127      */
128     public WeakReference<SurfaceControl> touchableRegionSurfaceControl = new WeakReference<>(null);
129 
130     /**
131      * Replace {@link touchableRegion} with the bounds of {@link touchableRegionSurfaceControl}. If
132      * the handle is {@code null}, the bounds of the surface associated with this window is used
133      * as the touchable region.
134      */
135     public boolean replaceTouchableRegionWithCrop;
136 
137     /**
138      * The transform that should be applied to the Window to get it from screen coordinates to
139      * window coordinates
140      */
141     public Matrix transform;
142 
nativeDispose()143     private native void nativeDispose();
144 
InputWindowHandle(InputApplicationHandle inputApplicationHandle, int displayId)145     public InputWindowHandle(InputApplicationHandle inputApplicationHandle, int displayId) {
146         this.inputApplicationHandle = inputApplicationHandle;
147         this.displayId = displayId;
148     }
149 
150     @Override
toString()151     public String toString() {
152         return new StringBuilder(name != null ? name : "")
153                 .append(", frame=[").append(frameLeft).append(",").append(frameTop).append(",")
154                         .append(frameRight).append(",").append(frameBottom).append("]")
155                 .append(", touchableRegion=").append(touchableRegion)
156                 .append(", visible=").append(visible)
157                 .append(", scaleFactor=").append(scaleFactor)
158                 .append(", transform=").append(transform)
159                 .append(", windowToken=").append(windowToken)
160                 .toString();
161 
162     }
163 
164     @Override
finalize()165     protected void finalize() throws Throwable {
166         try {
167             nativeDispose();
168         } finally {
169             super.finalize();
170         }
171     }
172 
173     /**
174      * Set the window touchable region to the bounds of {@link touchableRegionBounds} ignoring any
175      * touchable region provided.
176      *
177      * @param bounds surface to set the touchable region to. Set to {@code null} to set the bounds
178      * to the current surface.
179      */
replaceTouchableRegionWithCrop(@ullable SurfaceControl bounds)180     public void replaceTouchableRegionWithCrop(@Nullable SurfaceControl bounds) {
181         setTouchableRegionCrop(bounds);
182         replaceTouchableRegionWithCrop = true;
183     }
184 
185     /**
186      * Crop the window touchable region to the bounds of the surface provided.
187      */
setTouchableRegionCrop(@ullable SurfaceControl bounds)188     public void setTouchableRegionCrop(@Nullable SurfaceControl bounds) {
189         touchableRegionSurfaceControl = new WeakReference<>(bounds);
190     }
191 
setWindowToken(IWindow iwindow)192     public void setWindowToken(IWindow iwindow) {
193         windowToken = iwindow.asBinder();
194         window = iwindow;
195     }
196 
getWindow()197     public IWindow getWindow() {
198         if (window != null) {
199             return window;
200         }
201         window = IWindow.Stub.asInterface(windowToken);
202         return window;
203     }
204 }
205