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.ServiceManager; 30 import android.os.SystemClock; 31 import android.text.format.DateUtils; 32 import android.widget.TextView; 33 34 import com.android.internal.app.IBatteryStats; 35 36 public class BatteryInfo extends Activity { 37 private TextView mStatus; 38 private TextView mPower; 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 IBatteryStats mBatteryStats; 47 private IPowerManager mScreenStats; 48 49 private static final int EVENT_TICK = 1; 50 51 private Handler mHandler = new Handler() { 52 @Override 53 public void handleMessage(Message msg) { 54 switch (msg.what) { 55 case EVENT_TICK: 56 updateBatteryStats(); 57 sendEmptyMessageDelayed(EVENT_TICK, 1000); 58 59 break; 60 } 61 } 62 }; 63 64 /** 65 * Format a number of tenths-units as a decimal string without using a 66 * conversion to float. E.g. 347 -> "34.7" 67 */ tenthsToFixedString(int x)68 private final String tenthsToFixedString(int x) { 69 int tens = x / 10; 70 return Integer.toString(tens) + "." + (x - 10 * tens); 71 } 72 73 /** 74 *Listens for intent broadcasts 75 */ 76 private IntentFilter mIntentFilter; 77 78 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 79 @Override 80 public void onReceive(Context context, Intent intent) { 81 String action = intent.getAction(); 82 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 83 int plugType = intent.getIntExtra("plugged", 0); 84 85 mLevel.setText("" + intent.getIntExtra("level", 0)); 86 mScale.setText("" + intent.getIntExtra("scale", 0)); 87 mVoltage.setText("" + intent.getIntExtra("voltage", 0) + " " 88 + getString(R.string.battery_info_voltage_units)); 89 mTemperature.setText("" + tenthsToFixedString(intent.getIntExtra("temperature", 0)) 90 + getString(R.string.battery_info_temperature_units)); 91 mTechnology.setText("" + intent.getStringExtra("technology")); 92 93 mStatus.setText(Utils.getBatteryStatus(getResources(), intent)); 94 95 switch (plugType) { 96 case 0: 97 mPower.setText(getString(R.string.battery_info_power_unplugged)); 98 break; 99 case BatteryManager.BATTERY_PLUGGED_AC: 100 mPower.setText(getString(R.string.battery_info_power_ac)); 101 break; 102 case BatteryManager.BATTERY_PLUGGED_USB: 103 mPower.setText(getString(R.string.battery_info_power_usb)); 104 break; 105 case (BatteryManager.BATTERY_PLUGGED_AC|BatteryManager.BATTERY_PLUGGED_USB): 106 mPower.setText(getString(R.string.battery_info_power_ac_usb)); 107 break; 108 default: 109 mPower.setText(getString(R.string.battery_info_power_unknown)); 110 break; 111 } 112 113 int health = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN); 114 String healthString; 115 if (health == BatteryManager.BATTERY_HEALTH_GOOD) { 116 healthString = getString(R.string.battery_info_health_good); 117 } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) { 118 healthString = getString(R.string.battery_info_health_overheat); 119 } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) { 120 healthString = getString(R.string.battery_info_health_dead); 121 } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) { 122 healthString = getString(R.string.battery_info_health_over_voltage); 123 } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) { 124 healthString = getString(R.string.battery_info_health_unspecified_failure); 125 } else if (health == BatteryManager.BATTERY_HEALTH_COLD) { 126 healthString = getString(R.string.battery_info_health_cold); 127 } else { 128 healthString = getString(R.string.battery_info_health_unknown); 129 } 130 mHealth.setText(healthString); 131 } 132 } 133 }; 134 135 @Override onCreate(Bundle icicle)136 public void onCreate(Bundle icicle) { 137 super.onCreate(icicle); 138 139 setContentView(R.layout.battery_info); 140 141 // create the IntentFilter that will be used to listen 142 // to battery status broadcasts 143 mIntentFilter = new IntentFilter(); 144 mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 145 } 146 147 @Override onResume()148 public void onResume() { 149 super.onResume(); 150 151 mStatus = (TextView)findViewById(R.id.status); 152 mPower = (TextView)findViewById(R.id.power); 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 161 // Get awake time plugged in and on battery 162 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo")); 163 mScreenStats = IPowerManager.Stub.asInterface(ServiceManager.getService(POWER_SERVICE)); 164 mHandler.sendEmptyMessageDelayed(EVENT_TICK, 1000); 165 166 registerReceiver(mIntentReceiver, mIntentFilter); 167 } 168 169 @Override onPause()170 public void onPause() { 171 super.onPause(); 172 mHandler.removeMessages(EVENT_TICK); 173 174 // we are no longer on the screen stop the observers 175 unregisterReceiver(mIntentReceiver); 176 } 177 updateBatteryStats()178 private void updateBatteryStats() { 179 long uptime = SystemClock.elapsedRealtime(); 180 mUptime.setText(DateUtils.formatElapsedTime(uptime / 1000)); 181 } 182 183 } 184