Log battery saver schedule type and percentage when its value is changed
Bug: 15125481 Test: make SettingsGoogleRoboTests ROBOTEST_FILTER="com.android.settings.fuelgauge" Change-Id: Ia728a4ea2da3930201d5634abda7aa8a8dd72133
This commit is contained in:
@@ -16,17 +16,20 @@
|
||||
|
||||
package com.android.settings.fuelgauge.batterysaver;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -34,6 +37,7 @@ import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.RadioButtonPickerFragment;
|
||||
import com.android.settingslib.fuelgauge.BatterySaverUtils;
|
||||
import com.android.settingslib.widget.CandidateInfo;
|
||||
@@ -58,6 +62,8 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
|
||||
public BatterySaverScheduleRadioButtonsController mRadioButtonController;
|
||||
@VisibleForTesting
|
||||
Context mContext;
|
||||
private int mSaverPercentage;
|
||||
private String mSaverScheduleKey;
|
||||
private BatterySaverScheduleSeekBarController mSeekBarController;
|
||||
|
||||
@VisibleForTesting
|
||||
@@ -90,6 +96,8 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
|
||||
Settings.Secure.getUriFor(Settings.Secure.LOW_POWER_WARNING_ACKNOWLEDGED),
|
||||
false,
|
||||
mSettingsObserver);
|
||||
mSaverScheduleKey = mRadioButtonController.getDefaultKey();
|
||||
mSaverPercentage = getSaverPercentage();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,6 +115,7 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
|
||||
@Override
|
||||
public void onPause() {
|
||||
mContext.getContentResolver().unregisterContentObserver(mSettingsObserver);
|
||||
AsyncTask.execute(() -> logPowerSaver());
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@@ -174,6 +183,43 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void logPowerSaver() {
|
||||
int currentSaverPercentage = getSaverPercentage();
|
||||
String currentSaverScheduleKey = mRadioButtonController.getDefaultKey();
|
||||
if (mSaverScheduleKey.equals(currentSaverScheduleKey)
|
||||
&& mSaverPercentage == currentSaverPercentage) {
|
||||
return;
|
||||
}
|
||||
int scheduleType = -1;
|
||||
int schedulePercentage = -1;
|
||||
switch (currentSaverScheduleKey) {
|
||||
case BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE:
|
||||
scheduleType = SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_NO_SCHEDULE;
|
||||
break;
|
||||
case BatterySaverScheduleRadioButtonsController.KEY_ROUTINE:
|
||||
scheduleType = SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_BASED_ON_ROUTINE;
|
||||
break;
|
||||
case BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE:
|
||||
scheduleType = SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_BASED_ON_PERCENTAGE;
|
||||
schedulePercentage = currentSaverPercentage;
|
||||
break;
|
||||
// Unknown schedule type.
|
||||
default:
|
||||
return;
|
||||
}
|
||||
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider()
|
||||
.action(mContext, SettingsEnums.FUELGAUGE_BATTERY_SAVER,
|
||||
Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_SCHEDULE_TYPE,
|
||||
scheduleType),
|
||||
Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_PERCENTAGE_VALUE,
|
||||
schedulePercentage));
|
||||
}
|
||||
|
||||
private int getSaverPercentage() {
|
||||
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1);
|
||||
}
|
||||
|
||||
static class BatterySaverScheduleCandidateInfo extends CandidateInfo {
|
||||
|
||||
private final CharSequence mLabel;
|
||||
@@ -207,4 +253,4 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
|
||||
return mSummary;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,119 @@
|
||||
package com.android.settings.fuelgauge.batterysaver;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class BatterySaverScheduleSettingsTest {
|
||||
|
||||
private Context mContext;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
private BatterySaverScheduleSettings mBatterySaverScheduleSettings;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mBatterySaverScheduleSettings = new BatterySaverScheduleSettings();
|
||||
mBatterySaverScheduleSettings.onAttach(mContext);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
|
||||
|
||||
setSchedule(PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE, 1);
|
||||
mBatterySaverScheduleSettings.onResume();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_withNoScheduleType_logExpectedData() {
|
||||
setSchedule(PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE, 0);
|
||||
|
||||
mBatterySaverScheduleSettings.onPause();
|
||||
|
||||
verifySchedule(SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_NO_SCHEDULE,
|
||||
/* schedulePercentage= */ -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_withRoutineScheduleType_logExpectedData() {
|
||||
setSchedule(PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC, 0);
|
||||
|
||||
mBatterySaverScheduleSettings.onPause();
|
||||
|
||||
verifySchedule(SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_BASED_ON_ROUTINE,
|
||||
/* schedulePercentage= */ -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_withPercentageScheduleType_logExpectedData() {
|
||||
int expectedPercentage = 10;
|
||||
setSchedule(PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE, expectedPercentage);
|
||||
|
||||
mBatterySaverScheduleSettings.onPause();
|
||||
|
||||
verifySchedule(SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_BASED_ON_PERCENTAGE,
|
||||
expectedPercentage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_scheduleTypeAndPercentageAreNotChanged_notLogAnyData() {
|
||||
mBatterySaverScheduleSettings.onResume();
|
||||
mBatterySaverScheduleSettings.onPause();
|
||||
|
||||
waitAWhile();
|
||||
verifyNoMoreInteractions(mMetricsFeatureProvider);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_multipleScheduleTypeChanges_logLastChangedData() {
|
||||
int expectedPercentage = 10;
|
||||
setSchedule(PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE, 0);
|
||||
setSchedule(PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC, 0);
|
||||
setSchedule(PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE, expectedPercentage);
|
||||
|
||||
mBatterySaverScheduleSettings.onPause();
|
||||
|
||||
verifySchedule(SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_BASED_ON_PERCENTAGE,
|
||||
expectedPercentage);
|
||||
}
|
||||
|
||||
private void setSchedule(int scheduleType, int schedulePercentage) {
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.AUTOMATIC_POWER_SAVE_MODE, scheduleType);
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, schedulePercentage);
|
||||
}
|
||||
|
||||
private void verifySchedule(int scheduleType, int schedulePercentage) {
|
||||
waitAWhile();
|
||||
verify(mMetricsFeatureProvider).action(mContext, SettingsEnums.FUELGAUGE_BATTERY_SAVER,
|
||||
Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_SCHEDULE_TYPE,
|
||||
scheduleType),
|
||||
Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_PERCENTAGE_VALUE,
|
||||
schedulePercentage));
|
||||
}
|
||||
|
||||
private void waitAWhile() {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user