Create a receiver scoped callback for addCallback
Rather than requiring developers construct
a OnBackPressedCallback, Kotlin users can
pass in a lambda that has a receiver scoped
OnBackPressedCallback, allowing them to access
the other methods on OnBackPressedCallback from
within their lambda.
The created OnBackPressedCallback is returned
so that it can be passed to other components or
have its methods called from outside the lambda.
Test: new tests
BUG: 129946737
Change-Id: Id510373020121621035f68fac44194d0e2584a95
diff --git a/activity/ktx/api/1.0.0-alpha07.txt b/activity/ktx/api/1.0.0-alpha07.txt
index be35c63..53f86e9 100644
--- a/activity/ktx/api/1.0.0-alpha07.txt
+++ b/activity/ktx/api/1.0.0-alpha07.txt
@@ -6,5 +6,10 @@
method @MainThread public static inline <reified VM extends androidx.lifecycle.ViewModel> kotlin.Lazy<VM>! viewModels(androidx.activity.ComponentActivity, kotlin.jvm.functions.Function0<? extends androidx.lifecycle.ViewModelProvider.Factory>! factoryProducer = null);
}
+ public final class OnBackPressedDispatcherKt {
+ ctor public OnBackPressedDispatcherKt();
+ method public static androidx.activity.OnBackPressedCallback addCallback(androidx.activity.OnBackPressedDispatcher, androidx.lifecycle.LifecycleOwner? owner = null, boolean enabled = true, kotlin.jvm.functions.Function1<? super androidx.activity.OnBackPressedCallback,kotlin.Unit> onBackPressed);
+ }
+
}
diff --git a/activity/ktx/api/current.txt b/activity/ktx/api/current.txt
index be35c63..53f86e9 100644
--- a/activity/ktx/api/current.txt
+++ b/activity/ktx/api/current.txt
@@ -6,5 +6,10 @@
method @MainThread public static inline <reified VM extends androidx.lifecycle.ViewModel> kotlin.Lazy<VM>! viewModels(androidx.activity.ComponentActivity, kotlin.jvm.functions.Function0<? extends androidx.lifecycle.ViewModelProvider.Factory>! factoryProducer = null);
}
+ public final class OnBackPressedDispatcherKt {
+ ctor public OnBackPressedDispatcherKt();
+ method public static androidx.activity.OnBackPressedCallback addCallback(androidx.activity.OnBackPressedDispatcher, androidx.lifecycle.LifecycleOwner? owner = null, boolean enabled = true, kotlin.jvm.functions.Function1<? super androidx.activity.OnBackPressedCallback,kotlin.Unit> onBackPressed);
+ }
+
}
diff --git a/activity/ktx/src/androidTest/java/androidx/activity/OnBackPressedDispatcherTest.kt b/activity/ktx/src/androidTest/java/androidx/activity/OnBackPressedDispatcherTest.kt
new file mode 100644
index 0000000..53f808e4
--- /dev/null
+++ b/activity/ktx/src/androidTest/java/androidx/activity/OnBackPressedDispatcherTest.kt
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2019 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
+
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.LifecycleRegistry
+import androidx.test.annotation.UiThreadTest
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.SmallTest
+import com.google.common.truth.Truth.assertWithMessage
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@SmallTest
+@RunWith(AndroidJUnit4::class)
+class OnBackPressedDispatcherTest {
+
+ private lateinit var dispatcher: OnBackPressedDispatcher
+
+ @Before
+ fun setup() {
+ dispatcher = OnBackPressedDispatcher()
+ }
+
+ @UiThreadTest
+ @Test
+ fun testRegisterCallback() {
+ var count = 0
+ val callback = dispatcher.addCallback {
+ count++
+ }
+ assertWithMessage("Callback should be enabled by default")
+ .that(callback.isEnabled)
+ .isTrue()
+ assertWithMessage("Dispatcher should have an enabled callback")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isTrue()
+
+ dispatcher.onBackPressed()
+
+ assertWithMessage("Count should be incremented after onBackPressed")
+ .that(count)
+ .isEqualTo(1)
+ }
+
+ @UiThreadTest
+ @Test
+ fun testRegisterDisabledCallback() {
+ var count = 0
+ val callback = dispatcher.addCallback(enabled = false) {
+ count++
+ }
+ assertWithMessage("Callback should be disabled by default")
+ .that(callback.isEnabled)
+ .isFalse()
+ assertWithMessage("Dispatcher should not have an enabled callback")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isFalse()
+
+ callback.isEnabled = true
+
+ assertWithMessage("Dispatcher should have an enabled callback after setting isEnabled")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isTrue()
+ }
+
+ @UiThreadTest
+ @Test
+ fun testLifecycleCallback() {
+ val lifecycleOwner = object : LifecycleOwner {
+ val lifecycleRegistry = LifecycleRegistry(this)
+
+ override fun getLifecycle() = lifecycleRegistry
+ }
+ var count = 0
+ dispatcher.addCallback(lifecycleOwner) {
+ count++
+ }
+ assertWithMessage("Handler should return false if the Lifecycle isn't started")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isFalse()
+ dispatcher.onBackPressed()
+ assertWithMessage("Non-started callbacks shouldn't have their count incremented")
+ .that(count)
+ .isEqualTo(0)
+
+ // Now start the Lifecycle
+ lifecycleOwner.lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
+ dispatcher.onBackPressed()
+ assertWithMessage("Once the callbacks is started, the count should increment")
+ .that(count)
+ .isEqualTo(1)
+
+ // Now stop the Lifecycle
+ lifecycleOwner.lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
+ assertWithMessage("Handler should return false if the Lifecycle isn't started")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isFalse()
+ dispatcher.onBackPressed()
+ assertWithMessage("Non-started callbacks shouldn't have their count incremented")
+ .that(count)
+ .isEqualTo(1)
+ }
+
+ @UiThreadTest
+ @Test
+ fun testIsEnabledWithinCallback() {
+ var count = 0
+ val callback = dispatcher.addCallback {
+ count++
+ isEnabled = false
+ }
+ assertWithMessage("Callback should be enabled by default")
+ .that(callback.isEnabled)
+ .isTrue()
+ assertWithMessage("Dispatcher should have an enabled callback")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isTrue()
+
+ dispatcher.onBackPressed()
+
+ assertWithMessage("Count should be incremented after onBackPressed")
+ .that(count)
+ .isEqualTo(1)
+ assertWithMessage("Callback should be disabled after onBackPressed()")
+ .that(callback.isEnabled)
+ .isFalse()
+ assertWithMessage("Dispatcher should have no enabled callbacks")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isFalse()
+ }
+
+ @UiThreadTest
+ @Test
+ fun testRemoveCallbackWithinCallback() {
+ var count = 0
+ dispatcher.addCallback {
+ count++
+ removeCallback()
+ }
+
+ dispatcher.onBackPressed()
+
+ assertWithMessage("Count should be incremented after onBackPressed")
+ .that(count)
+ .isEqualTo(1)
+ assertWithMessage("Dispatcher should have no enabled callbacks after removeCallback")
+ .that(dispatcher.hasEnabledCallbacks())
+ .isFalse()
+ }
+}
diff --git a/activity/ktx/src/main/java/androidx/activity/OnBackPressedDispatcher.kt b/activity/ktx/src/main/java/androidx/activity/OnBackPressedDispatcher.kt
new file mode 100644
index 0000000..cfa08a5
--- /dev/null
+++ b/activity/ktx/src/main/java/androidx/activity/OnBackPressedDispatcher.kt
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2019 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
+
+import androidx.lifecycle.LifecycleOwner
+
+/**
+ * Create and add a new [OnBackPressedCallback] that calls [onBackPressed] in
+ * [OnBackPressedCallback.handleOnBackPressed].
+ *
+ * If an [owner] is specified, the callback will only be added when the Lifecycle is
+ * [androidx.lifecycle.Lifecycle.State.STARTED].
+ *
+ * A default [enabled] state can be supplied.
+ */
+fun OnBackPressedDispatcher.addCallback(
+ owner: LifecycleOwner? = null,
+ enabled: Boolean = true,
+ onBackPressed: OnBackPressedCallback.() -> Unit
+): OnBackPressedCallback {
+ val callback = object : OnBackPressedCallback(enabled) {
+ override fun handleOnBackPressed() {
+ onBackPressed()
+ }
+ }
+ if (owner != null) {
+ addCallback(owner, callback)
+ } else {
+ addCallback(callback)
+ }
+ return callback
+}