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.statusbar.phone
17 
18 import android.view.View
19 import android.view.WindowManager
20 import com.android.systemui.shared.animation.UnfoldMoveFromCenterAnimator
21 import com.android.systemui.statusbar.phone.PhoneStatusBarViewController.StatusBarViewsCenterProvider
22 import com.android.systemui.unfold.SysUIUnfoldScope
23 import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener
24 import com.android.systemui.unfold.util.ScopedUnfoldTransitionProgressProvider
25 import javax.inject.Inject
26 
27 @SysUIUnfoldScope
28 class StatusBarMoveFromCenterAnimationController @Inject constructor(
29     private val progressProvider: ScopedUnfoldTransitionProgressProvider,
30     windowManager: WindowManager
31 ) {
32 
33     private val transitionListener = TransitionListener()
34     private val moveFromCenterAnimator = UnfoldMoveFromCenterAnimator(windowManager,
35         viewCenterProvider = StatusBarViewsCenterProvider())
36 
37     fun onViewsReady(viewsToAnimate: Array<View>) {
38         moveFromCenterAnimator.updateDisplayProperties()
39 
40         viewsToAnimate.forEach {
41             moveFromCenterAnimator.registerViewForAnimation(it)
42         }
43 
44         progressProvider.addCallback(transitionListener)
45     }
46 
47     fun onViewDetached() {
48         progressProvider.removeCallback(transitionListener)
49         moveFromCenterAnimator.clearRegisteredViews()
50     }
51 
52     fun onStatusBarWidthChanged() {
53         moveFromCenterAnimator.updateDisplayProperties()
54         moveFromCenterAnimator.updateViewPositions()
55     }
56 
57     private inner class TransitionListener : TransitionProgressListener {
58         override fun onTransitionProgress(progress: Float) {
59             moveFromCenterAnimator.onTransitionProgress(progress)
60         }
61 
62         override fun onTransitionFinished() {
63             // Reset translations when transition is stopped/cancelled
64             // (e.g. the transition could be cancelled mid-way when rotating the screen)
65             moveFromCenterAnimator.onTransitionProgress(1f)
66         }
67     }
68 }
69