blob: 98d44ac3bffefb71c1f2c4f10c157b793b10d4ff [file] [log] [blame]
Svetoslav8e3feb12014-02-24 13:46:47 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080019import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
21
Svetoslav8e3feb12014-02-24 13:46:47 -080022import android.animation.ObjectAnimator;
23import android.animation.ValueAnimator;
Alan Viverette59e53a12016-03-28 13:41:32 -040024import android.annotation.NonNull;
Svetoslav8e3feb12014-02-24 13:46:47 -080025import android.app.Service;
26import android.content.Context;
27import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.Path;
32import android.graphics.PixelFormat;
33import android.graphics.Point;
34import android.graphics.PorterDuff.Mode;
35import android.graphics.Rect;
36import android.graphics.RectF;
37import android.graphics.Region;
38import android.os.Handler;
39import android.os.IBinder;
40import android.os.Looper;
41import android.os.Message;
42import android.util.ArraySet;
43import android.util.Log;
44import android.util.Slog;
45import android.util.SparseArray;
46import android.util.TypedValue;
47import android.view.MagnificationSpec;
48import android.view.Surface;
49import android.view.Surface.OutOfResourcesException;
50import android.view.SurfaceControl;
Svetoslavf7174e82014-06-12 11:29:35 -070051import android.view.ViewConfiguration;
Svetoslav8e3feb12014-02-24 13:46:47 -080052import android.view.WindowInfo;
53import android.view.WindowManager;
54import android.view.WindowManagerInternal.MagnificationCallbacks;
55import android.view.WindowManagerInternal.WindowsForAccessibilityCallback;
56import android.view.WindowManagerPolicy;
57import android.view.animation.DecelerateInterpolator;
58import android.view.animation.Interpolator;
59
60import com.android.internal.R;
61import com.android.internal.os.SomeArgs;
62
63import java.util.ArrayList;
Allen Hairf20ac2c2016-02-11 17:42:59 -080064import java.util.HashSet;
Svetoslav8e3feb12014-02-24 13:46:47 -080065import java.util.List;
66import java.util.Set;
67
68/**
69 * This class contains the accessibility related logic of the window manger.
70 */
71final class AccessibilityController {
72
73 private final WindowManagerService mWindowManagerService;
74
75 private static final float[] sTempFloats = new float[9];
76
77 public AccessibilityController(WindowManagerService service) {
78 mWindowManagerService = service;
79 }
80
81 private DisplayMagnifier mDisplayMagnifier;
82
83 private WindowsForAccessibilityObserver mWindowsForAccessibilityObserver;
84
85 public void setMagnificationCallbacksLocked(MagnificationCallbacks callbacks) {
86 if (callbacks != null) {
87 if (mDisplayMagnifier != null) {
88 throw new IllegalStateException("Magnification callbacks already set!");
89 }
90 mDisplayMagnifier = new DisplayMagnifier(mWindowManagerService, callbacks);
91 } else {
92 if (mDisplayMagnifier == null) {
93 throw new IllegalStateException("Magnification callbacks already cleared!");
94 }
95 mDisplayMagnifier.destroyLocked();
96 mDisplayMagnifier = null;
97 }
98 }
99
100 public void setWindowsForAccessibilityCallback(WindowsForAccessibilityCallback callback) {
101 if (callback != null) {
102 if (mWindowsForAccessibilityObserver != null) {
103 throw new IllegalStateException(
104 "Windows for accessibility callback already set!");
105 }
106 mWindowsForAccessibilityObserver = new WindowsForAccessibilityObserver(
107 mWindowManagerService, callback);
108 } else {
109 if (mWindowsForAccessibilityObserver == null) {
110 throw new IllegalStateException(
111 "Windows for accessibility callback already cleared!");
112 }
113 mWindowsForAccessibilityObserver = null;
114 }
115 }
116
117 public void setMagnificationSpecLocked(MagnificationSpec spec) {
118 if (mDisplayMagnifier != null) {
119 mDisplayMagnifier.setMagnificationSpecLocked(spec);
120 }
121 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700122 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800123 }
124 }
125
Alan Viverette59e53a12016-03-28 13:41:32 -0400126 public void getMagnificationRegionsLocked(Region outMagnified, Region outAvailable) {
127 if (mDisplayMagnifier != null) {
128 mDisplayMagnifier.getMagnificationRegionsLocked(outMagnified, outAvailable);
129 }
130 }
131
Svetoslavf7174e82014-06-12 11:29:35 -0700132 public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800133 if (mDisplayMagnifier != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700134 mDisplayMagnifier.onRectangleOnScreenRequestedLocked(rectangle);
Svetoslav8e3feb12014-02-24 13:46:47 -0800135 }
136 // Not relevant for the window observer.
137 }
138
139 public void onWindowLayersChangedLocked() {
140 if (mDisplayMagnifier != null) {
141 mDisplayMagnifier.onWindowLayersChangedLocked();
142 }
143 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700144 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800145 }
146 }
147
148 public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
149 if (mDisplayMagnifier != null) {
150 mDisplayMagnifier.onRotationChangedLocked(displayContent, rotation);
151 }
152 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700153 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800154 }
155 }
156
157 public void onAppWindowTransitionLocked(WindowState windowState, int transition) {
158 if (mDisplayMagnifier != null) {
159 mDisplayMagnifier.onAppWindowTransitionLocked(windowState, transition);
160 }
161 // Not relevant for the window observer.
162 }
163
164 public void onWindowTransitionLocked(WindowState windowState, int transition) {
165 if (mDisplayMagnifier != null) {
166 mDisplayMagnifier.onWindowTransitionLocked(windowState, transition);
167 }
168 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700169 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800170 }
171 }
172
Svetoslav3a0d8782014-12-04 12:50:11 -0800173 public void onWindowFocusChangedNotLocked() {
Svetoslav8e3feb12014-02-24 13:46:47 -0800174 // Not relevant for the display magnifier.
175
Svetoslav3a0d8782014-12-04 12:50:11 -0800176 WindowsForAccessibilityObserver observer = null;
177 synchronized (mWindowManagerService) {
178 observer = mWindowsForAccessibilityObserver;
179 }
180 if (observer != null) {
181 observer.performComputeChangedWindowsNotLocked();
Svetoslav8e3feb12014-02-24 13:46:47 -0800182 }
183 }
184
Svetoslav4604abc2014-06-10 18:59:30 -0700185
Svetoslavf7174e82014-06-12 11:29:35 -0700186 public void onSomeWindowResizedOrMovedLocked() {
Svetoslav4604abc2014-06-10 18:59:30 -0700187 // Not relevant for the display magnifier.
188
189 if (mWindowsForAccessibilityObserver != null) {
Svetoslavf7174e82014-06-12 11:29:35 -0700190 mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
Svetoslav4604abc2014-06-10 18:59:30 -0700191 }
192 }
193
Svetoslav8e3feb12014-02-24 13:46:47 -0800194 /** NOTE: This has to be called within a surface transaction. */
195 public void drawMagnifiedRegionBorderIfNeededLocked() {
196 if (mDisplayMagnifier != null) {
197 mDisplayMagnifier.drawMagnifiedRegionBorderIfNeededLocked();
198 }
199 // Not relevant for the window observer.
200 }
201
202 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
203 if (mDisplayMagnifier != null) {
204 return mDisplayMagnifier.getMagnificationSpecForWindowLocked(windowState);
205 }
206 return null;
207 }
208
209 public boolean hasCallbacksLocked() {
210 return (mDisplayMagnifier != null
211 || mWindowsForAccessibilityObserver != null);
212 }
213
214 private static void populateTransformationMatrixLocked(WindowState windowState,
215 Matrix outMatrix) {
216 sTempFloats[Matrix.MSCALE_X] = windowState.mWinAnimator.mDsDx;
217 sTempFloats[Matrix.MSKEW_Y] = windowState.mWinAnimator.mDtDx;
218 sTempFloats[Matrix.MSKEW_X] = windowState.mWinAnimator.mDsDy;
219 sTempFloats[Matrix.MSCALE_Y] = windowState.mWinAnimator.mDtDy;
Filip Gruszczynski2a6a2c22015-10-14 12:00:53 -0700220 sTempFloats[Matrix.MTRANS_X] = windowState.mShownPosition.x;
221 sTempFloats[Matrix.MTRANS_Y] = windowState.mShownPosition.y;
Svetoslav8e3feb12014-02-24 13:46:47 -0800222 sTempFloats[Matrix.MPERSP_0] = 0;
223 sTempFloats[Matrix.MPERSP_1] = 0;
224 sTempFloats[Matrix.MPERSP_2] = 1;
225 outMatrix.setValues(sTempFloats);
226 }
227
228 /**
229 * This class encapsulates the functionality related to display magnification.
230 */
231 private static final class DisplayMagnifier {
232
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800233 private static final String LOG_TAG = TAG_WITH_CLASS_NAME ? "DisplayMagnifier" : TAG_WM;
Svetoslav8e3feb12014-02-24 13:46:47 -0800234
235 private static final boolean DEBUG_WINDOW_TRANSITIONS = false;
236 private static final boolean DEBUG_ROTATION = false;
237 private static final boolean DEBUG_LAYERS = false;
238 private static final boolean DEBUG_RECTANGLE_REQUESTED = false;
239 private static final boolean DEBUG_VIEWPORT_WINDOW = false;
240
241 private final Rect mTempRect1 = new Rect();
242 private final Rect mTempRect2 = new Rect();
243
244 private final Region mTempRegion1 = new Region();
245 private final Region mTempRegion2 = new Region();
246 private final Region mTempRegion3 = new Region();
247 private final Region mTempRegion4 = new Region();
248
249 private final Context mContext;
250 private final WindowManagerService mWindowManagerService;
251 private final MagnifiedViewport mMagnifedViewport;
252 private final Handler mHandler;
253
254 private final MagnificationCallbacks mCallbacks;
255
256 private final long mLongAnimationDuration;
257
258 public DisplayMagnifier(WindowManagerService windowManagerService,
259 MagnificationCallbacks callbacks) {
260 mContext = windowManagerService.mContext;
261 mWindowManagerService = windowManagerService;
262 mCallbacks = callbacks;
263 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
264 mMagnifedViewport = new MagnifiedViewport();
265 mLongAnimationDuration = mContext.getResources().getInteger(
266 com.android.internal.R.integer.config_longAnimTime);
267 }
268
269 public void setMagnificationSpecLocked(MagnificationSpec spec) {
270 mMagnifedViewport.updateMagnificationSpecLocked(spec);
271 mMagnifedViewport.recomputeBoundsLocked();
272 mWindowManagerService.scheduleAnimationLocked();
273 }
274
Svetoslavf7174e82014-06-12 11:29:35 -0700275 public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800276 if (DEBUG_RECTANGLE_REQUESTED) {
277 Slog.i(LOG_TAG, "Rectangle on screen requested: " + rectangle);
278 }
279 if (!mMagnifedViewport.isMagnifyingLocked()) {
280 return;
281 }
282 Rect magnifiedRegionBounds = mTempRect2;
283 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(magnifiedRegionBounds);
284 if (magnifiedRegionBounds.contains(rectangle)) {
285 return;
286 }
287 SomeArgs args = SomeArgs.obtain();
288 args.argi1 = rectangle.left;
289 args.argi2 = rectangle.top;
290 args.argi3 = rectangle.right;
291 args.argi4 = rectangle.bottom;
292 mHandler.obtainMessage(MyHandler.MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED,
293 args).sendToTarget();
294 }
295
296 public void onWindowLayersChangedLocked() {
297 if (DEBUG_LAYERS) {
298 Slog.i(LOG_TAG, "Layers changed.");
299 }
300 mMagnifedViewport.recomputeBoundsLocked();
301 mWindowManagerService.scheduleAnimationLocked();
302 }
303
304 public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
305 if (DEBUG_ROTATION) {
306 Slog.i(LOG_TAG, "Rotaton: " + Surface.rotationToString(rotation)
307 + " displayId: " + displayContent.getDisplayId());
308 }
309 mMagnifedViewport.onRotationChangedLocked();
310 mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_ROTATION_CHANGED);
311 }
312
313 public void onAppWindowTransitionLocked(WindowState windowState, int transition) {
314 if (DEBUG_WINDOW_TRANSITIONS) {
315 Slog.i(LOG_TAG, "Window transition: "
316 + AppTransition.appTransitionToString(transition)
317 + " displayId: " + windowState.getDisplayId());
318 }
319 final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
320 if (magnifying) {
321 switch (transition) {
322 case AppTransition.TRANSIT_ACTIVITY_OPEN:
323 case AppTransition.TRANSIT_TASK_OPEN:
324 case AppTransition.TRANSIT_TASK_TO_FRONT:
325 case AppTransition.TRANSIT_WALLPAPER_OPEN:
326 case AppTransition.TRANSIT_WALLPAPER_CLOSE:
327 case AppTransition.TRANSIT_WALLPAPER_INTRA_OPEN: {
328 mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_USER_CONTEXT_CHANGED);
329 }
330 }
331 }
332 }
333
334 public void onWindowTransitionLocked(WindowState windowState, int transition) {
335 if (DEBUG_WINDOW_TRANSITIONS) {
336 Slog.i(LOG_TAG, "Window transition: "
337 + AppTransition.appTransitionToString(transition)
338 + " displayId: " + windowState.getDisplayId());
339 }
340 final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
341 final int type = windowState.mAttrs.type;
342 switch (transition) {
343 case WindowManagerPolicy.TRANSIT_ENTER:
344 case WindowManagerPolicy.TRANSIT_SHOW: {
345 if (!magnifying) {
346 break;
347 }
348 switch (type) {
349 case WindowManager.LayoutParams.TYPE_APPLICATION:
350 case WindowManager.LayoutParams.TYPE_APPLICATION_PANEL:
351 case WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA:
352 case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
Wale Ogunwale0a4dc222015-04-14 12:58:42 -0700353 case WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL:
Svetoslav8e3feb12014-02-24 13:46:47 -0800354 case WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG:
355 case WindowManager.LayoutParams.TYPE_SEARCH_BAR:
356 case WindowManager.LayoutParams.TYPE_PHONE:
357 case WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
358 case WindowManager.LayoutParams.TYPE_TOAST:
359 case WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY:
360 case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
361 case WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG:
362 case WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG:
363 case WindowManager.LayoutParams.TYPE_SYSTEM_ERROR:
364 case WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY:
Jason Monk8f7f3182015-11-18 16:35:14 -0500365 case WindowManager.LayoutParams.TYPE_QS_DIALOG:
Adrian Roos9a645132014-10-08 02:59:56 +0200366 case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL: {
Svetoslav8e3feb12014-02-24 13:46:47 -0800367 Rect magnifiedRegionBounds = mTempRect2;
368 mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(
369 magnifiedRegionBounds);
370 Rect touchableRegionBounds = mTempRect1;
371 windowState.getTouchableRegion(mTempRegion1);
372 mTempRegion1.getBounds(touchableRegionBounds);
373 if (!magnifiedRegionBounds.intersect(touchableRegionBounds)) {
374 mCallbacks.onRectangleOnScreenRequested(
375 touchableRegionBounds.left,
376 touchableRegionBounds.top,
377 touchableRegionBounds.right,
378 touchableRegionBounds.bottom);
379 }
380 } break;
381 } break;
382 }
383 }
384 }
385
386 public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
387 MagnificationSpec spec = mMagnifedViewport.getMagnificationSpecLocked();
388 if (spec != null && !spec.isNop()) {
389 WindowManagerPolicy policy = mWindowManagerService.mPolicy;
390 final int windowType = windowState.mAttrs.type;
391 if (!policy.isTopLevelWindow(windowType) && windowState.mAttachedWindow != null
392 && !policy.canMagnifyWindow(windowType)) {
393 return null;
394 }
395 if (!policy.canMagnifyWindow(windowState.mAttrs.type)) {
396 return null;
397 }
398 }
399 return spec;
400 }
401
Alan Viverette59e53a12016-03-28 13:41:32 -0400402 public void getMagnificationRegionsLocked(Region outMagnified, Region outAvailable) {
403 mMagnifedViewport.getBoundsLocked(outMagnified, outAvailable);
404 }
405
Svetoslav8e3feb12014-02-24 13:46:47 -0800406 public void destroyLocked() {
407 mMagnifedViewport.destroyWindow();
408 }
409
410 /** NOTE: This has to be called within a surface transaction. */
411 public void drawMagnifiedRegionBorderIfNeededLocked() {
412 mMagnifedViewport.drawWindowIfNeededLocked();
413 }
414
415 private final class MagnifiedViewport {
416
Svetoslav8e3feb12014-02-24 13:46:47 -0800417 private final SparseArray<WindowState> mTempWindowStates =
418 new SparseArray<WindowState>();
419
420 private final RectF mTempRectF = new RectF();
421
422 private final Point mTempPoint = new Point();
423
424 private final Matrix mTempMatrix = new Matrix();
425
426 private final Region mMagnifiedBounds = new Region();
Alan Viverette59e53a12016-03-28 13:41:32 -0400427 private final Region mAvailableBounds = new Region();
Svetoslav8e3feb12014-02-24 13:46:47 -0800428 private final Region mOldMagnifiedBounds = new Region();
Alan Viverette214fb682015-11-17 09:47:11 -0500429 private final Region mOldAvailableBounds = new Region();
Svetoslav8e3feb12014-02-24 13:46:47 -0800430
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800431 private final Path mCircularPath;
432
Svetoslav8e3feb12014-02-24 13:46:47 -0800433 private final MagnificationSpec mMagnificationSpec = MagnificationSpec.obtain();
434
435 private final WindowManager mWindowManager;
436
Svetoslav7505e332014-08-22 12:14:28 -0700437 private final float mBorderWidth;
Svetoslav8e3feb12014-02-24 13:46:47 -0800438 private final int mHalfBorderWidth;
Svetoslav7505e332014-08-22 12:14:28 -0700439 private final int mDrawBorderInset;
Svetoslav8e3feb12014-02-24 13:46:47 -0800440
441 private final ViewportWindow mWindow;
442
443 private boolean mFullRedrawNeeded;
444
445 public MagnifiedViewport() {
446 mWindowManager = (WindowManager) mContext.getSystemService(Service.WINDOW_SERVICE);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800447 mBorderWidth = mContext.getResources().getDimension(
448 com.android.internal.R.dimen.accessibility_magnification_indicator_width);
Svetoslav7505e332014-08-22 12:14:28 -0700449 mHalfBorderWidth = (int) Math.ceil(mBorderWidth / 2);
450 mDrawBorderInset = (int) mBorderWidth / 2;
Svetoslav8e3feb12014-02-24 13:46:47 -0800451 mWindow = new ViewportWindow(mContext);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800452
Adam Powell01f280d2015-05-18 16:07:42 -0700453 if (mContext.getResources().getConfiguration().isScreenRound()) {
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800454 mCircularPath = new Path();
455 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
456 final int centerXY = mTempPoint.x / 2;
457 mCircularPath.addCircle(centerXY, centerXY, centerXY, Path.Direction.CW);
458 } else {
459 mCircularPath = null;
460 }
461
Svetoslav8e3feb12014-02-24 13:46:47 -0800462 recomputeBoundsLocked();
463 }
464
Alan Viverette59e53a12016-03-28 13:41:32 -0400465 public void getBoundsLocked(@NonNull Region outMagnified,
466 @NonNull Region outAvailable) {
467 outMagnified.set(mMagnifiedBounds);
468 outAvailable.set(mAvailableBounds);
469 }
470
Svetoslav8e3feb12014-02-24 13:46:47 -0800471 public void updateMagnificationSpecLocked(MagnificationSpec spec) {
472 if (spec != null) {
473 mMagnificationSpec.initialize(spec.scale, spec.offsetX, spec.offsetY);
474 } else {
475 mMagnificationSpec.clear();
476 }
477 // If this message is pending we are in a rotation animation and do not want
478 // to show the border. We will do so when the pending message is handled.
Svetoslav7505e332014-08-22 12:14:28 -0700479 if (!mHandler.hasMessages(
480 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED)) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800481 setMagnifiedRegionBorderShownLocked(isMagnifyingLocked(), true);
482 }
483 }
484
485 public void recomputeBoundsLocked() {
486 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
487 final int screenWidth = mTempPoint.x;
488 final int screenHeight = mTempPoint.y;
489
Alan Viverette59e53a12016-03-28 13:41:32 -0400490 mMagnifiedBounds.set(0, 0, 0, 0);
491 mAvailableBounds.set(0, 0, screenWidth, screenHeight);
Svetoslav8e3feb12014-02-24 13:46:47 -0800492
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800493 if (mCircularPath != null) {
Alan Viverette59e53a12016-03-28 13:41:32 -0400494 mAvailableBounds.setPath(mCircularPath, mAvailableBounds);
Casey Burkhardtd29a1e42015-02-12 14:07:55 -0800495 }
496
Svetoslav8e3feb12014-02-24 13:46:47 -0800497 Region nonMagnifiedBounds = mTempRegion4;
Svet Ganovb21df802014-09-01 19:06:33 -0700498 nonMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800499
500 SparseArray<WindowState> visibleWindows = mTempWindowStates;
501 visibleWindows.clear();
502 populateWindowsOnScreenLocked(visibleWindows);
503
504 final int visibleWindowCount = visibleWindows.size();
505 for (int i = visibleWindowCount - 1; i >= 0; i--) {
506 WindowState windowState = visibleWindows.valueAt(i);
507 if (windowState.mAttrs.type == WindowManager
508 .LayoutParams.TYPE_MAGNIFICATION_OVERLAY) {
509 continue;
510 }
511
512 Region windowBounds = mTempRegion2;
513 Matrix matrix = mTempMatrix;
514 populateTransformationMatrixLocked(windowState, matrix);
515 RectF windowFrame = mTempRectF;
516
517 if (mWindowManagerService.mPolicy.canMagnifyWindow(windowState.mAttrs.type)) {
518 windowFrame.set(windowState.mFrame);
519 windowFrame.offset(-windowFrame.left, -windowFrame.top);
520 matrix.mapRect(windowFrame);
521 windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
522 (int) windowFrame.right, (int) windowFrame.bottom);
Alan Viverette59e53a12016-03-28 13:41:32 -0400523 mMagnifiedBounds.op(windowBounds, Region.Op.UNION);
524 mMagnifiedBounds.op(mAvailableBounds, Region.Op.INTERSECT);
Svetoslav8e3feb12014-02-24 13:46:47 -0800525 } else {
526 Region touchableRegion = mTempRegion3;
527 windowState.getTouchableRegion(touchableRegion);
528 Rect touchableFrame = mTempRect1;
529 touchableRegion.getBounds(touchableFrame);
530 windowFrame.set(touchableFrame);
531 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
532 matrix.mapRect(windowFrame);
533 windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
534 (int) windowFrame.right, (int) windowFrame.bottom);
535 nonMagnifiedBounds.op(windowBounds, Region.Op.UNION);
Alan Viverette59e53a12016-03-28 13:41:32 -0400536 windowBounds.op(mMagnifiedBounds, Region.Op.DIFFERENCE);
537 mAvailableBounds.op(windowBounds, Region.Op.DIFFERENCE);
Svetoslav8e3feb12014-02-24 13:46:47 -0800538 }
539
540 Region accountedBounds = mTempRegion2;
Alan Viverette59e53a12016-03-28 13:41:32 -0400541 accountedBounds.set(mMagnifiedBounds);
Svetoslav8e3feb12014-02-24 13:46:47 -0800542 accountedBounds.op(nonMagnifiedBounds, Region.Op.UNION);
543 accountedBounds.op(0, 0, screenWidth, screenHeight, Region.Op.INTERSECT);
544
545 if (accountedBounds.isRect()) {
546 Rect accountedFrame = mTempRect1;
547 accountedBounds.getBounds(accountedFrame);
548 if (accountedFrame.width() == screenWidth
549 && accountedFrame.height() == screenHeight) {
550 break;
551 }
552 }
553 }
554
555 visibleWindows.clear();
556
Alan Viverette59e53a12016-03-28 13:41:32 -0400557 mMagnifiedBounds.op(mDrawBorderInset, mDrawBorderInset,
Svetoslav7505e332014-08-22 12:14:28 -0700558 screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset,
Svetoslav8e3feb12014-02-24 13:46:47 -0800559 Region.Op.INTERSECT);
560
Alan Viverette59e53a12016-03-28 13:41:32 -0400561 final boolean magnifiedChanged = !mOldMagnifiedBounds.equals(mMagnifiedBounds);
562 final boolean availableChanged = !mOldAvailableBounds.equals(mAvailableBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500563 if (magnifiedChanged || availableChanged) {
564 if (magnifiedChanged) {
Alan Viverette59e53a12016-03-28 13:41:32 -0400565 mWindow.setBounds(mMagnifiedBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500566 Rect dirtyRect = mTempRect1;
567 if (mFullRedrawNeeded) {
568 mFullRedrawNeeded = false;
569 dirtyRect.set(mDrawBorderInset, mDrawBorderInset,
570 screenWidth - mDrawBorderInset,
571 screenHeight - mDrawBorderInset);
572 mWindow.invalidate(dirtyRect);
573 } else {
574 Region dirtyRegion = mTempRegion3;
Alan Viverette59e53a12016-03-28 13:41:32 -0400575 dirtyRegion.set(mMagnifiedBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500576 dirtyRegion.op(mOldMagnifiedBounds, Region.Op.UNION);
577 dirtyRegion.op(nonMagnifiedBounds, Region.Op.INTERSECT);
578 dirtyRegion.getBounds(dirtyRect);
579 mWindow.invalidate(dirtyRect);
580 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800581
Alan Viverette59e53a12016-03-28 13:41:32 -0400582 mOldMagnifiedBounds.set(mMagnifiedBounds);
Svetoslav8e3feb12014-02-24 13:46:47 -0800583 }
584
Alan Viverette214fb682015-11-17 09:47:11 -0500585 if (availableChanged) {
Alan Viverette59e53a12016-03-28 13:41:32 -0400586 mOldAvailableBounds.set(mAvailableBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500587 }
588
589 final SomeArgs args = SomeArgs.obtain();
Alan Viverette59e53a12016-03-28 13:41:32 -0400590 args.arg1 = Region.obtain(mMagnifiedBounds);
591 args.arg2 = Region.obtain(mAvailableBounds);
Alan Viverette214fb682015-11-17 09:47:11 -0500592 mHandler.obtainMessage(
593 MyHandler.MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED, args).sendToTarget();
Svetoslav8e3feb12014-02-24 13:46:47 -0800594 }
595 }
596
597 public void onRotationChangedLocked() {
598 // If we are magnifying, hide the magnified border window immediately so
599 // the user does not see strange artifacts during rotation. The screenshot
600 // used for rotation has already the border. After the rotation is complete
601 // we will show the border.
602 if (isMagnifyingLocked()) {
603 setMagnifiedRegionBorderShownLocked(false, false);
604 final long delay = (long) (mLongAnimationDuration
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700605 * mWindowManagerService.getWindowAnimationScaleLocked());
Svetoslav8e3feb12014-02-24 13:46:47 -0800606 Message message = mHandler.obtainMessage(
607 MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED);
608 mHandler.sendMessageDelayed(message, delay);
609 }
610 recomputeBoundsLocked();
611 mWindow.updateSize();
612 }
613
614 public void setMagnifiedRegionBorderShownLocked(boolean shown, boolean animate) {
615 if (shown) {
616 mFullRedrawNeeded = true;
Svet Ganovb21df802014-09-01 19:06:33 -0700617 mOldMagnifiedBounds.set(0, 0, 0, 0);
Svetoslav8e3feb12014-02-24 13:46:47 -0800618 }
619 mWindow.setShown(shown, animate);
620 }
621
622 public void getMagnifiedFrameInContentCoordsLocked(Rect rect) {
623 MagnificationSpec spec = mMagnificationSpec;
624 mMagnifiedBounds.getBounds(rect);
625 rect.offset((int) -spec.offsetX, (int) -spec.offsetY);
626 rect.scale(1.0f / spec.scale);
627 }
628
629 public boolean isMagnifyingLocked() {
630 return mMagnificationSpec.scale > 1.0f;
631 }
632
633 public MagnificationSpec getMagnificationSpecLocked() {
634 return mMagnificationSpec;
635 }
636
637 /** NOTE: This has to be called within a surface transaction. */
638 public void drawWindowIfNeededLocked() {
639 recomputeBoundsLocked();
640 mWindow.drawIfNeeded();
641 }
642
643 public void destroyWindow() {
644 mWindow.releaseSurface();
645 }
646
647 private void populateWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
648 DisplayContent displayContent = mWindowManagerService
649 .getDefaultDisplayContentLocked();
650 WindowList windowList = displayContent.getWindowList();
651 final int windowCount = windowList.size();
652 for (int i = 0; i < windowCount; i++) {
653 WindowState windowState = windowList.get(i);
Craig Mautner165be0c2015-01-27 15:16:58 -0800654 if (windowState.isOnScreen() &&
655 !windowState.mWinAnimator.mEnterAnimationPending) {
Svetoslav8e3feb12014-02-24 13:46:47 -0800656 outWindows.put(windowState.mLayer, windowState);
657 }
658 }
659 }
660
661 private final class ViewportWindow {
662 private static final String SURFACE_TITLE = "Magnification Overlay";
663
Svetoslav8e3feb12014-02-24 13:46:47 -0800664 private final Region mBounds = new Region();
665 private final Rect mDirtyRect = new Rect();
666 private final Paint mPaint = new Paint();
667
Svetoslav8e3feb12014-02-24 13:46:47 -0800668 private final SurfaceControl mSurfaceControl;
669 private final Surface mSurface = new Surface();
670
Svet Ganovb21df802014-09-01 19:06:33 -0700671 private final AnimationController mAnimationController;
672
Svetoslav8e3feb12014-02-24 13:46:47 -0800673 private boolean mShown;
674 private int mAlpha;
675
676 private boolean mInvalidated;
677
678 public ViewportWindow(Context context) {
679 SurfaceControl surfaceControl = null;
680 try {
681 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
682 surfaceControl = new SurfaceControl(mWindowManagerService.mFxSession,
683 SURFACE_TITLE, mTempPoint.x, mTempPoint.y, PixelFormat.TRANSLUCENT,
684 SurfaceControl.HIDDEN);
685 } catch (OutOfResourcesException oore) {
686 /* ignore */
687 }
688 mSurfaceControl = surfaceControl;
689 mSurfaceControl.setLayerStack(mWindowManager.getDefaultDisplay()
690 .getLayerStack());
691 mSurfaceControl.setLayer(mWindowManagerService.mPolicy.windowTypeToLayerLw(
692 WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY)
693 * WindowManagerService.TYPE_LAYER_MULTIPLIER);
694 mSurfaceControl.setPosition(0, 0);
695 mSurface.copyFrom(mSurfaceControl);
696
Svet Ganovb21df802014-09-01 19:06:33 -0700697 mAnimationController = new AnimationController(context,
698 mWindowManagerService.mH.getLooper());
699
Svetoslav8e3feb12014-02-24 13:46:47 -0800700 TypedValue typedValue = new TypedValue();
701 context.getTheme().resolveAttribute(R.attr.colorActivatedHighlight,
702 typedValue, true);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700703 final int borderColor = context.getColor(typedValue.resourceId);
Svetoslav8e3feb12014-02-24 13:46:47 -0800704
705 mPaint.setStyle(Paint.Style.STROKE);
706 mPaint.setStrokeWidth(mBorderWidth);
707 mPaint.setColor(borderColor);
708
Svetoslav8e3feb12014-02-24 13:46:47 -0800709 mInvalidated = true;
710 }
711
712 public void setShown(boolean shown, boolean animate) {
713 synchronized (mWindowManagerService.mWindowMap) {
714 if (mShown == shown) {
715 return;
716 }
717 mShown = shown;
Svet Ganovb21df802014-09-01 19:06:33 -0700718 mAnimationController.onFrameShownStateChanged(shown, animate);
Svetoslav8e3feb12014-02-24 13:46:47 -0800719 if (DEBUG_VIEWPORT_WINDOW) {
720 Slog.i(LOG_TAG, "ViewportWindow shown: " + mShown);
721 }
722 }
723 }
724
725 @SuppressWarnings("unused")
726 // Called reflectively from an animator.
727 public int getAlpha() {
728 synchronized (mWindowManagerService.mWindowMap) {
729 return mAlpha;
730 }
731 }
732
733 public void setAlpha(int alpha) {
734 synchronized (mWindowManagerService.mWindowMap) {
735 if (mAlpha == alpha) {
736 return;
737 }
738 mAlpha = alpha;
739 invalidate(null);
740 if (DEBUG_VIEWPORT_WINDOW) {
741 Slog.i(LOG_TAG, "ViewportWindow set alpha: " + alpha);
742 }
743 }
744 }
745
746 public void setBounds(Region bounds) {
747 synchronized (mWindowManagerService.mWindowMap) {
748 if (mBounds.equals(bounds)) {
749 return;
750 }
751 mBounds.set(bounds);
752 invalidate(mDirtyRect);
753 if (DEBUG_VIEWPORT_WINDOW) {
754 Slog.i(LOG_TAG, "ViewportWindow set bounds: " + bounds);
755 }
756 }
757 }
758
759 public void updateSize() {
760 synchronized (mWindowManagerService.mWindowMap) {
761 mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
762 mSurfaceControl.setSize(mTempPoint.x, mTempPoint.y);
763 invalidate(mDirtyRect);
764 }
765 }
766
767 public void invalidate(Rect dirtyRect) {
768 if (dirtyRect != null) {
769 mDirtyRect.set(dirtyRect);
770 } else {
771 mDirtyRect.setEmpty();
772 }
773 mInvalidated = true;
774 mWindowManagerService.scheduleAnimationLocked();
775 }
776
777 /** NOTE: This has to be called within a surface transaction. */
778 public void drawIfNeeded() {
779 synchronized (mWindowManagerService.mWindowMap) {
780 if (!mInvalidated) {
781 return;
782 }
783 mInvalidated = false;
784 Canvas canvas = null;
785 try {
786 // Empty dirty rectangle means unspecified.
787 if (mDirtyRect.isEmpty()) {
788 mBounds.getBounds(mDirtyRect);
789 }
790 mDirtyRect.inset(- mHalfBorderWidth, - mHalfBorderWidth);
791 canvas = mSurface.lockCanvas(mDirtyRect);
792 if (DEBUG_VIEWPORT_WINDOW) {
793 Slog.i(LOG_TAG, "Dirty rect: " + mDirtyRect);
794 }
795 } catch (IllegalArgumentException iae) {
796 /* ignore */
797 } catch (Surface.OutOfResourcesException oore) {
798 /* ignore */
799 }
800 if (canvas == null) {
801 return;
802 }
803 if (DEBUG_VIEWPORT_WINDOW) {
804 Slog.i(LOG_TAG, "Bounds: " + mBounds);
805 }
806 canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
807 mPaint.setAlpha(mAlpha);
808 Path path = mBounds.getBoundaryPath();
809 canvas.drawPath(path, mPaint);
810
811 mSurface.unlockCanvasAndPost(canvas);
812
813 if (mAlpha > 0) {
814 mSurfaceControl.show();
815 } else {
816 mSurfaceControl.hide();
817 }
818 }
819 }
820
821 public void releaseSurface() {
822 mSurfaceControl.release();
823 mSurface.release();
824 }
Svet Ganovb21df802014-09-01 19:06:33 -0700825
826 private final class AnimationController extends Handler {
827 private static final String PROPERTY_NAME_ALPHA = "alpha";
828
829 private static final int MIN_ALPHA = 0;
830 private static final int MAX_ALPHA = 255;
831
832 private static final int MSG_FRAME_SHOWN_STATE_CHANGED = 1;
833
834 private final ValueAnimator mShowHideFrameAnimator;
835
836 public AnimationController(Context context, Looper looper) {
837 super(looper);
838 mShowHideFrameAnimator = ObjectAnimator.ofInt(ViewportWindow.this,
839 PROPERTY_NAME_ALPHA, MIN_ALPHA, MAX_ALPHA);
840
841 Interpolator interpolator = new DecelerateInterpolator(2.5f);
842 final long longAnimationDuration = context.getResources().getInteger(
843 com.android.internal.R.integer.config_longAnimTime);
844
845 mShowHideFrameAnimator.setInterpolator(interpolator);
846 mShowHideFrameAnimator.setDuration(longAnimationDuration);
847 }
848
849 public void onFrameShownStateChanged(boolean shown, boolean animate) {
850 obtainMessage(MSG_FRAME_SHOWN_STATE_CHANGED,
851 shown ? 1 : 0, animate ? 1 : 0).sendToTarget();
852 }
853
854 @Override
855 public void handleMessage(Message message) {
856 switch (message.what) {
857 case MSG_FRAME_SHOWN_STATE_CHANGED: {
858 final boolean shown = message.arg1 == 1;
859 final boolean animate = message.arg2 == 1;
860
861 if (animate) {
862 if (mShowHideFrameAnimator.isRunning()) {
863 mShowHideFrameAnimator.reverse();
864 } else {
865 if (shown) {
866 mShowHideFrameAnimator.start();
867 } else {
868 mShowHideFrameAnimator.reverse();
869 }
870 }
871 } else {
872 mShowHideFrameAnimator.cancel();
873 if (shown) {
874 setAlpha(MAX_ALPHA);
875 } else {
876 setAlpha(MIN_ALPHA);
877 }
878 }
879 } break;
880 }
881 }
882 }
Svetoslav8e3feb12014-02-24 13:46:47 -0800883 }
884 }
885
886 private class MyHandler extends Handler {
887 public static final int MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED = 1;
888 public static final int MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED = 2;
889 public static final int MESSAGE_NOTIFY_USER_CONTEXT_CHANGED = 3;
890 public static final int MESSAGE_NOTIFY_ROTATION_CHANGED = 4;
891 public static final int MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED = 5;
892
893 public MyHandler(Looper looper) {
894 super(looper);
895 }
896
897 @Override
898 public void handleMessage(Message message) {
899 switch (message.what) {
900 case MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED: {
Alan Viverette214fb682015-11-17 09:47:11 -0500901 final SomeArgs args = (SomeArgs) message.obj;
902 final Region magnifiedBounds = (Region) args.arg1;
903 final Region availableBounds = (Region) args.arg2;
904 mCallbacks.onMagnifiedBoundsChanged(magnifiedBounds, availableBounds);
905 magnifiedBounds.recycle();
906 availableBounds.recycle();
Svetoslav8e3feb12014-02-24 13:46:47 -0800907 } break;
908
909 case MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED: {
910 SomeArgs args = (SomeArgs) message.obj;
911 final int left = args.argi1;
912 final int top = args.argi2;
913 final int right = args.argi3;
914 final int bottom = args.argi4;
915 mCallbacks.onRectangleOnScreenRequested(left, top, right, bottom);
916 args.recycle();
917 } break;
918
919 case MESSAGE_NOTIFY_USER_CONTEXT_CHANGED: {
920 mCallbacks.onUserContextChanged();
921 } break;
922
923 case MESSAGE_NOTIFY_ROTATION_CHANGED: {
924 final int rotation = message.arg1;
925 mCallbacks.onRotationChanged(rotation);
926 } break;
927
928 case MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED : {
929 synchronized (mWindowManagerService.mWindowMap) {
930 if (mMagnifedViewport.isMagnifyingLocked()) {
931 mMagnifedViewport.setMagnifiedRegionBorderShownLocked(true, true);
932 mWindowManagerService.scheduleAnimationLocked();
933 }
934 }
935 } break;
936 }
937 }
938 }
939 }
940
941 /**
942 * This class encapsulates the functionality related to computing the windows
943 * reported for accessibility purposes. These windows are all windows a sighted
944 * user can see on the screen.
945 */
946 private static final class WindowsForAccessibilityObserver {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800947 private static final String LOG_TAG = TAG_WITH_CLASS_NAME ?
948 "WindowsForAccessibilityObserver" : TAG_WM;
Svetoslav8e3feb12014-02-24 13:46:47 -0800949
950 private static final boolean DEBUG = false;
951
952 private final SparseArray<WindowState> mTempWindowStates =
953 new SparseArray<WindowState>();
954
955 private final List<WindowInfo> mOldWindows = new ArrayList<WindowInfo>();
956
957 private final Set<IBinder> mTempBinderSet = new ArraySet<IBinder>();
958
959 private final RectF mTempRectF = new RectF();
960
961 private final Matrix mTempMatrix = new Matrix();
962
963 private final Point mTempPoint = new Point();
964
965 private final Rect mTempRect = new Rect();
966
967 private final Region mTempRegion = new Region();
968
969 private final Region mTempRegion1 = new Region();
970
971 private final Context mContext;
972
973 private final WindowManagerService mWindowManagerService;
974
975 private final Handler mHandler;
976
977 private final WindowsForAccessibilityCallback mCallback;
978
Svetoslavf7174e82014-06-12 11:29:35 -0700979 private final long mRecurringAccessibilityEventsIntervalMillis;
980
Svetoslav8e3feb12014-02-24 13:46:47 -0800981 public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
982 WindowsForAccessibilityCallback callback) {
983 mContext = windowManagerService.mContext;
984 mWindowManagerService = windowManagerService;
985 mCallback = callback;
986 mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
Svetoslavf7174e82014-06-12 11:29:35 -0700987 mRecurringAccessibilityEventsIntervalMillis = ViewConfiguration
988 .getSendRecurringAccessibilityEventsInterval();
Svetoslav8e3feb12014-02-24 13:46:47 -0800989 computeChangedWindows();
990 }
991
Svetoslav3a0d8782014-12-04 12:50:11 -0800992 public void performComputeChangedWindowsNotLocked() {
993 mHandler.removeMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS);
994 computeChangedWindows();
995 }
996
Svetoslavf7174e82014-06-12 11:29:35 -0700997 public void scheduleComputeChangedWindowsLocked() {
Svetoslav3a0d8782014-12-04 12:50:11 -0800998 if (!mHandler.hasMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS)) {
Svetoslavf7174e82014-06-12 11:29:35 -0700999 mHandler.sendEmptyMessageDelayed(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS,
1000 mRecurringAccessibilityEventsIntervalMillis);
1001 }
1002 }
1003
Svetoslav8e3feb12014-02-24 13:46:47 -08001004 public void computeChangedWindows() {
1005 if (DEBUG) {
1006 Slog.i(LOG_TAG, "computeChangedWindows()");
1007 }
1008
Svetoslav3a0d8782014-12-04 12:50:11 -08001009 boolean windowsChanged = false;
1010 List<WindowInfo> windows = new ArrayList<WindowInfo>();
1011
Svetoslav8e3feb12014-02-24 13:46:47 -08001012 synchronized (mWindowManagerService.mWindowMap) {
Svetoslavf7174e82014-06-12 11:29:35 -07001013 // Do not send the windows if there is no current focus as
1014 // the window manager is still looking for where to put it.
1015 // We will do the work when we get a focus change callback.
1016 if (mWindowManagerService.mCurrentFocus == null) {
1017 return;
1018 }
1019
Svetoslav8e3feb12014-02-24 13:46:47 -08001020 WindowManager windowManager = (WindowManager)
1021 mContext.getSystemService(Context.WINDOW_SERVICE);
1022 windowManager.getDefaultDisplay().getRealSize(mTempPoint);
1023 final int screenWidth = mTempPoint.x;
1024 final int screenHeight = mTempPoint.y;
1025
1026 Region unaccountedSpace = mTempRegion;
1027 unaccountedSpace.set(0, 0, screenWidth, screenHeight);
1028
1029 SparseArray<WindowState> visibleWindows = mTempWindowStates;
1030 populateVisibleWindowsOnScreenLocked(visibleWindows);
1031
Svetoslav8e3feb12014-02-24 13:46:47 -08001032 Set<IBinder> addedWindows = mTempBinderSet;
1033 addedWindows.clear();
1034
Svetoslavf7174e82014-06-12 11:29:35 -07001035 boolean focusedWindowAdded = false;
1036
Svetoslav8e3feb12014-02-24 13:46:47 -08001037 final int visibleWindowCount = visibleWindows.size();
Allen Hairf20ac2c2016-02-11 17:42:59 -08001038 int skipRemainingWindowsForTaskId = -1;
1039 HashSet<Integer> skipRemainingWindowsForTasks = new HashSet<>();
Svetoslav8e3feb12014-02-24 13:46:47 -08001040 for (int i = visibleWindowCount - 1; i >= 0; i--) {
Alan Viverette9538eea2014-11-13 14:49:20 -08001041 final WindowState windowState = visibleWindows.valueAt(i);
Svetoslav8e3feb12014-02-24 13:46:47 -08001042 final int flags = windowState.mAttrs.flags;
Allen Hairf20ac2c2016-02-11 17:42:59 -08001043 final Task task = windowState.getTask();
1044
1045 // If the window is part of a task that we're finished with - ignore.
1046 if (task != null && skipRemainingWindowsForTasks.contains(task.mTaskId)) {
1047 continue;
1048 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001049
Svetoslav3a5c7212014-10-14 09:54:26 -07001050 // If the window is not touchable - ignore.
Svetoslavf7174e82014-06-12 11:29:35 -07001051 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001052 continue;
1053 }
1054
Alan Viverette9538eea2014-11-13 14:49:20 -08001055 // Compute the bounds in the screen.
1056 final Rect boundsInScreen = mTempRect;
1057 computeWindowBoundsInScreen(windowState, boundsInScreen);
1058
Svetoslav8e3feb12014-02-24 13:46:47 -08001059 // If the window is completely covered by other windows - ignore.
1060 if (unaccountedSpace.quickReject(boundsInScreen)) {
1061 continue;
1062 }
1063
1064 // Add windows of certain types not covered by modal windows.
1065 if (isReportedWindowType(windowState.mAttrs.type)) {
1066 // Add the window to the ones to be reported.
Svetoslavf7174e82014-06-12 11:29:35 -07001067 WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
Svetoslav8e3feb12014-02-24 13:46:47 -08001068 addedWindows.add(window.token);
Svetoslav8e3feb12014-02-24 13:46:47 -08001069 windows.add(window);
Svetoslavf7174e82014-06-12 11:29:35 -07001070 if (windowState.isFocused()) {
1071 focusedWindowAdded = true;
1072 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001073 }
1074
Alan Viveretted0c73f42014-11-18 10:25:04 -08001075 // Account for the space this window takes if the window
1076 // is not an accessibility overlay which does not change
1077 // the reported windows.
1078 if (windowState.mAttrs.type !=
1079 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY) {
1080 unaccountedSpace.op(boundsInScreen, unaccountedSpace,
1081 Region.Op.REVERSE_DIFFERENCE);
1082 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001083
1084 // We figured out what is touchable for the entire screen - done.
1085 if (unaccountedSpace.isEmpty()) {
1086 break;
1087 }
1088
Allen Hairf20ac2c2016-02-11 17:42:59 -08001089 // If a window is modal it prevents other windows from being touched
Svetoslav8e3feb12014-02-24 13:46:47 -08001090 if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1091 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
Allen Hairf20ac2c2016-02-11 17:42:59 -08001092 if (task != null) {
1093 // If the window is associated with a particular task, we can skip the
1094 // rest of the windows for that task.
1095 skipRemainingWindowsForTasks.add(task.mTaskId);
1096 continue;
1097 } else {
1098 // If the window is not associated with a particular task, then it is
1099 // globally modal. In this case we can skip all remaining windows.
1100 break;
1101 }
Svetoslav8e3feb12014-02-24 13:46:47 -08001102 }
1103 }
1104
Svetoslavf7174e82014-06-12 11:29:35 -07001105 // Always report the focused window.
1106 if (!focusedWindowAdded) {
1107 for (int i = visibleWindowCount - 1; i >= 0; i--) {
1108 WindowState windowState = visibleWindows.valueAt(i);
1109 if (windowState.isFocused()) {
1110 // Compute the bounds in the screen.
1111 Rect boundsInScreen = mTempRect;
1112 computeWindowBoundsInScreen(windowState, boundsInScreen);
1113
1114 // Add the window to the ones to be reported.
1115 WindowInfo window = obtainPopulatedWindowInfo(windowState,
1116 boundsInScreen);
1117 addedWindows.add(window.token);
1118 windows.add(window);
1119 break;
1120 }
1121 }
1122 }
1123
Svetoslav8e3feb12014-02-24 13:46:47 -08001124 // Remove child/parent references to windows that were not added.
1125 final int windowCount = windows.size();
1126 for (int i = 0; i < windowCount; i++) {
1127 WindowInfo window = windows.get(i);
1128 if (!addedWindows.contains(window.parentToken)) {
1129 window.parentToken = null;
1130 }
1131 if (window.childTokens != null) {
1132 final int childTokenCount = window.childTokens.size();
1133 for (int j = childTokenCount - 1; j >= 0; j--) {
1134 if (!addedWindows.contains(window.childTokens.get(j))) {
1135 window.childTokens.remove(j);
1136 }
1137 }
1138 // Leave the child token list if empty.
1139 }
1140 }
1141
1142 visibleWindows.clear();
1143 addedWindows.clear();
1144
1145 // We computed the windows and if they changed notify the client.
Svetoslav8e3feb12014-02-24 13:46:47 -08001146 if (mOldWindows.size() != windows.size()) {
1147 // Different size means something changed.
1148 windowsChanged = true;
1149 } else if (!mOldWindows.isEmpty() || !windows.isEmpty()) {
1150 // Since we always traverse windows from high to low layer
1151 // the old and new windows at the same index should be the
1152 // same, otherwise something changed.
1153 for (int i = 0; i < windowCount; i++) {
1154 WindowInfo oldWindow = mOldWindows.get(i);
1155 WindowInfo newWindow = windows.get(i);
1156 // We do not care for layer changes given the window
1157 // order does not change. This brings no new information
1158 // to the clients.
1159 if (windowChangedNoLayer(oldWindow, newWindow)) {
1160 windowsChanged = true;
1161 break;
1162 }
1163 }
1164 }
1165
1166 if (windowsChanged) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001167 cacheWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001168 }
1169 }
Svetoslav3a0d8782014-12-04 12:50:11 -08001170
1171 // Now we do not hold the lock, so send the windows over.
1172 if (windowsChanged) {
1173 if (DEBUG) {
1174 Log.i(LOG_TAG, "Windows changed:" + windows);
1175 }
1176 mCallback.onWindowsForAccessibilityChanged(windows);
1177 } else {
1178 if (DEBUG) {
1179 Log.i(LOG_TAG, "No windows changed.");
1180 }
1181 }
1182
1183 // Recycle the windows as we do not need them.
1184 clearAndRecycleWindows(windows);
Svetoslav8e3feb12014-02-24 13:46:47 -08001185 }
1186
Svetoslavf7174e82014-06-12 11:29:35 -07001187 private void computeWindowBoundsInScreen(WindowState windowState, Rect outBounds) {
1188 // Get the touchable frame.
1189 Region touchableRegion = mTempRegion1;
1190 windowState.getTouchableRegion(touchableRegion);
1191 Rect touchableFrame = mTempRect;
1192 touchableRegion.getBounds(touchableFrame);
1193
1194 // Move to origin as all transforms are captured by the matrix.
1195 RectF windowFrame = mTempRectF;
1196 windowFrame.set(touchableFrame);
1197 windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
1198
1199 // Map the frame to get what appears on the screen.
1200 Matrix matrix = mTempMatrix;
1201 populateTransformationMatrixLocked(windowState, matrix);
1202 matrix.mapRect(windowFrame);
1203
1204 // Got the bounds.
1205 outBounds.set((int) windowFrame.left, (int) windowFrame.top,
1206 (int) windowFrame.right, (int) windowFrame.bottom);
1207 }
1208
1209 private static WindowInfo obtainPopulatedWindowInfo(WindowState windowState,
1210 Rect boundsInScreen) {
1211 WindowInfo window = WindowInfo.obtain();
1212 window.type = windowState.mAttrs.type;
1213 window.layer = windowState.mLayer;
1214 window.token = windowState.mClient.asBinder();
1215
1216 WindowState attachedWindow = windowState.mAttachedWindow;
1217 if (attachedWindow != null) {
1218 window.parentToken = attachedWindow.mClient.asBinder();
1219 }
1220
1221 window.focused = windowState.isFocused();
1222 window.boundsInScreen.set(boundsInScreen);
1223
1224 final int childCount = windowState.mChildWindows.size();
1225 if (childCount > 0) {
1226 if (window.childTokens == null) {
1227 window.childTokens = new ArrayList<IBinder>();
1228 }
1229 for (int j = 0; j < childCount; j++) {
1230 WindowState child = windowState.mChildWindows.get(j);
1231 window.childTokens.add(child.mClient.asBinder());
1232 }
1233 }
1234
1235 return window;
1236 }
1237
Svetoslav8e3feb12014-02-24 13:46:47 -08001238 private void cacheWindows(List<WindowInfo> windows) {
1239 final int oldWindowCount = mOldWindows.size();
1240 for (int i = oldWindowCount - 1; i >= 0; i--) {
1241 mOldWindows.remove(i).recycle();
1242 }
1243 final int newWindowCount = windows.size();
1244 for (int i = 0; i < newWindowCount; i++) {
1245 WindowInfo newWindow = windows.get(i);
1246 mOldWindows.add(WindowInfo.obtain(newWindow));
1247 }
1248 }
1249
1250 private boolean windowChangedNoLayer(WindowInfo oldWindow, WindowInfo newWindow) {
1251 if (oldWindow == newWindow) {
1252 return false;
1253 }
Svetoslavf7174e82014-06-12 11:29:35 -07001254 if (oldWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001255 return true;
1256 }
Svetoslavf7174e82014-06-12 11:29:35 -07001257 if (newWindow == null) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001258 return true;
1259 }
1260 if (oldWindow.type != newWindow.type) {
1261 return true;
1262 }
1263 if (oldWindow.focused != newWindow.focused) {
1264 return true;
1265 }
1266 if (oldWindow.token == null) {
1267 if (newWindow.token != null) {
1268 return true;
1269 }
1270 } else if (!oldWindow.token.equals(newWindow.token)) {
1271 return true;
1272 }
1273 if (oldWindow.parentToken == null) {
1274 if (newWindow.parentToken != null) {
1275 return true;
1276 }
1277 } else if (!oldWindow.parentToken.equals(newWindow.parentToken)) {
1278 return true;
1279 }
1280 if (!oldWindow.boundsInScreen.equals(newWindow.boundsInScreen)) {
1281 return true;
1282 }
1283 if (oldWindow.childTokens != null && newWindow.childTokens != null
1284 && !oldWindow.childTokens.equals(newWindow.childTokens)) {
1285 return true;
1286 }
1287 return false;
1288 }
1289
Svetoslav3a0d8782014-12-04 12:50:11 -08001290 private static void clearAndRecycleWindows(List<WindowInfo> windows) {
Svetoslav8e3feb12014-02-24 13:46:47 -08001291 final int windowCount = windows.size();
1292 for (int i = windowCount - 1; i >= 0; i--) {
1293 windows.remove(i).recycle();
1294 }
1295 }
1296
1297 private static boolean isReportedWindowType(int windowType) {
1298 return (windowType != WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM
1299 && windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
1300 && windowType != WindowManager.LayoutParams.TYPE_BOOT_PROGRESS
1301 && windowType != WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY
1302 && windowType != WindowManager.LayoutParams.TYPE_DRAG
Selim Cinekf83e8242015-05-19 18:08:14 -07001303 && windowType != WindowManager.LayoutParams.TYPE_INPUT_CONSUMER
Svetoslav8e3feb12014-02-24 13:46:47 -08001304 && windowType != WindowManager.LayoutParams.TYPE_POINTER
Svetoslav8e3feb12014-02-24 13:46:47 -08001305 && windowType != WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY
1306 && windowType != WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY
1307 && windowType != WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY
1308 && windowType != WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION);
1309 }
1310
1311 private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
1312 DisplayContent displayContent = mWindowManagerService
1313 .getDefaultDisplayContentLocked();
1314 WindowList windowList = displayContent.getWindowList();
1315 final int windowCount = windowList.size();
1316 for (int i = 0; i < windowCount; i++) {
1317 WindowState windowState = windowList.get(i);
1318 if (windowState.isVisibleLw()) {
1319 outWindows.put(windowState.mLayer, windowState);
1320 }
1321 }
1322 }
1323
1324 private class MyHandler extends Handler {
Svetoslavf7174e82014-06-12 11:29:35 -07001325 public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
Svetoslav8e3feb12014-02-24 13:46:47 -08001326
1327 public MyHandler(Looper looper) {
1328 super(looper, null, false);
1329 }
1330
1331 @Override
1332 @SuppressWarnings("unchecked")
1333 public void handleMessage(Message message) {
1334 switch (message.what) {
Svetoslavf7174e82014-06-12 11:29:35 -07001335 case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1336 computeChangedWindows();
1337 } break;
Svetoslav8e3feb12014-02-24 13:46:47 -08001338 }
1339 }
1340 }
1341 }
1342}