Daniel Sandler | d42497e | 2011-06-04 00:32:50 -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 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 21 | import android.animation.LayoutTransition; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
Daniel Sandler | 1cdf067 | 2011-09-07 22:13:29 -0400 | [diff] [blame] | 23 | import android.animation.ValueAnimator; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 24 | import android.content.Context; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 27 | import android.graphics.Rect; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 30 | import android.util.Slog; |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 31 | import android.view.MotionEvent; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 32 | import android.view.View; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 33 | import android.view.ViewConfiguration; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 34 | import android.view.ViewGroup; |
Daniel Sandler | 469e96e | 2012-05-04 15:56:19 -0400 | [diff] [blame] | 35 | import android.view.inputmethod.InputMethodManager; |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 36 | import android.widget.LinearLayout; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 37 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 38 | import com.android.systemui.ExpandHelper; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 39 | import com.android.systemui.R; |
| 40 | import com.android.systemui.SwipeHelper; |
Chris Wren | 8fd1265 | 2012-05-09 21:25:57 -0400 | [diff] [blame] | 41 | import com.android.systemui.statusbar.NotificationData; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 42 | |
Daniel Sandler | 1cdf067 | 2011-09-07 22:13:29 -0400 | [diff] [blame] | 43 | import java.util.HashMap; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 44 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 45 | public class NotificationRowLayout |
| 46 | extends LinearLayout |
Daniel Sandler | 3d32a24 | 2012-06-05 13:44:14 -0400 | [diff] [blame] | 47 | implements SwipeHelper.Callback, ExpandHelper.Callback |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 48 | { |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 49 | private static final String TAG = "NotificationRowLayout"; |
| 50 | private static final boolean DEBUG = false; |
Daniel Sandler | 1cdf067 | 2011-09-07 22:13:29 -0400 | [diff] [blame] | 51 | private static final boolean SLOW_ANIMATIONS = DEBUG; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 52 | |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 53 | private static final int APPEAR_ANIM_LEN = SLOW_ANIMATIONS ? 5000 : 250; |
| 54 | private static final int DISAPPEAR_ANIM_LEN = APPEAR_ANIM_LEN; |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 55 | |
Daniel Sandler | 8ba33c9 | 2011-10-04 21:49:30 -0400 | [diff] [blame] | 56 | boolean mAnimateBounds = true; |
| 57 | |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 58 | Rect mTmpRect = new Rect(); |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 59 | |
Daniel Sandler | 1cdf067 | 2011-09-07 22:13:29 -0400 | [diff] [blame] | 60 | HashMap<View, ValueAnimator> mAppearingViews = new HashMap<View, ValueAnimator>(); |
| 61 | HashMap<View, ValueAnimator> mDisappearingViews = new HashMap<View, ValueAnimator>(); |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 62 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 63 | private SwipeHelper mSwipeHelper; |
Daniel Sandler | 3d32a24 | 2012-06-05 13:44:14 -0400 | [diff] [blame] | 64 | |
| 65 | private OnSizeChangedListener mOnSizeChangedListener; |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 66 | |
Chet Haase | 2f2022a | 2011-10-11 06:41:59 -0700 | [diff] [blame] | 67 | // Flag set during notification removal animation to avoid causing too much work until |
| 68 | // animation is done |
| 69 | boolean mRemoveViews = true; |
| 70 | |
Daniel Sandler | e5011a3 | 2012-06-11 16:07:52 -0400 | [diff] [blame] | 71 | private LayoutTransition mRealLayoutTransition; |
| 72 | |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 73 | public NotificationRowLayout(Context context, AttributeSet attrs) { |
| 74 | this(context, attrs, 0); |
| 75 | } |
| 76 | |
| 77 | public NotificationRowLayout(Context context, AttributeSet attrs, int defStyle) { |
| 78 | super(context, attrs, defStyle); |
| 79 | |
Daniel Sandler | e5011a3 | 2012-06-11 16:07:52 -0400 | [diff] [blame] | 80 | mRealLayoutTransition = new LayoutTransition(); |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 81 | mRealLayoutTransition.setAnimateParentHierarchy(true); |
Daniel Sandler | e5011a3 | 2012-06-11 16:07:52 -0400 | [diff] [blame] | 82 | setLayoutTransitionsEnabled(true); |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 83 | |
| 84 | setOrientation(LinearLayout.VERTICAL); |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 85 | |
| 86 | if (DEBUG) { |
| 87 | setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() { |
| 88 | @Override |
| 89 | public void onChildViewAdded(View parent, View child) { |
| 90 | Slog.d(TAG, "view added: " + child + "; new count: " + getChildCount()); |
| 91 | } |
| 92 | @Override |
| 93 | public void onChildViewRemoved(View parent, View child) { |
| 94 | Slog.d(TAG, "view removed: " + child + "; new count: " + (getChildCount() - 1)); |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | setBackgroundColor(0x80FF8000); |
| 99 | } |
| 100 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 101 | float densityScale = getResources().getDisplayMetrics().density; |
| 102 | float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop(); |
| 103 | mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop); |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 106 | public void setLongPressListener(View.OnLongClickListener listener) { |
| 107 | mSwipeHelper.setLongPressListener(listener); |
| 108 | } |
| 109 | |
Daniel Sandler | 3d32a24 | 2012-06-05 13:44:14 -0400 | [diff] [blame] | 110 | public void setOnSizeChangedListener(OnSizeChangedListener l) { |
| 111 | mOnSizeChangedListener = l; |
| 112 | } |
| 113 | |
Daniel Sandler | 469e96e | 2012-05-04 15:56:19 -0400 | [diff] [blame] | 114 | @Override |
| 115 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 116 | super.onWindowFocusChanged(hasWindowFocus); |
| 117 | if (!hasWindowFocus) { |
| 118 | mSwipeHelper.removeLongPressCallback(); |
| 119 | } |
| 120 | } |
| 121 | |
Daniel Sandler | 8ba33c9 | 2011-10-04 21:49:30 -0400 | [diff] [blame] | 122 | public void setAnimateBounds(boolean anim) { |
| 123 | mAnimateBounds = anim; |
| 124 | } |
| 125 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 126 | private void logLayoutTransition() { |
| 127 | Log.v(TAG, "layout " + |
Daniel Sandler | e5011a3 | 2012-06-11 16:07:52 -0400 | [diff] [blame] | 128 | (mRealLayoutTransition.isChangingLayout() ? "is " : "is not ") + |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 129 | "in transition and animations " + |
Daniel Sandler | e5011a3 | 2012-06-11 16:07:52 -0400 | [diff] [blame] | 130 | (mRealLayoutTransition.isRunning() ? "are " : "are not ") + |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 131 | "running."); |
| 132 | } |
| 133 | |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 134 | @Override |
| 135 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 136 | if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()"); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 137 | if (DEBUG) logLayoutTransition(); |
| 138 | |
Chris Wren | 5de6e94 | 2012-05-16 14:22:21 -0400 | [diff] [blame] | 139 | return mSwipeHelper.onInterceptTouchEvent(ev) || |
| 140 | super.onInterceptTouchEvent(ev); |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 143 | @Override |
| 144 | public boolean onTouchEvent(MotionEvent ev) { |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 145 | if (DEBUG) Log.v(TAG, "onTouchEvent()"); |
| 146 | if (DEBUG) logLayoutTransition(); |
Chris Wren | 5de6e94 | 2012-05-16 14:22:21 -0400 | [diff] [blame] | 147 | |
| 148 | return mSwipeHelper.onTouchEvent(ev) || |
| 149 | super.onTouchEvent(ev); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 150 | } |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 151 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 152 | public boolean canChildBeDismissed(View v) { |
| 153 | final View veto = v.findViewById(R.id.veto); |
| 154 | return (veto != null && veto.getVisibility() != View.GONE); |
| 155 | } |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 156 | |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 157 | public boolean canChildBeExpanded(View v) { |
Chris Wren | 8fd1265 | 2012-05-09 21:25:57 -0400 | [diff] [blame] | 158 | return NotificationData.getIsExpandable(v); |
| 159 | } |
| 160 | |
| 161 | public boolean setUserExpandedChild(View v, boolean userExpanded) { |
| 162 | return NotificationData.setUserExpanded(v, userExpanded); |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Chris Wren | 3ddab0d | 2012-08-02 16:52:21 -0400 | [diff] [blame] | 165 | public boolean setUserLockedChild(View v, boolean userLocked) { |
| 166 | return NotificationData.setUserLocked(v, userLocked); |
| 167 | } |
| 168 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 169 | public void onChildDismissed(View v) { |
Daniel Sandler | e7237fc | 2012-08-14 16:08:27 -0400 | [diff] [blame] | 170 | if (DEBUG) Slog.v(TAG, "onChildDismissed: " + v + " mRemoveViews=" + mRemoveViews); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 171 | final View veto = v.findViewById(R.id.veto); |
Chet Haase | 2f2022a | 2011-10-11 06:41:59 -0700 | [diff] [blame] | 172 | if (veto != null && veto.getVisibility() != View.GONE && mRemoveViews) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 173 | veto.performClick(); |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 174 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | public void onBeginDrag(View v) { |
| 178 | // We need to prevent the surrounding ScrollView from intercepting us now; |
| 179 | // the scroll position will be locked while we swipe |
| 180 | requestDisallowInterceptTouchEvent(true); |
| 181 | } |
| 182 | |
Peter Ng | 622a976 | 2011-08-29 10:56:53 -0700 | [diff] [blame] | 183 | public void onDragCancelled(View v) { |
| 184 | } |
| 185 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 186 | public View getChildAtPosition(MotionEvent ev) { |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 187 | return getChildAtPosition(ev.getX(), ev.getY()); |
| 188 | } |
Chris Wren | 5de6e94 | 2012-05-16 14:22:21 -0400 | [diff] [blame] | 189 | |
| 190 | public View getChildAtRawPosition(float touchX, float touchY) { |
| 191 | int[] location = new int[2]; |
| 192 | getLocationOnScreen(location); |
| 193 | return getChildAtPosition((float) (touchX - location[0]), (float) (touchY - location[1])); |
| 194 | } |
| 195 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 196 | public View getChildAtPosition(float touchX, float touchY) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 197 | // find the view under the pointer, accounting for GONE views |
| 198 | final int count = getChildCount(); |
| 199 | int y = 0; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 200 | int childIdx = 0; |
| 201 | View slidingChild; |
| 202 | for (; childIdx < count; childIdx++) { |
| 203 | slidingChild = getChildAt(childIdx); |
| 204 | if (slidingChild.getVisibility() == GONE) { |
| 205 | continue; |
| 206 | } |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 207 | y += slidingChild.getMeasuredHeight(); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 208 | if (touchY < y) return slidingChild; |
| 209 | } |
| 210 | return null; |
| 211 | } |
| 212 | |
| 213 | public View getChildContentView(View v) { |
| 214 | return v; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | protected void onConfigurationChanged(Configuration newConfig) { |
| 219 | super.onConfigurationChanged(newConfig); |
| 220 | float densityScale = getResources().getDisplayMetrics().density; |
| 221 | mSwipeHelper.setDensityScale(densityScale); |
| 222 | float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop(); |
| 223 | mSwipeHelper.setPagingTouchSlop(pagingTouchSlop); |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 226 | |
Chet Haase | 2f2022a | 2011-10-11 06:41:59 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Sets a flag to tell us whether to actually remove views. Removal is delayed by setting this |
| 229 | * to false during some animations to smooth out performance. Callers should restore the |
| 230 | * flag to true after the animation is done, and then they should make sure that the views |
| 231 | * get removed properly. |
| 232 | */ |
| 233 | public void setViewRemoval(boolean removeViews) { |
Daniel Sandler | e7237fc | 2012-08-14 16:08:27 -0400 | [diff] [blame] | 234 | if (DEBUG) Slog.v(TAG, "setViewRemoval: " + removeViews); |
Chet Haase | 2f2022a | 2011-10-11 06:41:59 -0700 | [diff] [blame] | 235 | mRemoveViews = removeViews; |
| 236 | } |
| 237 | |
Daniel Sandler | e5011a3 | 2012-06-11 16:07:52 -0400 | [diff] [blame] | 238 | // Suppress layout transitions for a little while. |
| 239 | public void setLayoutTransitionsEnabled(boolean b) { |
| 240 | if (b) { |
| 241 | setLayoutTransition(mRealLayoutTransition); |
| 242 | } else { |
| 243 | if (mRealLayoutTransition.isRunning()) { |
| 244 | mRealLayoutTransition.cancel(); |
| 245 | } |
| 246 | setLayoutTransition(null); |
| 247 | } |
| 248 | } |
| 249 | |
Daniel Sandler | 8ba33c9 | 2011-10-04 21:49:30 -0400 | [diff] [blame] | 250 | public void dismissRowAnimated(View child) { |
| 251 | dismissRowAnimated(child, 0); |
| 252 | } |
| 253 | |
| 254 | public void dismissRowAnimated(View child, int vel) { |
| 255 | mSwipeHelper.dismissChild(child, vel); |
| 256 | } |
| 257 | |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 258 | @Override |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 259 | public void onFinishInflate() { |
| 260 | super.onFinishInflate(); |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 261 | if (DEBUG) setWillNotDraw(false); |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void onDraw(android.graphics.Canvas c) { |
| 266 | super.onDraw(c); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 267 | if (DEBUG) logLayoutTransition(); |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 268 | if (DEBUG) { |
Daniel Sandler | ad6352b | 2011-06-13 11:43:45 -0400 | [diff] [blame] | 269 | //Slog.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: " |
| 270 | // + getMeasuredHeight() + "px"); |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 271 | c.save(); |
| 272 | c.clipRect(6, 6, c.getWidth() - 6, getMeasuredHeight() - 6, |
| 273 | android.graphics.Region.Op.DIFFERENCE); |
| 274 | c.drawColor(0xFFFF8000); |
| 275 | c.restore(); |
| 276 | } |
| 277 | } |
Daniel Sandler | 3d32a24 | 2012-06-05 13:44:14 -0400 | [diff] [blame] | 278 | |
| 279 | @Override |
| 280 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 281 | if (mOnSizeChangedListener != null) { |
| 282 | mOnSizeChangedListener.onSizeChanged(this, w, h, oldw, oldh); |
| 283 | } |
| 284 | } |
Daniel Sandler | d42497e | 2011-06-04 00:32:50 -0400 | [diff] [blame] | 285 | } |