Don't crash when primary volume is null in AppCollector.
am: d54f3a487b
Change-Id: I77e27f4a7e901e98995039bcfd68453bec83a6cd
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.server.storage;
|
package com.android.server.storage;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.IPackageStatsObserver;
|
import android.content.pm.IPackageStatsObserver;
|
||||||
@@ -32,6 +33,7 @@ import android.os.UserManager;
|
|||||||
import android.os.storage.VolumeInfo;
|
import android.os.storage.VolumeInfo;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.android.internal.os.BackgroundThread;
|
import com.android.internal.os.BackgroundThread;
|
||||||
|
import com.android.internal.util.Preconditions;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -56,7 +58,9 @@ public class AppCollector {
|
|||||||
* @param context Android context used to get
|
* @param context Android context used to get
|
||||||
* @param volume Volume to check for apps.
|
* @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(),
|
mBackgroundHandler = new BackgroundHandler(BackgroundThread.get().getLooper(),
|
||||||
volume,
|
volume,
|
||||||
context.getPackageManager(),
|
context.getPackageManager(),
|
||||||
@@ -117,7 +121,7 @@ public class AppCollector {
|
|||||||
private final PackageManager mPm;
|
private final PackageManager mPm;
|
||||||
private final UserManager mUm;
|
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);
|
super(looper);
|
||||||
mVolume = volume;
|
mVolume = volume;
|
||||||
mPm = pm;
|
mPm = pm;
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import android.os.BatteryManager;
|
|||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Environment.UserEnvironment;
|
import android.os.Environment.UserEnvironment;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
import android.os.storage.VolumeInfo;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -61,10 +62,16 @@ public class DiskStatsLoggingService extends JobService {
|
|||||||
return false;
|
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();
|
final int userId = UserHandle.myUserId();
|
||||||
UserEnvironment environment = new UserEnvironment(userId);
|
UserEnvironment environment = new UserEnvironment(userId);
|
||||||
AppCollector collector = new AppCollector(this,
|
|
||||||
getPackageManager().getPrimaryStorageCurrentVolume());
|
|
||||||
LogRunnable task = new LogRunnable();
|
LogRunnable task = new LogRunnable();
|
||||||
task.setRootDirectory(environment.getExternalStorageDirectory());
|
task.setRootDirectory(environment.getExternalStorageDirectory());
|
||||||
task.setDownloadsDirectory(
|
task.setDownloadsDirectory(
|
||||||
|
|||||||
@@ -187,10 +187,14 @@ public class AppCollectorTest extends AndroidTestCase {
|
|||||||
}).start();
|
}).start();
|
||||||
latch.await();
|
latch.await();
|
||||||
|
|
||||||
// This should
|
|
||||||
assertThat(myStats).containsAllOf(stats, otherStats);
|
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) {
|
private void addApplication(String packageName, String uuid) {
|
||||||
ApplicationInfo info = new ApplicationInfo();
|
ApplicationInfo info = new ApplicationInfo();
|
||||||
info.packageName = packageName;
|
info.packageName = packageName;
|
||||||
|
|||||||
Reference in New Issue
Block a user