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.annotation.TargetApi;
20 import android.content.Context;
21 import android.hardware.input.InputManager;
22 import android.os.Build;
23 import android.os.Handler;
24 import android.view.InputDevice;
25 import android.view.MotionEvent;
26 
27 import java.util.HashMap;
28 import java.util.Map;
29 
30 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
31 public class InputManagerV16 implements InputManagerCompat {
32 
33     private final InputManager mInputManager;
34     private final Map<InputManagerCompat.InputDeviceListener, V16InputDeviceListener> mListeners;
35 
InputManagerV16(Context context)36     public InputManagerV16(Context context) {
37         mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
38         mListeners = new HashMap<InputManagerCompat.InputDeviceListener, V16InputDeviceListener>();
39     }
40 
41     @Override
getInputDevice(int id)42     public InputDevice getInputDevice(int id) {
43         return mInputManager.getInputDevice(id);
44     }
45 
46     @Override
getInputDeviceIds()47     public int[] getInputDeviceIds() {
48         return mInputManager.getInputDeviceIds();
49     }
50 
51     static class V16InputDeviceListener implements InputManager.InputDeviceListener {
52         final InputManagerCompat.InputDeviceListener mIDL;
53 
V16InputDeviceListener(InputDeviceListener idl)54         public V16InputDeviceListener(InputDeviceListener idl) {
55             mIDL = idl;
56         }
57 
58         @Override
onInputDeviceAdded(int deviceId)59         public void onInputDeviceAdded(int deviceId) {
60             mIDL.onInputDeviceAdded(deviceId);
61         }
62 
63         @Override
onInputDeviceChanged(int deviceId)64         public void onInputDeviceChanged(int deviceId) {
65             mIDL.onInputDeviceChanged(deviceId);
66         }
67 
68         @Override
onInputDeviceRemoved(int deviceId)69         public void onInputDeviceRemoved(int deviceId) {
70             mIDL.onInputDeviceRemoved(deviceId);
71         }
72 
73     }
74 
75     @Override
registerInputDeviceListener(InputDeviceListener listener, Handler handler)76     public void registerInputDeviceListener(InputDeviceListener listener, Handler handler) {
77         V16InputDeviceListener v16Listener = new V16InputDeviceListener(listener);
78         mInputManager.registerInputDeviceListener(v16Listener, handler);
79         mListeners.put(listener, v16Listener);
80     }
81 
82     @Override
unregisterInputDeviceListener(InputDeviceListener listener)83     public void unregisterInputDeviceListener(InputDeviceListener listener) {
84         V16InputDeviceListener curListener = mListeners.remove(listener);
85         if (null != curListener)
86         {
87             mInputManager.unregisterInputDeviceListener(curListener);
88         }
89 
90     }
91 
92     @Override
onGenericMotionEvent(MotionEvent event)93     public void onGenericMotionEvent(MotionEvent event) {
94         // unused in V16
95     }
96 
97     @Override
onPause()98     public void onPause() {
99         // unused in V16
100     }
101 
102     @Override
onResume()103     public void onResume() {
104         // unused in V16
105     }
106 
107 }
108