Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 android.view; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 23 | import android.widget.ImageView; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 24 | import android.widget.RemoteViews; |
Selim Cinek | eaa29ca | 2015-11-23 13:51:13 -0800 | [diff] [blame] | 25 | import android.widget.TextView; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 26 | |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 27 | import java.util.ArrayList; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * A header of a notification view |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
| 34 | @RemoteViews.RemoteView |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 35 | public class NotificationHeaderView extends ViewGroup { |
Selim Cinek | ea4bef7 | 2015-12-02 15:51:10 -0800 | [diff] [blame] | 36 | public static final int NO_COLOR = -1; |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 37 | private final int mChildMinWidth; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 38 | private final int mContentEndMargin; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 39 | private View mAppName; |
| 40 | private View mSubTextView; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 41 | private OnClickListener mExpandClickListener; |
| 42 | private HeaderTouchListener mTouchListener = new HeaderTouchListener(); |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 43 | private ImageView mExpandButton; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 44 | private View mIcon; |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 45 | private View mProfileBadge; |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 46 | private View mInfo; |
Selim Cinek | ea4bef7 | 2015-12-02 15:51:10 -0800 | [diff] [blame] | 47 | private int mIconColor; |
| 48 | private int mOriginalNotificationColor; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 49 | private boolean mExpanded; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 50 | private boolean mShowWorkBadgeAtEnd; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 51 | |
| 52 | public NotificationHeaderView(Context context) { |
| 53 | this(context, null); |
| 54 | } |
| 55 | |
| 56 | public NotificationHeaderView(Context context, @Nullable AttributeSet attrs) { |
| 57 | this(context, attrs, 0); |
| 58 | } |
| 59 | |
| 60 | public NotificationHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 61 | this(context, attrs, defStyleAttr, 0); |
| 62 | } |
| 63 | |
| 64 | public NotificationHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 65 | super(context, attrs, defStyleAttr, defStyleRes); |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 66 | mChildMinWidth = getResources().getDimensionPixelSize( |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 67 | com.android.internal.R.dimen.notification_header_shrink_min_width); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 68 | mContentEndMargin = getResources().getDimensionPixelSize( |
| 69 | com.android.internal.R.dimen.notification_content_margin_end); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void onFinishInflate() { |
| 74 | super.onFinishInflate(); |
| 75 | mAppName = findViewById(com.android.internal.R.id.app_name_text); |
| 76 | mSubTextView = findViewById(com.android.internal.R.id.header_sub_text); |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 77 | mExpandButton = (ImageView) findViewById(com.android.internal.R.id.expand_button); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 78 | mIcon = findViewById(com.android.internal.R.id.icon); |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 79 | mProfileBadge = findViewById(com.android.internal.R.id.profile_badge); |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 80 | mInfo = findViewById(com.android.internal.R.id.header_content_info); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 85 | final int givenWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 86 | final int givenHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 87 | int wrapContentWidthSpec = MeasureSpec.makeMeasureSpec(givenWidth, |
| 88 | MeasureSpec.AT_MOST); |
| 89 | int wrapContentHeightSpec = MeasureSpec.makeMeasureSpec(givenHeight, |
| 90 | MeasureSpec.AT_MOST); |
| 91 | int totalWidth = getPaddingStart() + getPaddingEnd(); |
| 92 | for (int i = 0; i < getChildCount(); i++) { |
| 93 | final View child = getChildAt(i); |
| 94 | if (child.getVisibility() == GONE) { |
| 95 | // We'll give it the rest of the space in the end |
| 96 | continue; |
| 97 | } |
| 98 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 99 | int childWidthSpec = getChildMeasureSpec(wrapContentWidthSpec, |
| 100 | lp.leftMargin + lp.rightMargin, lp.width); |
| 101 | int childHeightSpec = getChildMeasureSpec(wrapContentHeightSpec, |
| 102 | lp.topMargin + lp.bottomMargin, lp.height); |
| 103 | child.measure(childWidthSpec, childHeightSpec); |
| 104 | totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth(); |
| 105 | } |
| 106 | if (totalWidth > givenWidth) { |
| 107 | int overFlow = totalWidth - givenWidth; |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 108 | // We are overflowing, lets shrink the info first |
| 109 | final int infoWidth = mInfo.getMeasuredWidth(); |
| 110 | if (mInfo.getVisibility() != GONE && infoWidth > mChildMinWidth) { |
| 111 | int newSize = infoWidth - Math.min(infoWidth - mChildMinWidth, overFlow); |
| 112 | int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST); |
| 113 | mInfo.measure(childWidthSpec, wrapContentHeightSpec); |
| 114 | overFlow -= infoWidth - newSize; |
| 115 | } |
| 116 | // still overflowing, lets shrink the app name now |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 117 | final int appWidth = mAppName.getMeasuredWidth(); |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 118 | if (overFlow > 0 && mAppName.getVisibility() != GONE && appWidth > mChildMinWidth) { |
| 119 | int newSize = appWidth - Math.min(appWidth - mChildMinWidth, overFlow); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 120 | int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST); |
| 121 | mAppName.measure(childWidthSpec, wrapContentHeightSpec); |
| 122 | overFlow -= appWidth - newSize; |
| 123 | } |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 124 | // still overflowing, finaly we shrink the subtext |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 125 | if (overFlow > 0 && mSubTextView.getVisibility() != GONE) { |
| 126 | // we're still too big |
| 127 | final int subTextWidth = mSubTextView.getMeasuredWidth(); |
| 128 | int newSize = Math.max(0, subTextWidth - overFlow); |
| 129 | int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST); |
| 130 | mSubTextView.measure(childWidthSpec, wrapContentHeightSpec); |
| 131 | } |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 132 | } |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 133 | setMeasuredDimension(givenWidth, givenHeight); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 134 | } |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 135 | |
| 136 | @Override |
| 137 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 138 | int left = getPaddingStart(); |
| 139 | int childCount = getChildCount(); |
| 140 | int ownHeight = getHeight() - getPaddingTop() - getPaddingBottom(); |
| 141 | for (int i = 0; i < childCount; i++) { |
| 142 | View child = getChildAt(i); |
| 143 | if (child.getVisibility() == GONE) { |
| 144 | continue; |
| 145 | } |
| 146 | int childHeight = child.getMeasuredHeight(); |
| 147 | MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams(); |
| 148 | left += params.getMarginStart(); |
| 149 | int right = left + child.getMeasuredWidth(); |
| 150 | int top = (int) (getPaddingTop() + (ownHeight - childHeight) / 2.0f); |
| 151 | int bottom = top + childHeight; |
| 152 | int layoutLeft = left; |
| 153 | int layoutRight = right; |
| 154 | if (child == mProfileBadge) { |
| 155 | int paddingEnd = getPaddingEnd(); |
| 156 | if (mShowWorkBadgeAtEnd) { |
| 157 | paddingEnd = mContentEndMargin; |
| 158 | } |
| 159 | layoutRight = getWidth() - paddingEnd; |
| 160 | layoutLeft = layoutRight - child.getMeasuredWidth(); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 161 | } |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 162 | if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) { |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 163 | int ltrLeft = layoutLeft; |
| 164 | layoutLeft = getWidth() - layoutRight; |
| 165 | layoutRight = getWidth() - ltrLeft; |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 166 | } |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 167 | child.layout(layoutLeft, top, layoutRight, bottom); |
| 168 | left = right + params.getMarginEnd(); |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 169 | } |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 170 | updateTouchListener(); |
| 171 | } |
| 172 | |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 173 | @Override |
| 174 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 175 | return new ViewGroup.MarginLayoutParams(getContext(), attrs); |
| 176 | } |
| 177 | |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 178 | private void updateTouchListener() { |
| 179 | if (mExpandClickListener != null) { |
| 180 | mTouchListener.bindTouchRects(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void setOnClickListener(@Nullable OnClickListener l) { |
| 186 | mExpandClickListener = l; |
| 187 | setOnTouchListener(mExpandClickListener != null ? mTouchListener : null); |
| 188 | updateTouchListener(); |
| 189 | } |
| 190 | |
Selim Cinek | ea4bef7 | 2015-12-02 15:51:10 -0800 | [diff] [blame] | 191 | @RemotableViewMethod |
| 192 | public void setOriginalIconColor(int color) { |
| 193 | mIconColor = color; |
| 194 | } |
| 195 | |
| 196 | public int getOriginalIconColor() { |
| 197 | return mIconColor; |
| 198 | } |
| 199 | |
| 200 | @RemotableViewMethod |
| 201 | public void setOriginalNotificationColor(int color) { |
| 202 | mOriginalNotificationColor = color; |
| 203 | } |
| 204 | |
| 205 | public int getOriginalNotificationColor() { |
| 206 | return mOriginalNotificationColor; |
| 207 | } |
| 208 | |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 209 | @RemotableViewMethod |
| 210 | public void setExpanded(boolean expanded) { |
| 211 | mExpanded = expanded; |
| 212 | updateExpandButton(); |
| 213 | } |
| 214 | |
| 215 | private void updateExpandButton() { |
| 216 | int drawableId; |
Selim Cinek | 6db5758 | 2016-03-04 19:11:27 -0800 | [diff] [blame] | 217 | if (mExpanded) { |
| 218 | drawableId = com.android.internal.R.drawable.ic_collapse_notification; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 219 | } else { |
Selim Cinek | 6db5758 | 2016-03-04 19:11:27 -0800 | [diff] [blame] | 220 | drawableId = com.android.internal.R.drawable.ic_expand_notification; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 221 | } |
| 222 | mExpandButton.setImageDrawable(getContext().getDrawable(drawableId)); |
| 223 | mExpandButton.setColorFilter(mOriginalNotificationColor); |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 226 | public void setShowWorkBadgeAtEnd(boolean showWorkBadgeAtEnd) { |
| 227 | if (showWorkBadgeAtEnd != mShowWorkBadgeAtEnd) { |
| 228 | setClipToPadding(!showWorkBadgeAtEnd); |
| 229 | mShowWorkBadgeAtEnd = showWorkBadgeAtEnd; |
| 230 | } |
| 231 | } |
| 232 | |
Selim Cinek | 0d07c7e | 2016-01-27 18:38:31 -0800 | [diff] [blame] | 233 | public View getWorkProfileIcon() { |
| 234 | return mProfileBadge; |
| 235 | } |
| 236 | |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 237 | public class HeaderTouchListener implements View.OnTouchListener { |
| 238 | |
| 239 | private final ArrayList<Rect> mTouchRects = new ArrayList<>(); |
| 240 | private int mTouchSlop; |
| 241 | private boolean mTrackGesture; |
| 242 | private float mDownX; |
| 243 | private float mDownY; |
| 244 | |
| 245 | public HeaderTouchListener() { |
| 246 | } |
| 247 | |
| 248 | public void bindTouchRects() { |
| 249 | mTouchRects.clear(); |
| 250 | addRectAroundViewView(mIcon); |
| 251 | addRectAroundViewView(mExpandButton); |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 252 | addWidthRect(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 253 | mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
| 254 | } |
| 255 | |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 256 | private void addWidthRect() { |
| 257 | Rect r = new Rect(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 258 | r.top = 0; |
| 259 | r.bottom = (int) (32 * getResources().getDisplayMetrics().density); |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 260 | r.left = 0; |
| 261 | r.right = getWidth(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 262 | mTouchRects.add(r); |
| 263 | } |
| 264 | |
| 265 | private void addRectAroundViewView(View view) { |
| 266 | final Rect r = getRectAroundView(view); |
| 267 | mTouchRects.add(r); |
| 268 | } |
| 269 | |
| 270 | private Rect getRectAroundView(View view) { |
| 271 | float size = 48 * getResources().getDisplayMetrics().density; |
| 272 | final Rect r = new Rect(); |
| 273 | if (view.getVisibility() == GONE) { |
| 274 | view = getFirstChildNotGone(); |
| 275 | r.left = (int) (view.getLeft() - size / 2.0f); |
| 276 | } else { |
| 277 | r.left = (int) ((view.getLeft() + view.getRight()) / 2.0f - size / 2.0f); |
| 278 | } |
| 279 | r.top = (int) ((view.getTop() + view.getBottom()) / 2.0f - size / 2.0f); |
| 280 | r.bottom = (int) (r.top + size); |
| 281 | r.right = (int) (r.left + size); |
| 282 | return r; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public boolean onTouch(View v, MotionEvent event) { |
| 287 | float x = event.getX(); |
| 288 | float y = event.getY(); |
| 289 | switch (event.getActionMasked() & MotionEvent.ACTION_MASK) { |
| 290 | case MotionEvent.ACTION_DOWN: |
| 291 | mTrackGesture = false; |
| 292 | if (isInside(x, y)) { |
| 293 | mTrackGesture = true; |
| 294 | return true; |
| 295 | } |
| 296 | break; |
| 297 | case MotionEvent.ACTION_MOVE: |
| 298 | if (mTrackGesture) { |
| 299 | if (Math.abs(mDownX - x) > mTouchSlop |
| 300 | || Math.abs(mDownY - y) > mTouchSlop) { |
| 301 | mTrackGesture = false; |
| 302 | } |
| 303 | } |
| 304 | break; |
| 305 | case MotionEvent.ACTION_UP: |
| 306 | if (mTrackGesture) { |
| 307 | mExpandClickListener.onClick(NotificationHeaderView.this); |
| 308 | } |
| 309 | break; |
| 310 | } |
| 311 | return mTrackGesture; |
| 312 | } |
| 313 | |
| 314 | private boolean isInside(float x, float y) { |
| 315 | for (int i = 0; i < mTouchRects.size(); i++) { |
| 316 | Rect r = mTouchRects.get(i); |
| 317 | if (r.contains((int) x, (int) y)) { |
| 318 | mDownX = x; |
| 319 | mDownY = y; |
| 320 | return true; |
| 321 | } |
| 322 | } |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | private View getFirstChildNotGone() { |
| 328 | for (int i = 0; i < getChildCount(); i++) { |
| 329 | final View child = getChildAt(i); |
| 330 | if (child.getVisibility() != GONE) { |
| 331 | return child; |
| 332 | } |
| 333 | } |
| 334 | return this; |
| 335 | } |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 336 | |
| 337 | public ImageView getExpandButton() { |
| 338 | return mExpandButton; |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public boolean hasOverlappingRendering() { |
| 343 | return false; |
| 344 | } |
Selim Cinek | 6183d12 | 2016-01-14 18:48:41 -0800 | [diff] [blame] | 345 | |
| 346 | public boolean isInTouchRect(float x, float y) { |
| 347 | if (mExpandClickListener == null) { |
| 348 | return false; |
| 349 | } |
| 350 | return mTouchListener.isInside(x, y); |
| 351 | } |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 352 | } |