Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2016 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.Activity;
     20 import android.app.admin.DevicePolicyManager;
     21 import android.content.Context;
     22 import android.graphics.Color;
     23 import android.graphics.drawable.ColorDrawable;
     24 import android.graphics.drawable.Drawable;
     25 import android.os.Bundle;
     26 import android.support.v7.preference.Preference;
     27 import android.view.inputmethod.InputMethodInfo;
     28 import android.view.inputmethod.InputMethodManager;
     29 import com.android.internal.logging.MetricsProto.MetricsEvent;
     30 import com.android.internal.util.Preconditions;
     31 import com.android.settings.R;
     32 import com.android.settings.SettingsPreferenceFragment;
     33 
     34 import java.text.Collator;
     35 import java.util.ArrayList;
     36 import java.util.Collections;
     37 import java.util.Comparator;
     38 import java.util.List;
     39 
     40 public final class VirtualKeyboardFragment extends SettingsPreferenceFragment {
     41 
     42     private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen";
     43     private static final Drawable NO_ICON = new ColorDrawable(Color.TRANSPARENT);
     44 
     45     private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>();
     46     private InputMethodManager mImm;
     47     private DevicePolicyManager mDpm;
     48     private Preference mAddVirtualKeyboardScreen;
     49 
     50     @Override
     51     public void onCreatePreferences(Bundle bundle, String s) {
     52         Activity activity = Preconditions.checkNotNull(getActivity());
     53         addPreferencesFromResource(R.xml.virtual_keyboard_settings);
     54         mImm = Preconditions.checkNotNull(activity.getSystemService(InputMethodManager.class));
     55         mDpm = Preconditions.checkNotNull(activity.getSystemService(DevicePolicyManager.class));
     56         mAddVirtualKeyboardScreen = Preconditions.checkNotNull(
     57                 findPreference(ADD_VIRTUAL_KEYBOARD_SCREEN));
     58     }
     59 
     60     @Override
     61     public void onResume() {
     62         super.onResume();
     63         // Refresh internal states in mInputMethodSettingValues to keep the latest
     64         // "InputMethodInfo"s and "InputMethodSubtype"s
     65         updateInputMethodPreferenceViews();
     66     }
     67 
     68     @Override
     69     protected int getMetricsCategory() {
     70         return MetricsEvent.VIRTUAL_KEYBOARDS;
     71     }
     72 
     73     private void updateInputMethodPreferenceViews() {
     74         // Clear existing "InputMethodPreference"s
     75         mInputMethodPreferenceList.clear();
     76         List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
     77         final Context context = getPrefContext();
     78         final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList();
     79         final int N = (imis == null ? 0 : imis.size());
     80         for (int i = 0; i < N; ++i) {
     81             final InputMethodInfo imi = imis.get(i);
     82             final boolean isAllowedByOrganization = permittedList == null
     83                     || permittedList.contains(imi.getPackageName());
     84             Drawable icon;
     85             try {
     86                 // TODO: Consider other ways to retrieve an icon to show here.
     87                 icon = getActivity().getPackageManager().getApplicationIcon(imi.getPackageName());
     88             } catch (Exception e) {
     89                 // TODO: Consider handling the error differently perhaps by showing default icons.
     90                 icon = NO_ICON;
     91             }
     92             final InputMethodPreference pref = new InputMethodPreference(
     93                     context,
     94                     imi,
     95                     false,  /* isImeEnabler */
     96                     isAllowedByOrganization,
     97                     null  /* this can be null since isImeEnabler is false */);
     98             pref.setIcon(icon);
     99             mInputMethodPreferenceList.add(pref);
    100         }
    101         final Collator collator = Collator.getInstance();
    102         Collections.sort(mInputMethodPreferenceList, new Comparator<InputMethodPreference>() {
    103             @Override
    104             public int compare(InputMethodPreference lhs, InputMethodPreference rhs) {
    105                 return lhs.compareTo(rhs, collator);
    106             }
    107         });
    108         getPreferenceScreen().removeAll();
    109         for (int i = 0; i < N; ++i) {
    110             final InputMethodPreference pref = mInputMethodPreferenceList.get(i);
    111             pref.setOrder(i);
    112             getPreferenceScreen().addPreference(pref);
    113             InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref);
    114             pref.updatePreferenceViews();
    115         }
    116         mAddVirtualKeyboardScreen.setIcon(R.drawable.ic_add_24dp);
    117         mAddVirtualKeyboardScreen.setOrder(N);
    118         getPreferenceScreen().addPreference(mAddVirtualKeyboardScreen);
    119     }
    120 }
    121