Add null check for worker

Add null check to avoid crash when worker cannot get through uri.

Bug: 128492874
Test: make -j42 RunSettingsRoboTests
Change-Id: I789ce0bfa98c0b0b145a8605c58c88a77eeb76a9
This commit is contained in:
hughchen
2019-03-14 11:29:32 +08:00
parent d4ab14969f
commit 8f76b44410
2 changed files with 16 additions and 2 deletions

View File

@@ -76,6 +76,11 @@ public class MediaOutputSlice implements CustomSliceable {
return null; return null;
} }
if (getWorker() == null) {
Log.d(TAG, "getSlice() Can not get worker through uri!");
return null;
}
final List<MediaDevice> devices = getMediaDevices(); final List<MediaDevice> devices = getMediaDevices();
@ColorInt final int color = Utils.getColorAccentDefaultColor(mContext); @ColorInt final int color = Utils.getColorAccentDefaultColor(mContext);
@@ -115,13 +120,15 @@ public class MediaOutputSlice implements CustomSliceable {
private MediaDeviceUpdateWorker getWorker() { private MediaDeviceUpdateWorker getWorker() {
if (mWorker == null) { if (mWorker == null) {
mWorker = (MediaDeviceUpdateWorker) SliceBackgroundWorker.getInstance(getUri()); mWorker = (MediaDeviceUpdateWorker) SliceBackgroundWorker.getInstance(getUri());
mWorker.setPackageName(mPackageName); if (mWorker != null) {
mWorker.setPackageName(mPackageName);
}
} }
return mWorker; return mWorker;
} }
private List<MediaDevice> getMediaDevices() { private List<MediaDevice> getMediaDevices() {
List<MediaDevice> devices = getWorker().getMediaDevices(); final List<MediaDevice> devices = getWorker().getMediaDevices();
return devices; return devices;
} }

View File

@@ -93,6 +93,13 @@ public class MediaOutputSliceTest {
mMediaOutputSlice.init(TEST_PACKAGE_NAME, mMediaDeviceUpdateWorker); mMediaOutputSlice.init(TEST_PACKAGE_NAME, mMediaDeviceUpdateWorker);
} }
@Test
public void getSlice_workerIsNull_shouldNotCrash() {
mMediaOutputSlice.init(TEST_PACKAGE_NAME, null);
mMediaOutputSlice.getSlice();
}
@Test @Test
public void getSlice_shouldHaveActiveDeviceName() { public void getSlice_shouldHaveActiveDeviceName() {
mDevices.clear(); mDevices.clear();