AccessibilityController.java revision 7505e3315cb997165035f3bcc736d9d257b1fe3c
1/*
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;
47import android.view.ViewConfiguration;
48import 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) {
117            mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
118        }
119    }
120
121    public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
122        if (mDisplayMagnifier != null) {
123            mDisplayMagnifier.onRectangleOnScreenRequestedLocked(rectangle);
124        }
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) {
133            mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
134        }
135    }
136
137    public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
138        if (mDisplayMagnifier != null) {
139            mDisplayMagnifier.onRotationChangedLocked(displayContent, rotation);
140        }
141        if (mWindowsForAccessibilityObserver != null) {
142            mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
143        }
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) {
158            mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
159        }
160    }
161
162    public void onWindowFocusChangedLocked() {
163        // Not relevant for the display magnifier.
164
165        if (mWindowsForAccessibilityObserver != null) {
166            mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
167        }
168    }
169
170
171    public void onSomeWindowResizedOrMovedLocked() {
172        // Not relevant for the display magnifier.
173
174        if (mWindowsForAccessibilityObserver != null) {
175            mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
176        }
177    }
178
179    /** NOTE: This has to be called within a surface transaction. */
180    public void drawMagnifiedRegionBorderIfNeededLocked() {
181        if (mDisplayMagnifier != null) {
182            mDisplayMagnifier.drawMagnifiedRegionBorderIfNeededLocked();
183        }
184        // Not relevant for the window observer.
185    }
186
187    public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
188        if (mDisplayMagnifier != null) {
189            return mDisplayMagnifier.getMagnificationSpecForWindowLocked(windowState);
190        }
191        return null;
192    }
193
194    public boolean hasCallbacksLocked() {
195        return (mDisplayMagnifier != null
196                || mWindowsForAccessibilityObserver != null);
197    }
198
199    private static void populateTransformationMatrixLocked(WindowState windowState,
200            Matrix outMatrix) {
201        sTempFloats[Matrix.MSCALE_X] = windowState.mWinAnimator.mDsDx;
202        sTempFloats[Matrix.MSKEW_Y] = windowState.mWinAnimator.mDtDx;
203        sTempFloats[Matrix.MSKEW_X] = windowState.mWinAnimator.mDsDy;
204        sTempFloats[Matrix.MSCALE_Y] = windowState.mWinAnimator.mDtDy;
205        sTempFloats[Matrix.MTRANS_X] = windowState.mShownFrame.left;
206        sTempFloats[Matrix.MTRANS_Y] = windowState.mShownFrame.top;
207        sTempFloats[Matrix.MPERSP_0] = 0;
208        sTempFloats[Matrix.MPERSP_1] = 0;
209        sTempFloats[Matrix.MPERSP_2] = 1;
210        outMatrix.setValues(sTempFloats);
211    }
212
213    /**
214     * This class encapsulates the functionality related to display magnification.
215     */
216    private static final class DisplayMagnifier {
217
218        private static final String LOG_TAG = "DisplayMagnifier";
219
220        private static final boolean DEBUG_WINDOW_TRANSITIONS = false;
221        private static final boolean DEBUG_ROTATION = false;
222        private static final boolean DEBUG_LAYERS = false;
223        private static final boolean DEBUG_RECTANGLE_REQUESTED = false;
224        private static final boolean DEBUG_VIEWPORT_WINDOW = false;
225
226        private final Rect mTempRect1 = new Rect();
227        private final Rect mTempRect2 = new Rect();
228
229        private final Region mTempRegion1 = new Region();
230        private final Region mTempRegion2 = new Region();
231        private final Region mTempRegion3 = new Region();
232        private final Region mTempRegion4 = new Region();
233
234        private final Context mContext;
235        private final WindowManagerService mWindowManagerService;
236        private final MagnifiedViewport mMagnifedViewport;
237        private final Handler mHandler;
238
239        private final MagnificationCallbacks mCallbacks;
240
241        private final long mLongAnimationDuration;
242
243        public DisplayMagnifier(WindowManagerService windowManagerService,
244                MagnificationCallbacks callbacks) {
245            mContext = windowManagerService.mContext;
246            mWindowManagerService = windowManagerService;
247            mCallbacks = callbacks;
248            mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
249            mMagnifedViewport = new MagnifiedViewport();
250            mLongAnimationDuration = mContext.getResources().getInteger(
251                    com.android.internal.R.integer.config_longAnimTime);
252        }
253
254        public void setMagnificationSpecLocked(MagnificationSpec spec) {
255            mMagnifedViewport.updateMagnificationSpecLocked(spec);
256            mMagnifedViewport.recomputeBoundsLocked();
257            mWindowManagerService.scheduleAnimationLocked();
258        }
259
260        public void onRectangleOnScreenRequestedLocked(Rect rectangle) {
261            if (DEBUG_RECTANGLE_REQUESTED) {
262                Slog.i(LOG_TAG, "Rectangle on screen requested: " + rectangle);
263            }
264            if (!mMagnifedViewport.isMagnifyingLocked()) {
265                return;
266            }
267            Rect magnifiedRegionBounds = mTempRect2;
268            mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(magnifiedRegionBounds);
269            if (magnifiedRegionBounds.contains(rectangle)) {
270                return;
271            }
272            SomeArgs args = SomeArgs.obtain();
273            args.argi1 = rectangle.left;
274            args.argi2 = rectangle.top;
275            args.argi3 = rectangle.right;
276            args.argi4 = rectangle.bottom;
277            mHandler.obtainMessage(MyHandler.MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED,
278                    args).sendToTarget();
279        }
280
281        public void onWindowLayersChangedLocked() {
282            if (DEBUG_LAYERS) {
283                Slog.i(LOG_TAG, "Layers changed.");
284            }
285            mMagnifedViewport.recomputeBoundsLocked();
286            mWindowManagerService.scheduleAnimationLocked();
287        }
288
289        public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
290            if (DEBUG_ROTATION) {
291                Slog.i(LOG_TAG, "Rotaton: " + Surface.rotationToString(rotation)
292                        + " displayId: " + displayContent.getDisplayId());
293            }
294            mMagnifedViewport.onRotationChangedLocked();
295            mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_ROTATION_CHANGED);
296        }
297
298        public void onAppWindowTransitionLocked(WindowState windowState, int transition) {
299            if (DEBUG_WINDOW_TRANSITIONS) {
300                Slog.i(LOG_TAG, "Window transition: "
301                        + AppTransition.appTransitionToString(transition)
302                        + " displayId: " + windowState.getDisplayId());
303            }
304            final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
305            if (magnifying) {
306                switch (transition) {
307                    case AppTransition.TRANSIT_ACTIVITY_OPEN:
308                    case AppTransition.TRANSIT_TASK_OPEN:
309                    case AppTransition.TRANSIT_TASK_TO_FRONT:
310                    case AppTransition.TRANSIT_WALLPAPER_OPEN:
311                    case AppTransition.TRANSIT_WALLPAPER_CLOSE:
312                    case AppTransition.TRANSIT_WALLPAPER_INTRA_OPEN: {
313                        mHandler.sendEmptyMessage(MyHandler.MESSAGE_NOTIFY_USER_CONTEXT_CHANGED);
314                    }
315                }
316            }
317        }
318
319        public void onWindowTransitionLocked(WindowState windowState, int transition) {
320            if (DEBUG_WINDOW_TRANSITIONS) {
321                Slog.i(LOG_TAG, "Window transition: "
322                        + AppTransition.appTransitionToString(transition)
323                        + " displayId: " + windowState.getDisplayId());
324            }
325            final boolean magnifying = mMagnifedViewport.isMagnifyingLocked();
326            final int type = windowState.mAttrs.type;
327            switch (transition) {
328                case WindowManagerPolicy.TRANSIT_ENTER:
329                case WindowManagerPolicy.TRANSIT_SHOW: {
330                    if (!magnifying) {
331                        break;
332                    }
333                    switch (type) {
334                        case WindowManager.LayoutParams.TYPE_APPLICATION:
335                        case WindowManager.LayoutParams.TYPE_APPLICATION_PANEL:
336                        case WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA:
337                        case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
338                        case WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG:
339                        case WindowManager.LayoutParams.TYPE_SEARCH_BAR:
340                        case WindowManager.LayoutParams.TYPE_PHONE:
341                        case WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
342                        case WindowManager.LayoutParams.TYPE_TOAST:
343                        case WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY:
344                        case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
345                        case WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG:
346                        case WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG:
347                        case WindowManager.LayoutParams.TYPE_SYSTEM_ERROR:
348                        case WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY:
349                        case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL:
350                        case WindowManager.LayoutParams.TYPE_RECENTS_OVERLAY: {
351                            Rect magnifiedRegionBounds = mTempRect2;
352                            mMagnifedViewport.getMagnifiedFrameInContentCoordsLocked(
353                                    magnifiedRegionBounds);
354                            Rect touchableRegionBounds = mTempRect1;
355                            windowState.getTouchableRegion(mTempRegion1);
356                            mTempRegion1.getBounds(touchableRegionBounds);
357                            if (!magnifiedRegionBounds.intersect(touchableRegionBounds)) {
358                                mCallbacks.onRectangleOnScreenRequested(
359                                        touchableRegionBounds.left,
360                                        touchableRegionBounds.top,
361                                        touchableRegionBounds.right,
362                                        touchableRegionBounds.bottom);
363                            }
364                        } break;
365                    } break;
366                }
367            }
368        }
369
370        public MagnificationSpec getMagnificationSpecForWindowLocked(WindowState windowState) {
371            MagnificationSpec spec = mMagnifedViewport.getMagnificationSpecLocked();
372            if (spec != null && !spec.isNop()) {
373                WindowManagerPolicy policy = mWindowManagerService.mPolicy;
374                final int windowType = windowState.mAttrs.type;
375                if (!policy.isTopLevelWindow(windowType) && windowState.mAttachedWindow != null
376                        && !policy.canMagnifyWindow(windowType)) {
377                    return null;
378                }
379                if (!policy.canMagnifyWindow(windowState.mAttrs.type)) {
380                    return null;
381                }
382            }
383            return spec;
384        }
385
386        public void destroyLocked() {
387            mMagnifedViewport.destroyWindow();
388        }
389
390        /** NOTE: This has to be called within a surface transaction. */
391        public void drawMagnifiedRegionBorderIfNeededLocked() {
392            mMagnifedViewport.drawWindowIfNeededLocked();
393        }
394
395        private final class MagnifiedViewport {
396
397            private static final int DEFAUTLT_BORDER_WIDTH_DIP = 5;
398
399            private final SparseArray<WindowState> mTempWindowStates =
400                    new SparseArray<WindowState>();
401
402            private final RectF mTempRectF = new RectF();
403
404            private final Point mTempPoint = new Point();
405
406            private final Matrix mTempMatrix = new Matrix();
407
408            private final Region mMagnifiedBounds = new Region();
409            private final Region mOldMagnifiedBounds = new Region();
410
411            private final MagnificationSpec mMagnificationSpec = MagnificationSpec.obtain();
412
413            private final WindowManager mWindowManager;
414
415            private final float mBorderWidth;
416            private final int mHalfBorderWidth;
417            private final int mDrawBorderInset;
418
419            private final ViewportWindow mWindow;
420
421            private boolean mFullRedrawNeeded;
422
423            public MagnifiedViewport() {
424                mWindowManager = (WindowManager) mContext.getSystemService(Service.WINDOW_SERVICE);
425                mBorderWidth = TypedValue.applyDimension(
426                        TypedValue.COMPLEX_UNIT_DIP, DEFAUTLT_BORDER_WIDTH_DIP,
427                                mContext.getResources().getDisplayMetrics());
428                mHalfBorderWidth = (int) Math.ceil(mBorderWidth / 2);
429                mDrawBorderInset = (int) mBorderWidth / 2;
430                mWindow = new ViewportWindow(mContext);
431                recomputeBoundsLocked();
432            }
433
434            public void updateMagnificationSpecLocked(MagnificationSpec spec) {
435                if (spec != null) {
436                    mMagnificationSpec.initialize(spec.scale, spec.offsetX, spec.offsetY);
437                } else {
438                    mMagnificationSpec.clear();
439                }
440                // If this message is pending we are in a rotation animation and do not want
441                // to show the border. We will do so when the pending message is handled.
442                if (!mHandler.hasMessages(
443                        MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED)) {
444                    setMagnifiedRegionBorderShownLocked(isMagnifyingLocked(), true);
445                }
446            }
447
448            public void recomputeBoundsLocked() {
449                mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
450                final int screenWidth = mTempPoint.x;
451                final int screenHeight = mTempPoint.y;
452
453                Region magnifiedBounds = mMagnifiedBounds;
454                magnifiedBounds.set(0, 0, 0, 0);
455
456                Region availableBounds = mTempRegion1;
457                availableBounds.set(0, 0, screenWidth, screenHeight);
458
459                Region nonMagnifiedBounds = mTempRegion4;
460                nonMagnifiedBounds.set(0,  0,  0,  0);
461
462                SparseArray<WindowState> visibleWindows = mTempWindowStates;
463                visibleWindows.clear();
464                populateWindowsOnScreenLocked(visibleWindows);
465
466                final int visibleWindowCount = visibleWindows.size();
467                for (int i = visibleWindowCount - 1; i >= 0; i--) {
468                    WindowState windowState = visibleWindows.valueAt(i);
469                    if (windowState.mAttrs.type == WindowManager
470                            .LayoutParams.TYPE_MAGNIFICATION_OVERLAY) {
471                        continue;
472                    }
473
474                    Region windowBounds = mTempRegion2;
475                    Matrix matrix = mTempMatrix;
476                    populateTransformationMatrixLocked(windowState, matrix);
477                    RectF windowFrame = mTempRectF;
478
479                    if (mWindowManagerService.mPolicy.canMagnifyWindow(windowState.mAttrs.type)) {
480                        windowFrame.set(windowState.mFrame);
481                        windowFrame.offset(-windowFrame.left, -windowFrame.top);
482                        matrix.mapRect(windowFrame);
483                        windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
484                                (int) windowFrame.right, (int) windowFrame.bottom);
485                        magnifiedBounds.op(windowBounds, Region.Op.UNION);
486                        magnifiedBounds.op(availableBounds, Region.Op.INTERSECT);
487                    } else {
488                        Region touchableRegion = mTempRegion3;
489                        windowState.getTouchableRegion(touchableRegion);
490                        Rect touchableFrame = mTempRect1;
491                        touchableRegion.getBounds(touchableFrame);
492                        windowFrame.set(touchableFrame);
493                        windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
494                        matrix.mapRect(windowFrame);
495                        windowBounds.set((int) windowFrame.left, (int) windowFrame.top,
496                                (int) windowFrame.right, (int) windowFrame.bottom);
497                        nonMagnifiedBounds.op(windowBounds, Region.Op.UNION);
498                        windowBounds.op(magnifiedBounds, Region.Op.DIFFERENCE);
499                        availableBounds.op(windowBounds, Region.Op.DIFFERENCE);
500                    }
501
502                    Region accountedBounds = mTempRegion2;
503                    accountedBounds.set(magnifiedBounds);
504                    accountedBounds.op(nonMagnifiedBounds, Region.Op.UNION);
505                    accountedBounds.op(0, 0, screenWidth, screenHeight, Region.Op.INTERSECT);
506
507                    if (accountedBounds.isRect()) {
508                        Rect accountedFrame = mTempRect1;
509                        accountedBounds.getBounds(accountedFrame);
510                        if (accountedFrame.width() == screenWidth
511                                && accountedFrame.height() == screenHeight) {
512                            break;
513                        }
514                    }
515                }
516
517                visibleWindows.clear();
518
519                magnifiedBounds.op(mDrawBorderInset, mDrawBorderInset,
520                        screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset,
521                        Region.Op.INTERSECT);
522
523                if (!mOldMagnifiedBounds.equals(magnifiedBounds)) {
524                    Region bounds = Region.obtain();
525                    bounds.set(magnifiedBounds);
526                    mHandler.obtainMessage(MyHandler.MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED,
527                            bounds).sendToTarget();
528
529                    mWindow.setBounds(magnifiedBounds);
530                    Rect dirtyRect = mTempRect1;
531                    if (mFullRedrawNeeded) {
532                        mFullRedrawNeeded = false;
533                        dirtyRect.set(mDrawBorderInset, mDrawBorderInset,
534                                screenWidth - mDrawBorderInset, screenHeight - mDrawBorderInset);
535                        mWindow.invalidate(dirtyRect);
536                    } else {
537                        Region dirtyRegion = mTempRegion3;
538                        dirtyRegion.set(magnifiedBounds);
539                        dirtyRegion.op(mOldMagnifiedBounds, Region.Op.UNION);
540                        dirtyRegion.op(nonMagnifiedBounds, Region.Op.INTERSECT);
541                        dirtyRegion.getBounds(dirtyRect);
542                        mWindow.invalidate(dirtyRect);
543                    }
544
545                    mOldMagnifiedBounds.set(magnifiedBounds);
546                }
547            }
548
549            public void onRotationChangedLocked() {
550                // If we are magnifying, hide the magnified border window immediately so
551                // the user does not see strange artifacts during rotation. The screenshot
552                // used for rotation has already the border. After the rotation is complete
553                // we will show the border.
554                if (isMagnifyingLocked()) {
555                    setMagnifiedRegionBorderShownLocked(false, false);
556                    final long delay = (long) (mLongAnimationDuration
557                            * mWindowManagerService.getWindowAnimationScaleLocked());
558                    Message message = mHandler.obtainMessage(
559                            MyHandler.MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED);
560                    mHandler.sendMessageDelayed(message, delay);
561                }
562                recomputeBoundsLocked();
563                mWindow.updateSize();
564            }
565
566            public void setMagnifiedRegionBorderShownLocked(boolean shown, boolean animate) {
567                if (shown) {
568                    mFullRedrawNeeded = true;
569                    mOldMagnifiedBounds.set(0,  0,  0,  0);
570                }
571                mWindow.setShown(shown, animate);
572            }
573
574            public void getMagnifiedFrameInContentCoordsLocked(Rect rect) {
575                MagnificationSpec spec = mMagnificationSpec;
576                mMagnifiedBounds.getBounds(rect);
577                rect.offset((int) -spec.offsetX, (int) -spec.offsetY);
578                rect.scale(1.0f / spec.scale);
579            }
580
581            public boolean isMagnifyingLocked() {
582                return mMagnificationSpec.scale > 1.0f;
583            }
584
585            public MagnificationSpec getMagnificationSpecLocked() {
586                return mMagnificationSpec;
587            }
588
589            /** NOTE: This has to be called within a surface transaction. */
590            public void drawWindowIfNeededLocked() {
591                recomputeBoundsLocked();
592                mWindow.drawIfNeeded();
593            }
594
595            public void destroyWindow() {
596                mWindow.releaseSurface();
597            }
598
599            private void populateWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
600                DisplayContent displayContent = mWindowManagerService
601                        .getDefaultDisplayContentLocked();
602                WindowList windowList = displayContent.getWindowList();
603                final int windowCount = windowList.size();
604                for (int i = 0; i < windowCount; i++) {
605                    WindowState windowState = windowList.get(i);
606                    if ((windowState.isOnScreen() || windowState.mAttrs.type == WindowManager
607                            .LayoutParams.TYPE_UNIVERSE_BACKGROUND)
608                            && !windowState.mWinAnimator.mEnterAnimationPending) {
609                        outWindows.put(windowState.mLayer, windowState);
610                    }
611                }
612            }
613
614            private final class ViewportWindow {
615                private static final String SURFACE_TITLE = "Magnification Overlay";
616
617                private static final String PROPERTY_NAME_ALPHA = "alpha";
618
619                private static final int MIN_ALPHA = 0;
620                private static final int MAX_ALPHA = 255;
621
622                private final Region mBounds = new Region();
623                private final Rect mDirtyRect = new Rect();
624                private final Paint mPaint = new Paint();
625
626                private final ValueAnimator mShowHideFrameAnimator;
627                private final SurfaceControl mSurfaceControl;
628                private final Surface mSurface = new Surface();
629
630                private boolean mShown;
631                private int mAlpha;
632
633                private boolean mInvalidated;
634
635                public ViewportWindow(Context context) {
636                    SurfaceControl surfaceControl = null;
637                    try {
638                        mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
639                        surfaceControl = new SurfaceControl(mWindowManagerService.mFxSession,
640                                SURFACE_TITLE, mTempPoint.x, mTempPoint.y, PixelFormat.TRANSLUCENT,
641                                SurfaceControl.HIDDEN);
642                    } catch (OutOfResourcesException oore) {
643                        /* ignore */
644                    }
645                    mSurfaceControl = surfaceControl;
646                    mSurfaceControl.setLayerStack(mWindowManager.getDefaultDisplay()
647                            .getLayerStack());
648                    mSurfaceControl.setLayer(mWindowManagerService.mPolicy.windowTypeToLayerLw(
649                            WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY)
650                            * WindowManagerService.TYPE_LAYER_MULTIPLIER);
651                    mSurfaceControl.setPosition(0, 0);
652                    mSurface.copyFrom(mSurfaceControl);
653
654                    TypedValue typedValue = new TypedValue();
655                    context.getTheme().resolveAttribute(R.attr.colorActivatedHighlight,
656                            typedValue, true);
657                    final int borderColor = context.getResources().getColor(typedValue.resourceId);
658
659                    mPaint.setStyle(Paint.Style.STROKE);
660                    mPaint.setStrokeWidth(mBorderWidth);
661                    mPaint.setColor(borderColor);
662
663                    Interpolator interpolator = new DecelerateInterpolator(2.5f);
664                    final long longAnimationDuration = context.getResources().getInteger(
665                            com.android.internal.R.integer.config_longAnimTime);
666
667                    mShowHideFrameAnimator = ObjectAnimator.ofInt(this, PROPERTY_NAME_ALPHA,
668                            MIN_ALPHA, MAX_ALPHA);
669                    mShowHideFrameAnimator.setInterpolator(interpolator);
670                    mShowHideFrameAnimator.setDuration(longAnimationDuration);
671                    mInvalidated = true;
672                }
673
674                public void setShown(boolean shown, boolean animate) {
675                    synchronized (mWindowManagerService.mWindowMap) {
676                        if (mShown == shown) {
677                            return;
678                        }
679                        mShown = shown;
680                        if (animate) {
681                            if (mShowHideFrameAnimator.isRunning()) {
682                                mShowHideFrameAnimator.reverse();
683                            } else {
684                                if (shown) {
685                                    mShowHideFrameAnimator.start();
686                                } else {
687                                    mShowHideFrameAnimator.reverse();
688                                }
689                            }
690                        } else {
691                            mShowHideFrameAnimator.cancel();
692                            if (shown) {
693                                setAlpha(MAX_ALPHA);
694                            } else {
695                                setAlpha(MIN_ALPHA);
696                            }
697                        }
698                        if (DEBUG_VIEWPORT_WINDOW) {
699                            Slog.i(LOG_TAG, "ViewportWindow shown: " + mShown);
700                        }
701                    }
702                }
703
704                @SuppressWarnings("unused")
705                // Called reflectively from an animator.
706                public int getAlpha() {
707                    synchronized (mWindowManagerService.mWindowMap) {
708                        return mAlpha;
709                    }
710                }
711
712                public void setAlpha(int alpha) {
713                    synchronized (mWindowManagerService.mWindowMap) {
714                        if (mAlpha == alpha) {
715                            return;
716                        }
717                        mAlpha = alpha;
718                        invalidate(null);
719                        if (DEBUG_VIEWPORT_WINDOW) {
720                            Slog.i(LOG_TAG, "ViewportWindow set alpha: " + alpha);
721                        }
722                    }
723                }
724
725                public void setBounds(Region bounds) {
726                    synchronized (mWindowManagerService.mWindowMap) {
727                        if (mBounds.equals(bounds)) {
728                            return;
729                        }
730                        mBounds.set(bounds);
731                        invalidate(mDirtyRect);
732                        if (DEBUG_VIEWPORT_WINDOW) {
733                            Slog.i(LOG_TAG, "ViewportWindow set bounds: " + bounds);
734                        }
735                    }
736                }
737
738                public void updateSize() {
739                    synchronized (mWindowManagerService.mWindowMap) {
740                        mWindowManager.getDefaultDisplay().getRealSize(mTempPoint);
741                        mSurfaceControl.setSize(mTempPoint.x, mTempPoint.y);
742                        invalidate(mDirtyRect);
743                    }
744                }
745
746                public void invalidate(Rect dirtyRect) {
747                    if (dirtyRect != null) {
748                        mDirtyRect.set(dirtyRect);
749                    } else {
750                        mDirtyRect.setEmpty();
751                    }
752                    mInvalidated = true;
753                    mWindowManagerService.scheduleAnimationLocked();
754                }
755
756                /** NOTE: This has to be called within a surface transaction. */
757                public void drawIfNeeded() {
758                    synchronized (mWindowManagerService.mWindowMap) {
759                        if (!mInvalidated) {
760                            return;
761                        }
762                        mInvalidated = false;
763                        Canvas canvas = null;
764                        try {
765                            // Empty dirty rectangle means unspecified.
766                            if (mDirtyRect.isEmpty()) {
767                                mBounds.getBounds(mDirtyRect);
768                            }
769                            mDirtyRect.inset(- mHalfBorderWidth, - mHalfBorderWidth);
770                            canvas = mSurface.lockCanvas(mDirtyRect);
771                            if (DEBUG_VIEWPORT_WINDOW) {
772                                Slog.i(LOG_TAG, "Dirty rect: " + mDirtyRect);
773                            }
774                        } catch (IllegalArgumentException iae) {
775                            /* ignore */
776                        } catch (Surface.OutOfResourcesException oore) {
777                            /* ignore */
778                        }
779                        if (canvas == null) {
780                            return;
781                        }
782                        if (DEBUG_VIEWPORT_WINDOW) {
783                            Slog.i(LOG_TAG, "Bounds: " + mBounds);
784                        }
785                        canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
786                        mPaint.setAlpha(mAlpha);
787                        Path path = mBounds.getBoundaryPath();
788                        canvas.drawPath(path, mPaint);
789
790                        mSurface.unlockCanvasAndPost(canvas);
791
792                        if (mAlpha > 0) {
793                            mSurfaceControl.show();
794                        } else {
795                            mSurfaceControl.hide();
796                        }
797                    }
798                }
799
800                public void releaseSurface() {
801                    mSurfaceControl.release();
802                    mSurface.release();
803                }
804            }
805        }
806
807        private class MyHandler extends Handler {
808            public static final int MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED = 1;
809            public static final int MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED = 2;
810            public static final int MESSAGE_NOTIFY_USER_CONTEXT_CHANGED = 3;
811            public static final int MESSAGE_NOTIFY_ROTATION_CHANGED = 4;
812            public static final int MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED = 5;
813
814            public MyHandler(Looper looper) {
815                super(looper);
816            }
817
818            @Override
819            public void handleMessage(Message message) {
820                switch (message.what) {
821                    case MESSAGE_NOTIFY_MAGNIFIED_BOUNDS_CHANGED: {
822                        Region bounds = (Region) message.obj;
823                        mCallbacks.onMagnifedBoundsChanged(bounds);
824                        bounds.recycle();
825                    } break;
826
827                    case MESSAGE_NOTIFY_RECTANGLE_ON_SCREEN_REQUESTED: {
828                        SomeArgs args = (SomeArgs) message.obj;
829                        final int left = args.argi1;
830                        final int top = args.argi2;
831                        final int right = args.argi3;
832                        final int bottom = args.argi4;
833                        mCallbacks.onRectangleOnScreenRequested(left, top, right, bottom);
834                        args.recycle();
835                    } break;
836
837                    case MESSAGE_NOTIFY_USER_CONTEXT_CHANGED: {
838                        mCallbacks.onUserContextChanged();
839                    } break;
840
841                    case MESSAGE_NOTIFY_ROTATION_CHANGED: {
842                        final int rotation = message.arg1;
843                        mCallbacks.onRotationChanged(rotation);
844                    } break;
845
846                    case MESSAGE_SHOW_MAGNIFIED_REGION_BOUNDS_IF_NEEDED : {
847                        synchronized (mWindowManagerService.mWindowMap) {
848                            if (mMagnifedViewport.isMagnifyingLocked()) {
849                                mMagnifedViewport.setMagnifiedRegionBorderShownLocked(true, true);
850                                mWindowManagerService.scheduleAnimationLocked();
851                            }
852                        }
853                    } break;
854                }
855            }
856        }
857    }
858
859    /**
860     * This class encapsulates the functionality related to computing the windows
861     * reported for accessibility purposes. These windows are all windows a sighted
862     * user can see on the screen.
863     */
864    private static final class WindowsForAccessibilityObserver {
865        private static final String LOG_TAG = "WindowsForAccessibilityObserver";
866
867        private static final boolean DEBUG = false;
868
869        private final SparseArray<WindowState> mTempWindowStates =
870                new SparseArray<WindowState>();
871
872        private final List<WindowInfo> mOldWindows = new ArrayList<WindowInfo>();
873
874        private final Set<IBinder> mTempBinderSet = new ArraySet<IBinder>();
875
876        private final RectF mTempRectF = new RectF();
877
878        private final Matrix mTempMatrix = new Matrix();
879
880        private final Point mTempPoint = new Point();
881
882        private final Rect mTempRect = new Rect();
883
884        private final Region mTempRegion = new Region();
885
886        private final Region mTempRegion1 = new Region();
887
888        private final Context mContext;
889
890        private final WindowManagerService mWindowManagerService;
891
892        private final Handler mHandler;
893
894        private final WindowsForAccessibilityCallback mCallback;
895
896        private final long mRecurringAccessibilityEventsIntervalMillis;
897
898        public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
899                WindowsForAccessibilityCallback callback) {
900            mContext = windowManagerService.mContext;
901            mWindowManagerService = windowManagerService;
902            mCallback = callback;
903            mHandler = new MyHandler(mWindowManagerService.mH.getLooper());
904            mRecurringAccessibilityEventsIntervalMillis = ViewConfiguration
905                    .getSendRecurringAccessibilityEventsInterval();
906            computeChangedWindows();
907        }
908
909        public void scheduleComputeChangedWindowsLocked() {
910            // If focus changed, compute changed windows immediately as the focused window
911            // is used by the accessibility manager service to determine the active window.
912            if (mWindowManagerService.mCurrentFocus != null
913                    && mWindowManagerService.mCurrentFocus != mWindowManagerService.mLastFocus) {
914                mHandler.removeMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS);
915                computeChangedWindows();
916            } else if (!mHandler.hasMessages(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS)) {
917                mHandler.sendEmptyMessageDelayed(MyHandler.MESSAGE_COMPUTE_CHANGED_WINDOWS,
918                        mRecurringAccessibilityEventsIntervalMillis);
919            }
920        }
921
922        public void computeChangedWindows() {
923            if (DEBUG) {
924                Slog.i(LOG_TAG, "computeChangedWindows()");
925            }
926
927            synchronized (mWindowManagerService.mWindowMap) {
928                // Do not send the windows if there is no current focus as
929                // the window manager is still looking for where to put it.
930                // We will do the work when we get a focus change callback.
931                if (mWindowManagerService.mCurrentFocus == null) {
932                    return;
933                }
934
935                WindowManager windowManager = (WindowManager)
936                        mContext.getSystemService(Context.WINDOW_SERVICE);
937                windowManager.getDefaultDisplay().getRealSize(mTempPoint);
938                final int screenWidth = mTempPoint.x;
939                final int screenHeight = mTempPoint.y;
940
941                Region unaccountedSpace = mTempRegion;
942                unaccountedSpace.set(0, 0, screenWidth, screenHeight);
943
944                SparseArray<WindowState> visibleWindows = mTempWindowStates;
945                populateVisibleWindowsOnScreenLocked(visibleWindows);
946
947                List<WindowInfo> windows = new ArrayList<WindowInfo>();
948
949                Set<IBinder> addedWindows = mTempBinderSet;
950                addedWindows.clear();
951
952                boolean focusedWindowAdded = false;
953
954                final int visibleWindowCount = visibleWindows.size();
955                for (int i = visibleWindowCount - 1; i >= 0; i--) {
956                    WindowState windowState = visibleWindows.valueAt(i);
957
958                    // Compute the bounds in the screen.
959                    Rect boundsInScreen = mTempRect;
960                    computeWindowBoundsInScreen(windowState, boundsInScreen);
961
962                    final int flags = windowState.mAttrs.flags;
963
964                    // If the window is not touchable, do not report it but take into account
965                    // the space it takes since the content behind it cannot be touched.
966                    if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
967                        continue;
968                    }
969
970                    // If the window is completely covered by other windows - ignore.
971                    if (unaccountedSpace.quickReject(boundsInScreen)) {
972                        continue;
973                    }
974
975                    // Add windows of certain types not covered by modal windows.
976                    if (isReportedWindowType(windowState.mAttrs.type)) {
977                        // Add the window to the ones to be reported.
978                        WindowInfo window = obtainPopulatedWindowInfo(windowState, boundsInScreen);
979                        addedWindows.add(window.token);
980                        windows.add(window);
981                        if (windowState.isFocused()) {
982                            focusedWindowAdded = true;
983                        }
984                    }
985
986                    // Account for the space this window takes.
987                    unaccountedSpace.op(boundsInScreen, unaccountedSpace,
988                            Region.Op.REVERSE_DIFFERENCE);
989
990                    // We figured out what is touchable for the entire screen - done.
991                    if (unaccountedSpace.isEmpty()) {
992                        break;
993                    }
994
995                    // If a window is modal, no other below can be touched - done.
996                    if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
997                            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
998                        break;
999                    }
1000                }
1001
1002                // Always report the focused window.
1003                if (!focusedWindowAdded) {
1004                    for (int i = visibleWindowCount - 1; i >= 0; i--) {
1005                        WindowState windowState = visibleWindows.valueAt(i);
1006                        if (windowState.isFocused()) {
1007                            // Compute the bounds in the screen.
1008                            Rect boundsInScreen = mTempRect;
1009                            computeWindowBoundsInScreen(windowState, boundsInScreen);
1010
1011                            // Add the window to the ones to be reported.
1012                            WindowInfo window = obtainPopulatedWindowInfo(windowState,
1013                                    boundsInScreen);
1014                            addedWindows.add(window.token);
1015                            windows.add(window);
1016                            break;
1017                        }
1018                    }
1019                }
1020
1021                // Remove child/parent references to windows that were not added.
1022                final int windowCount = windows.size();
1023                for (int i = 0; i < windowCount; i++) {
1024                    WindowInfo window = windows.get(i);
1025                    if (!addedWindows.contains(window.parentToken)) {
1026                        window.parentToken = null;
1027                    }
1028                    if (window.childTokens != null) {
1029                        final int childTokenCount = window.childTokens.size();
1030                        for (int j = childTokenCount - 1; j >= 0; j--) {
1031                            if (!addedWindows.contains(window.childTokens.get(j))) {
1032                                window.childTokens.remove(j);
1033                            }
1034                        }
1035                        // Leave the child token list if empty.
1036                    }
1037                }
1038
1039                visibleWindows.clear();
1040                addedWindows.clear();
1041
1042                // We computed the windows and if they changed notify the client.
1043                boolean windowsChanged = false;
1044                if (mOldWindows.size() != windows.size()) {
1045                    // Different size means something changed.
1046                    windowsChanged = true;
1047                } else if (!mOldWindows.isEmpty() || !windows.isEmpty()) {
1048                    // Since we always traverse windows from high to low layer
1049                    // the old and new windows at the same index should be the
1050                    // same, otherwise something changed.
1051                    for (int i = 0; i < windowCount; i++) {
1052                        WindowInfo oldWindow = mOldWindows.get(i);
1053                        WindowInfo newWindow = windows.get(i);
1054                        // We do not care for layer changes given the window
1055                        // order does not change. This brings no new information
1056                        // to the clients.
1057                        if (windowChangedNoLayer(oldWindow, newWindow)) {
1058                            windowsChanged = true;
1059                            break;
1060                        }
1061                    }
1062                }
1063
1064                if (windowsChanged) {
1065                    if (DEBUG) {
1066                        Log.i(LOG_TAG, "Windows changed:" + windows);
1067                    }
1068                    // Remember the old windows to detect changes.
1069                    cacheWindows(windows);
1070                    // Announce the change.
1071                    mHandler.obtainMessage(MyHandler.MESSAGE_NOTIFY_WINDOWS_CHANGED,
1072                            windows).sendToTarget();
1073                } else {
1074                    if (DEBUG) {
1075                        Log.i(LOG_TAG, "No windows changed.");
1076                    }
1077                    // Recycle the nodes as we do not need them.
1078                    clearAndRecycleWindows(windows);
1079                }
1080            }
1081        }
1082
1083        private void computeWindowBoundsInScreen(WindowState windowState, Rect outBounds) {
1084            // Get the touchable frame.
1085            Region touchableRegion = mTempRegion1;
1086            windowState.getTouchableRegion(touchableRegion);
1087            Rect touchableFrame = mTempRect;
1088            touchableRegion.getBounds(touchableFrame);
1089
1090            // Move to origin as all transforms are captured by the matrix.
1091            RectF windowFrame = mTempRectF;
1092            windowFrame.set(touchableFrame);
1093            windowFrame.offset(-windowState.mFrame.left, -windowState.mFrame.top);
1094
1095            // Map the frame to get what appears on the screen.
1096            Matrix matrix = mTempMatrix;
1097            populateTransformationMatrixLocked(windowState, matrix);
1098            matrix.mapRect(windowFrame);
1099
1100            // Got the bounds.
1101            outBounds.set((int) windowFrame.left, (int) windowFrame.top,
1102                    (int) windowFrame.right, (int) windowFrame.bottom);
1103        }
1104
1105        private static WindowInfo obtainPopulatedWindowInfo(WindowState windowState,
1106                Rect boundsInScreen) {
1107            WindowInfo window = WindowInfo.obtain();
1108            window.type = windowState.mAttrs.type;
1109            window.layer = windowState.mLayer;
1110            window.token = windowState.mClient.asBinder();
1111
1112            WindowState attachedWindow = windowState.mAttachedWindow;
1113            if (attachedWindow != null) {
1114                window.parentToken = attachedWindow.mClient.asBinder();
1115            }
1116
1117            window.focused = windowState.isFocused();
1118            window.boundsInScreen.set(boundsInScreen);
1119
1120            final int childCount = windowState.mChildWindows.size();
1121            if (childCount > 0) {
1122                if (window.childTokens == null) {
1123                    window.childTokens = new ArrayList<IBinder>();
1124                }
1125                for (int j = 0; j < childCount; j++) {
1126                    WindowState child = windowState.mChildWindows.get(j);
1127                    window.childTokens.add(child.mClient.asBinder());
1128                }
1129            }
1130
1131            return window;
1132        }
1133
1134        private void cacheWindows(List<WindowInfo> windows) {
1135            final int oldWindowCount = mOldWindows.size();
1136            for (int i = oldWindowCount - 1; i >= 0; i--) {
1137                mOldWindows.remove(i).recycle();
1138            }
1139            final int newWindowCount = windows.size();
1140            for (int i = 0; i < newWindowCount; i++) {
1141                WindowInfo newWindow = windows.get(i);
1142                mOldWindows.add(WindowInfo.obtain(newWindow));
1143            }
1144        }
1145
1146        private boolean windowChangedNoLayer(WindowInfo oldWindow, WindowInfo newWindow) {
1147            if (oldWindow == newWindow) {
1148                return false;
1149            }
1150            if (oldWindow == null) {
1151                return true;
1152            }
1153            if (newWindow == null) {
1154                return true;
1155            }
1156            if (oldWindow.type != newWindow.type) {
1157                return true;
1158            }
1159            if (oldWindow.focused != newWindow.focused) {
1160                return true;
1161            }
1162            if (oldWindow.token == null) {
1163                if (newWindow.token != null) {
1164                    return true;
1165                }
1166            } else if (!oldWindow.token.equals(newWindow.token)) {
1167                return true;
1168            }
1169            if (oldWindow.parentToken == null) {
1170                if (newWindow.parentToken != null) {
1171                    return true;
1172                }
1173            } else if (!oldWindow.parentToken.equals(newWindow.parentToken)) {
1174                return true;
1175            }
1176            if (!oldWindow.boundsInScreen.equals(newWindow.boundsInScreen)) {
1177                return true;
1178            }
1179            if (oldWindow.childTokens != null && newWindow.childTokens != null
1180                    && !oldWindow.childTokens.equals(newWindow.childTokens)) {
1181                return true;
1182            }
1183            return false;
1184        }
1185
1186        private void clearAndRecycleWindows(List<WindowInfo> windows) {
1187            final int windowCount = windows.size();
1188            for (int i = windowCount - 1; i >= 0; i--) {
1189                windows.remove(i).recycle();
1190            }
1191        }
1192
1193        private static boolean isReportedWindowType(int windowType) {
1194            return (windowType != WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM
1195                    && windowType != WindowManager.LayoutParams.TYPE_WALLPAPER
1196                    && windowType != WindowManager.LayoutParams.TYPE_BOOT_PROGRESS
1197                    && windowType != WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY
1198                    && windowType != WindowManager.LayoutParams.TYPE_DRAG
1199                    && windowType != WindowManager.LayoutParams.TYPE_HIDDEN_NAV_CONSUMER
1200                    && windowType != WindowManager.LayoutParams.TYPE_POINTER
1201                    && windowType != WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND
1202                    && windowType != WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAY
1203                    && windowType != WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY
1204                    && windowType != WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY
1205                    && windowType != WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION);
1206        }
1207
1208        private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
1209            DisplayContent displayContent = mWindowManagerService
1210                    .getDefaultDisplayContentLocked();
1211            WindowList windowList = displayContent.getWindowList();
1212            final int windowCount = windowList.size();
1213            for (int i = 0; i < windowCount; i++) {
1214                WindowState windowState = windowList.get(i);
1215                if (windowState.isVisibleLw()) {
1216                    outWindows.put(windowState.mLayer, windowState);
1217                }
1218            }
1219        }
1220
1221        private class MyHandler extends Handler {
1222            public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;
1223            public static final int MESSAGE_NOTIFY_WINDOWS_CHANGED = 2;
1224
1225            public MyHandler(Looper looper) {
1226                super(looper, null, false);
1227            }
1228
1229            @Override
1230            @SuppressWarnings("unchecked")
1231            public void handleMessage(Message message) {
1232                switch (message.what) {
1233                    case MESSAGE_COMPUTE_CHANGED_WINDOWS: {
1234                        computeChangedWindows();
1235                    } break;
1236
1237                    case MESSAGE_NOTIFY_WINDOWS_CHANGED: {
1238                        List<WindowInfo> windows = (List<WindowInfo>) message.obj;
1239                        mCallback.onWindowsForAccessibilityChanged(windows);
1240                        clearAndRecycleWindows(windows);
1241                    } break;
1242                }
1243            }
1244        }
1245    }
1246}
1247