user6747225
user6747225

Reputation:

IPackageStatsObserver not triggered on actual device

I have method to calculate installed applications storage size, data size, and cache size using IPackageStatsObserver, but on actual device callback.onSucces() is not triggered. On emulator is working fine, but on board success in never called. I also have GET_PACKAGE_SIZE permission in manifest.

Here is the code

public void requestAppInfo(String packageName, final AsyncDataReceiver<ArrayList<InstalledAppItem>> callback) {

    PackageManager packageManager = getPackageManager();

    try {
        Method getPackageSizeInfo = packageManager.getClass().getMethod(GET_PACKAGE_SIZE_INFO, String.class, IPackageStatsObserver.class);
        getPackageSizeInfo.invoke(packageManager, packageName, new IPackageStatsObserver.Stub() {
            @Override
            public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) {

                int appSize = (int) pStats.codeSize / 1048576;
                int dataSize = (int) pStats.dataSize / 1024;
                int cacheSize = (int) pStats.cacheSize / 1024;

                systemData.clear();
                systemData.add(new InstalledAppItem(0, "Internal Storage Used", appSize + " MB"));
                systemData.add(new InstalledAppItem(1, "Data Used", dataSize + " KB"));
                systemData.add(new InstalledAppItem(2, "Cache Used", cacheSize + "KB"));

                //TODO Check Why callback not triggered on board.
                callback.onSuccess(systemData);
            }
        });

    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }

Upvotes: 0

Views: 587

Answers (2)

Husein Behboudi Rad
Husein Behboudi Rad

Reputation: 5462

Updated For supporting Android O (API Level 26)

From Android O (API level 26) you can not use getPackageSizeInfo method with reflaction. Following post includes a code that can help you for both apps below api level 26 and above it:

https://stackoverflow.com/a/56616172/1939409

Upvotes: 1

Mohd Saquib
Mohd Saquib

Reputation: 590

Please go through this solution link

I calculated cache size of all installed app hope this might help you...

//here packages is the list of all packages

Method mGetPackageSizeInfoMethod;
long mCacheSize;

for (ApplicationInfo pkg : packages) {
                    mGetPackageSizeInfoMethod.invoke(getPackageManager(), pkg.packageName,
                            new IPackageStatsObserver.Stub() {
                                @Override
                                public void onGetStatsCompleted(PackageStats pStats,
                                                                boolean succeeded)
                                        throws RemoteException {
                                    synchronized (apps) {

                                        mCacheSize += addPackage(apps, pStats, succeeded);
                                    }
                                }
                            }
                    );
                }

private long addPackage(List<AppsListItem> apps, PackageStats pStats, boolean succeeded) {
            long cacheSize = 0;

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                cacheSize += pStats.cacheSize;
            }

            cacheSize += pStats.externalCacheSize;

            if (!succeeded || cacheSize <= 0) {
                return 0;
            }

            return cacheSize;
        }
    }

Upvotes: 0

Related Questions