Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.statusbar.phone; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.view.MotionEvent; |
| 21 | import android.view.ViewConfiguration; |
| 22 | |
| 23 | import com.android.systemui.Gefingerpoken; |
| 24 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
| 25 | import com.android.systemui.statusbar.ExpandableView; |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 26 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 27 | import com.android.systemui.statusbar.stack.NotificationStackScrollLayout; |
| 28 | |
| 29 | /** |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 30 | * A helper class to handle touches on the heads-up views. |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 31 | */ |
| 32 | public class HeadsUpTouchHelper implements Gefingerpoken { |
| 33 | |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 34 | private HeadsUpManager mHeadsUpManager; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 35 | private NotificationStackScrollLayout mStackScroller; |
| 36 | private int mTrackingPointer; |
| 37 | private float mTouchSlop; |
| 38 | private float mInitialTouchX; |
| 39 | private float mInitialTouchY; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 40 | private boolean mTouchingHeadsUpView; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 41 | private boolean mTrackingHeadsUp; |
| 42 | private boolean mCollapseSnoozes; |
| 43 | private NotificationPanelView mPanel; |
| 44 | private ExpandableNotificationRow mPickedChild; |
| 45 | |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 46 | public HeadsUpTouchHelper(HeadsUpManager headsUpManager, |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 47 | NotificationStackScrollLayout stackScroller, |
| 48 | NotificationPanelView notificationPanelView) { |
| 49 | mHeadsUpManager = headsUpManager; |
| 50 | mStackScroller = stackScroller; |
| 51 | mPanel = notificationPanelView; |
| 52 | Context context = stackScroller.getContext(); |
| 53 | final ViewConfiguration configuration = ViewConfiguration.get(context); |
| 54 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 55 | } |
| 56 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 57 | public boolean isTrackingHeadsUp() { |
| 58 | return mTrackingHeadsUp; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean onInterceptTouchEvent(MotionEvent event) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 63 | if (!mTouchingHeadsUpView && event.getActionMasked() != MotionEvent.ACTION_DOWN) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | int pointerIndex = event.findPointerIndex(mTrackingPointer); |
| 67 | if (pointerIndex < 0) { |
| 68 | pointerIndex = 0; |
| 69 | mTrackingPointer = event.getPointerId(pointerIndex); |
| 70 | } |
| 71 | final float x = event.getX(pointerIndex); |
| 72 | final float y = event.getY(pointerIndex); |
| 73 | switch (event.getActionMasked()) { |
| 74 | case MotionEvent.ACTION_DOWN: |
| 75 | mInitialTouchY = y; |
| 76 | mInitialTouchX = x; |
| 77 | setTrackingHeadsUp(false); |
Selim Cinek | 35b7771 | 2015-05-11 17:00:39 -0700 | [diff] [blame] | 78 | ExpandableView child = mStackScroller.getChildAtRawPosition(x, y); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 79 | mTouchingHeadsUpView = false; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 80 | if (child instanceof ExpandableNotificationRow) { |
| 81 | mPickedChild = (ExpandableNotificationRow) child; |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 82 | mTouchingHeadsUpView = !mStackScroller.isExpanded() |
| 83 | && mPickedChild.isHeadsUp() && mPickedChild.isPinned(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 84 | } |
| 85 | break; |
| 86 | case MotionEvent.ACTION_POINTER_UP: |
| 87 | final int upPointer = event.getPointerId(event.getActionIndex()); |
| 88 | if (mTrackingPointer == upPointer) { |
| 89 | // gesture is ongoing, find a new pointer to track |
| 90 | final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; |
| 91 | mTrackingPointer = event.getPointerId(newIndex); |
| 92 | mInitialTouchX = event.getX(newIndex); |
| 93 | mInitialTouchY = event.getY(newIndex); |
| 94 | } |
| 95 | break; |
| 96 | |
| 97 | case MotionEvent.ACTION_MOVE: |
| 98 | final float h = y - mInitialTouchY; |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 99 | if (mTouchingHeadsUpView && Math.abs(h) > mTouchSlop |
| 100 | && Math.abs(h) > Math.abs(x - mInitialTouchX)) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 101 | setTrackingHeadsUp(true); |
| 102 | mCollapseSnoozes = h < 0; |
| 103 | mInitialTouchX = x; |
| 104 | mInitialTouchY = y; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 105 | int expandedHeight = mPickedChild.getActualHeight(); |
Selim Cinek | 3d395c6 | 2015-06-16 19:37:37 -0700 | [diff] [blame] | 106 | mPanel.setPanelScrimMinFraction((float) expandedHeight |
| 107 | / mPanel.getMaxPanelHeight()); |
Selim Cinek | 33d4614 | 2016-01-15 18:58:39 -0800 | [diff] [blame] | 108 | mPanel.startExpandMotion(x, y, true /* startTracking */, expandedHeight); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 109 | mPanel.startExpandingFromPeek(); |
Selim Cinek | def35a8 | 2016-05-03 15:52:51 -0700 | [diff] [blame] | 110 | // This call needs to be after the expansion start otherwise we will get a |
| 111 | // flicker of one frame as it's not expanded yet. |
| 112 | mHeadsUpManager.unpinAll(); |
Xiaohui Chen | 9528f43 | 2016-02-12 15:03:18 -0800 | [diff] [blame] | 113 | mPanel.clearNotificationEffects(); |
Selim Cinek | 31f708c | 2017-08-23 10:59:57 -0700 | [diff] [blame] | 114 | endMotion(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | break; |
| 118 | |
| 119 | case MotionEvent.ACTION_CANCEL: |
| 120 | case MotionEvent.ACTION_UP: |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 121 | if (mPickedChild != null && mTouchingHeadsUpView) { |
| 122 | // We may swallow this click if the heads up just came in. |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 123 | if (mHeadsUpManager.shouldSwallowClick( |
| 124 | mPickedChild.getStatusBarNotification().getKey())) { |
| 125 | endMotion(); |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | endMotion(); |
| 130 | break; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | private void setTrackingHeadsUp(boolean tracking) { |
| 136 | mTrackingHeadsUp = tracking; |
| 137 | mHeadsUpManager.setTrackingHeadsUp(tracking); |
| 138 | mPanel.setTrackingHeadsUp(tracking); |
| 139 | } |
| 140 | |
| 141 | public void notifyFling(boolean collapse) { |
| 142 | if (collapse && mCollapseSnoozes) { |
| 143 | mHeadsUpManager.snooze(); |
| 144 | } |
| 145 | mCollapseSnoozes = false; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean onTouchEvent(MotionEvent event) { |
| 150 | if (!mTrackingHeadsUp) { |
| 151 | return false; |
| 152 | } |
| 153 | switch (event.getActionMasked()) { |
| 154 | case MotionEvent.ACTION_UP: |
| 155 | case MotionEvent.ACTION_CANCEL: |
| 156 | endMotion(); |
| 157 | setTrackingHeadsUp(false); |
| 158 | break; |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | private void endMotion() { |
| 164 | mTrackingPointer = -1; |
| 165 | mPickedChild = null; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 166 | mTouchingHeadsUpView = false; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 167 | } |
| 168 | } |