blob: 1d6cc32a9d614fca12e780f3d6c4672f18d05214 [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:
361 case WindowManager.LayoutParams.TYPE_APPLICATION_PANEL:
362 case WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA:
363 case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
Wale Ogunwale0a4dc222015-04-14 12:58:42 -0700364 case WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL:
Svetoslav8e3feb12014-02-24 13:46:47 -0800365 case WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG:
366 case WindowManager.LayoutParams.TYPE_SEARCH_BAR:
367 case WindowManager.LayoutParams.TYPE_PHONE:
368 case WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
369 case WindowManager.LayoutParams.TYPE_TOAST:
370 case WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY:
371 case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
372 case WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG:
373 case WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG:
374 case WindowManager.LayoutParams.TYPE_SYSTEM_ERROR:
375 case WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY:
Jason Monk8f7f3182015-11-18 16:35:14 -0500376 case WindowManager.LayoutParams.TYPE_QS_DIALOG:
Adrian Roos9a645132014-10-08 02:59:56 +0200377 case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL: {
Svetoslav8e3feb12014-02-24 13:46:47 -0800378 Rect magnifiedRegionBounds = mTempRect2;
379 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(
380 magnifiedRegionBounds);
381 Rect touchableRegionBounds = mTempRect1;
382 windowState.getTouchableRegion(mTempRegion1);
383 mTempRegion1.getBounds(touchableRegionBounds);
384 if (!magnifiedRegionBounds.intersect(touchableRegionBounds)) {
385 mCallbacks.onRectangleOnScreenRequested(
386 touchableRegionBounds.left,
387 touchableRegionBounds.top,
388 touchableRegionBounds.right,
389 touchableRegionBounds.bottom);
390 }
391 } break;
392 } break;
393 }
394 }
395 }
396
397 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
398 MagnificationSpec spec = mMagnifedViewport.getMagnificationSpecLocked();
399 if (spec != null && !spec.isNop()) {
400 WindowManagerPolicy policy = mWindowManagerService.mPolicy;
401 final int windowType = windowState.mAttrs.type;
Wale Ogunwale7ed4d372016-07-09 15:28:55 -0700402 if (!policy.isTopLevelWindow(windowType) && windowState.isChildWindow()
Svetoslav8e3feb12014-02-24 13:46:47 -0800403 && !policy.canMagnifyWindow(windowType)) {
404 return null;
405 }
406 if (!policy.canMagnifyWindow(windowState.mAttrs.type)) {
407 return null;
408 }
409 }
410 return spec;
411 }
412
Phil Weaver70439242016-03-10 15:15:49 -0800413 public void getMagnificationRegionLocked(Region outMagnificationRegion) {
414 mMagnifedViewport.getMagnificationRegionLocked(outMagnificationRegion);
Alan Viverette59e53a12016-03-28 13:41:32 -0400415 }
416
Svetoslav8e3feb12014-02-24 13:46:47 -0800417 public void destroyLocked() {
418 mMagnifedViewport.destroyWindow();
419 }
420
421 /** NOTE: This has to be called within a surface transaction. */
422 public void drawMagnifiedRegionBorderIfNeededLocked() {
423 mMagnifedViewport.drawWindowIfNeededLocked();
424 }
425
426 private final class MagnifiedViewport {
427
Svetoslav8e3feb12014-02-24 13:46:47 -0800428 private final SparseArray<WindowState> mTempWindowStates =
429 new SparseArray<WindowState>();
430
431 private final RectF mTempRectF = new RectF();
432
433 private final Point mTempPoint = new Point();
434
435 private final Matrix mTempMatrix = new Matrix();
436
Phil Weaver70439242016-03-10 15:15:49 -0800437 private final Region mMagnificationRegion = new Region();
438 private final Region mOldMagnificationRegion = new Region();
Svetoslav8e3feb12014-02-24 13:46:47 -0800439
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800440 private final Path mCircularPath;
441
Svetoslav8e3feb12014-02-24 13:46:47 -0800442 private final MagnificationSpec mMagnificationSpec = MagnificationSpec.obtain();
443
444 private final WindowManager mWindowManager;
445
Svetoslav7505e332014-08-22 12:14:28 -0700446 private final float mBorderWidth;
Svetoslav8e3feb12014-02-24 13:46:47 -0800447 private final int mHalfBorderWidth;
Svetoslav7505e332014-08-22 12:14:28 -0700448 private final int mDrawBorderInset;
Svetoslav8e3feb12014-02-24 13:46:47 -0800449
450 private final ViewportWindow mWindow;
451
452 private boolean mFullRedrawNeeded;
453
454 public MagnifiedViewport() {
455 mWindowManager = (WindowManager) mContext.getSystemService(Service.WINDOW_SERVICE);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800456 mBorderWidth = mContext.getResources().getDimension(
457 com.android.internal.R.dimen.accessibility_magnification_indicator_width);
Svetoslav7505e332014-08-22 12:14:28 -0700458 mHalfBorderWidth = (int) Math.ceil(mBorderWidth / 2);
459 mDrawBorderInset = (int) mBorderWidth / 2;
Svetoslav8e3feb12014-02-24 13:46:47 -0800460 mWindow = new ViewportWindow(mContext);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800461
Adam Powell01f280d2015-05-18 16:07:42 -0700462 if (mContext.getResources().getConfiguration().isScreenRound()) {
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800463 mCircularPath = new Path();
464 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
465 final int centerXY = mTempPoint.x / 2;
466 mCircularPath.addCircle(centerXY, centerXY, centerXY, Path.Direction.CW);
467 } else {
468 mCircularPath = null;
469 }
470
Svetoslav8e3feb12014-02-24 13:46:47 -0800471 recomputeBoundsLocked();
472 }
473
Phil Weaver70439242016-03-10 15:15:49 -0800474 public void getMagnificationRegionLocked(@NonNull Region outMagnificationRegion) {
475 outMagnificationRegion.set(mMagnificationRegion);
Alan Viverette59e53a12016-03-28 13:41:32 -0400476 }
477
Svetoslav8e3feb12014-02-24 13:46:47 -0800478 public void updateMagnificationSpecLocked(MagnificationSpec spec) {
479 if (spec != null) {
480 mMagnificationSpec.initialize(spec.scale, spec.offsetX, spec.offsetY);
481 } else {
482 mMagnificationSpec.clear();
483 }
484 // If this message is pending we are in a rotation animation and do not want
485 // to show the border. We will do so when the pending message is handled.
Svetoslav7505e332014-08-22 12:14:28 -0700486 if (!mHandler.hasMessages(
487 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED)) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800488 setMagnifiedRegionBorderShownLocked(isMagnifyingLocked(), true);
489 }
490 }
491
492 public void recomputeBoundsLocked() {
493 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
494 final int screenWidth = mTempPoint.x;
495 final int screenHeight = mTempPoint.y;
496
Phil Weaver70439242016-03-10 15:15:49 -0800497 mMagnificationRegion.set(0, 0, 0, 0);
498 final Region availableBounds = mTempRegion1;
499 availableBounds.set(0, 0, screenWidth, screenHeight);
Svetoslav8e3feb12014-02-24 13:46:47 -0800500
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800501 if (mCircularPath != null) {
Phil Weaver70439242016-03-10 15:15:49 -0800502 availableBounds.setPath(mCircularPath, availableBounds);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800503 }
504
Svetoslav8e3feb12014-02-24 13:46:47 -0800505 Region nonMagnifiedBounds = mTempRegion4;
Svet Ganovb21df802014-09-01 19:06:33 -0700506 nonMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800507
508 SparseArray<WindowState> visibleWindows = mTempWindowStates;
509 visibleWindows.clear();
510 populateWindowsOnScreenLocked(visibleWindows);
511
512 final int visibleWindowCount = visibleWindows.size();
513 for (int i = visibleWindowCount - 1; i >= 0; i--) {
514 WindowState windowState = visibleWindows.valueAt(i);
515 if (windowState.mAttrs.type == WindowManager
516 .LayoutParams.TYPE_MAGNIFICATION_OVERLAY) {
517 continue;
518 }
519
Phil Weaver65c06702016-03-15 15:33:46 -0700520 // Consider the touchable portion of the window
Svetoslav8e3feb12014-02-24 13:46:47 -0800521 Matrix matrix = mTempMatrix;
522 populateTransformationMatrixLocked(windowState, matrix);
Phil Weaver65c06702016-03-15 15:33:46 -0700523 Region touchableRegion = mTempRegion3;
524 windowState.getTouchableRegion(touchableRegion);
525 Rect touchableFrame = mTempRect1;
526 touchableRegion.getBounds(touchableFrame);
Svetoslav8e3feb12014-02-24 13:46:47 -0800527 RectF windowFrame = mTempRectF;
Phil Weaver65c06702016-03-15 15:33:46 -0700528 windowFrame.set(touchableFrame);
529 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
530 matrix.mapRect(windowFrame);
531 Region windowBounds = mTempRegion2;
532 windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
533 (int) windowFrame.right, (int) windowFrame.bottom);
534 // Only update new regions
535 Region portionOfWindowAlreadyAccountedFor = mTempRegion3;
Phil Weaver70439242016-03-10 15:15:49 -0800536 portionOfWindowAlreadyAccountedFor.set(mMagnificationRegion);
Phil Weaver65c06702016-03-15 15:33:46 -0700537 portionOfWindowAlreadyAccountedFor.op(nonMagnifiedBounds, Region.Op.UNION);
538 windowBounds.op(portionOfWindowAlreadyAccountedFor, Region.Op.DIFFERENCE);
Svetoslav8e3feb12014-02-24 13:46:47 -0800539
540 if (mWindowManagerService.mPolicy.canMagnifyWindow(windowState.mAttrs.type)) {
Phil Weaver70439242016-03-10 15:15:49 -0800541 mMagnificationRegion.op(windowBounds, Region.Op.UNION);
542 mMagnificationRegion.op(availableBounds, Region.Op.INTERSECT);
Svetoslav8e3feb12014-02-24 13:46:47 -0800543 } else {
Svetoslav8e3feb12014-02-24 13:46:47 -0800544 nonMagnifiedBounds.op(windowBounds, Region.Op.UNION);
Phil Weaver70439242016-03-10 15:15:49 -0800545 availableBounds.op(windowBounds, Region.Op.DIFFERENCE);
Svetoslav8e3feb12014-02-24 13:46:47 -0800546 }
547
Phil Weaver65c06702016-03-15 15:33:46 -0700548 // Update accounted bounds
Svetoslav8e3feb12014-02-24 13:46:47 -0800549 Region accountedBounds = mTempRegion2;
Phil Weaver70439242016-03-10 15:15:49 -0800550 accountedBounds.set(mMagnificationRegion);
Svetoslav8e3feb12014-02-24 13:46:47 -0800551 accountedBounds.op(nonMagnifiedBounds, Region.Op.UNION);
552 accountedBounds.op(0, 0, screenWidth, screenHeight, Region.Op.INTERSECT);
553
554 if (accountedBounds.isRect()) {
555 Rect accountedFrame = mTempRect1;
556 accountedBounds.getBounds(accountedFrame);
557 if (accountedFrame.width() == screenWidth
558 && accountedFrame.height() == screenHeight) {
559 break;
560 }
561 }
562 }
563
564 visibleWindows.clear();
565
Phil Weaver70439242016-03-10 15:15:49 -0800566 mMagnificationRegion.op(mDrawBorderInset, mDrawBorderInset,
Svetoslav7505e332014-08-22 12:14:28 -0700567 screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset,
Svetoslav8e3feb12014-02-24 13:46:47 -0800568 Region.Op.INTERSECT);
569
Phil Weaver70439242016-03-10 15:15:49 -0800570 final boolean magnifiedChanged =
571 !mOldMagnificationRegion.equals(mMagnificationRegion);
572 if (magnifiedChanged) {
573 mWindow.setBounds(mMagnificationRegion);
574 final Rect dirtyRect = mTempRect1;
575 if (mFullRedrawNeeded) {
576 mFullRedrawNeeded = false;
577 dirtyRect.set(mDrawBorderInset, mDrawBorderInset,
578 screenWidth - mDrawBorderInset,
579 screenHeight - mDrawBorderInset);
580 mWindow.invalidate(dirtyRect);
581 } else {
582 final Region dirtyRegion = mTempRegion3;
583 dirtyRegion.set(mMagnificationRegion);
584 dirtyRegion.op(mOldMagnificationRegion, Region.Op.UNION);
585 dirtyRegion.op(nonMagnifiedBounds, Region.Op.INTERSECT);
586 dirtyRegion.getBounds(dirtyRect);
587 mWindow.invalidate(dirtyRect);
Svetoslav8e3feb12014-02-24 13:46:47 -0800588 }
589
Phil Weaver70439242016-03-10 15:15:49 -0800590 mOldMagnificationRegion.set(mMagnificationRegion);
Alan Viverette214fb682015-11-17 09:47:11 -0500591 final SomeArgs args = SomeArgs.obtain();
Phil Weaver70439242016-03-10 15:15:49 -0800592 args.arg1 = Region.obtain(mMagnificationRegion);
Alan Viverette214fb682015-11-17 09:47:11 -0500593 mHandler.obtainMessage(
Phil Weaver70439242016-03-10 15:15:49 -0800594 MyHandler.MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED, args)
595 .sendToTarget();
Svetoslav8e3feb12014-02-24 13:46:47 -0800596 }
597 }
598
599 public void onRotationChangedLocked() {
600 // If we are magnifying, hide the magnified border window immediately so
601 // the user does not see strange artifacts during rotation. The screenshot
602 // used for rotation has already the border. After the rotation is complete
603 // we will show the border.
604 if (isMagnifyingLocked()) {
605 setMagnifiedRegionBorderShownLocked(false, false);
606 final long delay = (long) (mLongAnimationDuration
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700607 * mWindowManagerService.getWindowAnimationScaleLocked());
Svetoslav8e3feb12014-02-24 13:46:47 -0800608 Message message = mHandler.obtainMessage(
609 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED);
610 mHandler.sendMessageDelayed(message, delay);
611 }
612 recomputeBoundsLocked();
613 mWindow.updateSize();
614 }
615
616 public void setMagnifiedRegionBorderShownLocked(boolean shown, boolean animate) {
617 if (shown) {
618 mFullRedrawNeeded = true;
Phil Weaver70439242016-03-10 15:15:49 -0800619 mOldMagnificationRegion.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800620 }
621 mWindow.setShown(shown, animate);
622 }
623
624 public void getMagnifiedFrameInContentCoordsLocked(Rect rect) {
625 MagnificationSpec spec = mMagnificationSpec;
Phil Weaver70439242016-03-10 15:15:49 -0800626 mMagnificationRegion.getBounds(rect);
Svetoslav8e3feb12014-02-24 13:46:47 -0800627 rect.offset((int) -spec.offsetX, (int) -spec.offsetY);
628 rect.scale(1.0f / spec.scale);
629 }
630
631 public boolean isMagnifyingLocked() {
632 return mMagnificationSpec.scale > 1.0f;
633 }
634
635 public MagnificationSpec getMagnificationSpecLocked() {
636 return mMagnificationSpec;
637 }
638
639 /** NOTE: This has to be called within a surface transaction. */
640 public void drawWindowIfNeededLocked() {
641 recomputeBoundsLocked();
642 mWindow.drawIfNeeded();
643 }
644
645 public void destroyWindow() {
646 mWindow.releaseSurface();
647 }
648
649 private void populateWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
650 DisplayContent displayContent = mWindowManagerService
651 .getDefaultDisplayContentLocked();
652 WindowList windowList = displayContent.getWindowList();
653 final int windowCount = windowList.size();
654 for (int i = 0; i < windowCount; i++) {
655 WindowState windowState = windowList.get(i);
Craig Mautner165be0c2015-01-27 15:16:58 -0800656 if (windowState.isOnScreen() &&
657 !windowState.mWinAnimator.mEnterAnimationPending) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800658 outWindows.put(windowState.mLayer, windowState);
659 }
660 }
661 }
662
663 private final class ViewportWindow {
664 private static final String SURFACE_TITLE = "Magnification Overlay";
665
Svetoslav8e3feb12014-02-24 13:46:47 -0800666 private final Region mBounds = new Region();
667 private final Rect mDirtyRect = new Rect();
668 private final Paint mPaint = new Paint();
669
Svetoslav8e3feb12014-02-24 13:46:47 -0800670 private final SurfaceControl mSurfaceControl;
671 private final Surface mSurface = new Surface();
672
Svet Ganovb21df802014-09-01 19:06:33 -0700673 private final AnimationController mAnimationController;
674
Svetoslav8e3feb12014-02-24 13:46:47 -0800675 private boolean mShown;
676 private int mAlpha;
677
678 private boolean mInvalidated;
679
680 public ViewportWindow(Context context) {
681 SurfaceControl surfaceControl = null;
682 try {
683 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
684 surfaceControl = new SurfaceControl(mWindowManagerService.mFxSession,
685 SURFACE_TITLE, mTempPoint.x, mTempPoint.y, PixelFormat.TRANSLUCENT,
686 SurfaceControl.HIDDEN);
687 } catch (OutOfResourcesException oore) {
688 /* ignore */
689 }
690 mSurfaceControl = surfaceControl;
691 mSurfaceControl.setLayerStack(mWindowManager.getDefaultDisplay()
692 .getLayerStack());
693 mSurfaceControl.setLayer(mWindowManagerService.mPolicy.windowTypeToLayerLw(
694 WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY)
695 * WindowManagerService.TYPE_LAYER_MULTIPLIER);
696 mSurfaceControl.setPosition(0, 0);
697 mSurface.copyFrom(mSurfaceControl);
698
Svet Ganovb21df802014-09-01 19:06:33 -0700699 mAnimationController = new AnimationController(context,
700 mWindowManagerService.mH.getLooper());
701
Svetoslav8e3feb12014-02-24 13:46:47 -0800702 TypedValue typedValue = new TypedValue();
703 context.getTheme().resolveAttribute(R.attr.colorActivatedHighlight,
704 typedValue, true);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700705 final int borderColor = context.getColor(typedValue.resourceId);
Svetoslav8e3feb12014-02-24 13:46:47 -0800706
707 mPaint.setStyle(Paint.Style.STROKE);
708 mPaint.setStrokeWidth(mBorderWidth);
709 mPaint.setColor(borderColor);
710
Svetoslav8e3feb12014-02-24 13:46:47 -0800711 mInvalidated = true;
712 }
713
714 public void setShown(boolean shown, boolean animate) {
715 synchronized (mWindowManagerService.mWindowMap) {
716 if (mShown == shown) {
717 return;
718 }
719 mShown = shown;
Svet Ganovb21df802014-09-01 19:06:33 -0700720 mAnimationController.onFrameShownStateChanged(shown, animate);
Svetoslav8e3feb12014-02-24 13:46:47 -0800721 if (DEBUG_VIEWPORT_WINDOW) {
722 Slog.i(LOG_TAG, "ViewportWindow shown: " + mShown);
723 }
724 }
725 }
726
727 @SuppressWarnings("unused")
728 // Called reflectively from an animator.
729 public int getAlpha() {
730 synchronized (mWindowManagerService.mWindowMap) {
731 return mAlpha;
732 }
733 }
734
735 public void setAlpha(int alpha) {
736 synchronized (mWindowManagerService.mWindowMap) {
737 if (mAlpha == alpha) {
738 return;
739 }
740 mAlpha = alpha;
741 invalidate(null);
742 if (DEBUG_VIEWPORT_WINDOW) {
743 Slog.i(LOG_TAG, "ViewportWindow set alpha: " + alpha);
744 }
745 }
746 }
747
748 public void setBounds(Region bounds) {
749 synchronized (mWindowManagerService.mWindowMap) {
750 if (mBounds.equals(bounds)) {
751 return;
752 }
753 mBounds.set(bounds);
754 invalidate(mDirtyRect);
755 if (DEBUG_VIEWPORT_WINDOW) {
756 Slog.i(LOG_TAG, "ViewportWindow set bounds: " + bounds);
757 }
758 }
759 }
760
761 public void updateSize() {
762 synchronized (mWindowManagerService.mWindowMap) {
763 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
764 mSurfaceControl.setSize(mTempPoint.x, mTempPoint.y);
765 invalidate(mDirtyRect);
766 }
767 }
768
769 public void invalidate(Rect dirtyRect) {
770 if (dirtyRect != null) {
771 mDirtyRect.set(dirtyRect);
772 } else {
773 mDirtyRect.setEmpty();
774 }
775 mInvalidated = true;
776 mWindowManagerService.scheduleAnimationLocked();
777 }
778
779 /** NOTE: This has to be called within a surface transaction. */
780 public void drawIfNeeded() {
781 synchronized (mWindowManagerService.mWindowMap) {
782 if (!mInvalidated) {
783 return;
784 }
785 mInvalidated = false;
786 Canvas canvas = null;
787 try {
788 // Empty dirty rectangle means unspecified.
789 if (mDirtyRect.isEmpty()) {
790 mBounds.getBounds(mDirtyRect);
791 }
792 mDirtyRect.inset(- mHalfBorderWidth, - mHalfBorderWidth);
793 canvas = mSurface.lockCanvas(mDirtyRect);
794 if (DEBUG_VIEWPORT_WINDOW) {
795 Slog.i(LOG_TAG, "Dirty rect: " + mDirtyRect);
796 }
797 } catch (IllegalArgumentException iae) {
798 /* ignore */
799 } catch (Surface.OutOfResourcesException oore) {
800 /* ignore */
801 }
802 if (canvas == null) {
803 return;
804 }
805 if (DEBUG_VIEWPORT_WINDOW) {
806 Slog.i(LOG_TAG, "Bounds: " + mBounds);
807 }
808 canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
809 mPaint.setAlpha(mAlpha);
810 Path path = mBounds.getBoundaryPath();
811 canvas.drawPath(path, mPaint);
812
813 mSurface.unlockCanvasAndPost(canvas);
814
815 if (mAlpha > 0) {
816 mSurfaceControl.show();
817 } else {
818 mSurfaceControl.hide();
819 }
820 }
821 }
822
823 public void releaseSurface() {
824 mSurfaceControl.release();
825 mSurface.release();
826 }
Svet Ganovb21df802014-09-01 19:06:33 -0700827
828 private final class AnimationController extends Handler {
829 private static final String PROPERTY_NAME_ALPHA = "alpha";
830
831 private static final int MIN_ALPHA = 0;
832 private static final int MAX_ALPHA = 255;
833
834 private static final int MSG_FRAME_SHOWN_STATE_CHANGED = 1;
835
836 private final ValueAnimator mShowHideFrameAnimator;
837
838 public AnimationController(Context context, Looper looper) {
839 super(looper);
840 mShowHideFrameAnimator = ObjectAnimator.ofInt(ViewportWindow.this,
841 PROPERTY_NAME_ALPHA, MIN_ALPHA, MAX_ALPHA);
842
843 Interpolator interpolator = new DecelerateInterpolator(2.5f);
844 final long longAnimationDuration = context.getResources().getInteger(
845 com.android.internal.R.integer.config_longAnimTime);
846
847 mShowHideFrameAnimator.setInterpolator(interpolator);
848 mShowHideFrameAnimator.setDuration(longAnimationDuration);
849 }
850
851 public void onFrameShownStateChanged(boolean shown, boolean animate) {
852 obtainMessage(MSG_FRAME_SHOWN_STATE_CHANGED,
853 shown ? 1 : 0, animate ? 1 : 0).sendToTarget();
854 }
855
856 @Override
857 public void handleMessage(Message message) {
858 switch (message.what) {
859 case MSG_FRAME_SHOWN_STATE_CHANGED: {
860 final boolean shown = message.arg1 == 1;
861 final boolean animate = message.arg2 == 1;
862
863 if (animate) {
864 if (mShowHideFrameAnimator.isRunning()) {
865 mShowHideFrameAnimator.reverse();
866 } else {
867 if (shown) {
868 mShowHideFrameAnimator.start();
869 } else {
870 mShowHideFrameAnimator.reverse();
871 }
872 }
873 } else {
874 mShowHideFrameAnimator.cancel();
875 if (shown) {
876 setAlpha(MAX_ALPHA);
877 } else {
878 setAlpha(MIN_ALPHA);
879 }
880 }
881 } break;
882 }
883 }
884 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800885 }
886 }
887
888 private class MyHandler extends Handler {
Phil Weaver70439242016-03-10 15:15:49 -0800889 public static final int MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -0800890 public static final int MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED = 2;
891 public static final int MESSAGE_NOTIFY_USER_CONTEXT_CHANGED = 3;
892 public static final int MESSAGE_NOTIFY_ROTATION_CHANGED = 4;
893 public static final int MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED = 5;
894
895 public MyHandler(Looper looper) {
896 super(looper);
897 }
898
899 @Override
900 public void handleMessage(Message message) {
901 switch (message.what) {
Phil Weaver70439242016-03-10 15:15:49 -0800902 case MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED: {
Alan Viverette214fb682015-11-17 09:47:11 -0500903 final SomeArgs args = (SomeArgs) message.obj;
904 final Region magnifiedBounds = (Region) args.arg1;
Phil Weaver70439242016-03-10 15:15:49 -0800905 mCallbacks.onMagnificationRegionChanged(magnifiedBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500906 magnifiedBounds.recycle();
Svetoslav8e3feb12014-02-24 13:46:47 -0800907 } break;
908
909 case MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED: {
910 SomeArgs args = (SomeArgs) message.obj;
911 final int left = args.argi1;
912 final int top = args.argi2;
913 final int right = args.argi3;
914 final int bottom = args.argi4;
915 mCallbacks.onRectangleOnScreenRequested(left, top, right, bottom);
916 args.recycle();
917 } break;
918
919 case MESSAGE_NOTIFY_USER_CONTEXT_CHANGED: {
920 mCallbacks.onUserContextChanged();
921 } break;
922
923 case MESSAGE_NOTIFY_ROTATION_CHANGED: {
924 final int rotation = message.arg1;
925 mCallbacks.onRotationChanged(rotation);
926 } break;
927
928 case MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED : {
929 synchronized (mWindowManagerService.mWindowMap) {
930 if (mMagnifedViewport.isMagnifyingLocked()) {
931 mMagnifedViewport.setMagnifiedRegionBorderShownLocked(true, true);
932 mWindowManagerService.scheduleAnimationLocked();
933 }
934 }
935 } break;
936 }
937 }
938 }
939 }
940
941 /**
942 * This class encapsulates the functionality related to computing the windows
943 * reported for accessibility purposes. These windows are all windows a sighted
944 * user can see on the screen.
945 */
946 private static final class WindowsForAccessibilityObserver {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800947 private static final String LOG_TAG = TAG_WITH_CLASS_NAME ?
948 "WindowsForAccessibilityObserver" : TAG_WM;
Svetoslav8e3feb12014-02-24 13:46:47 -0800949
950 private static final boolean DEBUG = false;
951
952 private final SparseArray<WindowState> mTempWindowStates =
953 new SparseArray<WindowState>();
954
955 private final List<WindowInfo> mOldWindows = new ArrayList<WindowInfo>();
956
957 private final Set<IBinder> mTempBinderSet = new ArraySet<IBinder>();
958
959 private final RectF mTempRectF = new RectF();
960
961 private final Matrix mTempMatrix = new Matrix();
962
963 private final Point mTempPoint = new Point();
964
965 private final Rect mTempRect = new Rect();
966
967 private final Region mTempRegion = new Region();
968
969 private final Region mTempRegion1 = new Region();
970
971 private final Context mContext;
972
973 private final WindowManagerService mWindowManagerService;
974
975 private final Handler mHandler;
976
977 private final WindowsForAccessibilityCallback mCallback;
978
Svetoslavf7174e82014-06-12 11:29:35 -0700979 private final long mRecurringAccessibilityEventsIntervalMillis;
980
Svetoslav8e3feb12014-02-24 13:46:47 -0800981 public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
982 WindowsForAccessibilityCallback callback) {
983 mContext = windowManagerService.mContext;
984 mWindowManagerService = windowManagerService;
985 mCallback = callback;
986 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
Svetoslavf7174e82014-06-12 11:29:35 -0700987 mRecurringAccessibilityEventsIntervalMillis = ViewConfiguration
988 .getSendRecurringAccessibilityEventsInterval();
Svetoslav8e3feb12014-02-24 13:46:47 -0800989 computeChangedWindows();
990 }
991
Svetoslav3a0d8782014-12-04 12:50:11 -0800992 public void performComputeChangedWindowsNotLocked() {
993 mHandler.removeMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS);
994 computeChangedWindows();
995 }
996
Svetoslavf7174e82014-06-12 11:29:35 -0700997 public void scheduleComputeChangedWindowsLocked() {
Svetoslav3a0d8782014-12-04 12:50:11 -0800998 if (!mHandler.hasMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS)) {
Svetoslavf7174e82014-06-12 11:29:35 -0700999 mHandler.sendEmptyMessageDelayed(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS,
1000 mRecurringAccessibilityEventsIntervalMillis);
1001 }
1002 }
1003
Svetoslav8e3feb12014-02-24 13:46:47 -08001004 public void computeChangedWindows() {
1005 if (DEBUG) {
1006 Slog.i(LOG_TAG, "computeChangedWindows()");
1007 }
1008
Svetoslav3a0d8782014-12-04 12:50:11 -08001009 boolean windowsChanged = false;
1010 List<WindowInfo> windows = new ArrayList<WindowInfo>();
1011
Svetoslav8e3feb12014-02-24 13:46:47 -08001012 synchronized (mWindowManagerService.mWindowMap) {
Svetoslavf7174e82014-06-12 11:29:35 -07001013 // Do not send the windows if there is no current focus as
1014 // the window manager is still looking for where to put it.
1015 // We will do the work when we get a focus change callback.
1016 if (mWindowManagerService.mCurrentFocus == null) {
1017 return;
1018 }
1019
Svetoslav8e3feb12014-02-24 13:46:47 -08001020 WindowManager windowManager = (WindowManager)
1021 mContext.getSystemService(Context.WINDOW_SERVICE);
1022 windowManager.getDefaultDisplay().getRealSize(mTempPoint);
1023 final int screenWidth = mTempPoint.x;
1024 final int screenHeight = mTempPoint.y;
1025
1026 Region unaccountedSpace = mTempRegion;
1027 unaccountedSpace.set(0, 0, screenWidth, screenHeight);
1028
1029 SparseArray<WindowState> visibleWindows = mTempWindowStates;
1030 populateVisibleWindowsOnScreenLocked(visibleWindows);
1031
Svetoslav8e3feb12014-02-24 13:46:47 -08001032 Set<IBinder> addedWindows = mTempBinderSet;
1033 addedWindows.clear();
1034
Svetoslavf7174e82014-06-12 11:29:35 -07001035 boolean focusedWindowAdded = false;
1036
Svetoslav8e3feb12014-02-24 13:46:47 -08001037 final int visibleWindowCount = visibleWindows.size();
Allen Hairf20ac2c2016-02-11 17:42:59 -08001038 int skipRemainingWindowsForTaskId = -1;
1039 HashSet<Integer> skipRemainingWindowsForTasks = new HashSet<>();
Svetoslav8e3feb12014-02-24 13:46:47 -08001040 for (int i = visibleWindowCount - 1; i >= 0; i--) {
Alan Viverette9538eea2014-11-13 14:49:20 -08001041 final WindowState windowState = visibleWindows.valueAt(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001042 final int flags = windowState.mAttrs.flags;
Allen Hairf20ac2c2016-02-11 17:42:59 -08001043 final Task task = windowState.getTask();
1044
1045 // If the window is part of a task that we're finished with - ignore.
1046 if (task != null && skipRemainingWindowsForTasks.contains(task.mTaskId)) {
1047 continue;
1048 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001049
Svetoslav3a5c7212014-10-14 09:54:26 -07001050 // If the window is not touchable - ignore.
Svetoslavf7174e82014-06-12 11:29:35 -07001051 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001052 continue;
1053 }
1054
Alan Viverette9538eea2014-11-13 14:49:20 -08001055 // Compute the bounds in the screen.
1056 final Rect boundsInScreen = mTempRect;
1057 computeWindowBoundsInScreen(windowState, boundsInScreen);
1058
Svetoslav8e3feb12014-02-24 13:46:47 -08001059 // If the window is completely covered by other windows - ignore.
1060 if (unaccountedSpace.quickReject(boundsInScreen)) {
1061 continue;
1062 }
1063
1064 // Add windows of certain types not covered by modal windows.
1065 if (isReportedWindowType(windowState.mAttrs.type)) {
1066 // Add the window to the ones to be reported.
Svetoslavf7174e82014-06-12 11:29:35 -07001067 WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
Svetoslav8e3feb12014-02-24 13:46:47 -08001068 addedWindows.add(window.token);
Svetoslav8e3feb12014-02-24 13:46:47 -08001069 windows.add(window);
Svetoslavf7174e82014-06-12 11:29:35 -07001070 if (windowState.isFocused()) {
1071 focusedWindowAdded = true;
1072 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001073 }
1074
Alan Viveretted0c73f42014-11-18 10:25:04 -08001075 // Account for the space this window takes if the window
1076 // is not an accessibility overlay which does not change
1077 // the reported windows.
1078 if (windowState.mAttrs.type !=
1079 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY) {
1080 unaccountedSpace.op(boundsInScreen, unaccountedSpace,
1081 Region.Op.REVERSE_DIFFERENCE);
1082 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001083
1084 // We figured out what is touchable for the entire screen - done.
1085 if (unaccountedSpace.isEmpty()) {
1086 break;
1087 }
1088
Allen Hairf20ac2c2016-02-11 17:42:59 -08001089 // If a window is modal it prevents other windows from being touched
Svetoslav8e3feb12014-02-24 13:46:47 -08001090 if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1091 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
Allen Hairf20ac2c2016-02-11 17:42:59 -08001092 if (task != null) {
1093 // If the window is associated with a particular task, we can skip the
1094 // rest of the windows for that task.
1095 skipRemainingWindowsForTasks.add(task.mTaskId);
1096 continue;
1097 } else {
1098 // If the window is not associated with a particular task, then it is
1099 // globally modal. In this case we can skip all remaining windows.
1100 break;
1101 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001102 }
1103 }
1104
Svetoslavf7174e82014-06-12 11:29:35 -07001105 // Always report the focused window.
1106 if (!focusedWindowAdded) {
1107 for (int i = visibleWindowCount - 1; i >= 0; i--) {
1108 WindowState windowState = visibleWindows.valueAt(i);
1109 if (windowState.isFocused()) {
1110 // Compute the bounds in the screen.
1111 Rect boundsInScreen = mTempRect;
1112 computeWindowBoundsInScreen(windowState, boundsInScreen);
1113
1114 // Add the window to the ones to be reported.
1115 WindowInfo window = obtainPopulatedWindowInfo(windowState,
1116 boundsInScreen);
1117 addedWindows.add(window.token);
1118 windows.add(window);
1119 break;
1120 }
1121 }
1122 }
1123
Svetoslav8e3feb12014-02-24 13:46:47 -08001124 // Remove child/parent references to windows that were not added.
1125 final int windowCount = windows.size();
1126 for (int i = 0; i < windowCount; i++) {
1127 WindowInfo window = windows.get(i);
1128 if (!addedWindows.contains(window.parentToken)) {
1129 window.parentToken = null;
1130 }
1131 if (window.childTokens != null) {
1132 final int childTokenCount = window.childTokens.size();
1133 for (int j = childTokenCount - 1; j >= 0; j--) {
1134 if (!addedWindows.contains(window.childTokens.get(j))) {
1135 window.childTokens.remove(j);
1136 }
1137 }
1138 // Leave the child token list if empty.
1139 }
1140 }
1141
1142 visibleWindows.clear();
1143 addedWindows.clear();
1144
1145 // We computed the windows and if they changed notify the client.
Svetoslav8e3feb12014-02-24 13:46:47 -08001146 if (mOldWindows.size() != windows.size()) {
1147 // Different size means something changed.
1148 windowsChanged = true;
1149 } else if (!mOldWindows.isEmpty() || !windows.isEmpty()) {
1150 // Since we always traverse windows from high to low layer
1151 // the old and new windows at the same index should be the
1152 // same, otherwise something changed.
1153 for (int i = 0; i < windowCount; i++) {
1154 WindowInfo oldWindow = mOldWindows.get(i);
1155 WindowInfo newWindow = windows.get(i);
1156 // We do not care for layer changes given the window
1157 // order does not change. This brings no new information
1158 // to the clients.
1159 if (windowChangedNoLayer(oldWindow, newWindow)) {
1160 windowsChanged = true;
1161 break;
1162 }
1163 }
1164 }
1165
1166 if (windowsChanged) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001167 cacheWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001168 }
1169 }
Svetoslav3a0d8782014-12-04 12:50:11 -08001170
1171 // Now we do not hold the lock, so send the windows over.
1172 if (windowsChanged) {
1173 if (DEBUG) {
1174 Log.i(LOG_TAG, "Windows changed:" + windows);
1175 }
1176 mCallback.onWindowsForAccessibilityChanged(windows);
1177 } else {
1178 if (DEBUG) {
1179 Log.i(LOG_TAG, "No windows changed.");
1180 }
1181 }
1182
1183 // Recycle the windows as we do not need them.
1184 clearAndRecycleWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001185 }
1186
Svetoslavf7174e82014-06-12 11:29:35 -07001187 private void computeWindowBoundsInScreen(WindowState windowState, Rect outBounds) {
1188 // Get the touchable frame.
1189 Region touchableRegion = mTempRegion1;
1190 windowState.getTouchableRegion(touchableRegion);
1191 Rect touchableFrame = mTempRect;
1192 touchableRegion.getBounds(touchableFrame);
1193
1194 // Move to origin as all transforms are captured by the matrix.
1195 RectF windowFrame = mTempRectF;
1196 windowFrame.set(touchableFrame);
1197 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
1198
1199 // Map the frame to get what appears on the screen.
1200 Matrix matrix = mTempMatrix;
1201 populateTransformationMatrixLocked(windowState, matrix);
1202 matrix.mapRect(windowFrame);
1203
1204 // Got the bounds.
1205 outBounds.set((int) windowFrame.left, (int) windowFrame.top,
1206 (int) windowFrame.right, (int) windowFrame.bottom);
1207 }
1208
Wale Ogunwaleadde52e2016-07-16 13:11:55 -07001209 private static WindowInfo obtainPopulatedWindowInfo(
1210 WindowState windowState, Rect boundsInScreen) {
1211 final WindowInfo window = windowState.getWindowInfo();
Svetoslavf7174e82014-06-12 11:29:35 -07001212 window.boundsInScreen.set(boundsInScreen);
Svetoslavf7174e82014-06-12 11:29:35 -07001213 return window;
1214 }
1215
Svetoslav8e3feb12014-02-24 13:46:47 -08001216 private void cacheWindows(List<WindowInfo> windows) {
1217 final int oldWindowCount = mOldWindows.size();
1218 for (int i = oldWindowCount - 1; i >= 0; i--) {
1219 mOldWindows.remove(i).recycle();
1220 }
1221 final int newWindowCount = windows.size();
1222 for (int i = 0; i < newWindowCount; i++) {
1223 WindowInfo newWindow = windows.get(i);
1224 mOldWindows.add(WindowInfo.obtain(newWindow));
1225 }
1226 }
1227
1228 private boolean windowChangedNoLayer(WindowInfo oldWindow, WindowInfo newWindow) {
1229 if (oldWindow == newWindow) {
1230 return false;
1231 }
Svetoslavf7174e82014-06-12 11:29:35 -07001232 if (oldWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001233 return true;
1234 }
Svetoslavf7174e82014-06-12 11:29:35 -07001235 if (newWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001236 return true;
1237 }
1238 if (oldWindow.type != newWindow.type) {
1239 return true;
1240 }
1241 if (oldWindow.focused != newWindow.focused) {
1242 return true;
1243 }
1244 if (oldWindow.token == null) {
1245 if (newWindow.token != null) {
1246 return true;
1247 }
1248 } else if (!oldWindow.token.equals(newWindow.token)) {
1249 return true;
1250 }
1251 if (oldWindow.parentToken == null) {
1252 if (newWindow.parentToken != null) {
1253 return true;
1254 }
1255 } else if (!oldWindow.parentToken.equals(newWindow.parentToken)) {
1256 return true;
1257 }
1258 if (!oldWindow.boundsInScreen.equals(newWindow.boundsInScreen)) {
1259 return true;
1260 }
1261 if (oldWindow.childTokens != null && newWindow.childTokens != null
1262 && !oldWindow.childTokens.equals(newWindow.childTokens)) {
1263 return true;
1264 }
Phil Weaver396d5492016-03-22 17:53:50 -07001265 if (!TextUtils.equals(oldWindow.title, newWindow.title)) {
1266 return true;
1267 }
1268 if (oldWindow.accessibilityIdOfAnchor != newWindow.accessibilityIdOfAnchor) {
1269 return true;
1270 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001271 return false;
1272 }
1273
Svetoslav3a0d8782014-12-04 12:50:11 -08001274 private static void clearAndRecycleWindows(List<WindowInfo> windows) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001275 final int windowCount = windows.size();
1276 for (int i = windowCount - 1; i >= 0; i--) {
1277 windows.remove(i).recycle();
1278 }
1279 }
1280
1281 private static boolean isReportedWindowType(int windowType) {
1282 return (windowType != WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM
1283 && windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
1284 && 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) {
1296 DisplayContent displayContent = mWindowManagerService
1297 .getDefaultDisplayContentLocked();
1298 WindowList windowList = displayContent.getWindowList();
1299 final int windowCount = windowList.size();
1300 for (int i = 0; i < windowCount; i++) {
1301 WindowState windowState = windowList.get(i);
1302 if (windowState.isVisibleLw()) {
1303 outWindows.put(windowState.mLayer, windowState);
1304 }
1305 }
1306 }
1307
1308 private class MyHandler extends Handler {
Svetoslavf7174e82014-06-12 11:29:35 -07001309 public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -08001310
1311 public MyHandler(Looper looper) {
1312 super(looper, null, false);
1313 }
1314
1315 @Override
1316 @SuppressWarnings("unchecked")
1317 public void handleMessage(Message message) {
1318 switch (message.what) {
Svetoslavf7174e82014-06-12 11:29:35 -07001319 case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1320 computeChangedWindows();
1321 } break;
Svetoslav8e3feb12014-02-24 13:46:47 -08001322 }
1323 }
1324 }
1325 }
1326}