1 /* 2 * Copyright (C) 2017 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.support.v7.preference.Preference; 22 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; 31 32 import java.util.List; 33 34 public class PhysicalKeyboardPreferenceController extends AbstractPreferenceController 35 implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause, 36 InputManager.InputDeviceListener { 37 38 private final InputManager mIm; 39 40 private Preference mPreference; 41 42 public PhysicalKeyboardPreferenceController(Context context, Lifecycle lifecycle) { 43 super(context); 44 mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE); 45 46 if (lifecycle != null) { 47 lifecycle.addObserver(this); 48 } 49 } 50 51 @Override 52 public boolean isAvailable() { 53 return true; 54 } 55 56 @Override 57 public void updateState(Preference preference) { 58 mPreference = preference; 59 updateSummary(); 60 } 61 62 @Override 63 public String getPreferenceKey() { 64 return "physical_keyboard_pref"; 65 } 66 67 @Override 68 public void onPause() { 69 mIm.registerInputDeviceListener(this, null); 70 } 71 72 @Override 73 public void onResume() { 74 mIm.unregisterInputDeviceListener(this); 75 } 76 77 @Override 78 public void onInputDeviceAdded(int deviceId) { 79 updateSummary(); 80 } 81 82 @Override 83 public void onInputDeviceRemoved(int deviceId) { 84 updateSummary(); 85 } 86 87 @Override 88 public void onInputDeviceChanged(int deviceId) { 89 updateSummary(); 90 } 91 92 private void updateSummary() { 93 if (mPreference == null) { 94 return; 95 } 96 final List<HardKeyboardDeviceInfo> keyboards = 97 PhysicalKeyboardFragment.getHardKeyboards(); 98 if (keyboards.isEmpty()) { 99 mPreference.setSummary(R.string.disconnected); 100 return; 101 } 102 String summary = null; 103 for (HardKeyboardDeviceInfo info : keyboards) { 104 if (summary == null) { 105 summary = info.mDeviceName; 106 } else { 107 summary = mContext.getString(R.string.join_many_items_middle, summary, 108 info.mDeviceName); 109 } 110 } 111 mPreference.setSummary(summary); 112 } 113 } 114