Example usage for android.content.pm PackageInfo INSTALL_LOCATION_PREFER_EXTERNAL

List of usage examples for android.content.pm PackageInfo INSTALL_LOCATION_PREFER_EXTERNAL

Introduction

In this page you can find the example usage for android.content.pm PackageInfo INSTALL_LOCATION_PREFER_EXTERNAL.

Prototype

int INSTALL_LOCATION_PREFER_EXTERNAL

To view the source code for android.content.pm PackageInfo INSTALL_LOCATION_PREFER_EXTERNAL.

Click Source Link

Document

Constant corresponding to preferExternal in the android.R.attr#installLocation attribute.

Usage

From source file:com.android.settings.applications.CanBeOnSdCardChecker.java

boolean check(ApplicationInfo info) {
    boolean canBe = false;
    if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
        canBe = true;//  w w  w .  j  av a  2  s  .co  m
    } else {
        if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            if (info.installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL
                    || info.installLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
                canBe = true;
            } else if (info.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED) {
                if (mInstallLocation == PackageHelper.APP_INSTALL_EXTERNAL) {
                    // For apps with no preference and the default value set
                    // to install on sdcard.
                    canBe = true;
                }
            }
        }
    }
    return canBe;
}

From source file:com.landenlabs.all_devtool.PackageFragment.java

/**
 * Load installed (user or system) packages.
 *
 * TODO - include// w ww .  ja v a 2 s.c  o m
 *    /data/local/tmp
 *    /sdcard/local/tmp ?
 *    /storage/sdcardx/LOST.DIR/
 *    /sdcard/download
 *
 */
