NotificationPanelView.java revision be565dfc1c17b7ddafa9753851b8f82849fd3f42
1/*
2 * Copyright (C) 2012 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.util.AttributeSet;
21import android.view.MotionEvent;
22import android.view.View;
23import android.view.accessibility.AccessibilityEvent;
24
25import com.android.systemui.R;
26import com.android.systemui.statusbar.ExpandableView;
27import com.android.systemui.statusbar.GestureRecorder;
28import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
29
30public class NotificationPanelView extends PanelView implements
31        ExpandableView.OnHeightChangedListener {
32    public static final boolean DEBUG_GESTURES = true;
33
34    PhoneStatusBar mStatusBar;
35    private View mHeader;
36    private View mKeyguardStatusView;
37
38    private NotificationStackScrollLayout mNotificationStackScroller;
39    private int[] mTempLocation = new int[2];
40    private int[] mTempChildLocation = new int[2];
41    private View mNotificationParent;
42    private boolean mTrackingSettings;
43    private float mExpandedHeight = -1;
44
45    public NotificationPanelView(Context context, AttributeSet attrs) {
46        super(context, attrs);
47    }
48
49    public void setStatusBar(PhoneStatusBar bar) {
50        if (mStatusBar != null) {
51            mStatusBar.setOnFlipRunnable(null);
52        }
53        mStatusBar = bar;
54        if (bar != null) {
55            mStatusBar.setOnFlipRunnable(new Runnable() {
56                @Override
57                public void run() {
58                    requestPanelHeightUpdate();
59                }
60            });
61        }
62    }
63
64    @Override
65    protected void onFinishInflate() {
66        super.onFinishInflate();
67
68        mHeader = findViewById(R.id.header);
69        mKeyguardStatusView = findViewById(R.id.keyguard_status_view);
70        mNotificationStackScroller = (NotificationStackScrollLayout)
71                findViewById(R.id.notification_stack_scroller);
72        mNotificationStackScroller.setOnHeightChangedListener(this);
73        mNotificationParent = findViewById(R.id.notification_container_parent);
74    }
75
76    @Override
77    public void fling(float vel, boolean always) {
78        GestureRecorder gr = ((PhoneStatusBarView) mBar).mBar.getGestureRecorder();
79        if (gr != null) {
80            gr.tag(
81                "fling " + ((vel > 0) ? "open" : "closed"),
82                "notifications,v=" + vel);
83        }
84        super.fling(vel, always);
85    }
86
87    @Override
88    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
89        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
90            event.getText()
91                    .add(getContext().getString(R.string.accessibility_desc_notification_shade));
92            return true;
93        }
94
95        return super.dispatchPopulateAccessibilityEvent(event);
96    }
97
98    /**
99     * Gets the relative position of a view on the screen in regard to this view.
100     *
101     * @param requestedView the view we want to find the relative position for
102     * @return
103     */
104    private int getRelativeTop(View requestedView) {
105        getLocationOnScreen(mTempLocation);
106        requestedView.getLocationOnScreen(mTempChildLocation);
107        return mTempChildLocation[1] - mTempLocation[1];
108    }
109
110    @Override
111    public boolean onInterceptTouchEvent(MotionEvent event) {
112        // intercept for quick settings
113        if (event.getAction() == MotionEvent.ACTION_DOWN) {
114            final View target = mStatusBar.isOnKeyguard() ?  mKeyguardStatusView : mHeader;
115            final boolean inTarget = PhoneStatusBar.inBounds(target, event, true);
116            if (inTarget && !isInSettings()) {
117                mTrackingSettings = true;
118                return true;
119            }
120            if (!inTarget && isInSettings()) {
121                mTrackingSettings = true;
122                return true;
123            }
124        }
125        return super.onInterceptTouchEvent(event);
126    }
127
128    @Override
129    public boolean onTouchEvent(MotionEvent event) {
130        // TODO: Handle doublefinger swipe to notifications again. Look at history for a reference
131        // implementation.
132        if (mTrackingSettings) {
133            mStatusBar.onSettingsEvent(event);
134            if (event.getAction() == MotionEvent.ACTION_UP
135                    || event.getAction() == MotionEvent.ACTION_CANCEL) {
136                mTrackingSettings = false;
137            }
138            return true;
139        }
140        if (isInSettings()) {
141            return true;
142        }
143        return super.onTouchEvent(event);
144    }
145
146    @Override
147    protected boolean isScrolledToBottom() {
148        if (!isInSettings()) {
149            return mNotificationStackScroller.isScrolledToBottom();
150        }
151        return super.isScrolledToBottom();
152    }
153
154    @Override
155    protected int getMaxPanelHeight() {
156        if (!isInSettings()) {
157            int maxPanelHeight = super.getMaxPanelHeight();
158            int emptyBottomMargin = mNotificationStackScroller.getEmptyBottomMargin();
159            return maxPanelHeight - emptyBottomMargin;
160        }
161        return super.getMaxPanelHeight();
162    }
163
164    private boolean isInSettings() {
165        return mStatusBar != null && mStatusBar.isFlippedToSettings();
166    }
167
168    @Override
169    protected void onHeightUpdated(float expandedHeight) {
170        updateNotificationStackHeight(expandedHeight);
171    }
172
173    /**
174     * Update the height of the {@link #mNotificationStackScroller} to the new expanded height.
175     * This is much more efficient than doing it over the layout pass.
176     *
177     * @param expandedHeight the new expanded height
178     */
179    private void updateNotificationStackHeight(float expandedHeight) {
180        if (mExpandedHeight == expandedHeight) return;
181        mExpandedHeight = expandedHeight;
182        mNotificationStackScroller.setIsExpanded(expandedHeight > 0.0f);
183        float childOffset = getRelativeTop(mNotificationStackScroller)
184                - mNotificationParent.getTranslationY();
185        int newStackHeight = (int) (expandedHeight - childOffset);
186        int itemHeight = mNotificationStackScroller.getItemHeight();
187        int bottomStackPeekSize = mNotificationStackScroller.getBottomStackPeekSize();
188        int minStackHeight = itemHeight + bottomStackPeekSize;
189        if (newStackHeight >= minStackHeight) {
190            mNotificationParent.setTranslationY(0);
191            mNotificationStackScroller.setCurrentStackHeight(newStackHeight);
192        } else {
193
194            // We did not reach the position yet where we actually start growing,
195            // so we translate the stack upwards.
196            int translationY = (newStackHeight - minStackHeight);
197            // A slight parallax effect is introduced in order for the stack to catch up with
198            // the top card.
199            float partiallyThere = (float) newStackHeight / minStackHeight;
200            partiallyThere = Math.max(0, partiallyThere);
201            translationY += (1 - partiallyThere) * bottomStackPeekSize;
202            mNotificationParent.setTranslationY(translationY);
203            mNotificationStackScroller.setCurrentStackHeight(
204                    (int) (expandedHeight - (childOffset + translationY)));
205        }
206    }
207
208    @Override
209    protected int getDesiredMeasureHeight() {
210        return mMaxPanelHeight;
211    }
212
213    @Override
214    protected void onExpandingStarted() {
215        super.onExpandingStarted();
216        mNotificationStackScroller.onExpansionStarted();
217    }
218
219    @Override
220    protected void onExpandingFinished() {
221        super.onExpandingFinished();
222        mNotificationStackScroller.onExpansionStopped();
223    }
224
225    @Override
226    public void onHeightChanged(ExpandableView view) {
227        requestPanelHeightUpdate();
228    }
229}
230