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
This commit is contained in:
@@ -16,20 +16,11 @@
|
|||||||
|
|
||||||
package com.android.settings.inputmethod;
|
package com.android.settings.inputmethod;
|
||||||
|
|
||||||
import android.annotation.DrawableRes;
|
|
||||||
import android.annotation.NonNull;
|
|
||||||
import android.annotation.Nullable;
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.admin.DevicePolicyManager;
|
import android.app.admin.DevicePolicyManager;
|
||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ApplicationInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.content.pm.ServiceInfo;
|
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.graphics.drawable.ColorDrawable;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
import android.view.inputmethod.InputMethodInfo;
|
import android.view.inputmethod.InputMethodInfo;
|
||||||
@@ -93,59 +84,12 @@ public final class AvailableVirtualKeyboardFragment extends SettingsPreferenceFr
|
|||||||
return SettingsEnums.ENABLE_VIRTUAL_KEYBOARDS;
|
return SettingsEnums.ENABLE_VIRTUAL_KEYBOARDS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private static Drawable loadDrawable(@NonNull final PackageManager packageManager,
|
|
||||||
@NonNull final String packageName, @DrawableRes final int resId,
|
|
||||||
@NonNull final ApplicationInfo applicationInfo) {
|
|
||||||
if (resId == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return packageManager.getDrawable(packageName, resId, applicationInfo);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private static Drawable getInputMethodIcon(@NonNull final PackageManager packageManager,
|
|
||||||
@NonNull final InputMethodInfo imi) {
|
|
||||||
final ServiceInfo si = imi.getServiceInfo();
|
|
||||||
final ApplicationInfo ai = si != null ? si.applicationInfo : null;
|
|
||||||
final String packageName = imi.getPackageName();
|
|
||||||
if (si == null || ai == null || packageName == null) {
|
|
||||||
return new ColorDrawable(Color.TRANSPARENT);
|
|
||||||
}
|
|
||||||
// We do not use ServiceInfo#loadLogo() and ServiceInfo#loadIcon here since those methods
|
|
||||||
// internally have some fallback rules, which we want to do manually.
|
|
||||||
Drawable drawable = loadDrawable(packageManager, packageName, si.logo, ai);
|
|
||||||
if (drawable != null) {
|
|
||||||
return drawable;
|
|
||||||
}
|
|
||||||
drawable = loadDrawable(packageManager, packageName, si.icon, ai);
|
|
||||||
if (drawable != null) {
|
|
||||||
return drawable;
|
|
||||||
}
|
|
||||||
// We do not use ApplicationInfo#loadLogo() and ApplicationInfo#loadIcon here since those
|
|
||||||
// methods internally have some fallback rules, which we want to do manually.
|
|
||||||
drawable = loadDrawable(packageManager, packageName, ai.logo, ai);
|
|
||||||
if (drawable != null) {
|
|
||||||
return drawable;
|
|
||||||
}
|
|
||||||
drawable = loadDrawable(packageManager, packageName, ai.icon, ai);
|
|
||||||
if (drawable != null) {
|
|
||||||
return drawable;
|
|
||||||
}
|
|
||||||
return new ColorDrawable(Color.TRANSPARENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateInputMethodPreferenceViews() {
|
private void updateInputMethodPreferenceViews() {
|
||||||
mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
|
mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
|
||||||
// Clear existing "InputMethodPreference"s
|
// Clear existing "InputMethodPreference"s
|
||||||
mInputMethodPreferenceList.clear();
|
mInputMethodPreferenceList.clear();
|
||||||
List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
|
List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
|
||||||
final Context context = getPrefContext();
|
final Context context = getPrefContext();
|
||||||
final PackageManager packageManager = getActivity().getPackageManager();
|
|
||||||
final List<InputMethodInfo> imis = mInputMethodSettingValues.getInputMethodList();
|
final List<InputMethodInfo> imis = mInputMethodSettingValues.getInputMethodList();
|
||||||
final int numImis = (imis == null ? 0 : imis.size());
|
final int numImis = (imis == null ? 0 : imis.size());
|
||||||
for (int i = 0; i < numImis; ++i) {
|
for (int i = 0; i < numImis; ++i) {
|
||||||
@@ -154,7 +98,7 @@ public final class AvailableVirtualKeyboardFragment extends SettingsPreferenceFr
|
|||||||
|| permittedList.contains(imi.getPackageName());
|
|| permittedList.contains(imi.getPackageName());
|
||||||
final InputMethodPreference pref = new InputMethodPreference(
|
final InputMethodPreference pref = new InputMethodPreference(
|
||||||
context, imi, true, isAllowedByOrganization, this);
|
context, imi, true, isAllowedByOrganization, this);
|
||||||
pref.setIcon(getInputMethodIcon(packageManager, imi));
|
pref.setIcon(imi.loadIcon(context.getPackageManager()));
|
||||||
mInputMethodPreferenceList.add(pref);
|
mInputMethodPreferenceList.add(pref);
|
||||||
}
|
}
|
||||||
final Collator collator = Collator.getInstance();
|
final Collator collator = Collator.getInstance();
|
||||||
|
@@ -20,8 +20,6 @@ import android.app.Activity;
|
|||||||
import android.app.admin.DevicePolicyManager;
|
import android.app.admin.DevicePolicyManager;
|
||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.graphics.drawable.ColorDrawable;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
@@ -48,7 +46,6 @@ import java.util.List;
|
|||||||
public final class VirtualKeyboardFragment extends SettingsPreferenceFragment implements Indexable {
|
public final class VirtualKeyboardFragment extends SettingsPreferenceFragment implements Indexable {
|
||||||
|
|
||||||
private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen";
|
private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen";
|
||||||
private static final Drawable NO_ICON = new ColorDrawable(Color.TRANSPARENT);
|
|
||||||
|
|
||||||
private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>();
|
private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>();
|
||||||
private InputMethodManager mImm;
|
private InputMethodManager mImm;
|
||||||
@@ -89,14 +86,7 @@ public final class VirtualKeyboardFragment extends SettingsPreferenceFragment im
|
|||||||
final InputMethodInfo imi = imis.get(i);
|
final InputMethodInfo imi = imis.get(i);
|
||||||
final boolean isAllowedByOrganization = permittedList == null
|
final boolean isAllowedByOrganization = permittedList == null
|
||||||
|| permittedList.contains(imi.getPackageName());
|
|| permittedList.contains(imi.getPackageName());
|
||||||
Drawable icon;
|
final Drawable icon = imi.loadIcon(context.getPackageManager());
|
||||||
try {
|
|
||||||
// TODO: Consider other ways to retrieve an icon to show here.
|
|
||||||
icon = getActivity().getPackageManager().getApplicationIcon(imi.getPackageName());
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: Consider handling the error differently perhaps by showing default icons.
|
|
||||||
icon = NO_ICON;
|
|
||||||
}
|
|
||||||
final InputMethodPreference pref = new InputMethodPreference(
|
final InputMethodPreference pref = new InputMethodPreference(
|
||||||
context,
|
context,
|
||||||
imi,
|
imi,
|
||||||
|
Reference in New Issue
Block a user