Home | History | Annotate | Download | only in inputmethod
      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.admin.DevicePolicyManager;
     20 import android.content.Context;
     21 import android.content.pm.PackageManager;
     22 import android.support.v7.preference.Preference;
     23 import android.text.BidiFormatter;
     24 import android.view.inputmethod.InputMethodInfo;
     25 import android.view.inputmethod.InputMethodManager;
     26 
     27 import com.android.settings.R;
     28 import com.android.settings.core.PreferenceControllerMixin;
     29 import com.android.settingslib.core.AbstractPreferenceController;
     30 
     31 import java.util.ArrayList;
     32 import java.util.List;
     33 
     34 public class VirtualKeyboardPreferenceController extends AbstractPreferenceController
     35         implements PreferenceControllerMixin {
     36 
     37     private final InputMethodManager mImm;
     38     private final DevicePolicyManager mDpm;
     39     private final PackageManager mPm;
     40 
     41     public VirtualKeyboardPreferenceController(Context context) {
     42         super(context);
     43         mPm = mContext.getPackageManager();
     44         mDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
     45         mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
     46     }
     47 
     48     @Override
     49     public boolean isAvailable() {
     50         return mContext.getResources().getBoolean(R.bool.config_show_virtual_keyboard_pref);
     51     }
     52 
     53     @Override
     54     public String getPreferenceKey() {
     55         return "virtual_keyboard_pref";
     56     }
     57 
     58     @Override
     59     public void updateState(Preference preference) {
     60         final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList();
     61         if (imis == null) {
     62             preference.setSummary(R.string.summary_empty);
     63             return;
     64         }
     65 
     66         final List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
     67         final List<String> labels = new ArrayList<>();
     68 
     69         for (InputMethodInfo imi : imis) {
     70             final boolean isAllowedByOrganization = permittedList == null
     71                     || permittedList.contains(imi.getPackageName());
     72             if (!isAllowedByOrganization) {
     73                 continue;
     74             }
     75             labels.add(imi.loadLabel(mPm).toString());
     76         }
     77         if (labels.isEmpty()) {
     78             preference.setSummary(R.string.summary_empty);
     79             return;
     80         }
     81 
     82         final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
     83 
     84         String summary = null;
     85         for (String label : labels) {
     86             if (summary == null) {
     87                 summary = bidiFormatter.unicodeWrap(label);
     88             } else {
     89                 summary = mContext.getString(R.string.join_many_items_middle, summary,
     90                         bidiFormatter.unicodeWrap(label));
     91             }
     92         }
     93         preference.setSummary(summary);
     94     }
     95 }
     96