Merge "Add icon in PowerUsageAnomalyDetails"
This commit is contained in:
@@ -17,11 +17,15 @@
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceGroup;
|
||||
import android.util.IconDrawableFactory;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
@@ -50,6 +54,9 @@ public class PowerUsageAnomalyDetails extends DashboardFragment implements
|
||||
List<Anomaly> mAnomalies;
|
||||
@VisibleForTesting
|
||||
PreferenceGroup mAbnormalListGroup;
|
||||
@VisibleForTesting
|
||||
PackageManager mPackageManager;
|
||||
IconDrawableFactory mIconDrawableFactory;
|
||||
|
||||
public static void startBatteryAbnormalPage(SettingsActivity caller,
|
||||
PreferenceFragment fragment, List<Anomaly> anomalies) {
|
||||
@@ -64,9 +71,12 @@ public class PowerUsageAnomalyDetails extends DashboardFragment implements
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
final Context context = getContext();
|
||||
|
||||
mAnomalies = getArguments().getParcelableArrayList(EXTRA_ANOMALY_LIST);
|
||||
mAbnormalListGroup = (PreferenceGroup) findPreference(KEY_PREF_ANOMALY_LIST);
|
||||
mPackageManager = context.getPackageManager();
|
||||
mIconDrawableFactory = IconDrawableFactory.newInstance(context, false /* EmbedShadow */);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -120,6 +130,10 @@ public class PowerUsageAnomalyDetails extends DashboardFragment implements
|
||||
final Anomaly anomaly = mAnomalies.get(i);
|
||||
Preference pref = new AnomalyPreference(getPrefContext(), anomaly);
|
||||
|
||||
Drawable icon = getIconFromPackageName(anomaly.packageName);
|
||||
if (icon != null) {
|
||||
pref.setIcon(icon);
|
||||
}
|
||||
mAbnormalListGroup.addPreference(pref);
|
||||
}
|
||||
}
|
||||
@@ -129,4 +143,14 @@ public class PowerUsageAnomalyDetails extends DashboardFragment implements
|
||||
mAnomalies.remove(anomaly);
|
||||
refreshUi();
|
||||
}
|
||||
|
||||
Drawable getIconFromPackageName(String packageName) {
|
||||
try {
|
||||
final ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName,
|
||||
PackageManager.GET_META_DATA);
|
||||
return mIconDrawableFactory.getBadgedIcon(appInfo);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return mPackageManager.getDefaultActivityIcon();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ package com.android.settings.fuelgauge.anomaly;
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
|
||||
/**
|
||||
* Preference that stores {@link Anomaly}
|
||||
@@ -14,6 +14,7 @@ public class AnomalyPreference extends Preference {
|
||||
public AnomalyPreference(Context context, Anomaly anomaly) {
|
||||
super(context);
|
||||
mAnomaly = anomaly;
|
||||
setLayoutResource(R.layout.preference_app);
|
||||
|
||||
if (anomaly != null) {
|
||||
setTitle(anomaly.displayName);
|
||||
|
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
@@ -59,11 +60,17 @@ import java.util.List;
|
||||
public class PowerUsageAnomalyDetailsTest {
|
||||
private static final String NAME_APP_1 = "app1";
|
||||
private static final String NAME_APP_2 = "app2";
|
||||
private static final String PACKAGE_NAME_1 = "com.android.app1";
|
||||
private static final String PACKAGE_NAME_2 = "com.android.app2";
|
||||
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
@Mock
|
||||
private PreferenceManager mPreferenceManager;
|
||||
@Mock
|
||||
private Drawable mDrawable1;
|
||||
@Mock
|
||||
private Drawable mDrawable2;
|
||||
private Context mContext;
|
||||
private PowerUsageAnomalyDetails mFragment;
|
||||
private PreferenceGroup mAbnormalListGroup;
|
||||
@@ -80,16 +87,19 @@ public class PowerUsageAnomalyDetailsTest {
|
||||
mAnomalyList = new ArrayList<>();
|
||||
Anomaly anomaly1 = new Anomaly.Builder()
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setPackageName(PACKAGE_NAME_1)
|
||||
.setDisplayName(NAME_APP_1)
|
||||
.build();
|
||||
mAnomalyList.add(anomaly1);
|
||||
Anomaly anomaly2 = new Anomaly.Builder()
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setPackageName(PACKAGE_NAME_2)
|
||||
.setDisplayName(NAME_APP_2)
|
||||
.build();
|
||||
mAnomalyList.add(anomaly2);
|
||||
|
||||
mFragment = spy(new PowerUsageAnomalyDetails());
|
||||
doReturn(null).when(mFragment).getIconFromPackageName(any());
|
||||
mFragment.mAbnormalListGroup = mAbnormalListGroup;
|
||||
mFragment.mAnomalies = mAnomalyList;
|
||||
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
|
||||
@@ -116,6 +126,28 @@ public class PowerUsageAnomalyDetailsTest {
|
||||
assertThat(testAnomalyList).containsExactlyElementsIn(mAnomalyList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshUi_iconCorrect() {
|
||||
doReturn(mDrawable1).when(mFragment).getIconFromPackageName(PACKAGE_NAME_1);
|
||||
doReturn(mDrawable2).when(mFragment).getIconFromPackageName(PACKAGE_NAME_2);
|
||||
|
||||
final List<Drawable> testIcons = new ArrayList<>();
|
||||
final ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(
|
||||
Preference.class);
|
||||
Answer<Void> prefCallable = new Answer<Void>() {
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
testIcons.add(preferenceCaptor.getValue().getIcon());
|
||||
return null;
|
||||
}
|
||||
};
|
||||
doAnswer(prefCallable).when(mAbnormalListGroup).addPreference(preferenceCaptor.capture());
|
||||
|
||||
mFragment.refreshUi();
|
||||
|
||||
assertThat(testIcons).containsExactly(mDrawable1, mDrawable2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartBatteryAbnormalPage_dataCorrect() {
|
||||
final ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
|
||||
|
Reference in New Issue
Block a user