Files
app_Settings/src/com/android/settings/inputmethod/VirtualKeyboardFragment.java
Yohei Yukawa 8cb387adae Let Settings app use InputMethodInfo#loadIcon()
This is a follow up CL to my previous CL [1], which attempted to show
the most appropriate IME icon even when multiple IMEs are implemented
in a single APK.

This CL simplifies the way how IME icon is obtained in the Settings
app, by simply using InputMethodInfo#loadIcon(), which already does
what we want.

Notable user-visible behavior changes are:
 - Like before my previous CL was submitted, android:logo will not be
   used in the IME settings (again).
 - Icons shown in IME settings start having (user) badges like other
   settings pages.
 - If no icon is available in IME,
   PackageManager#getDefaultActivityIcon() will be used. This is
   actually consistent with other settings pages.

 [1]: I406ccc0d53e6ec69793c2fc8be8c6c1c90b34811
      2cae5b8952

Bug: 28204635
Fix: 131432102
Test: Manually verified as follows.
  1. Build aosp_taimen-userdebug and flash it
  2. Install TestDPC.
  3. Set up a work profile
  4. Open Settings App -> System -> Languages & input
     -> Advanced -> Virtual keyboard for work
  5. Make sure the icon of "Android Keyboard (AOSP)" has a small badge.
  6. Tap "Manage keyboards"
  7. Make sure the icon of "Android Keyboard (AOSP)" has the same icon
     as the step 5.
  8. Open Settings App -> System -> Languages & input
     -> Virtual keyboard
  9. Make sure the icon of "Android Keyboard (AOSP)" does not have a
     small badge on it.
 10. Tap "Manage keyboards"
 11. Make sure the icon of "Android Keyboard (AOSP)" has the same icon
     as the step 9.
Change-Id: Ie9be1eb3071abcc2df7723ceb10d76fe458c4808
2019-04-29 18:11:33 -07:00

125 lines
5.2 KiB
Java

/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.inputmethod;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import androidx.preference.Preference;
import com.android.internal.util.Preconditions;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.inputmethod.InputMethodAndSubtypeUtilCompat;
import com.android.settingslib.inputmethod.InputMethodPreference;
import com.android.settingslib.search.SearchIndexable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SearchIndexable
public final class VirtualKeyboardFragment extends SettingsPreferenceFragment implements Indexable {
private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen";
private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>();
private InputMethodManager mImm;
private DevicePolicyManager mDpm;
private Preference mAddVirtualKeyboardScreen;
@Override
public void onCreatePreferences(Bundle bundle, String s) {
Activity activity = Preconditions.checkNotNull(getActivity());
addPreferencesFromResource(R.xml.virtual_keyboard_settings);
mImm = Preconditions.checkNotNull(activity.getSystemService(InputMethodManager.class));
mDpm = Preconditions.checkNotNull(activity.getSystemService(DevicePolicyManager.class));
mAddVirtualKeyboardScreen = Preconditions.checkNotNull(
findPreference(ADD_VIRTUAL_KEYBOARD_SCREEN));
}
@Override
public void onResume() {
super.onResume();
// Refresh internal states in mInputMethodSettingValues to keep the latest
// "InputMethodInfo"s and "InputMethodSubtype"s
updateInputMethodPreferenceViews();
}
@Override
public int getMetricsCategory() {
return SettingsEnums.VIRTUAL_KEYBOARDS;
}
private void updateInputMethodPreferenceViews() {
// Clear existing "InputMethodPreference"s
mInputMethodPreferenceList.clear();
List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
final Context context = getPrefContext();
final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList();
final int N = (imis == null ? 0 : imis.size());
for (int i = 0; i < N; ++i) {
final InputMethodInfo imi = imis.get(i);
final boolean isAllowedByOrganization = permittedList == null
|| permittedList.contains(imi.getPackageName());
final Drawable icon = imi.loadIcon(context.getPackageManager());
final InputMethodPreference pref = new InputMethodPreference(
context,
imi,
false, /* isImeEnabler */
isAllowedByOrganization,
null /* this can be null since isImeEnabler is false */);
pref.setIcon(icon);
mInputMethodPreferenceList.add(pref);
}
final Collator collator = Collator.getInstance();
mInputMethodPreferenceList.sort((lhs, rhs) -> lhs.compareTo(rhs, collator));
getPreferenceScreen().removeAll();
for (int i = 0; i < N; ++i) {
final InputMethodPreference pref = mInputMethodPreferenceList.get(i);
pref.setOrder(i);
getPreferenceScreen().addPreference(pref);
InputMethodAndSubtypeUtilCompat.removeUnnecessaryNonPersistentPreference(pref);
pref.updatePreferenceViews();
}
mAddVirtualKeyboardScreen.setIcon(R.drawable.ic_add_24dp);
mAddVirtualKeyboardScreen.setOrder(N);
getPreferenceScreen().addPreference(mAddVirtualKeyboardScreen);
}
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(
Context context, boolean enabled) {
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.virtual_keyboard_settings;
return Arrays.asList(sir);
}
};
}