StatusBarWindowView.java revision d7daab7b9e33cd56f2692b9ebea87ef21290c1ea
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.app.StatusBarManager;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.Rect;
24import android.util.AttributeSet;
25import android.view.KeyEvent;
26import android.view.MotionEvent;
27import android.view.View;
28import android.view.ViewRootImpl;
29import android.widget.FrameLayout;
30
31import com.android.systemui.ExpandHelper;
32import com.android.systemui.R;
33import com.android.systemui.statusbar.BaseStatusBar;
34import com.android.systemui.statusbar.DragDownHelper;
35import com.android.systemui.statusbar.StatusBarState;
36import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
37
38
39public class StatusBarWindowView extends FrameLayout {
40    public static final String TAG = "StatusBarWindowView";
41    public static final boolean DEBUG = BaseStatusBar.DEBUG;
42
43    private ExpandHelper mExpandHelper;
44    private DragDownHelper mDragDownHelper;
45    private NotificationStackScrollLayout mStackScrollLayout;
46    private NotificationPanelView mNotificationPanel;
47
48    PhoneStatusBar mService;
49
50    public StatusBarWindowView(Context context, AttributeSet attrs) {
51        super(context, attrs);
52        setMotionEventSplittingEnabled(false);
53        setWillNotDraw(!DEBUG);
54    }
55
56    @Override
57    protected boolean fitSystemWindows(Rect insets) {
58        if (getFitsSystemWindows()) {
59            setPadding(insets.left, insets.top, insets.right, 0);
60            insets.left = 0;
61            insets.top = 0;
62            insets.right = 0;
63        } else {
64            setPadding(0, 0, 0, 0);
65        }
66        return false;
67    }
68
69    @Override
70    protected void onAttachedToWindow () {
71        super.onAttachedToWindow();
72
73        mStackScrollLayout = (NotificationStackScrollLayout) findViewById(
74                R.id.notification_stack_scroller);
75        mNotificationPanel = (NotificationPanelView) findViewById(R.id.notification_panel);
76        int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_min_height);
77        int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_max_height);
78        mExpandHelper = new ExpandHelper(getContext(), mStackScrollLayout,
79                minHeight, maxHeight);
80        mExpandHelper.setEventSource(this);
81        mExpandHelper.setScrollAdapter(mStackScrollLayout);
82        mDragDownHelper = new DragDownHelper(getContext(), this, mStackScrollLayout, mService);
83
84        // We really need to be able to animate while window animations are going on
85        // so that activities may be started asynchronously from panel animations
86        final ViewRootImpl root = getViewRootImpl();
87        if (root != null) {
88            root.setDrawDuringWindowsAnimating(true);
89        }
90    }
91
92    @Override
93    public boolean dispatchKeyEvent(KeyEvent event) {
94        boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
95        switch (event.getKeyCode()) {
96            case KeyEvent.KEYCODE_BACK:
97                if (!down) {
98                    mService.onBackPressed();
99                }
100                return true;
101            case KeyEvent.KEYCODE_MENU:
102                if (!down) {
103                    return mService.onMenuPressed();
104                }
105        }
106        return super.dispatchKeyEvent(event);
107    }
108
109    @Override
110    public boolean onInterceptTouchEvent(MotionEvent ev) {
111        boolean intercept = false;
112        if (mNotificationPanel.isFullyExpanded()
113                && mStackScrollLayout.getVisibility() == View.VISIBLE
114                && (mService.getBarState() == StatusBarState.SHADE
115                        || (mService.getBarState() == StatusBarState.SHADE_LOCKED
116                                && !mService.isBouncerShowing()))) {
117            intercept = mExpandHelper.onInterceptTouchEvent(ev);
118        } else if (mNotificationPanel.isFullyExpanded()
119                && mStackScrollLayout.getVisibility() == View.VISIBLE
120                && mService.getBarState() == StatusBarState.KEYGUARD
121                && !mService.isBouncerShowing()) {
122            intercept = mDragDownHelper.onInterceptTouchEvent(ev);
123        }
124        if (!intercept) {
125            super.onInterceptTouchEvent(ev);
126        }
127        if (intercept) {
128            MotionEvent cancellation = MotionEvent.obtain(ev);
129            cancellation.setAction(MotionEvent.ACTION_CANCEL);
130            mStackScrollLayout.onInterceptTouchEvent(cancellation);
131            cancellation.recycle();
132        }
133        return intercept;
134    }
135
136    @Override
137    public boolean onTouchEvent(MotionEvent ev) {
138        boolean handled = false;
139        if (mNotificationPanel.isFullyExpanded()
140                && mService.getBarState() != StatusBarState.KEYGUARD) {
141            handled = mExpandHelper.onTouchEvent(ev);
142        } else if (mService.getBarState() == StatusBarState.KEYGUARD) {
143            handled = mDragDownHelper.onTouchEvent(ev);
144        }
145        if (!handled) {
146            handled = super.onTouchEvent(ev);
147        }
148        final int action = ev.getAction();
149        if (!handled && (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL)) {
150            mService.setInteracting(StatusBarManager.WINDOW_STATUS_BAR, false);
151        }
152        return handled;
153    }
154
155    @Override
156    public void onDraw(Canvas canvas) {
157        super.onDraw(canvas);
158        if (DEBUG) {
159            Paint pt = new Paint();
160            pt.setColor(0x80FFFF00);
161            pt.setStrokeWidth(12.0f);
162            pt.setStyle(Paint.Style.STROKE);
163            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), pt);
164        }
165    }
166
167    public void cancelExpandHelper() {
168        if (mExpandHelper != null) {
169            mExpandHelper.cancel();
170        }
171    }
172}
173
174