Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.systemui.pip.phone; |
| 18 | |
| 19 | import static android.view.WindowManager.INPUT_CONSUMER_PIP; |
| 20 | |
Winson Chung | 6463c36 | 2017-09-25 16:23:26 -0700 | [diff] [blame^] | 21 | import android.os.Binder; |
| 22 | import android.os.IBinder; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 23 | import android.os.Looper; |
| 24 | import android.os.RemoteException; |
| 25 | import android.util.Log; |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 26 | import android.view.BatchedInputEventReceiver; |
| 27 | import android.view.Choreographer; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 28 | import android.view.InputChannel; |
| 29 | import android.view.InputEvent; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 30 | import android.view.IWindowManager; |
| 31 | import android.view.MotionEvent; |
| 32 | |
| 33 | import java.io.PrintWriter; |
| 34 | |
| 35 | /** |
| 36 | * Manages the input consumer that allows the SystemUI to control the PiP. |
| 37 | */ |
| 38 | public class InputConsumerController { |
| 39 | |
| 40 | private static final String TAG = InputConsumerController.class.getSimpleName(); |
| 41 | |
| 42 | /** |
| 43 | * Listener interface for callers to subscribe to touch events. |
| 44 | */ |
| 45 | public interface TouchListener { |
| 46 | boolean onTouchEvent(MotionEvent ev); |
| 47 | } |
| 48 | |
| 49 | /** |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 50 | * Listener interface for callers to learn when this class is registered or unregistered with |
| 51 | * window manager |
| 52 | */ |
| 53 | public interface RegistrationListener { |
| 54 | void onRegistrationChanged(boolean isRegistered); |
| 55 | } |
| 56 | |
| 57 | /** |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 58 | * Input handler used for the PiP input consumer. Input events are batched and consumed with the |
| 59 | * SurfaceFlinger vsync. |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 60 | */ |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 61 | private final class PipInputEventReceiver extends BatchedInputEventReceiver { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 62 | |
| 63 | public PipInputEventReceiver(InputChannel inputChannel, Looper looper) { |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 64 | super(inputChannel, looper, Choreographer.getSfInstance()); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 68 | public void onInputEvent(InputEvent event, int displayId) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 69 | boolean handled = true; |
| 70 | try { |
| 71 | // To be implemented for input handling over Pip windows |
| 72 | if (mListener != null && event instanceof MotionEvent) { |
| 73 | MotionEvent ev = (MotionEvent) event; |
| 74 | handled = mListener.onTouchEvent(ev); |
| 75 | } |
| 76 | } finally { |
| 77 | finishInputEvent(event, handled); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Winson Chung | 6463c36 | 2017-09-25 16:23:26 -0700 | [diff] [blame^] | 82 | private final IWindowManager mWindowManager; |
| 83 | private final IBinder mToken; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 84 | |
| 85 | private PipInputEventReceiver mInputEventReceiver; |
| 86 | private TouchListener mListener; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 87 | private RegistrationListener mRegistrationListener; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 88 | |
| 89 | public InputConsumerController(IWindowManager windowManager) { |
| 90 | mWindowManager = windowManager; |
Winson Chung | 6463c36 | 2017-09-25 16:23:26 -0700 | [diff] [blame^] | 91 | mToken = new Binder(); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 92 | registerInputConsumer(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets the touch listener. |
| 97 | */ |
| 98 | public void setTouchListener(TouchListener listener) { |
| 99 | mListener = listener; |
| 100 | } |
| 101 | |
| 102 | /** |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 103 | * Sets the registration listener. |
| 104 | */ |
| 105 | public void setRegistrationListener(RegistrationListener listener) { |
| 106 | mRegistrationListener = listener; |
Winson Chung | fe1fa64 | 2017-03-13 10:51:22 -0700 | [diff] [blame] | 107 | if (mRegistrationListener != null) { |
| 108 | mRegistrationListener.onRegistrationChanged(mInputEventReceiver != null); |
| 109 | } |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Check if the InputConsumer is currently registered with WindowManager |
| 114 | * |
| 115 | * @return {@code true} if registered, {@code false} if not. |
| 116 | */ |
| 117 | public boolean isRegistered() { |
| 118 | return mInputEventReceiver != null; |
| 119 | } |
| 120 | |
| 121 | /** |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 122 | * Registers the input consumer. |
| 123 | */ |
| 124 | public void registerInputConsumer() { |
| 125 | if (mInputEventReceiver == null) { |
| 126 | final InputChannel inputChannel = new InputChannel(); |
| 127 | try { |
| 128 | mWindowManager.destroyInputConsumer(INPUT_CONSUMER_PIP); |
Winson Chung | 6463c36 | 2017-09-25 16:23:26 -0700 | [diff] [blame^] | 129 | mWindowManager.createInputConsumer(mToken, INPUT_CONSUMER_PIP, inputChannel); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 130 | } catch (RemoteException e) { |
| 131 | Log.e(TAG, "Failed to create PIP input consumer", e); |
| 132 | } |
| 133 | mInputEventReceiver = new PipInputEventReceiver(inputChannel, Looper.myLooper()); |
Winson Chung | fe1fa64 | 2017-03-13 10:51:22 -0700 | [diff] [blame] | 134 | if (mRegistrationListener != null) { |
| 135 | mRegistrationListener.onRegistrationChanged(true /* isRegistered */); |
| 136 | } |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Unregisters the input consumer. |
| 142 | */ |
| 143 | public void unregisterInputConsumer() { |
| 144 | if (mInputEventReceiver != null) { |
| 145 | try { |
| 146 | mWindowManager.destroyInputConsumer(INPUT_CONSUMER_PIP); |
| 147 | } catch (RemoteException e) { |
| 148 | Log.e(TAG, "Failed to destroy PIP input consumer", e); |
| 149 | } |
| 150 | mInputEventReceiver.dispose(); |
| 151 | mInputEventReceiver = null; |
Winson Chung | fe1fa64 | 2017-03-13 10:51:22 -0700 | [diff] [blame] | 152 | if (mRegistrationListener != null) { |
| 153 | mRegistrationListener.onRegistrationChanged(false /* isRegistered */); |
| 154 | } |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | public void dump(PrintWriter pw, String prefix) { |
| 159 | final String innerPrefix = prefix + " "; |
| 160 | pw.println(prefix + TAG); |
| 161 | pw.println(innerPrefix + "registered=" + (mInputEventReceiver != null)); |
| 162 | } |
| 163 | } |