Android Open Source - unbounce-android Uid Name Resolver






From Project

Back to project page unbounce-android.

License

The source code is released under:

MIT License

If you think the Android project unbounce-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2011 asksven//from w ww  . j a v  a  2 s . c o m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ryansteckler.nlpunbounce.helpers;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;

/**
 * @author sven
 */
public class UidNameResolver {

    protected String[] m_packages;
    protected String[] m_packageNames;
    private static Context m_context;
    private static UidNameResolver m_instance;

    private UidNameResolver(Context ctx) {
        m_context = ctx;
    }

    public static UidNameResolver getInstance(Context ctx) {
        if (m_instance == null) {
            m_instance = new UidNameResolver(ctx);
        }

        return m_instance;
    }

    public Drawable getIcon(String packageName) {
        Drawable icon = null;
        // retrieve and store the icon for that package
        String myPackage = packageName;
        if (!myPackage.equals("")) {
            PackageManager manager = m_context.getPackageManager();
            try {
                icon = manager.getApplicationIcon(myPackage);
            } catch (Exception e) {
                // nop: no icon found
                icon = null;
            }
        }
        return icon;
    }

    public String getLabel(String packageName) {
        String ret = packageName;
        PackageManager pm = m_context.getPackageManager();
        try {
            ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
            CharSequence label = ai.loadLabel(pm);
            if (label != null) {
                ret = label.toString();
            }
        } catch (NameNotFoundException e) {
            ret = packageName;
        }

        return ret;
    }

    public String getLabelForServices(int uId, String serviceName) {

        PackageManager pm = m_context.getPackageManager();
        String applicationName = null;
        if (uId > 0) {
            String[] packages = pm.getPackagesForUid(uId);
            if (null != packages) {
                for (String packageEntry : packages) {
                    if (serviceName.contains(packageEntry)) {
                        ApplicationInfo ai;
                        try {
                            ai = pm.getApplicationInfo(packageEntry, 0);
                        } catch (final PackageManager.NameNotFoundException e) {
                            ai = null;
                        }
                        applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "Unknown");
                        break;
                    }
                }

                return applicationName;

            } else {
                return "Unknown";
            }
        } else {
            return "Unknown";
        }
    }

    // Side effects: sets mName and mUniqueName
    // Sets mNamePackage, mName and mUniqueName
    public String getNameForUid(int uid) {
        String uidName = "";
        String uidNamePackage = "";
        boolean uidUniqueName = false;

        PackageManager pm = m_context.getPackageManager();
        m_packages = pm.getPackagesForUid(uid);

        if (m_packages == null) {
            uidName = Integer.toString(uid);
            return uidName;
        }

        m_packageNames = new String[m_packages.length];
        System.arraycopy(m_packages, 0, m_packageNames, 0, m_packages.length);

        // Convert package names to user-facing labels where possible
        for (int i = 0; i < m_packageNames.length; i++) {
            m_packageNames[i] = getLabel(m_packageNames[i]);
        }

        if (m_packageNames.length == 1) {
            uidNamePackage = m_packages[0];
            uidName = m_packageNames[0];
            uidUniqueName = true;
        } else {
            uidName = "UID"; // Default name
            // Look for an official name for this UID.
            for (String name : m_packages) {
                try {
                    PackageInfo pi = pm.getPackageInfo(name, 0);
                    if (pi.sharedUserLabel != 0) {
                        CharSequence nm = pm.getText(name,
                                pi.sharedUserLabel, pi.applicationInfo);
                        if (nm != null) {
                            uidName = nm.toString();
                            break;
                        }
                    }
                } catch (NameNotFoundException e) {
                }
            }
        }
        return uidName;
    }

    /**
     * Returns the name for UIDs < 2000
     *
     * @param uid
     * @return
     */
    String getName(int uid) {
        String ret = "";
        switch (uid) {
            case 0:
                ret = "root";
                break;
            case 1000:
                ret = "system";
                break;
            case 1001:
                ret = "radio";
                break;
            case 1002:
                ret = "bluetooth";
                break;
            case 1003:
                ret = "graphics";
                break;
            case 1004:
                ret = "input";
                break;
            case 1005:
                ret = "audio";
                break;
            case 1006:
                ret = "camera";
                break;
            case 1007:
                ret = "log";
                break;
            case 1008:
                ret = "compass";
                break;
            case 1009:
                ret = "mount";
                break;
            case 1010:
                ret = "wifi";
                break;
            case 1011:
                ret = "adb";
                break;
            case 1013:
                ret = "media";
                break;
            case 1014:
                ret = "dhcp";
                break;

        }
        return ret;
    }

}




