1 /*
2  * Copyright (C) 2019 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.car.settings.inputmethod;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.car.drivingstate.CarUxRestrictions;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.icu.text.ListFormatter;
24 import android.text.BidiFormatter;
25 import android.view.inputmethod.InputMethodInfo;
26 import android.view.inputmethod.InputMethodManager;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.car.settings.common.FragmentController;
31 import com.android.car.settings.common.PreferenceController;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /** Updates the keyboard settings entry summary with the currently enabled keyboard. */
37 public class KeyboardPreferenceController extends PreferenceController<Preference> {
38     private static final String SUMMARY_EMPTY = "";
39 
40     private final InputMethodManager mInputMethodManager;
41     private final DevicePolicyManager mDevicePolicyManager;
42     private final PackageManager mPackageManager;
43 
KeyboardPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public KeyboardPreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47         mPackageManager = context.getPackageManager();
48         mDevicePolicyManager =
49                 (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
50         mInputMethodManager =
51                 (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
52     }
53 
54     @Override
getPreferenceType()55     protected Class<Preference> getPreferenceType() {
56         return Preference.class;
57     }
58 
59     @Override
updateState(Preference preference)60     protected void updateState(Preference preference) {
61         List<InputMethodInfo> inputMethodInfos =
62                 InputMethodUtil.getPermittedAndEnabledInputMethodList(
63                     mInputMethodManager, mDevicePolicyManager);
64         if (inputMethodInfos == null) {
65             preference.setSummary(SUMMARY_EMPTY);
66             return;
67         }
68 
69         List<String> labels = new ArrayList<>();
70         for (InputMethodInfo inputMethodInfo : inputMethodInfos) {
71             labels.add(InputMethodUtil.getPackageLabel(mPackageManager, inputMethodInfo));
72         }
73         if (labels.isEmpty()) {
74             preference.setSummary(SUMMARY_EMPTY);
75             return;
76         }
77 
78         BidiFormatter bidiFormatter = BidiFormatter.getInstance();
79 
80         List<String> summaries = new ArrayList<>();
81         for (String label : labels) {
82             summaries.add(bidiFormatter.unicodeWrap(label));
83         }
84         preference.setSummary(ListFormatter.getInstance().format(summaries));
85     }
86 }
87