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