HeadsUpTouchHelper.java revision 684a442b812a5e95d813700ffa2fd17ca72048a7
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
17package com.android.systemui.statusbar.phone;
18
19import android.content.Context;
20import android.view.MotionEvent;
21import android.view.ViewConfiguration;
22
23import com.android.systemui.Gefingerpoken;
24import com.android.systemui.statusbar.ExpandableNotificationRow;
25import com.android.systemui.statusbar.ExpandableView;
26import com.android.systemui.statusbar.policy.HeadsUpManager;
27import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
28
29/**
30 * A helper class to handle touches on the heads-up views.
31 */
32public class HeadsUpTouchHelper implements Gefingerpoken {
33
34    private HeadsUpManager mHeadsUpManager;
35    private NotificationStackScrollLayout mStackScroller;
36    private int mTrackingPointer;
37    private float mTouchSlop;
38    private float mInitialTouchX;
39    private float mInitialTouchY;
40    private boolean mTouchingHeadsUpView;
41    private boolean mTrackingHeadsUp;
42    private boolean mCollapseSnoozes;
43    private NotificationPanelView mPanel;
44    private ExpandableNotificationRow mPickedChild;
45
46    public HeadsUpTouchHelper(HeadsUpManager headsUpManager,
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
57    public boolean isTrackingHeadsUp() {
58        return mTrackingHeadsUp;
59    }
60
61    @Override
62    public boolean onInterceptTouchEvent(MotionEvent event) {
63        if (!mTouchingHeadsUpView && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
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);
78                ExpandableView child = mStackScroller.getChildAtPosition(x, y);
79                mTouchingHeadsUpView = false;
80                if (child instanceof ExpandableNotificationRow) {
81                    mPickedChild = (ExpandableNotificationRow) child;
82                    mTouchingHeadsUpView = mPickedChild.isHeadsUp() && mPickedChild.isPinned();
83                }
84                break;
85            case MotionEvent.ACTION_POINTER_UP:
86                final int upPointer = event.getPointerId(event.getActionIndex());
87                if (mTrackingPointer == upPointer) {
88                    // gesture is ongoing, find a new pointer to track
89                    final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
90                    mTrackingPointer = event.getPointerId(newIndex);
91                    mInitialTouchX = event.getX(newIndex);
92                    mInitialTouchY = event.getY(newIndex);
93                }
94                break;
95
96            case MotionEvent.ACTION_MOVE:
97                final float h = y - mInitialTouchY;
98                if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)) {
99                    setTrackingHeadsUp(true);
100                    mCollapseSnoozes = h < 0;
101                    mInitialTouchX = x;
102                    mInitialTouchY = y;
103                    int expandedHeight = mPickedChild.getActualHeight();
104                    mPanel.startExpandMotion(x, y, true /* startTracking */, expandedHeight);
105                    return true;
106                }
107                break;
108
109            case MotionEvent.ACTION_CANCEL:
110            case MotionEvent.ACTION_UP:
111                if (mPickedChild != null && mTouchingHeadsUpView) {
112                    // We may swallow this click if the heads up just came in.
113                    if (mHeadsUpManager.shouldSwallowClick(
114                            mPickedChild.getStatusBarNotification().getKey())) {
115                        endMotion();
116                        return true;
117                    }
118                }
119                endMotion();
120                break;
121        }
122        return false;
123    }
124
125    private void setTrackingHeadsUp(boolean tracking) {
126        mTrackingHeadsUp = tracking;
127        mHeadsUpManager.setTrackingHeadsUp(tracking);
128        mPanel.setTrackingHeadsUp(tracking);
129    }
130
131    public void notifyFling(boolean collapse) {
132        if (collapse && mCollapseSnoozes) {
133            mHeadsUpManager.snooze();
134        }
135        mCollapseSnoozes = false;
136    }
137
138    @Override
139    public boolean onTouchEvent(MotionEvent event) {
140        if (!mTrackingHeadsUp) {
141            return false;
142        }
143        switch (event.getActionMasked()) {
144            case MotionEvent.ACTION_UP:
145            case MotionEvent.ACTION_CANCEL:
146                endMotion();
147                setTrackingHeadsUp(false);
148                break;
149        }
150        return true;
151    }
152
153    private void endMotion() {
154        mTrackingPointer = -1;
155        mPickedChild = null;
156        mTouchingHeadsUpView = false;
157    }
158}
159