diff --git a/api/current.txt b/api/current.txt index 5b79660e..f0cd9f5a 100644 --- a/api/current.txt +++ b/api/current.txt @@ -1,5 +1,12 @@ package androidx.core.animation { + public final class Animatable2Kt { + ctor public Animatable2Kt(); + method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback addCallback(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1? onStart = "null", kotlin.jvm.functions.Function1? onEnd = "null"); + method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback doOnEnd(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1 action); + method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback doOnStart(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1 action); + } + public final class AnimatorKt { ctor public AnimatorKt(); method public static android.animation.Animator.AnimatorListener addListener(android.animation.Animator, kotlin.jvm.functions.Function1? onEnd = "null", kotlin.jvm.functions.Function1? onStart = "null", kotlin.jvm.functions.Function1? onCancel = "null", kotlin.jvm.functions.Function1? onRepeat = "null"); @@ -12,6 +19,13 @@ package androidx.core.animation { method public static android.animation.Animator.AnimatorListener doOnStart(android.animation.Animator, kotlin.jvm.functions.Function1 action); } + public final class LayoutTransitionKt { + ctor public LayoutTransitionKt(); + method public static void addListener(android.animation.LayoutTransition, kotlin.jvm.functions.Function4? onStart = "null", kotlin.jvm.functions.Function4? onEnd = "null"); + method public static void doOnEnd(android.animation.LayoutTransition, kotlin.jvm.functions.Function4 action); + method public static void doOnStart(android.animation.LayoutTransition, kotlin.jvm.functions.Function4 action); + } + } package androidx.core.content { diff --git a/src/androidTest/java/androidx/core/animation/Animatable2Test.kt b/src/androidTest/java/androidx/core/animation/Animatable2Test.kt new file mode 100644 index 00000000..de06a7a3 --- /dev/null +++ b/src/androidTest/java/androidx/core/animation/Animatable2Test.kt @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2018 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.core.animation + +import android.graphics.drawable.Animatable2 +import android.graphics.drawable.Animatable2.AnimationCallback +import android.support.test.runner.AndroidJUnit4 +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class Animatable2Test { + + private lateinit var animatable: Animatable2 + + @Before fun init() { + animatable = object : Animatable2 { + + private var callback: AnimationCallback? = null + + override fun isRunning(): Boolean = false + + override fun registerAnimationCallback(callback: AnimationCallback?) { + this.callback = callback + } + + override fun start() { + callback?.onAnimationStart(null) + } + + override fun stop() { + callback?.onAnimationEnd(null) + } + + override fun clearAnimationCallbacks() { + callback = null + } + + override fun unregisterAnimationCallback(callback: AnimationCallback?) = false + } + } + + @Test fun testDoOnStart() { + var called = false + animatable.doOnStart { called = true } + animatable.start() + assertTrue(called) + } + + @Test fun testDoOnEnd() { + var called = false + animatable.doOnEnd { called = true } + animatable.start() + animatable.stop() + assertTrue(called) + } +} \ No newline at end of file diff --git a/src/androidTest/java/androidx/core/animation/AnimatorTest.kt b/src/androidTest/java/androidx/core/animation/AnimatorTest.kt index de298a59..23242f24 100644 --- a/src/androidTest/java/androidx/core/animation/AnimatorTest.kt +++ b/src/androidTest/java/androidx/core/animation/AnimatorTest.kt @@ -30,6 +30,7 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class AnimatorTest { + private val context = InstrumentationRegistry.getContext() private val view = View(context) @@ -44,7 +45,6 @@ class AnimatorTest { animator.doOnStart { called = true } - animator.listeners.forEach { it.onAnimationStart(animator) } assertTrue(called) } diff --git a/src/androidTest/java/androidx/core/animation/LayoutTransitionTest.kt b/src/androidTest/java/androidx/core/animation/LayoutTransitionTest.kt new file mode 100644 index 00000000..9436ec43 --- /dev/null +++ b/src/androidTest/java/androidx/core/animation/LayoutTransitionTest.kt @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 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.core.animation + +import android.animation.LayoutTransition +import android.support.test.runner.AndroidJUnit4 +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class LayoutTransitionTest { + + private lateinit var transition: LayoutTransition + + @Before fun before() { + transition = LayoutTransition() + } + + @Test fun testDoOnStart() { + var called = false + transition.doOnStart { _, _, _, _ -> + called = true + } + transition.transitionListeners.forEach { + it.startTransition(transition, null, null, LayoutTransition.DISAPPEARING) + } + assertTrue(called) + } + + @Test fun testDoOnEnd() { + var called = false + transition.doOnEnd { _, _, _, _ -> + called = true + } + transition.transitionListeners.forEach { + it.startTransition(transition, null, null, LayoutTransition.DISAPPEARING) + it.endTransition(transition, null, null, LayoutTransition.DISAPPEARING) + } + assertTrue(called) + } +} \ No newline at end of file diff --git a/src/main/java/androidx/core/animation/Animatable2.kt b/src/main/java/androidx/core/animation/Animatable2.kt new file mode 100644 index 00000000..3007ab79 --- /dev/null +++ b/src/main/java/androidx/core/animation/Animatable2.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2018 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.core.animation + +import android.graphics.drawable.Animatable2 +import android.graphics.drawable.Drawable +import android.support.annotation.RequiresApi + +@RequiresApi(23) +fun Animatable2.doOnStart(action: (Drawable?) -> Unit) = addCallback(onStart = action) + +@RequiresApi(23) +fun Animatable2.doOnEnd(action: (Drawable?) -> Unit) = addCallback(onEnd = action) + +/** + * Add a callback to this Animatable2 using the provided actions. + */ +@RequiresApi(23) +fun Animatable2.addCallback( + onStart: ((drawable: Drawable?) -> Unit)? = null, + onEnd: ((drawable: Drawable?) -> Unit)? = null +): Animatable2.AnimationCallback { + val callback = object : Animatable2.AnimationCallback() { + override fun onAnimationStart(drawable: Drawable?) { + onStart?.invoke(drawable) + } + + override fun onAnimationEnd(drawable: Drawable?) { + onEnd?.invoke(drawable) + } + } + registerAnimationCallback(callback) + return callback +} \ No newline at end of file diff --git a/src/main/java/androidx/core/animation/LayoutTransition.kt b/src/main/java/androidx/core/animation/LayoutTransition.kt new file mode 100644 index 00000000..3cc3094c --- /dev/null +++ b/src/main/java/androidx/core/animation/LayoutTransition.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2018 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.core.animation + +import android.animation.LayoutTransition +import android.animation.LayoutTransition.TransitionListener +import android.view.View +import android.view.ViewGroup + +fun LayoutTransition.doOnStart(action: (LayoutTransition, ViewGroup, View, Int) -> Unit) = + addListener(onStart = action) + +fun LayoutTransition.doOnEnd(action: (LayoutTransition, ViewGroup, View, Int) -> Unit) = + addListener(onEnd = action) + +fun LayoutTransition.addListener( + onStart: ((LayoutTransition, ViewGroup, View, Int) -> Unit)? = null, + onEnd: ((LayoutTransition, ViewGroup, View, Int) -> Unit)? = null +) { + addTransitionListener( + object : TransitionListener { + override fun startTransition( + transition: LayoutTransition, + container: ViewGroup, + view: View, + transitionType: Int + ) { + onStart?.invoke(transition, container, view, transitionType) + } + + override fun endTransition( + transition: LayoutTransition, + container: ViewGroup, + view: View, + transitionType: Int + ) { + onEnd?.invoke(transition, container, view, transitionType) + } + } + ) +} \ No newline at end of file