• 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.example.inputmanagercompat;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.os.Handler;
22 import android.view.InputDevice;
23 import android.view.MotionEvent;
24 
25 public interface InputManagerCompat {
26     /**
27      * Gets information about the input device with the specified id.
28      *
29      * @param id The device id
30      * @return The input device or null if not found
31      */
getInputDevice(int id)32     public InputDevice getInputDevice(int id);
33 
34     /**
35      * Gets the ids of all input devices in the system.
36      *
37      * @return The input device ids.
38      */
getInputDeviceIds()39     public int[] getInputDeviceIds();
40 
41     /**
42      * Registers an input device listener to receive notifications about when
43      * input devices are added, removed or changed.
44      *
45      * @param listener The listener to register.
46      * @param handler The handler on which the listener should be invoked, or
47      *            null if the listener should be invoked on the calling thread's
48      *            looper.
49      */
registerInputDeviceListener(InputManagerCompat.InputDeviceListener listener, Handler handler)50     public void registerInputDeviceListener(InputManagerCompat.InputDeviceListener listener,
51             Handler handler);
52 
53     /**
54      * Unregisters an input device listener.
55      *
56      * @param listener The listener to unregister.
57      */
unregisterInputDeviceListener(InputManagerCompat.InputDeviceListener listener)58     public void unregisterInputDeviceListener(InputManagerCompat.InputDeviceListener listener);
59 
60     /*
61      * The following three calls are to simulate V16 behavior on pre-Jellybean
62      * devices. If you don't call them, your callback will never be called
63      * pre-API 16.
64      */
65 
66     /**
67      * Pass the motion events to the InputManagerCompat. This is used to
68      * optimize for polling for controllers. If you do not pass these events in,
69      * polling will cause regular object creation.
70      *
71      * @param event the motion event from the app
72      */
onGenericMotionEvent(MotionEvent event)73     public void onGenericMotionEvent(MotionEvent event);
74 
75     /**
76      * Tell the V9 input manager that it should stop polling for disconnected
77      * devices. You can call this during onPause in your activity, although you
78      * might want to call it whenever your game is not active (or whenever you
79      * don't care about being notified of new input devices)
80      */
onPause()81     public void onPause();
82 
83     /**
84      * Tell the V9 input manager that it should start polling for disconnected
85      * devices. You can call this during onResume in your activity, although you
86      * might want to call it less often (only when the gameplay is actually
87      * active)
88      */
onResume()89     public void onResume();
90 
91     public interface InputDeviceListener {
92         /**
93          * Called whenever the input manager detects that a device has been
94          * added. This will only be called in the V9 version when a motion event
95          * is detected.
96          *
97          * @param deviceId The id of the input device that was added.
98          */
onInputDeviceAdded(int deviceId)99         void onInputDeviceAdded(int deviceId);
100 
101         /**
102          * Called whenever the properties of an input device have changed since
103          * they were last queried. This will not be called for the V9 version of
104          * the API.
105          *
106          * @param deviceId The id of the input device that changed.
107          */
onInputDeviceChanged(int deviceId)108         void onInputDeviceChanged(int deviceId);
109 
110         /**
111          * Called whenever the input manager detects that a device has been
112          * removed. For the V9 version, this can take some time depending on the
113          * poll rate.
114          *
115          * @param deviceId The id of the input device that was removed.
116          */
onInputDeviceRemoved(int deviceId)117         void onInputDeviceRemoved(int deviceId);
118     }
119 
120     /**
121      * Use this to construct a compatible InputManager.
122      */
123     public static class Factory {
124 
125         /**
126          * Constructs and returns a compatible InputManger
127          *
128          * @param context the Context that will be used to get the system
129          *            service from
130          * @return a compatible implementation of InputManager
131          */
getInputManager(Context context)132         public static InputManagerCompat getInputManager(Context context) {
133             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
134                 return new InputManagerV16(context);
135             } else {
136                 return new InputManagerV9();
137             }
138         }
139     }
140 }
141