mirror of https://github.com/grpc/grpc-java.git
binder: Work around an Android Intent bug (#9061)
Where filterEquals() can be inconsistent with filterHashCode(). Fixes #9045
This commit is contained in:
parent
3c2c357efa
commit
fba4ae496a
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue