blob: e5dbcd3741698a458d2cd03f5daee7943b79456c [file] [log] [blame]
Selim Cinek3d6ae232019-01-04 14:14:33 -08001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.statusbar
18
19import android.animation.Animator
20import android.animation.AnimatorListenerAdapter
21import android.animation.ObjectAnimator
22import android.animation.ValueAnimator
23import android.content.Context
24import android.os.PowerManager
25import android.os.PowerManager.WAKE_REASON_GESTURE
26import android.os.SystemClock
27import android.view.MotionEvent
28import android.view.ViewConfiguration
29
30import com.android.systemui.Gefingerpoken
31import com.android.systemui.Interpolators
32import com.android.systemui.R
33import com.android.systemui.classifier.FalsingManager
34import com.android.systemui.statusbar.notification.NotificationWakeUpCoordinator
35import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow
36import com.android.systemui.statusbar.notification.row.ExpandableView
37import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout
38import com.android.systemui.statusbar.phone.ShadeController
39
40import javax.inject.Inject
41import javax.inject.Singleton
42import kotlin.math.max
43
44/**
45 * A utility class to enable the downward swipe on when pulsing.
46 */
47@Singleton
48class PulseExpansionHandler @Inject
49constructor(context: Context,
Selim Cinek624d6ca2019-02-19 15:39:08 -080050 private val mWakeUpCoordinator: NotificationWakeUpCoordinator) : Gefingerpoken {
Selim Cinek34518f62019-02-28 19:41:18 -080051 companion object {
52 private val RUBBERBAND_FACTOR_STATIC = 0.25f
53 private val SPRING_BACK_ANIMATION_LENGTH_MS = 375
54 }
Selim Cinek3d6ae232019-01-04 14:14:33 -080055 private val mPowerManager: PowerManager?
56 private var mShadeController: ShadeController? = null
57
58 private val mMinDragDistance: Int
59 private var mInitialTouchX: Float = 0.0f
60 private var mInitialTouchY: Float = 0.0f
61 var isExpanding: Boolean = false
62 private set
63 private val mTouchSlop: Float
64 private var mExpansionCallback: ExpansionCallback? = null
65 private lateinit var mStackScroller: NotificationStackScrollLayout
66 private val mTemp2 = IntArray(2)
67 private var mDraggedFarEnough: Boolean = false
68 private var mStartingChild: ExpandableView? = null
69 private val mFalsingManager: FalsingManager
70 private var mPulsing: Boolean = false
71 var isWakingToShadeLocked: Boolean = false
72 private set
73 private var mEmptyDragAmount: Float = 0.0f
74 private var mWakeUpHeight: Float = 0.0f
75 private var mReachedWakeUpHeight: Boolean = false
76
77 private val isFalseTouch: Boolean
78 get() = mFalsingManager.isFalseTouch
79
80 init {
81 mMinDragDistance = context.resources.getDimensionPixelSize(
82 R.dimen.keyguard_drag_down_min_distance)
83 mTouchSlop = ViewConfiguration.get(context).scaledTouchSlop.toFloat()
84 mFalsingManager = FalsingManager.getInstance(context)
85 mPowerManager = context.getSystemService(PowerManager::class.java)
86 }
87
88 override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
89 return maybeStartExpansion(event)
90 }
91
92 private fun maybeStartExpansion(event: MotionEvent): Boolean {
93 if (!mPulsing) {
94 return false
95 }
96 val x = event.x
97 val y = event.y
98
99 when (event.actionMasked) {
100 MotionEvent.ACTION_DOWN -> {
101 mDraggedFarEnough = false
102 isExpanding = false
103 mStartingChild = null
104 mInitialTouchY = y
105 mInitialTouchX = x
106 }
107
108 MotionEvent.ACTION_MOVE -> {
109 val h = y - mInitialTouchY
110 if (h > mTouchSlop && h > Math.abs(x - mInitialTouchX)) {
111 mFalsingManager.onStartExpandingFromPulse()
112 isExpanding = true
113 captureStartingChild(mInitialTouchX, mInitialTouchY)
114 mInitialTouchY = y
115 mInitialTouchX = x
116 mWakeUpHeight = mWakeUpCoordinator.getWakeUpHeight()
117 mReachedWakeUpHeight = false
118 return true
119 }
120 }
121 }
122 return false
123 }
124
125 override fun onTouchEvent(event: MotionEvent): Boolean {
126 if (!isExpanding) {
127 return maybeStartExpansion(event)
128 }
129 val y = event.y
130
131 when (event.actionMasked) {
132 MotionEvent.ACTION_MOVE -> updateExpansionHeight(y - mInitialTouchY)
133 MotionEvent.ACTION_UP -> if (!mFalsingManager.isUnlockingDisabled && !isFalseTouch) {
134 finishExpansion()
135 } else {
136 cancelExpansion()
137 }
138 MotionEvent.ACTION_CANCEL -> cancelExpansion()
139 }
140 return isExpanding
141 }
142
143 private fun finishExpansion() {
144 resetClock()
145 if (mStartingChild != null) {
146 setUserLocked(mStartingChild!!, false)
147 mStartingChild = null
148 }
149 isExpanding = false
150 isWakingToShadeLocked = true
151 mPowerManager!!.wakeUp(SystemClock.uptimeMillis(), WAKE_REASON_GESTURE,
152 "com.android.systemui:PULSEDRAG")
153 mShadeController!!.goToLockedShade(mStartingChild)
154 if (mStartingChild is ExpandableNotificationRow) {
155 val row = mStartingChild as ExpandableNotificationRow?
156 row!!.onExpandedByGesture(true /* userExpanded */)
157 }
158 }
159
160 private fun updateExpansionHeight(height: Float) {
161 var expansionHeight = max(height, 0.0f)
162 if (!mReachedWakeUpHeight && height > mWakeUpHeight) {
163 mReachedWakeUpHeight = true;
164 }
165 if (mStartingChild != null) {
166 val child = mStartingChild!!
167 val newHeight = Math.min((child.collapsedHeight + expansionHeight).toInt(),
168 child.maxContentHeight)
169 child.actualHeight = newHeight
170 expansionHeight = max(newHeight.toFloat(), expansionHeight)
171 } else {
Selim Cinek624d6ca2019-02-19 15:39:08 -0800172 val target = if (mReachedWakeUpHeight) mWakeUpHeight else 0.0f
173 mWakeUpCoordinator.setNotificationsVisibleForExpansion(height > target,
174 true /* animate */,
175 true /* increaseSpeed */)
Selim Cinek3d6ae232019-01-04 14:14:33 -0800176 expansionHeight = max(mWakeUpHeight, expansionHeight)
177 }
Selim Cinek5040f2e2019-02-14 18:22:42 -0800178 val emptyDragAmount = mWakeUpCoordinator.setPulseHeight(expansionHeight)
Selim Cinek3d6ae232019-01-04 14:14:33 -0800179 setEmptyDragAmount(emptyDragAmount * RUBBERBAND_FACTOR_STATIC)
180 }
181
182 private fun captureStartingChild(x: Float, y: Float) {
183 if (mStartingChild == null) {
184 mStartingChild = findView(x, y)
185 if (mStartingChild != null) {
186 setUserLocked(mStartingChild!!, true)
187 }
188 }
189 }
190
191 private fun setEmptyDragAmount(amount: Float) {
192 mEmptyDragAmount = amount
193 mExpansionCallback!!.setEmptyDragAmount(amount)
194 }
195
196 private fun reset(child: ExpandableView) {
197 if (child.actualHeight == child.collapsedHeight) {
198 setUserLocked(child, false)
199 return
200 }
201 val anim = ObjectAnimator.ofInt(child, "actualHeight",
202 child.actualHeight, child.collapsedHeight)
203 anim.interpolator = Interpolators.FAST_OUT_SLOW_IN
204 anim.duration = SPRING_BACK_ANIMATION_LENGTH_MS.toLong()
205 anim.addListener(object : AnimatorListenerAdapter() {
206 override fun onAnimationEnd(animation: Animator) {
207 setUserLocked(child, false)
208 }
209 })
210 anim.start()
211 }
212
213 private fun setUserLocked(child: ExpandableView, userLocked: Boolean) {
214 if (child is ExpandableNotificationRow) {
215 child.isUserLocked = userLocked
216 }
217 }
218
219 private fun resetClock() {
220 val anim = ValueAnimator.ofFloat(mEmptyDragAmount, 0f)
221 anim.interpolator = Interpolators.FAST_OUT_SLOW_IN
222 anim.duration = SPRING_BACK_ANIMATION_LENGTH_MS.toLong()
223 anim.addUpdateListener { animation -> setEmptyDragAmount(animation.animatedValue as Float) }
224 anim.start()
225 }
226
227 private fun cancelExpansion() {
228 mFalsingManager.onExpansionFromPulseStopped()
229 if (mStartingChild != null) {
230 reset(mStartingChild!!)
231 mStartingChild = null
232 } else {
233 resetClock()
234 }
Selim Cinek624d6ca2019-02-19 15:39:08 -0800235 mWakeUpCoordinator.setNotificationsVisibleForExpansion(false /* visible */,
236 true /* animate */,
237 false /* increaseSpeed */)
Selim Cinek3d6ae232019-01-04 14:14:33 -0800238 isExpanding = false
239 }
240
241 private fun findView(x: Float, y: Float): ExpandableView? {
242 var totalX = x
243 var totalY = y
244 mStackScroller.getLocationOnScreen(mTemp2)
245 totalX += mTemp2[0].toFloat()
246 totalY += mTemp2[1].toFloat()
247 val childAtRawPosition = mStackScroller.getChildAtRawPosition(totalX, totalY)
248 return if (childAtRawPosition != null && childAtRawPosition.isContentExpandable) {
249 childAtRawPosition
250 } else null
251 }
252
253 fun setUp(notificationStackScroller: NotificationStackScrollLayout,
254 expansionCallback: ExpansionCallback,
255 shadeController: ShadeController) {
256 mExpansionCallback = expansionCallback
257 mShadeController = shadeController
258 mStackScroller = notificationStackScroller
259 }
260
261 fun setPulsing(pulsing: Boolean) {
262 mPulsing = pulsing
Selim Cinek3d6ae232019-01-04 14:14:33 -0800263 }
264
265 fun onStartedWakingUp() {
266 isWakingToShadeLocked = false
267 }
268
269 interface ExpansionCallback {
270 fun setEmptyDragAmount(amount: Float)
271 }
Selim Cinek3d6ae232019-01-04 14:14:33 -0800272}