blob: 91ce7398e2d7575957c9a9d0e9e0aa1895799ca8 [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;
209 sTempFloats[Matrix.MTRANS_X] = windowState.mShownFrame.left;
210 sTempFloats[Matrix.MTRANS_Y] = windowState.mShownFrame.top;
211 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();
412
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800413 private final Path mCircularPath;
414
Svetoslav8e3feb12014-02-24 13:46:47 -0800415 private final MagnificationSpec mMagnificationSpec = MagnificationSpec.obtain();
416
417 private final WindowManager mWindowManager;
418
Svetoslav7505e332014-08-22 12:14:28 -0700419 private final float mBorderWidth;
Svetoslav8e3feb12014-02-24 13:46:47 -0800420 private final int mHalfBorderWidth;
Svetoslav7505e332014-08-22 12:14:28 -0700421 private final int mDrawBorderInset;
Svetoslav8e3feb12014-02-24 13:46:47 -0800422
423 private final ViewportWindow mWindow;
424
425 private boolean mFullRedrawNeeded;
426
427 public MagnifiedViewport() {
428 mWindowManager = (WindowManager) mContext.getSystemService(Service.WINDOW_SERVICE);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800429 mBorderWidth = mContext.getResources().getDimension(
430 com.android.internal.R.dimen.accessibility_magnification_indicator_width);
Svetoslav7505e332014-08-22 12:14:28 -0700431 mHalfBorderWidth = (int) Math.ceil(mBorderWidth / 2);
432 mDrawBorderInset = (int) mBorderWidth / 2;
Svetoslav8e3feb12014-02-24 13:46:47 -0800433 mWindow = new ViewportWindow(mContext);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800434
435 if (mContext.getResources().getBoolean(
436 com.android.internal.R.bool.config_windowIsRound)) {
437 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
541 if (!mOldMagnifiedBounds.equals(magnifiedBounds)) {
542 Region bounds = Region.obtain();
543 bounds.set(magnifiedBounds);
544 mHandler.obtainMessage(MyHandler.MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED,
545 bounds).sendToTarget();
546
547 mWindow.setBounds(magnifiedBounds);
548 Rect dirtyRect = mTempRect1;
549 if (mFullRedrawNeeded) {
550 mFullRedrawNeeded = false;
Svetoslav7505e332014-08-22 12:14:28 -0700551 dirtyRect.set(mDrawBorderInset, mDrawBorderInset,
552 screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset);
Svetoslav8e3feb12014-02-24 13:46:47 -0800553 mWindow.invalidate(dirtyRect);
554 } else {
555 Region dirtyRegion = mTempRegion3;
556 dirtyRegion.set(magnifiedBounds);
557 dirtyRegion.op(mOldMagnifiedBounds, Region.Op.UNION);
558 dirtyRegion.op(nonMagnifiedBounds, Region.Op.INTERSECT);
559 dirtyRegion.getBounds(dirtyRect);
560 mWindow.invalidate(dirtyRect);
561 }
562
563 mOldMagnifiedBounds.set(magnifiedBounds);
564 }
565 }
566
567 public void onRotationChangedLocked() {
568 // If we are magnifying, hide the magnified border window immediately so
569 // the user does not see strange artifacts during rotation. The screenshot
570 // used for rotation has already the border. After the rotation is complete
571 // we will show the border.
572 if (isMagnifyingLocked()) {
573 setMagnifiedRegionBorderShownLocked(false, false);
574 final long delay = (long) (mLongAnimationDuration
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700575 * mWindowManagerService.getWindowAnimationScaleLocked());
Svetoslav8e3feb12014-02-24 13:46:47 -0800576 Message message = mHandler.obtainMessage(
577 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED);
578 mHandler.sendMessageDelayed(message, delay);
579 }
580 recomputeBoundsLocked();
581 mWindow.updateSize();
582 }
583
584 public void setMagnifiedRegionBorderShownLocked(boolean shown, boolean animate) {
585 if (shown) {
586 mFullRedrawNeeded = true;
Svet Ganovb21df802014-09-01 19:06:33 -0700587 mOldMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800588 }
589 mWindow.setShown(shown, animate);
590 }
591
592 public void getMagnifiedFrameInContentCoordsLocked(Rect rect) {
593 MagnificationSpec spec = mMagnificationSpec;
594 mMagnifiedBounds.getBounds(rect);
595 rect.offset((int) -spec.offsetX, (int) -spec.offsetY);
596 rect.scale(1.0f / spec.scale);
597 }
598
599 public boolean isMagnifyingLocked() {
600 return mMagnificationSpec.scale > 1.0f;
601 }
602
603 public MagnificationSpec getMagnificationSpecLocked() {
604 return mMagnificationSpec;
605 }
606
607 /** NOTE: This has to be called within a surface transaction. */
608 public void drawWindowIfNeededLocked() {
609 recomputeBoundsLocked();
610 mWindow.drawIfNeeded();
611 }
612
613 public void destroyWindow() {
614 mWindow.releaseSurface();
615 }
616
617 private void populateWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
618 DisplayContent displayContent = mWindowManagerService
619 .getDefaultDisplayContentLocked();
620 WindowList windowList = displayContent.getWindowList();
621 final int windowCount = windowList.size();
622 for (int i = 0; i < windowCount; i++) {
623 WindowState windowState = windowList.get(i);
Craig Mautner165be0c2015-01-27 15:16:58 -0800624 if (windowState.isOnScreen() &&
625 !windowState.mWinAnimator.mEnterAnimationPending) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800626 outWindows.put(windowState.mLayer, windowState);
627 }
628 }
629 }
630
631 private final class ViewportWindow {
632 private static final String SURFACE_TITLE = "Magnification Overlay";
633
Svetoslav8e3feb12014-02-24 13:46:47 -0800634 private final Region mBounds = new Region();
635 private final Rect mDirtyRect = new Rect();
636 private final Paint mPaint = new Paint();
637
Svetoslav8e3feb12014-02-24 13:46:47 -0800638 private final SurfaceControl mSurfaceControl;
639 private final Surface mSurface = new Surface();
640
Svet Ganovb21df802014-09-01 19:06:33 -0700641 private final AnimationController mAnimationController;
642
Svetoslav8e3feb12014-02-24 13:46:47 -0800643 private boolean mShown;
644 private int mAlpha;
645
646 private boolean mInvalidated;
647
648 public ViewportWindow(Context context) {
649 SurfaceControl surfaceControl = null;
650 try {
651 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
652 surfaceControl = new SurfaceControl(mWindowManagerService.mFxSession,
653 SURFACE_TITLE, mTempPoint.x, mTempPoint.y, PixelFormat.TRANSLUCENT,
654 SurfaceControl.HIDDEN);
655 } catch (OutOfResourcesException oore) {
656 /* ignore */
657 }
658 mSurfaceControl = surfaceControl;
659 mSurfaceControl.setLayerStack(mWindowManager.getDefaultDisplay()
660 .getLayerStack());
661 mSurfaceControl.setLayer(mWindowManagerService.mPolicy.windowTypeToLayerLw(
662 WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY)
663 * WindowManagerService.TYPE_LAYER_MULTIPLIER);
664 mSurfaceControl.setPosition(0, 0);
665 mSurface.copyFrom(mSurfaceControl);
666
Svet Ganovb21df802014-09-01 19:06:33 -0700667 mAnimationController = new AnimationController(context,
668 mWindowManagerService.mH.getLooper());
669
Svetoslav8e3feb12014-02-24 13:46:47 -0800670 TypedValue typedValue = new TypedValue();
671 context.getTheme().resolveAttribute(R.attr.colorActivatedHighlight,
672 typedValue, true);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700673 final int borderColor = context.getColor(typedValue.resourceId);
Svetoslav8e3feb12014-02-24 13:46:47 -0800674
675 mPaint.setStyle(Paint.Style.STROKE);
676 mPaint.setStrokeWidth(mBorderWidth);
677 mPaint.setColor(borderColor);
678
Svetoslav8e3feb12014-02-24 13:46:47 -0800679 mInvalidated = true;
680 }
681
682 public void setShown(boolean shown, boolean animate) {
683 synchronized (mWindowManagerService.mWindowMap) {
684 if (mShown == shown) {
685 return;
686 }
687 mShown = shown;
Svet Ganovb21df802014-09-01 19:06:33 -0700688 mAnimationController.onFrameShownStateChanged(shown, animate);
Svetoslav8e3feb12014-02-24 13:46:47 -0800689 if (DEBUG_VIEWPORT_WINDOW) {
690 Slog.i(LOG_TAG, "ViewportWindow shown: " + mShown);
691 }
692 }
693 }
694
695 @SuppressWarnings("unused")
696 // Called reflectively from an animator.
697 public int getAlpha() {
698 synchronized (mWindowManagerService.mWindowMap) {
699 return mAlpha;
700 }
701 }
702
703 public void setAlpha(int alpha) {
704 synchronized (mWindowManagerService.mWindowMap) {
705 if (mAlpha == alpha) {
706 return;
707 }
708 mAlpha = alpha;
709 invalidate(null);
710 if (DEBUG_VIEWPORT_WINDOW) {
711 Slog.i(LOG_TAG, "ViewportWindow set alpha: " + alpha);
712 }
713 }
714 }
715
716 public void setBounds(Region bounds) {
717 synchronized (mWindowManagerService.mWindowMap) {
718 if (mBounds.equals(bounds)) {
719 return;
720 }
721 mBounds.set(bounds);
722 invalidate(mDirtyRect);
723 if (DEBUG_VIEWPORT_WINDOW) {
724 Slog.i(LOG_TAG, "ViewportWindow set bounds: " + bounds);
725 }
726 }
727 }
728
729 public void updateSize() {
730 synchronized (mWindowManagerService.mWindowMap) {
731 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
732 mSurfaceControl.setSize(mTempPoint.x, mTempPoint.y);
733 invalidate(mDirtyRect);
734 }
735 }
736
737 public void invalidate(Rect dirtyRect) {
738 if (dirtyRect != null) {
739 mDirtyRect.set(dirtyRect);
740 } else {
741 mDirtyRect.setEmpty();
742 }
743 mInvalidated = true;
744 mWindowManagerService.scheduleAnimationLocked();
745 }
746
747 /** NOTE: This has to be called within a surface transaction. */
748 public void drawIfNeeded() {
749 synchronized (mWindowManagerService.mWindowMap) {
750 if (!mInvalidated) {
751 return;
752 }
753 mInvalidated = false;
754 Canvas canvas = null;
755 try {
756 // Empty dirty rectangle means unspecified.
757 if (mDirtyRect.isEmpty()) {
758 mBounds.getBounds(mDirtyRect);
759 }
760 mDirtyRect.inset(- mHalfBorderWidth, - mHalfBorderWidth);
761 canvas = mSurface.lockCanvas(mDirtyRect);
762 if (DEBUG_VIEWPORT_WINDOW) {
763 Slog.i(LOG_TAG, "Dirty rect: " + mDirtyRect);
764 }
765 } catch (IllegalArgumentException iae) {
766 /* ignore */
767 } catch (Surface.OutOfResourcesException oore) {
768 /* ignore */
769 }
770 if (canvas == null) {
771 return;
772 }
773 if (DEBUG_VIEWPORT_WINDOW) {
774 Slog.i(LOG_TAG, "Bounds: " + mBounds);
775 }
776 canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
777 mPaint.setAlpha(mAlpha);
778 Path path = mBounds.getBoundaryPath();
779 canvas.drawPath(path, mPaint);
780
781 mSurface.unlockCanvasAndPost(canvas);
782
783 if (mAlpha > 0) {
784 mSurfaceControl.show();
785 } else {
786 mSurfaceControl.hide();
787 }
788 }
789 }
790
791 public void releaseSurface() {
792 mSurfaceControl.release();
793 mSurface.release();
794 }
Svet Ganovb21df802014-09-01 19:06:33 -0700795
796 private final class AnimationController extends Handler {
797 private static final String PROPERTY_NAME_ALPHA = "alpha";
798
799 private static final int MIN_ALPHA = 0;
800 private static final int MAX_ALPHA = 255;
801
802 private static final int MSG_FRAME_SHOWN_STATE_CHANGED = 1;
803
804 private final ValueAnimator mShowHideFrameAnimator;
805
806 public AnimationController(Context context, Looper looper) {
807 super(looper);
808 mShowHideFrameAnimator = ObjectAnimator.ofInt(ViewportWindow.this,
809 PROPERTY_NAME_ALPHA, MIN_ALPHA, MAX_ALPHA);
810
811 Interpolator interpolator = new DecelerateInterpolator(2.5f);
812 final long longAnimationDuration = context.getResources().getInteger(
813 com.android.internal.R.integer.config_longAnimTime);
814
815 mShowHideFrameAnimator.setInterpolator(interpolator);
816 mShowHideFrameAnimator.setDuration(longAnimationDuration);
817 }
818
819 public void onFrameShownStateChanged(boolean shown, boolean animate) {
820 obtainMessage(MSG_FRAME_SHOWN_STATE_CHANGED,
821 shown ? 1 : 0, animate ? 1 : 0).sendToTarget();
822 }
823
824 @Override
825 public void handleMessage(Message message) {
826 switch (message.what) {
827 case MSG_FRAME_SHOWN_STATE_CHANGED: {
828 final boolean shown = message.arg1 == 1;
829 final boolean animate = message.arg2 == 1;
830
831 if (animate) {
832 if (mShowHideFrameAnimator.isRunning()) {
833 mShowHideFrameAnimator.reverse();
834 } else {
835 if (shown) {
836 mShowHideFrameAnimator.start();
837 } else {
838 mShowHideFrameAnimator.reverse();
839 }
840 }
841 } else {
842 mShowHideFrameAnimator.cancel();
843 if (shown) {
844 setAlpha(MAX_ALPHA);
845 } else {
846 setAlpha(MIN_ALPHA);
847 }
848 }
849 } break;
850 }
851 }
852 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800853 }
854 }
855
856 private class MyHandler extends Handler {
857 public static final int MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED = 1;
858 public static final int MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED = 2;
859 public static final int MESSAGE_NOTIFY_USER_CONTEXT_CHANGED = 3;
860 public static final int MESSAGE_NOTIFY_ROTATION_CHANGED = 4;
861 public static final int MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED = 5;
862
863 public MyHandler(Looper looper) {
864 super(looper);
865 }
866
867 @Override
868 public void handleMessage(Message message) {
869 switch (message.what) {
870 case MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED: {
871 Region bounds = (Region) message.obj;
872 mCallbacks.onMagnifedBoundsChanged(bounds);
873 bounds.recycle();
874 } break;
875
876 case MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED: {
877 SomeArgs args = (SomeArgs) message.obj;
878 final int left = args.argi1;
879 final int top = args.argi2;
880 final int right = args.argi3;
881 final int bottom = args.argi4;
882 mCallbacks.onRectangleOnScreenRequested(left, top, right, bottom);
883 args.recycle();
884 } break;
885
886 case MESSAGE_NOTIFY_USER_CONTEXT_CHANGED: {
887 mCallbacks.onUserContextChanged();
888 } break;
889
890 case MESSAGE_NOTIFY_ROTATION_CHANGED: {
891 final int rotation = message.arg1;
892 mCallbacks.onRotationChanged(rotation);
893 } break;
894
895 case MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED : {
896 synchronized (mWindowManagerService.mWindowMap) {
897 if (mMagnifedViewport.isMagnifyingLocked()) {
898 mMagnifedViewport.setMagnifiedRegionBorderShownLocked(true, true);
899 mWindowManagerService.scheduleAnimationLocked();
900 }
901 }
902 } break;
903 }
904 }
905 }
906 }
907
908 /**
909 * This class encapsulates the functionality related to computing the windows
910 * reported for accessibility purposes. These windows are all windows a sighted
911 * user can see on the screen.
912 */
913 private static final class WindowsForAccessibilityObserver {
914 private static final String LOG_TAG = "WindowsForAccessibilityObserver";
915
916 private static final boolean DEBUG = false;
917
918 private final SparseArray<WindowState> mTempWindowStates =
919 new SparseArray<WindowState>();
920
921 private final List<WindowInfo> mOldWindows = new ArrayList<WindowInfo>();
922
923 private final Set<IBinder> mTempBinderSet = new ArraySet<IBinder>();
924
925 private final RectF mTempRectF = new RectF();
926
927 private final Matrix mTempMatrix = new Matrix();
928
929 private final Point mTempPoint = new Point();
930
931 private final Rect mTempRect = new Rect();
932
933 private final Region mTempRegion = new Region();
934
935 private final Region mTempRegion1 = new Region();
936
937 private final Context mContext;
938
939 private final WindowManagerService mWindowManagerService;
940
941 private final Handler mHandler;
942
943 private final WindowsForAccessibilityCallback mCallback;
944
Svetoslavf7174e82014-06-12 11:29:35 -0700945 private final long mRecurringAccessibilityEventsIntervalMillis;
946
Svetoslav8e3feb12014-02-24 13:46:47 -0800947 public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
948 WindowsForAccessibilityCallback callback) {
949 mContext = windowManagerService.mContext;
950 mWindowManagerService = windowManagerService;
951 mCallback = callback;
952 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
Svetoslavf7174e82014-06-12 11:29:35 -0700953 mRecurringAccessibilityEventsIntervalMillis = ViewConfiguration
954 .getSendRecurringAccessibilityEventsInterval();
Svetoslav8e3feb12014-02-24 13:46:47 -0800955 computeChangedWindows();
956 }
957
Svetoslav3a0d8782014-12-04 12:50:11 -0800958 public void performComputeChangedWindowsNotLocked() {
959 mHandler.removeMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS);
960 computeChangedWindows();
961 }
962
Svetoslavf7174e82014-06-12 11:29:35 -0700963 public void scheduleComputeChangedWindowsLocked() {
Svetoslav3a0d8782014-12-04 12:50:11 -0800964 if (!mHandler.hasMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS)) {
Svetoslavf7174e82014-06-12 11:29:35 -0700965 mHandler.sendEmptyMessageDelayed(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS,
966 mRecurringAccessibilityEventsIntervalMillis);
967 }
968 }
969
Svetoslav8e3feb12014-02-24 13:46:47 -0800970 public void computeChangedWindows() {
971 if (DEBUG) {
972 Slog.i(LOG_TAG, "computeChangedWindows()");
973 }
974
Svetoslav3a0d8782014-12-04 12:50:11 -0800975 boolean windowsChanged = false;
976 List<WindowInfo> windows = new ArrayList<WindowInfo>();
977
Svetoslav8e3feb12014-02-24 13:46:47 -0800978 synchronized (mWindowManagerService.mWindowMap) {
Svetoslavf7174e82014-06-12 11:29:35 -0700979 // Do not send the windows if there is no current focus as
980 // the window manager is still looking for where to put it.
981 // We will do the work when we get a focus change callback.
982 if (mWindowManagerService.mCurrentFocus == null) {
983 return;
984 }
985
Svetoslav8e3feb12014-02-24 13:46:47 -0800986 WindowManager windowManager = (WindowManager)
987 mContext.getSystemService(Context.WINDOW_SERVICE);
988 windowManager.getDefaultDisplay().getRealSize(mTempPoint);
989 final int screenWidth = mTempPoint.x;
990 final int screenHeight = mTempPoint.y;
991
992 Region unaccountedSpace = mTempRegion;
993 unaccountedSpace.set(0, 0, screenWidth, screenHeight);
994
995 SparseArray<WindowState> visibleWindows = mTempWindowStates;
996 populateVisibleWindowsOnScreenLocked(visibleWindows);
997
Svetoslav8e3feb12014-02-24 13:46:47 -0800998 Set<IBinder> addedWindows = mTempBinderSet;
999 addedWindows.clear();
1000
Svetoslavf7174e82014-06-12 11:29:35 -07001001 boolean focusedWindowAdded = false;
1002
Svetoslav8e3feb12014-02-24 13:46:47 -08001003 final int visibleWindowCount = visibleWindows.size();
1004 for (int i = visibleWindowCount - 1; i >= 0; i--) {
Alan Viverette9538eea2014-11-13 14:49:20 -08001005 final WindowState windowState = visibleWindows.valueAt(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001006 final int flags = windowState.mAttrs.flags;
1007
Svetoslav3a5c7212014-10-14 09:54:26 -07001008 // If the window is not touchable - ignore.
Svetoslavf7174e82014-06-12 11:29:35 -07001009 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001010 continue;
1011 }
1012
Alan Viverette9538eea2014-11-13 14:49:20 -08001013 // Compute the bounds in the screen.
1014 final Rect boundsInScreen = mTempRect;
1015 computeWindowBoundsInScreen(windowState, boundsInScreen);
1016
Svetoslav8e3feb12014-02-24 13:46:47 -08001017 // If the window is completely covered by other windows - ignore.
1018 if (unaccountedSpace.quickReject(boundsInScreen)) {
1019 continue;
1020 }
1021
1022 // Add windows of certain types not covered by modal windows.
1023 if (isReportedWindowType(windowState.mAttrs.type)) {
1024 // Add the window to the ones to be reported.
Svetoslavf7174e82014-06-12 11:29:35 -07001025 WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
Svetoslav8e3feb12014-02-24 13:46:47 -08001026 addedWindows.add(window.token);
Svetoslav8e3feb12014-02-24 13:46:47 -08001027 windows.add(window);
Svetoslavf7174e82014-06-12 11:29:35 -07001028 if (windowState.isFocused()) {
1029 focusedWindowAdded = true;
1030 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001031 }
1032
Alan Viveretted0c73f42014-11-18 10:25:04 -08001033 // Account for the space this window takes if the window
1034 // is not an accessibility overlay which does not change
1035 // the reported windows.
1036 if (windowState.mAttrs.type !=
1037 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY) {
1038 unaccountedSpace.op(boundsInScreen, unaccountedSpace,
1039 Region.Op.REVERSE_DIFFERENCE);
1040 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001041
1042 // We figured out what is touchable for the entire screen - done.
1043 if (unaccountedSpace.isEmpty()) {
1044 break;
1045 }
1046
1047 // If a window is modal, no other below can be touched - done.
1048 if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1049 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
1050 break;
1051 }
1052 }
1053
Svetoslavf7174e82014-06-12 11:29:35 -07001054 // Always report the focused window.
1055 if (!focusedWindowAdded) {
1056 for (int i = visibleWindowCount - 1; i >= 0; i--) {
1057 WindowState windowState = visibleWindows.valueAt(i);
1058 if (windowState.isFocused()) {
1059 // Compute the bounds in the screen.
1060 Rect boundsInScreen = mTempRect;
1061 computeWindowBoundsInScreen(windowState, boundsInScreen);
1062
1063 // Add the window to the ones to be reported.
1064 WindowInfo window = obtainPopulatedWindowInfo(windowState,
1065 boundsInScreen);
1066 addedWindows.add(window.token);
1067 windows.add(window);
1068 break;
1069 }
1070 }
1071 }
1072
Svetoslav8e3feb12014-02-24 13:46:47 -08001073 // Remove child/parent references to windows that were not added.
1074 final int windowCount = windows.size();
1075 for (int i = 0; i < windowCount; i++) {
1076 WindowInfo window = windows.get(i);
1077 if (!addedWindows.contains(window.parentToken)) {
1078 window.parentToken = null;
1079 }
1080 if (window.childTokens != null) {
1081 final int childTokenCount = window.childTokens.size();
1082 for (int j = childTokenCount - 1; j >= 0; j--) {
1083 if (!addedWindows.contains(window.childTokens.get(j))) {
1084 window.childTokens.remove(j);
1085 }
1086 }
1087 // Leave the child token list if empty.
1088 }
1089 }
1090
1091 visibleWindows.clear();
1092 addedWindows.clear();
1093
1094 // We computed the windows and if they changed notify the client.
Svetoslav8e3feb12014-02-24 13:46:47 -08001095 if (mOldWindows.size() != windows.size()) {
1096 // Different size means something changed.
1097 windowsChanged = true;
1098 } else if (!mOldWindows.isEmpty() || !windows.isEmpty()) {
1099 // Since we always traverse windows from high to low layer
1100 // the old and new windows at the same index should be the
1101 // same, otherwise something changed.
1102 for (int i = 0; i < windowCount; i++) {
1103 WindowInfo oldWindow = mOldWindows.get(i);
1104 WindowInfo newWindow = windows.get(i);
1105 // We do not care for layer changes given the window
1106 // order does not change. This brings no new information
1107 // to the clients.
1108 if (windowChangedNoLayer(oldWindow, newWindow)) {
1109 windowsChanged = true;
1110 break;
1111 }
1112 }
1113 }
1114
1115 if (windowsChanged) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001116 cacheWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001117 }
1118 }
Svetoslav3a0d8782014-12-04 12:50:11 -08001119
1120 // Now we do not hold the lock, so send the windows over.
1121 if (windowsChanged) {
1122 if (DEBUG) {
1123 Log.i(LOG_TAG, "Windows changed:" + windows);
1124 }
1125 mCallback.onWindowsForAccessibilityChanged(windows);
1126 } else {
1127 if (DEBUG) {
1128 Log.i(LOG_TAG, "No windows changed.");
1129 }
1130 }
1131
1132 // Recycle the windows as we do not need them.
1133 clearAndRecycleWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001134 }
1135
Svetoslavf7174e82014-06-12 11:29:35 -07001136 private void computeWindowBoundsInScreen(WindowState windowState, Rect outBounds) {
1137 // Get the touchable frame.
1138 Region touchableRegion = mTempRegion1;
1139 windowState.getTouchableRegion(touchableRegion);
1140 Rect touchableFrame = mTempRect;
1141 touchableRegion.getBounds(touchableFrame);
1142
1143 // Move to origin as all transforms are captured by the matrix.
1144 RectF windowFrame = mTempRectF;
1145 windowFrame.set(touchableFrame);
1146 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
1147
1148 // Map the frame to get what appears on the screen.
1149 Matrix matrix = mTempMatrix;
1150 populateTransformationMatrixLocked(windowState, matrix);
1151 matrix.mapRect(windowFrame);
1152
1153 // Got the bounds.
1154 outBounds.set((int) windowFrame.left, (int) windowFrame.top,
1155 (int) windowFrame.right, (int) windowFrame.bottom);
1156 }
1157
1158 private static WindowInfo obtainPopulatedWindowInfo(WindowState windowState,
1159 Rect boundsInScreen) {
1160 WindowInfo window = WindowInfo.obtain();
1161 window.type = windowState.mAttrs.type;
1162 window.layer = windowState.mLayer;
1163 window.token = windowState.mClient.asBinder();
1164
1165 WindowState attachedWindow = windowState.mAttachedWindow;
1166 if (attachedWindow != null) {
1167 window.parentToken = attachedWindow.mClient.asBinder();
1168 }
1169
1170 window.focused = windowState.isFocused();
1171 window.boundsInScreen.set(boundsInScreen);
1172
1173 final int childCount = windowState.mChildWindows.size();
1174 if (childCount > 0) {
1175 if (window.childTokens == null) {
1176 window.childTokens = new ArrayList<IBinder>();
1177 }
1178 for (int j = 0; j < childCount; j++) {
1179 WindowState child = windowState.mChildWindows.get(j);
1180 window.childTokens.add(child.mClient.asBinder());
1181 }
1182 }
1183
1184 return window;
1185 }
1186
Svetoslav8e3feb12014-02-24 13:46:47 -08001187 private void cacheWindows(List<WindowInfo> windows) {
1188 final int oldWindowCount = mOldWindows.size();
1189 for (int i = oldWindowCount - 1; i >= 0; i--) {
1190 mOldWindows.remove(i).recycle();
1191 }
1192 final int newWindowCount = windows.size();
1193 for (int i = 0; i < newWindowCount; i++) {
1194 WindowInfo newWindow = windows.get(i);
1195 mOldWindows.add(WindowInfo.obtain(newWindow));
1196 }
1197 }
1198
1199 private boolean windowChangedNoLayer(WindowInfo oldWindow, WindowInfo newWindow) {
1200 if (oldWindow == newWindow) {
1201 return false;
1202 }
Svetoslavf7174e82014-06-12 11:29:35 -07001203 if (oldWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001204 return true;
1205 }
Svetoslavf7174e82014-06-12 11:29:35 -07001206 if (newWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001207 return true;
1208 }
1209 if (oldWindow.type != newWindow.type) {
1210 return true;
1211 }
1212 if (oldWindow.focused != newWindow.focused) {
1213 return true;
1214 }
1215 if (oldWindow.token == null) {
1216 if (newWindow.token != null) {
1217 return true;
1218 }
1219 } else if (!oldWindow.token.equals(newWindow.token)) {
1220 return true;
1221 }
1222 if (oldWindow.parentToken == null) {
1223 if (newWindow.parentToken != null) {
1224 return true;
1225 }
1226 } else if (!oldWindow.parentToken.equals(newWindow.parentToken)) {
1227 return true;
1228 }
1229 if (!oldWindow.boundsInScreen.equals(newWindow.boundsInScreen)) {
1230 return true;
1231 }
1232 if (oldWindow.childTokens != null && newWindow.childTokens != null
1233 && !oldWindow.childTokens.equals(newWindow.childTokens)) {
1234 return true;
1235 }
1236 return false;
1237 }
1238
Svetoslav3a0d8782014-12-04 12:50:11 -08001239 private static void clearAndRecycleWindows(List<WindowInfo> windows) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001240 final int windowCount = windows.size();
1241 for (int i = windowCount - 1; i >= 0; i--) {
1242 windows.remove(i).recycle();
1243 }
1244 }
1245
1246 private static boolean isReportedWindowType(int windowType) {
1247 return (windowType != WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM
1248 && windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
1249 && windowType != WindowManager.LayoutParams.TYPE_BOOT_PROGRESS
1250 && windowType != WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY
1251 && windowType != WindowManager.LayoutParams.TYPE_DRAG
1252 && windowType != WindowManager.LayoutParams.TYPE_HIDDEN_NAV_CONSUMER
1253 && windowType != WindowManager.LayoutParams.TYPE_POINTER
Svetoslav8e3feb12014-02-24 13:46:47 -08001254 && windowType != WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY
1255 && windowType != WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY
1256 && windowType != WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY
1257 && windowType != WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION);
1258 }
1259
1260 private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
1261 DisplayContent displayContent = mWindowManagerService
1262 .getDefaultDisplayContentLocked();
1263 WindowList windowList = displayContent.getWindowList();
1264 final int windowCount = windowList.size();
1265 for (int i = 0; i < windowCount; i++) {
1266 WindowState windowState = windowList.get(i);
1267 if (windowState.isVisibleLw()) {
1268 outWindows.put(windowState.mLayer, windowState);
1269 }
1270 }
1271 }
1272
1273 private class MyHandler extends Handler {
Svetoslavf7174e82014-06-12 11:29:35 -07001274 public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -08001275
1276 public MyHandler(Looper looper) {
1277 super(looper, null, false);
1278 }
1279
1280 @Override
1281 @SuppressWarnings("unchecked")
1282 public void handleMessage(Message message) {
1283 switch (message.what) {
Svetoslavf7174e82014-06-12 11:29:35 -07001284 case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1285 computeChangedWindows();
1286 } break;
Svetoslav8e3feb12014-02-24 13:46:47 -08001287 }
1288 }
1289 }
1290 }
1291}