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.content.res.Resources;
21import android.graphics.Canvas;
22import android.graphics.drawable.Drawable;
23import android.util.AttributeSet;
24import android.util.Slog;
25import android.view.MotionEvent;
26import android.view.View;
27
28import com.android.systemui.R;
29import com.android.systemui.statusbar.GestureRecorder;
30
31public class NotificationPanelView extends PanelView {
32
33    Drawable mHandleBar;
34    int mHandleBarHeight;
35    View mHandleView;
36    int mFingers;
37    PhoneStatusBar mStatusBar;
38    boolean mOkToFlip;
39
40    public NotificationPanelView(Context context, AttributeSet attrs) {
41        super(context, attrs);
42    }
43
44    public void setStatusBar(PhoneStatusBar bar) {
45        mStatusBar = bar;
46    }
47
48    @Override
49    protected void onFinishInflate() {
50        super.onFinishInflate();
51
52        Resources resources = getContext().getResources();
53        mHandleBar = resources.getDrawable(R.drawable.status_bar_close);
54        mHandleBarHeight = resources.getDimensionPixelSize(R.dimen.close_handle_height);
55        mHandleView = findViewById(R.id.handle);
56
57        setContentDescription(resources.getString(R.string.accessibility_desc_notification_shade));
58    }
59
60    @Override
61    public void fling(float vel, boolean always) {
62        GestureRecorder gr = ((PhoneStatusBarView) mBar).mBar.getGestureRecorder();
63        if (gr != null) {
64            gr.tag(
65                "fling " + ((vel > 0) ? "open" : "closed"),
66                "notifications,v=" + vel);
67        }
68        super.fling(vel, always);
69    }
70
71    // We draw the handle ourselves so that it's always glued to the bottom of the window.
72    @Override
73    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
74        super.onLayout(changed, left, top, right, bottom);
75        if (changed) {
76            final int pl = getPaddingLeft();
77            final int pr = getPaddingRight();
78            mHandleBar.setBounds(pl, 0, getWidth() - pr, (int) mHandleBarHeight);
79        }
80    }
81
82    @Override
83    public void draw(Canvas canvas) {
84        super.draw(canvas);
85        final int off = (int) (getHeight() - mHandleBarHeight - getPaddingBottom());
86        canvas.translate(0, off);
87        mHandleBar.setState(mHandleView.getDrawableState());
88        mHandleBar.draw(canvas);
89        canvas.translate(0, -off);
90    }
91
92    @Override
93    public boolean onTouchEvent(MotionEvent event) {
94        if (PhoneStatusBar.SETTINGS_DRAG_SHORTCUT && mStatusBar.mHasFlipSettings) {
95            switch (event.getActionMasked()) {
96                case MotionEvent.ACTION_DOWN:
97                    mOkToFlip = getExpandedHeight() == 0;
98                    break;
99                case MotionEvent.ACTION_POINTER_DOWN:
100                    if (mOkToFlip) {
101                        float miny = event.getY(0);
102                        float maxy = miny;
103                        for (int i=1; i<event.getPointerCount(); i++) {
104                            final float y = event.getY(i);
105                            if (y < miny) miny = y;
106                            if (y > maxy) maxy = y;
107                        }
108                        if (maxy - miny < mHandleBarHeight) {
109                            if (getMeasuredHeight() < mHandleBarHeight) {
110                                mStatusBar.switchToSettings();
111                            } else {
112                                mStatusBar.flipToSettings();
113                            }
114                            mOkToFlip = false;
115                        }
116                    }
117                    break;
118            }
119        }
120        return mHandleView.dispatchTouchEvent(event);
121    }
122}
123