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 com.android.systemui.accessibility;
18 
19 import android.graphics.Rect;
20 import android.graphics.Region;
21 import android.view.Display;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.WindowInsets;
25 import android.view.WindowManager;
26 import android.view.WindowMetrics;
27 
28 public class TestableWindowManager implements WindowManager {
29 
30     private final WindowManager mWindowManager;
31     private View mView;
32     private Rect mWindowBounds = null;
33     private WindowInsets mWindowInsets = null;
34 
TestableWindowManager(WindowManager windowManager)35     TestableWindowManager(WindowManager windowManager) {
36         mWindowManager = windowManager;
37     }
38 
39     @Override
getDefaultDisplay()40     public Display getDefaultDisplay() {
41         return mWindowManager.getDefaultDisplay();
42     }
43 
44     @Override
removeViewImmediate(View view)45     public void removeViewImmediate(View view) {
46         mWindowManager.removeViewImmediate(view);
47     }
48 
49     @Override
requestAppKeyboardShortcuts(WindowManager.KeyboardShortcutsReceiver receiver, int deviceId)50     public void requestAppKeyboardShortcuts(WindowManager.KeyboardShortcutsReceiver receiver,
51             int deviceId) {
52         mWindowManager.requestAppKeyboardShortcuts(receiver, deviceId);
53     }
54 
55     @Override
getCurrentImeTouchRegion()56     public Region getCurrentImeTouchRegion() {
57         return mWindowManager.getCurrentImeTouchRegion();
58     }
59 
60     @Override
addView(View view, ViewGroup.LayoutParams params)61     public void addView(View view, ViewGroup.LayoutParams params) {
62         mView = view;
63         mWindowManager.addView(view, params);
64     }
65 
66     @Override
updateViewLayout(View view, ViewGroup.LayoutParams params)67     public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
68         mWindowManager.updateViewLayout(view, params);
69     }
70 
71     @Override
removeView(View view)72     public void removeView(View view) {
73         mView = null;
74         mWindowManager.removeView(view);
75     }
76 
77     @Override
getCurrentWindowMetrics()78     public WindowMetrics getCurrentWindowMetrics() {
79         final WindowMetrics realMetrics = mWindowManager.getCurrentWindowMetrics();
80         final WindowMetrics windowMetrics = new WindowMetrics(
81                 mWindowBounds == null ? realMetrics.getBounds()
82                         : mWindowBounds,
83                 mWindowInsets == null ?  realMetrics.getWindowInsets() : mWindowInsets);
84         return windowMetrics;
85     }
86 
87     @Override
getMaximumWindowMetrics()88     public WindowMetrics getMaximumWindowMetrics() {
89         return mWindowManager.getMaximumWindowMetrics();
90     }
91 
getAttachedView()92     public View getAttachedView() {
93         return mView;
94     }
95 
getLayoutParamsFromAttachedView()96     public WindowManager.LayoutParams getLayoutParamsFromAttachedView() {
97         if (mView == null) {
98             return null;
99         }
100         return (WindowManager.LayoutParams) mView.getLayoutParams();
101     }
102 
103     /**
104      * Sets the given window bounds to current window metrics.
105      *
106      * @param bounds the window bounds
107      */
setWindowBounds(Rect bounds)108     public void setWindowBounds(Rect bounds) {
109         mWindowBounds = bounds;
110     }
111 
112     /**
113      * Sets the given window insets to the current window metrics.
114      *
115      * @param insets the window insets.
116      */
setWindowInsets(WindowInsets insets)117     public void setWindowInsets(WindowInsets insets) {
118         mWindowInsets = insets;
119     }
120 }
121