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