blob: cfd33589588c9bd1a5199f05f1a6fb705c840f9a [file] [log] [blame]
Daniel Sandlerc4f2a562012-05-04 11:55:46 -04001/*
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
John Spurlock257f2832013-09-21 18:41:53 -040019import android.app.StatusBarManager;
Daniel Sandlerc4f2a562012-05-04 11:55:46 -040020import android.content.Context;
Adrian Roos2f2bd9a2015-06-04 18:11:14 -070021import android.content.res.TypedArray;
Daniel Sandler198a0302012-08-17 16:04:31 -040022import android.graphics.Canvas;
23import android.graphics.Paint;
Selim Cineka0fad3b2014-09-19 17:20:05 +020024import android.graphics.PorterDuff;
25import android.graphics.PorterDuffXfermode;
John Spurlockfba91202014-04-22 12:58:26 -040026import android.graphics.Rect;
John Spurlock0b99ea92014-10-01 15:32:22 -040027import android.media.session.MediaSessionLegacyHelper;
Selim Cineka0fad3b2014-09-19 17:20:05 +020028import android.os.IBinder;
Daniel Sandlerc4f2a562012-05-04 11:55:46 -040029import android.util.AttributeSet;
30import android.view.KeyEvent;
Chris Wren5de6e942012-05-16 14:22:21 -040031import android.view.MotionEvent;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -070032import android.view.View;
Daniel Sandlerb10d8852013-05-08 15:57:06 -040033import android.view.ViewRootImpl;
Selim Cineka0fad3b2014-09-19 17:20:05 +020034import android.view.WindowManager;
35import android.view.WindowManagerGlobal;
Daniel Sandlerc4f2a562012-05-04 11:55:46 -040036import android.widget.FrameLayout;
Daniel Sandlerc4f2a562012-05-04 11:55:46 -040037
Chris Wren5de6e942012-05-16 14:22:21 -040038import com.android.systemui.R;
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -070039import com.android.systemui.classifier.FalsingManager;
Daniel Sandler198a0302012-08-17 16:04:31 -040040import com.android.systemui.statusbar.BaseStatusBar;
Jorim Jaggiecbab362014-04-23 16:13:15 +020041import com.android.systemui.statusbar.DragDownHelper;
42import com.android.systemui.statusbar.StatusBarState;
Selim Cinekfab078b2014-03-27 22:45:58 +010043import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
Chris Wren5de6e942012-05-16 14:22:21 -040044
Daniel Sandlerc4f2a562012-05-04 11:55:46 -040045
Jorim Jaggiecbab362014-04-23 16:13:15 +020046public class StatusBarWindowView extends FrameLayout {
Daniel Sandler198a0302012-08-17 16:04:31 -040047 public static final String TAG = "StatusBarWindowView";
48 public static final boolean DEBUG = BaseStatusBar.DEBUG;
Chris Wren5de6e942012-05-16 14:22:21 -040049
Jorim Jaggiecbab362014-04-23 16:13:15 +020050 private DragDownHelper mDragDownHelper;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010051 private NotificationStackScrollLayout mStackScrollLayout;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -070052 private NotificationPanelView mNotificationPanel;
Adrian Roos9dc32092014-09-02 23:34:10 +020053 private View mBrightnessMirror;
Chris Wren5de6e942012-05-16 14:22:21 -040054
Adrian Roos2f2bd9a2015-06-04 18:11:14 -070055 private int mRightInset = 0;
56
Selim Cinek4e6b2d32015-06-25 20:15:33 -040057 private PhoneStatusBar mService;
Selim Cineka0fad3b2014-09-19 17:20:05 +020058 private final Paint mTransparentSrcPaint = new Paint();
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -070059 private FalsingManager mFalsingManager;
Daniel Sandlerc4f2a562012-05-04 11:55:46 -040060
61 public StatusBarWindowView(Context context, AttributeSet attrs) {
62 super(context, attrs);
Chris Wren5de6e942012-05-16 14:22:21 -040063 setMotionEventSplittingEnabled(false);
Selim Cineka0fad3b2014-09-19 17:20:05 +020064 mTransparentSrcPaint.setColor(0);
65 mTransparentSrcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -070066 mFalsingManager = FalsingManager.getInstance(context);
Chris Wren5de6e942012-05-16 14:22:21 -040067 }
68
69 @Override
John Spurlockfba91202014-04-22 12:58:26 -040070 protected boolean fitSystemWindows(Rect insets) {
71 if (getFitsSystemWindows()) {
Adrian Roos2f2bd9a2015-06-04 18:11:14 -070072 boolean paddingChanged = insets.left != getPaddingLeft()
Jorim Jaggi416493b2014-09-13 03:57:32 +020073 || insets.top != getPaddingTop()
Jorim Jaggi416493b2014-09-13 03:57:32 +020074 || insets.bottom != getPaddingBottom();
Jorim Jaggiaa806142015-05-20 18:04:16 -070075
Adrian Roos2f2bd9a2015-06-04 18:11:14 -070076 // Super-special right inset handling, because scrims and backdrop need to ignore it.
77 if (insets.right != mRightInset) {
78 mRightInset = insets.right;
79 applyMargins();
80 }
81 // Drop top inset, apply left inset and pass through bottom inset.
82 if (paddingChanged) {
83 setPadding(insets.left, 0, 0, 0);
Jorim Jaggi416493b2014-09-13 03:57:32 +020084 }
Jorim Jaggid7daab72014-05-06 22:22:20 +020085 insets.left = 0;
86 insets.top = 0;
87 insets.right = 0;
John Spurlockfba91202014-04-22 12:58:26 -040088 } else {
Adrian Roose49e4092015-06-10 18:50:18 -070089 if (mRightInset != 0) {
90 mRightInset = 0;
91 applyMargins();
92 }
Jorim Jaggi416493b2014-09-13 03:57:32 +020093 boolean changed = getPaddingLeft() != 0
94 || getPaddingRight() != 0
95 || getPaddingTop() != 0
96 || getPaddingBottom() != 0;
97 if (changed) {
98 setPadding(0, 0, 0, 0);
99 }
Jorim Jaggiaa806142015-05-20 18:04:16 -0700100 insets.top = 0;
John Spurlockfba91202014-04-22 12:58:26 -0400101 }
Jorim Jaggid7daab72014-05-06 22:22:20 +0200102 return false;
John Spurlockfba91202014-04-22 12:58:26 -0400103 }
104
Adrian Roos2f2bd9a2015-06-04 18:11:14 -0700105 private void applyMargins() {
106 final int N = getChildCount();
107 for (int i = 0; i < N; i++) {
108 View child = getChildAt(i);
109 if (child.getLayoutParams() instanceof LayoutParams) {
110 LayoutParams lp = (LayoutParams) child.getLayoutParams();
111 if (!lp.ignoreRightInset && lp.rightMargin != mRightInset) {
112 lp.rightMargin = mRightInset;
113 child.requestLayout();
114 }
115 }
116 }
117 }
118
119 @Override
120 public FrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
121 return new LayoutParams(getContext(), attrs);
122 }
123
124 @Override
125 protected FrameLayout.LayoutParams generateDefaultLayoutParams() {
126 return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
127 }
128
John Spurlockfba91202014-04-22 12:58:26 -0400129 @Override
Selim Cinek4e6b2d32015-06-25 20:15:33 -0400130 protected void onFinishInflate() {
131 super.onFinishInflate();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100132 mStackScrollLayout = (NotificationStackScrollLayout) findViewById(
133 R.id.notification_stack_scroller);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700134 mNotificationPanel = (NotificationPanelView) findViewById(R.id.notification_panel);
Adrian Roos9dc32092014-09-02 23:34:10 +0200135 mBrightnessMirror = findViewById(R.id.brightness_mirror);
Selim Cinek4e6b2d32015-06-25 20:15:33 -0400136 }
137
138 public void setService(PhoneStatusBar service) {
139 mService = service;
140 mDragDownHelper = new DragDownHelper(getContext(), this, mStackScrollLayout, mService);
141 }
142
143 @Override
144 protected void onAttachedToWindow () {
145 super.onAttachedToWindow();
Daniel Sandlerb10d8852013-05-08 15:57:06 -0400146
147 // We really need to be able to animate while window animations are going on
148 // so that activities may be started asynchronously from panel animations
149 final ViewRootImpl root = getViewRootImpl();
150 if (root != null) {
151 root.setDrawDuringWindowsAnimating(true);
152 }
Selim Cineka0fad3b2014-09-19 17:20:05 +0200153
154 // We need to ensure that our window doesn't suffer from overdraw which would normally
155 // occur if our window is translucent. Since we are drawing the whole window anyway with
156 // the scrim, we don't need the window to be cleared in the beginning.
Jorim Jaggi0e664392014-09-27 01:30:22 +0200157 if (mService.isScrimSrcModeEnabled()) {
158 IBinder windowToken = getWindowToken();
159 WindowManager.LayoutParams lp = (WindowManager.LayoutParams) getLayoutParams();
160 lp.token = windowToken;
161 setLayoutParams(lp);
162 WindowManagerGlobal.getInstance().changeCanvasOpacity(windowToken, true);
163 setWillNotDraw(false);
164 } else {
165 setWillNotDraw(!DEBUG);
166 }
Daniel Sandlerc4f2a562012-05-04 11:55:46 -0400167 }
168
169 @Override
170 public boolean dispatchKeyEvent(KeyEvent event) {
171 boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
172 switch (event.getKeyCode()) {
Jorim Jaggi8c8bcc12014-04-16 21:36:51 +0200173 case KeyEvent.KEYCODE_BACK:
174 if (!down) {
175 mService.onBackPressed();
176 }
177 return true;
178 case KeyEvent.KEYCODE_MENU:
179 if (!down) {
180 return mService.onMenuPressed();
181 }
Jorim Jaggi34250762014-07-03 23:51:19 +0200182 case KeyEvent.KEYCODE_SPACE:
183 if (!down) {
184 return mService.onSpacePressed();
185 }
John Spurlock0b99ea92014-10-01 15:32:22 -0400186 break;
John Spurlockd06aa572014-09-10 10:40:49 -0400187 case KeyEvent.KEYCODE_VOLUME_DOWN:
188 case KeyEvent.KEYCODE_VOLUME_UP:
John Spurlock0b99ea92014-10-01 15:32:22 -0400189 if (mService.isDozing()) {
190 MediaSessionLegacyHelper.getHelper(mContext).sendVolumeKeyEvent(event, true);
191 return true;
John Spurlockd06aa572014-09-10 10:40:49 -0400192 }
John Spurlock0b99ea92014-10-01 15:32:22 -0400193 break;
Daniel Sandlerc4f2a562012-05-04 11:55:46 -0400194 }
Jorim Jaggidf993512014-05-13 23:06:35 +0200195 if (mService.interceptMediaKey(event)) {
196 return true;
197 }
Daniel Sandlerc4f2a562012-05-04 11:55:46 -0400198 return super.dispatchKeyEvent(event);
199 }
Chris Wren5de6e942012-05-16 14:22:21 -0400200
201 @Override
Adrian Roos9dc32092014-09-02 23:34:10 +0200202 public boolean dispatchTouchEvent(MotionEvent ev) {
Blazej Magnowski0e2ffbd2015-09-10 14:37:17 -0700203 mFalsingManager.onTouchEvent(ev, getWidth(), getHeight());
Adrian Roos9dc32092014-09-02 23:34:10 +0200204 if (mBrightnessMirror != null && mBrightnessMirror.getVisibility() == VISIBLE) {
205 // Disallow new pointers while the brightness mirror is visible. This is so that you
206 // can't touch anything other than the brightness slider while the mirror is showing
207 // and the rest of the panel is transparent.
208 if (ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
209 return false;
210 }
211 }
212 return super.dispatchTouchEvent(ev);
213 }
214
215 @Override
Chris Wren5de6e942012-05-16 14:22:21 -0400216 public boolean onInterceptTouchEvent(MotionEvent ev) {
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700217 boolean intercept = false;
Selim Cinekfab078b2014-03-27 22:45:58 +0100218 if (mNotificationPanel.isFullyExpanded()
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200219 && mStackScrollLayout.getVisibility() == View.VISIBLE
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200220 && mService.getBarState() == StatusBarState.KEYGUARD
221 && !mService.isBouncerShowing()) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200222 intercept = mDragDownHelper.onInterceptTouchEvent(ev);
John Spurlock8b12f222014-09-09 11:54:11 -0400223 // wake up on a touch down event, if dozing
224 if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
Jorim Jaggi2a5e4522014-11-24 21:45:20 +0100225 mService.wakeUpIfDozing(ev.getEventTime(), ev);
John Spurlock8b12f222014-09-09 11:54:11 -0400226 }
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700227 }
228 if (!intercept) {
229 super.onInterceptTouchEvent(ev);
230 }
Chris Wren5de6e942012-05-16 14:22:21 -0400231 if (intercept) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700232 MotionEvent cancellation = MotionEvent.obtain(ev);
233 cancellation.setAction(MotionEvent.ACTION_CANCEL);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100234 mStackScrollLayout.onInterceptTouchEvent(cancellation);
Selim Cinek31094df2014-08-14 19:28:15 +0200235 mNotificationPanel.onInterceptTouchEvent(cancellation);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700236 cancellation.recycle();
Chris Wren5de6e942012-05-16 14:22:21 -0400237 }
238 return intercept;
239 }
240
241 @Override
242 public boolean onTouchEvent(MotionEvent ev) {
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700243 boolean handled = false;
Selim Cinekcb9400a2015-06-03 16:56:13 +0200244 if (mService.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200245 handled = mDragDownHelper.onTouchEvent(ev);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700246 }
247 if (!handled) {
248 handled = super.onTouchEvent(ev);
249 }
John Spurlock257f2832013-09-21 18:41:53 -0400250 final int action = ev.getAction();
John Spurlockd157ca02013-11-04 10:28:05 -0500251 if (!handled && (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL)) {
John Spurlock257f2832013-09-21 18:41:53 -0400252 mService.setInteracting(StatusBarManager.WINDOW_STATUS_BAR, false);
253 }
Chris Wren5de6e942012-05-16 14:22:21 -0400254 return handled;
255 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400256
257 @Override
258 public void onDraw(Canvas canvas) {
259 super.onDraw(canvas);
Jorim Jaggi0e664392014-09-27 01:30:22 +0200260 if (mService.isScrimSrcModeEnabled()) {
261 // We need to ensure that our window is always drawn fully even when we have paddings,
262 // since we simulate it to be opaque.
263 int paddedBottom = getHeight() - getPaddingBottom();
264 int paddedRight = getWidth() - getPaddingRight();
265 if (getPaddingTop() != 0) {
266 canvas.drawRect(0, 0, getWidth(), getPaddingTop(), mTransparentSrcPaint);
267 }
268 if (getPaddingBottom() != 0) {
269 canvas.drawRect(0, paddedBottom, getWidth(), getHeight(), mTransparentSrcPaint);
270 }
271 if (getPaddingLeft() != 0) {
272 canvas.drawRect(0, getPaddingTop(), getPaddingLeft(), paddedBottom,
273 mTransparentSrcPaint);
274 }
275 if (getPaddingRight() != 0) {
276 canvas.drawRect(paddedRight, getPaddingTop(), getWidth(), paddedBottom,
277 mTransparentSrcPaint);
278 }
Selim Cineka0fad3b2014-09-19 17:20:05 +0200279 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400280 if (DEBUG) {
281 Paint pt = new Paint();
282 pt.setColor(0x80FFFF00);
283 pt.setStrokeWidth(12.0f);
284 pt.setStyle(Paint.Style.STROKE);
285 canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), pt);
286 }
287 }
Daniel Sandlerac47ff72012-10-23 10:41:44 -0400288
289 public void cancelExpandHelper() {
Selim Cinek1408eb52014-06-02 14:45:38 +0200290 if (mStackScrollLayout != null) {
291 mStackScrollLayout.cancelExpandHelper();
Daniel Sandler1a3bdd52012-10-23 19:21:20 -0400292 }
Daniel Sandlerac47ff72012-10-23 10:41:44 -0400293 }
Adrian Roos2f2bd9a2015-06-04 18:11:14 -0700294
295 public class LayoutParams extends FrameLayout.LayoutParams {
296
297 public boolean ignoreRightInset;
298
299 public LayoutParams(int width, int height) {
300 super(width, height);
301 }
302
303 public LayoutParams(Context c, AttributeSet attrs) {
304 super(c, attrs);
305
306 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.StatusBarWindowView_Layout);
307 ignoreRightInset = a.getBoolean(
308 R.styleable.StatusBarWindowView_Layout_ignoreRightInset, false);
309 a.recycle();
310 }
311 }
Daniel Sandlerc4f2a562012-05-04 11:55:46 -0400312}
313