1 /*
2  * Copyright (C) 2021 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.window;
18 
19 import android.Manifest;
20 import android.annotation.RequiresPermission;
21 import android.graphics.Matrix;
22 import android.util.Pair;
23 import android.util.Size;
24 import android.view.InputWindowHandle;
25 
26 import libcore.util.NativeAllocationRegistry;
27 
28 /**
29  * Listener for getting {@link InputWindowHandle} updates from SurfaceFlinger.
30  * @hide
31  */
32 public abstract class WindowInfosListener {
33     private final long mNativeListener;
34 
WindowInfosListener()35     public WindowInfosListener() {
36         NativeAllocationRegistry registry = NativeAllocationRegistry.createMalloced(
37                 WindowInfosListener.class.getClassLoader(), nativeGetFinalizer());
38 
39         mNativeListener = nativeCreate(this);
40         registry.registerNativeAllocation(this, mNativeListener);
41     }
42 
43     /**
44      * Called when WindowInfos in SurfaceFlinger have changed.
45      * @param windowHandles Reverse Z ordered array of window information that was on screen,
46      *                      where the first value is the topmost window.
47      */
onWindowInfosChanged(InputWindowHandle[] windowHandles, DisplayInfo[] displayInfos)48     public abstract void onWindowInfosChanged(InputWindowHandle[] windowHandles,
49             DisplayInfo[] displayInfos);
50 
51     /**
52      * Register the WindowInfosListener.
53      *
54      * Registering WindowInfosListeners should only be done within system_server and shell.
55      *
56      * @return The cached values for InputWindowHandles and DisplayInfos. This is the last updated
57      * value that was sent from SurfaceFlinger to this particular process. If there was nothing
58      * registered previously, then the data can be empty.
59      */
60     @RequiresPermission(Manifest.permission.ACCESS_SURFACE_FLINGER)
register()61     public Pair<InputWindowHandle[], DisplayInfo[]> register() {
62         return nativeRegister(mNativeListener);
63     }
64 
65     /**
66      * Unregisters the WindowInfosListener.
67      */
unregister()68     public void unregister() {
69         nativeUnregister(mNativeListener);
70     }
71 
nativeCreate(WindowInfosListener thiz)72     private static native long nativeCreate(WindowInfosListener thiz);
73 
74     @RequiresPermission(Manifest.permission.ACCESS_SURFACE_FLINGER)
nativeRegister(long ptr)75     private static native Pair<InputWindowHandle[], DisplayInfo[]> nativeRegister(long ptr);
76 
nativeUnregister(long ptr)77     private static native void nativeUnregister(long ptr);
78 
nativeGetFinalizer()79     private static native long nativeGetFinalizer();
80 
81     /**
82      * Describes information about a display that can have windows in it.
83      */
84     public static final class DisplayInfo {
85         public final int mDisplayId;
86 
87         /**
88          * Logical display dimensions.
89          */
90         public final Size mLogicalSize;
91 
92         /**
93          * The display transform. This takes display coordinates to logical display coordinates.
94          */
95         public final Matrix mTransform;
96 
DisplayInfo(int displayId, int logicalWidth, int logicalHeight, Matrix transform)97         private DisplayInfo(int displayId, int logicalWidth, int logicalHeight, Matrix transform) {
98             mDisplayId = displayId;
99             mLogicalSize = new Size(logicalWidth, logicalHeight);
100             mTransform = transform;
101         }
102 
103         @Override
toString()104         public String toString() {
105             return "displayId=" + mDisplayId
106                     + ", mLogicalSize=" + mLogicalSize
107                     + ", mTransform=" + mTransform;
108         }
109     }
110 }
111