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

Commit e13bfbde authored by Daniel Nishi's avatar Daniel Nishi Committed by android-build-merger
Browse files

Don't crash when primary volume is null in AppCollector.

am: d54f3a48

Change-Id: I77e27f4a7e901e98995039bcfd68453bec83a6cd
parents 0f99e536 d54f3a48
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.storage;

import android.annotation.NonNull;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageStatsObserver;
@@ -32,6 +33,7 @@ import android.os.UserManager;
import android.os.storage.VolumeInfo;
import android.util.Log;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.Preconditions;

import java.util.ArrayList;
import java.util.List;
@@ -56,7 +58,9 @@ public class AppCollector {
     * @param context Android context used to get
     * @param volume Volume to check for apps.
     */
    public AppCollector(Context context, VolumeInfo volume) {
    public AppCollector(Context context, @NonNull VolumeInfo volume) {
        Preconditions.checkNotNull(volume);

        mBackgroundHandler = new BackgroundHandler(BackgroundThread.get().getLooper(),
                volume,
                context.getPackageManager(),
@@ -117,7 +121,7 @@ public class AppCollector {
        private final PackageManager mPm;
        private final UserManager mUm;

        BackgroundHandler(Looper looper, VolumeInfo volume, PackageManager pm, UserManager um) {
        BackgroundHandler(Looper looper, @NonNull VolumeInfo volume, PackageManager pm, UserManager um) {
            super(looper);
            mVolume = volume;
            mPm = pm;
+9 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.os.BatteryManager;
import android.os.Environment;
import android.os.Environment.UserEnvironment;
import android.os.UserHandle;
import android.os.storage.VolumeInfo;
import android.provider.Settings;
import android.util.Log;

@@ -61,10 +62,16 @@ public class DiskStatsLoggingService extends JobService {
            return false;
        }


        VolumeInfo volume = getPackageManager().getPrimaryStorageCurrentVolume();
        // volume is null if the primary storage is not yet mounted.
        if (volume == null) {
            return false;
        }
        AppCollector collector = new AppCollector(this, volume);

        final int userId = UserHandle.myUserId();
        UserEnvironment environment = new UserEnvironment(userId);
        AppCollector collector = new AppCollector(this,
                getPackageManager().getPrimaryStorageCurrentVolume());
        LogRunnable task = new LogRunnable();
        task.setRootDirectory(environment.getExternalStorageDirectory());
        task.setDownloadsDirectory(
+5 −1
Original line number Diff line number Diff line
@@ -187,10 +187,14 @@ public class AppCollectorTest extends AndroidTestCase {
        }).start();
        latch.await();

        // This should
        assertThat(myStats).containsAllOf(stats, otherStats);
    }

    @Test(expected=NullPointerException.class)
    public void testNullVolumeShouldCauseNPE() throws Exception {
        AppCollector collector = new AppCollector(mContext, null);
    }

    private void addApplication(String packageName, String uuid) {
        ApplicationInfo info = new ApplicationInfo();
        info.packageName = packageName;