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.Region;
23 import android.os.IBinder;
24 
25 import java.lang.ref.WeakReference;
26 
27 /**
28  * Functions as a handle for a window that can receive input.
29  * Enables the native input dispatcher to refer indirectly to the window manager's window state.
30  * @hide
31  */
32 public final class InputWindowHandle {
33     // Pointer to the native input window handle.
34     // This field is lazily initialized via JNI.
35     @SuppressWarnings("unused")
36     private long ptr;
37 
38     // The input application handle.
39     public final InputApplicationHandle inputApplicationHandle;
40 
41     // The client window.
42     public final IWindow clientWindow;
43 
44     // The token associated with the window.
45     public IBinder token;
46 
47     // The window name.
48     public String name;
49 
50     // Window layout params attributes.  (WindowManager.LayoutParams)
51     public int layoutParamsFlags;
52     public int layoutParamsType;
53 
54     // Dispatching timeout.
55     public long dispatchingTimeoutNanos;
56 
57     // Window frame.
58     public int frameLeft;
59     public int frameTop;
60     public int frameRight;
61     public int frameBottom;
62 
63     public int surfaceInset;
64 
65     // Global scaling factor applied to touch events when they are dispatched
66     // to the window
67     public float scaleFactor;
68 
69     // Window touchable region.
70     public final Region touchableRegion = new Region();
71 
72     // Window is visible.
73     public boolean visible;
74 
75     // Window can receive keys.
76     public boolean canReceiveKeys;
77 
78     // Window has focus.
79     public boolean hasFocus;
80 
81     // Window has wallpaper.  (window is the current wallpaper target)
82     public boolean hasWallpaper;
83 
84     // Input event dispatching is paused.
85     public boolean paused;
86 
87     // Window layer.
88     public int layer;
89 
90     // Id of process and user that owns the window.
91     public int ownerPid;
92     public int ownerUid;
93 
94     // Window input features.
95     public int inputFeatures;
96 
97     // Display this input is on.
98     public int displayId;
99 
100     // If this value is set to a valid display ID, it indicates this window is a portal which
101     // transports the touch of this window to the display indicated by portalToDisplayId.
102     public int portalToDisplayId = INVALID_DISPLAY;
103 
104     /**
105      * Crops the touchable region to the bounds of the surface provided.
106      *
107      * This can be used in cases where the window is not
108      * {@link android.view.WindowManager#FLAG_NOT_TOUCH_MODAL} but should be constrained to the
109      * bounds of a parent window. That is the window should receive touch events outside its
110      * window but be limited to its stack bounds, such as in the case of split screen.
111      */
112     public WeakReference<IBinder> touchableRegionCropHandle = new WeakReference<>(null);
113 
114     /**
115      * Replace {@link touchableRegion} with the bounds of {@link touchableRegionCropHandle}. If
116      * the handle is {@code null}, the bounds of the surface associated with this window is used
117      * as the touchable region.
118      */
119     public boolean replaceTouchableRegionWithCrop;
120 
nativeDispose()121     private native void nativeDispose();
122 
InputWindowHandle(InputApplicationHandle inputApplicationHandle, IWindow clientWindow, int displayId)123     public InputWindowHandle(InputApplicationHandle inputApplicationHandle,
124             IWindow clientWindow, int displayId) {
125         this.inputApplicationHandle = inputApplicationHandle;
126         this.clientWindow = clientWindow;
127         this.displayId = displayId;
128     }
129 
130     @Override
toString()131     public String toString() {
132         return new StringBuilder(name != null ? name : "")
133                 .append(", layer=").append(layer)
134                 .append(", frame=[").append(frameLeft).append(",").append(frameTop).append(",")
135                         .append(frameRight).append(",").append(frameBottom).append("]")
136                 .append(", touchableRegion=").append(touchableRegion)
137                 .append(", visible=").append(visible)
138                 .toString();
139 
140     }
141 
142     @Override
finalize()143     protected void finalize() throws Throwable {
144         try {
145             nativeDispose();
146         } finally {
147             super.finalize();
148         }
149     }
150 
151     /**
152      * Set the window touchable region to the bounds of {@link touchableRegionBounds} ignoring any
153      * touchable region provided.
154      *
155      * @param bounds surface to set the touchable region to. Set to {@code null} to set the bounds
156      * to the current surface.
157      */
replaceTouchableRegionWithCrop(@ullable SurfaceControl bounds)158     public void replaceTouchableRegionWithCrop(@Nullable SurfaceControl bounds) {
159         setTouchableRegionCrop(bounds);
160         replaceTouchableRegionWithCrop = true;
161     }
162 
163     /**
164      * Crop the window touchable region to the bounds of the surface provided.
165      */
setTouchableRegionCrop(@ullable SurfaceControl bounds)166     public void setTouchableRegionCrop(@Nullable SurfaceControl bounds) {
167         if (bounds != null) {
168             touchableRegionCropHandle = new WeakReference<>(bounds.getHandle());
169         }
170     }
171 }
172