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