Skip to content

Commit

Permalink
binder: Avoid an ISE from asAndroidAppUri() (#9169)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdcormie committed May 17, 2022
1 parent a88f19f commit 3f19dfa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Expand Up @@ -121,9 +121,17 @@ public Intent asBindIntent() {

/**
* Returns this address as an "android-app://" uri.
*
* <p>See {@link Intent#URI_ANDROID_APP_SCHEME} for details.
*/
public String asAndroidAppUri() {
return bindIntent.toUri(URI_ANDROID_APP_SCHEME);
Intent intentForUri = bindIntent;
if (intentForUri.getPackage() == null) {
// URI_ANDROID_APP_SCHEME requires an "explicit package name" which isn't set by any of our
// factory methods. Oddly, our explicit ComponentName is not enough.
intentForUri = intentForUri.cloneFilter().setPackage(getComponent().getPackageName());
}
return intentForUri.toUri(URI_ANDROID_APP_SCHEME);
}

@Override
Expand Down
Expand Up @@ -16,6 +16,7 @@

package io.grpc.binder;

import static android.content.Intent.URI_ANDROID_APP_SCHEME;
import static com.google.common.truth.Truth.assertThat;

import android.content.ComponentName;
Expand All @@ -24,6 +25,7 @@
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import com.google.common.testing.EqualsTester;
import java.net.URISyntaxException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand Down Expand Up @@ -61,6 +63,30 @@ public void testAsBindIntent() {
assertThat(addr.asBindIntent().filterEquals(bindIntent)).isTrue();
}

@Test
@Config(sdk = 30)
public void testAsAndroidAppUriSdk30() throws URISyntaxException {
AndroidComponentAddress addr =
AndroidComponentAddress.forRemoteComponent("com.foo", "com.foo.Service");
AndroidComponentAddress addrClone =
AndroidComponentAddress.forBindIntent(
Intent.parseUri(addr.asAndroidAppUri(), URI_ANDROID_APP_SCHEME));
assertThat(addr).isEqualTo(addrClone);
}

@Test
@Config(sdk = 29)
public void testAsAndroidAppUriSdk29() throws URISyntaxException {
AndroidComponentAddress addr =
AndroidComponentAddress.forRemoteComponent("com.foo", "com.foo.Service");
AndroidComponentAddress addrClone =
AndroidComponentAddress.forBindIntent(
Intent.parseUri(addr.asAndroidAppUri(), URI_ANDROID_APP_SCHEME));
// Can't test for equality because URI_ANDROID_APP_SCHEME adds a (redundant) package filter.
assertThat(addr.getComponent()).isEqualTo(addrClone.getComponent());
assertThat(addr.getAuthority()).isEqualTo(addrClone.getAuthority());
}

@Test
public void testEquality() {
new EqualsTester()
Expand Down

0 comments on commit 3f19dfa

Please sign in to comment.