blob: 6b0e4c9f010a2c65a9e2741d60aeae18059c9307 [file] [log] [blame]
Craig Mautner037aa8d2013-06-07 10:35:44 -07001/*
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
17package com.android.server.wm;
18
19import android.view.InputChannel;
20import android.view.InputDevice;
21import android.view.InputEvent;
22import android.view.InputEventReceiver;
23import android.view.MotionEvent;
24import android.view.WindowManagerPolicy.PointerEventListener;
25
26import com.android.server.UiThread;
27
28import java.util.ArrayList;
29
30public class PointerEventDispatcher extends InputEventReceiver {
31 ArrayList<PointerEventListener> mListeners = new ArrayList<PointerEventListener>();
32 PointerEventListener[] mListenersArray = new PointerEventListener[0];
33
34 public PointerEventDispatcher(InputChannel inputChannel) {
35 super(inputChannel, UiThread.getHandler().getLooper());
36 }
37
38 @Override
39 public void onInputEvent(InputEvent event) {
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);
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 */
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 */
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}