PowerProfile.java revision 244fa5c05b2cc8c4c0754aeed4ee42c588ea89d1
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
17package com.android.internal.os;
18
19
20import android.content.Context;
21import android.content.res.XmlResourceParser;
22
23import com.android.internal.util.XmlUtils;
24
25import org.xmlpull.v1.XmlPullParser;
26import org.xmlpull.v1.XmlPullParserException;
27
28import java.io.IOException;
29import java.util.HashMap;
30
31/**
32 * Reports power consumption values for various device activities. Reads values from an XML file.
33 * Customize the XML file for different devices.
34 * [hidden]
35 */
36public class PowerProfile {
37
38    /**
39     * No power consumption, or accounted for elsewhere.
40     */
41    public static final String POWER_NONE = "none";
42
43    /**
44     * Power consumption when CPU is in power collapse mode.
45     */
46    public static final String POWER_CPU_IDLE = "cpu.idle";
47
48    /**
49     * Power consumption when CPU is running at normal speed.
50     */
51    public static final String POWER_CPU_NORMAL = "cpu.normal";
52
53    /**
54     * Power consumption when CPU is running at full speed.
55     */
56    public static final String POWER_CPU_FULL = "cpu.full";
57
58    /**
59     * Power consumption when WiFi driver is scanning for networks.
60     */
61    public static final String POWER_WIFI_SCAN = "wifi.scan";
62
63    /**
64     * Power consumption when WiFi driver is on.
65     */
66    public static final String POWER_WIFI_ON = "wifi.on";
67
68    /**
69     * Power consumption when WiFi driver is transmitting/receiving.
70     */
71    public static final String POWER_WIFI_ACTIVE = "wifi.active";
72
73    /**
74     * Power consumption when GPS is on.
75     */
76    public static final String POWER_GPS_ON = "gps.on";
77
78    /**
79     * Power consumption when Bluetooth driver is on.
80     */
81    public static final String POWER_BLUETOOTH_ON = "bluetooth.on";
82
83    /**
84     * Power consumption when Bluetooth driver is transmitting/receiving.
85     */
86    public static final String POWER_BLUETOOTH_ACTIVE = "bluetooth.active";
87
88    /**
89     * Power consumption when screen is on, not including the backlight power.
90     */
91    public static final String POWER_SCREEN_ON = "screen.on";
92
93    /**
94     * Power consumption when cell radio is on but not on a call.
95     */
96    public static final String POWER_RADIO_ON = "radio.on";
97
98    /**
99     * Power consumption when talking on the phone.
100     */
101    public static final String POWER_RADIO_ACTIVE = "radio.active";
102
103    /**
104     * Power consumption at full backlight brightness. If the backlight is at
105     * 50% brightness, then this should be multiplied by 0.5
106     */
107    public static final String POWER_SCREEN_FULL = "screen.full";
108
109    /**
110     * Power consumed by the audio hardware when playing back audio content. This is in addition
111     * to the CPU power, probably due to a DSP and / or amplifier.
112     */
113    public static final String POWER_AUDIO = "dsp.audio";
114
115    /**
116     * Power consumed by any media hardware when playing back video content. This is in addition
117     * to the CPU power, probably due to a DSP.
118     */
119    public static final String POWER_VIDEO = "dsp.video";
120
121    static final HashMap<String, Double> sPowerMap = new HashMap<String, Double>();
122
123    private static final String TAG_DEVICE = "device";
124    private static final String TAG_ITEM = "item";
125    private static final String ATTR_NAME = "name";
126
127    public PowerProfile(Context context, CharSequence profile) {
128        // Read the XML file for the given profile (normally only one per
129        // device)
130        if (sPowerMap.size() == 0) {
131            readPowerValuesFromXml(context, profile);
132        }
133    }
134
135    private void readPowerValuesFromXml(Context context, CharSequence profile) {
136        // FIXME
137        //int id = context.getResources().getIdentifier(profile.toString(), "xml",
138        //        "com.android.internal");
139        int id = com.android.internal.R.xml.power_profile_default;
140        XmlResourceParser parser = context.getResources().getXml(id);
141
142        try {
143            XmlUtils.beginDocument(parser, TAG_DEVICE);
144
145            while (true) {
146                XmlUtils.nextElement(parser);
147
148                String element = parser.getName();
149                if (element == null || !(element.equals(TAG_ITEM))) {
150                    break;
151                }
152
153                String name = parser.getAttributeValue(null, ATTR_NAME);
154                if (parser.next() == XmlPullParser.TEXT) {
155                    String power = parser.getText();
156                    double value = 0;
157                    try {
158                        value = Double.valueOf(power);
159                    } catch (NumberFormatException nfe) {
160                    }
161                    sPowerMap.put(name, value);
162                }
163            }
164        } catch (XmlPullParserException e) {
165            throw new RuntimeException(e);
166        } catch (IOException e) {
167            throw new RuntimeException(e);
168        } finally {
169            parser.close();
170        }
171    }
172
173    /**
174     * Returns the average current in mA consumed by the subsystem
175     * @param type the subsystem type
176     * @return the average current in milliAmps.
177     */
178    public double getAveragePower(String type) {
179        if (sPowerMap.containsKey(type)) {
180            return sPowerMap.get(type);
181        } else {
182            return 0;
183        }
184    }
185}
186