From 8c7ad03ea8c50b349044650e7309b8ca36b74659 Mon Sep 17 00:00:00 2001 From: Nicolo' Mazzucato Date: Thu, 2 Dec 2021 14:07:39 +0100 Subject: [PATCH] Convert ScopedUnfoldTransitionProgressProvider.java to kotlin Test: NaturalRotationUnfoldProgressProviderTest Bug: 208791440 Change-Id: I6b2f0fba5ab72aa1561403ed19ff71154f602d9e --- ...copedUnfoldTransitionProgressProvider.java | 142 ------------------ .../ScopedUnfoldTransitionProgressProvider.kt | 119 +++++++++++++++ 2 files changed, 119 insertions(+), 142 deletions(-) delete mode 100644 packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.java create mode 100644 packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt diff --git a/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.java b/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.java deleted file mode 100644 index 543232da303e5..0000000000000 --- a/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) 2021 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 com.android.systemui.unfold.util; - -import android.annotation.NonNull; -import android.annotation.Nullable; - -import com.android.systemui.unfold.UnfoldTransitionProgressProvider; -import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener; - -import java.util.ArrayList; -import java.util.List; - -/** - * Manages progress listeners that can have smaller lifespan than the unfold animation. - * Allows to limit getting transition updates to only when - * {@link ScopedUnfoldTransitionProgressProvider#setReadyToHandleTransition} is called - * with readyToHandleTransition = true - * - * If the transition has already started by the moment when the clients are ready to play - * the transition then it will report transition started callback and current animation progress. - */ -public final class ScopedUnfoldTransitionProgressProvider implements - UnfoldTransitionProgressProvider, TransitionProgressListener { - - private static final float PROGRESS_UNSET = -1f; - - @Nullable - private UnfoldTransitionProgressProvider mSource; - - private final List mListeners = new ArrayList<>(); - - private boolean mIsReadyToHandleTransition; - private boolean mIsTransitionRunning; - private float mLastTransitionProgress = PROGRESS_UNSET; - - public ScopedUnfoldTransitionProgressProvider() { - this(null); - } - - public ScopedUnfoldTransitionProgressProvider( - @Nullable UnfoldTransitionProgressProvider source) { - setSourceProvider(source); - } - - /** - * Sets the source for the unfold transition progress updates, - * it replaces current provider if it is already set - * @param provider transition provider that emits transition progress updates - */ - public void setSourceProvider(@Nullable UnfoldTransitionProgressProvider provider) { - if (mSource != null) { - mSource.removeCallback(this); - } - - if (provider != null) { - mSource = provider; - mSource.addCallback(this); - } else { - mSource = null; - } - } - - /** - * Allows to notify this provide whether the listeners can play the transition or not. - * Call this method with readyToHandleTransition = true when all listeners - * are ready to consume the transition progress events. - * Call it with readyToHandleTransition = false when listeners can't process the events. - */ - public void setReadyToHandleTransition(boolean isReadyToHandleTransition) { - if (mIsTransitionRunning) { - if (isReadyToHandleTransition) { - mListeners.forEach(TransitionProgressListener::onTransitionStarted); - - if (mLastTransitionProgress != PROGRESS_UNSET) { - mListeners.forEach(listener -> - listener.onTransitionProgress(mLastTransitionProgress)); - } - } else { - mIsTransitionRunning = false; - mListeners.forEach(TransitionProgressListener::onTransitionFinished); - } - } - - mIsReadyToHandleTransition = isReadyToHandleTransition; - } - - @Override - public void addCallback(@NonNull TransitionProgressListener listener) { - mListeners.add(listener); - } - - @Override - public void removeCallback(@NonNull TransitionProgressListener listener) { - mListeners.remove(listener); - } - - @Override - public void destroy() { - mSource.removeCallback(this); - } - - @Override - public void onTransitionStarted() { - this.mIsTransitionRunning = true; - if (mIsReadyToHandleTransition) { - mListeners.forEach(TransitionProgressListener::onTransitionStarted); - } - } - - @Override - public void onTransitionProgress(float progress) { - if (mIsReadyToHandleTransition) { - mListeners.forEach(listener -> listener.onTransitionProgress(progress)); - } - - mLastTransitionProgress = progress; - } - - @Override - public void onTransitionFinished() { - if (mIsReadyToHandleTransition) { - mListeners.forEach(TransitionProgressListener::onTransitionFinished); - } - - mIsTransitionRunning = false; - mLastTransitionProgress = PROGRESS_UNSET; - } -} diff --git a/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt b/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt new file mode 100644 index 0000000000000..22698a8537265 --- /dev/null +++ b/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2021 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 com.android.systemui.unfold.util + +import com.android.systemui.unfold.UnfoldTransitionProgressProvider +import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener + +/** + * Manages progress listeners that can have smaller lifespan than the unfold animation. + * Allows to limit getting transition updates to only when + * [ScopedUnfoldTransitionProgressProvider.setReadyToHandleTransition] is called + * with readyToHandleTransition = true + * + * If the transition has already started by the moment when the clients are ready to play + * the transition then it will report transition started callback and current animation progress. + */ +class ScopedUnfoldTransitionProgressProvider @JvmOverloads constructor( + source: UnfoldTransitionProgressProvider? = null +) : UnfoldTransitionProgressProvider, TransitionProgressListener { + + private var source: UnfoldTransitionProgressProvider? = null + + private val listeners: MutableList = mutableListOf() + + private var isReadyToHandleTransition = false + private var isTransitionRunning = false + private var lastTransitionProgress = PROGRESS_UNSET + + init { + setSourceProvider(source) + } + /** + * Sets the source for the unfold transition progress updates, + * it replaces current provider if it is already set + * @param provider transition provider that emits transition progress updates + */ + fun setSourceProvider(provider: UnfoldTransitionProgressProvider?) { + source?.removeCallback(this) + + if (provider != null) { + source = provider + provider.addCallback(this) + } else { + source = null + } + } + + /** + * Allows to notify this provide whether the listeners can play the transition or not. + * Call this method with readyToHandleTransition = true when all listeners + * are ready to consume the transition progress events. + * Call it with readyToHandleTransition = false when listeners can't process the events. + */ + fun setReadyToHandleTransition(isReadyToHandleTransition: Boolean) { + if (isTransitionRunning) { + if (isReadyToHandleTransition) { + listeners.forEach { it.onTransitionStarted() } + if (lastTransitionProgress != PROGRESS_UNSET) { + listeners.forEach { it.onTransitionProgress(lastTransitionProgress) } + } + } else { + isTransitionRunning = false + listeners.forEach { it.onTransitionFinished() } + } + } + this.isReadyToHandleTransition = isReadyToHandleTransition + } + + override fun addCallback(listener: TransitionProgressListener) { + listeners += listener + } + + override fun removeCallback(listener: TransitionProgressListener) { + listeners -= listener + } + + override fun destroy() { + source?.removeCallback(this) + } + + override fun onTransitionStarted() { + isTransitionRunning = true + if (isReadyToHandleTransition) { + listeners.forEach { it.onTransitionStarted() } + } + } + + override fun onTransitionProgress(progress: Float) { + if (isReadyToHandleTransition) { + listeners.forEach { it.onTransitionProgress(progress) } + } + lastTransitionProgress = progress + } + + override fun onTransitionFinished() { + if (isReadyToHandleTransition) { + listeners.forEach { it.onTransitionFinished() } + } + isTransitionRunning = false + lastTransitionProgress = PROGRESS_UNSET + } + + companion object { + private const val PROGRESS_UNSET = -1f + } +}