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