Merge changes from topic "battery_saver_step_1"
* changes: Add preference controller for battery seekbar Revamp the battery saver page
This commit is contained in:
47
res/layout/battery_saver_settings_button.xml
Normal file
47
res/layout/battery_saver_settings_button.xml
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright (C) 2018 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:gravity="bottom"
|
||||||
|
android:paddingStart="72dp"
|
||||||
|
android:paddingEnd="72dp"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/battery_saver_on_button"
|
||||||
|
style="@style/ActionPrimaryButton"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:text="@string/battery_saver_button_turn_on"
|
||||||
|
android:paddingEnd="8dp" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/battery_saver_off_button"
|
||||||
|
style="@style/ActionSecondaryButton"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:text="@string/battery_saver_button_turn_off"
|
||||||
|
android:paddingEnd="8dp" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@@ -5159,8 +5159,17 @@
|
|||||||
<!-- Label for dex2oat process in battery usage used for the optimization of one or more apps -->
|
<!-- Label for dex2oat process in battery usage used for the optimization of one or more apps -->
|
||||||
<string name="process_dex2oat_label">App optimization</string>
|
<string name="process_dex2oat_label">App optimization</string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=40] Battery saver: Label for feature, title + menu item -->
|
<!-- Battery saver: Label for feature, title + menu item [CHAR_LIMIT=40] -->
|
||||||
<string name="battery_saver">Battery Saver</string>
|
<string name="battery_saver">Reduced power mode</string>
|
||||||
|
|
||||||
|
<!-- Battery saver: Label for preference to turn on battery saver automatically when battery is low [CHAR_LIMIT=40] -->
|
||||||
|
<string name="battery_saver_auto_title">Schedule</string>
|
||||||
|
|
||||||
|
<!-- Battery saver: Summary for preference to turn on battery saver automatically when battery is low [CHAR_LIMIT=40] -->
|
||||||
|
<string name="battery_saver_auto_summary">Turn on Reduced power mode automatically when battery is low</string>
|
||||||
|
|
||||||
|
<!-- Battery saver: Label for seekbar to change battery saver threshold [CHAR_LIMIT=40] -->
|
||||||
|
<string name="battery_saver_seekbar_title">Turn on automatically at <xliff:g id="percent">%1$s</xliff:g></string>
|
||||||
|
|
||||||
<!-- Used in the Battery Saver settings screen to control turning on/off the feature entirely -->
|
<!-- Used in the Battery Saver settings screen to control turning on/off the feature entirely -->
|
||||||
<string name="battery_saver_master_switch_title">Use Battery Saver</string>
|
<string name="battery_saver_master_switch_title">Use Battery Saver</string>
|
||||||
@@ -8733,6 +8742,12 @@
|
|||||||
<!-- [CHAR_LIMIT=NONE] Battery saver: Description for automatic entry option: pct% battery -->
|
<!-- [CHAR_LIMIT=NONE] Battery saver: Description for automatic entry option: pct% battery -->
|
||||||
<string name="battery_saver_desc_turn_on_auto_pct">Turn on automatically at %1$s battery</string>
|
<string name="battery_saver_desc_turn_on_auto_pct">Turn on automatically at %1$s battery</string>
|
||||||
|
|
||||||
|
<!-- Battery saver: Label for button that will turn on battery saver. [CHAR LIMIT=30] -->
|
||||||
|
<string name="battery_saver_button_turn_on">Turn on now</string>
|
||||||
|
|
||||||
|
<!-- Battery saver: Label for button that will turn off battery saver. [CHAR LIMIT=30] -->
|
||||||
|
<string name="battery_saver_button_turn_off">Turn off now</string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=NONE] Label for when app is ignoring battery optimizations -->
|
<!-- [CHAR_LIMIT=NONE] Label for when app is ignoring battery optimizations -->
|
||||||
<string name="not_battery_optimizing">Not using battery optimization</string>
|
<string name="not_battery_optimizing">Not using battery optimization</string>
|
||||||
|
|
||||||
@@ -9287,6 +9302,9 @@
|
|||||||
<!-- Help URI, USB Audio [DO NOT TRANSLATE] -->
|
<!-- Help URI, USB Audio [DO NOT TRANSLATE] -->
|
||||||
<string name="help_url_audio_accessory_not_supported" translatable="false"></string>
|
<string name="help_url_audio_accessory_not_supported" translatable="false"></string>
|
||||||
|
|
||||||
|
<!-- Help URI, battery saver page [DO NOT TRANSLATE] -->
|
||||||
|
<string name="help_url_battery_saver_settings" translatable="false"></string>
|
||||||
|
|
||||||
<!-- Title label for new device suggestion, which is displayed in Settings homepage [CHAR LIMIT=100] -->
|
<!-- Title label for new device suggestion, which is displayed in Settings homepage [CHAR LIMIT=100] -->
|
||||||
<string name="new_device_suggestion_title">What\'s new and exciting?</string>
|
<string name="new_device_suggestion_title">What\'s new and exciting?</string>
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,32 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
<PreferenceScreen
|
||||||
android:title="@string/battery_saver"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:key="battery_saver">
|
android:title="@string/battery_saver"
|
||||||
|
android:key="battery_saver">
|
||||||
|
|
||||||
<!-- Turn on automatically -->
|
<!-- Turn on automatically -->
|
||||||
<DropDownPreference
|
<SwitchPreference
|
||||||
android:key="turn_on_automatically"
|
android:key="auto_battery_saver"
|
||||||
android:title="@string/battery_saver_turn_on_automatically_title"
|
android:title="@string/battery_saver_auto_title"
|
||||||
android:summary="%s" />
|
android:summary="@string/battery_saver_auto_summary"/>
|
||||||
|
<com.android.settings.widget.SeekBarPreference
|
||||||
|
android:key="battery_saver_seek_bar"
|
||||||
|
android:title="@string/battery_saver_seekbar_title"
|
||||||
|
android:max="75"
|
||||||
|
android:min="5"/>
|
||||||
|
<com.android.settings.applications.LayoutPreference
|
||||||
|
android:key="battery_saver_button_container"
|
||||||
|
android:selectable="false"
|
||||||
|
android:layout="@layout/battery_saver_settings_button"/>
|
||||||
|
|
||||||
|
<PreferenceCategory
|
||||||
|
android:key="battery_saver_footer">
|
||||||
|
<com.android.settingslib.widget.FooterPreference
|
||||||
|
android:key="battery_saver_footer_preference"
|
||||||
|
android:title="@*android:string/battery_saver_description"
|
||||||
|
android:selectable="false"/>
|
||||||
|
</PreferenceCategory>
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
android:key="power_management">
|
android:key="power_management">
|
||||||
|
|
||||||
<com.android.settings.widget.MasterSwitchPreference
|
<com.android.settings.widget.MasterSwitchPreference
|
||||||
android:fragment="com.android.settings.fuelgauge.BatterySaverSettings"
|
android:fragment="com.android.settings.fuelgauge.batterysaver.BatterySaverSettings"
|
||||||
android:key="battery_saver_summary"
|
android:key="battery_saver_summary"
|
||||||
android:title="@string/battery_saver"/>
|
android:title="@string/battery_saver"/>
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
android:title="@string/battery_power_management">
|
android:title="@string/battery_power_management">
|
||||||
|
|
||||||
<com.android.settings.widget.MasterSwitchPreference
|
<com.android.settings.widget.MasterSwitchPreference
|
||||||
android:fragment="com.android.settings.fuelgauge.BatterySaverSettings"
|
android:fragment="com.android.settings.fuelgauge.batterysaver.BatterySaverSettings"
|
||||||
android:key="battery_saver_summary"
|
android:key="battery_saver_summary"
|
||||||
android:title="@string/battery_saver"/>
|
android:title="@string/battery_saver"/>
|
||||||
|
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ import com.android.settings.display.NightDisplaySettings;
|
|||||||
import com.android.settings.dream.DreamSettings;
|
import com.android.settings.dream.DreamSettings;
|
||||||
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
||||||
import com.android.settings.fuelgauge.AdvancedPowerUsageDetail;
|
import com.android.settings.fuelgauge.AdvancedPowerUsageDetail;
|
||||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
|
||||||
import com.android.settings.fuelgauge.PowerUsageSummary;
|
import com.android.settings.fuelgauge.PowerUsageSummary;
|
||||||
import com.android.settings.fuelgauge.PowerUsageSummaryLegacy;
|
import com.android.settings.fuelgauge.PowerUsageSummaryLegacy;
|
||||||
import com.android.settings.gestures.AssistGestureSettings;
|
import com.android.settings.gestures.AssistGestureSettings;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import android.os.PowerManager;
|
|||||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.Utils;
|
import com.android.settings.Utils;
|
||||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
|
||||||
|
|
||||||
public class BatterySaverCondition extends Condition {
|
public class BatterySaverCondition extends Condition {
|
||||||
public BatterySaverCondition(ConditionManager manager) {
|
public BatterySaverCondition(ConditionManager manager) {
|
||||||
|
|||||||
@@ -1,253 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2014 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.fuelgauge;
|
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.res.Resources;
|
|
||||||
import android.database.ContentObserver;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.PowerManager;
|
|
||||||
import android.provider.SearchIndexableResource;
|
|
||||||
import android.provider.Settings;
|
|
||||||
import android.provider.Settings.Global;
|
|
||||||
import android.support.annotation.VisibleForTesting;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.Switch;
|
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
|
||||||
import com.android.internal.util.ArrayUtils;
|
|
||||||
import com.android.settings.R;
|
|
||||||
import com.android.settings.SettingsActivity;
|
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
|
||||||
import com.android.settings.Utils;
|
|
||||||
import com.android.settings.dashboard.conditional.BatterySaverCondition;
|
|
||||||
import com.android.settings.dashboard.conditional.ConditionManager;
|
|
||||||
import com.android.settings.notification.SettingPref;
|
|
||||||
import com.android.settings.search.BaseSearchIndexProvider;
|
|
||||||
import com.android.settings.search.Indexable;
|
|
||||||
import com.android.settings.widget.SwitchBar;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class BatterySaverSettings extends SettingsPreferenceFragment
|
|
||||||
implements SwitchBar.OnSwitchChangeListener, BatterySaverReceiver.BatterySaverListener,
|
|
||||||
Indexable {
|
|
||||||
private static final String TAG = "BatterySaverSettings";
|
|
||||||
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
|
|
||||||
private static final String KEY_TURN_ON_AUTOMATICALLY = "turn_on_automatically";
|
|
||||||
private static final long WAIT_FOR_SWITCH_ANIM = 500;
|
|
||||||
|
|
||||||
private final Handler mHandler = new Handler();
|
|
||||||
private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
SwitchBar mSwitchBar;
|
|
||||||
private Context mContext;
|
|
||||||
private boolean mCreated;
|
|
||||||
private SettingPref mTriggerPref;
|
|
||||||
private Switch mSwitch;
|
|
||||||
private boolean mValidListener;
|
|
||||||
private PowerManager mPowerManager;
|
|
||||||
private BatterySaverReceiver mReceiver;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getMetricsCategory() {
|
|
||||||
return MetricsEvent.FUELGAUGE_BATTERY_SAVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
|
||||||
super.onActivityCreated(savedInstanceState);
|
|
||||||
if (mCreated) {
|
|
||||||
mSwitchBar.show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
mCreated = true;
|
|
||||||
addPreferencesFromResource(R.xml.battery_saver_settings);
|
|
||||||
mFooterPreferenceMixin.createFooterPreference()
|
|
||||||
.setTitle(com.android.internal.R.string.battery_saver_description);
|
|
||||||
mContext = getActivity();
|
|
||||||
mSwitchBar = ((SettingsActivity) mContext).getSwitchBar();
|
|
||||||
mSwitchBar.setSwitchBarText(R.string.battery_saver_master_switch_title,
|
|
||||||
R.string.battery_saver_master_switch_title);
|
|
||||||
mSwitch = mSwitchBar.getSwitch();
|
|
||||||
mSwitchBar.show();
|
|
||||||
|
|
||||||
int[] levelChoices = getResources().getIntArray(R.array.battery_saver_trigger_values);
|
|
||||||
final int currentThreshold = Global.getInt(mContext.getContentResolver(),
|
|
||||||
Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
|
|
||||||
levelChoices = ArrayUtils.appendInt(levelChoices, currentThreshold);
|
|
||||||
Arrays.sort(levelChoices);
|
|
||||||
|
|
||||||
mTriggerPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_TURN_ON_AUTOMATICALLY,
|
|
||||||
Global.LOW_POWER_MODE_TRIGGER_LEVEL,
|
|
||||||
0, /*default*/
|
|
||||||
levelChoices) {
|
|
||||||
@Override
|
|
||||||
protected String getCaption(Resources res, int value) {
|
|
||||||
if (value > 0 && value <= 100) {
|
|
||||||
return res.getString(R.string.battery_saver_turn_on_automatically_pct,
|
|
||||||
Utils.formatPercentage(value));
|
|
||||||
}
|
|
||||||
return res.getString(R.string.battery_saver_turn_on_automatically_never);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
mTriggerPref.init(this);
|
|
||||||
|
|
||||||
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
|
|
||||||
mReceiver = new BatterySaverReceiver(mContext);
|
|
||||||
mReceiver.setBatterySaverListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroyView() {
|
|
||||||
super.onDestroyView();
|
|
||||||
mSwitchBar.hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
mSettingsObserver.setListening(true);
|
|
||||||
mReceiver.setListening(true);
|
|
||||||
if (!mValidListener) {
|
|
||||||
mSwitchBar.addOnSwitchChangeListener(this);
|
|
||||||
mValidListener = true;
|
|
||||||
}
|
|
||||||
updateSwitch();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
mSettingsObserver.setListening(false);
|
|
||||||
mReceiver.setListening(false);
|
|
||||||
if (mValidListener) {
|
|
||||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
|
||||||
mValidListener = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
|
||||||
mHandler.removeCallbacks(mStartMode);
|
|
||||||
if (isChecked) {
|
|
||||||
mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM);
|
|
||||||
} else {
|
|
||||||
if (DEBUG) Log.d(TAG, "Stopping low power mode from settings");
|
|
||||||
trySetPowerSaveMode(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void trySetPowerSaveMode(boolean mode) {
|
|
||||||
if (!mPowerManager.setPowerSaveMode(mode)) {
|
|
||||||
if (DEBUG) Log.d(TAG, "Setting mode failed, fallback to current value");
|
|
||||||
mHandler.post(mUpdateSwitch);
|
|
||||||
}
|
|
||||||
// TODO: Remove once broadcast is in place.
|
|
||||||
ConditionManager.get(getContext()).getCondition(BatterySaverCondition.class).refreshState();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateSwitch() {
|
|
||||||
final boolean mode = mPowerManager.isPowerSaveMode();
|
|
||||||
if (DEBUG) Log.d(TAG, "updateSwitch: isChecked=" + mSwitch.isChecked() + " mode=" + mode);
|
|
||||||
if (mode == mSwitch.isChecked()) return;
|
|
||||||
|
|
||||||
// set listener to null so that that code below doesn't trigger onCheckedChanged()
|
|
||||||
if (mValidListener) {
|
|
||||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
|
||||||
}
|
|
||||||
mSwitch.setChecked(mode);
|
|
||||||
if (mValidListener) {
|
|
||||||
mSwitchBar.addOnSwitchChangeListener(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Runnable mUpdateSwitch = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
updateSwitch();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final Runnable mStartMode = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
AsyncTask.execute(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (DEBUG) Log.d(TAG, "Starting low power mode from settings");
|
|
||||||
trySetPowerSaveMode(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPowerSaveModeChanged() {
|
|
||||||
mHandler.post(mUpdateSwitch);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBatteryChanged(boolean pluggedIn) {
|
|
||||||
mSwitchBar.setEnabled(!pluggedIn);
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class SettingsObserver extends ContentObserver {
|
|
||||||
private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI
|
|
||||||
= Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL);
|
|
||||||
|
|
||||||
public SettingsObserver(Handler handler) {
|
|
||||||
super(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onChange(boolean selfChange, Uri uri) {
|
|
||||||
if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) {
|
|
||||||
mTriggerPref.update(mContext);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setListening(boolean listening) {
|
|
||||||
final ContentResolver cr = getContentResolver();
|
|
||||||
if (listening) {
|
|
||||||
cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this);
|
|
||||||
} else {
|
|
||||||
cr.unregisterContentObserver(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For Search.
|
|
||||||
*/
|
|
||||||
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
|
||||||
new BaseSearchIndexProvider() {
|
|
||||||
@Override
|
|
||||||
public List<SearchIndexableResource> getXmlResourcesToIndex(
|
|
||||||
Context context, boolean enabled) {
|
|
||||||
final SearchIndexableResource sir = new SearchIndexableResource(context);
|
|
||||||
sir.xmlResId = R.xml.battery_saver_settings;
|
|
||||||
return Arrays.asList(sir);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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.fuelgauge.batterysaver;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settings.core.TogglePreferenceController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller that update whether to turn on battery saver automatically
|
||||||
|
*/
|
||||||
|
public class AutoBatterySaverPreferenceController extends TogglePreferenceController implements
|
||||||
|
Preference.OnPreferenceChangeListener {
|
||||||
|
private static final int LOW_POWER_MODE_TRIGGER_THRESHOLD = 15;
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static final String KEY_AUTO_BATTERY_SAVER = "auto_battery_saver";
|
||||||
|
|
||||||
|
public AutoBatterySaverPreferenceController(Context context) {
|
||||||
|
super(context, KEY_AUTO_BATTERY_SAVER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
return AVAILABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isChecked() {
|
||||||
|
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setChecked(boolean isChecked) {
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL,
|
||||||
|
isChecked
|
||||||
|
? LOW_POWER_MODE_TRIGGER_THRESHOLD
|
||||||
|
: 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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.fuelgauge.batterysaver;
|
||||||
|
|
||||||
|
import android.content.ContentResolver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.ContentObserver;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.Utils;
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settings.widget.SeekBarPreference;
|
||||||
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller that update the battery saver seekbar
|
||||||
|
*/
|
||||||
|
public class AutoBatterySeekBarPreferenceController extends BasePreferenceController implements
|
||||||
|
LifecycleObserver, OnStart, OnStop, SeekBarPreference.OnPreferenceChangeListener {
|
||||||
|
@VisibleForTesting
|
||||||
|
static final String KEY_AUTO_BATTERY_SEEK_BAR = "battery_saver_seek_bar";
|
||||||
|
private SeekBarPreference mPreference;
|
||||||
|
private AutoBatterySaverSettingObserver mContentObserver;
|
||||||
|
|
||||||
|
public AutoBatterySeekBarPreferenceController(Context context, Lifecycle lifecycle) {
|
||||||
|
super(context, KEY_AUTO_BATTERY_SEEK_BAR);
|
||||||
|
mContentObserver = new AutoBatterySaverSettingObserver(new Handler());
|
||||||
|
if (lifecycle != null) {
|
||||||
|
lifecycle.addObserver(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
|
super.displayPreference(screen);
|
||||||
|
mPreference = (SeekBarPreference) screen.findPreference(
|
||||||
|
KEY_AUTO_BATTERY_SEEK_BAR);
|
||||||
|
updatePreference(mPreference);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
return AVAILABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateState(Preference preference) {
|
||||||
|
super.updateState(preference);
|
||||||
|
updatePreference(preference);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
mContentObserver.registerContentObserver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop() {
|
||||||
|
mContentObserver.unRegisterContentObserver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
|
final int progress = (int) newValue;
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, progress);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void updatePreference(Preference preference) {
|
||||||
|
final ContentResolver contentResolver = mContext.getContentResolver();
|
||||||
|
final int level = Settings.Global.getInt(contentResolver,
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
|
||||||
|
if (level == 0) {
|
||||||
|
preference.setVisible(false);
|
||||||
|
} else {
|
||||||
|
preference.setVisible(true);
|
||||||
|
preference.setTitle(mContext.getString(R.string.battery_saver_seekbar_title,
|
||||||
|
Utils.formatPercentage(level)));
|
||||||
|
((SeekBarPreference) preference).setProgress(level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observer that listens to change from {@link Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL}
|
||||||
|
*/
|
||||||
|
private final class AutoBatterySaverSettingObserver extends ContentObserver {
|
||||||
|
private final Uri mUri = Settings.Global.getUriFor(
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL);
|
||||||
|
private final ContentResolver mContentResolver;
|
||||||
|
|
||||||
|
public AutoBatterySaverSettingObserver(Handler handler) {
|
||||||
|
super(handler);
|
||||||
|
mContentResolver = mContext.getContentResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerContentObserver() {
|
||||||
|
mContentResolver.registerContentObserver(mUri, false, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unRegisterContentObserver() {
|
||||||
|
mContentResolver.unregisterContentObserver(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChange(boolean selfChange, Uri uri, int userId) {
|
||||||
|
if (mUri.equals(uri)) {
|
||||||
|
updatePreference(mPreference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2014 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.fuelgauge.batterysaver;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
import android.provider.SearchIndexableResource;
|
||||||
|
import android.provider.Settings.Global;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
|
import com.android.internal.util.ArrayUtils;
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
|
import com.android.settings.search.BaseSearchIndexProvider;
|
||||||
|
import com.android.settings.search.Indexable;
|
||||||
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Battery saver settings page
|
||||||
|
*/
|
||||||
|
public class BatterySaverSettings extends DashboardFragment {
|
||||||
|
private static final String TAG = "BatterySaverSettings";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMetricsCategory() {
|
||||||
|
return MetricsEvent.FUELGAUGE_BATTERY_SAVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getPreferenceScreenResId() {
|
||||||
|
return R.xml.battery_saver_settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getLogTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||||
|
return buildPreferenceControllers(context, getLifecycle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHelpResource() {
|
||||||
|
return R.string.help_url_battery_saver_settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<AbstractPreferenceController> buildPreferenceControllers(
|
||||||
|
Context context, Lifecycle lifecycle) {
|
||||||
|
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||||
|
controllers.add(new AutoBatterySaverPreferenceController(context));
|
||||||
|
controllers.add(new AutoBatterySeekBarPreferenceController(context, lifecycle));
|
||||||
|
return controllers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For Search.
|
||||||
|
*/
|
||||||
|
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||||
|
new BaseSearchIndexProvider() {
|
||||||
|
@Override
|
||||||
|
public List<SearchIndexableResource> getXmlResourcesToIndex(
|
||||||
|
Context context, boolean enabled) {
|
||||||
|
final SearchIndexableResource sir = new SearchIndexableResource(context);
|
||||||
|
sir.xmlResId = R.xml.battery_saver_settings;
|
||||||
|
return Arrays.asList(sir);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||||
|
return buildPreferenceControllers(context, null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -50,7 +50,7 @@ import com.android.settings.display.NightDisplaySettings;
|
|||||||
import com.android.settings.display.ScreenZoomSettings;
|
import com.android.settings.display.ScreenZoomSettings;
|
||||||
import com.android.settings.dream.DreamSettings;
|
import com.android.settings.dream.DreamSettings;
|
||||||
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
||||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
|
||||||
import com.android.settings.fuelgauge.PowerUsageAdvanced;
|
import com.android.settings.fuelgauge.PowerUsageAdvanced;
|
||||||
import com.android.settings.fuelgauge.PowerUsageSummary;
|
import com.android.settings.fuelgauge.PowerUsageSummary;
|
||||||
import com.android.settings.fuelgauge.SmartBatterySettings;
|
import com.android.settings.fuelgauge.SmartBatterySettings;
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
|
|
||||||
private int mProgress;
|
private int mProgress;
|
||||||
private int mMax;
|
private int mMax;
|
||||||
|
private int mMin;
|
||||||
private boolean mTrackingTouch;
|
private boolean mTrackingTouch;
|
||||||
|
|
||||||
private boolean mContinuousUpdates;
|
private boolean mContinuousUpdates;
|
||||||
@@ -55,6 +56,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
TypedArray a = context.obtainStyledAttributes(
|
TypedArray a = context.obtainStyledAttributes(
|
||||||
attrs, com.android.internal.R.styleable.ProgressBar, defStyleAttr, defStyleRes);
|
attrs, com.android.internal.R.styleable.ProgressBar, defStyleAttr, defStyleRes);
|
||||||
setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax));
|
setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax));
|
||||||
|
setMin(a.getInt(com.android.internal.R.styleable.ProgressBar_min, mMin));
|
||||||
a.recycle();
|
a.recycle();
|
||||||
|
|
||||||
a = context.obtainStyledAttributes(attrs,
|
a = context.obtainStyledAttributes(attrs,
|
||||||
@@ -94,6 +96,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
com.android.internal.R.id.seekbar);
|
com.android.internal.R.id.seekbar);
|
||||||
mSeekBar.setOnSeekBarChangeListener(this);
|
mSeekBar.setOnSeekBarChangeListener(this);
|
||||||
mSeekBar.setMax(mMax);
|
mSeekBar.setMax(mMax);
|
||||||
|
mSeekBar.setMin(mMin);
|
||||||
mSeekBar.setProgress(mProgress);
|
mSeekBar.setProgress(mProgress);
|
||||||
mSeekBar.setEnabled(isEnabled());
|
mSeekBar.setEnabled(isEnabled());
|
||||||
final CharSequence title = getTitle();
|
final CharSequence title = getTitle();
|
||||||
@@ -154,10 +157,21 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMin(int min) {
|
||||||
|
if (min != mMin) {
|
||||||
|
mMin = min;
|
||||||
|
notifyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getMax() {
|
public int getMax() {
|
||||||
return mMax;
|
return mMax;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getMin() {
|
||||||
|
return mMin;
|
||||||
|
}
|
||||||
|
|
||||||
public void setProgress(int progress) {
|
public void setProgress(int progress) {
|
||||||
setProgress(progress, true);
|
setProgress(progress, true);
|
||||||
}
|
}
|
||||||
@@ -187,8 +201,8 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
if (progress > mMax) {
|
if (progress > mMax) {
|
||||||
progress = mMax;
|
progress = mMax;
|
||||||
}
|
}
|
||||||
if (progress < 0) {
|
if (progress < mMin) {
|
||||||
progress = 0;
|
progress = mMin;
|
||||||
}
|
}
|
||||||
if (progress != mProgress) {
|
if (progress != mProgress) {
|
||||||
mProgress = progress;
|
mProgress = progress;
|
||||||
@@ -257,6 +271,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
final SavedState myState = new SavedState(superState);
|
final SavedState myState = new SavedState(superState);
|
||||||
myState.progress = mProgress;
|
myState.progress = mProgress;
|
||||||
myState.max = mMax;
|
myState.max = mMax;
|
||||||
|
myState.min = mMin;
|
||||||
return myState;
|
return myState;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +288,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
super.onRestoreInstanceState(myState.getSuperState());
|
super.onRestoreInstanceState(myState.getSuperState());
|
||||||
mProgress = myState.progress;
|
mProgress = myState.progress;
|
||||||
mMax = myState.max;
|
mMax = myState.max;
|
||||||
|
mMin = myState.min;
|
||||||
notifyChanged();
|
notifyChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,6 +301,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
private static class SavedState extends BaseSavedState {
|
private static class SavedState extends BaseSavedState {
|
||||||
int progress;
|
int progress;
|
||||||
int max;
|
int max;
|
||||||
|
int min;
|
||||||
|
|
||||||
public SavedState(Parcel source) {
|
public SavedState(Parcel source) {
|
||||||
super(source);
|
super(source);
|
||||||
@@ -292,6 +309,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
// Restore the click counter
|
// Restore the click counter
|
||||||
progress = source.readInt();
|
progress = source.readInt();
|
||||||
max = source.readInt();
|
max = source.readInt();
|
||||||
|
min = source.readInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -301,6 +319,7 @@ public class SeekBarPreference extends RestrictedPreference
|
|||||||
// Save the click counter
|
// Save the click counter
|
||||||
dest.writeInt(progress);
|
dest.writeInt(progress);
|
||||||
dest.writeInt(max);
|
dest.writeInt(max);
|
||||||
|
dest.writeInt(min);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SavedState(Parcelable superState) {
|
public SavedState(Parcelable superState) {
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.fuelgauge;
|
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.provider.SearchIndexableResource;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
|
||||||
import com.android.settings.TestConfig;
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
|
||||||
import com.android.settings.widget.SwitchBar;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
import org.robolectric.annotation.Config;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
|
||||||
public class BatterySaverSettingsTest {
|
|
||||||
private Context mContext;
|
|
||||||
private BatterySaverSettings mBatterySaverSettings;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
|
|
||||||
mContext = RuntimeEnvironment.application;
|
|
||||||
mBatterySaverSettings = new BatterySaverSettings();
|
|
||||||
mBatterySaverSettings.mSwitchBar = new SwitchBar(mContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOnBatteryChanged_pluggedIn_setDisable() {
|
|
||||||
mBatterySaverSettings.onBatteryChanged(true /* pluggedIn */);
|
|
||||||
|
|
||||||
assertThat(mBatterySaverSettings.mSwitchBar.isEnabled()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOnBatteryChanged_notPluggedIn_setEnable() {
|
|
||||||
mBatterySaverSettings.onBatteryChanged(false /* pluggedIn */);
|
|
||||||
|
|
||||||
assertThat(mBatterySaverSettings.mSwitchBar.isEnabled()).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void searchProvider_shouldIndexDefaultXml() {
|
|
||||||
final List<SearchIndexableResource> sir = mBatterySaverSettings.SEARCH_INDEX_DATA_PROVIDER
|
|
||||||
.getXmlResourcesToIndex(mContext, true /* enabled */);
|
|
||||||
|
|
||||||
assertThat(sir).hasSize(1);
|
|
||||||
assertThat(sir.get(0).xmlResId).isEqualTo(R.xml.battery_saver_settings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* 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.fuelgauge.batterysaver;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
|
||||||
|
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.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class AutoBatterySaverPreferenceControllerTest {
|
||||||
|
|
||||||
|
private AutoBatterySaverPreferenceController mController;
|
||||||
|
private Context mContext;
|
||||||
|
private SwitchPreference mPreference;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mPreference = new SwitchPreference(mContext);
|
||||||
|
mController = new AutoBatterySaverPreferenceController(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateState_lowPowerLevelZero_preferenceNotChecked() {
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
|
||||||
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
|
assertThat(mPreference.isChecked()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateState_lowPowerLevelZero_preferenceChecked() {
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15);
|
||||||
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
|
assertThat(mPreference.isChecked()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnPreferenceChange_turnOn_setValueNotZero() {
|
||||||
|
mController.onPreferenceChange(mPreference, true);
|
||||||
|
|
||||||
|
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isNotEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnPreferenceChange_turnOff_setValueZero() {
|
||||||
|
mController.onPreferenceChange(mPreference, false);
|
||||||
|
|
||||||
|
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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.fuelgauge.batterysaver;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import android.arch.lifecycle.LifecycleOwner;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.widget.SeekBarPreference;
|
||||||
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
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 AutoBatterySeekBarPreferenceControllerTest {
|
||||||
|
private static final int TRIGGER_LEVEL = 15;
|
||||||
|
|
||||||
|
private AutoBatterySeekBarPreferenceController mController;
|
||||||
|
private Context mContext;
|
||||||
|
private SeekBarPreference mPreference;
|
||||||
|
private Lifecycle mLifecycle;
|
||||||
|
private LifecycleOwner mLifecycleOwner;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mLifecycleOwner = () -> mLifecycle;
|
||||||
|
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||||
|
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mPreference = new SeekBarPreference(mContext);
|
||||||
|
mPreference.setMax(100);
|
||||||
|
mController = new AutoBatterySeekBarPreferenceController(mContext, mLifecycle);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreference_lowPowerLevelZero_preferenceInvisible() {
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
|
||||||
|
|
||||||
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
|
assertThat(mPreference.isVisible()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreference_lowPowerLevelNotZero_updatePreference() {
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, TRIGGER_LEVEL);
|
||||||
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
|
assertThat(mPreference.isVisible()).isTrue();
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo("Turn on automatically at 15%");
|
||||||
|
assertThat(mPreference.getProgress()).isEqualTo(TRIGGER_LEVEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnPreferenceChange_updateValue() {
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
|
||||||
|
|
||||||
|
mController.onPreferenceChange(mPreference, TRIGGER_LEVEL);
|
||||||
|
|
||||||
|
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(TRIGGER_LEVEL);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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.widget;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
|
||||||
|
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.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class SeekBarPreferenceTest {
|
||||||
|
private static final int MAX = 75;
|
||||||
|
private static final int MIN = 5;
|
||||||
|
private static final int PROGRESS = 16;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private SeekBarPreference mSeekBarPreference;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
|
||||||
|
mSeekBarPreference = new SeekBarPreference(mContext);
|
||||||
|
mSeekBarPreference.setMax(MAX);
|
||||||
|
mSeekBarPreference.setMin(MIN);
|
||||||
|
mSeekBarPreference.setProgress(PROGRESS);
|
||||||
|
mSeekBarPreference.setPersistent(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSaveAndRestoreInstanceState() {
|
||||||
|
final Parcelable parcelable = mSeekBarPreference.onSaveInstanceState();
|
||||||
|
|
||||||
|
final SeekBarPreference preference = new SeekBarPreference(mContext);
|
||||||
|
preference.onRestoreInstanceState(parcelable);
|
||||||
|
|
||||||
|
assertThat(preference.getMax()).isEqualTo(MAX);
|
||||||
|
assertThat(preference.getMin()).isEqualTo(MIN);
|
||||||
|
assertThat(preference.getProgress()).isEqualTo(PROGRESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user