The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.os.BatteryManager; |
| 25 | import android.os.Bundle; |
| 26 | import android.os.Handler; |
| 27 | import android.os.IPowerManager; |
| 28 | import android.os.Message; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
| 31 | import android.os.SystemClock; |
The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 32 | import android.text.format.DateUtils; |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
| 35 | import com.android.internal.app.IBatteryStats; |
| 36 | |
| 37 | public 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 { |
The Android Open Source Project | 5962e18 | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 187 | long awakeTimeBattery = mBatteryStats.getAwakeTimeBattery() / 1000; |
| 188 | long awakeTimePluggedIn = mBatteryStats.getAwakeTimePlugged() / 1000; |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | } |