• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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.quickstep.util.unfold
17 
18 import androidx.annotation.AnyThread
19 import androidx.annotation.FloatRange
20 import com.android.launcher3.util.Executors.MAIN_EXECUTOR
21 import com.android.systemui.unfold.UnfoldTransitionProgressProvider
22 import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener
23 import com.android.systemui.unfold.progress.IUnfoldTransitionListener
24 import com.android.systemui.unfold.progress.UnfoldRemoteFilter
25 
26 /** Receives unfold events from remote senders (System UI). */
27 class ProxyUnfoldTransitionProvider :
28     UnfoldTransitionProgressProvider, IUnfoldTransitionListener.Stub() {
29 
30     private val listeners: MutableSet<TransitionProgressListener> = mutableSetOf()
31     private val delegate = UnfoldRemoteFilter(ProcessedProgressListener())
32 
33     private var transitionStarted = false
34     var isActive = false
35         set(value) {
36             field = value
37             if (!value) {
38                 // Finish any active transition
39                 onTransitionFinished()
40             }
41         }
42 
43     @AnyThread
onTransitionStartednull44     override fun onTransitionStarted() {
45         MAIN_EXECUTOR.execute(delegate::onTransitionStarted)
46     }
47 
48     @AnyThread
onTransitionProgressnull49     override fun onTransitionProgress(progress: Float) {
50         MAIN_EXECUTOR.execute { delegate.onTransitionProgress(progress) }
51     }
52 
53     @AnyThread
onTransitionFinishednull54     override fun onTransitionFinished() {
55         MAIN_EXECUTOR.execute(delegate::onTransitionFinished)
56     }
57 
addCallbacknull58     override fun addCallback(listener: TransitionProgressListener) {
59         listeners += listener
60         if (transitionStarted) {
61             // Update the listener in case there was is an active transition
62             listener.onTransitionStarted()
63         }
64     }
65 
removeCallbacknull66     override fun removeCallback(listener: TransitionProgressListener) {
67         listeners -= listener
68         if (transitionStarted) {
69             // Finish the transition if it was already running
70             listener.onTransitionFinished()
71         }
72     }
73 
destroynull74     override fun destroy() {
75         listeners.clear()
76     }
77 
78     private inner class ProcessedProgressListener : TransitionProgressListener {
onTransitionStartednull79         override fun onTransitionStarted() {
80             if (!transitionStarted) {
81                 transitionStarted = true
82                 listeners.forEach(TransitionProgressListener::onTransitionStarted)
83             }
84         }
85 
onTransitionProgressnull86         override fun onTransitionProgress(@FloatRange(from = 0.0, to = 1.0) progress: Float) {
87             listeners.forEach { it.onTransitionProgress(progress) }
88         }
89 
onTransitionFinishednull90         override fun onTransitionFinished() {
91             if (transitionStarted) {
92                 transitionStarted = false
93                 listeners.forEach(TransitionProgressListener::onTransitionFinished)
94             }
95         }
96     }
97 }
98