Merge "Prevent ClassCastException" into sc-v2-dev

This commit is contained in:
TreeHugger Robot
2021-11-12 21:09:26 +00:00
committed by Android (Google) Code Review
2 changed files with 18 additions and 2 deletions

View File

@@ -345,8 +345,12 @@ public class TileServices extends IQSService.Stub {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (TileService.ACTION_REQUEST_LISTENING.equals(intent.getAction())) { if (TileService.ACTION_REQUEST_LISTENING.equals(intent.getAction())) {
requestListening( try {
(ComponentName) intent.getParcelableExtra(Intent.EXTRA_COMPONENT_NAME)); ComponentName c = intent.getParcelableExtra(Intent.EXTRA_COMPONENT_NAME);
requestListening(c);
} catch (ClassCastException ex) {
Log.e(TAG, "Bad component name", ex);
}
} }
} }
}; };

View File

@@ -27,7 +27,9 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
@@ -142,6 +144,16 @@ public class TileServicesTest extends SysuiTestCase {
assertTrue(captor.getValue().hasAction(TileService.ACTION_REQUEST_LISTENING)); assertTrue(captor.getValue().hasAction(TileService.ACTION_REQUEST_LISTENING));
} }
@Test
public void testBadComponentName_doesntCrash() {
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
verify(mBroadcastDispatcher).registerReceiver(captor.capture(), any(), any(), eq(
UserHandle.ALL));
Intent intent = new Intent(TileService.ACTION_REQUEST_LISTENING)
.putExtra(Intent.EXTRA_COMPONENT_NAME, "abc");
captor.getValue().onReceive(mContext, intent);
}
@Test @Test
public void testRecalculateBindAllowance() { public void testRecalculateBindAllowance() {
// Add some fake tiles. // Add some fake tiles.