diff --git a/res/layout/battery_usage_graph.xml b/res/layout/battery_usage_graph.xml new file mode 100644 index 00000000000..ddf7d9330a1 --- /dev/null +++ b/res/layout/battery_usage_graph.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + diff --git a/res/layout/preference_widget_summary.xml b/res/layout/preference_widget_summary.xml new file mode 100644 index 00000000000..aa4c76b84d8 --- /dev/null +++ b/res/layout/preference_widget_summary.xml @@ -0,0 +1,22 @@ + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 47e47874e90..47ad7c928ed 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7166,4 +7166,19 @@ Additional Fingerprints + + On / %1$s + + + Off / %1$s + + + Never turn on automatically + + + Turn on automatically at %1$s battery + + + Not using battery optimization + diff --git a/res/xml/power_usage_summary.xml b/res/xml/power_usage_summary.xml index 3f39d472d9f..1ae0a445155 100644 --- a/res/xml/power_usage_summary.xml +++ b/res/xml/power_usage_summary.xml @@ -19,6 +19,10 @@ android:title="@string/power_usage_summary_title" settings:keywords="@string/keywords_battery"> + + diff --git a/src/com/android/settings/TintablePreference.java b/src/com/android/settings/TintablePreference.java index 0ada6ed2c07..45f43fbe969 100644 --- a/src/com/android/settings/TintablePreference.java +++ b/src/com/android/settings/TintablePreference.java @@ -42,6 +42,8 @@ public class TintablePreference extends Preference { if (mTintColor != 0) { ((ImageView) view.findViewById(android.R.id.icon)).setImageTintList( ColorStateList.valueOf(mTintColor)); + } else { + ((ImageView) view.findViewById(android.R.id.icon)).setImageTintList(null); } } } diff --git a/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java b/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java index 461c498ad6b..7aafebad79a 100644 --- a/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java +++ b/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java @@ -17,17 +17,17 @@ package com.android.settings.fuelgauge; import android.content.Context; -import android.content.Intent; -import android.os.BatteryStats; import android.os.Bundle; +import android.os.SystemClock; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceViewHolder; import android.util.AttributeSet; -import android.view.ViewGroup; - +import android.widget.TextView; import com.android.internal.os.BatteryStatsHelper; import com.android.settings.R; -import com.android.settings.SettingsActivity; +import com.android.settings.Utils; +import com.android.settingslib.BatteryInfo; +import com.android.settingslib.graph.UsageView; /** * Custom preference for displaying power consumption as a bar and an icon on the left for the @@ -38,73 +38,41 @@ public class BatteryHistoryPreference extends Preference { protected static final String BATTERY_HISTORY_FILE = "tmp_bat_history.bin"; - private BatteryStats mStats; - private Intent mBatteryBroadcast; - - private BatteryHistoryChart mChart; private BatteryStatsHelper mHelper; + private BatteryInfo mBatteryInfo; public BatteryHistoryPreference(Context context, AttributeSet attrs) { super(context, attrs); + setLayoutResource(R.layout.battery_usage_graph); } - + @Override protected void onClick() { - if (!isEnabled()) { - return; - } mHelper.storeStatsHistoryInFile(BATTERY_HISTORY_FILE); Bundle args = new Bundle(); args.putString(BatteryHistoryDetail.EXTRA_STATS, BATTERY_HISTORY_FILE); - args.putParcelable(BatteryHistoryDetail.EXTRA_BROADCAST, - mHelper.getBatteryBroadcast()); - if (getContext() instanceof SettingsActivity) { - SettingsActivity sa = (SettingsActivity) getContext(); - sa.startPreferencePanel(BatteryHistoryDetail.class.getName(), args, - R.string.history_details_title, null, null, 0); - } + args.putParcelable(BatteryHistoryDetail.EXTRA_BROADCAST, mHelper.getBatteryBroadcast()); + Utils.startWithFragment(getContext(), BatteryHistoryDetail.class.getName(), args, + null, 0, R.string.history_details_title, null); } public void setStats(BatteryStatsHelper batteryStats) { - // Clear out the chart to receive new data. - mChart = null; mHelper = batteryStats; - mStats = batteryStats.getStats(); - mBatteryBroadcast = batteryStats.getBatteryBroadcast(); - if (getLayoutResource() != R.layout.battery_history_chart) { - // Now we should have some data, set the layout we want. - setLayoutResource(R.layout.battery_history_chart); - } + final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000; + mBatteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryStats.getBatteryBroadcast(), + batteryStats.getStats(), elapsedRealtimeUs); notifyChanged(); } - BatteryStats getStats() { - return mStats; - } - @Override public void onBindViewHolder(PreferenceViewHolder view) { - super.onBindViewHolder(view); - - if (mStats == null) { + if (mBatteryInfo == null) { return; } - BatteryHistoryChart chart = (BatteryHistoryChart) view.findViewById( - R.id.battery_history_chart); - if (mChart == null) { - // First time: use and initialize this chart. - chart.setStats(mStats, mBatteryBroadcast); - mChart = chart; - } else { - // All future times: forget the newly inflated chart, re-use the - // already initialized chart from last time. - ViewGroup parent = (ViewGroup) chart.getParent(); - int index = parent.indexOfChild(chart); - parent.removeViewAt(index); - if (mChart.getParent() != null) { - ((ViewGroup) mChart.getParent()).removeView(mChart); - } - parent.addView(mChart, index); - } + view.setDividerAllowedAbove(true); + ((TextView) view.findViewById(R.id.charge)).setText(mBatteryInfo.batteryPercentString); + ((TextView) view.findViewById(R.id.estimation)).setText(mBatteryInfo.remainingLabel); + UsageView usageView = (UsageView) view.findViewById(R.id.battery_usage); + mBatteryInfo.bindHistory(usageView); } } diff --git a/src/com/android/settings/fuelgauge/BatterySaverPreference.java b/src/com/android/settings/fuelgauge/BatterySaverPreference.java new file mode 100644 index 00000000000..9e0f39b4858 --- /dev/null +++ b/src/com/android/settings/fuelgauge/BatterySaverPreference.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 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.Context; +import android.database.ContentObserver; +import android.os.Handler; +import android.os.PowerManager; +import android.provider.Settings; +import android.provider.Settings.Global; +import android.support.v7.preference.Preference; +import android.util.AttributeSet; +import android.view.View; +import com.android.settings.R; +import com.android.settings.Utils; + +public class BatterySaverPreference extends Preference { + + private PowerManager mPowerManager; + + public BatterySaverPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + protected void performClick(View view) { + Utils.startWithFragment(getContext(), getFragment(), null, null, 0, 0, getTitle()); + } + + @Override + public void onAttached() { + super.onAttached(); + mPowerManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); + mObserver.onChange(true); + getContext().getContentResolver().registerContentObserver( + Settings.Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL), true, mObserver); + getContext().getContentResolver().registerContentObserver( + Settings.Global.getUriFor(Global.LOW_POWER_MODE), true, mObserver); + } + + @Override + public void onDetached() { + super.onDetached(); + getContext().getContentResolver().unregisterContentObserver(mObserver); + } + + private void updateSwitch() { + final Context context = getContext(); + final boolean mode = mPowerManager.isPowerSaveMode(); + int format = mode ? R.string.battery_saver_on_summary + : R.string.battery_saver_off_summary; + int percent = Settings.Global.getInt(context.getContentResolver(), + Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); + int percentFormat = percent > 0 ? R.string.battery_saver_desc_turn_on_auto_pct + : R.string.battery_saver_desc_turn_on_auto_never; + setSummary(context.getString(format, context.getString(percentFormat, + Utils.formatPercentage(percent)))); + } + + private final ContentObserver mObserver = new ContentObserver(new Handler()) { + @Override + public void onChange(boolean selfChange) { + updateSwitch(); + } + }; + +} diff --git a/src/com/android/settings/fuelgauge/PowerGaugePreference.java b/src/com/android/settings/fuelgauge/PowerGaugePreference.java index 5b39a14b57e..b0bf4b41722 100644 --- a/src/com/android/settings/fuelgauge/PowerGaugePreference.java +++ b/src/com/android/settings/fuelgauge/PowerGaugePreference.java @@ -20,30 +20,37 @@ import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.support.v7.preference.PreferenceViewHolder; +import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; - -import com.android.settings.AppProgressPreference; +import com.android.settings.R; +import com.android.settings.TintablePreference; import com.android.settings.Utils; /** * Custom preference for displaying power consumption as a bar and an icon on * the left for the subsystem/app type. */ -public class PowerGaugePreference extends AppProgressPreference { +public class PowerGaugePreference extends TintablePreference { + private final int mIconSize; + private BatteryEntry mInfo; private final CharSequence mContentDescription; + private CharSequence mProgress; public PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info) { super(context, null); setIcon(icon != null ? icon : new ColorDrawable(0)); + setWidgetLayoutResource(R.layout.preference_widget_summary); mInfo = info; mContentDescription = contentDescription; + mIconSize = context.getResources().getDimensionPixelSize(R.dimen.app_icon_size); } public void setPercent(double percentOfMax, double percentOfTotal) { - setProgress((int) Math.ceil(percentOfMax)); - setSummary(Utils.formatPercentage((int) (percentOfTotal + 0.5))); + mProgress = Utils.formatPercentage((int) (percentOfTotal + 0.5)); + notifyChanged(); } BatteryEntry getInfo() { @@ -53,7 +60,10 @@ public class PowerGaugePreference extends AppProgressPreference { @Override public void onBindViewHolder(PreferenceViewHolder view) { super.onBindViewHolder(view); + ImageView icon = (ImageView) view.findViewById(android.R.id.icon); + icon.setLayoutParams(new LinearLayout.LayoutParams(mIconSize, mIconSize)); + ((TextView) view.findViewById(R.id.widget_summary)).setText(mProgress); if (mContentDescription != null) { final TextView titleView = (TextView) view.findViewById(android.R.id.title); titleView.setContentDescription(mContentDescription); diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java index d7b6849800b..a0d276f20d6 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java +++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java @@ -65,7 +65,6 @@ public class PowerUsageSummary extends PowerUsageBase { private static final String KEY_BATTERY_HISTORY = "battery_history"; private static final int MENU_STATS_TYPE = Menu.FIRST; - private static final int MENU_BATTERY_SAVER = Menu.FIRST + 2; private static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3; private static final int MENU_HELP = Menu.FIRST + 4; @@ -134,9 +133,6 @@ public class PowerUsageSummary extends PowerUsageBase { .setAlphabeticShortcut('t'); } - MenuItem batterySaver = menu.add(0, MENU_BATTERY_SAVER, 0, R.string.battery_saver); - batterySaver.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); - menu.add(0, MENU_HIGH_POWER_APPS, 0, R.string.high_power_apps); super.onCreateOptionsMenu(menu, inflater); } @@ -158,10 +154,6 @@ public class PowerUsageSummary extends PowerUsageBase { } refreshStats(); return true; - case MENU_BATTERY_SAVER: - sa.startPreferencePanel(BatterySaverSettings.class.getName(), null, - R.string.battery_saver, null, null, 0); - return true; case MENU_HIGH_POWER_APPS: Bundle args = new Bundle(); args.putString(ManageApplications.EXTRA_CLASSNAME, @@ -280,6 +272,7 @@ public class PowerUsageSummary extends PowerUsageBase { protected void refreshStats() { super.refreshStats(); + PowerWhitelistBackend powerWhiteist = PowerWhitelistBackend.getInstance(); updatePreference(mHistPref); mAppListGroup.removeAll(); mAppListGroup.setOrderingAsAdded(false); @@ -352,6 +345,12 @@ public class PowerUsageSummary extends PowerUsageBase { pref.setTitle(entry.getLabel()); pref.setOrder(i + 1); pref.setPercent(percentOfMax, percentOfTotal); + if (sipper.drainType == DrainType.APP) { + pref.setSummary(powerWhiteist.isWhitelisted(entry.defaultPackageName) + || powerWhiteist.isSysWhitelisted(entry.defaultPackageName) + ? getString(R.string.not_battery_optimizing) + : null); + } if (sipper.uidObj != null) { pref.setKey(Integer.toString(sipper.uidObj.getUid())); }