2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.fuelgauge;
18 import com.android.settings.R;
19 import com.android.settings.fuelgauge.PowerUsageDetail.DrainType;
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;
30 import java.util.ArrayList;
31 import java.util.HashMap;
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;
41 int iconId; // For passing to the detail screen.
52 long tcpBytesReceived;
55 double noCoveragePercent;
56 String defaultPackageName;
58 static class UidToDetail {
64 BatterySipper(Context context, ArrayList<BatterySipper> requestQueue,
65 Handler handler, String label, DrainType drainType,
66 int iconId, Uid uid, double[] values) {
68 mRequestQueue = requestQueue;
72 this.drainType = drainType;
74 icon = mContext.getResources().getDrawable(iconId);
76 if (values != null) value = values[0];
77 if ((label == null || iconId == 0) && uid != null) {
78 getQuickNameIconForUid(uid);
83 double getSortValue() {
87 double[] getValues() {
95 public int compareTo(BatterySipper other) {
96 // Return the flipped value because we want the items in descending order
97 return Double.compare(other.getSortValue(), getSortValue());
100 void getQuickNameIconForUid(Uid uidObj) {
101 final int uid = uidObj.getUid();
102 final String uidString = Integer.toString(uid);
103 if (sUidCache.containsKey(uidString)) {
104 UidToDetail utd = sUidCache.get(uidString);
105 defaultPackageName = utd.packageName;
110 PackageManager pm = mContext.getPackageManager();
111 String[] packages = pm.getPackagesForUid(uid);
112 icon = pm.getDefaultActivityIcon();
113 if (packages == null) {
114 //name = Integer.toString(uid);
116 name = mContext.getResources().getString(R.string.process_kernel_label);
117 } else if ("mediaserver".equals(name)) {
118 name = mContext.getResources().getString(R.string.process_mediaserver_label);
120 iconId = R.drawable.ic_power_system;
121 icon = mContext.getResources().getDrawable(iconId);
124 //name = packages[0];
126 synchronized (mRequestQueue) {
127 mRequestQueue.add(this);
133 * @param uid Uid of the application
136 PackageManager pm = mContext.getPackageManager();
137 final int uid = uidObj.getUid();
138 final Drawable defaultActivityIcon = pm.getDefaultActivityIcon();
139 String[] packages = pm.getPackagesForUid(uid);
140 if (packages == null) {
141 name = Integer.toString(uid);
145 String[] packageLabels = new String[packages.length];
146 System.arraycopy(packages, 0, packageLabels, 0, packages.length);
148 int preferredIndex = -1;
149 // Convert package names to user-facing labels where possible
150 for (int i = 0; i < packageLabels.length; i++) {
151 // Check if package matches preferred package
152 if (packageLabels[i].equals(name)) preferredIndex = i;
154 ApplicationInfo ai = pm.getApplicationInfo(packageLabels[i], 0);
155 CharSequence label = ai.loadLabel(pm);
157 packageLabels[i] = label.toString();
160 defaultPackageName = packages[i];
161 icon = ai.loadIcon(pm);
164 } catch (NameNotFoundException e) {
167 if (icon == null) icon = defaultActivityIcon;
169 if (packageLabels.length == 1) {
170 name = packageLabels[0];
172 // Look for an official name for this UID.
173 for (String pkgName : packages) {
175 final PackageInfo pi = pm.getPackageInfo(pkgName, 0);
176 if (pi.sharedUserLabel != 0) {
177 final CharSequence nm = pm.getText(pkgName,
178 pi.sharedUserLabel, pi.applicationInfo);
180 name = nm.toString();
181 if (pi.applicationInfo.icon != 0) {
182 defaultPackageName = pkgName;
183 icon = pi.applicationInfo.loadIcon(pm);
188 } catch (PackageManager.NameNotFoundException e) {
192 final String uidString = Integer.toString(uidObj.getUid());
193 UidToDetail utd = new UidToDetail();
196 utd.packageName = defaultPackageName;
197 sUidCache.put(uidString, utd);
198 mHandler.sendMessage(mHandler.obtainMessage(PowerUsageSummary.MSG_UPDATE_NAME_ICON, this));