BatteryInfo.java revision 5962e18d0e5741511e78102a3746828b05f9f9ea
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.settings; 18 19import android.app.Activity; 20import android.content.BroadcastReceiver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.os.BatteryManager; 25import android.os.Bundle; 26import android.os.Handler; 27import android.os.IPowerManager; 28import android.os.Message; 29import android.os.RemoteException; 30import android.os.ServiceManager; 31import android.os.SystemClock; 32import android.text.format.DateUtils; 33import android.widget.TextView; 34 35import com.android.internal.app.IBatteryStats; 36 37public class BatteryInfo extends Activity { 38 private TextView mStatus; 39 private TextView mLevel; 40 private TextView mScale; 41 private TextView mHealth; 42 private TextView mVoltage; 43 private TextView mTemperature; 44 private TextView mTechnology; 45 private TextView mUptime; 46 private TextView mAwakeBattery; 47 private TextView mAwakePlugged; 48 private TextView mScreenOn; 49 private IBatteryStats mBatteryStats; 50 private IPowerManager mScreenStats; 51 52 private static final int EVENT_TICK = 1; 53 54 private Handler mHandler = new Handler() { 55 @Override 56 public void handleMessage(Message msg) { 57 switch (msg.what) { 58 case EVENT_TICK: 59 updateBatteryStats(); 60 sendEmptyMessageDelayed(EVENT_TICK, 1000); 61 break; 62 } 63 } 64 }; 65 66 /** 67 * Format a number of tenths-units as a decimal string without using a 68 * conversion to float. E.g. 347 -> "34.7" 69 */ 70 private final String tenthsToFixedString(int x) { 71 int tens = x / 10; 72 return new String("" + tens + "." + (x - 10*tens)); 73 } 74 75 /** 76 *Listens for intent broadcasts 77 */ 78 private IntentFilter mIntentFilter; 79 80 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 81 @Override 82 public void onReceive(Context context, Intent intent) { 83 String action = intent.getAction(); 84 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 85 int plugType = intent.getIntExtra("plugged", 0); 86 87 mLevel.setText("" + intent.getIntExtra("level", 0)); 88 mScale.setText("" + intent.getIntExtra("scale", 0)); 89 mVoltage.setText("" + intent.getIntExtra("voltage", 0) + " " 90 + getString(R.string.battery_info_voltage_units)); 91 mTemperature.setText("" + tenthsToFixedString(intent.getIntExtra("temperature", 0)) 92 + getString(R.string.battery_info_temperature_units)); 93 mTechnology.setText("" + intent.getStringExtra("technology")); 94 95 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN); 96 String statusString; 97 if (status == BatteryManager.BATTERY_STATUS_CHARGING) { 98 statusString = getString(R.string.battery_info_status_charging); 99 if (plugType > 0) { 100 statusString = statusString + " " + getString( 101 (plugType == BatteryManager.BATTERY_PLUGGED_AC) 102 ? R.string.battery_info_status_charging_ac 103 : R.string.battery_info_status_charging_usb); 104 } 105 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { 106 statusString = getString(R.string.battery_info_status_discharging); 107 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { 108 statusString = getString(R.string.battery_info_status_not_charging); 109 } else if (status == BatteryManager.BATTERY_STATUS_FULL) { 110 statusString = getString(R.string.battery_info_status_full); 111 } else { 112 statusString = getString(R.string.battery_info_status_unknown); 113 } 114 mStatus.setText(statusString); 115 116 int health = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN); 117 String healthString; 118 if (health == BatteryManager.BATTERY_HEALTH_GOOD) { 119 healthString = getString(R.string.battery_info_health_good); 120 } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) { 121 healthString = getString(R.string.battery_info_health_overheat); 122 } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) { 123 healthString = getString(R.string.battery_info_health_dead); 124 } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) { 125 healthString = getString(R.string.battery_info_health_over_voltage); 126 } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) { 127 healthString = getString(R.string.battery_info_health_unspecified_failure); 128 } else { 129 healthString = getString(R.string.battery_info_health_unknown); 130 } 131 mHealth.setText(healthString); 132 } 133 } 134 }; 135 136 @Override 137 public void onCreate(Bundle icicle) { 138 super.onCreate(icicle); 139 140 setContentView(R.layout.battery_info); 141 142 // create the IntentFilter that will be used to listen 143 // to battery status broadcasts 144 mIntentFilter = new IntentFilter(); 145 mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 146 } 147 148 @Override 149 public void onResume() { 150 super.onResume(); 151 152 mStatus = (TextView)findViewById(R.id.status); 153 mLevel = (TextView)findViewById(R.id.level); 154 mScale = (TextView)findViewById(R.id.scale); 155 mHealth = (TextView)findViewById(R.id.health); 156 mTechnology = (TextView)findViewById(R.id.technology); 157 mVoltage = (TextView)findViewById(R.id.voltage); 158 mTemperature = (TextView)findViewById(R.id.temperature); 159 mUptime = (TextView) findViewById(R.id.uptime); 160 mAwakeBattery = (TextView) findViewById(R.id.awakeBattery); 161 mAwakePlugged = (TextView) findViewById(R.id.awakePlugged); 162 mScreenOn = (TextView) findViewById(R.id.screenOn); 163 164 // Get awake time plugged in and on battery 165 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo")); 166 mScreenStats = IPowerManager.Stub.asInterface(ServiceManager.getService(POWER_SERVICE)); 167 mHandler.sendEmptyMessageDelayed(EVENT_TICK, 1000); 168 169 registerReceiver(mIntentReceiver, mIntentFilter); 170 } 171 172 @Override 173 public void onPause() { 174 super.onPause(); 175 mHandler.removeMessages(EVENT_TICK); 176 177 // we are no longer on the screen stop the observers 178 unregisterReceiver(mIntentReceiver); 179 } 180 181 private void updateBatteryStats() { 182 long uptime = SystemClock.elapsedRealtime(); 183 mUptime.setText(DateUtils.formatElapsedTime(uptime / 1000)); 184 185 if (mBatteryStats != null) { 186 try { 187 long awakeTimeBattery = mBatteryStats.getAwakeTimeBattery() / 1000; 188 long awakeTimePluggedIn = mBatteryStats.getAwakeTimePlugged() / 1000; 189 mAwakeBattery.setText(DateUtils.formatElapsedTime(awakeTimeBattery / 1000) 190 + " (" + (100 * awakeTimeBattery / uptime) + "%)"); 191 mAwakePlugged.setText(DateUtils.formatElapsedTime(awakeTimePluggedIn / 1000) 192 + " (" + (100 * awakeTimePluggedIn / uptime) + "%)"); 193 } catch (RemoteException re) { 194 mAwakeBattery.setText("Unknown"); 195 mAwakePlugged.setText("Unknown"); 196 } 197 } 198 if (mScreenStats != null) { 199 try { 200 long screenOnTime = mScreenStats.getScreenOnTime(); 201 mScreenOn.setText(DateUtils.formatElapsedTime(screenOnTime / 1000)); 202 } catch (RemoteException re) { 203 mScreenOn.setText("Unknown"); 204 } 205 } 206 } 207 208} 209