binder: Work around an Android Intent bug (#9061)

Where filterEquals() can be inconsistent with filterHashCode().

Fixes #9045
This commit is contained in:
John Cormie 2022-04-06 07:37:00 -07:00 committed by GitHub
parent 3c2c357efa
commit fba4ae496a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -128,7 +128,14 @@ public class AndroidComponentAddress extends SocketAddress { // NOTE: Only tempo
@Override
public int hashCode() {
return bindIntent.filterHashCode();
Intent intentForHashCode = bindIntent;
// Clear a (usually redundant) package filter to work around an Android >= 31 bug where certain
// Intents compare filterEquals() but have different filterHashCode() values. It's always safe
// to include fewer fields in the hashCode() computation.
if (intentForHashCode.getPackage() != null) {
intentForHashCode = intentForHashCode.cloneFilter().setPackage(null);
}
return intentForHashCode.filterHashCode();
}
@Override

View File

@ -27,6 +27,7 @@ import com.google.common.testing.EqualsTester;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
public final class AndroidComponentAddressTest {
@ -85,4 +86,35 @@ public final class AndroidComponentAddressTest {
.setComponent(hostComponent)))
.testEquals();
}
@Test
@Config(sdk = 30)
public void testPackageFilterEquality30AndUp() {
new EqualsTester()
.addEqualityGroup(
AndroidComponentAddress.forBindIntent(
new Intent().setAction("action").setComponent(new ComponentName("pkg", "cls"))),
AndroidComponentAddress.forBindIntent(
new Intent()
.setAction("action")
.setPackage("pkg")
.setComponent(new ComponentName("pkg", "cls"))))
.testEquals();
}
@Test
@Config(sdk = 29)
public void testPackageFilterEqualityPre30() {
new EqualsTester()
.addEqualityGroup(
AndroidComponentAddress.forBindIntent(
new Intent().setAction("action").setComponent(new ComponentName("pkg", "cls"))))
.addEqualityGroup(
AndroidComponentAddress.forBindIntent(
new Intent()
.setAction("action")
.setPackage("pkg")
.setComponent(new ComponentName("pkg", "cls"))))
.testEquals();
}
}