Rework the battery graph to include the time remaining or time to charge. Change-Id: Ib26b761cb10e01f5f3aa4189db10d44b8ce62f89
288 lines
11 KiB
Java
288 lines
11 KiB
Java
/*
|
|
* Copyright (C) 2009 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.app.Activity;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.os.BatteryManager;
|
|
import android.os.BatteryStats;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.os.Parcel;
|
|
import android.os.UserHandle;
|
|
import android.os.UserManager;
|
|
import android.preference.Preference;
|
|
import android.preference.PreferenceFragment;
|
|
import android.preference.PreferenceGroup;
|
|
import android.preference.PreferenceScreen;
|
|
import android.text.TextUtils;
|
|
import android.view.Menu;
|
|
import android.view.MenuInflater;
|
|
import android.view.MenuItem;
|
|
|
|
import com.android.internal.os.BatterySipper;
|
|
import com.android.internal.os.BatteryStatsHelper;
|
|
import com.android.internal.os.PowerProfile;
|
|
import com.android.settings.HelpUtils;
|
|
import com.android.settings.R;
|
|
import com.android.settings.SettingsActivity;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Displays a list of apps and subsystems that consume power, ordered by how much power was
|
|
* consumed since the last time it was unplugged.
|
|
*/
|
|
public class PowerUsageSummary extends PreferenceFragment {
|
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
private static final String TAG = "PowerUsageSummary";
|
|
|
|
private static final String KEY_APP_LIST = "app_list";
|
|
|
|
private static final int MENU_STATS_TYPE = Menu.FIRST;
|
|
private static final int MENU_STATS_REFRESH = Menu.FIRST + 1;
|
|
private static final int MENU_HELP = Menu.FIRST + 2;
|
|
|
|
private UserManager mUm;
|
|
|
|
private PreferenceGroup mAppListGroup;
|
|
private String mBatteryLevel;
|
|
private String mBatteryStatus;
|
|
|
|
private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
|
|
|
|
private static final int MIN_POWER_THRESHOLD = 5;
|
|
private static final int MAX_ITEMS_TO_LIST = 10;
|
|
|
|
private BatteryStatsHelper mStatsHelper;
|
|
|
|
private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
String action = intent.getAction();
|
|
if (Intent.ACTION_BATTERY_CHANGED.equals(action)
|
|
&& updateBatteryStatus(intent)) {
|
|
mStatsHelper.clearStats();
|
|
refreshStats();
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
public void onAttach(Activity activity) {
|
|
super.onAttach(activity);
|
|
mUm = (UserManager) activity.getSystemService(Context.USER_SERVICE);
|
|
mStatsHelper = new BatteryStatsHelper(activity, true);
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle icicle) {
|
|
super.onCreate(icicle);
|
|
mStatsHelper.create(icicle);
|
|
|
|
addPreferencesFromResource(R.xml.power_usage_summary);
|
|
mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
|
|
setHasOptionsMenu(true);
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
updateBatteryStatus(getActivity().registerReceiver(mBatteryInfoReceiver,
|
|
new IntentFilter(Intent.ACTION_BATTERY_CHANGED)));
|
|
refreshStats();
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
BatteryEntry.stopRequestQueue();
|
|
mHandler.removeMessages(BatteryEntry.MSG_UPDATE_NAME_ICON);
|
|
getActivity().unregisterReceiver(mBatteryInfoReceiver);
|
|
super.onPause();
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
if (getActivity().isChangingConfigurations()) {
|
|
mStatsHelper.storeState();
|
|
BatteryEntry.clearUidCache();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
|
|
if (preference instanceof BatteryHistoryPreference) {
|
|
Parcel hist = Parcel.obtain();
|
|
mStatsHelper.getStats().writeToParcelWithoutUids(hist, 0);
|
|
byte[] histData = hist.marshall();
|
|
Bundle args = new Bundle();
|
|
args.putByteArray(BatteryHistoryDetail.EXTRA_STATS, histData);
|
|
args.putParcelable(BatteryHistoryDetail.EXTRA_BROADCAST,
|
|
mStatsHelper.getBatteryBroadcast());
|
|
SettingsActivity sa = (SettingsActivity) getActivity();
|
|
sa.startPreferencePanel(BatteryHistoryDetail.class.getName(), args,
|
|
R.string.history_details_title, null, null, 0);
|
|
return super.onPreferenceTreeClick(preferenceScreen, preference);
|
|
}
|
|
if (!(preference instanceof PowerGaugePreference)) {
|
|
return false;
|
|
}
|
|
PowerGaugePreference pgp = (PowerGaugePreference) preference;
|
|
BatteryEntry entry = pgp.getInfo();
|
|
PowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(), mStatsHelper,
|
|
mStatsType, entry, true);
|
|
return super.onPreferenceTreeClick(preferenceScreen, preference);
|
|
}
|
|
|
|
@Override
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
if (DEBUG) {
|
|
menu.add(0, MENU_STATS_TYPE, 0, R.string.menu_stats_total)
|
|
.setIcon(com.android.internal.R.drawable.ic_menu_info_details)
|
|
.setAlphabeticShortcut('t');
|
|
}
|
|
MenuItem refresh = menu.add(0, MENU_STATS_REFRESH, 0, R.string.menu_stats_refresh)
|
|
.setIcon(R.drawable.ic_menu_refresh_holo_dark)
|
|
.setAlphabeticShortcut('r');
|
|
refresh.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
|
|
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
|
|
|
|
String helpUrl;
|
|
if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_battery))) {
|
|
final MenuItem help = menu.add(0, MENU_HELP, 0, R.string.help_label);
|
|
HelpUtils.prepareHelpMenuItem(getActivity(), help, helpUrl);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
switch (item.getItemId()) {
|
|
case MENU_STATS_TYPE:
|
|
if (mStatsType == BatteryStats.STATS_SINCE_CHARGED) {
|
|
mStatsType = BatteryStats.STATS_SINCE_UNPLUGGED;
|
|
} else {
|
|
mStatsType = BatteryStats.STATS_SINCE_CHARGED;
|
|
}
|
|
refreshStats();
|
|
return true;
|
|
case MENU_STATS_REFRESH:
|
|
mStatsHelper.clearStats();
|
|
refreshStats();
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void addNotAvailableMessage() {
|
|
Preference notAvailable = new Preference(getActivity());
|
|
notAvailable.setTitle(R.string.power_usage_not_available);
|
|
mAppListGroup.addPreference(notAvailable);
|
|
}
|
|
|
|
private boolean updateBatteryStatus(Intent intent) {
|
|
if (intent != null) {
|
|
String batteryLevel = com.android.settings.Utils.getBatteryPercentage(intent);
|
|
String batteryStatus = com.android.settings.Utils.getBatteryStatus(getResources(),
|
|
intent);
|
|
if (!batteryLevel.equals(mBatteryLevel) || !batteryStatus.equals(mBatteryStatus)) {
|
|
mBatteryLevel = batteryLevel;
|
|
mBatteryStatus = batteryStatus;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void refreshStats() {
|
|
mAppListGroup.removeAll();
|
|
mAppListGroup.setOrderingAsAdded(false);
|
|
|
|
mStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId());
|
|
|
|
BatteryHistoryPreference hist = new BatteryHistoryPreference(
|
|
getActivity(), mStatsHelper.getStats(), mStatsHelper.getBatteryBroadcast());
|
|
hist.setOrder(-1);
|
|
mAppListGroup.addPreference(hist);
|
|
|
|
if (mStatsHelper.getPowerProfile().getAveragePower(
|
|
PowerProfile.POWER_SCREEN_FULL) < 10) {
|
|
addNotAvailableMessage();
|
|
return;
|
|
}
|
|
final int dischargeAmount = mStatsHelper.getStats().getDischargeAmount(mStatsType);
|
|
List<BatterySipper> usageList = mStatsHelper.getUsageList();
|
|
for (int i=0; i<usageList.size(); i++) {
|
|
BatterySipper sipper = usageList.get(i);
|
|
if ((sipper.value*60*60) < MIN_POWER_THRESHOLD) continue;
|
|
final double percentOfTotal =
|
|
((sipper.value / mStatsHelper.getTotalPower()) * dischargeAmount);
|
|
if (((int)(percentOfTotal+.5)) < 1) continue;
|
|
BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
|
|
PowerGaugePreference pref =
|
|
new PowerGaugePreference(getActivity(), entry.getIcon(), entry);
|
|
final double percentOfMax =
|
|
(sipper.value * 100) / mStatsHelper.getMaxPower();
|
|
sipper.percent = percentOfTotal;
|
|
pref.setTitle(entry.getLabel());
|
|
pref.setOrder(i+1);
|
|
pref.setPercent(percentOfMax, percentOfTotal);
|
|
if (sipper.uidObj != null) {
|
|
pref.setKey(Integer.toString(sipper.uidObj.getUid()));
|
|
}
|
|
mAppListGroup.addPreference(pref);
|
|
if (mAppListGroup.getPreferenceCount() > (MAX_ITEMS_TO_LIST+1)) break;
|
|
}
|
|
|
|
BatteryEntry.startRequestQueue();
|
|
}
|
|
|
|
Handler mHandler = new Handler() {
|
|
|
|
@Override
|
|
public void handleMessage(Message msg) {
|
|
switch (msg.what) {
|
|
case BatteryEntry.MSG_UPDATE_NAME_ICON:
|
|
BatteryEntry entry = (BatteryEntry) msg.obj;
|
|
PowerGaugePreference pgp =
|
|
(PowerGaugePreference) findPreference(
|
|
Integer.toString(entry.sipper.uidObj.getUid()));
|
|
if (pgp != null) {
|
|
pgp.setIcon(entry.icon);
|
|
pgp.setTitle(entry.name);
|
|
}
|
|
break;
|
|
case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
|
|
Activity activity = getActivity();
|
|
if (activity != null) {
|
|
activity.reportFullyDrawn();
|
|
}
|
|
break;
|
|
}
|
|
super.handleMessage(msg);
|
|
}
|
|
};
|
|
}
|