Add battery usage metric
Bug: 188046672 Test: make SettingsRoboTests Change-Id: I1716913eee7c93cafd622052dcbca940c9726a17
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.settings.fuelgauge;
|
package com.android.settings.fuelgauge;
|
||||||
|
|
||||||
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
@@ -37,6 +38,7 @@ import com.android.settings.core.InstrumentedPreferenceFragment;
|
|||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnCreate;
|
import com.android.settingslib.core.lifecycle.events.OnCreate;
|
||||||
@@ -89,9 +91,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
private final String mPreferenceKey;
|
private final String mPreferenceKey;
|
||||||
private final SettingsActivity mActivity;
|
private final SettingsActivity mActivity;
|
||||||
private final InstrumentedPreferenceFragment mFragment;
|
private final InstrumentedPreferenceFragment mFragment;
|
||||||
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
|
||||||
private final CharSequence[] mNotAllowShowSummaryPackages;
|
|
||||||
private final CharSequence[] mNotAllowShowEntryPackages;
|
private final CharSequence[] mNotAllowShowEntryPackages;
|
||||||
|
private final CharSequence[] mNotAllowShowSummaryPackages;
|
||||||
|
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||||
|
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
||||||
|
|
||||||
// Preference cache to avoid create new instance each time.
|
// Preference cache to avoid create new instance each time.
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -111,6 +114,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
.getTextArray(R.array.allowlist_hide_summary_in_battery_usage);
|
.getTextArray(R.array.allowlist_hide_summary_in_battery_usage);
|
||||||
mNotAllowShowEntryPackages = context.getResources()
|
mNotAllowShowEntryPackages = context.getResources()
|
||||||
.getTextArray(R.array.allowlist_hide_entry_in_battery_usage);
|
.getTextArray(R.array.allowlist_hide_entry_in_battery_usage);
|
||||||
|
mMetricsFeatureProvider =
|
||||||
|
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider();
|
||||||
if (lifecycle != null) {
|
if (lifecycle != null) {
|
||||||
lifecycle.addObserver(this);
|
lifecycle.addObserver(this);
|
||||||
}
|
}
|
||||||
@@ -127,6 +132,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
savedInstanceState.getBoolean(KEY_EXPAND_SYSTEM_INFO, mIsExpanded);
|
savedInstanceState.getBoolean(KEY_EXPAND_SYSTEM_INFO, mIsExpanded);
|
||||||
Log.d(TAG, String.format("onCreate() slotIndex=%d isExpanded=%b",
|
Log.d(TAG, String.format("onCreate() slotIndex=%d isExpanded=%b",
|
||||||
mTrapezoidIndex, mIsExpanded));
|
mTrapezoidIndex, mIsExpanded));
|
||||||
|
mMetricsFeatureProvider.action(mPrefContext, SettingsEnums.OPEN_BATTERY_USAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -191,15 +197,22 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
final BatteryDiffEntry diffEntry = powerPref.getBatteryDiffEntry();
|
final BatteryDiffEntry diffEntry = powerPref.getBatteryDiffEntry();
|
||||||
final BatteryHistEntry histEntry = diffEntry.mBatteryHistEntry;
|
final BatteryHistEntry histEntry = diffEntry.mBatteryHistEntry;
|
||||||
final String packageName = histEntry.mPackageName;
|
final String packageName = histEntry.mPackageName;
|
||||||
|
final boolean isAppEntry = histEntry.isAppEntry();
|
||||||
// Checks whether the package is installed or not.
|
// Checks whether the package is installed or not.
|
||||||
boolean isValidPackage = true;
|
boolean isValidPackage = true;
|
||||||
if (histEntry.isAppEntry()) {
|
if (isAppEntry) {
|
||||||
if (mBatteryUtils == null) {
|
if (mBatteryUtils == null) {
|
||||||
mBatteryUtils = BatteryUtils.getInstance(mPrefContext);
|
mBatteryUtils = BatteryUtils.getInstance(mPrefContext);
|
||||||
}
|
}
|
||||||
isValidPackage = mBatteryUtils.getPackageUid(packageName)
|
isValidPackage = mBatteryUtils.getPackageUid(packageName)
|
||||||
!= BatteryUtils.UID_NULL;
|
!= BatteryUtils.UID_NULL;
|
||||||
}
|
}
|
||||||
|
mMetricsFeatureProvider.action(
|
||||||
|
mPrefContext,
|
||||||
|
isAppEntry
|
||||||
|
? SettingsEnums.ACTION_BATTERY_USAGE_APP_ITEM
|
||||||
|
: SettingsEnums.ACTION_BATTERY_USAGE_SYSTEM_ITEM,
|
||||||
|
packageName);
|
||||||
Log.d(TAG, String.format("handleClick() label=%s key=%s isValid:%b\n%s",
|
Log.d(TAG, String.format("handleClick() label=%s key=%s isValid:%b\n%s",
|
||||||
diffEntry.getAppLabel(), histEntry.getKey(), isValidPackage, histEntry));
|
diffEntry.getAppLabel(), histEntry.getKey(), isValidPackage, histEntry));
|
||||||
if (isValidPackage) {
|
if (isValidPackage) {
|
||||||
@@ -215,11 +228,20 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
public void onSelect(int trapezoidIndex) {
|
public void onSelect(int trapezoidIndex) {
|
||||||
Log.d(TAG, "onChartSelect:" + trapezoidIndex);
|
Log.d(TAG, "onChartSelect:" + trapezoidIndex);
|
||||||
refreshUi(trapezoidIndex, /*isForce=*/ false);
|
refreshUi(trapezoidIndex, /*isForce=*/ false);
|
||||||
|
mMetricsFeatureProvider.action(
|
||||||
|
mPrefContext,
|
||||||
|
trapezoidIndex == BatteryChartView.SELECTED_INDEX_ALL
|
||||||
|
? SettingsEnums.ACTION_BATTERY_USAGE_SHOW_ALL
|
||||||
|
: SettingsEnums.ACTION_BATTERY_USAGE_TIME_SLOT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExpand(boolean isExpanded) {
|
public void onExpand(boolean isExpanded) {
|
||||||
mIsExpanded = isExpanded;
|
mIsExpanded = isExpanded;
|
||||||
|
mMetricsFeatureProvider.action(
|
||||||
|
mPrefContext,
|
||||||
|
SettingsEnums.ACTION_BATTERY_USAGE_EXPAND_ITEM,
|
||||||
|
isExpanded);
|
||||||
refreshExpandUi();
|
refreshExpandUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -26,6 +26,7 @@ import static org.mockito.Mockito.spy;
|
|||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
@@ -43,6 +44,7 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -82,12 +84,16 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
@Mock private Resources mResources;
|
@Mock private Resources mResources;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private FakeFeatureFactory mFeatureFactory;
|
||||||
private BatteryDiffEntry mBatteryDiffEntry;
|
private BatteryDiffEntry mBatteryDiffEntry;
|
||||||
|
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||||
private BatteryChartPreferenceController mBatteryChartPreferenceController;
|
private BatteryChartPreferenceController mBatteryChartPreferenceController;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
|
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mBatteryChartPreferenceController = createController();
|
mBatteryChartPreferenceController = createController();
|
||||||
mBatteryChartPreferenceController.mPrefContext = mContext;
|
mBatteryChartPreferenceController.mPrefContext = mContext;
|
||||||
@@ -331,9 +337,14 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHandlePreferenceTreeClick_notPowerGaugePreference_returnFalse() {
|
public void testHandlePreferenceTreeiClick_notPowerGaugePreference_returnFalse() {
|
||||||
assertThat(mBatteryChartPreferenceController.handlePreferenceTreeClick(mAppListGroup))
|
assertThat(mBatteryChartPreferenceController.handlePreferenceTreeClick(mAppListGroup))
|
||||||
.isFalse();
|
.isFalse();
|
||||||
|
|
||||||
|
verify(mMetricsFeatureProvider, never())
|
||||||
|
.action(mContext, SettingsEnums.ACTION_BATTERY_USAGE_APP_ITEM);
|
||||||
|
verify(mMetricsFeatureProvider, never())
|
||||||
|
.action(mContext, SettingsEnums.ACTION_BATTERY_USAGE_SYSTEM_ITEM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -343,6 +354,11 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
|
|
||||||
assertThat(mBatteryChartPreferenceController.handlePreferenceTreeClick(
|
assertThat(mBatteryChartPreferenceController.handlePreferenceTreeClick(
|
||||||
mPowerGaugePreference)).isTrue();
|
mPowerGaugePreference)).isTrue();
|
||||||
|
verify(mMetricsFeatureProvider)
|
||||||
|
.action(
|
||||||
|
mContext,
|
||||||
|
SettingsEnums.ACTION_BATTERY_USAGE_SYSTEM_ITEM,
|
||||||
|
mBatteryHistEntry.mPackageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -355,6 +371,11 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
|
|
||||||
assertThat(mBatteryChartPreferenceController.handlePreferenceTreeClick(
|
assertThat(mBatteryChartPreferenceController.handlePreferenceTreeClick(
|
||||||
mPowerGaugePreference)).isFalse();
|
mPowerGaugePreference)).isFalse();
|
||||||
|
verify(mMetricsFeatureProvider)
|
||||||
|
.action(
|
||||||
|
mContext,
|
||||||
|
SettingsEnums.ACTION_BATTERY_USAGE_APP_ITEM,
|
||||||
|
mBatteryHistEntry.mPackageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -474,6 +495,11 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
verify(mAppListGroup).addPreference(captor.capture());
|
verify(mAppListGroup).addPreference(captor.capture());
|
||||||
// Verifies the added preference.
|
// Verifies the added preference.
|
||||||
assertThat(captor.getValue().getKey()).isEqualTo(PREF_KEY);
|
assertThat(captor.getValue().getKey()).isEqualTo(PREF_KEY);
|
||||||
|
verify(mMetricsFeatureProvider)
|
||||||
|
.action(
|
||||||
|
mContext,
|
||||||
|
SettingsEnums.ACTION_BATTERY_USAGE_EXPAND_ITEM,
|
||||||
|
true /*isExpanded*/);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -489,6 +515,28 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
verify(mAppListGroup).findPreference(PREF_KEY);
|
verify(mAppListGroup).findPreference(PREF_KEY);
|
||||||
verify(mAppListGroup).removePreference(mPowerGaugePreference);
|
verify(mAppListGroup).removePreference(mPowerGaugePreference);
|
||||||
assertThat(mBatteryChartPreferenceController.mPreferenceCache).hasSize(1);
|
assertThat(mBatteryChartPreferenceController.mPreferenceCache).hasSize(1);
|
||||||
|
verify(mMetricsFeatureProvider)
|
||||||
|
.action(
|
||||||
|
mContext,
|
||||||
|
SettingsEnums.ACTION_BATTERY_USAGE_EXPAND_ITEM,
|
||||||
|
false /*isExpanded*/);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnSelect_selectSpecificTimeSlot_logMetric() {
|
||||||
|
mBatteryChartPreferenceController.onSelect(1 /*slot index*/);
|
||||||
|
|
||||||
|
verify(mMetricsFeatureProvider)
|
||||||
|
.action(mContext, SettingsEnums.ACTION_BATTERY_USAGE_TIME_SLOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnSelect_selectAll_logMetric() {
|
||||||
|
mBatteryChartPreferenceController.onSelect(
|
||||||
|
BatteryChartView.SELECTED_INDEX_ALL /*slot index*/);
|
||||||
|
|
||||||
|
verify(mMetricsFeatureProvider)
|
||||||
|
.action(mContext, SettingsEnums.ACTION_BATTERY_USAGE_SHOW_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -610,6 +658,8 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
assertThat(mBatteryChartPreferenceController.mTrapezoidIndex)
|
assertThat(mBatteryChartPreferenceController.mTrapezoidIndex)
|
||||||
.isEqualTo(expectedIndex);
|
.isEqualTo(expectedIndex);
|
||||||
assertThat(mBatteryChartPreferenceController.mIsExpanded).isTrue();
|
assertThat(mBatteryChartPreferenceController.mIsExpanded).isTrue();
|
||||||
|
verify(mMetricsFeatureProvider)
|
||||||
|
.action(mContext, SettingsEnums.OPEN_BATTERY_USAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Reference in New Issue
Block a user