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.PreferenceController;
25 import com.android.settings.inputmethod.PhysicalKeyboardFragment.HardKeyboardDeviceInfo;
26 import com.android.settingslib.core.lifecycle.Lifecycle;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnPause;
29 import com.android.settingslib.core.lifecycle.events.OnResume;
31 import java.util.List;
33 public class PhysicalKeyboardPreferenceController extends PreferenceController implements
34 LifecycleObserver, OnResume, OnPause, InputManager.InputDeviceListener {
36 private final InputManager mIm;
38 private Preference mPreference;
40 public PhysicalKeyboardPreferenceController(Context context, Lifecycle lifecycle) {
42 mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
44 if (lifecycle != null) {
45 lifecycle.addObserver(this);
50 public boolean isAvailable() {
55 public void updateState(Preference preference) {
56 mPreference = preference;
61 public String getPreferenceKey() {
62 return "physical_keyboard_pref";
66 public void onPause() {
67 mIm.registerInputDeviceListener(this, null);
71 public void onResume() {
72 mIm.unregisterInputDeviceListener(this);
76 public void onInputDeviceAdded(int deviceId) {
81 public void onInputDeviceRemoved(int deviceId) {
86 public void onInputDeviceChanged(int deviceId) {
90 private void updateSummary() {
91 if (mPreference == null) {
94 final List<HardKeyboardDeviceInfo> keyboards =
95 PhysicalKeyboardFragment.getHardKeyboards();
96 if (keyboards.isEmpty()) {
97 mPreference.setSummary(R.string.disconnected);
100 String summary = null;
101 for (HardKeyboardDeviceInfo info : keyboards) {
102 if (summary == null) {
103 summary = info.mDeviceName;
105 summary = mContext.getString(R.string.join_many_items_middle, summary,
109 mPreference.setSummary(summary);