Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2009 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.internal.os;
     18 
     19 
     20 import android.content.Context;
     21 import android.content.res.XmlResourceParser;
     22 
     23 import com.android.internal.util.XmlUtils;
     24 
     25 import org.xmlpull.v1.XmlPullParser;
     26 import org.xmlpull.v1.XmlPullParserException;
     27 
     28 import java.io.IOException;
     29 import java.util.ArrayList;
     30 import java.util.HashMap;
     31 
     32 /**
     33  * Reports power consumption values for various device activities. Reads values from an XML file.
     34  * Customize the XML file for different devices.
     35  * [hidden]
     36  */
     37 public class PowerProfile {
     38 
     39     /**
     40      * No power consumption, or accounted for elsewhere.
     41      */
     42     public static final String POWER_NONE = "none";
     43 
     44     /**
     45      * Power consumption when CPU is in power collapse mode.
     46      */
     47     public static final String POWER_CPU_IDLE = "cpu.idle";
     48 
     49     /**
     50      * Power consumption when CPU is awake (when a wake lock is held).  This
     51      * should be 0 on devices that can go into full CPU power collapse even
     52      * when a wake lock is held.  Otherwise, this is the power consumption in
     53      * addition to POWERR_CPU_IDLE due to a wake lock being held but with no
     54      * CPU activity.
     55      */
     56     public static final String POWER_CPU_AWAKE = "cpu.awake";
     57 
     58     /**
     59      * Power consumption when CPU is in power collapse mode.
     60      */
     61     public static final String POWER_CPU_ACTIVE = "cpu.active";
     62 
     63     /**
     64      * Power consumption when WiFi driver is scanning for networks.
     65      */
     66     public static final String POWER_WIFI_SCAN = "wifi.scan";
     67 
     68     /**
     69      * Power consumption when WiFi driver is on.
     70      */
     71     public static final String POWER_WIFI_ON = "wifi.on";
     72 
     73     /**
     74      * Power consumption when WiFi driver is transmitting/receiving.
     75      */
     76     public static final String POWER_WIFI_ACTIVE = "wifi.active";
     77 
     78     /**
     79      * Power consumption when GPS is on.
     80      */
     81     public static final String POWER_GPS_ON = "gps.on";
     82 
     83     /**
     84      * Power consumption when Bluetooth driver is on.
     85      */
     86     public static final String POWER_BLUETOOTH_ON = "bluetooth.on";
     87 
     88     /**
     89      * Power consumption when Bluetooth driver is transmitting/receiving.
     90      */
     91     public static final String POWER_BLUETOOTH_ACTIVE = "bluetooth.active";
     92 
     93     /**
     94      * Power consumption when Bluetooth driver gets an AT command.
     95      */
     96     public static final String POWER_BLUETOOTH_AT_CMD = "bluetooth.at";
     97 
     98     /**
     99      * Power consumption when screen is on, not including the backlight power.
    100      */
    101     public static final String POWER_SCREEN_ON = "screen.on";
    102 
    103     /**
    104      * Power consumption when cell radio is on but not on a call.
    105      */
    106     public static final String POWER_RADIO_ON = "radio.on";
    107 
    108     /**
    109      * Power consumption when cell radio is hunting for a signal.
    110      */
    111     public static final String POWER_RADIO_SCANNING = "radio.scanning";
    112 
    113     /**
    114      * Power consumption when talking on the phone.
    115      */
    116     public static final String POWER_RADIO_ACTIVE = "radio.active";
    117 
    118     /**
    119      * Power consumption at full backlight brightness. If the backlight is at
    120      * 50% brightness, then this should be multiplied by 0.5
    121      */
    122     public static final String POWER_SCREEN_FULL = "screen.full";
    123 
    124     /**
    125      * Power consumed by the audio hardware when playing back audio content. This is in addition
    126      * to the CPU power, probably due to a DSP and / or amplifier.
    127      */
    128     public static final String POWER_AUDIO = "dsp.audio";
    129 
    130     /**
    131      * Power consumed by any media hardware when playing back video content. This is in addition
    132      * to the CPU power, probably due to a DSP.
    133      */
    134     public static final String POWER_VIDEO = "dsp.video";
    135 
    136     public static final String POWER_CPU_SPEEDS = "cpu.speeds";
    137 
    138     /**
    139      * Power consumed by wif batched scaning.  Broken down into bins by
    140      * Channels Scanned per Hour.  May do 1-720 scans per hour of 1-100 channels
    141      * for a range of 1-72,000.  Going logrithmic (1-8, 9-64, 65-512, 513-4096, 4097-)!
    142      */
    143     public static final String POWER_WIFI_BATCHED_SCAN = "wifi.batchedscan";
    144 
    145     /**
    146      * Battery capacity in milliAmpHour (mAh).
    147      */
    148     public static final String POWER_BATTERY_CAPACITY = "battery.capacity";
    149 
    150     static final HashMap<String, Object> sPowerMap = new HashMap<String, Object>();
    151 
    152     private static final String TAG_DEVICE = "device";
    153     private static final String TAG_ITEM = "item";
    154     private static final String TAG_ARRAY = "array";
    155     private static final String TAG_ARRAYITEM = "value";
    156     private static final String ATTR_NAME = "name";
    157 
    158     public PowerProfile(Context context) {
    159         // Read the XML file for the given profile (normally only one per
    160         // device)
    161         if (sPowerMap.size() == 0) {
    162             readPowerValuesFromXml(context);
    163         }
    164     }
    165 
    166     private void readPowerValuesFromXml(Context context) {
    167         int id = com.android.internal.R.xml.power_profile;
    168         XmlResourceParser parser = context.getResources().getXml(id);
    169         boolean parsingArray = false;
    170         ArrayList<Double> array = new ArrayList<Double>();
    171         String arrayName = null;
    172 
    173         try {
    174             XmlUtils.beginDocument(parser, TAG_DEVICE);
    175 
    176             while (true) {
    177                 XmlUtils.nextElement(parser);
    178 
    179                 String element = parser.getName();
    180                 if (element == null) break;
    181 
    182                 if (parsingArray && !element.equals(TAG_ARRAYITEM)) {
    183                     // Finish array
    184                     sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
    185                     parsingArray = false;
    186                 }
    187                 if (element.equals(TAG_ARRAY)) {
    188                     parsingArray = true;
    189                     array.clear();
    190                     arrayName = parser.getAttributeValue(null, ATTR_NAME);
    191                 } else if (element.equals(TAG_ITEM) || element.equals(TAG_ARRAYITEM)) {
    192                     String name = null;
    193                     if (!parsingArray) name = parser.getAttributeValue(null, ATTR_NAME);
    194                     if (parser.next() == XmlPullParser.TEXT) {
    195                         String power = parser.getText();
    196                         double value = 0;
    197                         try {
    198                             value = Double.valueOf(power);
    199                         } catch (NumberFormatException nfe) {
    200                         }
    201                         if (element.equals(TAG_ITEM)) {
    202                             sPowerMap.put(name, value);
    203                         } else if (parsingArray) {
    204                             array.add(value);
    205                         }
    206                     }
    207                 }
    208             }
    209             if (parsingArray) {
    210                 sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
    211             }
    212         } catch (XmlPullParserException e) {
    213             throw new RuntimeException(e);
    214         } catch (IOException e) {
    215             throw new RuntimeException(e);
    216         } finally {
    217             parser.close();
    218         }
    219     }
    220 
    221     /**
    222      * Returns the average current in mA consumed by the subsystem
    223      * @param type the subsystem type
    224      * @return the average current in milliAmps.
    225      */
    226     public double getAveragePower(String type) {
    227         if (sPowerMap.containsKey(type)) {
    228             Object data = sPowerMap.get(type);
    229             if (data instanceof Double[]) {
    230                 return ((Double[])data)[0];
    231             } else {
    232                 return (Double) sPowerMap.get(type);
    233             }
    234         } else {
    235             return 0;
    236         }
    237     }
    238 
    239     /**
    240      * Returns the average current in mA consumed by the subsystem for the given level.
    241      * @param type the subsystem type
    242      * @param level the level of power at which the subsystem is running. For instance, the
    243      *  signal strength of the cell network between 0 and 4 (if there are 4 bars max.)
    244      *  If there is no data for multiple levels, the level is ignored.
    245      * @return the average current in milliAmps.
    246      */
    247     public double getAveragePower(String type, int level) {
    248         if (sPowerMap.containsKey(type)) {
    249             Object data = sPowerMap.get(type);
    250             if (data instanceof Double[]) {
    251                 final Double[] values = (Double[]) data;
    252                 if (values.length > level && level >= 0) {
    253                     return values[level];
    254                 } else if (level < 0) {
    255                     return 0;
    256                 } else {
    257                     return values[values.length - 1];
    258                 }
    259             } else {
    260                 return (Double) data;
    261             }
    262         } else {
    263             return 0;
    264         }
    265     }
    266 
    267     /**
    268      * Returns the battery capacity, if available, in milli Amp Hours. If not available,
    269      * it returns zero.
    270      * @return the battery capacity in mAh
    271      */
    272     public double getBatteryCapacity() {
    273         return getAveragePower(POWER_BATTERY_CAPACITY);
    274     }
    275 
    276     /**
    277      * Returns the number of speeds that the CPU can be run at.
    278      * @return
    279      */
    280     public int getNumSpeedSteps() {
    281         Object value = sPowerMap.get(POWER_CPU_SPEEDS);
    282         if (value != null && value instanceof Double[]) {
    283             return ((Double[])value).length;
    284         }
    285         return 1; // Only one speed
    286     }
    287 }
    288