blob: f7a9e41ba70683e5d4569b16d4e23e333bc670ac [file] [log] [blame]
Svetoslav8e3feb12014-02-24 13:46:47 -08001/*
2 * Copyright (C) 2014 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
17package com.android.server.wm;
18
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080019import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
21
Svetoslav8e3feb12014-02-24 13:46:47 -080022import android.animation.ObjectAnimator;
23import android.animation.ValueAnimator;
Alan Viverette59e53a12016-03-28 13:41:32 -040024import android.annotation.NonNull;
Svetoslav8e3feb12014-02-24 13:46:47 -080025import android.app.Service;
26import android.content.Context;
27import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.Path;
32import android.graphics.PixelFormat;
33import android.graphics.Point;
34import android.graphics.PorterDuff.Mode;
35import android.graphics.Rect;
36import android.graphics.RectF;
37import android.graphics.Region;
38import android.os.Handler;
39import android.os.IBinder;
40import android.os.Looper;
41import android.os.Message;
Phil Weaver396d5492016-03-22 17:53:50 -070042import android.text.TextUtils;
Svetoslav8e3feb12014-02-24 13:46:47 -080043import android.util.ArraySet;
44import android.util.Log;
45import android.util.Slog;
46import android.util.SparseArray;
47import android.util.TypedValue;
48import android.view.MagnificationSpec;
49import android.view.Surface;
50import android.view.Surface.OutOfResourcesException;
51import android.view.SurfaceControl;
Svetoslavf7174e82014-06-12 11:29:35 -070052import android.view.ViewConfiguration;
Svetoslav8e3feb12014-02-24 13:46:47 -080053import android.view.WindowInfo;
54import android.view.WindowManager;
55import android.view.WindowManagerInternal.MagnificationCallbacks;
56import android.view.WindowManagerInternal.WindowsForAccessibilityCallback;
57import android.view.WindowManagerPolicy;
58import android.view.animation.DecelerateInterpolator;
59import android.view.animation.Interpolator;
60
61import com.android.internal.R;
62import com.android.internal.os.SomeArgs;
63
64import java.util.ArrayList;
Allen Hairf20ac2c2016-02-11 17:42:59 -080065import java.util.HashSet;
Svetoslav8e3feb12014-02-24 13:46:47 -080066import java.util.List;
67import java.util.Set;
68
69/**
70 * This class contains the accessibility related logic of the window manger.
71 */
72final class AccessibilityController {
73
74 private final WindowManagerService mWindowManagerService;
75
76 private static final float[] sTempFloats = new float[9];
77
78 public AccessibilityController(WindowManagerService service) {
79 mWindowManagerService = service;
80 }
81
82 private DisplayMagnifier mDisplayMagnifier;
83
84 private WindowsForAccessibilityObserver mWindowsForAccessibilityObserver;
85
86 public void setMagnificationCallbacksLocked(MagnificationCallbacks callbacks) {
87 if (callbacks != null) {
88 if (mDisplayMagnifier != null) {
89 throw new IllegalStateException("Magnification callbacks already set!");
90 }
91 mDisplayMagnifier = new DisplayMagnifier(mWindowManagerService, callbacks);
92 } else {
93 if (mDisplayMagnifier == null) {
94 throw new IllegalStateException("Magnification callbacks already cleared!");
95 }
96 mDisplayMagnifier.destroyLocked();
97 mDisplayMagnifier = null;
98 }
99 }
100
101 public void setWindowsForAccessibilityCallback(WindowsForAccessibilityCallback callback) {
102 if (callback != null) {
103 if (mWindowsForAccessibilityObserver != null) {
104 throw new IllegalStateException(
105 "Windows for accessibility callback already set!");
106 }
107 mWindowsForAccessibilityObserver = new WindowsForAccessibilityObserver(
108 mWindowManagerService, callback);
109 } else {
110 if (mWindowsForAccessibilityObserver == null) {
111 throw new IllegalStateException(
112 "Windows for accessibility callback already cleared!");
113 }
114 mWindowsForAccessibilityObserver = null;
115 }
116 }
117
Svetoslav Ganovfd138892016-07-13 18:20:42 -0700118 public void performComputeChangedWindowsNotLocked() {
119 WindowsForAccessibilityObserver observer = null;
120 synchronized (mWindowManagerService) {
121 observer = mWindowsForAccessibilityObserver;
122 }
123 if (observer != null) {
124 observer.performComputeChangedWindowsNotLocked();
125 }
126 }
127
Svetoslav8e3feb12014-02-24 13:46:47 -0800128 public void setMagnificationSpecLocked(MagnificationSpec spec) {
129 if (mDisplayMagnifier != null) {
130 mDisplayMagnifier.setMagnificationSpecLocked(spec);
131 }
132 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700133 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800134 }
135 }
136
Phil Weaver70439242016-03-10 15:15:49 -0800137 public void getMagnificationRegionLocked(Region outMagnificationRegion) {
Alan Viverette59e53a12016-03-28 13:41:32 -0400138 if (mDisplayMagnifier != null) {
Phil Weaver70439242016-03-10 15:15:49 -0800139 mDisplayMagnifier.getMagnificationRegionLocked(outMagnificationRegion);
Alan Viverette59e53a12016-03-28 13:41:32 -0400140 }
141 }
142
Svetoslavf7174e82014-06-12 11:29:35 -0700143 public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800144 if (mDisplayMagnifier != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700145 mDisplayMagnifier.onRectangleOnScreenRequestedLocked(rectangle);
Svetoslav8e3feb12014-02-24 13:46:47 -0800146 }
147 // Not relevant for the window observer.
148 }
149
150 public void onWindowLayersChangedLocked() {
151 if (mDisplayMagnifier != null) {
152 mDisplayMagnifier.onWindowLayersChangedLocked();
153 }
154 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700155 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800156 }
157 }
158
159 public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
160 if (mDisplayMagnifier != null) {
161 mDisplayMagnifier.onRotationChangedLocked(displayContent, rotation);
162 }
163 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700164 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800165 }
166 }
167
168 public void onAppWindowTransitionLocked(WindowState windowState, int transition) {
169 if (mDisplayMagnifier != null) {
170 mDisplayMagnifier.onAppWindowTransitionLocked(windowState, transition);
171 }
172 // Not relevant for the window observer.
173 }
174
175 public void onWindowTransitionLocked(WindowState windowState, int transition) {
176 if (mDisplayMagnifier != null) {
177 mDisplayMagnifier.onWindowTransitionLocked(windowState, transition);
178 }
179 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700180 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800181 }
182 }
183
Svetoslav3a0d8782014-12-04 12:50:11 -0800184 public void onWindowFocusChangedNotLocked() {
Svetoslav8e3feb12014-02-24 13:46:47 -0800185 // Not relevant for the display magnifier.
186
Svetoslav3a0d8782014-12-04 12:50:11 -0800187 WindowsForAccessibilityObserver observer = null;
188 synchronized (mWindowManagerService) {
189 observer = mWindowsForAccessibilityObserver;
190 }
191 if (observer != null) {
192 observer.performComputeChangedWindowsNotLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800193 }
194 }
195
Svetoslav4604abc2014-06-10 18:59:30 -0700196
Svetoslavf7174e82014-06-12 11:29:35 -0700197 public void onSomeWindowResizedOrMovedLocked() {
Svetoslav4604abc2014-06-10 18:59:30 -0700198 // Not relevant for the display magnifier.
199
200 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700201 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav4604abc2014-06-10 18:59:30 -0700202 }
203 }
204
Svetoslav8e3feb12014-02-24 13:46:47 -0800205 /** NOTE: This has to be called within a surface transaction. */
206 public void drawMagnifiedRegionBorderIfNeededLocked() {
207 if (mDisplayMagnifier != null) {
208 mDisplayMagnifier.drawMagnifiedRegionBorderIfNeededLocked();
209 }
210 // Not relevant for the window observer.
211 }
212
213 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
214 if (mDisplayMagnifier != null) {
215 return mDisplayMagnifier.getMagnificationSpecForWindowLocked(windowState);
216 }
217 return null;
218 }
219
220 public boolean hasCallbacksLocked() {
221 return (mDisplayMagnifier != null
222 || mWindowsForAccessibilityObserver != null);
223 }
224
225 private static void populateTransformationMatrixLocked(WindowState windowState,
226 Matrix outMatrix) {
227 sTempFloats[Matrix.MSCALE_X] = windowState.mWinAnimator.mDsDx;
228 sTempFloats[Matrix.MSKEW_Y] = windowState.mWinAnimator.mDtDx;
229 sTempFloats[Matrix.MSKEW_X] = windowState.mWinAnimator.mDsDy;
230 sTempFloats[Matrix.MSCALE_Y] = windowState.mWinAnimator.mDtDy;
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -0700231 sTempFloats[Matrix.MTRANS_X] = windowState.mShownPosition.x;
232 sTempFloats[Matrix.MTRANS_Y] = windowState.mShownPosition.y;
Svetoslav8e3feb12014-02-24 13:46:47 -0800233 sTempFloats[Matrix.MPERSP_0] = 0;
234 sTempFloats[Matrix.MPERSP_1] = 0;
235 sTempFloats[Matrix.MPERSP_2] = 1;
236 outMatrix.setValues(sTempFloats);
237 }
238
239 /**
240 * This class encapsulates the functionality related to display magnification.
241 */
242 private static final class DisplayMagnifier {
243
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800244 private static final String LOG_TAG = TAG_WITH_CLASS_NAME ? "DisplayMagnifier" : TAG_WM;
Svetoslav8e3feb12014-02-24 13:46:47 -0800245
246 private static final boolean DEBUG_WINDOW_TRANSITIONS = false;
247 private static final boolean DEBUG_ROTATION = false;
248 private static final boolean DEBUG_LAYERS = false;
249 private static final boolean DEBUG_RECTANGLE_REQUESTED = false;
250 private static final boolean DEBUG_VIEWPORT_WINDOW = false;
251
252 private final Rect mTempRect1 = new Rect();
253 private final Rect mTempRect2 = new Rect();
254
255 private final Region mTempRegion1 = new Region();
256 private final Region mTempRegion2 = new Region();
257 private final Region mTempRegion3 = new Region();
258 private final Region mTempRegion4 = new Region();
259
260 private final Context mContext;
261 private final WindowManagerService mWindowManagerService;
262 private final MagnifiedViewport mMagnifedViewport;
263 private final Handler mHandler;
264
265 private final MagnificationCallbacks mCallbacks;
266
267 private final long mLongAnimationDuration;
268
269 public DisplayMagnifier(WindowManagerService windowManagerService,
270 MagnificationCallbacks callbacks) {
271 mContext = windowManagerService.mContext;
272 mWindowManagerService = windowManagerService;
273 mCallbacks = callbacks;
274 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
275 mMagnifedViewport = new MagnifiedViewport();
276 mLongAnimationDuration = mContext.getResources().getInteger(
277 com.android.internal.R.integer.config_longAnimTime);
278 }
279
280 public void setMagnificationSpecLocked(MagnificationSpec spec) {
281 mMagnifedViewport.updateMagnificationSpecLocked(spec);
282 mMagnifedViewport.recomputeBoundsLocked();
283 mWindowManagerService.scheduleAnimationLocked();
284 }
285
Svetoslavf7174e82014-06-12 11:29:35 -0700286 public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800287 if (DEBUG_RECTANGLE_REQUESTED) {
288 Slog.i(LOG_TAG, "Rectangle on screen requested: " + rectangle);
289 }
290 if (!mMagnifedViewport.isMagnifyingLocked()) {
291 return;
292 }
293 Rect magnifiedRegionBounds = mTempRect2;
294 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(magnifiedRegionBounds);
295 if (magnifiedRegionBounds.contains(rectangle)) {
296 return;
297 }
298 SomeArgs args = SomeArgs.obtain();
299 args.argi1 = rectangle.left;
300 args.argi2 = rectangle.top;
301 args.argi3 = rectangle.right;
302 args.argi4 = rectangle.bottom;
303 mHandler.obtainMessage(MyHandler.MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED,
304 args).sendToTarget();
305 }
306
307 public void onWindowLayersChangedLocked() {
308 if (DEBUG_LAYERS) {
309 Slog.i(LOG_TAG, "Layers changed.");
310 }
311 mMagnifedViewport.recomputeBoundsLocked();
312 mWindowManagerService.scheduleAnimationLocked();
313 }
314
315 public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
316 if (DEBUG_ROTATION) {
317 Slog.i(LOG_TAG, "Rotaton: " + Surface.rotationToString(rotation)
318 + " displayId: " + displayContent.getDisplayId());
319 }
320 mMagnifedViewport.onRotationChangedLocked();
321 mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_ROTATION_CHANGED);
322 }
323
324 public void onAppWindowTransitionLocked(WindowState windowState, int transition) {
325 if (DEBUG_WINDOW_TRANSITIONS) {
326 Slog.i(LOG_TAG, "Window transition: "
327 + AppTransition.appTransitionToString(transition)
328 + " displayId: " + windowState.getDisplayId());
329 }
330 final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
331 if (magnifying) {
332 switch (transition) {
333 case AppTransition.TRANSIT_ACTIVITY_OPEN:
334 case AppTransition.TRANSIT_TASK_OPEN:
335 case AppTransition.TRANSIT_TASK_TO_FRONT:
336 case AppTransition.TRANSIT_WALLPAPER_OPEN:
337 case AppTransition.TRANSIT_WALLPAPER_CLOSE:
338 case AppTransition.TRANSIT_WALLPAPER_INTRA_OPEN: {
339 mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_USER_CONTEXT_CHANGED);
340 }
341 }
342 }
343 }
344
345 public void onWindowTransitionLocked(WindowState windowState, int transition) {
346 if (DEBUG_WINDOW_TRANSITIONS) {
347 Slog.i(LOG_TAG, "Window transition: "
348 + AppTransition.appTransitionToString(transition)
349 + " displayId: " + windowState.getDisplayId());
350 }
351 final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
352 final int type = windowState.mAttrs.type;
353 switch (transition) {
354 case WindowManagerPolicy.TRANSIT_ENTER:
355 case WindowManagerPolicy.TRANSIT_SHOW: {
356 if (!magnifying) {
357 break;
358 }
359 switch (type) {
360 case WindowManager.LayoutParams.TYPE_APPLICATION:
Chong Zhangfea963e2016-08-15 17:14:16 -0700361 case WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION:
Svetoslav8e3feb12014-02-24 13:46:47 -0800362 case WindowManager.LayoutParams.TYPE_APPLICATION_PANEL:
363 case WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA:
364 case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
Wale Ogunwale0a4dc222015-04-14 12:58:42 -0700365 case WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL:
Svetoslav8e3feb12014-02-24 13:46:47 -0800366 case WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG:
367 case WindowManager.LayoutParams.TYPE_SEARCH_BAR:
368 case WindowManager.LayoutParams.TYPE_PHONE:
369 case WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
370 case WindowManager.LayoutParams.TYPE_TOAST:
371 case WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY:
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800372 case WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY:
Svetoslav8e3feb12014-02-24 13:46:47 -0800373 case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
374 case WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG:
375 case WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG:
376 case WindowManager.LayoutParams.TYPE_SYSTEM_ERROR:
377 case WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY:
Jason Monk8f7f3182015-11-18 16:35:14 -0500378 case WindowManager.LayoutParams.TYPE_QS_DIALOG:
Adrian Roos9a645132014-10-08 02:59:56 +0200379 case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL: {
Svetoslav8e3feb12014-02-24 13:46:47 -0800380 Rect magnifiedRegionBounds = mTempRect2;
381 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(
382 magnifiedRegionBounds);
383 Rect touchableRegionBounds = mTempRect1;
384 windowState.getTouchableRegion(mTempRegion1);
385 mTempRegion1.getBounds(touchableRegionBounds);
386 if (!magnifiedRegionBounds.intersect(touchableRegionBounds)) {
387 mCallbacks.onRectangleOnScreenRequested(
388 touchableRegionBounds.left,
389 touchableRegionBounds.top,
390 touchableRegionBounds.right,
391 touchableRegionBounds.bottom);
392 }
393 } break;
394 } break;
395 }
396 }
397 }
398
399 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
400 MagnificationSpec spec = mMagnifedViewport.getMagnificationSpecLocked();
401 if (spec != null && !spec.isNop()) {
402 WindowManagerPolicy policy = mWindowManagerService.mPolicy;
403 final int windowType = windowState.mAttrs.type;
Wale Ogunwale7ed4d372016-07-09 15:28:55 -0700404 if (!policy.isTopLevelWindow(windowType) && windowState.isChildWindow()
Svetoslav8e3feb12014-02-24 13:46:47 -0800405 && !policy.canMagnifyWindow(windowType)) {
406 return null;
407 }
408 if (!policy.canMagnifyWindow(windowState.mAttrs.type)) {
409 return null;
410 }
411 }
412 return spec;
413 }
414
Phil Weaver70439242016-03-10 15:15:49 -0800415 public void getMagnificationRegionLocked(Region outMagnificationRegion) {
416 mMagnifedViewport.getMagnificationRegionLocked(outMagnificationRegion);
Alan Viverette59e53a12016-03-28 13:41:32 -0400417 }
418
Svetoslav8e3feb12014-02-24 13:46:47 -0800419 public void destroyLocked() {
420 mMagnifedViewport.destroyWindow();
421 }
422
423 /** NOTE: This has to be called within a surface transaction. */
424 public void drawMagnifiedRegionBorderIfNeededLocked() {
425 mMagnifedViewport.drawWindowIfNeededLocked();
426 }
427
428 private final class MagnifiedViewport {
429
Svetoslav8e3feb12014-02-24 13:46:47 -0800430 private final SparseArray<WindowState> mTempWindowStates =
431 new SparseArray<WindowState>();
432
433 private final RectF mTempRectF = new RectF();
434
435 private final Point mTempPoint = new Point();
436
437 private final Matrix mTempMatrix = new Matrix();
438
Phil Weaver70439242016-03-10 15:15:49 -0800439 private final Region mMagnificationRegion = new Region();
440 private final Region mOldMagnificationRegion = new Region();
Svetoslav8e3feb12014-02-24 13:46:47 -0800441
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800442 private final Path mCircularPath;
443
Svetoslav8e3feb12014-02-24 13:46:47 -0800444 private final MagnificationSpec mMagnificationSpec = MagnificationSpec.obtain();
445
446 private final WindowManager mWindowManager;
447
Svetoslav7505e332014-08-22 12:14:28 -0700448 private final float mBorderWidth;
Svetoslav8e3feb12014-02-24 13:46:47 -0800449 private final int mHalfBorderWidth;
Svetoslav7505e332014-08-22 12:14:28 -0700450 private final int mDrawBorderInset;
Svetoslav8e3feb12014-02-24 13:46:47 -0800451
452 private final ViewportWindow mWindow;
453
454 private boolean mFullRedrawNeeded;
455
456 public MagnifiedViewport() {
457 mWindowManager = (WindowManager) mContext.getSystemService(Service.WINDOW_SERVICE);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800458 mBorderWidth = mContext.getResources().getDimension(
459 com.android.internal.R.dimen.accessibility_magnification_indicator_width);
Svetoslav7505e332014-08-22 12:14:28 -0700460 mHalfBorderWidth = (int) Math.ceil(mBorderWidth / 2);
461 mDrawBorderInset = (int) mBorderWidth / 2;
Svetoslav8e3feb12014-02-24 13:46:47 -0800462 mWindow = new ViewportWindow(mContext);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800463
Adam Powell01f280d2015-05-18 16:07:42 -0700464 if (mContext.getResources().getConfiguration().isScreenRound()) {
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800465 mCircularPath = new Path();
466 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
467 final int centerXY = mTempPoint.x / 2;
468 mCircularPath.addCircle(centerXY, centerXY, centerXY, Path.Direction.CW);
469 } else {
470 mCircularPath = null;
471 }
472
Svetoslav8e3feb12014-02-24 13:46:47 -0800473 recomputeBoundsLocked();
474 }
475
Phil Weaver70439242016-03-10 15:15:49 -0800476 public void getMagnificationRegionLocked(@NonNull Region outMagnificationRegion) {
477 outMagnificationRegion.set(mMagnificationRegion);
Alan Viverette59e53a12016-03-28 13:41:32 -0400478 }
479
Svetoslav8e3feb12014-02-24 13:46:47 -0800480 public void updateMagnificationSpecLocked(MagnificationSpec spec) {
481 if (spec != null) {
482 mMagnificationSpec.initialize(spec.scale, spec.offsetX, spec.offsetY);
483 } else {
484 mMagnificationSpec.clear();
485 }
486 // If this message is pending we are in a rotation animation and do not want
487 // to show the border. We will do so when the pending message is handled.
Svetoslav7505e332014-08-22 12:14:28 -0700488 if (!mHandler.hasMessages(
489 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED)) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800490 setMagnifiedRegionBorderShownLocked(isMagnifyingLocked(), true);
491 }
492 }
493
494 public void recomputeBoundsLocked() {
495 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
496 final int screenWidth = mTempPoint.x;
497 final int screenHeight = mTempPoint.y;
498
Phil Weaver70439242016-03-10 15:15:49 -0800499 mMagnificationRegion.set(0, 0, 0, 0);
500 final Region availableBounds = mTempRegion1;
501 availableBounds.set(0, 0, screenWidth, screenHeight);
Svetoslav8e3feb12014-02-24 13:46:47 -0800502
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800503 if (mCircularPath != null) {
Phil Weaver70439242016-03-10 15:15:49 -0800504 availableBounds.setPath(mCircularPath, availableBounds);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800505 }
506
Svetoslav8e3feb12014-02-24 13:46:47 -0800507 Region nonMagnifiedBounds = mTempRegion4;
Svet Ganovb21df802014-09-01 19:06:33 -0700508 nonMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800509
510 SparseArray<WindowState> visibleWindows = mTempWindowStates;
511 visibleWindows.clear();
512 populateWindowsOnScreenLocked(visibleWindows);
513
514 final int visibleWindowCount = visibleWindows.size();
515 for (int i = visibleWindowCount - 1; i >= 0; i--) {
516 WindowState windowState = visibleWindows.valueAt(i);
517 if (windowState.mAttrs.type == WindowManager
518 .LayoutParams.TYPE_MAGNIFICATION_OVERLAY) {
519 continue;
520 }
521
Phil Weaver65c06702016-03-15 15:33:46 -0700522 // Consider the touchable portion of the window
Svetoslav8e3feb12014-02-24 13:46:47 -0800523 Matrix matrix = mTempMatrix;
524 populateTransformationMatrixLocked(windowState, matrix);
Phil Weaver65c06702016-03-15 15:33:46 -0700525 Region touchableRegion = mTempRegion3;
526 windowState.getTouchableRegion(touchableRegion);
527 Rect touchableFrame = mTempRect1;
528 touchableRegion.getBounds(touchableFrame);
Svetoslav8e3feb12014-02-24 13:46:47 -0800529 RectF windowFrame = mTempRectF;
Phil Weaver65c06702016-03-15 15:33:46 -0700530 windowFrame.set(touchableFrame);
531 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
532 matrix.mapRect(windowFrame);
533 Region windowBounds = mTempRegion2;
534 windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
535 (int) windowFrame.right, (int) windowFrame.bottom);
536 // Only update new regions
537 Region portionOfWindowAlreadyAccountedFor = mTempRegion3;
Phil Weaver70439242016-03-10 15:15:49 -0800538 portionOfWindowAlreadyAccountedFor.set(mMagnificationRegion);
Phil Weaver65c06702016-03-15 15:33:46 -0700539 portionOfWindowAlreadyAccountedFor.op(nonMagnifiedBounds, Region.Op.UNION);
540 windowBounds.op(portionOfWindowAlreadyAccountedFor, Region.Op.DIFFERENCE);
Svetoslav8e3feb12014-02-24 13:46:47 -0800541
542 if (mWindowManagerService.mPolicy.canMagnifyWindow(windowState.mAttrs.type)) {
Phil Weaver70439242016-03-10 15:15:49 -0800543 mMagnificationRegion.op(windowBounds, Region.Op.UNION);
544 mMagnificationRegion.op(availableBounds, Region.Op.INTERSECT);
Svetoslav8e3feb12014-02-24 13:46:47 -0800545 } else {
Svetoslav8e3feb12014-02-24 13:46:47 -0800546 nonMagnifiedBounds.op(windowBounds, Region.Op.UNION);
Phil Weaver70439242016-03-10 15:15:49 -0800547 availableBounds.op(windowBounds, Region.Op.DIFFERENCE);
Svetoslav8e3feb12014-02-24 13:46:47 -0800548 }
549
Phil Weaver65c06702016-03-15 15:33:46 -0700550 // Update accounted bounds
Svetoslav8e3feb12014-02-24 13:46:47 -0800551 Region accountedBounds = mTempRegion2;
Phil Weaver70439242016-03-10 15:15:49 -0800552 accountedBounds.set(mMagnificationRegion);
Svetoslav8e3feb12014-02-24 13:46:47 -0800553 accountedBounds.op(nonMagnifiedBounds, Region.Op.UNION);
554 accountedBounds.op(0, 0, screenWidth, screenHeight, Region.Op.INTERSECT);
555
556 if (accountedBounds.isRect()) {
557 Rect accountedFrame = mTempRect1;
558 accountedBounds.getBounds(accountedFrame);
559 if (accountedFrame.width() == screenWidth
560 && accountedFrame.height() == screenHeight) {
561 break;
562 }
563 }
564 }
565
566 visibleWindows.clear();
567
Phil Weaver70439242016-03-10 15:15:49 -0800568 mMagnificationRegion.op(mDrawBorderInset, mDrawBorderInset,
Svetoslav7505e332014-08-22 12:14:28 -0700569 screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset,
Svetoslav8e3feb12014-02-24 13:46:47 -0800570 Region.Op.INTERSECT);
571
Phil Weaver70439242016-03-10 15:15:49 -0800572 final boolean magnifiedChanged =
573 !mOldMagnificationRegion.equals(mMagnificationRegion);
574 if (magnifiedChanged) {
575 mWindow.setBounds(mMagnificationRegion);
576 final Rect dirtyRect = mTempRect1;
577 if (mFullRedrawNeeded) {
578 mFullRedrawNeeded = false;
579 dirtyRect.set(mDrawBorderInset, mDrawBorderInset,
580 screenWidth - mDrawBorderInset,
581 screenHeight - mDrawBorderInset);
582 mWindow.invalidate(dirtyRect);
583 } else {
584 final Region dirtyRegion = mTempRegion3;
585 dirtyRegion.set(mMagnificationRegion);
586 dirtyRegion.op(mOldMagnificationRegion, Region.Op.UNION);
587 dirtyRegion.op(nonMagnifiedBounds, Region.Op.INTERSECT);
588 dirtyRegion.getBounds(dirtyRect);
589 mWindow.invalidate(dirtyRect);
Svetoslav8e3feb12014-02-24 13:46:47 -0800590 }
591
Phil Weaver70439242016-03-10 15:15:49 -0800592 mOldMagnificationRegion.set(mMagnificationRegion);
Alan Viverette214fb682015-11-17 09:47:11 -0500593 final SomeArgs args = SomeArgs.obtain();
Phil Weaver70439242016-03-10 15:15:49 -0800594 args.arg1 = Region.obtain(mMagnificationRegion);
Alan Viverette214fb682015-11-17 09:47:11 -0500595 mHandler.obtainMessage(
Phil Weaver70439242016-03-10 15:15:49 -0800596 MyHandler.MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED, args)
597 .sendToTarget();
Svetoslav8e3feb12014-02-24 13:46:47 -0800598 }
599 }
600
601 public void onRotationChangedLocked() {
602 // If we are magnifying, hide the magnified border window immediately so
603 // the user does not see strange artifacts during rotation. The screenshot
604 // used for rotation has already the border. After the rotation is complete
605 // we will show the border.
606 if (isMagnifyingLocked()) {
607 setMagnifiedRegionBorderShownLocked(false, false);
608 final long delay = (long) (mLongAnimationDuration
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700609 * mWindowManagerService.getWindowAnimationScaleLocked());
Svetoslav8e3feb12014-02-24 13:46:47 -0800610 Message message = mHandler.obtainMessage(
611 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED);
612 mHandler.sendMessageDelayed(message, delay);
613 }
614 recomputeBoundsLocked();
615 mWindow.updateSize();
616 }
617
618 public void setMagnifiedRegionBorderShownLocked(boolean shown, boolean animate) {
619 if (shown) {
620 mFullRedrawNeeded = true;
Phil Weaver70439242016-03-10 15:15:49 -0800621 mOldMagnificationRegion.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800622 }
623 mWindow.setShown(shown, animate);
624 }
625
626 public void getMagnifiedFrameInContentCoordsLocked(Rect rect) {
627 MagnificationSpec spec = mMagnificationSpec;
Phil Weaver70439242016-03-10 15:15:49 -0800628 mMagnificationRegion.getBounds(rect);
Svetoslav8e3feb12014-02-24 13:46:47 -0800629 rect.offset((int) -spec.offsetX, (int) -spec.offsetY);
630 rect.scale(1.0f / spec.scale);
631 }
632
633 public boolean isMagnifyingLocked() {
634 return mMagnificationSpec.scale > 1.0f;
635 }
636
637 public MagnificationSpec getMagnificationSpecLocked() {
638 return mMagnificationSpec;
639 }
640
641 /** NOTE: This has to be called within a surface transaction. */
642 public void drawWindowIfNeededLocked() {
643 recomputeBoundsLocked();
644 mWindow.drawIfNeeded();
645 }
646
647 public void destroyWindow() {
648 mWindow.releaseSurface();
649 }
650
651 private void populateWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700652 final DisplayContent dc = mWindowManagerService.getDefaultDisplayContentLocked();
Wale Ogunwaled1880962016-11-08 10:31:59 -0800653 dc.forAllWindows((w) -> {
654 if (w.isOnScreen() && w.isVisibleLw()
655 && !w.mWinAnimator.mEnterAnimationPending) {
656 outWindows.put(w.mLayer, w);
Svetoslav8e3feb12014-02-24 13:46:47 -0800657 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800658 }, false /* traverseTopToBottom */ );
Svetoslav8e3feb12014-02-24 13:46:47 -0800659 }
660
661 private final class ViewportWindow {
662 private static final String SURFACE_TITLE = "Magnification Overlay";
663
Svetoslav8e3feb12014-02-24 13:46:47 -0800664 private final Region mBounds = new Region();
665 private final Rect mDirtyRect = new Rect();
666 private final Paint mPaint = new Paint();
667
Svetoslav8e3feb12014-02-24 13:46:47 -0800668 private final SurfaceControl mSurfaceControl;
669 private final Surface mSurface = new Surface();
670
Svet Ganovb21df802014-09-01 19:06:33 -0700671 private final AnimationController mAnimationController;
672
Svetoslav8e3feb12014-02-24 13:46:47 -0800673 private boolean mShown;
674 private int mAlpha;
675
676 private boolean mInvalidated;
677
678 public ViewportWindow(Context context) {
679 SurfaceControl surfaceControl = null;
680 try {
681 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
682 surfaceControl = new SurfaceControl(mWindowManagerService.mFxSession,
683 SURFACE_TITLE, mTempPoint.x, mTempPoint.y, PixelFormat.TRANSLUCENT,
684 SurfaceControl.HIDDEN);
685 } catch (OutOfResourcesException oore) {
686 /* ignore */
687 }
688 mSurfaceControl = surfaceControl;
689 mSurfaceControl.setLayerStack(mWindowManager.getDefaultDisplay()
690 .getLayerStack());
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800691 mSurfaceControl.setLayer(mWindowManagerService.mPolicy.getWindowLayerFromTypeLw(
Svetoslav8e3feb12014-02-24 13:46:47 -0800692 WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY)
693 * WindowManagerService.TYPE_LAYER_MULTIPLIER);
694 mSurfaceControl.setPosition(0, 0);
695 mSurface.copyFrom(mSurfaceControl);
696
Svet Ganovb21df802014-09-01 19:06:33 -0700697 mAnimationController = new AnimationController(context,
698 mWindowManagerService.mH.getLooper());
699
Svetoslav8e3feb12014-02-24 13:46:47 -0800700 TypedValue typedValue = new TypedValue();
701 context.getTheme().resolveAttribute(R.attr.colorActivatedHighlight,
702 typedValue, true);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700703 final int borderColor = context.getColor(typedValue.resourceId);
Svetoslav8e3feb12014-02-24 13:46:47 -0800704
705 mPaint.setStyle(Paint.Style.STROKE);
706 mPaint.setStrokeWidth(mBorderWidth);
707 mPaint.setColor(borderColor);
708
Svetoslav8e3feb12014-02-24 13:46:47 -0800709 mInvalidated = true;
710 }
711
712 public void setShown(boolean shown, boolean animate) {
713 synchronized (mWindowManagerService.mWindowMap) {
714 if (mShown == shown) {
715 return;
716 }
717 mShown = shown;
Svet Ganovb21df802014-09-01 19:06:33 -0700718 mAnimationController.onFrameShownStateChanged(shown, animate);
Svetoslav8e3feb12014-02-24 13:46:47 -0800719 if (DEBUG_VIEWPORT_WINDOW) {
720 Slog.i(LOG_TAG, "ViewportWindow shown: " + mShown);
721 }
722 }
723 }
724
725 @SuppressWarnings("unused")
726 // Called reflectively from an animator.
727 public int getAlpha() {
728 synchronized (mWindowManagerService.mWindowMap) {
729 return mAlpha;
730 }
731 }
732
733 public void setAlpha(int alpha) {
734 synchronized (mWindowManagerService.mWindowMap) {
735 if (mAlpha == alpha) {
736 return;
737 }
738 mAlpha = alpha;
739 invalidate(null);
740 if (DEBUG_VIEWPORT_WINDOW) {
741 Slog.i(LOG_TAG, "ViewportWindow set alpha: " + alpha);
742 }
743 }
744 }
745
746 public void setBounds(Region bounds) {
747 synchronized (mWindowManagerService.mWindowMap) {
748 if (mBounds.equals(bounds)) {
749 return;
750 }
751 mBounds.set(bounds);
752 invalidate(mDirtyRect);
753 if (DEBUG_VIEWPORT_WINDOW) {
754 Slog.i(LOG_TAG, "ViewportWindow set bounds: " + bounds);
755 }
756 }
757 }
758
759 public void updateSize() {
760 synchronized (mWindowManagerService.mWindowMap) {
761 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
762 mSurfaceControl.setSize(mTempPoint.x, mTempPoint.y);
763 invalidate(mDirtyRect);
764 }
765 }
766
767 public void invalidate(Rect dirtyRect) {
768 if (dirtyRect != null) {
769 mDirtyRect.set(dirtyRect);
770 } else {
771 mDirtyRect.setEmpty();
772 }
773 mInvalidated = true;
774 mWindowManagerService.scheduleAnimationLocked();
775 }
776
777 /** NOTE: This has to be called within a surface transaction. */
778 public void drawIfNeeded() {
779 synchronized (mWindowManagerService.mWindowMap) {
780 if (!mInvalidated) {
781 return;
782 }
783 mInvalidated = false;
784 Canvas canvas = null;
785 try {
786 // Empty dirty rectangle means unspecified.
787 if (mDirtyRect.isEmpty()) {
788 mBounds.getBounds(mDirtyRect);
789 }
790 mDirtyRect.inset(- mHalfBorderWidth, - mHalfBorderWidth);
791 canvas = mSurface.lockCanvas(mDirtyRect);
792 if (DEBUG_VIEWPORT_WINDOW) {
793 Slog.i(LOG_TAG, "Dirty rect: " + mDirtyRect);
794 }
795 } catch (IllegalArgumentException iae) {
796 /* ignore */
797 } catch (Surface.OutOfResourcesException oore) {
798 /* ignore */
799 }
800 if (canvas == null) {
801 return;
802 }
803 if (DEBUG_VIEWPORT_WINDOW) {
804 Slog.i(LOG_TAG, "Bounds: " + mBounds);
805 }
806 canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
807 mPaint.setAlpha(mAlpha);
808 Path path = mBounds.getBoundaryPath();
809 canvas.drawPath(path, mPaint);
810
811 mSurface.unlockCanvasAndPost(canvas);
812
813 if (mAlpha > 0) {
814 mSurfaceControl.show();
815 } else {
816 mSurfaceControl.hide();
817 }
818 }
819 }
820
821 public void releaseSurface() {
822 mSurfaceControl.release();
823 mSurface.release();
824 }
Svet Ganovb21df802014-09-01 19:06:33 -0700825
826 private final class AnimationController extends Handler {
827 private static final String PROPERTY_NAME_ALPHA = "alpha";
828
829 private static final int MIN_ALPHA = 0;
830 private static final int MAX_ALPHA = 255;
831
832 private static final int MSG_FRAME_SHOWN_STATE_CHANGED = 1;
833
834 private final ValueAnimator mShowHideFrameAnimator;
835
836 public AnimationController(Context context, Looper looper) {
837 super(looper);
838 mShowHideFrameAnimator = ObjectAnimator.ofInt(ViewportWindow.this,
839 PROPERTY_NAME_ALPHA, MIN_ALPHA, MAX_ALPHA);
840
841 Interpolator interpolator = new DecelerateInterpolator(2.5f);
842 final long longAnimationDuration = context.getResources().getInteger(
843 com.android.internal.R.integer.config_longAnimTime);
844
845 mShowHideFrameAnimator.setInterpolator(interpolator);
846 mShowHideFrameAnimator.setDuration(longAnimationDuration);
847 }
848
849 public void onFrameShownStateChanged(boolean shown, boolean animate) {
850 obtainMessage(MSG_FRAME_SHOWN_STATE_CHANGED,
851 shown ? 1 : 0, animate ? 1 : 0).sendToTarget();
852 }
853
854 @Override
855 public void handleMessage(Message message) {
856 switch (message.what) {
857 case MSG_FRAME_SHOWN_STATE_CHANGED: {
858 final boolean shown = message.arg1 == 1;
859 final boolean animate = message.arg2 == 1;
860
861 if (animate) {
862 if (mShowHideFrameAnimator.isRunning()) {
863 mShowHideFrameAnimator.reverse();
864 } else {
865 if (shown) {
866 mShowHideFrameAnimator.start();
867 } else {
868 mShowHideFrameAnimator.reverse();
869 }
870 }
871 } else {
872 mShowHideFrameAnimator.cancel();
873 if (shown) {
874 setAlpha(MAX_ALPHA);
875 } else {
876 setAlpha(MIN_ALPHA);
877 }
878 }
879 } break;
880 }
881 }
882 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800883 }
884 }
885
886 private class MyHandler extends Handler {
Phil Weaver70439242016-03-10 15:15:49 -0800887 public static final int MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -0800888 public static final int MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED = 2;
889 public static final int MESSAGE_NOTIFY_USER_CONTEXT_CHANGED = 3;
890 public static final int MESSAGE_NOTIFY_ROTATION_CHANGED = 4;
891 public static final int MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED = 5;
892
893 public MyHandler(Looper looper) {
894 super(looper);
895 }
896
897 @Override
898 public void handleMessage(Message message) {
899 switch (message.what) {
Phil Weaver70439242016-03-10 15:15:49 -0800900 case MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED: {
Alan Viverette214fb682015-11-17 09:47:11 -0500901 final SomeArgs args = (SomeArgs) message.obj;
902 final Region magnifiedBounds = (Region) args.arg1;
Phil Weaver70439242016-03-10 15:15:49 -0800903 mCallbacks.onMagnificationRegionChanged(magnifiedBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500904 magnifiedBounds.recycle();
Svetoslav8e3feb12014-02-24 13:46:47 -0800905 } break;
906
907 case MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED: {
908 SomeArgs args = (SomeArgs) message.obj;
909 final int left = args.argi1;
910 final int top = args.argi2;
911 final int right = args.argi3;
912 final int bottom = args.argi4;
913 mCallbacks.onRectangleOnScreenRequested(left, top, right, bottom);
914 args.recycle();
915 } break;
916
917 case MESSAGE_NOTIFY_USER_CONTEXT_CHANGED: {
918 mCallbacks.onUserContextChanged();
919 } break;
920
921 case MESSAGE_NOTIFY_ROTATION_CHANGED: {
922 final int rotation = message.arg1;
923 mCallbacks.onRotationChanged(rotation);
924 } break;
925
926 case MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED : {
927 synchronized (mWindowManagerService.mWindowMap) {
928 if (mMagnifedViewport.isMagnifyingLocked()) {
929 mMagnifedViewport.setMagnifiedRegionBorderShownLocked(true, true);
930 mWindowManagerService.scheduleAnimationLocked();
931 }
932 }
933 } break;
934 }
935 }
936 }
937 }
938
939 /**
940 * This class encapsulates the functionality related to computing the windows
941 * reported for accessibility purposes. These windows are all windows a sighted
942 * user can see on the screen.
943 */
944 private static final class WindowsForAccessibilityObserver {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800945 private static final String LOG_TAG = TAG_WITH_CLASS_NAME ?
946 "WindowsForAccessibilityObserver" : TAG_WM;
Svetoslav8e3feb12014-02-24 13:46:47 -0800947
948 private static final boolean DEBUG = false;
949
950 private final SparseArray<WindowState> mTempWindowStates =
951 new SparseArray<WindowState>();
952
953 private final List<WindowInfo> mOldWindows = new ArrayList<WindowInfo>();
954
955 private final Set<IBinder> mTempBinderSet = new ArraySet<IBinder>();
956
957 private final RectF mTempRectF = new RectF();
958
959 private final Matrix mTempMatrix = new Matrix();
960
961 private final Point mTempPoint = new Point();
962
963 private final Rect mTempRect = new Rect();
964
965 private final Region mTempRegion = new Region();
966
967 private final Region mTempRegion1 = new Region();
968
969 private final Context mContext;
970
971 private final WindowManagerService mWindowManagerService;
972
973 private final Handler mHandler;
974
975 private final WindowsForAccessibilityCallback mCallback;
976
Svetoslavf7174e82014-06-12 11:29:35 -0700977 private final long mRecurringAccessibilityEventsIntervalMillis;
978
Svetoslav8e3feb12014-02-24 13:46:47 -0800979 public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
980 WindowsForAccessibilityCallback callback) {
981 mContext = windowManagerService.mContext;
982 mWindowManagerService = windowManagerService;
983 mCallback = callback;
984 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
Svetoslavf7174e82014-06-12 11:29:35 -0700985 mRecurringAccessibilityEventsIntervalMillis = ViewConfiguration
986 .getSendRecurringAccessibilityEventsInterval();
Svetoslav8e3feb12014-02-24 13:46:47 -0800987 computeChangedWindows();
988 }
989
Svetoslav3a0d8782014-12-04 12:50:11 -0800990 public void performComputeChangedWindowsNotLocked() {
991 mHandler.removeMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS);
992 computeChangedWindows();
993 }
994
Svetoslavf7174e82014-06-12 11:29:35 -0700995 public void scheduleComputeChangedWindowsLocked() {
Svetoslav3a0d8782014-12-04 12:50:11 -0800996 if (!mHandler.hasMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS)) {
Svetoslavf7174e82014-06-12 11:29:35 -0700997 mHandler.sendEmptyMessageDelayed(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS,
998 mRecurringAccessibilityEventsIntervalMillis);
999 }
1000 }
1001
Svetoslav8e3feb12014-02-24 13:46:47 -08001002 public void computeChangedWindows() {
1003 if (DEBUG) {
1004 Slog.i(LOG_TAG, "computeChangedWindows()");
1005 }
1006
Svetoslav3a0d8782014-12-04 12:50:11 -08001007 boolean windowsChanged = false;
1008 List<WindowInfo> windows = new ArrayList<WindowInfo>();
1009
Svetoslav8e3feb12014-02-24 13:46:47 -08001010 synchronized (mWindowManagerService.mWindowMap) {
Svetoslavf7174e82014-06-12 11:29:35 -07001011 // Do not send the windows if there is no current focus as
1012 // the window manager is still looking for where to put it.
1013 // We will do the work when we get a focus change callback.
1014 if (mWindowManagerService.mCurrentFocus == null) {
1015 return;
1016 }
1017
Svetoslav8e3feb12014-02-24 13:46:47 -08001018 WindowManager windowManager = (WindowManager)
1019 mContext.getSystemService(Context.WINDOW_SERVICE);
1020 windowManager.getDefaultDisplay().getRealSize(mTempPoint);
1021 final int screenWidth = mTempPoint.x;
1022 final int screenHeight = mTempPoint.y;
1023
1024 Region unaccountedSpace = mTempRegion;
1025 unaccountedSpace.set(0, 0, screenWidth, screenHeight);
1026
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001027 final SparseArray<WindowState> visibleWindows = mTempWindowStates;
Svetoslav8e3feb12014-02-24 13:46:47 -08001028 populateVisibleWindowsOnScreenLocked(visibleWindows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001029 Set<IBinder> addedWindows = mTempBinderSet;
1030 addedWindows.clear();
1031
Svetoslavf7174e82014-06-12 11:29:35 -07001032 boolean focusedWindowAdded = false;
1033
Svetoslav8e3feb12014-02-24 13:46:47 -08001034 final int visibleWindowCount = visibleWindows.size();
Allen Hairf20ac2c2016-02-11 17:42:59 -08001035 HashSet<Integer> skipRemainingWindowsForTasks = new HashSet<>();
Svetoslav8e3feb12014-02-24 13:46:47 -08001036 for (int i = visibleWindowCount - 1; i >= 0; i--) {
Alan Viverette9538eea2014-11-13 14:49:20 -08001037 final WindowState windowState = visibleWindows.valueAt(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001038 final int flags = windowState.mAttrs.flags;
Allen Hairf20ac2c2016-02-11 17:42:59 -08001039 final Task task = windowState.getTask();
1040
1041 // If the window is part of a task that we're finished with - ignore.
1042 if (task != null && skipRemainingWindowsForTasks.contains(task.mTaskId)) {
1043 continue;
1044 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001045
Svetoslav3a5c7212014-10-14 09:54:26 -07001046 // If the window is not touchable - ignore.
Svetoslavf7174e82014-06-12 11:29:35 -07001047 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001048 continue;
1049 }
1050
Alan Viverette9538eea2014-11-13 14:49:20 -08001051 // Compute the bounds in the screen.
1052 final Rect boundsInScreen = mTempRect;
1053 computeWindowBoundsInScreen(windowState, boundsInScreen);
1054
Svetoslav8e3feb12014-02-24 13:46:47 -08001055 // If the window is completely covered by other windows - ignore.
1056 if (unaccountedSpace.quickReject(boundsInScreen)) {
1057 continue;
1058 }
1059
1060 // Add windows of certain types not covered by modal windows.
1061 if (isReportedWindowType(windowState.mAttrs.type)) {
1062 // Add the window to the ones to be reported.
Svetoslavf7174e82014-06-12 11:29:35 -07001063 WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
Svetoslav8e3feb12014-02-24 13:46:47 -08001064 addedWindows.add(window.token);
Svetoslav8e3feb12014-02-24 13:46:47 -08001065 windows.add(window);
Svetoslavf7174e82014-06-12 11:29:35 -07001066 if (windowState.isFocused()) {
1067 focusedWindowAdded = true;
1068 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001069 }
1070
Alan Viveretted0c73f42014-11-18 10:25:04 -08001071 // Account for the space this window takes if the window
1072 // is not an accessibility overlay which does not change
1073 // the reported windows.
1074 if (windowState.mAttrs.type !=
1075 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY) {
1076 unaccountedSpace.op(boundsInScreen, unaccountedSpace,
1077 Region.Op.REVERSE_DIFFERENCE);
1078 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001079
Allen Hairf20ac2c2016-02-11 17:42:59 -08001080 // If a window is modal it prevents other windows from being touched
Svetoslav8e3feb12014-02-24 13:46:47 -08001081 if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1082 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
Phil Weaverac9ad702016-07-27 18:03:10 -07001083 // Account for all space in the task, whether the windows in it are
1084 // touchable or not. The modal window blocks all touches from the task's
1085 // area.
1086 unaccountedSpace.op(windowState.getDisplayFrameLw(), unaccountedSpace,
1087 Region.Op.REVERSE_DIFFERENCE);
1088
Allen Hairf20ac2c2016-02-11 17:42:59 -08001089 if (task != null) {
1090 // If the window is associated with a particular task, we can skip the
1091 // rest of the windows for that task.
1092 skipRemainingWindowsForTasks.add(task.mTaskId);
1093 continue;
1094 } else {
1095 // If the window is not associated with a particular task, then it is
1096 // globally modal. In this case we can skip all remaining windows.
1097 break;
1098 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001099 }
Phil Weaverac9ad702016-07-27 18:03:10 -07001100 // We figured out what is touchable for the entire screen - done.
1101 if (unaccountedSpace.isEmpty()) {
1102 break;
1103 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001104 }
1105
Svetoslavf7174e82014-06-12 11:29:35 -07001106 // Always report the focused window.
1107 if (!focusedWindowAdded) {
1108 for (int i = visibleWindowCount - 1; i >= 0; i--) {
1109 WindowState windowState = visibleWindows.valueAt(i);
1110 if (windowState.isFocused()) {
1111 // Compute the bounds in the screen.
1112 Rect boundsInScreen = mTempRect;
1113 computeWindowBoundsInScreen(windowState, boundsInScreen);
1114
1115 // Add the window to the ones to be reported.
1116 WindowInfo window = obtainPopulatedWindowInfo(windowState,
1117 boundsInScreen);
1118 addedWindows.add(window.token);
1119 windows.add(window);
1120 break;
1121 }
1122 }
1123 }
1124
Svetoslav8e3feb12014-02-24 13:46:47 -08001125 // Remove child/parent references to windows that were not added.
1126 final int windowCount = windows.size();
1127 for (int i = 0; i < windowCount; i++) {
1128 WindowInfo window = windows.get(i);
1129 if (!addedWindows.contains(window.parentToken)) {
1130 window.parentToken = null;
1131 }
1132 if (window.childTokens != null) {
1133 final int childTokenCount = window.childTokens.size();
1134 for (int j = childTokenCount - 1; j >= 0; j--) {
1135 if (!addedWindows.contains(window.childTokens.get(j))) {
1136 window.childTokens.remove(j);
1137 }
1138 }
1139 // Leave the child token list if empty.
1140 }
1141 }
1142
1143 visibleWindows.clear();
1144 addedWindows.clear();
1145
1146 // We computed the windows and if they changed notify the client.
Svetoslav8e3feb12014-02-24 13:46:47 -08001147 if (mOldWindows.size() != windows.size()) {
1148 // Different size means something changed.
1149 windowsChanged = true;
1150 } else if (!mOldWindows.isEmpty() || !windows.isEmpty()) {
1151 // Since we always traverse windows from high to low layer
1152 // the old and new windows at the same index should be the
1153 // same, otherwise something changed.
1154 for (int i = 0; i < windowCount; i++) {
1155 WindowInfo oldWindow = mOldWindows.get(i);
1156 WindowInfo newWindow = windows.get(i);
1157 // We do not care for layer changes given the window
1158 // order does not change. This brings no new information
1159 // to the clients.
1160 if (windowChangedNoLayer(oldWindow, newWindow)) {
1161 windowsChanged = true;
1162 break;
1163 }
1164 }
1165 }
1166
1167 if (windowsChanged) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001168 cacheWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001169 }
1170 }
Svetoslav3a0d8782014-12-04 12:50:11 -08001171
1172 // Now we do not hold the lock, so send the windows over.
1173 if (windowsChanged) {
1174 if (DEBUG) {
1175 Log.i(LOG_TAG, "Windows changed:" + windows);
1176 }
1177 mCallback.onWindowsForAccessibilityChanged(windows);
1178 } else {
1179 if (DEBUG) {
1180 Log.i(LOG_TAG, "No windows changed.");
1181 }
1182 }
1183
1184 // Recycle the windows as we do not need them.
1185 clearAndRecycleWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001186 }
1187
Svetoslavf7174e82014-06-12 11:29:35 -07001188 private void computeWindowBoundsInScreen(WindowState windowState, Rect outBounds) {
1189 // Get the touchable frame.
1190 Region touchableRegion = mTempRegion1;
1191 windowState.getTouchableRegion(touchableRegion);
1192 Rect touchableFrame = mTempRect;
1193 touchableRegion.getBounds(touchableFrame);
1194
1195 // Move to origin as all transforms are captured by the matrix.
1196 RectF windowFrame = mTempRectF;
1197 windowFrame.set(touchableFrame);
1198 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
1199
1200 // Map the frame to get what appears on the screen.
1201 Matrix matrix = mTempMatrix;
1202 populateTransformationMatrixLocked(windowState, matrix);
1203 matrix.mapRect(windowFrame);
1204
1205 // Got the bounds.
1206 outBounds.set((int) windowFrame.left, (int) windowFrame.top,
1207 (int) windowFrame.right, (int) windowFrame.bottom);
1208 }
1209
Wale Ogunwaleadde52e2016-07-16 13:11:55 -07001210 private static WindowInfo obtainPopulatedWindowInfo(
1211 WindowState windowState, Rect boundsInScreen) {
1212 final WindowInfo window = windowState.getWindowInfo();
Svetoslavf7174e82014-06-12 11:29:35 -07001213 window.boundsInScreen.set(boundsInScreen);
Svetoslavf7174e82014-06-12 11:29:35 -07001214 return window;
1215 }
1216
Svetoslav8e3feb12014-02-24 13:46:47 -08001217 private void cacheWindows(List<WindowInfo> windows) {
1218 final int oldWindowCount = mOldWindows.size();
1219 for (int i = oldWindowCount - 1; i >= 0; i--) {
1220 mOldWindows.remove(i).recycle();
1221 }
1222 final int newWindowCount = windows.size();
1223 for (int i = 0; i < newWindowCount; i++) {
1224 WindowInfo newWindow = windows.get(i);
1225 mOldWindows.add(WindowInfo.obtain(newWindow));
1226 }
1227 }
1228
1229 private boolean windowChangedNoLayer(WindowInfo oldWindow, WindowInfo newWindow) {
1230 if (oldWindow == newWindow) {
1231 return false;
1232 }
Svetoslavf7174e82014-06-12 11:29:35 -07001233 if (oldWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001234 return true;
1235 }
Svetoslavf7174e82014-06-12 11:29:35 -07001236 if (newWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001237 return true;
1238 }
1239 if (oldWindow.type != newWindow.type) {
1240 return true;
1241 }
1242 if (oldWindow.focused != newWindow.focused) {
1243 return true;
1244 }
1245 if (oldWindow.token == null) {
1246 if (newWindow.token != null) {
1247 return true;
1248 }
1249 } else if (!oldWindow.token.equals(newWindow.token)) {
1250 return true;
1251 }
1252 if (oldWindow.parentToken == null) {
1253 if (newWindow.parentToken != null) {
1254 return true;
1255 }
1256 } else if (!oldWindow.parentToken.equals(newWindow.parentToken)) {
1257 return true;
1258 }
1259 if (!oldWindow.boundsInScreen.equals(newWindow.boundsInScreen)) {
1260 return true;
1261 }
1262 if (oldWindow.childTokens != null && newWindow.childTokens != null
1263 && !oldWindow.childTokens.equals(newWindow.childTokens)) {
1264 return true;
1265 }
Phil Weaver396d5492016-03-22 17:53:50 -07001266 if (!TextUtils.equals(oldWindow.title, newWindow.title)) {
1267 return true;
1268 }
1269 if (oldWindow.accessibilityIdOfAnchor != newWindow.accessibilityIdOfAnchor) {
1270 return true;
1271 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001272 return false;
1273 }
1274
Svetoslav3a0d8782014-12-04 12:50:11 -08001275 private static void clearAndRecycleWindows(List<WindowInfo> windows) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001276 final int windowCount = windows.size();
1277 for (int i = windowCount - 1; i >= 0; i--) {
1278 windows.remove(i).recycle();
1279 }
1280 }
1281
1282 private static boolean isReportedWindowType(int windowType) {
Jorim Jaggi73294b62016-10-26 18:02:36 -07001283 return (windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
Svetoslav8e3feb12014-02-24 13:46:47 -08001284 && windowType != WindowManager.LayoutParams.TYPE_BOOT_PROGRESS
1285 && windowType != WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY
1286 && windowType != WindowManager.LayoutParams.TYPE_DRAG
Selim Cinekf83e8242015-05-19 18:08:14 -07001287 && windowType != WindowManager.LayoutParams.TYPE_INPUT_CONSUMER
Svetoslav8e3feb12014-02-24 13:46:47 -08001288 && windowType != WindowManager.LayoutParams.TYPE_POINTER
Svetoslav8e3feb12014-02-24 13:46:47 -08001289 && windowType != WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY
1290 && windowType != WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY
1291 && windowType != WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY
1292 && windowType != WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION);
1293 }
1294
1295 private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001296 final DisplayContent dc = mWindowManagerService.getDefaultDisplayContentLocked();
Wale Ogunwaled1880962016-11-08 10:31:59 -08001297 dc.forAllWindows((w) -> {
1298 if (w.isVisibleLw()) {
1299 outWindows.put(w.mLayer, w);
Svetoslav8e3feb12014-02-24 13:46:47 -08001300 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001301 }, false /* traverseTopToBottom */ );
Svetoslav8e3feb12014-02-24 13:46:47 -08001302 }
1303
1304 private class MyHandler extends Handler {
Svetoslavf7174e82014-06-12 11:29:35 -07001305 public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -08001306
1307 public MyHandler(Looper looper) {
1308 super(looper, null, false);
1309 }
1310
1311 @Override
1312 @SuppressWarnings("unchecked")
1313 public void handleMessage(Message message) {
1314 switch (message.what) {
Svetoslavf7174e82014-06-12 11:29:35 -07001315 case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1316 computeChangedWindows();
1317 } break;
Svetoslav8e3feb12014-02-24 13:46:47 -08001318 }
1319 }
1320 }
1321 }
1322}