import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
import com.android.settings.enterprise.DevicePolicyManagerWrapperImpl;
-import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
import com.android.settings.fuelgauge.anomaly.AnomalyLoader;
@Override
public void onLoadFinished(Loader<List<Anomaly>> loader, List<Anomaly> data) {
- final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(getContext());
- anomalyUtils.logAnomalies(mMetricsFeatureProvider, data,
- MetricsEvent.FUELGAUGE_POWER_USAGE_DETAIL);
mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(data);
}
import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment.AnomalyDialogListener;
import com.android.settings.fuelgauge.anomaly.AnomalyLoader;
import com.android.settings.fuelgauge.anomaly.AnomalySummaryPreferenceController;
-import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
@Override
public void onLoadFinished(Loader<List<Anomaly>> loader, List<Anomaly> data) {
- final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(getContext());
- anomalyUtils.logAnomalies(mMetricsFeatureProvider, data,
- MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY);
-
// show high usage preference if possible
mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(data);
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle bundle = getArguments();
- final Context context = getContext();
- final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(context);
-
mAnomaly = bundle.getParcelable(ARG_ANOMALY);
- anomalyUtils.logAnomaly(mMetricsFeatureProvider, mAnomaly,
- MetricsProto.MetricsEvent.DIALOG_HANDLE_ANOMALY);
+ final Context context = getContext();
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
switch (anomalyAction.getActionType()) {
case Anomaly.AnomalyActionType.FORCE_STOP:
import android.content.Context;
import android.os.Build;
import android.support.annotation.VisibleForTesting;
-import android.util.Pair;
-import android.util.SparseIntArray;
-import com.android.internal.logging.nano.MetricsProto;
import com.android.internal.os.BatteryStatsHelper;
-import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
import com.android.settings.fuelgauge.anomaly.action.LocationCheckAction;
private Context mContext;
private static AnomalyUtils sInstance;
- private static final SparseIntArray mMetricArray;
- static {
- mMetricArray = new SparseIntArray();
- mMetricArray.append(Anomaly.AnomalyType.WAKE_LOCK,
- MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKELOCK);
- mMetricArray.append(Anomaly.AnomalyType.WAKEUP_ALARM,
- MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKEUP_ALARM);
- mMetricArray.append(Anomaly.AnomalyType.BLUETOOTH_SCAN,
- MetricsProto.MetricsEvent.ANOMALY_TYPE_UNOPTIMIZED_BT);
- }
-
@VisibleForTesting
AnomalyUtils(Context context) {
mContext = context.getApplicationContext();
return anomalies;
}
- /**
- * Log the list of {@link Anomaly} using {@link MetricsFeatureProvider}, which contains
- * anomaly type, package name, field_context, field_action_type
- *
- * @param provider provider to do the logging
- * @param anomalies contains the data to log
- * @param contextId which page invoke this logging
- * @see #logAnomaly(MetricsFeatureProvider, Anomaly, int)
- */
- public void logAnomalies(MetricsFeatureProvider provider, List<Anomaly> anomalies,
- int contextId) {
- for (int i = 0, size = anomalies.size(); i < size; i++) {
- logAnomaly(provider, anomalies.get(i), contextId);
- }
- }
-
- /**
- * Log the {@link Anomaly} using {@link MetricsFeatureProvider}, which contains
- * anomaly type, package name, field_context, field_action_type
- *
- * @param provider provider to do the logging
- * @param anomaly contains the data to log
- * @param contextId which page invoke this logging
- * @see #logAnomalies(MetricsFeatureProvider, List, int)
- */
- public void logAnomaly(MetricsFeatureProvider provider, Anomaly anomaly, int contextId) {
- provider.action(
- mContext,
- mMetricArray.get(anomaly.type, MetricsProto.MetricsEvent.VIEW_UNKNOWN),
- anomaly.packageName,
- Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, contextId),
- Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
- getAnomalyAction(anomaly).getActionType()));
- }
-
}
*/
public abstract boolean isActionActive(Anomaly anomaly);
- @Anomaly.AnomalyActionType
public abstract int getActionType();
}
import android.content.Context;
import android.support.annotation.VisibleForTesting;
-import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.fuelgauge.anomaly.Anomaly;
/**
public StopAndBackgroundCheckAction(Context context) {
this(context, new ForceStopAction(context), new BackgroundCheckAction(context));
- mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_STOP_AND_BACKGROUND_CHECK;
}
@VisibleForTesting
@Override
public void handlePositiveAction(Anomaly anomaly, int metricsKey) {
- super.handlePositiveAction(anomaly, metricsKey);
mForceStopAction.handlePositiveAction(anomaly, metricsKey);
mBackgroundCheckAction.handlePositiveAction(anomaly, metricsKey);
}
import com.android.settings.R;
import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
-import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
public void setUp() {
MockitoAnnotations.initMocks(this);
- mContext = spy(RuntimeEnvironment.application);
+ mContext = RuntimeEnvironment.application;
mWakeLockAnomaly = new Anomaly.Builder()
.setType(Anomaly.AnomalyType.WAKE_LOCK)
.setUid(UID)
.setPackageName(PACKAGE_NAME)
.setDisplayName(DISPLAY_NAME)
.build();
- FakeFeatureFactory.setupForTest(mContext);
}
@Test
assertThat(dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(
mContext.getString(R.string.dlg_cancel));
}
+
+ @Test
+ public void testOnCreateDialog_bluetoothAnomaly_fireLocationCheckDialog() {
+ mAnomalyDialogFragment = spy(AnomalyDialogFragment.newInstance(mBluetoothAnomaly,
+ 0 /* metricskey */));
+ mAnomalyDialogFragment.mAnomalyUtils = mAnomalyUtils;
+ doReturn(mAnomalyAction).when(mAnomalyUtils).getAnomalyAction(any());
+ doNothing().when(mAnomalyDialogFragment).initAnomalyUtils();
+ doReturn(Anomaly.AnomalyActionType.LOCATION_CHECK).when(mAnomalyAction).getActionType();
+
+ FragmentTestUtil.startFragment(mAnomalyDialogFragment);
+
+ final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
+ ShadowAlertDialog shadowDialog = shadowOf(dialog);
+
+ assertThat(shadowDialog.getMessage()).isEqualTo(
+ mContext.getString(R.string.dialog_location_message,
+ mWakeLockAnomaly.displayName));
+ assertThat(shadowDialog.getTitle()).isEqualTo(
+ mContext.getString(R.string.dialog_location_title));
+ assertThat(dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText()).isEqualTo(
+ mContext.getString(R.string.dialog_location_ok));
+ assertThat(dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(
+ mContext.getString(R.string.dlg_cancel));
+ }
}
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.verify;
-
import android.os.Build;
-import android.util.Pair;
-import com.android.internal.logging.nano.MetricsProto;
-import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.fuelgauge.anomaly.action.StopAndBackgroundCheckAction;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
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;
-import java.util.ArrayList;
-import java.util.List;
-
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows = {
ShadowKeyValueListParserWrapperImpl.class})
public class AnomalyUtilsTest {
- private static final String PACKAGE_NAME_WAKEUP = "com.android.app1";
- private static final String PACKAGE_NAME_WAKELOCK = "com.android.app2";
- private static final int CONTEXT_ID = 55;
-
- @Mock
- private MetricsFeatureProvider mMetricsFeatureProvider;
private AnomalyUtils mAnomalyUtils;
- private Anomaly mWakeupAnomaly;
- private Anomaly mWakeLockAnomaly;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
mAnomalyUtils = new AnomalyUtils(RuntimeEnvironment.application);
-
- mWakeLockAnomaly = new Anomaly.Builder()
- .setType(Anomaly.AnomalyType.WAKE_LOCK)
- .setPackageName(PACKAGE_NAME_WAKELOCK)
- .build();
- mWakeupAnomaly = new Anomaly.Builder()
- .setType(Anomaly.AnomalyType.WAKEUP_ALARM)
- .setPackageName(PACKAGE_NAME_WAKEUP)
- .build();
}
@Test
assertThat(mAnomalyUtils.getAnomalyDetector(Anomaly.AnomalyType.WAKEUP_ALARM)).isInstanceOf(
WakeupAlarmAnomalyDetector.class);
}
-
- @Test
- public void testLogAnomaly() {
- mAnomalyUtils.logAnomaly(mMetricsFeatureProvider, mWakeLockAnomaly, CONTEXT_ID);
-
- verify(mMetricsFeatureProvider).action(RuntimeEnvironment.application,
- MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKELOCK,
- PACKAGE_NAME_WAKELOCK,
- Pair.create(
- MetricsProto.MetricsEvent.FIELD_CONTEXT,
- CONTEXT_ID),
- Pair.create(
- MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
- Anomaly.AnomalyActionType.FORCE_STOP));
- }
-
- @Test
- public void testLogAnomalies() {
- final List<Anomaly> anomalies = new ArrayList<>();
- anomalies.add(mWakeLockAnomaly);
- anomalies.add(mWakeupAnomaly);
-
- mAnomalyUtils.logAnomalies(mMetricsFeatureProvider, anomalies, CONTEXT_ID);
-
- verify(mMetricsFeatureProvider).action(RuntimeEnvironment.application,
- MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKELOCK,
- PACKAGE_NAME_WAKELOCK,
- Pair.create(
- MetricsProto.MetricsEvent.FIELD_CONTEXT,
- CONTEXT_ID),
- Pair.create(
- MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
- Anomaly.AnomalyActionType.FORCE_STOP));
- verify(mMetricsFeatureProvider).action(RuntimeEnvironment.application,
- MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKEUP_ALARM,
- PACKAGE_NAME_WAKEUP,
- Pair.create(
- MetricsProto.MetricsEvent.FIELD_CONTEXT,
- CONTEXT_ID),
- Pair.create(
- MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
- Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK));
- }
}