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 = context.getSystemService(DevicePolicyManager.class);
49         mInputMethodManager = context.getSystemService(InputMethodManager.class);
50     }
51 
52     @Override
getPreferenceType()53     protected Class<Preference> getPreferenceType() {
54         return Preference.class;
55     }
56 
57     @Override
updateState(Preference preference)58     protected void updateState(Preference preference) {
59         List<InputMethodInfo> inputMethodInfos =
60                 InputMethodUtil.getPermittedAndEnabledInputMethodList(
61                     mInputMethodManager, mDevicePolicyManager);
62         if (inputMethodInfos == null) {
63             preference.setSummary(SUMMARY_EMPTY);
64             return;
65         }
66 
67         List<String> labels = new ArrayList<>();
68         for (InputMethodInfo inputMethodInfo : inputMethodInfos) {
69             labels.add(InputMethodUtil.getPackageLabel(mPackageManager, inputMethodInfo));
70         }
71         if (labels.isEmpty()) {
72             preference.setSummary(SUMMARY_EMPTY);
73             return;
74         }
75 
76         BidiFormatter bidiFormatter = BidiFormatter.getInstance();
77 
78         List<String> summaries = new ArrayList<>();
79         for (String label : labels) {
80             summaries.add(bidiFormatter.unicodeWrap(label));
81         }
82         preference.setSummary(ListFormatter.getInstance().format(summaries));
83     }
84 }
85