Java Source Code List

com.example.rsteckler.myapplication.ApplicationTest.java
com.ryansteckler.inappbilling.Base64DecoderException.java
com.ryansteckler.inappbilling.Base64.java
com.ryansteckler.inappbilling.IabException.java
com.ryansteckler.inappbilling.IabHelper.java
com.ryansteckler.inappbilling.IabResult.java
com.ryansteckler.inappbilling.Inventory.java
com.ryansteckler.inappbilling.Purchase.java
com.ryansteckler.inappbilling.Security.java
com.ryansteckler.inappbilling.SkuDetails.java
com.ryansteckler.nlpunbounce.ActivityReceiver.java
com.ryansteckler.nlpunbounce.AlarmDetailFragment.java
com.ryansteckler.nlpunbounce.AlarmsFragment.java
com.ryansteckler.nlpunbounce.BaseDetailFragment.java
com.ryansteckler.nlpunbounce.ExpandingLayout.java
com.ryansteckler.nlpunbounce.HomeFragment.java
com.ryansteckler.nlpunbounce.MaterialSettingsActivity.java
com.ryansteckler.nlpunbounce.NavigationDrawerFragment.java
com.ryansteckler.nlpunbounce.ServiceDetailFragment.java
com.ryansteckler.nlpunbounce.ServicesFragment.java
com.ryansteckler.nlpunbounce.SettingsActivity.java
com.ryansteckler.nlpunbounce.WakelockDetailFragment.java
com.ryansteckler.nlpunbounce.WakelocksFragment.java
com.ryansteckler.nlpunbounce.XposedReceiver.java
com.ryansteckler.nlpunbounce.adapters.AlarmsAdapter.java
com.ryansteckler.nlpunbounce.adapters.BaseAdapter.java
com.ryansteckler.nlpunbounce.adapters.ServicesAdapter.java
com.ryansteckler.nlpunbounce.adapters.WakelocksAdapter.java
com.ryansteckler.nlpunbounce.helpers.DownloadHelper.java
com.ryansteckler.nlpunbounce.helpers.LocaleHelper.java
com.ryansteckler.nlpunbounce.helpers.LogHelper.java
com.ryansteckler.nlpunbounce.helpers.NetworkHelper.java
com.ryansteckler.nlpunbounce.helpers.RootHelper.java
com.ryansteckler.nlpunbounce.helpers.SettingsHelper.java
com.ryansteckler.nlpunbounce.helpers.SortWakeLocks.java
com.ryansteckler.nlpunbounce.helpers.ThemeHelper.java
com.ryansteckler.nlpunbounce.helpers.UidNameResolver.java
com.ryansteckler.nlpunbounce.hooks.Wakelocks.java
com.ryansteckler.nlpunbounce.models.AlarmStats.java
com.ryansteckler.nlpunbounce.models.BaseStatsWrapper.java
com.ryansteckler.nlpunbounce.models.BaseStats.java
com.ryansteckler.nlpunbounce.models.EventLookup.java
com.ryansteckler.nlpunbounce.models.InterimEvent.java
com.ryansteckler.nlpunbounce.models.ServiceStats.java
com.ryansteckler.nlpunbounce.models.UnbounceStatsCollection.java
com.ryansteckler.nlpunbounce.models.WakelockStats.java
com.ryansteckler.nlpunbounce.tasker.TaskerActivity.java
com.ryansteckler.nlpunbounce.tasker.TaskerReceiver.java
com.ryansteckler.nlpunbounce.tasker.TaskerWhichFragment.java