blob: 49ffa22b53e3b1d0ad535271fcd4ea58ce2c710e [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();
Wale Ogunwaled1880962016-11-08 10:31:59 -0800652 dc.forAllWindows((w) -> {
653 if (w.isOnScreen() && w.isVisibleLw()
654 && !w.mWinAnimator.mEnterAnimationPending) {
655 outWindows.put(w.mLayer, w);
Svetoslav8e3feb12014-02-24 13:46:47 -0800656 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800657 }, false /* traverseTopToBottom */ );
Svetoslav8e3feb12014-02-24 13:46:47 -0800658 }
659
660 private final class ViewportWindow {
661 private static final String SURFACE_TITLE = "Magnification Overlay";
662
Svetoslav8e3feb12014-02-24 13:46:47 -0800663 private final Region mBounds = new Region();
664 private final Rect mDirtyRect = new Rect();
665 private final Paint mPaint = new Paint();
666
Svetoslav8e3feb12014-02-24 13:46:47 -0800667 private final SurfaceControl mSurfaceControl;
668 private final Surface mSurface = new Surface();
669
Svet Ganovb21df802014-09-01 19:06:33 -0700670 private final AnimationController mAnimationController;
671
Svetoslav8e3feb12014-02-24 13:46:47 -0800672 private boolean mShown;
673 private int mAlpha;
674
675 private boolean mInvalidated;
676
677 public ViewportWindow(Context context) {
678 SurfaceControl surfaceControl = null;
679 try {
680 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
681 surfaceControl = new SurfaceControl(mWindowManagerService.mFxSession,
682 SURFACE_TITLE, mTempPoint.x, mTempPoint.y, PixelFormat.TRANSLUCENT,
683 SurfaceControl.HIDDEN);
684 } catch (OutOfResourcesException oore) {
685 /* ignore */
686 }
687 mSurfaceControl = surfaceControl;
688 mSurfaceControl.setLayerStack(mWindowManager.getDefaultDisplay()
689 .getLayerStack());
690 mSurfaceControl.setLayer(mWindowManagerService.mPolicy.windowTypeToLayerLw(
691 WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY)
692 * WindowManagerService.TYPE_LAYER_MULTIPLIER);
693 mSurfaceControl.setPosition(0, 0);
694 mSurface.copyFrom(mSurfaceControl);
695
Svet Ganovb21df802014-09-01 19:06:33 -0700696 mAnimationController = new AnimationController(context,
697 mWindowManagerService.mH.getLooper());
698
Svetoslav8e3feb12014-02-24 13:46:47 -0800699 TypedValue typedValue = new TypedValue();
700 context.getTheme().resolveAttribute(R.attr.colorActivatedHighlight,
701 typedValue, true);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700702 final int borderColor = context.getColor(typedValue.resourceId);
Svetoslav8e3feb12014-02-24 13:46:47 -0800703
704 mPaint.setStyle(Paint.Style.STROKE);
705 mPaint.setStrokeWidth(mBorderWidth);
706 mPaint.setColor(borderColor);
707
Svetoslav8e3feb12014-02-24 13:46:47 -0800708 mInvalidated = true;
709 }
710
711 public void setShown(boolean shown, boolean animate) {
712 synchronized (mWindowManagerService.mWindowMap) {
713 if (mShown == shown) {
714 return;
715 }
716 mShown = shown;
Svet Ganovb21df802014-09-01 19:06:33 -0700717 mAnimationController.onFrameShownStateChanged(shown, animate);
Svetoslav8e3feb12014-02-24 13:46:47 -0800718 if (DEBUG_VIEWPORT_WINDOW) {
719 Slog.i(LOG_TAG, "ViewportWindow shown: " + mShown);
720 }
721 }
722 }
723
724 @SuppressWarnings("unused")
725 // Called reflectively from an animator.
726 public int getAlpha() {
727 synchronized (mWindowManagerService.mWindowMap) {
728 return mAlpha;
729 }
730 }
731
732 public void setAlpha(int alpha) {
733 synchronized (mWindowManagerService.mWindowMap) {
734 if (mAlpha == alpha) {
735 return;
736 }
737 mAlpha = alpha;
738 invalidate(null);
739 if (DEBUG_VIEWPORT_WINDOW) {
740 Slog.i(LOG_TAG, "ViewportWindow set alpha: " + alpha);
741 }
742 }
743 }
744
745 public void setBounds(Region bounds) {
746 synchronized (mWindowManagerService.mWindowMap) {
747 if (mBounds.equals(bounds)) {
748 return;
749 }
750 mBounds.set(bounds);
751 invalidate(mDirtyRect);
752 if (DEBUG_VIEWPORT_WINDOW) {
753 Slog.i(LOG_TAG, "ViewportWindow set bounds: " + bounds);
754 }
755 }
756 }
757
758 public void updateSize() {
759 synchronized (mWindowManagerService.mWindowMap) {
760 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
761 mSurfaceControl.setSize(mTempPoint.x, mTempPoint.y);
762 invalidate(mDirtyRect);
763 }
764 }
765
766 public void invalidate(Rect dirtyRect) {
767 if (dirtyRect != null) {
768 mDirtyRect.set(dirtyRect);
769 } else {
770 mDirtyRect.setEmpty();
771 }
772 mInvalidated = true;
773 mWindowManagerService.scheduleAnimationLocked();
774 }
775
776 /** NOTE: This has to be called within a surface transaction. */
777 public void drawIfNeeded() {
778 synchronized (mWindowManagerService.mWindowMap) {
779 if (!mInvalidated) {
780 return;
781 }
782 mInvalidated = false;
783 Canvas canvas = null;
784 try {
785 // Empty dirty rectangle means unspecified.
786 if (mDirtyRect.isEmpty()) {
787 mBounds.getBounds(mDirtyRect);
788 }
789 mDirtyRect.inset(- mHalfBorderWidth, - mHalfBorderWidth);
790 canvas = mSurface.lockCanvas(mDirtyRect);
791 if (DEBUG_VIEWPORT_WINDOW) {
792 Slog.i(LOG_TAG, "Dirty rect: " + mDirtyRect);
793 }
794 } catch (IllegalArgumentException iae) {
795 /* ignore */
796 } catch (Surface.OutOfResourcesException oore) {
797 /* ignore */
798 }
799 if (canvas == null) {
800 return;
801 }
802 if (DEBUG_VIEWPORT_WINDOW) {
803 Slog.i(LOG_TAG, "Bounds: " + mBounds);
804 }
805 canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
806 mPaint.setAlpha(mAlpha);
807 Path path = mBounds.getBoundaryPath();
808 canvas.drawPath(path, mPaint);
809
810 mSurface.unlockCanvasAndPost(canvas);
811
812 if (mAlpha > 0) {
813 mSurfaceControl.show();
814 } else {
815 mSurfaceControl.hide();
816 }
817 }
818 }
819
820 public void releaseSurface() {
821 mSurfaceControl.release();
822 mSurface.release();
823 }
Svet Ganovb21df802014-09-01 19:06:33 -0700824
825 private final class AnimationController extends Handler {
826 private static final String PROPERTY_NAME_ALPHA = "alpha";
827
828 private static final int MIN_ALPHA = 0;
829 private static final int MAX_ALPHA = 255;
830
831 private static final int MSG_FRAME_SHOWN_STATE_CHANGED = 1;
832
833 private final ValueAnimator mShowHideFrameAnimator;
834
835 public AnimationController(Context context, Looper looper) {
836 super(looper);
837 mShowHideFrameAnimator = ObjectAnimator.ofInt(ViewportWindow.this,
838 PROPERTY_NAME_ALPHA, MIN_ALPHA, MAX_ALPHA);
839
840 Interpolator interpolator = new DecelerateInterpolator(2.5f);
841 final long longAnimationDuration = context.getResources().getInteger(
842 com.android.internal.R.integer.config_longAnimTime);
843
844 mShowHideFrameAnimator.setInterpolator(interpolator);
845 mShowHideFrameAnimator.setDuration(longAnimationDuration);
846 }
847
848 public void onFrameShownStateChanged(boolean shown, boolean animate) {
849 obtainMessage(MSG_FRAME_SHOWN_STATE_CHANGED,
850 shown ? 1 : 0, animate ? 1 : 0).sendToTarget();
851 }
852
853 @Override
854 public void handleMessage(Message message) {
855 switch (message.what) {
856 case MSG_FRAME_SHOWN_STATE_CHANGED: {
857 final boolean shown = message.arg1 == 1;
858 final boolean animate = message.arg2 == 1;
859
860 if (animate) {
861 if (mShowHideFrameAnimator.isRunning()) {
862 mShowHideFrameAnimator.reverse();
863 } else {
864 if (shown) {
865 mShowHideFrameAnimator.start();
866 } else {
867 mShowHideFrameAnimator.reverse();
868 }
869 }
870 } else {
871 mShowHideFrameAnimator.cancel();
872 if (shown) {
873 setAlpha(MAX_ALPHA);
874 } else {
875 setAlpha(MIN_ALPHA);
876 }
877 }
878 } break;
879 }
880 }
881 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800882 }
883 }
884
885 private class MyHandler extends Handler {
Phil Weaver70439242016-03-10 15:15:49 -0800886 public static final int MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -0800887 public static final int MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED = 2;
888 public static final int MESSAGE_NOTIFY_USER_CONTEXT_CHANGED = 3;
889 public static final int MESSAGE_NOTIFY_ROTATION_CHANGED = 4;
890 public static final int MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED = 5;
891
892 public MyHandler(Looper looper) {
893 super(looper);
894 }
895
896 @Override
897 public void handleMessage(Message message) {
898 switch (message.what) {
Phil Weaver70439242016-03-10 15:15:49 -0800899 case MESSAGE_NOTIFY_MAGNIFICATION_REGION_CHANGED: {
Alan Viverette214fb682015-11-17 09:47:11 -0500900 final SomeArgs args = (SomeArgs) message.obj;
901 final Region magnifiedBounds = (Region) args.arg1;
Phil Weaver70439242016-03-10 15:15:49 -0800902 mCallbacks.onMagnificationRegionChanged(magnifiedBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500903 magnifiedBounds.recycle();
Svetoslav8e3feb12014-02-24 13:46:47 -0800904 } break;
905
906 case MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED: {
907 SomeArgs args = (SomeArgs) message.obj;
908 final int left = args.argi1;
909 final int top = args.argi2;
910 final int right = args.argi3;
911 final int bottom = args.argi4;
912 mCallbacks.onRectangleOnScreenRequested(left, top, right, bottom);
913 args.recycle();
914 } break;
915
916 case MESSAGE_NOTIFY_USER_CONTEXT_CHANGED: {
917 mCallbacks.onUserContextChanged();
918 } break;
919
920 case MESSAGE_NOTIFY_ROTATION_CHANGED: {
921 final int rotation = message.arg1;
922 mCallbacks.onRotationChanged(rotation);
923 } break;
924
925 case MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED : {
926 synchronized (mWindowManagerService.mWindowMap) {
927 if (mMagnifedViewport.isMagnifyingLocked()) {
928 mMagnifedViewport.setMagnifiedRegionBorderShownLocked(true, true);
929 mWindowManagerService.scheduleAnimationLocked();
930 }
931 }
932 } break;
933 }
934 }
935 }
936 }
937
938 /**
939 * This class encapsulates the functionality related to computing the windows
940 * reported for accessibility purposes. These windows are all windows a sighted
941 * user can see on the screen.
942 */
943 private static final class WindowsForAccessibilityObserver {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800944 private static final String LOG_TAG = TAG_WITH_CLASS_NAME ?
945 "WindowsForAccessibilityObserver" : TAG_WM;
Svetoslav8e3feb12014-02-24 13:46:47 -0800946
947 private static final boolean DEBUG = false;
948
949 private final SparseArray<WindowState> mTempWindowStates =
950 new SparseArray<WindowState>();
951
952 private final List<WindowInfo> mOldWindows = new ArrayList<WindowInfo>();
953
954 private final Set<IBinder> mTempBinderSet = new ArraySet<IBinder>();
955
956 private final RectF mTempRectF = new RectF();
957
958 private final Matrix mTempMatrix = new Matrix();
959
960 private final Point mTempPoint = new Point();
961
962 private final Rect mTempRect = new Rect();
963
964 private final Region mTempRegion = new Region();
965
966 private final Region mTempRegion1 = new Region();
967
968 private final Context mContext;
969
970 private final WindowManagerService mWindowManagerService;
971
972 private final Handler mHandler;
973
974 private final WindowsForAccessibilityCallback mCallback;
975
Svetoslavf7174e82014-06-12 11:29:35 -0700976 private final long mRecurringAccessibilityEventsIntervalMillis;
977
Svetoslav8e3feb12014-02-24 13:46:47 -0800978 public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
979 WindowsForAccessibilityCallback callback) {
980 mContext = windowManagerService.mContext;
981 mWindowManagerService = windowManagerService;
982 mCallback = callback;
983 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
Svetoslavf7174e82014-06-12 11:29:35 -0700984 mRecurringAccessibilityEventsIntervalMillis = ViewConfiguration
985 .getSendRecurringAccessibilityEventsInterval();
Svetoslav8e3feb12014-02-24 13:46:47 -0800986 computeChangedWindows();
987 }
988
Svetoslav3a0d8782014-12-04 12:50:11 -0800989 public void performComputeChangedWindowsNotLocked() {
990 mHandler.removeMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS);
991 computeChangedWindows();
992 }
993
Svetoslavf7174e82014-06-12 11:29:35 -0700994 public void scheduleComputeChangedWindowsLocked() {
Svetoslav3a0d8782014-12-04 12:50:11 -0800995 if (!mHandler.hasMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS)) {
Svetoslavf7174e82014-06-12 11:29:35 -0700996 mHandler.sendEmptyMessageDelayed(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS,
997 mRecurringAccessibilityEventsIntervalMillis);
998 }
999 }
1000
Svetoslav8e3feb12014-02-24 13:46:47 -08001001 public void computeChangedWindows() {
1002 if (DEBUG) {
1003 Slog.i(LOG_TAG, "computeChangedWindows()");
1004 }
1005
Svetoslav3a0d8782014-12-04 12:50:11 -08001006 boolean windowsChanged = false;
1007 List<WindowInfo> windows = new ArrayList<WindowInfo>();
1008
Svetoslav8e3feb12014-02-24 13:46:47 -08001009 synchronized (mWindowManagerService.mWindowMap) {
Svetoslavf7174e82014-06-12 11:29:35 -07001010 // Do not send the windows if there is no current focus as
1011 // the window manager is still looking for where to put it.
1012 // We will do the work when we get a focus change callback.
1013 if (mWindowManagerService.mCurrentFocus == null) {
1014 return;
1015 }
1016
Svetoslav8e3feb12014-02-24 13:46:47 -08001017 WindowManager windowManager = (WindowManager)
1018 mContext.getSystemService(Context.WINDOW_SERVICE);
1019 windowManager.getDefaultDisplay().getRealSize(mTempPoint);
1020 final int screenWidth = mTempPoint.x;
1021 final int screenHeight = mTempPoint.y;
1022
1023 Region unaccountedSpace = mTempRegion;
1024 unaccountedSpace.set(0, 0, screenWidth, screenHeight);
1025
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001026 final SparseArray<WindowState> visibleWindows = mTempWindowStates;
Svetoslav8e3feb12014-02-24 13:46:47 -08001027 populateVisibleWindowsOnScreenLocked(visibleWindows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001028 Set<IBinder> addedWindows = mTempBinderSet;
1029 addedWindows.clear();
1030
Svetoslavf7174e82014-06-12 11:29:35 -07001031 boolean focusedWindowAdded = false;
1032
Svetoslav8e3feb12014-02-24 13:46:47 -08001033 final int visibleWindowCount = visibleWindows.size();
Allen Hairf20ac2c2016-02-11 17:42:59 -08001034 HashSet<Integer> skipRemainingWindowsForTasks = new HashSet<>();
Svetoslav8e3feb12014-02-24 13:46:47 -08001035 for (int i = visibleWindowCount - 1; i >= 0; i--) {
Alan Viverette9538eea2014-11-13 14:49:20 -08001036 final WindowState windowState = visibleWindows.valueAt(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001037 final int flags = windowState.mAttrs.flags;
Allen Hairf20ac2c2016-02-11 17:42:59 -08001038 final Task task = windowState.getTask();
1039
1040 // If the window is part of a task that we're finished with - ignore.
1041 if (task != null && skipRemainingWindowsForTasks.contains(task.mTaskId)) {
1042 continue;
1043 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001044
Svetoslav3a5c7212014-10-14 09:54:26 -07001045 // If the window is not touchable - ignore.
Svetoslavf7174e82014-06-12 11:29:35 -07001046 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001047 continue;
1048 }
1049
Alan Viverette9538eea2014-11-13 14:49:20 -08001050 // Compute the bounds in the screen.
1051 final Rect boundsInScreen = mTempRect;
1052 computeWindowBoundsInScreen(windowState, boundsInScreen);
1053
Svetoslav8e3feb12014-02-24 13:46:47 -08001054 // If the window is completely covered by other windows - ignore.
1055 if (unaccountedSpace.quickReject(boundsInScreen)) {
1056 continue;
1057 }
1058
1059 // Add windows of certain types not covered by modal windows.
1060 if (isReportedWindowType(windowState.mAttrs.type)) {
1061 // Add the window to the ones to be reported.
Svetoslavf7174e82014-06-12 11:29:35 -07001062 WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
Svetoslav8e3feb12014-02-24 13:46:47 -08001063 addedWindows.add(window.token);
Svetoslav8e3feb12014-02-24 13:46:47 -08001064 windows.add(window);
Svetoslavf7174e82014-06-12 11:29:35 -07001065 if (windowState.isFocused()) {
1066 focusedWindowAdded = true;
1067 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001068 }
1069
Alan Viveretted0c73f42014-11-18 10:25:04 -08001070 // Account for the space this window takes if the window
1071 // is not an accessibility overlay which does not change
1072 // the reported windows.
1073 if (windowState.mAttrs.type !=
1074 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY) {
1075 unaccountedSpace.op(boundsInScreen, unaccountedSpace,
1076 Region.Op.REVERSE_DIFFERENCE);
1077 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001078
Allen Hairf20ac2c2016-02-11 17:42:59 -08001079 // If a window is modal it prevents other windows from being touched
Svetoslav8e3feb12014-02-24 13:46:47 -08001080 if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1081 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
Phil Weaverac9ad702016-07-27 18:03:10 -07001082 // Account for all space in the task, whether the windows in it are
1083 // touchable or not. The modal window blocks all touches from the task's
1084 // area.
1085 unaccountedSpace.op(windowState.getDisplayFrameLw(), unaccountedSpace,
1086 Region.Op.REVERSE_DIFFERENCE);
1087
Allen Hairf20ac2c2016-02-11 17:42:59 -08001088 if (task != null) {
1089 // If the window is associated with a particular task, we can skip the
1090 // rest of the windows for that task.
1091 skipRemainingWindowsForTasks.add(task.mTaskId);
1092 continue;
1093 } else {
1094 // If the window is not associated with a particular task, then it is
1095 // globally modal. In this case we can skip all remaining windows.
1096 break;
1097 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001098 }
Phil Weaverac9ad702016-07-27 18:03:10 -07001099 // We figured out what is touchable for the entire screen - done.
1100 if (unaccountedSpace.isEmpty()) {
1101 break;
1102 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001103 }
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) {
Jorim Jaggi73294b62016-10-26 18:02:36 -07001282 return (windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
Svetoslav8e3feb12014-02-24 13:46:47 -08001283 && windowType != WindowManager.LayoutParams.TYPE_BOOT_PROGRESS
1284 && windowType != WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY
1285 && windowType != WindowManager.LayoutParams.TYPE_DRAG
Selim Cinekf83e8242015-05-19 18:08:14 -07001286 && windowType != WindowManager.LayoutParams.TYPE_INPUT_CONSUMER
Svetoslav8e3feb12014-02-24 13:46:47 -08001287 && windowType != WindowManager.LayoutParams.TYPE_POINTER
Svetoslav8e3feb12014-02-24 13:46:47 -08001288 && windowType != WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY
1289 && windowType != WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY
1290 && windowType != WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY
1291 && windowType != WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION);
1292 }
1293
1294 private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001295 final DisplayContent dc = mWindowManagerService.getDefaultDisplayContentLocked();
Wale Ogunwaled1880962016-11-08 10:31:59 -08001296 dc.forAllWindows((w) -> {
1297 if (w.isVisibleLw()) {
1298 outWindows.put(w.mLayer, w);
Svetoslav8e3feb12014-02-24 13:46:47 -08001299 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001300 }, false /* traverseTopToBottom */ );
Svetoslav8e3feb12014-02-24 13:46:47 -08001301 }
1302
1303 private class MyHandler extends Handler {
Svetoslavf7174e82014-06-12 11:29:35 -07001304 public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -08001305
1306 public MyHandler(Looper looper) {
1307 super(looper, null, false);
1308 }
1309
1310 @Override
1311 @SuppressWarnings("unchecked")
1312 public void handleMessage(Message message) {
1313 switch (message.what) {
Svetoslavf7174e82014-06-12 11:29:35 -07001314 case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1315 computeChangedWindows();
1316 } break;
Svetoslav8e3feb12014-02-24 13:46:47 -08001317 }
1318 }
1319 }
1320 }
1321}