1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software distributed under the
11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12 * KIND, either express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 package com.android.systemui.unfold.util
16
17 import com.android.systemui.dagger.qualifiers.Main
18 import com.android.systemui.unfold.UnfoldTransitionProgressProvider
19 import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener
20 import com.android.systemui.unfold.updates.FoldProvider
21 import com.android.systemui.unfold.updates.FoldProvider.FoldCallback
22 import java.util.concurrent.Executor
23
24 /**
25 * [UnfoldTransitionProgressProvider] that emits transition progress only when unfolding but not
26 * when folding, so we can play the animation only one way but not the other way.
27 */
28 class UnfoldOnlyProgressProvider(
29 foldProvider: FoldProvider,
30 @Main private val executor: Executor,
31 private val sourceProvider: UnfoldTransitionProgressProvider,
32 private val scopedProvider: ScopedUnfoldTransitionProgressProvider =
33 ScopedUnfoldTransitionProgressProvider(sourceProvider)
<lambda>null34 ) : UnfoldTransitionProgressProvider by scopedProvider {
35
36 private var isFolded = false
37
38 init {
39 foldProvider.registerCallback(FoldListener(), executor)
40 sourceProvider.addCallback(SourceTransitionListener())
41 }
42
43 private inner class SourceTransitionListener : TransitionProgressListener {
44 override fun onTransitionFinished() {
45 // Disable scoped progress provider after the first unfold animation, so fold animation
46 // will not be propagated. It will be re-enabled after folding so we can play
47 // the unfold animation again.
48 if (!isFolded) {
49 scopedProvider.setReadyToHandleTransition(false)
50 }
51 }
52 }
53
54 private inner class FoldListener : FoldCallback {
55 override fun onFoldUpdated(isFolded: Boolean) {
56 if (isFolded) {
57 scopedProvider.setReadyToHandleTransition(true)
58 }
59
60 this@UnfoldOnlyProgressProvider.isFolded = isFolded
61 }
62 }
63 }
64