Home | History | Annotate | Download | only in settings
      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.BatteryStats;
     26 import android.os.Bundle;
     27 import android.os.Handler;
     28 import android.os.IPowerManager;
     29 import android.os.Message;
     30 import android.os.ServiceManager;
     31 import android.os.SystemClock;
     32 import android.text.format.DateUtils;
     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 mPower;
     40     private TextView mLevel;
     41     private TextView mScale;
     42     private TextView mHealth;
     43     private TextView mVoltage;
     44     private TextView mTemperature;
     45     private TextView mTechnology;
     46     private TextView mUptime;
     47     private IBatteryStats mBatteryStats;
     48     private IPowerManager mScreenStats;
     49 
     50     private static final int EVENT_TICK = 1;
     51 
     52     private Handler mHandler = new Handler() {
     53         @Override
     54         public void handleMessage(Message msg) {
     55             switch (msg.what) {
     56                 case EVENT_TICK:
     57                     updateBatteryStats();
     58                     sendEmptyMessageDelayed(EVENT_TICK, 1000);
     59 
     60                     break;
     61             }
     62         }
     63     };
     64 
     65     /**
     66      * Format a number of tenths-units as a decimal string without using a
     67      * conversion to float.  E.g. 347 -> "34.7", -99 -> "-9.9"
     68      */
     69     private final String tenthsToFixedString(int x) {
     70         int tens = x / 10;
     71         // use Math.abs to avoid "-9.-9" about -99
     72         return Integer.toString(tens) + "." + Math.abs(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                 mStatus.setText(Utils.getBatteryStatus(getResources(), intent));
     96 
     97                 switch (plugType) {
     98                     case 0:
     99                         mPower.setText(getString(R.string.battery_info_power_unplugged));
    100                         break;
    101                     case BatteryManager.BATTERY_PLUGGED_AC:
    102                         mPower.setText(getString(R.string.battery_info_power_ac));
    103                         break;
    104                     case BatteryManager.BATTERY_PLUGGED_USB:
    105                         mPower.setText(getString(R.string.battery_info_power_usb));
    106                         break;
    107                     case BatteryManager.BATTERY_PLUGGED_WIRELESS:
    108                         mPower.setText(getString(R.string.battery_info_power_wireless));
    109                         break;
    110                     case (BatteryManager.BATTERY_PLUGGED_AC|BatteryManager.BATTERY_PLUGGED_USB):
    111                         mPower.setText(getString(R.string.battery_info_power_ac_usb));
    112                         break;
    113                     default:
    114                         mPower.setText(getString(R.string.battery_info_power_unknown));
    115                         break;
    116                 }
    117 
    118                 int health = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
    119                 String healthString;
    120                 if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
    121                     healthString = getString(R.string.battery_info_health_good);
    122                 } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
    123                     healthString = getString(R.string.battery_info_health_overheat);
    124                 } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
    125                     healthString = getString(R.string.battery_info_health_dead);
    126                 } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
    127                     healthString = getString(R.string.battery_info_health_over_voltage);
    128                 } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
    129                     healthString = getString(R.string.battery_info_health_unspecified_failure);
    130                 } else if (health == BatteryManager.BATTERY_HEALTH_COLD) {
    131                     healthString = getString(R.string.battery_info_health_cold);
    132                 } else {
    133                     healthString = getString(R.string.battery_info_health_unknown);
    134                 }
    135                 mHealth.setText(healthString);
    136             }
    137         }
    138     };
    139 
    140     @Override
    141     public void onCreate(Bundle icicle) {
    142         super.onCreate(icicle);
    143 
    144         setContentView(R.layout.battery_info);
    145 
    146         // create the IntentFilter that will be used to listen
    147         // to battery status broadcasts
    148         mIntentFilter = new IntentFilter();
    149         mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
    150     }
    151 
    152     @Override
    153     public void onResume() {
    154         super.onResume();
    155 
    156         mStatus = (TextView)findViewById(R.id.status);
    157         mPower = (TextView)findViewById(R.id.power);
    158         mLevel = (TextView)findViewById(R.id.level);
    159         mScale = (TextView)findViewById(R.id.scale);
    160         mHealth = (TextView)findViewById(R.id.health);
    161         mTechnology = (TextView)findViewById(R.id.technology);
    162         mVoltage = (TextView)findViewById(R.id.voltage);
    163         mTemperature = (TextView)findViewById(R.id.temperature);
    164         mUptime = (TextView) findViewById(R.id.uptime);
    165 
    166         // Get awake time plugged in and on battery
    167         mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
    168                 BatteryStats.SERVICE_NAME));
    169         mScreenStats = IPowerManager.Stub.asInterface(ServiceManager.getService(POWER_SERVICE));
    170         mHandler.sendEmptyMessageDelayed(EVENT_TICK, 1000);
    171 
    172         registerReceiver(mIntentReceiver, mIntentFilter);
    173     }
    174 
    175     @Override
    176     public void onPause() {
    177         super.onPause();
    178         mHandler.removeMessages(EVENT_TICK);
    179 
    180         // we are no longer on the screen stop the observers
    181         unregisterReceiver(mIntentReceiver);
    182     }
    183 
    184     private void updateBatteryStats() {
    185         long uptime = SystemClock.elapsedRealtime();
    186         mUptime.setText(DateUtils.formatElapsedTime(uptime / 1000));
    187     }
    188 
    189 }
    190