Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6d687f05 authored by LuK1337's avatar LuK1337
Browse files

PackageInstaller: Fix crash when uninstalling apps with fragile data

parent 7d16102b
Loading
Loading
Loading
Loading
+8 −20
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -64,29 +63,18 @@ public class UninstallAlertDialogFragment extends DialogFragment implements
     * @return The number of bytes.
     */
    private long getAppDataSizeForUser(@NonNull String pkg, @NonNull UserHandle user) {
        StorageManager storageManager = getContext().getSystemService(StorageManager.class);
        PackageManager packageManager = getContext().getPackageManager();
        StorageStatsManager storageStatsManager =
                getContext().getSystemService(StorageStatsManager.class);

        List<StorageVolume> volumes = storageManager.getStorageVolumes();
        long appDataSize = 0;

        int numVolumes = volumes.size();
        for (int i = 0; i < numVolumes; i++) {
            StorageStats stats;
        try {
                stats = storageStatsManager.queryStatsForPackage(convert(volumes.get(i).getUuid()),
                        pkg, user);
            ApplicationInfo info = packageManager.getApplicationInfo(pkg, 0);
            return storageStatsManager.queryStatsForPackage(
                    info.storageUuid, pkg, user).getDataBytes();
        } catch (PackageManager.NameNotFoundException | IOException e) {
                Log.e(LOG_TAG, "Cannot determine amount of app data for " + pkg + " on "
                        + volumes.get(i) + " (user " + user + ")", e);
                continue;
            }

            appDataSize += stats.getDataBytes();
            Log.e(LOG_TAG, "Cannot determine amount of app data for " + pkg, e);
            return 0;
        }

        return appDataSize;
    }

    /**