Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
Chris Wren | eda110f | 2013-07-25 15:31:59 -0400 | [diff] [blame] | 19 | import android.app.Notification; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Configuration; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 22 | import android.graphics.Rect; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.util.Log; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 25 | import android.view.MotionEvent; |
| 26 | import android.view.View; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 27 | import android.view.ViewConfiguration; |
| 28 | import android.view.ViewGroup; |
Chris Wren | 0f2aa68 | 2013-08-02 12:03:02 -0400 | [diff] [blame] | 29 | import android.widget.FrameLayout; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 30 | import android.widget.LinearLayout; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 31 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 32 | import com.android.systemui.ExpandHelper; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 33 | import com.android.systemui.R; |
| 34 | import com.android.systemui.SwipeHelper; |
| 35 | import com.android.systemui.statusbar.BaseStatusBar; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.NotificationData; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 37 | |
Chris Wren | 0f2aa68 | 2013-08-02 12:03:02 -0400 | [diff] [blame] | 38 | public class HeadsUpNotificationView extends FrameLayout implements SwipeHelper.Callback, ExpandHelper.Callback { |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 39 | private static final String TAG = "HeadsUpNotificationView"; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 40 | private static final boolean DEBUG = false; |
Chris Wren | 6d15a36 | 2013-08-20 18:46:29 -0400 | [diff] [blame] | 41 | private static final boolean SPEW = DEBUG; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 42 | |
| 43 | Rect mTmpRect = new Rect(); |
| 44 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 45 | private final int mTouchSensitivityDelay; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 46 | private SwipeHelper mSwipeHelper; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 47 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 48 | private BaseStatusBar mBar; |
| 49 | private ExpandHelper mExpandHelper; |
| 50 | private long mStartTouchTime; |
| 51 | |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 52 | private ViewGroup mContentHolder; |
Chris Wren | 0f2aa68 | 2013-08-02 12:03:02 -0400 | [diff] [blame] | 53 | private ViewGroup mContentSlider; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 54 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 55 | private NotificationData.Entry mHeadsUp; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 56 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 57 | public HeadsUpNotificationView(Context context, AttributeSet attrs) { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 58 | this(context, attrs, 0); |
| 59 | } |
| 60 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 61 | public HeadsUpNotificationView(Context context, AttributeSet attrs, int defStyle) { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 62 | super(context, attrs, defStyle); |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 63 | mTouchSensitivityDelay = getResources().getInteger(R.integer.heads_up_sensitivity_delay); |
| 64 | if (DEBUG) Log.v(TAG, "create() " + mTouchSensitivityDelay); |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 67 | public void setBar(BaseStatusBar bar) { |
| 68 | mBar = bar; |
| 69 | } |
| 70 | |
Chris Wren | 0f2aa68 | 2013-08-02 12:03:02 -0400 | [diff] [blame] | 71 | public ViewGroup getHolder() { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 72 | return mContentHolder; |
| 73 | } |
| 74 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 75 | public boolean setNotification(NotificationData.Entry headsUp) { |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 76 | mHeadsUp = headsUp; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 77 | mHeadsUp.row.setExpanded(false); |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 78 | if (mContentHolder == null) { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 79 | // too soon! |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 80 | return false; |
| 81 | } |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 82 | mContentHolder.setX(0); |
| 83 | mContentHolder.setVisibility(View.VISIBLE); |
| 84 | mContentHolder.setAlpha(1f); |
| 85 | mContentHolder.removeAllViews(); |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 86 | mContentHolder.addView(mHeadsUp.row); |
Chris Wren | 4c91312 | 2013-08-05 18:08:16 -0400 | [diff] [blame] | 87 | mSwipeHelper.snapChild(mContentSlider, 1f); |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 88 | mStartTouchTime = System.currentTimeMillis() + mTouchSensitivityDelay; |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 89 | return true; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 90 | } |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 91 | |
Chris Wren | 6d15a36 | 2013-08-20 18:46:29 -0400 | [diff] [blame] | 92 | public boolean isClearable() { |
| 93 | return mHeadsUp == null || mHeadsUp.notification.isClearable(); |
Chris Wren | 0f2aa68 | 2013-08-02 12:03:02 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public void setMargin(int notificationPanelMarginPx) { |
Chris Wren | 6d15a36 | 2013-08-20 18:46:29 -0400 | [diff] [blame] | 97 | if (SPEW) Log.v(TAG, "setMargin() " + notificationPanelMarginPx); |
Chris Wren | 0f2aa68 | 2013-08-02 12:03:02 -0400 | [diff] [blame] | 98 | if (mContentSlider != null) { |
| 99 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentSlider.getLayoutParams(); |
| 100 | lp.setMarginStart(notificationPanelMarginPx); |
| 101 | mContentSlider.setLayoutParams(lp); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // LinearLayout methods |
| 106 | |
| 107 | @Override |
| 108 | public void onDraw(android.graphics.Canvas c) { |
| 109 | super.onDraw(c); |
| 110 | if (DEBUG) { |
| 111 | //Log.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: " |
| 112 | // + getMeasuredHeight() + "px"); |
| 113 | c.save(); |
| 114 | c.clipRect(6, 6, c.getWidth() - 6, getMeasuredHeight() - 6, |
| 115 | android.graphics.Region.Op.DIFFERENCE); |
| 116 | c.drawColor(0xFFcc00cc); |
| 117 | c.restore(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // ViewGroup methods |
| 122 | |
| 123 | @Override |
| 124 | public void onAttachedToWindow() { |
| 125 | float densityScale = getResources().getDisplayMetrics().density; |
| 126 | float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); |
| 127 | mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop); |
| 128 | |
| 129 | int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_min_height); |
| 130 | int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_max_height); |
| 131 | mExpandHelper = new ExpandHelper(mContext, this, minHeight, maxHeight); |
| 132 | |
| 133 | mContentHolder = (ViewGroup) findViewById(R.id.content_holder); |
| 134 | mContentSlider = (ViewGroup) findViewById(R.id.content_slider); |
| 135 | |
| 136 | if (mHeadsUp != null) { |
| 137 | // whoops, we're on already! |
| 138 | setNotification(mHeadsUp); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 144 | if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()"); |
| 145 | if (System.currentTimeMillis() < mStartTouchTime) { |
| 146 | return true; |
| 147 | } |
| 148 | return mSwipeHelper.onInterceptTouchEvent(ev) |
| 149 | || mExpandHelper.onInterceptTouchEvent(ev) |
| 150 | || super.onInterceptTouchEvent(ev); |
| 151 | } |
| 152 | |
| 153 | // View methods |
| 154 | |
| 155 | @Override |
| 156 | public boolean onTouchEvent(MotionEvent ev) { |
| 157 | if (System.currentTimeMillis() < mStartTouchTime) { |
| 158 | return false; |
| 159 | } |
| 160 | mBar.resetHeadsUpDecayTimer(); |
| 161 | return mSwipeHelper.onTouchEvent(ev) |
| 162 | || mExpandHelper.onTouchEvent(ev) |
| 163 | || super.onTouchEvent(ev); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | protected void onConfigurationChanged(Configuration newConfig) { |
| 168 | super.onConfigurationChanged(newConfig); |
| 169 | float densityScale = getResources().getDisplayMetrics().density; |
| 170 | mSwipeHelper.setDensityScale(densityScale); |
| 171 | float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); |
| 172 | mSwipeHelper.setPagingTouchSlop(pagingTouchSlop); |
| 173 | } |
| 174 | |
| 175 | // ExpandHelper.Callback methods |
| 176 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 177 | @Override |
| 178 | public View getChildAtRawPosition(float x, float y) { |
| 179 | return getChildAtPosition(x, y); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public View getChildAtPosition(float x, float y) { |
| 184 | return mHeadsUp == null ? null : mHeadsUp.row; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public boolean canChildBeExpanded(View v) { |
| 189 | return mHeadsUp != null && mHeadsUp.row == v && mHeadsUp.row.isExpandable(); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void setUserExpandedChild(View v, boolean userExpanded) { |
| 194 | if (mHeadsUp != null && mHeadsUp.row == v) { |
| 195 | mHeadsUp.row.setUserExpanded(userExpanded); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void setUserLockedChild(View v, boolean userLocked) { |
| 201 | if (mHeadsUp != null && mHeadsUp.row == v) { |
| 202 | mHeadsUp.row.setUserLocked(userLocked); |
| 203 | } |
| 204 | } |
Chris Wren | eda110f | 2013-07-25 15:31:59 -0400 | [diff] [blame] | 205 | |
Chris Wren | 0f2aa68 | 2013-08-02 12:03:02 -0400 | [diff] [blame] | 206 | // SwipeHelper.Callback methods |
| 207 | |
| 208 | @Override |
| 209 | public boolean canChildBeDismissed(View v) { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onChildDismissed(View v) { |
| 215 | Log.v(TAG, "User swiped heads up to dismiss"); |
| 216 | mBar.onHeadsUpDismissed(); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void onBeginDrag(View v) { |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void onDragCancelled(View v) { |
| 225 | mContentHolder.setAlpha(1f); // sometimes this isn't quite reset |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public View getChildAtPosition(MotionEvent ev) { |
| 230 | return mContentSlider; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public View getChildContentView(View v) { |
Chris Wren | 4c91312 | 2013-08-05 18:08:16 -0400 | [diff] [blame] | 235 | return mContentSlider; |
Chris Wren | eda110f | 2013-07-25 15:31:59 -0400 | [diff] [blame] | 236 | } |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 237 | } |