• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 
17 package com.android.systemui.notifications.ui.composable
18 
19 import androidx.compose.animation.core.AnimationState
20 import androidx.compose.animation.core.DecayAnimationSpec
21 import androidx.compose.animation.core.animateDecay
22 import androidx.compose.foundation.gestures.FlingBehavior
23 import androidx.compose.foundation.gestures.ScrollScope
24 import androidx.compose.ui.MotionDurationScale
25 import com.android.systemui.scene.session.ui.composable.rememberSession
26 import kotlinx.coroutines.CancellationException
27 import kotlinx.coroutines.withContext
28 import kotlin.math.abs
29 
30 /**
31  * Fork of [androidx.compose.foundation.gestures.DefaultFlingBehavior] to allow us to use it with
32  * [rememberSession].
33  */
34 internal class NotificationScrimFlingBehavior(
35     private var flingDecay: DecayAnimationSpec<Float>,
36     private val motionDurationScale: MotionDurationScale = NotificationScrimMotionDurationScale
37 ) : FlingBehavior {
performFlingnull38     override suspend fun ScrollScope.performFling(initialVelocity: Float): Float {
39         // come up with the better threshold, but we need it since spline curve gives us NaNs
40         return withContext(motionDurationScale) {
41             if (abs(initialVelocity) > 1f) {
42                 var velocityLeft = initialVelocity
43                 var lastValue = 0f
44                 val animationState =
45                     AnimationState(
46                         initialValue = 0f,
47                         initialVelocity = initialVelocity,
48                     )
49                 try {
50                     animationState.animateDecay(flingDecay) {
51                         val delta = value - lastValue
52                         val consumed = scrollBy(delta)
53                         lastValue = value
54                         velocityLeft = this.velocity
55                         // avoid rounding errors and stop if anything is unconsumed
56                         if (abs(delta - consumed) > 0.5f) this.cancelAnimation()
57                     }
58                 } catch (exception: CancellationException) {
59                     velocityLeft = animationState.velocity
60                 }
61                 velocityLeft
62             } else {
63                 initialVelocity
64             }
65         }
66     }
67 }
68 
69 internal val NotificationScrimMotionDurationScale =
70     object : MotionDurationScale {
71         override val scaleFactor: Float
72             get() = 1f
73     }