Example usage for android.content.pm PackageManager getInstalledApplications

List of usage examples for android.content.pm PackageManager getInstalledApplications

Introduction

In this page you can find the example usage for android.content.pm PackageManager getInstalledApplications.

Prototype

public abstract List<ApplicationInfo> getInstalledApplications(@ApplicationInfoFlags int flags);

Source Link

Document

Return a List of all application packages that are installed for the current user.

Usage

From source file:fr.simon.marquis.preferencesmanager.util.Utils.java

public static ArrayList<AppEntry> getApplications(Context ctx) {
    PackageManager pm = ctx.getPackageManager();
    if (pm == null) {
        applications = new ArrayList<AppEntry>();
    } else {// w  w  w  .ja  va  2 s  .  c  o  m
        boolean showSystemApps = isShowSystemApps(ctx);
        List<ApplicationInfo> appsInfo = pm.getInstalledApplications(
                PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_DISABLED_COMPONENTS);
        if (appsInfo == null) {
            appsInfo = new ArrayList<ApplicationInfo>();
        }

        List<AppEntry> entries = new ArrayList<AppEntry>(appsInfo.size());
        for (ApplicationInfo a : appsInfo) {
            if (showSystemApps || (a.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                entries.add(new AppEntry(a, ctx));
            }
        }

        Collections.sort(entries, new MyComparator());
        applications = new ArrayList<AppEntry>(entries);
    }
    Log.d(TAG, "Applications: " + Arrays.toString(applications.toArray()));
    return applications;
}

From source file:com.farmerbb.secondscreen.util.U.java

public static String uiRefreshCommand2(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    // For better reliability, we execute the UI refresh while on the home screen
    Intent homeIntent = new Intent(Intent.ACTION_MAIN);
    homeIntent.addCategory(Intent.CATEGORY_HOME);
    homeIntent.addCategory(Intent.CATEGORY_DEFAULT);
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(homeIntent);//from www. j a  va  2s.  c  o m

    // Kill all background processes, in order to fully refresh UI
    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if (!packageInfo.packageName.equalsIgnoreCase(context.getPackageName()))
            am.killBackgroundProcesses(packageInfo.packageName);
    }

    // Get launcher package name
    final ResolveInfo mInfo = pm.resolveActivity(homeIntent, 0);

    return "sleep 1 && am force-stop " + mInfo.activityInfo.applicationInfo.packageName;
}

From source file:org.deviceconnect.android.observer.fragment.WarningDialogFragment.java

/**
 * ????./*from   w w w.  j a  v a 2s .  c  o m*/
 * 
 * @param packageName ???
 * @return ??
 */
private String getAppName(final String packageName) {
    PackageManager pm = getActivity().getPackageManager();
    final List<ApplicationInfo> appInfoList = pm.getInstalledApplications(
            PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_DISABLED_COMPONENTS);

    for (ApplicationInfo ai : appInfoList) {
        String appName = ai.loadLabel(pm).toString();
        if (ai.packageName.equals(packageName)) {
            return appName;
        }
    }
    return "NoName";
}

From source file:com.xargsgrep.portknocker.asynctask.RetrieveApplicationsAsyncTask.java

@Override
protected List<Application> doInBackground(Void... params) {
    PackageManager packageManager = activity.getPackageManager();
    List<ApplicationInfo> installedApplications = packageManager
            .getInstalledApplications(PackageManager.GET_META_DATA);

    List<Application> applications = new ArrayList<Application>();
    for (ApplicationInfo applicationInfo : installedApplications) {
        if (isSystemPackage(applicationInfo)
                || packageManager.getLaunchIntentForPackage(applicationInfo.packageName) == null)
            continue;
        applications.add(new Application(packageManager.getApplicationLabel(applicationInfo).toString(),
                applicationInfo.loadIcon(packageManager), applicationInfo.packageName));
    }//from  w  w  w.  j av  a 2 s .c om

    Collections.sort(applications, new Comparator<Application>() {
        @Override
        public int compare(Application app1, Application app2) {
            return app1.getLabel().compareTo(app2.getLabel());
        }
    });
    applications.add(0, new Application("None", null, ""));

    return applications;
}

