blob: c2466091bb9130777cf6326906f1d80608f4398e [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
19import android.animation.ObjectAnimator;
20import android.animation.ValueAnimator;
21import android.app.Service;
22import android.content.Context;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.Matrix;
26import android.graphics.Paint;
27import android.graphics.Path;
28import android.graphics.PixelFormat;
29import android.graphics.Point;
30import android.graphics.PorterDuff.Mode;
31import android.graphics.Rect;
32import android.graphics.RectF;
33import android.graphics.Region;
34import android.os.Handler;
35import android.os.IBinder;
36import android.os.Looper;
37import android.os.Message;
38import android.util.ArraySet;
39import android.util.Log;
40import android.util.Slog;
41import android.util.SparseArray;
42import android.util.TypedValue;
43import android.view.MagnificationSpec;
44import android.view.Surface;
45import android.view.Surface.OutOfResourcesException;
46import android.view.SurfaceControl;
Svetoslavf7174e82014-06-12 11:29:35 -070047import android.view.ViewConfiguration;
Svetoslav8e3feb12014-02-24 13:46:47 -080048import android.view.WindowInfo;
49import android.view.WindowManager;
50import android.view.WindowManagerInternal.MagnificationCallbacks;
51import android.view.WindowManagerInternal.WindowsForAccessibilityCallback;
52import android.view.WindowManagerPolicy;
53import android.view.animation.DecelerateInterpolator;
54import android.view.animation.Interpolator;
55
56import com.android.internal.R;
57import com.android.internal.os.SomeArgs;
58
59import java.util.ArrayList;
60import java.util.List;
61import java.util.Set;
62
63/**
64 * This class contains the accessibility related logic of the window manger.
65 */
66final class AccessibilityController {
67
68 private final WindowManagerService mWindowManagerService;
69
70 private static final float[] sTempFloats = new float[9];
71
72 public AccessibilityController(WindowManagerService service) {
73 mWindowManagerService = service;
74 }
75
76 private DisplayMagnifier mDisplayMagnifier;
77
78 private WindowsForAccessibilityObserver mWindowsForAccessibilityObserver;
79
80 public void setMagnificationCallbacksLocked(MagnificationCallbacks callbacks) {
81 if (callbacks != null) {
82 if (mDisplayMagnifier != null) {
83 throw new IllegalStateException("Magnification callbacks already set!");
84 }
85 mDisplayMagnifier = new DisplayMagnifier(mWindowManagerService, callbacks);
86 } else {
87 if (mDisplayMagnifier == null) {
88 throw new IllegalStateException("Magnification callbacks already cleared!");
89 }
90 mDisplayMagnifier.destroyLocked();
91 mDisplayMagnifier = null;
92 }
93 }
94
95 public void setWindowsForAccessibilityCallback(WindowsForAccessibilityCallback callback) {
96 if (callback != null) {
97 if (mWindowsForAccessibilityObserver != null) {
98 throw new IllegalStateException(
99 "Windows for accessibility callback already set!");
100 }
101 mWindowsForAccessibilityObserver = new WindowsForAccessibilityObserver(
102 mWindowManagerService, callback);
103 } else {
104 if (mWindowsForAccessibilityObserver == null) {
105 throw new IllegalStateException(
106 "Windows for accessibility callback already cleared!");
107 }
108 mWindowsForAccessibilityObserver = null;
109 }
110 }
111
112 public void setMagnificationSpecLocked(MagnificationSpec spec) {
113 if (mDisplayMagnifier != null) {
114 mDisplayMagnifier.setMagnificationSpecLocked(spec);
115 }
116 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700117 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800118 }
119 }
120
Svetoslavf7174e82014-06-12 11:29:35 -0700121 public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800122 if (mDisplayMagnifier != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700123 mDisplayMagnifier.onRectangleOnScreenRequestedLocked(rectangle);
Svetoslav8e3feb12014-02-24 13:46:47 -0800124 }
125 // Not relevant for the window observer.
126 }
127
128 public void onWindowLayersChangedLocked() {
129 if (mDisplayMagnifier != null) {
130 mDisplayMagnifier.onWindowLayersChangedLocked();
131 }
132 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700133 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800134 }
135 }
136
137 public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
138 if (mDisplayMagnifier != null) {
139 mDisplayMagnifier.onRotationChangedLocked(displayContent, rotation);
140 }
141 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700142 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800143 }
144 }
145
146 public void onAppWindowTransitionLocked(WindowState windowState, int transition) {
147 if (mDisplayMagnifier != null) {
148 mDisplayMagnifier.onAppWindowTransitionLocked(windowState, transition);
149 }
150 // Not relevant for the window observer.
151 }
152
153 public void onWindowTransitionLocked(WindowState windowState, int transition) {
154 if (mDisplayMagnifier != null) {
155 mDisplayMagnifier.onWindowTransitionLocked(windowState, transition);
156 }
157 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700158 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800159 }
160 }
161
Svetoslav3a0d8782014-12-04 12:50:11 -0800162 public void onWindowFocusChangedNotLocked() {
Svetoslav8e3feb12014-02-24 13:46:47 -0800163 // Not relevant for the display magnifier.
164
Svetoslav3a0d8782014-12-04 12:50:11 -0800165 WindowsForAccessibilityObserver observer = null;
166 synchronized (mWindowManagerService) {
167 observer = mWindowsForAccessibilityObserver;
168 }
169 if (observer != null) {
170 observer.performComputeChangedWindowsNotLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800171 }
172 }
173
Svetoslav4604abc2014-06-10 18:59:30 -0700174
Svetoslavf7174e82014-06-12 11:29:35 -0700175 public void onSomeWindowResizedOrMovedLocked() {
Svetoslav4604abc2014-06-10 18:59:30 -0700176 // Not relevant for the display magnifier.
177
178 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700179 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav4604abc2014-06-10 18:59:30 -0700180 }
181 }
182
Svetoslav8e3feb12014-02-24 13:46:47 -0800183 /** NOTE: This has to be called within a surface transaction. */
184 public void drawMagnifiedRegionBorderIfNeededLocked() {
185 if (mDisplayMagnifier != null) {
186 mDisplayMagnifier.drawMagnifiedRegionBorderIfNeededLocked();
187 }
188 // Not relevant for the window observer.
189 }
190
191 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
192 if (mDisplayMagnifier != null) {
193 return mDisplayMagnifier.getMagnificationSpecForWindowLocked(windowState);
194 }
195 return null;
196 }
197
198 public boolean hasCallbacksLocked() {
199 return (mDisplayMagnifier != null
200 || mWindowsForAccessibilityObserver != null);
201 }
202
203 private static void populateTransformationMatrixLocked(WindowState windowState,
204 Matrix outMatrix) {
205 sTempFloats[Matrix.MSCALE_X] = windowState.mWinAnimator.mDsDx;
206 sTempFloats[Matrix.MSKEW_Y] = windowState.mWinAnimator.mDtDx;
207 sTempFloats[Matrix.MSKEW_X] = windowState.mWinAnimator.mDsDy;
208 sTempFloats[Matrix.MSCALE_Y] = windowState.mWinAnimator.mDtDy;
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -0700209 sTempFloats[Matrix.MTRANS_X] = windowState.mShownPosition.x;
210 sTempFloats[Matrix.MTRANS_Y] = windowState.mShownPosition.y;
Svetoslav8e3feb12014-02-24 13:46:47 -0800211 sTempFloats[Matrix.MPERSP_0] = 0;
212 sTempFloats[Matrix.MPERSP_1] = 0;
213 sTempFloats[Matrix.MPERSP_2] = 1;
214 outMatrix.setValues(sTempFloats);
215 }
216
217 /**
218 * This class encapsulates the functionality related to display magnification.
219 */
220 private static final class DisplayMagnifier {
221
222 private static final String LOG_TAG = "DisplayMagnifier";
223
224 private static final boolean DEBUG_WINDOW_TRANSITIONS = false;
225 private static final boolean DEBUG_ROTATION = false;
226 private static final boolean DEBUG_LAYERS = false;
227 private static final boolean DEBUG_RECTANGLE_REQUESTED = false;
228 private static final boolean DEBUG_VIEWPORT_WINDOW = false;
229
230 private final Rect mTempRect1 = new Rect();
231 private final Rect mTempRect2 = new Rect();
232
233 private final Region mTempRegion1 = new Region();
234 private final Region mTempRegion2 = new Region();
235 private final Region mTempRegion3 = new Region();
236 private final Region mTempRegion4 = new Region();
237
238 private final Context mContext;
239 private final WindowManagerService mWindowManagerService;
240 private final MagnifiedViewport mMagnifedViewport;
241 private final Handler mHandler;
242
243 private final MagnificationCallbacks mCallbacks;
244
245 private final long mLongAnimationDuration;
246
247 public DisplayMagnifier(WindowManagerService windowManagerService,
248 MagnificationCallbacks callbacks) {
249 mContext = windowManagerService.mContext;
250 mWindowManagerService = windowManagerService;
251 mCallbacks = callbacks;
252 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
253 mMagnifedViewport = new MagnifiedViewport();
254 mLongAnimationDuration = mContext.getResources().getInteger(
255 com.android.internal.R.integer.config_longAnimTime);
256 }
257
258 public void setMagnificationSpecLocked(MagnificationSpec spec) {
259 mMagnifedViewport.updateMagnificationSpecLocked(spec);
260 mMagnifedViewport.recomputeBoundsLocked();
261 mWindowManagerService.scheduleAnimationLocked();
262 }
263
Svetoslavf7174e82014-06-12 11:29:35 -0700264 public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800265 if (DEBUG_RECTANGLE_REQUESTED) {
266 Slog.i(LOG_TAG, "Rectangle on screen requested: " + rectangle);
267 }
268 if (!mMagnifedViewport.isMagnifyingLocked()) {
269 return;
270 }
271 Rect magnifiedRegionBounds = mTempRect2;
272 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(magnifiedRegionBounds);
273 if (magnifiedRegionBounds.contains(rectangle)) {
274 return;
275 }
276 SomeArgs args = SomeArgs.obtain();
277 args.argi1 = rectangle.left;
278 args.argi2 = rectangle.top;
279 args.argi3 = rectangle.right;
280 args.argi4 = rectangle.bottom;
281 mHandler.obtainMessage(MyHandler.MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED,
282 args).sendToTarget();
283 }
284
285 public void onWindowLayersChangedLocked() {
286 if (DEBUG_LAYERS) {
287 Slog.i(LOG_TAG, "Layers changed.");
288 }
289 mMagnifedViewport.recomputeBoundsLocked();
290 mWindowManagerService.scheduleAnimationLocked();
291 }
292
293 public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
294 if (DEBUG_ROTATION) {
295 Slog.i(LOG_TAG, "Rotaton: " + Surface.rotationToString(rotation)
296 + " displayId: " + displayContent.getDisplayId());
297 }
298 mMagnifedViewport.onRotationChangedLocked();
299 mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_ROTATION_CHANGED);
300 }
301
302 public void onAppWindowTransitionLocked(WindowState windowState, int transition) {
303 if (DEBUG_WINDOW_TRANSITIONS) {
304 Slog.i(LOG_TAG, "Window transition: "
305 + AppTransition.appTransitionToString(transition)
306 + " displayId: " + windowState.getDisplayId());
307 }
308 final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
309 if (magnifying) {
310 switch (transition) {
311 case AppTransition.TRANSIT_ACTIVITY_OPEN:
312 case AppTransition.TRANSIT_TASK_OPEN:
313 case AppTransition.TRANSIT_TASK_TO_FRONT:
314 case AppTransition.TRANSIT_WALLPAPER_OPEN:
315 case AppTransition.TRANSIT_WALLPAPER_CLOSE:
316 case AppTransition.TRANSIT_WALLPAPER_INTRA_OPEN: {
317 mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_USER_CONTEXT_CHANGED);
318 }
319 }
320 }
321 }
322
323 public void onWindowTransitionLocked(WindowState windowState, int transition) {
324 if (DEBUG_WINDOW_TRANSITIONS) {
325 Slog.i(LOG_TAG, "Window transition: "
326 + AppTransition.appTransitionToString(transition)
327 + " displayId: " + windowState.getDisplayId());
328 }
329 final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
330 final int type = windowState.mAttrs.type;
331 switch (transition) {
332 case WindowManagerPolicy.TRANSIT_ENTER:
333 case WindowManagerPolicy.TRANSIT_SHOW: {
334 if (!magnifying) {
335 break;
336 }
337 switch (type) {
338 case WindowManager.LayoutParams.TYPE_APPLICATION:
339 case WindowManager.LayoutParams.TYPE_APPLICATION_PANEL:
340 case WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA:
341 case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
Wale Ogunwale0a4dc222015-04-14 12:58:42 -0700342 case WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL:
Svetoslav8e3feb12014-02-24 13:46:47 -0800343 case WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG:
344 case WindowManager.LayoutParams.TYPE_SEARCH_BAR:
345 case WindowManager.LayoutParams.TYPE_PHONE:
346 case WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
347 case WindowManager.LayoutParams.TYPE_TOAST:
348 case WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY:
349 case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
350 case WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG:
351 case WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG:
352 case WindowManager.LayoutParams.TYPE_SYSTEM_ERROR:
353 case WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY:
Adrian Roos9a645132014-10-08 02:59:56 +0200354 case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL: {
Svetoslav8e3feb12014-02-24 13:46:47 -0800355 Rect magnifiedRegionBounds = mTempRect2;
356 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(
357 magnifiedRegionBounds);
358 Rect touchableRegionBounds = mTempRect1;
359 windowState.getTouchableRegion(mTempRegion1);
360 mTempRegion1.getBounds(touchableRegionBounds);
361 if (!magnifiedRegionBounds.intersect(touchableRegionBounds)) {
362 mCallbacks.onRectangleOnScreenRequested(
363 touchableRegionBounds.left,
364 touchableRegionBounds.top,
365 touchableRegionBounds.right,
366 touchableRegionBounds.bottom);
367 }
368 } break;
369 } break;
370 }
371 }
372 }
373
374 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
375 MagnificationSpec spec = mMagnifedViewport.getMagnificationSpecLocked();
376 if (spec != null && !spec.isNop()) {
377 WindowManagerPolicy policy = mWindowManagerService.mPolicy;
378 final int windowType = windowState.mAttrs.type;
379 if (!policy.isTopLevelWindow(windowType) && windowState.mAttachedWindow != null
380 && !policy.canMagnifyWindow(windowType)) {
381 return null;
382 }
383 if (!policy.canMagnifyWindow(windowState.mAttrs.type)) {
384 return null;
385 }
386 }
387 return spec;
388 }
389
390 public void destroyLocked() {
391 mMagnifedViewport.destroyWindow();
392 }
393
394 /** NOTE: This has to be called within a surface transaction. */
395 public void drawMagnifiedRegionBorderIfNeededLocked() {
396 mMagnifedViewport.drawWindowIfNeededLocked();
397 }
398
399 private final class MagnifiedViewport {
400
Svetoslav8e3feb12014-02-24 13:46:47 -0800401 private final SparseArray<WindowState> mTempWindowStates =
402 new SparseArray<WindowState>();
403
404 private final RectF mTempRectF = new RectF();
405
406 private final Point mTempPoint = new Point();
407
408 private final Matrix mTempMatrix = new Matrix();
409
410 private final Region mMagnifiedBounds = new Region();
411 private final Region mOldMagnifiedBounds = new Region();
Alan Viverette214fb682015-11-17 09:47:11 -0500412 private final Region mOldAvailableBounds = new Region();
Svetoslav8e3feb12014-02-24 13:46:47 -0800413
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800414 private final Path mCircularPath;
415
Svetoslav8e3feb12014-02-24 13:46:47 -0800416 private final MagnificationSpec mMagnificationSpec = MagnificationSpec.obtain();
417
418 private final WindowManager mWindowManager;
419
Svetoslav7505e332014-08-22 12:14:28 -0700420 private final float mBorderWidth;
Svetoslav8e3feb12014-02-24 13:46:47 -0800421 private final int mHalfBorderWidth;
Svetoslav7505e332014-08-22 12:14:28 -0700422 private final int mDrawBorderInset;
Svetoslav8e3feb12014-02-24 13:46:47 -0800423
424 private final ViewportWindow mWindow;
425
426 private boolean mFullRedrawNeeded;
427
428 public MagnifiedViewport() {
429 mWindowManager = (WindowManager) mContext.getSystemService(Service.WINDOW_SERVICE);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800430 mBorderWidth = mContext.getResources().getDimension(
431 com.android.internal.R.dimen.accessibility_magnification_indicator_width);
Svetoslav7505e332014-08-22 12:14:28 -0700432 mHalfBorderWidth = (int) Math.ceil(mBorderWidth / 2);
433 mDrawBorderInset = (int) mBorderWidth / 2;
Svetoslav8e3feb12014-02-24 13:46:47 -0800434 mWindow = new ViewportWindow(mContext);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800435
Adam Powell01f280d2015-05-18 16:07:42 -0700436 if (mContext.getResources().getConfiguration().isScreenRound()) {
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800437 mCircularPath = new Path();
438 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
439 final int centerXY = mTempPoint.x / 2;
440 mCircularPath.addCircle(centerXY, centerXY, centerXY, Path.Direction.CW);
441 } else {
442 mCircularPath = null;
443 }
444
Svetoslav8e3feb12014-02-24 13:46:47 -0800445 recomputeBoundsLocked();
446 }
447
448 public void updateMagnificationSpecLocked(MagnificationSpec spec) {
449 if (spec != null) {
450 mMagnificationSpec.initialize(spec.scale, spec.offsetX, spec.offsetY);
451 } else {
452 mMagnificationSpec.clear();
453 }
454 // If this message is pending we are in a rotation animation and do not want
455 // to show the border. We will do so when the pending message is handled.
Svetoslav7505e332014-08-22 12:14:28 -0700456 if (!mHandler.hasMessages(
457 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED)) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800458 setMagnifiedRegionBorderShownLocked(isMagnifyingLocked(), true);
459 }
460 }
461
462 public void recomputeBoundsLocked() {
463 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
464 final int screenWidth = mTempPoint.x;
465 final int screenHeight = mTempPoint.y;
466
467 Region magnifiedBounds = mMagnifiedBounds;
468 magnifiedBounds.set(0, 0, 0, 0);
469
470 Region availableBounds = mTempRegion1;
471 availableBounds.set(0, 0, screenWidth, screenHeight);
472
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800473 if (mCircularPath != null) {
474 availableBounds.setPath(mCircularPath, availableBounds);
475 }
476
Svetoslav8e3feb12014-02-24 13:46:47 -0800477 Region nonMagnifiedBounds = mTempRegion4;
Svet Ganovb21df802014-09-01 19:06:33 -0700478 nonMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800479
480 SparseArray<WindowState> visibleWindows = mTempWindowStates;
481 visibleWindows.clear();
482 populateWindowsOnScreenLocked(visibleWindows);
483
484 final int visibleWindowCount = visibleWindows.size();
485 for (int i = visibleWindowCount - 1; i >= 0; i--) {
486 WindowState windowState = visibleWindows.valueAt(i);
487 if (windowState.mAttrs.type == WindowManager
488 .LayoutParams.TYPE_MAGNIFICATION_OVERLAY) {
489 continue;
490 }
491
492 Region windowBounds = mTempRegion2;
493 Matrix matrix = mTempMatrix;
494 populateTransformationMatrixLocked(windowState, matrix);
495 RectF windowFrame = mTempRectF;
496
497 if (mWindowManagerService.mPolicy.canMagnifyWindow(windowState.mAttrs.type)) {
498 windowFrame.set(windowState.mFrame);
499 windowFrame.offset(-windowFrame.left, -windowFrame.top);
500 matrix.mapRect(windowFrame);
501 windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
502 (int) windowFrame.right, (int) windowFrame.bottom);
503 magnifiedBounds.op(windowBounds, Region.Op.UNION);
504 magnifiedBounds.op(availableBounds, Region.Op.INTERSECT);
505 } else {
506 Region touchableRegion = mTempRegion3;
507 windowState.getTouchableRegion(touchableRegion);
508 Rect touchableFrame = mTempRect1;
509 touchableRegion.getBounds(touchableFrame);
510 windowFrame.set(touchableFrame);
511 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
512 matrix.mapRect(windowFrame);
513 windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
514 (int) windowFrame.right, (int) windowFrame.bottom);
515 nonMagnifiedBounds.op(windowBounds, Region.Op.UNION);
516 windowBounds.op(magnifiedBounds, Region.Op.DIFFERENCE);
517 availableBounds.op(windowBounds, Region.Op.DIFFERENCE);
518 }
519
520 Region accountedBounds = mTempRegion2;
521 accountedBounds.set(magnifiedBounds);
522 accountedBounds.op(nonMagnifiedBounds, Region.Op.UNION);
523 accountedBounds.op(0, 0, screenWidth, screenHeight, Region.Op.INTERSECT);
524
525 if (accountedBounds.isRect()) {
526 Rect accountedFrame = mTempRect1;
527 accountedBounds.getBounds(accountedFrame);
528 if (accountedFrame.width() == screenWidth
529 && accountedFrame.height() == screenHeight) {
530 break;
531 }
532 }
533 }
534
535 visibleWindows.clear();
536
Svetoslav7505e332014-08-22 12:14:28 -0700537 magnifiedBounds.op(mDrawBorderInset, mDrawBorderInset,
538 screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset,
Svetoslav8e3feb12014-02-24 13:46:47 -0800539 Region.Op.INTERSECT);
540
Alan Viverette214fb682015-11-17 09:47:11 -0500541 final boolean magnifiedChanged = !mOldMagnifiedBounds.equals(magnifiedBounds);
542 final boolean availableChanged = !mOldAvailableBounds.equals(availableBounds);
543 if (magnifiedChanged || availableChanged) {
544 if (magnifiedChanged) {
545 mWindow.setBounds(magnifiedBounds);
546 Rect dirtyRect = mTempRect1;
547 if (mFullRedrawNeeded) {
548 mFullRedrawNeeded = false;
549 dirtyRect.set(mDrawBorderInset, mDrawBorderInset,
550 screenWidth - mDrawBorderInset,
551 screenHeight - mDrawBorderInset);
552 mWindow.invalidate(dirtyRect);
553 } else {
554 Region dirtyRegion = mTempRegion3;
555 dirtyRegion.set(magnifiedBounds);
556 dirtyRegion.op(mOldMagnifiedBounds, Region.Op.UNION);
557 dirtyRegion.op(nonMagnifiedBounds, Region.Op.INTERSECT);
558 dirtyRegion.getBounds(dirtyRect);
559 mWindow.invalidate(dirtyRect);
560 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800561
Alan Viverette214fb682015-11-17 09:47:11 -0500562 mOldMagnifiedBounds.set(magnifiedBounds);
Svetoslav8e3feb12014-02-24 13:46:47 -0800563 }
564
Alan Viverette214fb682015-11-17 09:47:11 -0500565 if (availableChanged) {
566 mOldAvailableBounds.set(availableBounds);
567 }
568
569 final SomeArgs args = SomeArgs.obtain();
570 args.arg1 = Region.obtain(magnifiedBounds);
571 args.arg2 = Region.obtain(availableBounds);
572 mHandler.obtainMessage(
573 MyHandler.MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED, args).sendToTarget();
Svetoslav8e3feb12014-02-24 13:46:47 -0800574 }
575 }
576
577 public void onRotationChangedLocked() {
578 // If we are magnifying, hide the magnified border window immediately so
579 // the user does not see strange artifacts during rotation. The screenshot
580 // used for rotation has already the border. After the rotation is complete
581 // we will show the border.
582 if (isMagnifyingLocked()) {
583 setMagnifiedRegionBorderShownLocked(false, false);
584 final long delay = (long) (mLongAnimationDuration
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700585 * mWindowManagerService.getWindowAnimationScaleLocked());
Svetoslav8e3feb12014-02-24 13:46:47 -0800586 Message message = mHandler.obtainMessage(
587 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED);
588 mHandler.sendMessageDelayed(message, delay);
589 }
590 recomputeBoundsLocked();
591 mWindow.updateSize();
592 }
593
594 public void setMagnifiedRegionBorderShownLocked(boolean shown, boolean animate) {
595 if (shown) {
596 mFullRedrawNeeded = true;
Svet Ganovb21df802014-09-01 19:06:33 -0700597 mOldMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800598 }
599 mWindow.setShown(shown, animate);
600 }
601
602 public void getMagnifiedFrameInContentCoordsLocked(Rect rect) {
603 MagnificationSpec spec = mMagnificationSpec;
604 mMagnifiedBounds.getBounds(rect);
605 rect.offset((int) -spec.offsetX, (int) -spec.offsetY);
606 rect.scale(1.0f / spec.scale);
607 }
608
609 public boolean isMagnifyingLocked() {
610 return mMagnificationSpec.scale > 1.0f;
611 }
612
613 public MagnificationSpec getMagnificationSpecLocked() {
614 return mMagnificationSpec;
615 }
616
617 /** NOTE: This has to be called within a surface transaction. */
618 public void drawWindowIfNeededLocked() {
619 recomputeBoundsLocked();
620 mWindow.drawIfNeeded();
621 }
622
623 public void destroyWindow() {
624 mWindow.releaseSurface();
625 }
626
627 private void populateWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
628 DisplayContent displayContent = mWindowManagerService
629 .getDefaultDisplayContentLocked();
630 WindowList windowList = displayContent.getWindowList();
631 final int windowCount = windowList.size();
632 for (int i = 0; i < windowCount; i++) {
633 WindowState windowState = windowList.get(i);
Craig Mautner165be0c2015-01-27 15:16:58 -0800634 if (windowState.isOnScreen() &&
635 !windowState.mWinAnimator.mEnterAnimationPending) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800636 outWindows.put(windowState.mLayer, windowState);
637 }
638 }
639 }
640
641 private final class ViewportWindow {
642 private static final String SURFACE_TITLE = "Magnification Overlay";
643
Svetoslav8e3feb12014-02-24 13:46:47 -0800644 private final Region mBounds = new Region();
645 private final Rect mDirtyRect = new Rect();
646 private final Paint mPaint = new Paint();
647
Svetoslav8e3feb12014-02-24 13:46:47 -0800648 private final SurfaceControl mSurfaceControl;
649 private final Surface mSurface = new Surface();
650
Svet Ganovb21df802014-09-01 19:06:33 -0700651 private final AnimationController mAnimationController;
652
Svetoslav8e3feb12014-02-24 13:46:47 -0800653 private boolean mShown;
654 private int mAlpha;
655
656 private boolean mInvalidated;
657
658 public ViewportWindow(Context context) {
659 SurfaceControl surfaceControl = null;
660 try {
661 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
662 surfaceControl = new SurfaceControl(mWindowManagerService.mFxSession,
663 SURFACE_TITLE, mTempPoint.x, mTempPoint.y, PixelFormat.TRANSLUCENT,
664 SurfaceControl.HIDDEN);
665 } catch (OutOfResourcesException oore) {
666 /* ignore */
667 }
668 mSurfaceControl = surfaceControl;
669 mSurfaceControl.setLayerStack(mWindowManager.getDefaultDisplay()
670 .getLayerStack());
671 mSurfaceControl.setLayer(mWindowManagerService.mPolicy.windowTypeToLayerLw(
672 WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY)
673 * WindowManagerService.TYPE_LAYER_MULTIPLIER);
674 mSurfaceControl.setPosition(0, 0);
675 mSurface.copyFrom(mSurfaceControl);
676
Svet Ganovb21df802014-09-01 19:06:33 -0700677 mAnimationController = new AnimationController(context,
678 mWindowManagerService.mH.getLooper());
679
Svetoslav8e3feb12014-02-24 13:46:47 -0800680 TypedValue typedValue = new TypedValue();
681 context.getTheme().resolveAttribute(R.attr.colorActivatedHighlight,
682 typedValue, true);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700683 final int borderColor = context.getColor(typedValue.resourceId);
Svetoslav8e3feb12014-02-24 13:46:47 -0800684
685 mPaint.setStyle(Paint.Style.STROKE);
686 mPaint.setStrokeWidth(mBorderWidth);
687 mPaint.setColor(borderColor);
688
Svetoslav8e3feb12014-02-24 13:46:47 -0800689 mInvalidated = true;
690 }
691
692 public void setShown(boolean shown, boolean animate) {
693 synchronized (mWindowManagerService.mWindowMap) {
694 if (mShown == shown) {
695 return;
696 }
697 mShown = shown;
Svet Ganovb21df802014-09-01 19:06:33 -0700698 mAnimationController.onFrameShownStateChanged(shown, animate);
Svetoslav8e3feb12014-02-24 13:46:47 -0800699 if (DEBUG_VIEWPORT_WINDOW) {
700 Slog.i(LOG_TAG, "ViewportWindow shown: " + mShown);
701 }
702 }
703 }
704
705 @SuppressWarnings("unused")
706 // Called reflectively from an animator.
707 public int getAlpha() {
708 synchronized (mWindowManagerService.mWindowMap) {
709 return mAlpha;
710 }
711 }
712
713 public void setAlpha(int alpha) {
714 synchronized (mWindowManagerService.mWindowMap) {
715 if (mAlpha == alpha) {
716 return;
717 }
718 mAlpha = alpha;
719 invalidate(null);
720 if (DEBUG_VIEWPORT_WINDOW) {
721 Slog.i(LOG_TAG, "ViewportWindow set alpha: " + alpha);
722 }
723 }
724 }
725
726 public void setBounds(Region bounds) {
727 synchronized (mWindowManagerService.mWindowMap) {
728 if (mBounds.equals(bounds)) {
729 return;
730 }
731 mBounds.set(bounds);
732 invalidate(mDirtyRect);
733 if (DEBUG_VIEWPORT_WINDOW) {
734 Slog.i(LOG_TAG, "ViewportWindow set bounds: " + bounds);
735 }
736 }
737 }
738
739 public void updateSize() {
740 synchronized (mWindowManagerService.mWindowMap) {
741 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
742 mSurfaceControl.setSize(mTempPoint.x, mTempPoint.y);
743 invalidate(mDirtyRect);
744 }
745 }
746
747 public void invalidate(Rect dirtyRect) {
748 if (dirtyRect != null) {
749 mDirtyRect.set(dirtyRect);
750 } else {
751 mDirtyRect.setEmpty();
752 }
753 mInvalidated = true;
754 mWindowManagerService.scheduleAnimationLocked();
755 }
756
757 /** NOTE: This has to be called within a surface transaction. */
758 public void drawIfNeeded() {
759 synchronized (mWindowManagerService.mWindowMap) {
760 if (!mInvalidated) {
761 return;
762 }
763 mInvalidated = false;
764 Canvas canvas = null;
765 try {
766 // Empty dirty rectangle means unspecified.
767 if (mDirtyRect.isEmpty()) {
768 mBounds.getBounds(mDirtyRect);
769 }
770 mDirtyRect.inset(- mHalfBorderWidth, - mHalfBorderWidth);
771 canvas = mSurface.lockCanvas(mDirtyRect);
772 if (DEBUG_VIEWPORT_WINDOW) {
773 Slog.i(LOG_TAG, "Dirty rect: " + mDirtyRect);
774 }
775 } catch (IllegalArgumentException iae) {
776 /* ignore */
777 } catch (Surface.OutOfResourcesException oore) {
778 /* ignore */
779 }
780 if (canvas == null) {
781 return;
782 }
783 if (DEBUG_VIEWPORT_WINDOW) {
784 Slog.i(LOG_TAG, "Bounds: " + mBounds);
785 }
786 canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
787 mPaint.setAlpha(mAlpha);
788 Path path = mBounds.getBoundaryPath();
789 canvas.drawPath(path, mPaint);
790
791 mSurface.unlockCanvasAndPost(canvas);
792
793 if (mAlpha > 0) {
794 mSurfaceControl.show();
795 } else {
796 mSurfaceControl.hide();
797 }
798 }
799 }
800
801 public void releaseSurface() {
802 mSurfaceControl.release();
803 mSurface.release();
804 }
Svet Ganovb21df802014-09-01 19:06:33 -0700805
806 private final class AnimationController extends Handler {
807 private static final String PROPERTY_NAME_ALPHA = "alpha";
808
809 private static final int MIN_ALPHA = 0;
810 private static final int MAX_ALPHA = 255;
811
812 private static final int MSG_FRAME_SHOWN_STATE_CHANGED = 1;
813
814 private final ValueAnimator mShowHideFrameAnimator;
815
816 public AnimationController(Context context, Looper looper) {
817 super(looper);
818 mShowHideFrameAnimator = ObjectAnimator.ofInt(ViewportWindow.this,
819 PROPERTY_NAME_ALPHA, MIN_ALPHA, MAX_ALPHA);
820
821 Interpolator interpolator = new DecelerateInterpolator(2.5f);
822 final long longAnimationDuration = context.getResources().getInteger(
823 com.android.internal.R.integer.config_longAnimTime);
824
825 mShowHideFrameAnimator.setInterpolator(interpolator);
826 mShowHideFrameAnimator.setDuration(longAnimationDuration);
827 }
828
829 public void onFrameShownStateChanged(boolean shown, boolean animate) {
830 obtainMessage(MSG_FRAME_SHOWN_STATE_CHANGED,
831 shown ? 1 : 0, animate ? 1 : 0).sendToTarget();
832 }
833
834 @Override
835 public void handleMessage(Message message) {
836 switch (message.what) {
837 case MSG_FRAME_SHOWN_STATE_CHANGED: {
838 final boolean shown = message.arg1 == 1;
839 final boolean animate = message.arg2 == 1;
840
841 if (animate) {
842 if (mShowHideFrameAnimator.isRunning()) {
843 mShowHideFrameAnimator.reverse();
844 } else {
845 if (shown) {
846 mShowHideFrameAnimator.start();
847 } else {
848 mShowHideFrameAnimator.reverse();
849 }
850 }
851 } else {
852 mShowHideFrameAnimator.cancel();
853 if (shown) {
854 setAlpha(MAX_ALPHA);
855 } else {
856 setAlpha(MIN_ALPHA);
857 }
858 }
859 } break;
860 }
861 }
862 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800863 }
864 }
865
866 private class MyHandler extends Handler {
867 public static final int MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED = 1;
868 public static final int MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED = 2;
869 public static final int MESSAGE_NOTIFY_USER_CONTEXT_CHANGED = 3;
870 public static final int MESSAGE_NOTIFY_ROTATION_CHANGED = 4;
871 public static final int MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED = 5;
872
873 public MyHandler(Looper looper) {
874 super(looper);
875 }
876
877 @Override
878 public void handleMessage(Message message) {
879 switch (message.what) {
880 case MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED: {
Alan Viverette214fb682015-11-17 09:47:11 -0500881 final SomeArgs args = (SomeArgs) message.obj;
882 final Region magnifiedBounds = (Region) args.arg1;
883 final Region availableBounds = (Region) args.arg2;
884 mCallbacks.onMagnifiedBoundsChanged(magnifiedBounds, availableBounds);
885 magnifiedBounds.recycle();
886 availableBounds.recycle();
Svetoslav8e3feb12014-02-24 13:46:47 -0800887 } break;
888
889 case MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED: {
890 SomeArgs args = (SomeArgs) message.obj;
891 final int left = args.argi1;
892 final int top = args.argi2;
893 final int right = args.argi3;
894 final int bottom = args.argi4;
895 mCallbacks.onRectangleOnScreenRequested(left, top, right, bottom);
896 args.recycle();
897 } break;
898
899 case MESSAGE_NOTIFY_USER_CONTEXT_CHANGED: {
900 mCallbacks.onUserContextChanged();
901 } break;
902
903 case MESSAGE_NOTIFY_ROTATION_CHANGED: {
904 final int rotation = message.arg1;
905 mCallbacks.onRotationChanged(rotation);
906 } break;
907
908 case MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED : {
909 synchronized (mWindowManagerService.mWindowMap) {
910 if (mMagnifedViewport.isMagnifyingLocked()) {
911 mMagnifedViewport.setMagnifiedRegionBorderShownLocked(true, true);
912 mWindowManagerService.scheduleAnimationLocked();
913 }
914 }
915 } break;
916 }
917 }
918 }
919 }
920
921 /**
922 * This class encapsulates the functionality related to computing the windows
923 * reported for accessibility purposes. These windows are all windows a sighted
924 * user can see on the screen.
925 */
926 private static final class WindowsForAccessibilityObserver {
927 private static final String LOG_TAG = "WindowsForAccessibilityObserver";
928
929 private static final boolean DEBUG = false;
930
931 private final SparseArray<WindowState> mTempWindowStates =
932 new SparseArray<WindowState>();
933
934 private final List<WindowInfo> mOldWindows = new ArrayList<WindowInfo>();
935
936 private final Set<IBinder> mTempBinderSet = new ArraySet<IBinder>();
937
938 private final RectF mTempRectF = new RectF();
939
940 private final Matrix mTempMatrix = new Matrix();
941
942 private final Point mTempPoint = new Point();
943
944 private final Rect mTempRect = new Rect();
945
946 private final Region mTempRegion = new Region();
947
948 private final Region mTempRegion1 = new Region();
949
950 private final Context mContext;
951
952 private final WindowManagerService mWindowManagerService;
953
954 private final Handler mHandler;
955
956 private final WindowsForAccessibilityCallback mCallback;
957
Svetoslavf7174e82014-06-12 11:29:35 -0700958 private final long mRecurringAccessibilityEventsIntervalMillis;
959
Svetoslav8e3feb12014-02-24 13:46:47 -0800960 public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
961 WindowsForAccessibilityCallback callback) {
962 mContext = windowManagerService.mContext;
963 mWindowManagerService = windowManagerService;
964 mCallback = callback;
965 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
Svetoslavf7174e82014-06-12 11:29:35 -0700966 mRecurringAccessibilityEventsIntervalMillis = ViewConfiguration
967 .getSendRecurringAccessibilityEventsInterval();
Svetoslav8e3feb12014-02-24 13:46:47 -0800968 computeChangedWindows();
969 }
970
Svetoslav3a0d8782014-12-04 12:50:11 -0800971 public void performComputeChangedWindowsNotLocked() {
972 mHandler.removeMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS);
973 computeChangedWindows();
974 }
975
Svetoslavf7174e82014-06-12 11:29:35 -0700976 public void scheduleComputeChangedWindowsLocked() {
Svetoslav3a0d8782014-12-04 12:50:11 -0800977 if (!mHandler.hasMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS)) {
Svetoslavf7174e82014-06-12 11:29:35 -0700978 mHandler.sendEmptyMessageDelayed(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS,
979 mRecurringAccessibilityEventsIntervalMillis);
980 }
981 }
982
Svetoslav8e3feb12014-02-24 13:46:47 -0800983 public void computeChangedWindows() {
984 if (DEBUG) {
985 Slog.i(LOG_TAG, "computeChangedWindows()");
986 }
987
Svetoslav3a0d8782014-12-04 12:50:11 -0800988 boolean windowsChanged = false;
989 List<WindowInfo> windows = new ArrayList<WindowInfo>();
990
Svetoslav8e3feb12014-02-24 13:46:47 -0800991 synchronized (mWindowManagerService.mWindowMap) {
Svetoslavf7174e82014-06-12 11:29:35 -0700992 // Do not send the windows if there is no current focus as
993 // the window manager is still looking for where to put it.
994 // We will do the work when we get a focus change callback.
995 if (mWindowManagerService.mCurrentFocus == null) {
996 return;
997 }
998
Svetoslav8e3feb12014-02-24 13:46:47 -0800999 WindowManager windowManager = (WindowManager)
1000 mContext.getSystemService(Context.WINDOW_SERVICE);
1001 windowManager.getDefaultDisplay().getRealSize(mTempPoint);
1002 final int screenWidth = mTempPoint.x;
1003 final int screenHeight = mTempPoint.y;
1004
1005 Region unaccountedSpace = mTempRegion;
1006 unaccountedSpace.set(0, 0, screenWidth, screenHeight);
1007
1008 SparseArray<WindowState> visibleWindows = mTempWindowStates;
1009 populateVisibleWindowsOnScreenLocked(visibleWindows);
1010
Svetoslav8e3feb12014-02-24 13:46:47 -08001011 Set<IBinder> addedWindows = mTempBinderSet;
1012 addedWindows.clear();
1013
Svetoslavf7174e82014-06-12 11:29:35 -07001014 boolean focusedWindowAdded = false;
1015
Svetoslav8e3feb12014-02-24 13:46:47 -08001016 final int visibleWindowCount = visibleWindows.size();
1017 for (int i = visibleWindowCount - 1; i >= 0; i--) {
Alan Viverette9538eea2014-11-13 14:49:20 -08001018 final WindowState windowState = visibleWindows.valueAt(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001019 final int flags = windowState.mAttrs.flags;
1020
Svetoslav3a5c7212014-10-14 09:54:26 -07001021 // If the window is not touchable - ignore.
Svetoslavf7174e82014-06-12 11:29:35 -07001022 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001023 continue;
1024 }
1025
Alan Viverette9538eea2014-11-13 14:49:20 -08001026 // Compute the bounds in the screen.
1027 final Rect boundsInScreen = mTempRect;
1028 computeWindowBoundsInScreen(windowState, boundsInScreen);
1029
Svetoslav8e3feb12014-02-24 13:46:47 -08001030 // If the window is completely covered by other windows - ignore.
1031 if (unaccountedSpace.quickReject(boundsInScreen)) {
1032 continue;
1033 }
1034
1035 // Add windows of certain types not covered by modal windows.
1036 if (isReportedWindowType(windowState.mAttrs.type)) {
1037 // Add the window to the ones to be reported.
Svetoslavf7174e82014-06-12 11:29:35 -07001038 WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
Svetoslav8e3feb12014-02-24 13:46:47 -08001039 addedWindows.add(window.token);
Svetoslav8e3feb12014-02-24 13:46:47 -08001040 windows.add(window);
Svetoslavf7174e82014-06-12 11:29:35 -07001041 if (windowState.isFocused()) {
1042 focusedWindowAdded = true;
1043 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001044 }
1045
Alan Viveretted0c73f42014-11-18 10:25:04 -08001046 // Account for the space this window takes if the window
1047 // is not an accessibility overlay which does not change
1048 // the reported windows.
1049 if (windowState.mAttrs.type !=
1050 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY) {
1051 unaccountedSpace.op(boundsInScreen, unaccountedSpace,
1052 Region.Op.REVERSE_DIFFERENCE);
1053 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001054
1055 // We figured out what is touchable for the entire screen - done.
1056 if (unaccountedSpace.isEmpty()) {
1057 break;
1058 }
1059
1060 // If a window is modal, no other below can be touched - done.
1061 if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1062 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
1063 break;
1064 }
1065 }
1066
Svetoslavf7174e82014-06-12 11:29:35 -07001067 // Always report the focused window.
1068 if (!focusedWindowAdded) {
1069 for (int i = visibleWindowCount - 1; i >= 0; i--) {
1070 WindowState windowState = visibleWindows.valueAt(i);
1071 if (windowState.isFocused()) {
1072 // Compute the bounds in the screen.
1073 Rect boundsInScreen = mTempRect;
1074 computeWindowBoundsInScreen(windowState, boundsInScreen);
1075
1076 // Add the window to the ones to be reported.
1077 WindowInfo window = obtainPopulatedWindowInfo(windowState,
1078 boundsInScreen);
1079 addedWindows.add(window.token);
1080 windows.add(window);
1081 break;
1082 }
1083 }
1084 }
1085
Svetoslav8e3feb12014-02-24 13:46:47 -08001086 // Remove child/parent references to windows that were not added.
1087 final int windowCount = windows.size();
1088 for (int i = 0; i < windowCount; i++) {
1089 WindowInfo window = windows.get(i);
1090 if (!addedWindows.contains(window.parentToken)) {
1091 window.parentToken = null;
1092 }
1093 if (window.childTokens != null) {
1094 final int childTokenCount = window.childTokens.size();
1095 for (int j = childTokenCount - 1; j >= 0; j--) {
1096 if (!addedWindows.contains(window.childTokens.get(j))) {
1097 window.childTokens.remove(j);
1098 }
1099 }
1100 // Leave the child token list if empty.
1101 }
1102 }
1103
1104 visibleWindows.clear();
1105 addedWindows.clear();
1106
1107 // We computed the windows and if they changed notify the client.
Svetoslav8e3feb12014-02-24 13:46:47 -08001108 if (mOldWindows.size() != windows.size()) {
1109 // Different size means something changed.
1110 windowsChanged = true;
1111 } else if (!mOldWindows.isEmpty() || !windows.isEmpty()) {
1112 // Since we always traverse windows from high to low layer
1113 // the old and new windows at the same index should be the
1114 // same, otherwise something changed.
1115 for (int i = 0; i < windowCount; i++) {
1116 WindowInfo oldWindow = mOldWindows.get(i);
1117 WindowInfo newWindow = windows.get(i);
1118 // We do not care for layer changes given the window
1119 // order does not change. This brings no new information
1120 // to the clients.
1121 if (windowChangedNoLayer(oldWindow, newWindow)) {
1122 windowsChanged = true;
1123 break;
1124 }
1125 }
1126 }
1127
1128 if (windowsChanged) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001129 cacheWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001130 }
1131 }
Svetoslav3a0d8782014-12-04 12:50:11 -08001132
1133 // Now we do not hold the lock, so send the windows over.
1134 if (windowsChanged) {
1135 if (DEBUG) {
1136 Log.i(LOG_TAG, "Windows changed:" + windows);
1137 }
1138 mCallback.onWindowsForAccessibilityChanged(windows);
1139 } else {
1140 if (DEBUG) {
1141 Log.i(LOG_TAG, "No windows changed.");
1142 }
1143 }
1144
1145 // Recycle the windows as we do not need them.
1146 clearAndRecycleWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001147 }
1148
Svetoslavf7174e82014-06-12 11:29:35 -07001149 private void computeWindowBoundsInScreen(WindowState windowState, Rect outBounds) {
1150 // Get the touchable frame.
1151 Region touchableRegion = mTempRegion1;
1152 windowState.getTouchableRegion(touchableRegion);
1153 Rect touchableFrame = mTempRect;
1154 touchableRegion.getBounds(touchableFrame);
1155
1156 // Move to origin as all transforms are captured by the matrix.
1157 RectF windowFrame = mTempRectF;
1158 windowFrame.set(touchableFrame);
1159 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
1160
1161 // Map the frame to get what appears on the screen.
1162 Matrix matrix = mTempMatrix;
1163 populateTransformationMatrixLocked(windowState, matrix);
1164 matrix.mapRect(windowFrame);
1165
1166 // Got the bounds.
1167 outBounds.set((int) windowFrame.left, (int) windowFrame.top,
1168 (int) windowFrame.right, (int) windowFrame.bottom);
1169 }
1170
1171 private static WindowInfo obtainPopulatedWindowInfo(WindowState windowState,
1172 Rect boundsInScreen) {
1173 WindowInfo window = WindowInfo.obtain();
1174 window.type = windowState.mAttrs.type;
1175 window.layer = windowState.mLayer;
1176 window.token = windowState.mClient.asBinder();
1177
1178 WindowState attachedWindow = windowState.mAttachedWindow;
1179 if (attachedWindow != null) {
1180 window.parentToken = attachedWindow.mClient.asBinder();
1181 }
1182
1183 window.focused = windowState.isFocused();
1184 window.boundsInScreen.set(boundsInScreen);
1185
1186 final int childCount = windowState.mChildWindows.size();
1187 if (childCount > 0) {
1188 if (window.childTokens == null) {
1189 window.childTokens = new ArrayList<IBinder>();
1190 }
1191 for (int j = 0; j < childCount; j++) {
1192 WindowState child = windowState.mChildWindows.get(j);
1193 window.childTokens.add(child.mClient.asBinder());
1194 }
1195 }
1196
1197 return window;
1198 }
1199
Svetoslav8e3feb12014-02-24 13:46:47 -08001200 private void cacheWindows(List<WindowInfo> windows) {
1201 final int oldWindowCount = mOldWindows.size();
1202 for (int i = oldWindowCount - 1; i >= 0; i--) {
1203 mOldWindows.remove(i).recycle();
1204 }
1205 final int newWindowCount = windows.size();
1206 for (int i = 0; i < newWindowCount; i++) {
1207 WindowInfo newWindow = windows.get(i);
1208 mOldWindows.add(WindowInfo.obtain(newWindow));
1209 }
1210 }
1211
1212 private boolean windowChangedNoLayer(WindowInfo oldWindow, WindowInfo newWindow) {
1213 if (oldWindow == newWindow) {
1214 return false;
1215 }
Svetoslavf7174e82014-06-12 11:29:35 -07001216 if (oldWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001217 return true;
1218 }
Svetoslavf7174e82014-06-12 11:29:35 -07001219 if (newWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001220 return true;
1221 }
1222 if (oldWindow.type != newWindow.type) {
1223 return true;
1224 }
1225 if (oldWindow.focused != newWindow.focused) {
1226 return true;
1227 }
1228 if (oldWindow.token == null) {
1229 if (newWindow.token != null) {
1230 return true;
1231 }
1232 } else if (!oldWindow.token.equals(newWindow.token)) {
1233 return true;
1234 }
1235 if (oldWindow.parentToken == null) {
1236 if (newWindow.parentToken != null) {
1237 return true;
1238 }
1239 } else if (!oldWindow.parentToken.equals(newWindow.parentToken)) {
1240 return true;
1241 }
1242 if (!oldWindow.boundsInScreen.equals(newWindow.boundsInScreen)) {
1243 return true;
1244 }
1245 if (oldWindow.childTokens != null && newWindow.childTokens != null
1246 && !oldWindow.childTokens.equals(newWindow.childTokens)) {
1247 return true;
1248 }
1249 return false;
1250 }
1251
Svetoslav3a0d8782014-12-04 12:50:11 -08001252 private static void clearAndRecycleWindows(List<WindowInfo> windows) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001253 final int windowCount = windows.size();
1254 for (int i = windowCount - 1; i >= 0; i--) {
1255 windows.remove(i).recycle();
1256 }
1257 }
1258
1259 private static boolean isReportedWindowType(int windowType) {
1260 return (windowType != WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM
1261 && windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
1262 && windowType != WindowManager.LayoutParams.TYPE_BOOT_PROGRESS
1263 && windowType != WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY
1264 && windowType != WindowManager.LayoutParams.TYPE_DRAG
Selim Cinekf83e8242015-05-19 18:08:14 -07001265 && windowType != WindowManager.LayoutParams.TYPE_INPUT_CONSUMER
Svetoslav8e3feb12014-02-24 13:46:47 -08001266 && windowType != WindowManager.LayoutParams.TYPE_POINTER
Svetoslav8e3feb12014-02-24 13:46:47 -08001267 && windowType != WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY
1268 && windowType != WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY
1269 && windowType != WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY
1270 && windowType != WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION);
1271 }
1272
1273 private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
1274 DisplayContent displayContent = mWindowManagerService
1275 .getDefaultDisplayContentLocked();
1276 WindowList windowList = displayContent.getWindowList();
1277 final int windowCount = windowList.size();
1278 for (int i = 0; i < windowCount; i++) {
1279 WindowState windowState = windowList.get(i);
1280 if (windowState.isVisibleLw()) {
1281 outWindows.put(windowState.mLayer, windowState);
1282 }
1283 }
1284 }
1285
1286 private class MyHandler extends Handler {
Svetoslavf7174e82014-06-12 11:29:35 -07001287 public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -08001288
1289 public MyHandler(Looper looper) {
1290 super(looper, null, false);
1291 }
1292
1293 @Override
1294 @SuppressWarnings("unchecked")
1295 public void handleMessage(Message message) {
1296 switch (message.what) {
Svetoslavf7174e82014-06-12 11:29:35 -07001297 case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1298 computeChangedWindows();
1299 } break;
Svetoslav8e3feb12014-02-24 13:46:47 -08001300 }
1301 }
1302 }
1303 }
1304}