OSDN Git Service

Follow network stats changes.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / BatterySipper.java
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 package com.android.settings.fuelgauge;
17
18 import com.android.settings.R;
19 import com.android.settings.fuelgauge.PowerUsageDetail.DrainType;
20
21 import android.content.Context;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.graphics.drawable.Drawable;
27 import android.os.Handler;
28 import android.os.BatteryStats.Uid;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32
33 class BatterySipper implements Comparable<BatterySipper> {
34     final Context mContext;
35     /* Cache cleared when PowerUsageSummary is destroyed */
36     static final HashMap<String,UidToDetail> sUidCache = new HashMap<String,UidToDetail>();
37     final ArrayList<BatterySipper> mRequestQueue;
38     final Handler mHandler;
39     String name;
40     Drawable icon;
41     int iconId; // For passing to the detail screen.
42     Uid uidObj;
43     double value;
44     double[] values;
45     DrainType drainType;
46     long usageTime;
47     long cpuTime;
48     long gpsTime;
49     long wifiRunningTime;
50     long cpuFgTime;
51     long wakeLockTime;
52     long mobileRxBytes;
53     long mobileTxBytes;
54     long wifiRxBytes;
55     long wifiTxBytes;
56     double percent;
57     double noCoveragePercent;
58     String defaultPackageName;
59
60     static class UidToDetail {
61         String name;
62         String packageName;
63         Drawable icon;
64     }
65
66     BatterySipper(Context context, ArrayList<BatterySipper> requestQueue,
67             Handler handler, String label, DrainType drainType,
68             int iconId, Uid uid, double[] values) {
69         mContext = context;
70         mRequestQueue = requestQueue;
71         mHandler = handler;
72         this.values = values;
73         name = label;
74         this.drainType = drainType;
75         if (iconId > 0) {
76             icon = mContext.getResources().getDrawable(iconId);
77         }
78         if (values != null) value = values[0];
79         if ((label == null || iconId == 0) && uid != null) {
80             getQuickNameIconForUid(uid);
81         }
82         uidObj = uid;
83     }
84
85     double getSortValue() {
86         return value;
87     }
88
89     double[] getValues() {
90         return values;
91     }
92
93     Drawable getIcon() {
94         return icon;
95     }
96
97     public int compareTo(BatterySipper other) {
98         // Return the flipped value because we want the items in descending order
99         return Double.compare(other.getSortValue(), getSortValue());
100     }
101
102     void getQuickNameIconForUid(Uid uidObj) {
103         final int uid = uidObj.getUid();
104         final String uidString = Integer.toString(uid);
105         if (sUidCache.containsKey(uidString)) {
106             UidToDetail utd = sUidCache.get(uidString);
107             defaultPackageName = utd.packageName;
108             name = utd.name;
109             icon = utd.icon;
110             return;
111         }
112         PackageManager pm = mContext.getPackageManager();
113         String[] packages = pm.getPackagesForUid(uid);
114         icon = pm.getDefaultActivityIcon();
115         if (packages == null) {
116             //name = Integer.toString(uid);
117             if (uid == 0) {
118                 name = mContext.getResources().getString(R.string.process_kernel_label);
119             } else if ("mediaserver".equals(name)) {
120                 name = mContext.getResources().getString(R.string.process_mediaserver_label);
121             }
122             iconId = R.drawable.ic_power_system;
123             icon = mContext.getResources().getDrawable(iconId);
124             return;
125         } else {
126             //name = packages[0];
127         }
128         synchronized (mRequestQueue) {
129             mRequestQueue.add(this);
130         }
131     }
132
133     /**
134      * Sets name and icon
135      * @param uid Uid of the application
136      */
137     void getNameIcon() {
138         PackageManager pm = mContext.getPackageManager();
139         final int uid = uidObj.getUid();
140         final Drawable defaultActivityIcon = pm.getDefaultActivityIcon();
141         String[] packages = pm.getPackagesForUid(uid);
142         if (packages == null) {
143             name = Integer.toString(uid);
144             return;
145         }
146
147         String[] packageLabels = new String[packages.length];
148         System.arraycopy(packages, 0, packageLabels, 0, packages.length);
149
150         int preferredIndex = -1;
151         // Convert package names to user-facing labels where possible
152         for (int i = 0; i < packageLabels.length; i++) {
153             // Check if package matches preferred package
154             if (packageLabels[i].equals(name)) preferredIndex = i;
155             try {
156                 ApplicationInfo ai = pm.getApplicationInfo(packageLabels[i], 0);
157                 CharSequence label = ai.loadLabel(pm);
158                 if (label != null) {
159                     packageLabels[i] = label.toString();
160                 }
161                 if (ai.icon != 0) {
162                     defaultPackageName = packages[i];
163                     icon = ai.loadIcon(pm);
164                     break;
165                 }
166             } catch (NameNotFoundException e) {
167             }
168         }
169         if (icon == null) icon = defaultActivityIcon;
170
171         if (packageLabels.length == 1) {
172             name = packageLabels[0];
173         } else {
174             // Look for an official name for this UID.
175             for (String pkgName : packages) {
176                 try {
177                     final PackageInfo pi = pm.getPackageInfo(pkgName, 0);
178                     if (pi.sharedUserLabel != 0) {
179                         final CharSequence nm = pm.getText(pkgName,
180                                 pi.sharedUserLabel, pi.applicationInfo);
181                         if (nm != null) {
182                             name = nm.toString();
183                             if (pi.applicationInfo.icon != 0) {
184                                 defaultPackageName = pkgName;
185                                 icon = pi.applicationInfo.loadIcon(pm);
186                             }
187                             break;
188                         }
189                     }
190                 } catch (PackageManager.NameNotFoundException e) {
191                 }
192             }
193         }
194         final String uidString = Integer.toString(uidObj.getUid());
195         UidToDetail utd = new UidToDetail();
196         utd.name = name;
197         utd.icon = icon;
198         utd.packageName = defaultPackageName;
199         sUidCache.put(uidString, utd);
200         mHandler.sendMessage(mHandler.obtainMessage(PowerUsageSummary.MSG_UPDATE_NAME_ICON, this));
201     }
202 }