From source file:com.emetophobe.permissionviewer.PermissionScanner.java

@Override
public void run() {
    // Get the list of installed packages
    PackageManager pm = mContext.getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    // Send a message to the main thread to display the progress dialog
    sendMessage(MESSAGE_PROGRESS_INIT, packages.size());

    String packageName, appName, permissionName;
    PackageInfo packageInfo;/*from w  w  w . j  a  va  2  s.com*/
    boolean system;
    int count = 0;

    // Iterate over each package in the list
    for (ApplicationInfo appInfo : packages) {
        // Get the package name and label
        packageName = appInfo.packageName;
        try {
            appName = pm.getApplicationLabel(appInfo).toString();
        } catch (Resources.NotFoundException e) { // application not found
            appName = packageName;
        }

        // Get the system flag
        system = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;

        try {
            // Get the list of permissions
            packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
            if (packageInfo.requestedPermissions != null) {
                for (int i = 0; i < packageInfo.requestedPermissions.length; ++i) {
                    if (packageInfo.requestedPermissions[i].startsWith(ANDROID_PERMISSION)) {
                        permissionName = packageInfo.requestedPermissions[i]
                                .substring(ANDROID_PERMISSION.length());

                        // Add a separate entry for each permission
                        addPackage(appName, packageName, permissionName, system);
                    }
                }
            } else {
                // Add an empty permission entry for packages that contain zero permissions
                addPackage(appName, packageName, null, system);
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, e.toString());
        }

        // Send a message to the main thread to update the progress dialog
        sendMessage(MESSAGE_PROGRESS_UPDATE, ++count);
    }

    // Send a message to the main thread that the thread is finished.
    sendMessage(MESSAGE_PROGRESS_COMPLETE, 0);
}

From source file:com.gft.cordova.plugins.trustdevice.TrustedDevicePlugin.java

/**
 * Check if most common rooted apps are installed.
 *
 * @return true = rooted app installed.//from ww w .jav a 2 s  .c  o m
 */
private boolean isRootAppInstalled() {
    // Installed ussing adb shell
    try {
        File file = new File("/system/app/Superuser.apk");
        if (file.exists())
            return true;
    } catch (Exception e) {
    }

    String[] forbiddenInstalledPackages = { "com.noshufou.android.su", "com.thirdparty.superuser",
            "eu.chainfire.supersu", "com.koushikdutta.superuser", "com.zachspong.temprootremovejb",
            "com.ramdroid.appquarantine" };
    try {
        List<String> badApps = Arrays.asList(forbiddenInstalledPackages);
        List<ApplicationInfo> packages;
        PackageManager pm = webView.getContext().getPackageManager();
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if (badApps.contains(packageInfo.packageName))
                return true;
        }
    } catch (Exception e) {
    }
    return false;
}

From source file:com.robopupu.component.AppManagerImpl.java

@Override
public boolean isPackageInstalled(final String packageName) {
    final Context context = application.getApplicationContext();
    final PackageManager manager = context.getPackageManager();
    final List<ApplicationInfo> infos = manager.getInstalledApplications(PackageManager.GET_META_DATA);

    for (final ApplicationInfo info : infos) {
        if (packageName.equalsIgnoreCase(info.packageName)) {
            return true;
        }/*ww  w .ja  va  2s.c om*/
    }
    return false;
}

From source file:eu.musesproject.client.contextmonitoring.sensors.DeviceProtectionSensor.java

