• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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"
68      */
tenthsToFixedString(int x)69     private final String tenthsToFixedString(int x) {
70         int tens = x / 10;
71         return Integer.toString(tens) + "." + (x - 10 * tens);
72     }
73 
74    /**
75     *Listens for intent broadcasts
76     */
77     private IntentFilter   mIntentFilter;
78 
79     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
80         @Override
81         public void onReceive(Context context, Intent intent) {
82             String action = intent.getAction();
83             if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
84                 int plugType = intent.getIntExtra("plugged", 0);
85 
86                 mLevel.setText("" + intent.getIntExtra("level", 0));
87                 mScale.setText("" + intent.getIntExtra("scale", 0));
88                 mVoltage.setText("" + intent.getIntExtra("voltage", 0) + " "
89                         + getString(R.string.battery_info_voltage_units));
90                 mTemperature.setText("" + tenthsToFixedString(intent.getIntExtra("temperature", 0))
91                         + getString(R.string.battery_info_temperature_units));
92                 mTechnology.setText("" + intent.getStringExtra("technology"));
93 
94                 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
95                 String statusString;
96                 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
97                     statusString = getString(R.string.battery_info_status_charging);
98                     if (plugType > 0) {
99                         statusString = statusString + " " + getString(
100                                 (plugType == BatteryManager.BATTERY_PLUGGED_AC)
101                                         ? R.string.battery_info_status_charging_ac
102                                         : R.string.battery_info_status_charging_usb);
103                     }
104                 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
105                     statusString = getString(R.string.battery_info_status_discharging);
106                 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
107                     statusString = getString(R.string.battery_info_status_not_charging);
108                 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
109                     statusString = getString(R.string.battery_info_status_full);
110                 } else {
111                     statusString = getString(R.string.battery_info_status_unknown);
112                 }
113                 mStatus.setText(statusString);
114 
115                 switch (plugType) {
116                     case 0:
117                         mPower.setText(getString(R.string.battery_info_power_unplugged));
118                         break;
119                     case BatteryManager.BATTERY_PLUGGED_AC:
120                         mPower.setText(getString(R.string.battery_info_power_ac));
121                         break;
122                     case BatteryManager.BATTERY_PLUGGED_USB:
123                         mPower.setText(getString(R.string.battery_info_power_usb));
124                         break;
125                     case (BatteryManager.BATTERY_PLUGGED_AC|BatteryManager.BATTERY_PLUGGED_USB):
126                         mPower.setText(getString(R.string.battery_info_power_ac_usb));
127                         break;
128                     default:
129                         mPower.setText(getString(R.string.battery_info_power_unknown));
130                         break;
131                 }
132 
133                 int health = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
134                 String healthString;
135                 if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
136                     healthString = getString(R.string.battery_info_health_good);
137                 } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
138                     healthString = getString(R.string.battery_info_health_overheat);
139                 } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
140                     healthString = getString(R.string.battery_info_health_dead);
141                 } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
142                     healthString = getString(R.string.battery_info_health_over_voltage);
143                 } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
144                     healthString = getString(R.string.battery_info_health_unspecified_failure);
145                 } else {
146                     healthString = getString(R.string.battery_info_health_unknown);
147                 }
148                 mHealth.setText(healthString);
149             }
150         }
151     };
152 
153     @Override
onCreate(Bundle icicle)154     public void onCreate(Bundle icicle) {
155         super.onCreate(icicle);
156 
157         setContentView(R.layout.battery_info);
158 
159         // create the IntentFilter that will be used to listen
160         // to battery status broadcasts
161         mIntentFilter = new IntentFilter();
162         mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
163     }
164 
165     @Override
onResume()166     public void onResume() {
167         super.onResume();
168 
169         mStatus = (TextView)findViewById(R.id.status);
170         mPower = (TextView)findViewById(R.id.power);
171         mLevel = (TextView)findViewById(R.id.level);
172         mScale = (TextView)findViewById(R.id.scale);
173         mHealth = (TextView)findViewById(R.id.health);
174         mTechnology = (TextView)findViewById(R.id.technology);
175         mVoltage = (TextView)findViewById(R.id.voltage);
176         mTemperature = (TextView)findViewById(R.id.temperature);
177         mUptime = (TextView) findViewById(R.id.uptime);
178 
179         // Get awake time plugged in and on battery
180         mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo"));
181         mScreenStats = IPowerManager.Stub.asInterface(ServiceManager.getService(POWER_SERVICE));
182         mHandler.sendEmptyMessageDelayed(EVENT_TICK, 1000);
183 
184         registerReceiver(mIntentReceiver, mIntentFilter);
185     }
186 
187     @Override
onPause()188     public void onPause() {
189         super.onPause();
190         mHandler.removeMessages(EVENT_TICK);
191 
192         // we are no longer on the screen stop the observers
193         unregisterReceiver(mIntentReceiver);
194     }
195 
updateBatteryStats()196     private void updateBatteryStats() {
197         long uptime = SystemClock.elapsedRealtime();
198         mUptime.setText(DateUtils.formatElapsedTime(uptime / 1000));
199 
200     }
201 
202 }
203