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.
57 double noCoveragePercent;
58 String defaultPackageName;
60 static class UidToDetail {
66 BatterySipper(Context context, ArrayList<BatterySipper> requestQueue,
67 Handler handler, String label, DrainType drainType,
68 int iconId, Uid uid, double[] values) {
70 mRequestQueue = requestQueue;
74 this.drainType = drainType;
76 icon = mContext.getResources().getDrawable(iconId);
78 if (values != null) value = values[0];
79 if ((label == null || iconId == 0) && uid != null) {
80 getQuickNameIconForUid(uid);
85 double getSortValue() {
89 double[] getValues() {
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());
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;
112 PackageManager pm = mContext.getPackageManager();
113 String[] packages = pm.getPackagesForUid(uid);
114 icon = pm.getDefaultActivityIcon();
115 if (packages == null) {
116 //name = Integer.toString(uid);
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);
122 iconId = R.drawable.ic_power_system;
123 icon = mContext.getResources().getDrawable(iconId);
126 //name = packages[0];
128 synchronized (mRequestQueue) {
129 mRequestQueue.add(this);
135 * @param uid Uid of the application
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);
147 String[] packageLabels = new String[packages.length];
148 System.arraycopy(packages, 0, packageLabels, 0, packages.length);
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;
156 ApplicationInfo ai = pm.getApplicationInfo(packageLabels[i], 0);
157 CharSequence label = ai.loadLabel(pm);
159 packageLabels[i] = label.toString();
162 defaultPackageName = packages[i];
163 icon = ai.loadIcon(pm);
166 } catch (NameNotFoundException e) {
169 if (icon == null) icon = defaultActivityIcon;
171 if (packageLabels.length == 1) {
172 name = packageLabels[0];
174 // Look for an official name for this UID.
175 for (String pkgName : packages) {
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);
182 name = nm.toString();
183 if (pi.applicationInfo.icon != 0) {
184 defaultPackageName = pkgName;
185 icon = pi.applicationInfo.loadIcon(pm);
190 } catch (PackageManager.NameNotFoundException e) {
194 final String uidString = Integer.toString(uidObj.getUid());
195 UidToDetail utd = new UidToDetail();
198 utd.packageName = defaultPackageName;
199 sUidCache.put(uidString, utd);
200 mHandler.sendMessage(mHandler.obtainMessage(PowerUsageSummary.MSG_UPDATE_NAME_ICON, this));