public boolean isTrustedAntiVirInstalled() {
    PackageManager packageManager = context.getPackageManager();
    List<ApplicationInfo> installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo appInfo : installedApps) {
        String appName = appInfo.loadLabel(packageManager).toString();

        for (String trustedAVName : trustedAVs) {
            if (trustedAVName.equals(appName)) {
                return true;
            }//  ww  w .  j a v a2s .co  m
        }
    }
    return false;
}

From source file:com.NotifyMe.GcmIntentService.java

private Intent getWeatherAppIntent() {
    final PackageManager pm = getPackageManager();
    //get a list of installed apps.
    String genieWidgetPackage = null;
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo packageInfo : packages) {
        if (packageInfo.packageName.toLowerCase().contains("weather")) {
            return pm.getLaunchIntentForPackage(packageInfo.packageName);
        } else if (packageInfo.packageName.toLowerCase().contains("geniewidget")) {
            genieWidgetPackage = packageInfo.packageName;
        }/*  w w  w.  j  a va2 s  .c o m*/
    }
    // Didn't find a weather app - Try Google News and Weather
    // Will return null if not found.
    return pm.getLaunchIntentForPackage(genieWidgetPackage);
}

From source file:com.shivshankar.MyFirebaseMessagingService.java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d("TAGRK", "From: " + remoteMessage.getFrom());
    String message = "", title = "", strImageURL = "";
    if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
        Log.d("TAGRK", "Message data payload: " + remoteMessage.getData());

        message = "" + remoteMessage.getData().get(Config.MESSAGE_KEY);
        title = "" + remoteMessage.getData().get(Config.TITLE_KEY);
        strImageURL = "" + remoteMessage.getData().get(Config.IMAGE_KEY);
    } else if (remoteMessage.getNotification() != null) {
        Log.d("TAGRK", "Message Notification Body: " + remoteMessage.getNotification().getBody());
        message = "" + remoteMessage.getNotification().getBody();
        title = "" + remoteMessage.getNotification().getTitle();
        strImageURL = "" + remoteMessage.getNotification().getIcon();
    }//from w w  w  . j a  v  a2  s  .  c o  m

    if (strImageURL != null && !strImageURL.equals("") && !strImageURL.equals("null")) {
        Handler handler = new Handler(Looper.getMainLooper());
        final String finalTitle = title;
        final String finalMessage = message;
        final String finalStrImageURL = strImageURL;
        handler.post(new Runnable() {
            public void run() {
                new ServerAPICallImageBitmap(finalTitle, finalMessage, finalStrImageURL, "").execute();
            }
        });
    } else if (title.equalsIgnoreCase("Logout")) {
        try {
            SharedPreferences.Editor editor = AppPreferences.getPrefs().edit();
            editor.putString(commonVariables.KEY_LOGIN_ID, "0");
            editor.putBoolean(commonVariables.KEY_IS_LOG_IN, false);
            editor.putString(commonVariables.KEY_SELLER_PROFILE, "");
            editor.putString(commonVariables.KEY_BUYER_PROFILE, "");
            editor.putString(commonVariables.KEY_BRAND, "");
            editor.putBoolean(commonVariables.KEY_IS_BRAND, false);
            editor.putBoolean(commonVariables.KEY_IS_SELLER, false);
            editor.putBoolean(commonVariables.KEY_IS_SKIPPED_LOGIN_BUYER, false);
            editor.commit();
            android.os.Process.killProcess(android.os.Process.myPid());
            List<ApplicationInfo> packages;
            PackageManager pm;
            pm = getPackageManager();
            packages = pm.getInstalledApplications(0);

            ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            String myPackage = getApplicationContext().getPackageName();
            for (ApplicationInfo packageInfo : packages) {
                if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
                    continue;
                if (packageInfo.packageName.equals(myPackage))
                    continue;
                mActivityManager.killBackgroundProcesses(packageInfo.packageName);
            }
            mActivityManager.restartPackage(myPackage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else
        sendNotification(title, message);
    Log.i("TAGRK", "Received: " + remoteMessage.toString());
}