get Installed App Names As App ListView Item List - Android User Interface

Android examples for User Interface:ListView

Description

get Installed App Names As App ListView Item List

Demo Code


import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;

public class Main{
    private static Activity mContext;
    public static ArrayList<AppListViewItem> getInstalledAppNamesAsAppListViewItemList(
            boolean onlySupportedApps) {
        List<ApplicationInfo> isa = getInstalledApps(onlySupportedApps);
        if (isa == null)
            return null;
        ArrayList<AppListViewItem> res = new ArrayList<AppListViewItem>();
        for (int i = 0; i < isa.size(); i++) {
            AppListViewItem alvi = new AppListViewItem(isa.get(i));
            res.add(alvi);/*from w ww  .  j  av a 2  s.c  o m*/
        }
        return res;
    }
    public static List<ApplicationInfo> getInstalledApps() {
        if (mContext != null) {
            final PackageManager pm = mContext.getPackageManager();
            return pm
                    .getInstalledApplications(PackageManager.GET_META_DATA);
        } else
            return null;
    }
    public static List<ApplicationInfo> getInstalledApps(
            boolean onlySupportedApps) {
        if (mContext != null) {
            List<ApplicationInfo> ai = getInstalledApps();
            if (!onlySupportedApps)
                return ai;

            List<ApplicationInfo> res = new ArrayList<ApplicationInfo>();
            for (ApplicationInfo pi : ai) {
                if (isBackupSupportedForApp(pi.packageName)) {
                    res.add(pi);
                }
            }

            return res;
        } else
            return null;
    }
    public static boolean isBackupSupportedForApp(String appPackageName) {
        boolean supported = true;
        if (appPackageName == null)
            return false;
        // UNTESTED APPS BUT PROBABLY BAD IDEA TO RESTORE DATA
        if (appPackageName.startsWith("com.android.")
                || appPackageName.startsWith("com.google.")
                || appPackageName.startsWith("android"))
            supported = false;
        if (appPackageName.startsWith("com.bel.android.")
                || appPackageName.startsWith("com.cyanogenmod.")
                || appPackageName.startsWith("com.koushikdutta."))
            supported = false;
        if (appPackageName.startsWith("com.swype.")
                || appPackageName.startsWith("com.svox.")
                || appPackageName.startsWith("com.tmobile.theme"))
            supported = false;
        // UNTESTED APPS BUT PROBABLY USELESS OR USER UNLIKELY TO RESTORE DATA
        if (appPackageName.startsWith("jackpal.androidterm")
                || appPackageName.startsWith("com.noshufou.android.su"))
            supported = false;
        return supported;
    }
}

Related Tutorials