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

am: d54f3a487b

Change-Id: I77e27f4a7e901e98995039bcfd68453bec83a6cd
This commit is contained in:
Daniel Nishi
2017-02-22 04:04:38 +00:00
committed by android-build-merger
3 changed files with 20 additions and 5 deletions

View File

@@ -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;

View File

@@ -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(

View File

@@ -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;