Merge "Convert more loadIcon() to IconDrawableFactory"
This commit is contained in:
committed by
Android (Google) Code Review
commit
2dda5a567d
@@ -18,6 +18,7 @@ package com.android.settings.applications.defaultapps;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.ComponentInfo;
|
import android.content.pm.ComponentInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
@@ -25,6 +26,7 @@ import android.graphics.drawable.Drawable;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.IconDrawableFactory;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -124,8 +126,19 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont
|
|||||||
final ResolveInfo info = list.get(0);
|
final ResolveInfo info = list.get(0);
|
||||||
final ComponentInfo cn = info.getComponentInfo();
|
final ComponentInfo cn = info.getComponentInfo();
|
||||||
final String packageName = cn == null ? null : cn.packageName;
|
final String packageName = cn == null ? null : cn.packageName;
|
||||||
|
if (TextUtils.isEmpty(packageName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final ApplicationInfo appInfo;
|
||||||
|
try {
|
||||||
|
appInfo = mPackageManager.getPackageManager().getApplicationInfo(packageName, 0);
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
Log.w(TAG, "Error getting app info for " + packageName);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Log.d(TAG, "Getting icon for the only browser app: " + packageName);
|
Log.d(TAG, "Getting icon for the only browser app: " + packageName);
|
||||||
return info.loadIcon(mPackageManager.getPackageManager());
|
final IconDrawableFactory iconFactory = IconDrawableFactory.newInstance(mContext);
|
||||||
|
return iconFactory.getBadgedIcon(cn, appInfo, mUserId);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -21,7 +21,10 @@ import android.content.Intent;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.IconDrawableFactory;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
@@ -63,7 +66,8 @@ public class AppGridView extends GridView {
|
|||||||
/**
|
/**
|
||||||
* Loads application labels and icons.
|
* Loads application labels and icons.
|
||||||
*/
|
*/
|
||||||
private static class AppsAdapter extends ArrayAdapter<ActivityEntry> {
|
@VisibleForTesting
|
||||||
|
public static class AppsAdapter extends ArrayAdapter<ActivityEntry> {
|
||||||
private final PackageManager mPackageManager;
|
private final PackageManager mPackageManager;
|
||||||
private final int mIconResId;
|
private final int mIconResId;
|
||||||
|
|
||||||
@@ -80,9 +84,8 @@ public class AppGridView extends GridView {
|
|||||||
public View getView(int position, View convertView, ViewGroup parent) {
|
public View getView(int position, View convertView, ViewGroup parent) {
|
||||||
final View view = super.getView(position, convertView, parent);
|
final View view = super.getView(position, convertView, parent);
|
||||||
final ActivityEntry entry = getItem(position);
|
final ActivityEntry entry = getItem(position);
|
||||||
final ImageView iconView = (ImageView) view.findViewById(mIconResId);
|
final ImageView iconView = view.findViewById(mIconResId);
|
||||||
final Drawable icon = entry.info.loadIcon(mPackageManager);
|
iconView.setImageDrawable(entry.getIcon());
|
||||||
iconView.setImageDrawable(icon);
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,10 +111,11 @@ public class AppGridView extends GridView {
|
|||||||
final PackageManager pm = mPackageManager;
|
final PackageManager pm = mPackageManager;
|
||||||
final ArrayList<ActivityEntry> results = new ArrayList<>();
|
final ArrayList<ActivityEntry> results = new ArrayList<>();
|
||||||
final List<ResolveInfo> infos = pm.queryIntentActivities(mainIntent, 0);
|
final List<ResolveInfo> infos = pm.queryIntentActivities(mainIntent, 0);
|
||||||
|
final IconDrawableFactory iconFactory = IconDrawableFactory.newInstance(getContext());
|
||||||
for (ResolveInfo info : infos) {
|
for (ResolveInfo info : infos) {
|
||||||
final CharSequence label = info.loadLabel(pm);
|
final CharSequence label = info.loadLabel(pm);
|
||||||
if (label != null) {
|
if (label != null) {
|
||||||
results.add(new ActivityEntry(info, label.toString()));
|
results.add(new ActivityEntry(info, label.toString(), iconFactory));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,13 +128,19 @@ public class AppGridView extends GridView {
|
|||||||
/**
|
/**
|
||||||
* Class used for caching the activity label and icon.
|
* Class used for caching the activity label and icon.
|
||||||
*/
|
*/
|
||||||
private static class ActivityEntry implements Comparable<ActivityEntry> {
|
@VisibleForTesting
|
||||||
|
public static class ActivityEntry implements Comparable<ActivityEntry> {
|
||||||
|
|
||||||
public final ResolveInfo info;
|
public final ResolveInfo info;
|
||||||
public final String label;
|
public final String label;
|
||||||
|
private final IconDrawableFactory mIconFactory;
|
||||||
|
private final int mUserId;
|
||||||
|
|
||||||
public ActivityEntry(ResolveInfo info, String label) {
|
public ActivityEntry(ResolveInfo info, String label, IconDrawableFactory iconFactory) {
|
||||||
this.info = info;
|
this.info = info;
|
||||||
this.label = label;
|
this.label = label;
|
||||||
|
mIconFactory = iconFactory;
|
||||||
|
mUserId = UserHandle.myUserId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -142,5 +152,10 @@ public class AppGridView extends GridView {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Drawable getIcon() {
|
||||||
|
return mIconFactory.getBadgedIcon(
|
||||||
|
info.activityInfo, info.activityInfo.applicationInfo, mUserId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,7 @@ import android.content.Context;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
import android.util.IconDrawableFactory;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
@@ -63,13 +64,14 @@ public class ApplicationListPreferenceController extends AbstractPreferenceContr
|
|||||||
if (screen == null) {
|
if (screen == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
final IconDrawableFactory iconDrawableFactory = IconDrawableFactory.newInstance(mContext);
|
||||||
final Context prefContext = mParent.getPreferenceManager().getContext();
|
final Context prefContext = mParent.getPreferenceManager().getContext();
|
||||||
for (int position = 0; position < result.size(); position++) {
|
for (int position = 0; position < result.size(); position++) {
|
||||||
final UserAppInfo item = result.get(position);
|
final UserAppInfo item = result.get(position);
|
||||||
final Preference preference = new Preference(prefContext);
|
final Preference preference = new Preference(prefContext);
|
||||||
preference.setLayoutResource(R.layout.preference_app);
|
preference.setLayoutResource(R.layout.preference_app);
|
||||||
preference.setTitle(item.appInfo.loadLabel(mPm));
|
preference.setTitle(item.appInfo.loadLabel(mPm));
|
||||||
preference.setIcon(item.appInfo.loadIcon(mPm));
|
preference.setIcon(iconDrawableFactory.getBadgedIcon(item.appInfo));
|
||||||
preference.setOrder(position);
|
preference.setOrder(position);
|
||||||
preference.setSelectable(false);
|
preference.setSelectable(false);
|
||||||
screen.addPreference(preference);
|
screen.addPreference(preference);
|
||||||
|
@@ -26,8 +26,10 @@ import android.content.pm.PackageManager;
|
|||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.content.pm.ServiceInfo;
|
import android.content.pm.ServiceInfo;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.util.IconDrawableFactory;
|
||||||
import android.view.accessibility.AccessibilityManager;
|
import android.view.accessibility.AccessibilityManager;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -44,17 +46,22 @@ public class AccessibilityServiceResultLoader extends AsyncLoader<Set<? extends
|
|||||||
|
|
||||||
private static final int NAME_NO_MATCH = -1;
|
private static final int NAME_NO_MATCH = -1;
|
||||||
|
|
||||||
|
private final Context mContext;
|
||||||
|
|
||||||
private List<String> mBreadcrumb;
|
private List<String> mBreadcrumb;
|
||||||
private SiteMapManager mSiteMapManager;
|
private SiteMapManager mSiteMapManager;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
final String mQuery;
|
final String mQuery;
|
||||||
private final AccessibilityManager mAccessibilityManager;
|
private final AccessibilityManager mAccessibilityManager;
|
||||||
private final PackageManager mPackageManager;
|
private final PackageManager mPackageManager;
|
||||||
|
private final int mUserId;
|
||||||
|
|
||||||
|
|
||||||
public AccessibilityServiceResultLoader(Context context, String query,
|
public AccessibilityServiceResultLoader(Context context, String query,
|
||||||
SiteMapManager mapManager) {
|
SiteMapManager mapManager) {
|
||||||
super(context);
|
super(context);
|
||||||
|
mContext = context;
|
||||||
|
mUserId = UserHandle.myUserId();
|
||||||
mSiteMapManager = mapManager;
|
mSiteMapManager = mapManager;
|
||||||
mPackageManager = context.getPackageManager();
|
mPackageManager = context.getPackageManager();
|
||||||
mAccessibilityManager =
|
mAccessibilityManager =
|
||||||
@@ -68,6 +75,7 @@ public class AccessibilityServiceResultLoader extends AsyncLoader<Set<? extends
|
|||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
final List<AccessibilityServiceInfo> services = mAccessibilityManager
|
final List<AccessibilityServiceInfo> services = mAccessibilityManager
|
||||||
.getInstalledAccessibilityServiceList();
|
.getInstalledAccessibilityServiceList();
|
||||||
|
final IconDrawableFactory iconFactory = IconDrawableFactory.newInstance(mContext);
|
||||||
final String screenTitle = context.getString(R.string.accessibility_settings);
|
final String screenTitle = context.getString(R.string.accessibility_settings);
|
||||||
for (AccessibilityServiceInfo service : services) {
|
for (AccessibilityServiceInfo service : services) {
|
||||||
if (service == null) {
|
if (service == null) {
|
||||||
@@ -87,7 +95,10 @@ public class AccessibilityServiceResultLoader extends AsyncLoader<Set<? extends
|
|||||||
if (resolveInfo.getIconResource() == 0) {
|
if (resolveInfo.getIconResource() == 0) {
|
||||||
icon = ContextCompat.getDrawable(context, R.mipmap.ic_accessibility_generic);
|
icon = ContextCompat.getDrawable(context, R.mipmap.ic_accessibility_generic);
|
||||||
} else {
|
} else {
|
||||||
icon = resolveInfo.loadIcon(mPackageManager);
|
icon = iconFactory.getBadgedIcon(
|
||||||
|
resolveInfo.serviceInfo,
|
||||||
|
resolveInfo.serviceInfo.applicationInfo,
|
||||||
|
mUserId);
|
||||||
}
|
}
|
||||||
final String componentName = new ComponentName(serviceInfo.packageName,
|
final String componentName = new ComponentName(serviceInfo.packageName,
|
||||||
serviceInfo.name).flattenToString();
|
serviceInfo.name).flattenToString();
|
||||||
|
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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.display;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.ActivityInfo;
|
||||||
|
import android.content.pm.ResolveInfo;
|
||||||
|
import android.util.IconDrawableFactory;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class AppGridViewTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ResolveInfo mInfo;
|
||||||
|
@Mock
|
||||||
|
private ActivityInfo mActivityInfo;
|
||||||
|
private Context mContext;
|
||||||
|
private IconDrawableFactory mIconFactory;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mInfo.activityInfo = mActivityInfo;
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mIconFactory = IconDrawableFactory.newInstance(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void appEntry_shouldLoadIcon() {
|
||||||
|
|
||||||
|
final AppGridView.ActivityEntry activityEntry = new AppGridView.ActivityEntry(
|
||||||
|
mInfo, "label", mIconFactory);
|
||||||
|
|
||||||
|
assertThat(activityEntry.label).isEqualTo("label");
|
||||||
|
assertThat(activityEntry.getIcon()).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void appEntry_compare_shouldCompareIgnoreCase() {
|
||||||
|
final AppGridView.ActivityEntry entry1 = new AppGridView.ActivityEntry(
|
||||||
|
mInfo, "label", mIconFactory);
|
||||||
|
final AppGridView.ActivityEntry entry2 = new AppGridView.ActivityEntry(
|
||||||
|
mInfo, "LABEL", mIconFactory);
|
||||||
|
final AppGridView.ActivityEntry entry3 = new AppGridView.ActivityEntry(
|
||||||
|
mInfo, "label2", mIconFactory);
|
||||||
|
|
||||||
|
assertThat(entry1.compareTo(entry2)).isEqualTo(0);
|
||||||
|
assertThat(entry1.compareTo(entry3)).isNotEqualTo(0);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user