Merge "Prevent ClassCastException" into sc-v2-dev

This commit is contained in:
TreeHugger Robot
2021-11-09 05:08:56 +00:00
committed by Android (Google) Code Review
2 changed files with 40 additions and 8 deletions

View File

@@ -28,6 +28,7 @@ import android.os.UserHandle
import android.service.controls.Control import android.service.controls.Control
import android.service.controls.ControlsProviderService import android.service.controls.ControlsProviderService
import android.util.Log import android.util.Log
import java.lang.ClassCastException
/** /**
* Proxy to launch in user 0 * Proxy to launch in user 0
@@ -59,20 +60,29 @@ class ControlsRequestReceiver : BroadcastReceiver() {
return return
} }
val packageName = intent.getParcelableExtra<ComponentName>(Intent.EXTRA_COMPONENT_NAME) val targetComponent = try {
?.packageName intent.getParcelableExtra<ComponentName>(Intent.EXTRA_COMPONENT_NAME)
} catch (e: ClassCastException) {
Log.e(TAG, "Malformed intent extra ComponentName", e)
return
}
val control = try {
intent.getParcelableExtra<Control>(ControlsProviderService.EXTRA_CONTROL)
} catch (e: ClassCastException) {
Log.e(TAG, "Malformed intent extra Control", e)
return
}
val packageName = targetComponent?.packageName
if (packageName == null || !isPackageInForeground(context, packageName)) { if (packageName == null || !isPackageInForeground(context, packageName)) {
return return
} }
val activityIntent = Intent(context, ControlsRequestDialog::class.java).apply { val activityIntent = Intent(context, ControlsRequestDialog::class.java).apply {
Intent.EXTRA_COMPONENT_NAME.let { putExtra(Intent.EXTRA_COMPONENT_NAME, targetComponent)
putExtra(it, intent.getParcelableExtra<ComponentName>(it)) putExtra(ControlsProviderService.EXTRA_CONTROL, control)
}
ControlsProviderService.EXTRA_CONTROL.let {
putExtra(it, intent.getParcelableExtra<Control>(it))
}
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
} }
activityIntent.putExtra(Intent.EXTRA_USER_ID, context.userId) activityIntent.putExtra(Intent.EXTRA_USER_ID, context.userId)

View File

@@ -154,6 +154,28 @@ class ControlsRequestReceiverTest : SysuiTestCase() {
assertNull(wrapper.intent) assertNull(wrapper.intent)
} }
@Test
fun testClassCastExceptionComponentName_noCrash() {
val badIntent = Intent(ControlsProviderService.ACTION_ADD_CONTROL).apply {
putExtra(Intent.EXTRA_COMPONENT_NAME, Intent())
putExtra(ControlsProviderService.EXTRA_CONTROL, control)
}
receiver.onReceive(wrapper, badIntent)
assertNull(wrapper.intent)
}
@Test
fun testClassCastExceptionControl_noCrash() {
val badIntent = Intent(ControlsProviderService.ACTION_ADD_CONTROL).apply {
putExtra(Intent.EXTRA_COMPONENT_NAME, componentName)
putExtra(ControlsProviderService.EXTRA_CONTROL, Intent())
}
receiver.onReceive(wrapper, badIntent)
assertNull(wrapper.intent)
}
class MyWrapper(context: Context) : ContextWrapper(context) { class MyWrapper(context: Context) : ContextWrapper(context) {
var intent: Intent? = null var intent: Intent? = null