1 /*
2  * Copyright (C) 2016 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.settings.testutils.shadow;
18 
19 import static org.robolectric.util.ReflectionHelpers.ClassParameter.from;
20 
21 import android.hardware.input.IInputManager;
22 import android.hardware.input.InputManager;
23 import android.os.Handler;
24 
25 import org.robolectric.annotation.Implementation;
26 import org.robolectric.annotation.Implements;
27 import org.robolectric.util.ReflectionHelpers;
28 
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 
33 /*
34  * Shadow for {@ InputManager} that has assessors for registered {@link InputDeviceListener}s.
35  */
36 @Implements(value = InputManager.class, callThroughByDefault = false)
37 public class ShadowInputManager {
38 
39     private ArrayList<InputManager.InputDeviceListener> mInputDeviceListeners;
40 
41     @Implementation
__constructor__(IInputManager service)42     public void __constructor__(IInputManager service) {
43         mInputDeviceListeners = new ArrayList<>();
44     }
45 
46     @Implementation
getInstance()47     public static InputManager getInstance() {
48         return ReflectionHelpers.callConstructor(
49                 InputManager.class,
50                 from(IInputManager.class, null));
51     }
52 
53     @Implementation
registerInputDeviceListener(InputManager.InputDeviceListener listener, Handler handler)54     public void registerInputDeviceListener(InputManager.InputDeviceListener listener,
55             Handler handler) {
56         // TODO: Use handler.
57         if (!mInputDeviceListeners.contains(listener)) {
58             mInputDeviceListeners.add(listener);
59         }
60     }
61 
62     @Implementation
unregisterInputDeviceListener(InputManager.InputDeviceListener listener)63     public void unregisterInputDeviceListener(InputManager.InputDeviceListener listener) {
64         if (mInputDeviceListeners.contains(listener)) {
65             mInputDeviceListeners.remove(listener);
66         }
67     }
68 
69     // Non-Android accessor.
getRegisteredInputDeviceListeners()70     public List<InputManager.InputDeviceListener> getRegisteredInputDeviceListeners() {
71         return Collections.unmodifiableList(mInputDeviceListeners);
72     }
73 }
74