Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2011 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.settings.net;
     18 
     19 import android.content.Context;
     20 import android.content.pm.ApplicationInfo;
     21 import android.content.pm.PackageInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.PackageManager.NameNotFoundException;
     24 import android.content.res.Resources;
     25 import android.net.ConnectivityManager;
     26 import android.net.TrafficStats;
     27 import android.text.TextUtils;
     28 import android.util.SparseArray;
     29 
     30 import com.android.settings.R;
     31 import com.android.settings.Utils;
     32 
     33 public class UidDetailProvider {
     34     private final Context mContext;
     35     private final SparseArray<UidDetail> mUidDetailCache;
     36 
     37     public UidDetailProvider(Context context) {
     38         mContext = context.getApplicationContext();
     39         mUidDetailCache = new SparseArray<UidDetail>();
     40     }
     41 
     42     public synchronized void clearCache() {
     43         mUidDetailCache.clear();
     44     }
     45 
     46     /**
     47      * Resolve best descriptive label for the given UID.
     48      */
     49     public synchronized UidDetail getUidDetail(int uid, boolean blocking) {
     50         final UidDetail cached = mUidDetailCache.get(uid);
     51         if (cached != null) {
     52             return cached;
     53         } else if (!blocking) {
     54             return null;
     55         }
     56 
     57         final Resources res = mContext.getResources();
     58         final PackageManager pm = mContext.getPackageManager();
     59 
     60         final UidDetail detail = new UidDetail();
     61         detail.label = pm.getNameForUid(uid);
     62         detail.icon = pm.getDefaultActivityIcon();
     63 
     64         // handle special case labels
     65         switch (uid) {
     66             case android.os.Process.SYSTEM_UID:
     67                 detail.label = res.getString(R.string.process_kernel_label);
     68                 detail.icon = pm.getDefaultActivityIcon();
     69                 mUidDetailCache.put(uid, detail);
     70                 return detail;
     71             case TrafficStats.UID_REMOVED:
     72                 detail.label = res.getString(R.string.data_usage_uninstalled_apps);
     73                 detail.icon = pm.getDefaultActivityIcon();
     74                 mUidDetailCache.put(uid, detail);
     75                 return detail;
     76             case TrafficStats.UID_TETHERING:
     77                 final ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(
     78                         Context.CONNECTIVITY_SERVICE);
     79                 detail.label = res.getString(Utils.getTetheringLabel(cm));
     80                 detail.icon = pm.getDefaultActivityIcon();
     81                 mUidDetailCache.put(uid, detail);
     82                 return detail;
     83         }
     84 
     85         // otherwise fall back to using packagemanager labels
     86         final String[] packageNames = pm.getPackagesForUid(uid);
     87         final int length = packageNames != null ? packageNames.length : 0;
     88 
     89         try {
     90             if (length == 1) {
     91                 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
     92                 detail.label = info.loadLabel(pm).toString();
     93                 detail.icon = info.loadIcon(pm);
     94             } else if (length > 1) {
     95                 detail.detailLabels = new CharSequence[length];
     96                 for (int i = 0; i < length; i++) {
     97                     final String packageName = packageNames[i];
     98                     final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
     99                     final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
    100 
    101                     detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
    102                     if (packageInfo.sharedUserLabel != 0) {
    103                         detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
    104                                 packageInfo.applicationInfo).toString();
    105                         detail.icon = appInfo.loadIcon(pm);
    106                     }
    107                 }
    108             }
    109         } catch (NameNotFoundException e) {
    110         }
    111 
    112         if (TextUtils.isEmpty(detail.label)) {
    113             detail.label = Integer.toString(uid);
    114         }
    115 
    116         mUidDetailCache.put(uid, detail);
    117         return detail;
    118     }
    119 }
    120