void loadCachedPackages() {
    try {
        // m_pkgUninstallBtn.setText(R.string.package_uninstall);
        m_uninstallResId = R.string.package_del_cache;
        m_pkgUninstallBtn.post(new Runnable() {
            @Override
            public void run() {
                updateUninstallBtn();
            }
        });

        m_workList = new ArrayList<PackingItem>();

        // PackageManager.GET_SIGNATURES | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS;
        int flags1 = PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES
                | PackageManager.GET_INTENT_FILTERS;
        int flags2 = PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES;
        int flags3 = PackageManager.GET_META_DATA;
        int flags4 = 0;

        List<PackageInfo> packList = getActivity().getPackageManager().getInstalledPackages(flags1);
        /*
        packList = mergePackages(packList,
            getActivity().getPackageManager().getInstalledPackages(flags2));
        packList = mergePackages(packList,
            getActivity().getPackageManager().getInstalledPackages(flags3));
        packList = mergePackages(packList,
            getActivity().getPackageManager().getInstalledPackages(flags4));
        */

        if (packList != null)
            for (int idx = 0; idx < packList.size(); idx++) {
                PackageInfo packInfo = packList.get(idx);
                long cacheSize = 0;
                long fileCount = 0;

                if (packInfo == null || packInfo.lastUpdateTime <= 0) {
                    continue; // Bad package
                }

                Context pkgContext;
                try {
                    m_log.d(String.format("%3d/%d : %s", idx, packList.size(), packInfo.packageName));
                    pkgContext = getActivity().createPackageContext(packInfo.packageName,
                            Context.CONTEXT_IGNORE_SECURITY);
                } catch (Exception ex) {
                    m_log.e(ex.getLocalizedMessage());
                    continue; // Bad package
                }

                File cacheDirectory = null;
                Utils.DirSizeCount cacheDirSize = null;
                if (pkgContext.getCacheDir() != null) {
                    cacheDirectory = pkgContext.getCacheDir();
                } else {
                    // cacheDirectory = new File(mContext.getPackageResourcePath());
                    if (pkgContext.getFilesDir() != null) {
                        String dataPath = pkgContext.getFilesDir().getPath(); // "/data/data/"
                        cacheDirectory = new File(dataPath, pkgContext.getPackageName() + "/cache");
                    }
                }

                if (cacheDirectory != null) {
                    // cacheSize = cacheDirectory.length()/1024;
                    cacheDirSize = Utils.getDirectorySize(cacheDirectory);
                    if (true) {
                        // Cache is not readable or empty,
                        // Try and map cache dir to one of the sd storage paths
                        for (String storageDir : m_storageDirs) {
                            try {
                                File cacheDirectory2 = new File(cacheDirectory.getCanonicalPath()
                                        .replace("/data/data", storageDir + "/Android/data"));
                                if (cacheDirectory2.exists()) {
                                    cacheDirectory = cacheDirectory2;
                                    Utils.DirSizeCount dirSize = Utils.getDirectorySize(cacheDirectory2);
                                    if (cacheDirSize == null || dirSize.size > cacheDirSize.size) {
                                        cacheDirSize = dirSize;
                                        cacheDirectory = cacheDirectory2;
                                    }
                                }
                            } catch (Exception ex) {
                                m_log.d(ex.getMessage());
                            }
                        }
                    }
                } else {
                    m_log.d(packInfo.packageName + " missing cache dir");
                }

                Utils.DirSizeCount datDirSize = null;
                if (packInfo.applicationInfo.dataDir != null) {
                    try {
                        datDirSize = Utils.getDirectorySize(new File(packInfo.applicationInfo.dataDir));
                    } catch (Exception ex) {

                    }
                }

                /*
                                    Method getPackageSizeInfo;
                                    try {
                getPackageSizeInfo = getActivity().getPackageManager().getClass().getMethod(
                        "getPackageSizeInfo", String.class,
                        Class.forName("android.content.pm.IPackageStatsObserver"));
                        
                getPackageSizeInfo.invoke(getActivity().getPackageManager(), packInfo.packageName,
                        new IPackageStatsObserver() {
                        
                            @Override
                            public void onGetStatsCompleted(
                                    PackageStats pStats, boolean succeeded)
                                    throws RemoteException {
                        
                                totalSize = totalSize + pStats.cacheSize;
                            }
                        }
                );
                                    } catch (Exception e) {
                continue;
                                    }
                */
                /* if ((packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) */ {
                    ArrayListPairString pkgList = new ArrayListPairString();
                    String appName = "unknown";
                    try {
                        appName = packInfo.applicationInfo.loadLabel(getActivity().getPackageManager())
                                .toString().trim();
                    } catch (Exception ex) {
                        m_log.e(ex.getLocalizedMessage());
                    }
                    long pkgSize = 0;

                    addList(pkgList, "Version", packInfo.versionName);
                    addList(pkgList, "TargetSDK", String.valueOf(packInfo.applicationInfo.targetSdkVersion));
                    String installTyp = "auto";
                    if (Build.VERSION.SDK_INT >= 21) {
                        switch (packInfo.installLocation) {
                        case PackageInfo.INSTALL_LOCATION_AUTO:
                            break;
                        case PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY:
                            installTyp = "internal";
                            break;
                        case PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL:
                            installTyp = "external";
                            break;
                        }
                    }
                    addList(pkgList, "Install", installTyp);

                    // Add application info.
                    try {
                        addList(pkgList, "Allow Backup",
                                String.valueOf((packInfo.applicationInfo.flags & FLAG_ALLOW_BACKUP) != 0));
                        addList(pkgList, "Debuggable", String.valueOf(
                                (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0));
                        addList(pkgList, "External Storage", String.valueOf(
                                (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0));
                        String themeName = getResourceName(packInfo, packInfo.applicationInfo.theme);
                        if (!TextUtils.isEmpty(themeName))
                            addList(pkgList, "Theme", themeName);
                    } catch (Exception ex) {
                        Log.d("foo", ex.getMessage());
                    }

                    try {
                        File file = new File(packInfo.applicationInfo.sourceDir);
                        pkgSize = file.length();
                    } catch (Exception ex) {
                    }

                    addList(pkgList, "Apk File", packInfo.applicationInfo.publicSourceDir);
                    addList(pkgList, "Apk Size",
                            NumberFormat.getNumberInstance(Locale.getDefault()).format(pkgSize));

                    addList(pkgList, "Src Dir", packInfo.applicationInfo.sourceDir);
                    addList(pkgList, "lib Dir", packInfo.applicationInfo.nativeLibraryDir);

                    addList(pkgList, "dat Dir", packInfo.applicationInfo.dataDir);
                    if (null != datDirSize) {
                        addList(pkgList, "*  Dir Size",
                                NumberFormat.getNumberInstance(Locale.getDefault()).format(datDirSize.size));
                        addList(pkgList, "*  File Count",
                                NumberFormat.getNumberInstance(Locale.getDefault()).format(datDirSize.count));
                    }

                    if (null != cacheDirectory) {
                        addList(pkgList, "Cache", cacheDirectory.getCanonicalPath());
                        if (null != cacheDirSize) {
                            cacheSize = cacheDirSize.size;
                            addList(pkgList, "*  Dir Size", NumberFormat.getNumberInstance(Locale.getDefault())
                                    .format(cacheDirSize.size));
                            addList(pkgList, "*  File Count", NumberFormat
                                    .getNumberInstance(Locale.getDefault()).format(cacheDirSize.count));
                        }
                    }

                    if (null != packInfo.applicationInfo.sharedLibraryFiles
                            && packInfo.applicationInfo.sharedLibraryFiles.length != 0) {
                        addList(pkgList, "ShareLibs", NumberFormat.getNumberInstance(Locale.getDefault())
                                .format(packInfo.applicationInfo.sharedLibraryFiles.length));
                        for (String shrLibStr : packInfo.applicationInfo.sharedLibraryFiles) {
                            addList(pkgList, "  ", shrLibStr);
                        }
                    }

                    if (true) {
                        // packInfo.configPreferences; use with flag= GET_CONFIGURATIONS;
                        // packInfo.providers use with GET_PROVIDERS;

                        List<IntentFilter> outFilters = new ArrayList<IntentFilter>();
                        List<ComponentName> outActivities = new ArrayList<ComponentName>();
                        int num = getActivity().getPackageManager().getPreferredActivities(outFilters,
                                outActivities, packInfo.packageName);
                        if (num > 0) {
                            addList(pkgList, "Preferred #", String.valueOf(num));
                        }
                    }

                    /* if (null != cacheDirectory) */
                    // if (cacheDirSize != null)
                    {
                        m_workList.add(new PackingItem(packInfo.packageName.trim(), pkgList, packInfo,
                                cacheSize, appName));
                    }
                }
            }

    } catch (Exception ex) {
        m_log.e(ex.getMessage());
    }
}