1 /*
2  * Copyright (C) 2021 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
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.systemui.unfold.util;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 
21 import com.android.systemui.unfold.UnfoldTransitionProgressProvider;
22 import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 /**
28  * Manages progress listeners that can have smaller lifespan than the unfold animation.
29  * Allows to limit getting transition updates to only when
30  * {@link ScopedUnfoldTransitionProgressProvider#setReadyToHandleTransition} is called
31  * with readyToHandleTransition = true
32  *
33  * If the transition has already started by the moment when the clients are ready to play
34  * the transition then it will report transition started callback and current animation progress.
35  */
36 public final class ScopedUnfoldTransitionProgressProvider implements
37         UnfoldTransitionProgressProvider, TransitionProgressListener {
38 
39     private static final float PROGRESS_UNSET = -1f;
40 
41     @Nullable
42     private UnfoldTransitionProgressProvider mSource;
43 
44     private final List<TransitionProgressListener> mListeners = new ArrayList<>();
45 
46     private boolean mIsReadyToHandleTransition;
47     private boolean mIsTransitionRunning;
48     private float mLastTransitionProgress = PROGRESS_UNSET;
49 
ScopedUnfoldTransitionProgressProvider()50     public ScopedUnfoldTransitionProgressProvider() {
51         this(null);
52     }
53 
ScopedUnfoldTransitionProgressProvider( @ullable UnfoldTransitionProgressProvider source)54     public ScopedUnfoldTransitionProgressProvider(
55             @Nullable UnfoldTransitionProgressProvider source) {
56         setSourceProvider(source);
57     }
58 
59     /**
60      * Sets the source for the unfold transition progress updates,
61      * it replaces current provider if it is already set
62      * @param provider transition provider that emits transition progress updates
63      */
setSourceProvider(@ullable UnfoldTransitionProgressProvider provider)64     public void setSourceProvider(@Nullable UnfoldTransitionProgressProvider provider) {
65         if (mSource != null) {
66             mSource.removeCallback(this);
67         }
68 
69         if (provider != null) {
70             mSource = provider;
71             mSource.addCallback(this);
72         } else {
73             mSource = null;
74         }
75     }
76 
77     /**
78      * Allows to notify this provide whether the listeners can play the transition or not.
79      * Call this method with readyToHandleTransition = true when all listeners
80      * are ready to consume the transition progress events.
81      * Call it with readyToHandleTransition = false when listeners can't process the events.
82      */
setReadyToHandleTransition(boolean isReadyToHandleTransition)83     public void setReadyToHandleTransition(boolean isReadyToHandleTransition) {
84         if (mIsTransitionRunning) {
85             if (isReadyToHandleTransition) {
86                 mListeners.forEach(TransitionProgressListener::onTransitionStarted);
87 
88                 if (mLastTransitionProgress != PROGRESS_UNSET) {
89                     mListeners.forEach(listener ->
90                             listener.onTransitionProgress(mLastTransitionProgress));
91                 }
92             } else {
93                 mIsTransitionRunning = false;
94                 mListeners.forEach(TransitionProgressListener::onTransitionFinished);
95             }
96         }
97 
98         mIsReadyToHandleTransition = isReadyToHandleTransition;
99     }
100 
101     @Override
addCallback(@onNull TransitionProgressListener listener)102     public void addCallback(@NonNull TransitionProgressListener listener) {
103         mListeners.add(listener);
104     }
105 
106     @Override
removeCallback(@onNull TransitionProgressListener listener)107     public void removeCallback(@NonNull TransitionProgressListener listener) {
108         mListeners.remove(listener);
109     }
110 
111     @Override
destroy()112     public void destroy() {
113         mSource.removeCallback(this);
114     }
115 
116     @Override
onTransitionStarted()117     public void onTransitionStarted() {
118         this.mIsTransitionRunning = true;
119         if (mIsReadyToHandleTransition) {
120             mListeners.forEach(TransitionProgressListener::onTransitionStarted);
121         }
122     }
123 
124     @Override
onTransitionProgress(float progress)125     public void onTransitionProgress(float progress) {
126         if (mIsReadyToHandleTransition) {
127             mListeners.forEach(listener -> listener.onTransitionProgress(progress));
128         }
129 
130         mLastTransitionProgress = progress;
131     }
132 
133     @Override
onTransitionFinished()134     public void onTransitionFinished() {
135         if (mIsReadyToHandleTransition) {
136             mListeners.forEach(TransitionProgressListener::onTransitionFinished);
137         }
138 
139         mIsTransitionRunning = false;
140         mLastTransitionProgress = PROGRESS_UNSET;
141     }
142 }
143