• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.server.wm;
18 
19 import android.view.InputChannel;
20 import android.view.InputDevice;
21 import android.view.InputEvent;
22 import android.view.InputEventReceiver;
23 import android.view.MotionEvent;
24 import android.view.WindowManagerPolicy.PointerEventListener;
25 
26 import com.android.server.UiThread;
27 
28 import java.util.ArrayList;
29 
30 public class PointerEventDispatcher extends InputEventReceiver {
31     ArrayList<PointerEventListener> mListeners = new ArrayList<PointerEventListener>();
32     PointerEventListener[] mListenersArray = new PointerEventListener[0];
33 
PointerEventDispatcher(InputChannel inputChannel)34     public PointerEventDispatcher(InputChannel inputChannel) {
35         super(inputChannel, UiThread.getHandler().getLooper());
36     }
37 
38     @Override
onInputEvent(InputEvent event, int displayId)39     public void onInputEvent(InputEvent event, int displayId) {
40         try {
41             if (event instanceof MotionEvent
42                     && (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
43                 final MotionEvent motionEvent = (MotionEvent) event;
44                 PointerEventListener[] listeners;
45                 synchronized (mListeners) {
46                     if (mListenersArray == null) {
47                         mListenersArray = new PointerEventListener[mListeners.size()];
48                         mListeners.toArray(mListenersArray);
49                     }
50                     listeners = mListenersArray;
51                 }
52                 for (int i = 0; i < listeners.length; ++i) {
53                     listeners[i].onPointerEvent(motionEvent, displayId);
54                 }
55             }
56         } finally {
57             finishInputEvent(event, false);
58         }
59     }
60 
61     /**
62      * Add the specified listener to the list.
63      * @param listener The listener to add.
64      */
registerInputEventListener(PointerEventListener listener)65     public void registerInputEventListener(PointerEventListener listener) {
66         synchronized (mListeners) {
67             if (mListeners.contains(listener)) {
68                 throw new IllegalStateException("registerInputEventListener: trying to register" +
69                         listener + " twice.");
70             }
71             mListeners.add(listener);
72             mListenersArray = null;
73         }
74     }
75 
76     /**
77      * Remove the specified listener from the list.
78      * @param listener The listener to remove.
79      */
unregisterInputEventListener(PointerEventListener listener)80     public void unregisterInputEventListener(PointerEventListener listener) {
81         synchronized (mListeners) {
82             if (!mListeners.contains(listener)) {
83                 throw new IllegalStateException("registerInputEventListener: " + listener +
84                         " not registered.");
85             }
86             mListeners.remove(listener);
87             mListenersArray = null;
88         }
89     }
90 }
91