2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.inputmethod;
19 import android.content.Context;
20 import android.hardware.input.InputManager;
21 import android.support.v7.preference.Preference;
23 import com.android.settings.R;
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settings.inputmethod.PhysicalKeyboardFragment.HardKeyboardDeviceInfo;
26 import com.android.settingslib.core.AbstractPreferenceController;
27 import com.android.settingslib.core.lifecycle.Lifecycle;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnPause;
30 import com.android.settingslib.core.lifecycle.events.OnResume;
32 import java.util.List;
34 public class PhysicalKeyboardPreferenceController extends AbstractPreferenceController
35 implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause,
36 InputManager.InputDeviceListener {
38 private final InputManager mIm;
40 private Preference mPreference;
42 public PhysicalKeyboardPreferenceController(Context context, Lifecycle lifecycle) {
44 mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
46 if (lifecycle != null) {
47 lifecycle.addObserver(this);
52 public boolean isAvailable() {
53 return mContext.getResources().getBoolean(R.bool.config_show_physical_keyboard_pref);
57 public void updateState(Preference preference) {
58 mPreference = preference;
63 public String getPreferenceKey() {
64 return "physical_keyboard_pref";
68 public void onPause() {
69 mIm.registerInputDeviceListener(this, null);
73 public void onResume() {
74 mIm.unregisterInputDeviceListener(this);
78 public void onInputDeviceAdded(int deviceId) {
83 public void onInputDeviceRemoved(int deviceId) {
88 public void onInputDeviceChanged(int deviceId) {
92 private void updateSummary() {
93 if (mPreference == null) {
96 final List<HardKeyboardDeviceInfo> keyboards =
97 PhysicalKeyboardFragment.getHardKeyboards();
98 if (keyboards.isEmpty()) {
99 mPreference.setSummary(R.string.disconnected);
102 String summary = null;
103 for (HardKeyboardDeviceInfo info : keyboards) {
104 if (summary == null) {
105 summary = info.mDeviceName;
107 summary = mContext.getString(R.string.join_many_items_middle, summary,
111 mPreference.setSummary(summary);