• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.inputmethod;
18 
19 import android.content.Context;
20 import android.hardware.input.InputManager;
21 import android.provider.Settings;
22 import android.view.InputDevice;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.PreferenceControllerMixin;
29 import com.android.settings.core.TogglePreferenceController;
30 import com.android.settingslib.core.lifecycle.LifecycleObserver;
31 import com.android.settingslib.core.lifecycle.events.OnPause;
32 import com.android.settingslib.core.lifecycle.events.OnResume;
33 
34 public class GameControllerPreferenceController extends TogglePreferenceController
35         implements PreferenceControllerMixin, InputManager.InputDeviceListener, LifecycleObserver,
36         OnResume, OnPause {
37 
38     private final InputManager mIm;
39 
40     private Preference mPreference;
41 
GameControllerPreferenceController(Context context, String key)42     public GameControllerPreferenceController(Context context, String key) {
43         super(context, key);
44         mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
45     }
46 
47     @Override
onResume()48     public void onResume() {
49         mIm.registerInputDeviceListener(this, null);
50     }
51 
52     @Override
onPause()53     public void onPause() {
54         mIm.unregisterInputDeviceListener(this);
55     }
56 
57     @Override
displayPreference(PreferenceScreen screen)58     public void displayPreference(PreferenceScreen screen) {
59         super.displayPreference(screen);
60         mPreference = screen.findPreference(getPreferenceKey());
61     }
62 
63     @Override
64     @AvailabilityStatus
getAvailabilityStatus()65     public int getAvailabilityStatus() {
66         // If device explicitly wants to hide this, return early.
67         if (!mContext.getResources().getBoolean(R.bool.config_show_vibrate_input_devices)) {
68             return UNSUPPORTED_ON_DEVICE;
69         }
70 
71         final int[] devices = mIm.getInputDeviceIds();
72         for (int deviceId : devices) {
73             InputDevice device = mIm.getInputDevice(deviceId);
74             if (device != null && !device.isVirtual() && device.getVibrator().hasVibrator()) {
75                 return AVAILABLE;
76             }
77         }
78         return CONDITIONALLY_UNAVAILABLE;
79     }
80 
81     @Override
updateState(Preference preference)82     public void updateState(Preference preference) {
83         super.updateState(preference);
84         if (preference == null) {
85             return;
86         }
87         mPreference.setVisible(isAvailable());
88     }
89 
90     @Override
isChecked()91     public boolean isChecked() {
92         return Settings.System.getInt(
93                 mContext.getContentResolver(),
94                 Settings.System.VIBRATE_INPUT_DEVICES, 1) > 0;
95     }
96 
97     @Override
setChecked(boolean isChecked)98     public boolean setChecked(boolean isChecked) {
99         return Settings.System.putInt(mContext.getContentResolver(),
100                 Settings.System.VIBRATE_INPUT_DEVICES, isChecked ? 1 : 0);
101     }
102 
103     @Override
onInputDeviceAdded(int deviceId)104     public void onInputDeviceAdded(int deviceId) {
105         updateState(mPreference);
106     }
107 
108     @Override
onInputDeviceRemoved(int deviceId)109     public void onInputDeviceRemoved(int deviceId) {
110         updateState(mPreference);
111     }
112 
113     @Override
onInputDeviceChanged(int deviceId)114     public void onInputDeviceChanged(int deviceId) {
115         updateState(mPreference);
116     }
117 }
118