Add getContract to ActivityResultLauncher
Intents provide a way to check if the intent can be handled by any
Activity components and the ActivityResultLauncher should provide
the ability to do something similar.
This change adds a getContract API to ActivityResultLauncher that gives
the returns the contract used to register the launcher. This means that
users can create an Intent from their contract and make all of their
desired checks.
Test: ActivityResultLauncherTest
Bug: 156875743
RelNote: "ActivityResultLauncher now allows you to get the
ActivityResultContract that was used to register the launcher."
Change-Id: I64f536f2b3dde3e87cea884c6b9c111e5efe26ed
diff --git a/activity/activity-ktx/build.gradle b/activity/activity-ktx/build.gradle
index c728b82..13a5978 100644
--- a/activity/activity-ktx/build.gradle
+++ b/activity/activity-ktx/build.gradle
@@ -51,6 +51,9 @@
androidTestImplementation(ANDROIDX_TEST_CORE)
androidTestImplementation(ANDROIDX_TEST_RUNNER)
androidTestImplementation(ANDROIDX_TEST_RULES)
+ androidTestImplementation project(':internal-testutils-runtime'), {
+ exclude group: 'androidx.activity', module: 'activity'
+ }
}
androidx {
diff --git a/activity/activity-ktx/src/androidTest/AndroidManifest.xml b/activity/activity-ktx/src/androidTest/AndroidManifest.xml
index fb50b6c..ee97faf 100644
--- a/activity/activity-ktx/src/androidTest/AndroidManifest.xml
+++ b/activity/activity-ktx/src/androidTest/AndroidManifest.xml
@@ -17,5 +17,6 @@
package="androidx.activity.ktx">
<application>
<activity android:name="androidx.activity.ActivityViewModelLazyTest$TestActivity"/>
+ <activity android:name="androidx.activity.result.EmptyContentActivity" />
</application>
</manifest>
diff --git a/activity/activity-ktx/src/androidTest/java/androidx/activity/result/ActivityResultCallerTest.kt b/activity/activity-ktx/src/androidTest/java/androidx/activity/result/ActivityResultCallerTest.kt
new file mode 100644
index 0000000..16ce5fc
--- /dev/null
+++ b/activity/activity-ktx/src/androidTest/java/androidx/activity/result/ActivityResultCallerTest.kt
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.activity.result
+
+import android.content.Intent
+import androidx.activity.ComponentActivity
+import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
+import androidx.test.core.app.ActivityScenario
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.MediumTest
+import androidx.testutils.withActivity
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@MediumTest
+@RunWith(AndroidJUnit4::class)
+class ActivityResultCallerTest {
+ @Test
+ fun getContractTest() {
+ with(ActivityScenario.launch(EmptyContentActivity::class.java)) {
+ val contract = StartActivityForResult()
+
+ val javaLauncher = withActivity {
+ registerForActivityResult(contract) { }
+ }
+
+ val kotlinLauncher = withActivity {
+ registerForActivityResult(contract, Intent()) { }
+ }
+
+ assertThat(javaLauncher.contract).isSameInstanceAs(contract)
+ assertThat(kotlinLauncher.contract).isNotSameInstanceAs(contract)
+ }
+ }
+}
+
+class EmptyContentActivity : ComponentActivity()
diff --git a/activity/activity-ktx/src/main/java/androidx/activity/result/ActivityResultCaller.kt b/activity/activity-ktx/src/main/java/androidx/activity/result/ActivityResultCaller.kt
index 45f3093..6ad3388 100644
--- a/activity/activity-ktx/src/main/java/androidx/activity/result/ActivityResultCaller.kt
+++ b/activity/activity-ktx/src/main/java/androidx/activity/result/ActivityResultCaller.kt
@@ -16,6 +16,8 @@
package androidx.activity.result
+import android.content.Context
+import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import androidx.core.app.ActivityOptionsCompat
@@ -33,7 +35,7 @@
callback: (O) -> Unit
): ActivityResultLauncher<Unit> {
val resultLauncher = registerForActivityResult(contract, registry) { callback(it) }
- return ActivityResultCallerLauncher(resultLauncher, input)
+ return ActivityResultCallerLauncher(resultLauncher, contract, input)
}
/**
@@ -49,13 +51,26 @@
callback: (O) -> Unit
): ActivityResultLauncher<Unit> {
val resultLauncher = registerForActivityResult(contract) { callback(it) }
- return ActivityResultCallerLauncher(resultLauncher, input)
+ return ActivityResultCallerLauncher(resultLauncher, contract, input)
}
-internal class ActivityResultCallerLauncher<I>(
+internal class ActivityResultCallerLauncher<I, O>(
val launcher: ActivityResultLauncher<I>,
+ val callerContract: ActivityResultContract<I, O>,
val input: I
) : ActivityResultLauncher<Unit>() {
+ val resultContract: ActivityResultContract<Unit, O> by lazy {
+ object : ActivityResultContract<Unit, O>() {
+ override fun createIntent(context: Context, void: Unit?): Intent {
+ return callerContract.createIntent(context, input)
+ }
+
+ override fun parseResult(resultCode: Int, intent: Intent?): O {
+ return callerContract.parseResult(resultCode, intent)
+ }
+ }
+ }
+
override fun launch(void: Unit?, options: ActivityOptionsCompat?) {
launcher.launch(input, options)
}
@@ -63,4 +78,8 @@
override fun unregister() {
launcher.unregister()
}
+
+ override fun getContract(): ActivityResultContract<Unit, O> {
+ return resultContract
+ }
}
diff --git a/activity/activity/api/1.2.0-alpha06.txt b/activity/activity/api/1.2.0-alpha06.txt
index 5627e74..efff88f 100644
--- a/activity/activity/api/1.2.0-alpha06.txt
+++ b/activity/activity/api/1.2.0-alpha06.txt
@@ -69,6 +69,7 @@
public abstract class ActivityResultLauncher<I> {
ctor public ActivityResultLauncher();
+ method public abstract androidx.activity.result.contract.ActivityResultContract<I!,?> getContract();
method public void launch(I!);
method public abstract void launch(I!, androidx.core.app.ActivityOptionsCompat?);
method @MainThread public abstract void unregister();
diff --git a/activity/activity/api/current.txt b/activity/activity/api/current.txt
index 5627e74..efff88f 100644
--- a/activity/activity/api/current.txt
+++ b/activity/activity/api/current.txt
@@ -69,6 +69,7 @@
public abstract class ActivityResultLauncher<I> {
ctor public ActivityResultLauncher();
+ method public abstract androidx.activity.result.contract.ActivityResultContract<I!,?> getContract();
method public void launch(I!);
method public abstract void launch(I!, androidx.core.app.ActivityOptionsCompat?);
method @MainThread public abstract void unregister();
diff --git a/activity/activity/api/public_plus_experimental_1.2.0-alpha06.txt b/activity/activity/api/public_plus_experimental_1.2.0-alpha06.txt
index 36c0825..f583f15 100644
--- a/activity/activity/api/public_plus_experimental_1.2.0-alpha06.txt
+++ b/activity/activity/api/public_plus_experimental_1.2.0-alpha06.txt
@@ -68,6 +68,7 @@
public abstract class ActivityResultLauncher<I> {
ctor public ActivityResultLauncher();
+ method public abstract androidx.activity.result.contract.ActivityResultContract<I!,?> getContract();
method public void launch(I!);
method public abstract void launch(I!, androidx.core.app.ActivityOptionsCompat?);
method @MainThread public abstract void unregister();
diff --git a/activity/activity/api/public_plus_experimental_current.txt b/activity/activity/api/public_plus_experimental_current.txt
index 36c0825..f583f15 100644
--- a/activity/activity/api/public_plus_experimental_current.txt
+++ b/activity/activity/api/public_plus_experimental_current.txt
@@ -68,6 +68,7 @@
public abstract class ActivityResultLauncher<I> {
ctor public ActivityResultLauncher();
+ method public abstract androidx.activity.result.contract.ActivityResultContract<I!,?> getContract();
method public void launch(I!);
method public abstract void launch(I!, androidx.core.app.ActivityOptionsCompat?);
method @MainThread public abstract void unregister();
diff --git a/activity/activity/api/restricted_1.2.0-alpha06.txt b/activity/activity/api/restricted_1.2.0-alpha06.txt
index 36c0825..f583f15 100644
--- a/activity/activity/api/restricted_1.2.0-alpha06.txt
+++ b/activity/activity/api/restricted_1.2.0-alpha06.txt
@@ -68,6 +68,7 @@
public abstract class ActivityResultLauncher<I> {
ctor public ActivityResultLauncher();
+ method public abstract androidx.activity.result.contract.ActivityResultContract<I!,?> getContract();
method public void launch(I!);
method public abstract void launch(I!, androidx.core.app.ActivityOptionsCompat?);
method @MainThread public abstract void unregister();
diff --git a/activity/activity/api/restricted_current.txt b/activity/activity/api/restricted_current.txt
index 36c0825..f583f15 100644
--- a/activity/activity/api/restricted_current.txt
+++ b/activity/activity/api/restricted_current.txt
@@ -68,6 +68,7 @@
public abstract class ActivityResultLauncher<I> {
ctor public ActivityResultLauncher();
+ method public abstract androidx.activity.result.contract.ActivityResultContract<I!,?> getContract();
method public void launch(I!);
method public abstract void launch(I!, androidx.core.app.ActivityOptionsCompat?);
method @MainThread public abstract void unregister();
diff --git a/activity/activity/src/androidTest/java/androidx/activity/result/ActivityResultLauncherTest.kt b/activity/activity/src/androidTest/java/androidx/activity/result/ActivityResultLauncherTest.kt
index c441637..73c3ab2 100644
--- a/activity/activity/src/androidTest/java/androidx/activity/result/ActivityResultLauncherTest.kt
+++ b/activity/activity/src/androidTest/java/androidx/activity/result/ActivityResultLauncherTest.kt
@@ -24,6 +24,7 @@
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import androidx.test.filters.SdkSuppress
+import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Test
import org.junit.runner.RunWith
@@ -85,4 +86,12 @@
.that(registry.invokeOptions?.launchBounds?.left)
.isEqualTo(1)
}
+
+ @Test
+ fun getContractTest() {
+ val contract = StartActivityForResult()
+ val launcher = registry.register("key", contract) {}
+
+ assertThat(contract).isSameInstanceAs(launcher.contract)
+ }
}
diff --git a/activity/activity/src/main/java/androidx/activity/result/ActivityResultLauncher.java b/activity/activity/src/main/java/androidx/activity/result/ActivityResultLauncher.java
index 5d29e2b..84aa1bf 100644
--- a/activity/activity/src/main/java/androidx/activity/result/ActivityResultLauncher.java
+++ b/activity/activity/src/main/java/androidx/activity/result/ActivityResultLauncher.java
@@ -21,6 +21,7 @@
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.MainThread;
+import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityOptionsCompat;
@@ -59,4 +60,12 @@
*/
@MainThread
public abstract void unregister();
+
+ /**
+ * Get the {@link ActivityResultContract} that was used to create this launcher.
+ *
+ * @return the contract that was used to create this launcher
+ */
+ @NonNull
+ public abstract ActivityResultContract<I, ?> getContract();
}
diff --git a/activity/activity/src/main/java/androidx/activity/result/ActivityResultRegistry.java b/activity/activity/src/main/java/androidx/activity/result/ActivityResultRegistry.java
index a390cbd..711d9c5 100644
--- a/activity/activity/src/main/java/androidx/activity/result/ActivityResultRegistry.java
+++ b/activity/activity/src/main/java/androidx/activity/result/ActivityResultRegistry.java
@@ -151,6 +151,12 @@
public void unregister() {
ActivityResultRegistry.this.unregister(key);
}
+
+ @NonNull
+ @Override
+ public ActivityResultContract<I, ?> getContract() {
+ return contract;
+ }
};
}
@@ -196,6 +202,12 @@
public void unregister() {
ActivityResultRegistry.this.unregister(key);
}
+
+ @NonNull
+ @Override
+ public ActivityResultContract<I, ?> getContract() {
+ return contract;
+ }
};
}
diff --git a/fragment/fragment/src/main/java/androidx/fragment/app/Fragment.java b/fragment/fragment/src/main/java/androidx/fragment/app/Fragment.java
index 796cf3b..895e9d5 100644
--- a/fragment/fragment/src/main/java/androidx/fragment/app/Fragment.java
+++ b/fragment/fragment/src/main/java/androidx/fragment/app/Fragment.java
@@ -3379,6 +3379,12 @@
delegate.unregister();
}
}
+
+ @NonNull
+ @Override
+ public ActivityResultContract<I, ?> getContract() {
+ return contract;
+ }
};
}