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.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.hardware.input.InputManager;
23 import android.icu.text.ListFormatter;
24 import android.provider.Settings;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.settings.R;
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settings.inputmethod.PhysicalKeyboardFragment.HardKeyboardDeviceInfo;
32 import com.android.settingslib.core.AbstractPreferenceController;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 import com.android.settingslib.core.lifecycle.LifecycleObserver;
35 import com.android.settingslib.core.lifecycle.events.OnPause;
36 import com.android.settingslib.core.lifecycle.events.OnResume;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 public class PhysicalKeyboardPreferenceController extends AbstractPreferenceController
42         implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause,
43         InputManager.InputDeviceListener {
44 
45     private final InputManager mIm;
46 
47     private Preference mPreference;
48 
PhysicalKeyboardPreferenceController(Context context, Lifecycle lifecycle)49     public PhysicalKeyboardPreferenceController(Context context, Lifecycle lifecycle) {
50         super(context);
51         mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
52 
53         if (lifecycle != null) {
54             lifecycle.addObserver(this);
55         }
56     }
57 
58     @Override
isAvailable()59     public boolean isAvailable() {
60         return !getKeyboards().isEmpty()
61                 && mContext.getResources().getBoolean(R.bool.config_show_physical_keyboard_pref);
62     }
63 
64     @Override
handlePreferenceTreeClick(Preference preference)65     public boolean handlePreferenceTreeClick(Preference preference) {
66         if (!getPreferenceKey().equals(preference.getKey())) {
67             return false;
68         }
69         Intent intent = new Intent(Settings.ACTION_HARD_KEYBOARD_SETTINGS);
70         intent.setPackage(mContext.getPackageName());
71         intent.putExtra(Settings.EXTRA_ENTRYPOINT, SettingsEnums.KEYBOARD_SETTINGS);
72         mContext.startActivity(intent);
73         return true;
74     }
75 
76     @Override
updateState(Preference preference)77     public void updateState(Preference preference) {
78         mPreference = preference;
79         updateEntry();
80     }
81 
82     @Override
getPreferenceKey()83     public String getPreferenceKey() {
84         return "physical_keyboard_pref";
85     }
86 
87     @Override
onPause()88     public void onPause() {
89         mIm.unregisterInputDeviceListener(this);
90     }
91 
92     @Override
onResume()93     public void onResume() {
94         mIm.registerInputDeviceListener(this, null);
95     }
96 
97     @Override
onInputDeviceAdded(int deviceId)98     public void onInputDeviceAdded(int deviceId) {
99         updateEntry();
100     }
101 
102     @Override
onInputDeviceRemoved(int deviceId)103     public void onInputDeviceRemoved(int deviceId) {
104         updateEntry();
105     }
106 
107     @Override
onInputDeviceChanged(int deviceId)108     public void onInputDeviceChanged(int deviceId) {
109         updateEntry();
110     }
111 
updateEntry()112     private void updateEntry() {
113         if (mPreference == null) {
114             return;
115         }
116         List<HardKeyboardDeviceInfo> keyboards = getKeyboards();
117         if (keyboards.isEmpty()) {
118             mPreference.setVisible(false);
119             return;
120         }
121         updateSummary(keyboards);
122     }
123 
updateSummary(List<HardKeyboardDeviceInfo> keyboards)124     private void updateSummary(List<HardKeyboardDeviceInfo> keyboards) {
125         mPreference.setVisible(true);
126         List<String> summaries = new ArrayList<>();
127         for (HardKeyboardDeviceInfo info : keyboards) {
128             summaries.add(info.mDeviceName);
129         }
130         mPreference.setSummary(ListFormatter.getInstance().format(summaries));
131     }
132 
133     @VisibleForTesting
getKeyboards()134     List<HardKeyboardDeviceInfo> getKeyboards() {
135         return PhysicalKeyboardFragment.getHardKeyboards(mContext);
136     }
137 }
138