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