InputMonitor.java revision ff73dd9c60e8616e60abcaf62d1156ba08bebc99
1/*
2 * Copyright (C) 2010 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 static android.app.ActivityManager.StackId.PINNED_STACK_ID;
20import static android.view.Display.DEFAULT_DISPLAY;
21import static android.view.WindowManager.INPUT_CONSUMER_NAVIGATION;
22import static android.view.WindowManager.INPUT_CONSUMER_PIP;
23import static android.view.WindowManager.INPUT_CONSUMER_WALLPAPER;
24import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
25import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS;
26import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
27import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
28import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG;
29import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
30import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT;
31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
32import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
33
34import android.app.ActivityManager;
35import android.graphics.Rect;
36import android.os.Debug;
37import android.os.Looper;
38import android.os.RemoteException;
39import android.util.ArrayMap;
40import android.util.Log;
41import android.util.Slog;
42import android.view.InputChannel;
43import android.view.InputEventReceiver;
44import android.view.KeyEvent;
45import android.view.WindowManager;
46
47import android.view.WindowManagerPolicy;
48
49import com.android.server.input.InputApplicationHandle;
50import com.android.server.input.InputManagerService;
51import com.android.server.input.InputWindowHandle;
52
53import java.io.PrintWriter;
54import java.util.Arrays;
55import java.util.function.Consumer;
56
57final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
58    private final WindowManagerService mService;
59
60    // Current window with input focus for keys and other non-touch events.  May be null.
61    private WindowState mInputFocus;
62
63    // When true, prevents input dispatch from proceeding until set to false again.
64    private boolean mInputDispatchFrozen;
65
66    // The reason the input is currently frozen or null if the input isn't frozen.
67    private String mInputFreezeReason = null;
68
69    // When true, input dispatch proceeds normally.  Otherwise all events are dropped.
70    // Initially false, so that input does not get dispatched until boot is finished at
71    // which point the ActivityManager will enable dispatching.
72    private boolean mInputDispatchEnabled;
73
74    // When true, need to call updateInputWindowsLw().
75    private boolean mUpdateInputWindowsNeeded = true;
76
77    // Array of window handles to provide to the input dispatcher.
78    private InputWindowHandle[] mInputWindowHandles;
79    private int mInputWindowHandleCount;
80    private InputWindowHandle mFocusedInputWindowHandle;
81
82    private boolean mAddInputConsumerHandle;
83    private boolean mAddPipInputConsumerHandle;
84    private boolean mAddWallpaperInputConsumerHandle;
85    private boolean mDisableWallpaperTouchEvents;
86    private final Rect mTmpRect = new Rect();
87    private final UpdateInputForAllWindowsConsumer mUpdateInputForAllWindowsConsumer =
88            new UpdateInputForAllWindowsConsumer();
89
90    // Set to true when the first input device configuration change notification
91    // is received to indicate that the input devices are ready.
92    private final Object mInputDevicesReadyMonitor = new Object();
93    private boolean mInputDevicesReady;
94
95    /**
96     * The set of input consumer added to the window manager by name, which consumes input events
97     * for the windows below it.
98     */
99    private final ArrayMap<String, InputConsumerImpl> mInputConsumers = new ArrayMap();
100
101    private static final class EventReceiverInputConsumer extends InputConsumerImpl
102            implements WindowManagerPolicy.InputConsumer {
103        private InputMonitor mInputMonitor;
104        private final InputEventReceiver mInputEventReceiver;
105
106        EventReceiverInputConsumer(WindowManagerService service, InputMonitor monitor,
107                                   Looper looper, String name,
108                                   InputEventReceiver.Factory inputEventReceiverFactory) {
109            super(service, name, null);
110            mInputMonitor = monitor;
111            mInputEventReceiver = inputEventReceiverFactory.createInputEventReceiver(
112                    mClientChannel, looper);
113        }
114
115        @Override
116        public void dismiss() {
117            synchronized (mService.mWindowMap) {
118                if (mInputMonitor.destroyInputConsumer(mWindowHandle.name)) {
119                    mInputEventReceiver.dispose();
120                }
121            }
122        }
123    }
124
125    public InputMonitor(WindowManagerService service) {
126        mService = service;
127    }
128
129    private void addInputConsumer(String name, InputConsumerImpl consumer) {
130        mInputConsumers.put(name, consumer);
131        updateInputWindowsLw(true /* force */);
132    }
133
134    boolean destroyInputConsumer(String name) {
135        if (disposeInputConsumer(mInputConsumers.remove(name))) {
136            updateInputWindowsLw(true /* force */);
137            return true;
138        }
139        return false;
140    }
141
142    private boolean disposeInputConsumer(InputConsumerImpl consumer) {
143        if (consumer != null) {
144            consumer.disposeChannelsLw();
145            return true;
146        }
147        return false;
148    }
149
150    InputConsumerImpl getInputConsumer(String name, int displayId) {
151        // TODO(multi-display): Allow input consumers on non-default displays?
152        return (displayId == DEFAULT_DISPLAY) ? mInputConsumers.get(name) : null;
153    }
154
155    void layoutInputConsumers(int dw, int dh) {
156        for (int i = mInputConsumers.size() - 1; i >= 0; i--) {
157            mInputConsumers.valueAt(i).layout(dw, dh);
158        }
159    }
160
161    WindowManagerPolicy.InputConsumer createInputConsumer(Looper looper, String name,
162            InputEventReceiver.Factory inputEventReceiverFactory) {
163        if (mInputConsumers.containsKey(name)) {
164            throw new IllegalStateException("Existing input consumer found with name: " + name);
165        }
166
167        final EventReceiverInputConsumer consumer = new EventReceiverInputConsumer(mService,
168                this, looper, name, inputEventReceiverFactory);
169        addInputConsumer(name, consumer);
170        return consumer;
171    }
172
173    void createInputConsumer(String name, InputChannel inputChannel) {
174        if (mInputConsumers.containsKey(name)) {
175            throw new IllegalStateException("Existing input consumer found with name: " + name);
176        }
177
178        final InputConsumerImpl consumer = new InputConsumerImpl(mService, name, inputChannel);
179        switch (name) {
180            case INPUT_CONSUMER_WALLPAPER:
181                consumer.mWindowHandle.hasWallpaper = true;
182                break;
183            case INPUT_CONSUMER_PIP:
184                // The touchable region of the Pip input window is cropped to the bounds of the
185                // stack, and we need FLAG_NOT_TOUCH_MODAL to ensure other events fall through
186                consumer.mWindowHandle.layoutParamsFlags |= FLAG_NOT_TOUCH_MODAL;
187                break;
188        }
189        addInputConsumer(name, consumer);
190    }
191
192    /* Notifies the window manager about a broken input channel.
193     *
194     * Called by the InputManager.
195     */
196    @Override
197    public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
198        if (inputWindowHandle == null) {
199            return;
200        }
201
202        synchronized (mService.mWindowMap) {
203            WindowState windowState = (WindowState) inputWindowHandle.windowState;
204            if (windowState != null) {
205                Slog.i(TAG_WM, "WINDOW DIED " + windowState);
206                windowState.removeIfPossible();
207            }
208        }
209    }
210
211    /* Notifies the window manager about an application that is not responding.
212     * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
213     *
214     * Called by the InputManager.
215     */
216    @Override
217    public long notifyANR(InputApplicationHandle inputApplicationHandle,
218            InputWindowHandle inputWindowHandle, String reason) {
219        AppWindowToken appWindowToken = null;
220        WindowState windowState = null;
221        boolean aboveSystem = false;
222        synchronized (mService.mWindowMap) {
223            if (inputWindowHandle != null) {
224                windowState = (WindowState) inputWindowHandle.windowState;
225                if (windowState != null) {
226                    appWindowToken = windowState.mAppToken;
227                }
228            }
229            if (appWindowToken == null && inputApplicationHandle != null) {
230                appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
231            }
232
233            if (windowState != null) {
234                Slog.i(TAG_WM, "Input event dispatching timed out "
235                        + "sending to " + windowState.mAttrs.getTitle()
236                        + ".  Reason: " + reason);
237                // Figure out whether this window is layered above system windows.
238                // We need to do this here to help the activity manager know how to
239                // layer its ANR dialog.
240                int systemAlertLayer = mService.mPolicy.windowTypeToLayerLw(
241                        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
242                aboveSystem = windowState.mBaseLayer > systemAlertLayer;
243            } else if (appWindowToken != null) {
244                Slog.i(TAG_WM, "Input event dispatching timed out "
245                        + "sending to application " + appWindowToken.stringName
246                        + ".  Reason: " + reason);
247            } else {
248                Slog.i(TAG_WM, "Input event dispatching timed out "
249                        + ".  Reason: " + reason);
250            }
251
252            mService.saveANRStateLocked(appWindowToken, windowState, reason);
253        }
254
255        if (appWindowToken != null && appWindowToken.appToken != null) {
256            // Notify the activity manager about the timeout and let it decide whether
257            // to abort dispatching or keep waiting.
258            final AppWindowContainerController controller = appWindowToken.getController();
259            final boolean abort = controller != null && controller.keyDispatchingTimedOut(reason);
260            if (!abort) {
261                // The activity manager declined to abort dispatching.
262                // Wait a bit longer and timeout again later.
263                return appWindowToken.mInputDispatchingTimeoutNanos;
264            }
265        } else if (windowState != null) {
266            try {
267                // Notify the activity manager about the timeout and let it decide whether
268                // to abort dispatching or keep waiting.
269                long timeout = ActivityManager.getService().inputDispatchingTimedOut(
270                        windowState.mSession.mPid, aboveSystem, reason);
271                if (timeout >= 0) {
272                    // The activity manager declined to abort dispatching.
273                    // Wait a bit longer and timeout again later.
274                    return timeout * 1000000L; // nanoseconds
275                }
276            } catch (RemoteException ex) {
277            }
278        }
279        return 0; // abort dispatching
280    }
281
282    void addInputWindowHandle(final InputWindowHandle windowHandle) {
283        if (mInputWindowHandles == null) {
284            mInputWindowHandles = new InputWindowHandle[16];
285        }
286        if (mInputWindowHandleCount >= mInputWindowHandles.length) {
287            mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
288                    mInputWindowHandleCount * 2);
289        }
290        mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
291    }
292
293    void addInputWindowHandle(final InputWindowHandle inputWindowHandle,
294            final WindowState child, int flags, final int type, final boolean isVisible,
295            final boolean hasFocus, final boolean hasWallpaper) {
296        // Add a window to our list of input windows.
297        inputWindowHandle.name = child.toString();
298        flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags);
299        inputWindowHandle.layoutParamsFlags = flags;
300        inputWindowHandle.layoutParamsType = type;
301        inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
302        inputWindowHandle.visible = isVisible;
303        inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
304        inputWindowHandle.hasFocus = hasFocus;
305        inputWindowHandle.hasWallpaper = hasWallpaper;
306        inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
307        inputWindowHandle.layer = child.mLayer;
308        inputWindowHandle.ownerPid = child.mSession.mPid;
309        inputWindowHandle.ownerUid = child.mSession.mUid;
310        inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
311
312        final Rect frame = child.mFrame;
313        inputWindowHandle.frameLeft = frame.left;
314        inputWindowHandle.frameTop = frame.top;
315        inputWindowHandle.frameRight = frame.right;
316        inputWindowHandle.frameBottom = frame.bottom;
317
318        if (child.mGlobalScale != 1) {
319            // If we are scaling the window, input coordinates need
320            // to be inversely scaled to map from what is on screen
321            // to what is actually being touched in the UI.
322            inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
323        } else {
324            inputWindowHandle.scaleFactor = 1;
325        }
326
327        if (DEBUG_INPUT) {
328            Slog.d(TAG_WM, "addInputWindowHandle: "
329                    + child + ", " + inputWindowHandle);
330        }
331        addInputWindowHandle(inputWindowHandle);
332        if (hasFocus) {
333            mFocusedInputWindowHandle = inputWindowHandle;
334        }
335    }
336
337    private void clearInputWindowHandlesLw() {
338        while (mInputWindowHandleCount != 0) {
339            mInputWindowHandles[--mInputWindowHandleCount] = null;
340        }
341        mFocusedInputWindowHandle = null;
342    }
343
344    void setUpdateInputWindowsNeededLw() {
345        mUpdateInputWindowsNeeded = true;
346    }
347
348    /* Updates the cached window information provided to the input dispatcher. */
349    void updateInputWindowsLw(boolean force) {
350        if (!force && !mUpdateInputWindowsNeeded) {
351            return;
352        }
353        mUpdateInputWindowsNeeded = false;
354
355        if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw");
356
357        // Populate the input window list with information about all of the windows that
358        // could potentially receive input.
359        // As an optimization, we could try to prune the list of windows but this turns
360        // out to be difficult because only the native code knows for sure which window
361        // currently has touch focus.
362
363        // If there's a drag in flight, provide a pseudo-window to catch drag input
364        final boolean inDrag = (mService.mDragState != null);
365        if (inDrag) {
366            if (DEBUG_DRAG) {
367                Log.d(TAG_WM, "Inserting drag window");
368            }
369            final InputWindowHandle dragWindowHandle = mService.mDragState.getInputWindowHandle();
370            if (dragWindowHandle != null) {
371                addInputWindowHandle(dragWindowHandle);
372            } else {
373                Slog.w(TAG_WM, "Drag is in progress but there is no "
374                        + "drag window handle.");
375            }
376        }
377
378        final boolean inPositioning = (mService.mTaskPositioner != null);
379        if (inPositioning) {
380            if (DEBUG_TASK_POSITIONING) {
381                Log.d(TAG_WM, "Inserting window handle for repositioning");
382            }
383            final InputWindowHandle dragWindowHandle = mService.mTaskPositioner.mDragWindowHandle;
384            if (dragWindowHandle != null) {
385                addInputWindowHandle(dragWindowHandle);
386            } else {
387                Slog.e(TAG_WM,
388                        "Repositioning is in progress but there is no drag window handle.");
389            }
390        }
391
392        // Add all windows on the default display.
393        mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
394
395        if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw");
396    }
397
398    /* Notifies that the input device configuration has changed. */
399    @Override
400    public void notifyConfigurationChanged() {
401        // TODO(multi-display): Notify proper displays that are associated with this input device.
402        mService.sendNewConfiguration(DEFAULT_DISPLAY);
403
404        synchronized (mInputDevicesReadyMonitor) {
405            if (!mInputDevicesReady) {
406                mInputDevicesReady = true;
407                mInputDevicesReadyMonitor.notifyAll();
408            }
409        }
410    }
411
412    /* Waits until the built-in input devices have been configured. */
413    public boolean waitForInputDevicesReady(long timeoutMillis) {
414        synchronized (mInputDevicesReadyMonitor) {
415            if (!mInputDevicesReady) {
416                try {
417                    mInputDevicesReadyMonitor.wait(timeoutMillis);
418                } catch (InterruptedException ex) {
419                }
420            }
421            return mInputDevicesReady;
422        }
423    }
424
425    /* Notifies that the lid switch changed state. */
426    @Override
427    public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
428        mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
429    }
430
431    /* Notifies that the camera lens cover state has changed. */
432    @Override
433    public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
434        mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
435    }
436
437    /* Provides an opportunity for the window manager policy to intercept early key
438     * processing as soon as the key has been read from the device. */
439    @Override
440    public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
441        return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags);
442    }
443
444    /* Provides an opportunity for the window manager policy to intercept early motion event
445     * processing when the device is in a non-interactive state since these events are normally
446     * dropped. */
447    @Override
448    public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
449        return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive(
450                whenNanos, policyFlags);
451    }
452
453    /* Provides an opportunity for the window manager policy to process a key before
454     * ordinary dispatch. */
455    @Override
456    public long interceptKeyBeforeDispatching(
457            InputWindowHandle focus, KeyEvent event, int policyFlags) {
458        WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
459        return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
460    }
461
462    /* Provides an opportunity for the window manager policy to process a key that
463     * the application did not handle. */
464    @Override
465    public KeyEvent dispatchUnhandledKey(
466            InputWindowHandle focus, KeyEvent event, int policyFlags) {
467        WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
468        return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
469    }
470
471    /* Callback to get pointer layer. */
472    @Override
473    public int getPointerLayer() {
474        return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER)
475                * WindowManagerService.TYPE_LAYER_MULTIPLIER
476                + WindowManagerService.TYPE_LAYER_OFFSET;
477    }
478
479    /* Called when the current input focus changes.
480     * Layer assignment is assumed to be complete by the time this is called.
481     */
482    public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
483        if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) {
484            Slog.d(TAG_WM, "Input focus has changed to " + newWindow);
485        }
486
487        if (newWindow != mInputFocus) {
488            if (newWindow != null && newWindow.canReceiveKeys()) {
489                // Displaying a window implicitly causes dispatching to be unpaused.
490                // This is to protect against bugs if someone pauses dispatching but
491                // forgets to resume.
492                newWindow.mToken.paused = false;
493            }
494
495            mInputFocus = newWindow;
496            setUpdateInputWindowsNeededLw();
497
498            if (updateInputWindows) {
499                updateInputWindowsLw(false /*force*/);
500            }
501        }
502    }
503
504    public void setFocusedAppLw(AppWindowToken newApp) {
505        // Focused app has changed.
506        if (newApp == null) {
507            mService.mInputManager.setFocusedApplication(null);
508        } else {
509            final InputApplicationHandle handle = newApp.mInputApplicationHandle;
510            handle.name = newApp.toString();
511            handle.dispatchingTimeoutNanos = newApp.mInputDispatchingTimeoutNanos;
512
513            mService.mInputManager.setFocusedApplication(handle);
514        }
515    }
516
517    public void pauseDispatchingLw(WindowToken window) {
518        if (! window.paused) {
519            if (DEBUG_INPUT) {
520                Slog.v(TAG_WM, "Pausing WindowToken " + window);
521            }
522
523            window.paused = true;
524            updateInputWindowsLw(true /*force*/);
525        }
526    }
527
528    public void resumeDispatchingLw(WindowToken window) {
529        if (window.paused) {
530            if (DEBUG_INPUT) {
531                Slog.v(TAG_WM, "Resuming WindowToken " + window);
532            }
533
534            window.paused = false;
535            updateInputWindowsLw(true /*force*/);
536        }
537    }
538
539    public void freezeInputDispatchingLw() {
540        if (!mInputDispatchFrozen) {
541            if (DEBUG_INPUT) {
542                Slog.v(TAG_WM, "Freezing input dispatching");
543            }
544
545            mInputDispatchFrozen = true;
546
547            if (DEBUG_INPUT || true) {
548                mInputFreezeReason = Debug.getCallers(6);
549            }
550            updateInputDispatchModeLw();
551        }
552    }
553
554    public void thawInputDispatchingLw() {
555        if (mInputDispatchFrozen) {
556            if (DEBUG_INPUT) {
557                Slog.v(TAG_WM, "Thawing input dispatching");
558            }
559
560            mInputDispatchFrozen = false;
561            mInputFreezeReason = null;
562            updateInputDispatchModeLw();
563        }
564    }
565
566    public void setEventDispatchingLw(boolean enabled) {
567        if (mInputDispatchEnabled != enabled) {
568            if (DEBUG_INPUT) {
569                Slog.v(TAG_WM, "Setting event dispatching to " + enabled);
570            }
571
572            mInputDispatchEnabled = enabled;
573            updateInputDispatchModeLw();
574        }
575    }
576
577    private void updateInputDispatchModeLw() {
578        mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
579    }
580
581    void dump(PrintWriter pw, String prefix) {
582        if (mInputFreezeReason != null) {
583            pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
584        }
585    }
586
587    private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {
588
589        InputConsumerImpl navInputConsumer;
590        InputConsumerImpl pipInputConsumer;
591        InputConsumerImpl wallpaperInputConsumer;
592        Rect pipTouchableBounds;
593        boolean inDrag;
594        WallpaperController wallpaperController;
595
596        private void updateInputWindows(boolean inDrag) {
597
598            // TODO: multi-display
599            navInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION, DEFAULT_DISPLAY);
600            pipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP, DEFAULT_DISPLAY);
601            wallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER, DEFAULT_DISPLAY);
602            mAddInputConsumerHandle = navInputConsumer != null;
603            mAddPipInputConsumerHandle = pipInputConsumer != null;
604            mAddWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
605            mTmpRect.setEmpty();
606            pipTouchableBounds = mAddPipInputConsumerHandle ? mTmpRect : null;
607            mDisableWallpaperTouchEvents = false;
608            this.inDrag = inDrag;
609            wallpaperController = mService.mRoot.mWallpaperController;
610
611            mService.mRoot.forAllWindows(this, true /* traverseTopToBottom */);
612            if (mAddWallpaperInputConsumerHandle) {
613                // No visible wallpaper found, add the wallpaper input consumer at the end.
614                addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
615            }
616
617            // Send windows to native code.
618            mService.mInputManager.setInputWindows(mInputWindowHandles, mFocusedInputWindowHandle);
619
620            clearInputWindowHandlesLw();
621        }
622
623        @Override
624        public void accept(WindowState w) {
625            final InputChannel inputChannel = w.mInputChannel;
626            final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
627            if (inputChannel == null || inputWindowHandle == null || w.mRemoved
628                    || w.canReceiveTouchInput()) {
629                // Skip this window because it cannot possibly receive input.
630                return;
631            }
632
633            if (mAddPipInputConsumerHandle
634                    && w.getStackId() == PINNED_STACK_ID
635                    && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
636                // Update the bounds of the Pip input consumer to match the Pinned stack
637                w.getStack().getBounds(pipTouchableBounds);
638                pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
639                addInputWindowHandle(pipInputConsumer.mWindowHandle);
640                mAddPipInputConsumerHandle = false;
641            }
642
643            if (mAddInputConsumerHandle
644                    && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
645                addInputWindowHandle(navInputConsumer.mWindowHandle);
646                mAddInputConsumerHandle = false;
647            }
648
649            if (mAddWallpaperInputConsumerHandle) {
650                if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
651                    // Add the wallpaper input consumer above the first visible wallpaper.
652                    addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
653                    mAddWallpaperInputConsumerHandle = false;
654                }
655            }
656
657            final int flags = w.mAttrs.flags;
658            final int privateFlags = w.mAttrs.privateFlags;
659            final int type = w.mAttrs.type;
660
661            final boolean hasFocus = w == mInputFocus;
662            final boolean isVisible = w.isVisibleLw();
663            if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
664                mDisableWallpaperTouchEvents = true;
665            }
666            final boolean hasWallpaper = wallpaperController.isWallpaperTarget(w)
667                    && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
668                    && !mDisableWallpaperTouchEvents;
669
670            // If there's a drag in progress and 'child' is a potential drop target,
671            // make sure it's been told about the drag
672            if (inDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
673                mService.mDragState.sendDragStartedIfNeededLw(w);
674            }
675
676            addInputWindowHandle(
677                    inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
678        }
679    }
680}
681