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.support.annotation.VisibleForTesting; 23 import android.support.v14.preference.SwitchPreference; 24 import android.support.v7.preference.Preference; 25 import android.support.v7.preference.PreferenceScreen; 26 import android.text.TextUtils; 27 import android.view.InputDevice; 28 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settingslib.core.AbstractPreferenceController; 31 import com.android.settingslib.core.lifecycle.LifecycleObserver; 32 import com.android.settingslib.core.lifecycle.events.OnPause; 33 import com.android.settingslib.core.lifecycle.events.OnResume; 34 35 import java.util.List; 36 37 public class GameControllerPreferenceController extends AbstractPreferenceController 38 implements PreferenceControllerMixin, InputManager.InputDeviceListener, LifecycleObserver, 39 OnResume, OnPause { 40 41 @VisibleForTesting 42 static final String PREF_KEY = "vibrate_input_devices"; 43 private static final String CATEGORY_KEY = "game_controller_settings_category"; 44 45 private final InputManager mIm; 46 47 private PreferenceScreen mScreen; 48 private Preference mCategory; 49 private Preference mPreference; 50 51 public GameControllerPreferenceController(Context context) { 52 super(context); 53 mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE); 54 } 55 56 @Override 57 public void onResume() { 58 mIm.registerInputDeviceListener(this, null); 59 } 60 61 @Override 62 public void onPause() { 63 mIm.unregisterInputDeviceListener(this); 64 } 65 66 @Override 67 public void displayPreference(PreferenceScreen screen) { 68 super.displayPreference(screen); 69 mScreen = screen; 70 mCategory = screen.findPreference(CATEGORY_KEY); 71 mPreference = screen.findPreference(PREF_KEY); 72 } 73 74 @Override 75 public boolean isAvailable() { 76 final int[] devices = mIm.getInputDeviceIds(); 77 for (int deviceId : devices) { 78 InputDevice device = mIm.getInputDevice(deviceId); 79 if (device != null && !device.isVirtual() && device.getVibrator().hasVibrator()) { 80 return true; 81 } 82 } 83 return false; 84 } 85 86 @Override 87 public boolean handlePreferenceTreeClick(Preference preference) { 88 if (TextUtils.equals(PREF_KEY, preference.getKey())) { 89 Settings.System.putInt(mContext.getContentResolver(), 90 Settings.System.VIBRATE_INPUT_DEVICES, 91 ((SwitchPreference) preference).isChecked() ? 1 : 0); 92 return true; 93 } 94 return false; 95 } 96 97 @Override 98 public String getPreferenceKey() { 99 return CATEGORY_KEY; 100 } 101 102 @Override 103 public void updateState(Preference preference) { 104 if (preference == null) { 105 return; 106 } 107 ((SwitchPreference) preference).setChecked(Settings.System.getInt( 108 mContext.getContentResolver(), 109 Settings.System.VIBRATE_INPUT_DEVICES, 1) > 0); 110 } 111 112 @Override 113 public void updateNonIndexableKeys(List<String> keys) { 114 if (!isAvailable()) { 115 keys.add(CATEGORY_KEY); 116 keys.add(PREF_KEY); 117 } 118 } 119 120 @Override 121 public void onInputDeviceAdded(int deviceId) { 122 updateGameControllers(); 123 } 124 125 @Override 126 public void onInputDeviceRemoved(int deviceId) { 127 updateGameControllers(); 128 } 129 130 @Override 131 public void onInputDeviceChanged(int deviceId) { 132 updateGameControllers(); 133 } 134 135 private void updateGameControllers() { 136 if (isAvailable()) { 137 mScreen.addPreference(mCategory); 138 updateState(mPreference); 139 } else { 140 if (mCategory != null) { 141 mScreen.removePreference(mCategory); 142 } 143 } 144 } 145 } 146