Skip to content

Commit

Permalink
binder: Work around an Android Intent bug (#9061)
Browse files Browse the repository at this point in the history
Where filterEquals() can be inconsistent with filterHashCode().

Fixes #9045
  • Loading branch information
jdcormie committed Apr 6, 2022
1 parent 3c2c357 commit fba4ae4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Expand Up @@ -128,7 +128,14 @@ public String asAndroidAppUri() {

@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
Expand Down
Expand Up @@ -27,6 +27,7 @@
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 {
Expand Down Expand Up @@ -85,4 +86,35 @@ public void testEquality() {
.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();
}
}

0 comments on commit fba4ae4

Please sign in to comment.