blob: bde65edcb7a3fcb68f5fffe8cf63c0307836e76d [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:
372 case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
373 case WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG:
374 case WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG:
375 case WindowManager.LayoutParams.TYPE_SYSTEM_ERROR:
376 case WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY:
Jason Monk8f7f3182015-11-18 16:35:14 -0500377 case WindowManager.LayoutParams.TYPE_QS_DIALOG:
Adrian Roos9a645132014-10-08 02:59:56 +0200378 case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL: {
Svetoslav8e3feb12014-02-24 13:46:47 -0800379 Rect magnifiedRegionBounds = mTempRect2;
380 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(
381 magnifiedRegionBounds);
382 Rect touchableRegionBounds = mTempRect1;
383 windowState.getTouchableRegion(mTempRegion1);
384 mTempRegion1.getBounds(touchableRegionBounds);
385 if (!magnifiedRegionBounds.intersect(touchableRegionBounds)) {
386 mCallbacks.onRectangleOnScreenRequested(
387 touchableRegionBounds.left,
388 touchableRegionBounds.top,
389 touchableRegionBounds.right,
390 touchableRegionBounds.bottom);
391 }
392 } break;
393 } break;
394 }
395 }
396 }
397
398 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
399 MagnificationSpec spec = mMagnifedViewport.getMagnificationSpecLocked();
400 if (spec != null && !spec.isNop()) {
401 WindowManagerPolicy policy = mWindowManagerService.mPolicy;
402 final int windowType = windowState.mAttrs.type;
Wale Ogunwale7ed4d372016-07-09 15:28:55 -0700403 if (!policy.isTopLevelWindow(windowType) && windowState.isChildWindow()
Svetoslav8e3feb12014-02-24 13:46:47 -0800404 && !policy.canMagnifyWindow(windowType)) {
405 return null;
406 }
407 if (!policy.canMagnifyWindow(windowState.mAttrs.type)) {
408 return null;
409 }
410 }
411 return spec;
412 }
413
Phil Weaver70439242016-03-10 15:15:49 -0800414 public void getMagnificationRegionLocked(Region outMagnificationRegion) {
415 mMagnifedViewport.getMagnificationRegionLocked(outMagnificationRegion);
Alan Viverette59e53a12016-03-28 13:41:32 -0400416 }
417
Svetoslav8e3feb12014-02-24 13:46:47 -0800418 public void destroyLocked() {
419 mMagnifedViewport.destroyWindow();
420 }
421
422 /** NOTE: This has to be called within a surface transaction. */
423 public void drawMagnifiedRegionBorderIfNeededLocked() {
424 mMagnifedViewport.drawWindowIfNeededLocked();
425 }
426
427 private final class MagnifiedViewport {
428
Svetoslav8e3feb12014-02-24 13:46:47 -0800429 private final SparseArray<WindowState> mTempWindowStates =
430 new SparseArray<WindowState>();
431
432 private final RectF mTempRectF = new RectF();
433
434 private final Point mTempPoint = new Point();
435
436 private final Matrix mTempMatrix = new Matrix();
437
Phil Weaver70439242016-03-10 15:15:49 -0800438 private final Region mMagnificationRegion = new Region();
439 private final Region mOldMagnificationRegion = new Region();
Svetoslav8e3feb12014-02-24 13:46:47 -0800440
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800441 private final Path mCircularPath;
442
Svetoslav8e3feb12014-02-24 13:46:47 -0800443 private final MagnificationSpec mMagnificationSpec = MagnificationSpec.obtain();
444
445 private final WindowManager mWindowManager;
446
Svetoslav7505e332014-08-22 12:14:28 -0700447 private final float mBorderWidth;
Svetoslav8e3feb12014-02-24 13:46:47 -0800448 private final int mHalfBorderWidth;
Svetoslav7505e332014-08-22 12:14:28 -0700449 private final int mDrawBorderInset;
Svetoslav8e3feb12014-02-24 13:46:47 -0800450
451 private final ViewportWindow mWindow;
452
453 private boolean mFullRedrawNeeded;
454
455 public MagnifiedViewport() {
456 mWindowManager = (WindowManager) mContext.getSystemService(Service.WINDOW_SERVICE);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800457 mBorderWidth = mContext.getResources().getDimension(
458 com.android.internal.R.dimen.accessibility_magnification_indicator_width);
Svetoslav7505e332014-08-22 12:14:28 -0700459 mHalfBorderWidth = (int) Math.ceil(mBorderWidth / 2);
460 mDrawBorderInset = (int) mBorderWidth / 2;
Svetoslav8e3feb12014-02-24 13:46:47 -0800461 mWindow = new ViewportWindow(mContext);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800462
Adam Powell01f280d2015-05-18 16:07:42 -0700463 if (mContext.getResources().getConfiguration().isScreenRound()) {
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800464 mCircularPath = new Path();
465 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
466 final int centerXY = mTempPoint.x / 2;
467 mCircularPath.addCircle(centerXY, centerXY, centerXY, Path.Direction.CW);
468 } else {
469 mCircularPath = null;
470 }
471
Svetoslav8e3feb12014-02-24 13:46:47 -0800472 recomputeBoundsLocked();
473 }
474
Phil Weaver70439242016-03-10 15:15:49 -0800475 public void getMagnificationRegionLocked(@NonNull Region outMagnificationRegion) {
476 outMagnificationRegion.set(mMagnificationRegion);
Alan Viverette59e53a12016-03-28 13:41:32 -0400477 }
478
Svetoslav8e3feb12014-02-24 13:46:47 -0800479 public void updateMagnificationSpecLocked(MagnificationSpec spec) {
480 if (spec != null) {
481 mMagnificationSpec.initialize(spec.scale, spec.offsetX, spec.offsetY);
482 } else {
483 mMagnificationSpec.clear();
484 }
485 // If this message is pending we are in a rotation animation and do not want
486 // to show the border. We will do so when the pending message is handled.
Svetoslav7505e332014-08-22 12:14:28 -0700487 if (!mHandler.hasMessages(
488 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED)) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800489 setMagnifiedRegionBorderShownLocked(isMagnifyingLocked(), true);
490 }
491 }
492
493 public void recomputeBoundsLocked() {
494 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
495 final int screenWidth = mTempPoint.x;
496 final int screenHeight = mTempPoint.y;
497
Phil Weaver70439242016-03-10 15:15:49 -0800498 mMagnificationRegion.set(0, 0, 0, 0);
499 final Region availableBounds = mTempRegion1;
500 availableBounds.set(0, 0, screenWidth, screenHeight);
Svetoslav8e3feb12014-02-24 13:46:47 -0800501
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800502 if (mCircularPath != null) {
Phil Weaver70439242016-03-10 15:15:49 -0800503 availableBounds.setPath(mCircularPath, availableBounds);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800504 }
505
Svetoslav8e3feb12014-02-24 13:46:47 -0800506 Region nonMagnifiedBounds = mTempRegion4;
Svet Ganovb21df802014-09-01 19:06:33 -0700507 nonMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800508
509 SparseArray<WindowState> visibleWindows = mTempWindowStates;
510 visibleWindows.clear();
511 populateWindowsOnScreenLocked(visibleWindows);
512
513 final int visibleWindowCount = visibleWindows.size();
514 for (int i = visibleWindowCount - 1; i >= 0; i--) {
515 WindowState windowState = visibleWindows.valueAt(i);
516 if (windowState.mAttrs.type == WindowManager
517 .LayoutParams.TYPE_MAGNIFICATION_OVERLAY) {
518 continue;
519 }
520
Phil Weaver65c06702016-03-15 15:33:46 -0700521 // Consider the touchable portion of the window
Svetoslav8e3feb12014-02-24 13:46:47 -0800522 Matrix matrix = mTempMatrix;
523 populateTransformationMatrixLocked(windowState, matrix);
Phil Weaver65c06702016-03-15 15:33:46 -0700524 Region touchableRegion = mTempRegion3;
525 windowState.getTouchableRegion(touchableRegion);
526 Rect touchableFrame = mTempRect1;
527 touchableRegion.getBounds(touchableFrame);
Svetoslav8e3feb12014-02-24 13:46:47 -0800528 RectF windowFrame = mTempRectF;
Phil Weaver65c06702016-03-15 15:33:46 -0700529 windowFrame.set(touchableFrame);
530 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
531 matrix.mapRect(windowFrame);
532 Region windowBounds = mTempRegion2;
533 windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
534 (int) windowFrame.right, (int) windowFrame.bottom);
535 // Only update new regions
536 Region portionOfWindowAlreadyAccountedFor = mTempRegion3;
Phil Weaver70439242016-03-10 15:15:49 -0800537 portionOfWindowAlreadyAccountedFor.set(mMagnificationRegion);
Phil Weaver65c06702016-03-15 15:33:46 -0700538 portionOfWindowAlreadyAccountedFor.op(nonMagnifiedBounds, Region.Op.UNION);
539 windowBounds.op(portionOfWindowAlreadyAccountedFor, Region.Op.DIFFERENCE);
Svetoslav8e3feb12014-02-24 13:46:47 -0800540
541 if (mWindowManagerService.mPolicy.canMagnifyWindow(windowState.mAttrs.type)) {
Phil Weaver70439242016-03-10 15:15:49 -0800542 mMagnificationRegion.op(windowBounds, Region.Op.UNION);
543 mMagnificationRegion.op(availableBounds, Region.Op.INTERSECT);
Svetoslav8e3feb12014-02-24 13:46:47 -0800544 } else {
Svetoslav8e3feb12014-02-24 13:46:47 -0800545 nonMagnifiedBounds.op(windowBounds, Region.Op.UNION);
Phil Weaver70439242016-03-10 15:15:49 -0800546 availableBounds.op(windowBounds, Region.Op.DIFFERENCE);
Svetoslav8e3feb12014-02-24 13:46:47 -0800547 }
548
Phil Weaver65c06702016-03-15 15:33:46 -0700549 // Update accounted bounds
Svetoslav8e3feb12014-02-24 13:46:47 -0800550 Region accountedBounds = mTempRegion2;
Phil Weaver70439242016-03-10 15:15:49 -0800551 accountedBounds.set(mMagnificationRegion);
Svetoslav8e3feb12014-02-24 13:46:47 -0800552 accountedBounds.op(nonMagnifiedBounds, Region.Op.UNION);
553 accountedBounds.op(0, 0, screenWidth, screenHeight, Region.Op.INTERSECT);
554
555 if (accountedBounds.isRect()) {
556 Rect accountedFrame = mTempRect1;
557 accountedBounds.getBounds(accountedFrame);
558 if (accountedFrame.width() == screenWidth
559 && accountedFrame.height() == screenHeight) {
560 break;
561 }
562 }
563 }
564
565 visibleWindows.clear();
566
Phil Weaver70439242016-03-10 15:15:49 -0800567 mMagnificationRegion.op(mDrawBorderInset, mDrawBorderInset,
Svetoslav7505e332014-08-22 12:14:28 -0700568 screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset,
Svetoslav8e3feb12014-02-24 13:46:47 -0800569 Region.Op.INTERSECT);
570
Phil Weaver70439242016-03-10 15:15:49 -0800571 final boolean magnifiedChanged =
572 !mOldMagnificationRegion.equals(mMagnificationRegion);
573 if (magnifiedChanged) {
574 mWindow.setBounds(mMagnificationRegion);
575 final Rect dirtyRect = mTempRect1;
576 if (mFullRedrawNeeded) {
577 mFullRedrawNeeded = false;
578 dirtyRect.set(mDrawBorderInset, mDrawBorderInset,
579 screenWidth - mDrawBorderInset,
580 screenHeight - mDrawBorderInset);
581 mWindow.invalidate(dirtyRect);
582 } else {
583 final Region dirtyRegion = mTempRegion3;
584 dirtyRegion.set(mMagnificationRegion);
585 dirtyRegion.op(mOldMagnificationRegion, Region.Op.UNION);
586 dirtyRegion.op(nonMagnifiedBounds, Region.Op.INTERSECT);
587 dirtyRegion.getBounds(dirtyRect);
588 mWindow.invalidate(dirtyRect);
Svetoslav8e3feb12014-02-24 13:46:47 -0800589 }
590
Phil Weaver70439242016-03-10 15:15:49 -0800591 mOldMagnificationRegion.set(mMagnificationRegion);
Alan Viverette214fb682015-11-17 09:47:11 -0500592 final SomeArgs args = SomeArgs.obtain();
Phil Weaver70439242016-03-10 15:15:49 -0800593 args.arg1 = Region.obtain(mMagnificationRegion);
Alan Viverette214fb682015-11-17 09:47:11 -0500594 mHandler.obtainMessage(
Phil Weaver70439242016-03-10 15:15:49 -0800595 MyHandler.MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED, args)
596 .sendToTarget();
Svetoslav8e3feb12014-02-24 13:46:47 -0800597 }
598 }
599
600 public void onRotationChangedLocked() {
601 // If we are magnifying, hide the magnified border window immediately so
602 // the user does not see strange artifacts during rotation. The screenshot
603 // used for rotation has already the border. After the rotation is complete
604 // we will show the border.
605 if (isMagnifyingLocked()) {
606 setMagnifiedRegionBorderShownLocked(false, false);
607 final long delay = (long) (mLongAnimationDuration
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700608 * mWindowManagerService.getWindowAnimationScaleLocked());
Svetoslav8e3feb12014-02-24 13:46:47 -0800609 Message message = mHandler.obtainMessage(
610 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED);
611 mHandler.sendMessageDelayed(message, delay);
612 }
613 recomputeBoundsLocked();
614 mWindow.updateSize();
615 }
616
617 public void setMagnifiedRegionBorderShownLocked(boolean shown, boolean animate) {
618 if (shown) {
619 mFullRedrawNeeded = true;
Phil Weaver70439242016-03-10 15:15:49 -0800620 mOldMagnificationRegion.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800621 }
622 mWindow.setShown(shown, animate);
623 }
624
625 public void getMagnifiedFrameInContentCoordsLocked(Rect rect) {
626 MagnificationSpec spec = mMagnificationSpec;
Phil Weaver70439242016-03-10 15:15:49 -0800627 mMagnificationRegion.getBounds(rect);
Svetoslav8e3feb12014-02-24 13:46:47 -0800628 rect.offset((int) -spec.offsetX, (int) -spec.offsetY);
629 rect.scale(1.0f / spec.scale);
630 }
631
632 public boolean isMagnifyingLocked() {
633 return mMagnificationSpec.scale > 1.0f;
634 }
635
636 public MagnificationSpec getMagnificationSpecLocked() {
637 return mMagnificationSpec;
638 }
639
640 /** NOTE: This has to be called within a surface transaction. */
641 public void drawWindowIfNeededLocked() {
642 recomputeBoundsLocked();
643 mWindow.drawIfNeeded();
644 }
645
646 public void destroyWindow() {
647 mWindow.releaseSurface();
648 }
649
650 private void populateWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700651 final DisplayContent dc = mWindowManagerService.getDefaultDisplayContentLocked();
652 final ReadOnlyWindowList windowList = dc.getReadOnlyWindowList();
Svetoslav8e3feb12014-02-24 13:46:47 -0800653 final int windowCount = windowList.size();
654 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700655 final WindowState windowState = windowList.get(i);
Phil Weaver473af052016-09-30 18:11:39 -0700656 if (windowState.isOnScreen() && windowState.isVisibleLw() &&
Craig Mautner165be0c2015-01-27 15:16:58 -0800657 !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
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001029 final SparseArray<WindowState> visibleWindows = mTempWindowStates;
Svetoslav8e3feb12014-02-24 13:46:47 -08001030 populateVisibleWindowsOnScreenLocked(visibleWindows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001031 Set<IBinder> addedWindows = mTempBinderSet;
1032 addedWindows.clear();
1033
Svetoslavf7174e82014-06-12 11:29:35 -07001034 boolean focusedWindowAdded = false;
1035
Svetoslav8e3feb12014-02-24 13:46:47 -08001036 final int visibleWindowCount = visibleWindows.size();
Allen Hairf20ac2c2016-02-11 17:42:59 -08001037 int skipRemainingWindowsForTaskId = -1;
1038 HashSet<Integer> skipRemainingWindowsForTasks = new HashSet<>();
Svetoslav8e3feb12014-02-24 13:46:47 -08001039 for (int i = visibleWindowCount - 1; i >= 0; i--) {
Alan Viverette9538eea2014-11-13 14:49:20 -08001040 final WindowState windowState = visibleWindows.valueAt(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001041 final int flags = windowState.mAttrs.flags;
Allen Hairf20ac2c2016-02-11 17:42:59 -08001042 final Task task = windowState.getTask();
1043
1044 // If the window is part of a task that we're finished with - ignore.
1045 if (task != null && skipRemainingWindowsForTasks.contains(task.mTaskId)) {
1046 continue;
1047 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001048
Svetoslav3a5c7212014-10-14 09:54:26 -07001049 // If the window is not touchable - ignore.
Svetoslavf7174e82014-06-12 11:29:35 -07001050 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001051 continue;
1052 }
1053
Alan Viverette9538eea2014-11-13 14:49:20 -08001054 // Compute the bounds in the screen.
1055 final Rect boundsInScreen = mTempRect;
1056 computeWindowBoundsInScreen(windowState, boundsInScreen);
1057
Svetoslav8e3feb12014-02-24 13:46:47 -08001058 // If the window is completely covered by other windows - ignore.
1059 if (unaccountedSpace.quickReject(boundsInScreen)) {
1060 continue;
1061 }
1062
1063 // Add windows of certain types not covered by modal windows.
1064 if (isReportedWindowType(windowState.mAttrs.type)) {
1065 // Add the window to the ones to be reported.
Svetoslavf7174e82014-06-12 11:29:35 -07001066 WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
Svetoslav8e3feb12014-02-24 13:46:47 -08001067 addedWindows.add(window.token);
Svetoslav8e3feb12014-02-24 13:46:47 -08001068 windows.add(window);
Svetoslavf7174e82014-06-12 11:29:35 -07001069 if (windowState.isFocused()) {
1070 focusedWindowAdded = true;
1071 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001072 }
1073
Alan Viveretted0c73f42014-11-18 10:25:04 -08001074 // Account for the space this window takes if the window
1075 // is not an accessibility overlay which does not change
1076 // the reported windows.
1077 if (windowState.mAttrs.type !=
1078 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY) {
1079 unaccountedSpace.op(boundsInScreen, unaccountedSpace,
1080 Region.Op.REVERSE_DIFFERENCE);
1081 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001082
Allen Hairf20ac2c2016-02-11 17:42:59 -08001083 // If a window is modal it prevents other windows from being touched
Svetoslav8e3feb12014-02-24 13:46:47 -08001084 if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1085 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
Phil Weaverac9ad702016-07-27 18:03:10 -07001086 // Account for all space in the task, whether the windows in it are
1087 // touchable or not. The modal window blocks all touches from the task's
1088 // area.
1089 unaccountedSpace.op(windowState.getDisplayFrameLw(), unaccountedSpace,
1090 Region.Op.REVERSE_DIFFERENCE);
1091
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 }
Phil Weaverac9ad702016-07-27 18:03:10 -07001103 // We figured out what is touchable for the entire screen - done.
1104 if (unaccountedSpace.isEmpty()) {
1105 break;
1106 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001107 }
1108
Svetoslavf7174e82014-06-12 11:29:35 -07001109 // Always report the focused window.
1110 if (!focusedWindowAdded) {
1111 for (int i = visibleWindowCount - 1; i >= 0; i--) {
1112 WindowState windowState = visibleWindows.valueAt(i);
1113 if (windowState.isFocused()) {
1114 // Compute the bounds in the screen.
1115 Rect boundsInScreen = mTempRect;
1116 computeWindowBoundsInScreen(windowState, boundsInScreen);
1117
1118 // Add the window to the ones to be reported.
1119 WindowInfo window = obtainPopulatedWindowInfo(windowState,
1120 boundsInScreen);
1121 addedWindows.add(window.token);
1122 windows.add(window);
1123 break;
1124 }
1125 }
1126 }
1127
Svetoslav8e3feb12014-02-24 13:46:47 -08001128 // Remove child/parent references to windows that were not added.
1129 final int windowCount = windows.size();
1130 for (int i = 0; i < windowCount; i++) {
1131 WindowInfo window = windows.get(i);
1132 if (!addedWindows.contains(window.parentToken)) {
1133 window.parentToken = null;
1134 }
1135 if (window.childTokens != null) {
1136 final int childTokenCount = window.childTokens.size();
1137 for (int j = childTokenCount - 1; j >= 0; j--) {
1138 if (!addedWindows.contains(window.childTokens.get(j))) {
1139 window.childTokens.remove(j);
1140 }
1141 }
1142 // Leave the child token list if empty.
1143 }
1144 }
1145
1146 visibleWindows.clear();
1147 addedWindows.clear();
1148
1149 // We computed the windows and if they changed notify the client.
Svetoslav8e3feb12014-02-24 13:46:47 -08001150 if (mOldWindows.size() != windows.size()) {
1151 // Different size means something changed.
1152 windowsChanged = true;
1153 } else if (!mOldWindows.isEmpty() || !windows.isEmpty()) {
1154 // Since we always traverse windows from high to low layer
1155 // the old and new windows at the same index should be the
1156 // same, otherwise something changed.
1157 for (int i = 0; i < windowCount; i++) {
1158 WindowInfo oldWindow = mOldWindows.get(i);
1159 WindowInfo newWindow = windows.get(i);
1160 // We do not care for layer changes given the window
1161 // order does not change. This brings no new information
1162 // to the clients.
1163 if (windowChangedNoLayer(oldWindow, newWindow)) {
1164 windowsChanged = true;
1165 break;
1166 }
1167 }
1168 }
1169
1170 if (windowsChanged) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001171 cacheWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001172 }
1173 }
Svetoslav3a0d8782014-12-04 12:50:11 -08001174
1175 // Now we do not hold the lock, so send the windows over.
1176 if (windowsChanged) {
1177 if (DEBUG) {
1178 Log.i(LOG_TAG, "Windows changed:" + windows);
1179 }
1180 mCallback.onWindowsForAccessibilityChanged(windows);
1181 } else {
1182 if (DEBUG) {
1183 Log.i(LOG_TAG, "No windows changed.");
1184 }
1185 }
1186
1187 // Recycle the windows as we do not need them.
1188 clearAndRecycleWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001189 }
1190
Svetoslavf7174e82014-06-12 11:29:35 -07001191 private void computeWindowBoundsInScreen(WindowState windowState, Rect outBounds) {
1192 // Get the touchable frame.
1193 Region touchableRegion = mTempRegion1;
1194 windowState.getTouchableRegion(touchableRegion);
1195 Rect touchableFrame = mTempRect;
1196 touchableRegion.getBounds(touchableFrame);
1197
1198 // Move to origin as all transforms are captured by the matrix.
1199 RectF windowFrame = mTempRectF;
1200 windowFrame.set(touchableFrame);
1201 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
1202
1203 // Map the frame to get what appears on the screen.
1204 Matrix matrix = mTempMatrix;
1205 populateTransformationMatrixLocked(windowState, matrix);
1206 matrix.mapRect(windowFrame);
1207
1208 // Got the bounds.
1209 outBounds.set((int) windowFrame.left, (int) windowFrame.top,
1210 (int) windowFrame.right, (int) windowFrame.bottom);
1211 }
1212
Wale Ogunwaleadde52e2016-07-16 13:11:55 -07001213 private static WindowInfo obtainPopulatedWindowInfo(
1214 WindowState windowState, Rect boundsInScreen) {
1215 final WindowInfo window = windowState.getWindowInfo();
Svetoslavf7174e82014-06-12 11:29:35 -07001216 window.boundsInScreen.set(boundsInScreen);
Svetoslavf7174e82014-06-12 11:29:35 -07001217 return window;
1218 }
1219
Svetoslav8e3feb12014-02-24 13:46:47 -08001220 private void cacheWindows(List<WindowInfo> windows) {
1221 final int oldWindowCount = mOldWindows.size();
1222 for (int i = oldWindowCount - 1; i >= 0; i--) {
1223 mOldWindows.remove(i).recycle();
1224 }
1225 final int newWindowCount = windows.size();
1226 for (int i = 0; i < newWindowCount; i++) {
1227 WindowInfo newWindow = windows.get(i);
1228 mOldWindows.add(WindowInfo.obtain(newWindow));
1229 }
1230 }
1231
1232 private boolean windowChangedNoLayer(WindowInfo oldWindow, WindowInfo newWindow) {
1233 if (oldWindow == newWindow) {
1234 return false;
1235 }
Svetoslavf7174e82014-06-12 11:29:35 -07001236 if (oldWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001237 return true;
1238 }
Svetoslavf7174e82014-06-12 11:29:35 -07001239 if (newWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001240 return true;
1241 }
1242 if (oldWindow.type != newWindow.type) {
1243 return true;
1244 }
1245 if (oldWindow.focused != newWindow.focused) {
1246 return true;
1247 }
1248 if (oldWindow.token == null) {
1249 if (newWindow.token != null) {
1250 return true;
1251 }
1252 } else if (!oldWindow.token.equals(newWindow.token)) {
1253 return true;
1254 }
1255 if (oldWindow.parentToken == null) {
1256 if (newWindow.parentToken != null) {
1257 return true;
1258 }
1259 } else if (!oldWindow.parentToken.equals(newWindow.parentToken)) {
1260 return true;
1261 }
1262 if (!oldWindow.boundsInScreen.equals(newWindow.boundsInScreen)) {
1263 return true;
1264 }
1265 if (oldWindow.childTokens != null && newWindow.childTokens != null
1266 && !oldWindow.childTokens.equals(newWindow.childTokens)) {
1267 return true;
1268 }
Phil Weaver396d5492016-03-22 17:53:50 -07001269 if (!TextUtils.equals(oldWindow.title, newWindow.title)) {
1270 return true;
1271 }
1272 if (oldWindow.accessibilityIdOfAnchor != newWindow.accessibilityIdOfAnchor) {
1273 return true;
1274 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001275 return false;
1276 }
1277
Svetoslav3a0d8782014-12-04 12:50:11 -08001278 private static void clearAndRecycleWindows(List<WindowInfo> windows) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001279 final int windowCount = windows.size();
1280 for (int i = windowCount - 1; i >= 0; i--) {
1281 windows.remove(i).recycle();
1282 }
1283 }
1284
1285 private static boolean isReportedWindowType(int windowType) {
1286 return (windowType != WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM
1287 && windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
1288 && windowType != WindowManager.LayoutParams.TYPE_BOOT_PROGRESS
1289 && windowType != WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY
1290 && windowType != WindowManager.LayoutParams.TYPE_DRAG
Selim Cinekf83e8242015-05-19 18:08:14 -07001291 && windowType != WindowManager.LayoutParams.TYPE_INPUT_CONSUMER
Svetoslav8e3feb12014-02-24 13:46:47 -08001292 && windowType != WindowManager.LayoutParams.TYPE_POINTER
Svetoslav8e3feb12014-02-24 13:46:47 -08001293 && windowType != WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY
1294 && windowType != WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY
1295 && windowType != WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY
1296 && windowType != WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION);
1297 }
1298
1299 private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001300 final DisplayContent dc = mWindowManagerService.getDefaultDisplayContentLocked();
1301 final ReadOnlyWindowList windowList = dc.getReadOnlyWindowList();
Svetoslav8e3feb12014-02-24 13:46:47 -08001302 final int windowCount = windowList.size();
1303 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001304 final WindowState windowState = windowList.get(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001305 if (windowState.isVisibleLw()) {
1306 outWindows.put(windowState.mLayer, windowState);
1307 }
1308 }
1309 }
1310
1311 private class MyHandler extends Handler {
Svetoslavf7174e82014-06-12 11:29:35 -07001312 public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -08001313
1314 public MyHandler(Looper looper) {
1315 super(looper, null, false);
1316 }
1317
1318 @Override
1319 @SuppressWarnings("unchecked")
1320 public void handleMessage(Message message) {
1321 switch (message.what) {
Svetoslavf7174e82014-06-12 11:29:35 -07001322 case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1323 computeChangedWindows();
1324 } break;
Svetoslav8e3feb12014-02-24 13:46:47 -08001325 }
1326 }
1327 }
1328 }
1329}