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; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 20 | import android.app.AppOpsManager; |
Selim Cinek | 9910483 | 2017-01-25 14:47:33 -0800 | [diff] [blame] | 21 | import android.app.Notification; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 23 | import android.content.Context; |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Lucas Dupin | bd9798f | 2017-10-24 18:04:51 -0700 | [diff] [blame] | 25 | import android.content.res.TypedArray; |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.Outline; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 28 | import android.graphics.Rect; |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 30 | import android.util.ArraySet; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 31 | import android.util.AttributeSet; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 32 | import android.widget.ImageView; |
Beth Thibodeau | 750ec58 | 2019-09-18 15:14:09 -0400 | [diff] [blame] | 33 | import android.widget.LinearLayout; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 34 | import android.widget.RemoteViews; |
| 35 | |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 36 | import com.android.internal.R; |
Selim Cinek | 0242fbb | 2016-10-19 13:38:32 -0700 | [diff] [blame] | 37 | import com.android.internal.widget.CachingIconView; |
Selim Cinek | 20d1ee2 | 2020-02-03 16:04:26 -0500 | [diff] [blame] | 38 | import com.android.internal.widget.NotificationExpandButton; |
Selim Cinek | 0242fbb | 2016-10-19 13:38:32 -0700 | [diff] [blame] | 39 | |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 40 | import java.util.ArrayList; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * A header of a notification view |
| 44 | * |
| 45 | * @hide |
| 46 | */ |
| 47 | @RemoteViews.RemoteView |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 48 | public class NotificationHeaderView extends ViewGroup { |
Selim Cinek | 9910483 | 2017-01-25 14:47:33 -0800 | [diff] [blame] | 49 | public static final int NO_COLOR = Notification.COLOR_INVALID; |
Selim Cinek | 413142a | 2016-02-03 10:58:13 -0800 | [diff] [blame] | 50 | private final int mChildMinWidth; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 51 | private final int mContentEndMargin; |
Lucas Dupin | bd9798f | 2017-10-24 18:04:51 -0700 | [diff] [blame] | 52 | private final int mGravity; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 53 | private View mAppName; |
Selim Cinek | 0f9dd1e | 2016-04-05 17:03:40 -0700 | [diff] [blame] | 54 | private View mHeaderText; |
Selim Cinek | afeed29 | 2017-12-12 17:32:44 -0800 | [diff] [blame] | 55 | private View mSecondaryHeaderText; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 56 | private OnClickListener mExpandClickListener; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 57 | private OnClickListener mAppOpsListener; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 58 | private HeaderTouchListener mTouchListener = new HeaderTouchListener(); |
Beth Thibodeau | 750ec58 | 2019-09-18 15:14:09 -0400 | [diff] [blame] | 59 | private LinearLayout mTransferChip; |
Selim Cinek | 20d1ee2 | 2020-02-03 16:04:26 -0500 | [diff] [blame] | 60 | private NotificationExpandButton mExpandButton; |
Selim Cinek | 0242fbb | 2016-10-19 13:38:32 -0700 | [diff] [blame] | 61 | private CachingIconView mIcon; |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 62 | private View mProfileBadge; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 63 | private View mAppOps; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 64 | private boolean mExpanded; |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 65 | private boolean mShowExpandButtonAtEnd; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 66 | private boolean mShowWorkBadgeAtEnd; |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 67 | private int mHeaderTextMarginEnd; |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 68 | private Drawable mBackground; |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 69 | private boolean mEntireHeaderClickable; |
Selim Cinek | 499c20f | 2017-07-20 14:06:09 -0700 | [diff] [blame] | 70 | private boolean mExpandOnlyOnButton; |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 71 | private boolean mAcceptAllTouches; |
Lucas Dupin | bd9798f | 2017-10-24 18:04:51 -0700 | [diff] [blame] | 72 | private int mTotalWidth; |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 73 | |
| 74 | ViewOutlineProvider mProvider = new ViewOutlineProvider() { |
| 75 | @Override |
| 76 | public void getOutline(View view, Outline outline) { |
| 77 | if (mBackground != null) { |
Selim Cinek | afeed29 | 2017-12-12 17:32:44 -0800 | [diff] [blame] | 78 | outline.setRect(0, 0, getWidth(), getHeight()); |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 79 | outline.setAlpha(1f); |
| 80 | } |
| 81 | } |
| 82 | }; |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 83 | |
| 84 | public NotificationHeaderView(Context context) { |
| 85 | this(context, null); |
| 86 | } |
| 87 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 88 | @UnsupportedAppUsage |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 89 | public NotificationHeaderView(Context context, @Nullable AttributeSet attrs) { |
| 90 | this(context, attrs, 0); |
| 91 | } |
| 92 | |
| 93 | public NotificationHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 94 | this(context, attrs, defStyleAttr, 0); |
| 95 | } |
| 96 | |
| 97 | public NotificationHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 98 | super(context, attrs, defStyleAttr, defStyleRes); |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 99 | Resources res = getResources(); |
| 100 | mChildMinWidth = res.getDimensionPixelSize(R.dimen.notification_header_shrink_min_width); |
| 101 | mContentEndMargin = res.getDimensionPixelSize(R.dimen.notification_content_margin_end); |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 102 | mEntireHeaderClickable = res.getBoolean(R.bool.config_notificationHeaderClickableForExpand); |
Lucas Dupin | bd9798f | 2017-10-24 18:04:51 -0700 | [diff] [blame] | 103 | |
| 104 | int[] attrIds = { android.R.attr.gravity }; |
| 105 | TypedArray ta = context.obtainStyledAttributes(attrs, attrIds, defStyleAttr, defStyleRes); |
| 106 | mGravity = ta.getInt(0, 0); |
| 107 | ta.recycle(); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected void onFinishInflate() { |
| 112 | super.onFinishInflate(); |
| 113 | mAppName = findViewById(com.android.internal.R.id.app_name_text); |
Selim Cinek | 0f9dd1e | 2016-04-05 17:03:40 -0700 | [diff] [blame] | 114 | mHeaderText = findViewById(com.android.internal.R.id.header_text); |
Selim Cinek | afeed29 | 2017-12-12 17:32:44 -0800 | [diff] [blame] | 115 | mSecondaryHeaderText = findViewById(com.android.internal.R.id.header_text_secondary); |
Beth Thibodeau | 750ec58 | 2019-09-18 15:14:09 -0400 | [diff] [blame] | 116 | mTransferChip = findViewById(com.android.internal.R.id.media_seamless); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 117 | mExpandButton = findViewById(com.android.internal.R.id.expand_button); |
| 118 | mIcon = findViewById(com.android.internal.R.id.icon); |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 119 | mProfileBadge = findViewById(com.android.internal.R.id.profile_badge); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 120 | mAppOps = findViewById(com.android.internal.R.id.app_ops); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 125 | final int givenWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 126 | final int givenHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 127 | int wrapContentWidthSpec = MeasureSpec.makeMeasureSpec(givenWidth, |
| 128 | MeasureSpec.AT_MOST); |
| 129 | int wrapContentHeightSpec = MeasureSpec.makeMeasureSpec(givenHeight, |
| 130 | MeasureSpec.AT_MOST); |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 131 | int totalWidth = getPaddingStart(); |
| 132 | int iconWidth = getPaddingEnd(); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 133 | for (int i = 0; i < getChildCount(); i++) { |
| 134 | final View child = getChildAt(i); |
| 135 | if (child.getVisibility() == GONE) { |
| 136 | // We'll give it the rest of the space in the end |
| 137 | continue; |
| 138 | } |
| 139 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 140 | int childWidthSpec = getChildMeasureSpec(wrapContentWidthSpec, |
| 141 | lp.leftMargin + lp.rightMargin, lp.width); |
| 142 | int childHeightSpec = getChildMeasureSpec(wrapContentHeightSpec, |
| 143 | lp.topMargin + lp.bottomMargin, lp.height); |
| 144 | child.measure(childWidthSpec, childHeightSpec); |
Beth Thibodeau | 750ec58 | 2019-09-18 15:14:09 -0400 | [diff] [blame] | 145 | // Icons that should go at the end |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 146 | if ((child == mExpandButton && mShowExpandButtonAtEnd) |
| 147 | || child == mProfileBadge |
Beth Thibodeau | 750ec58 | 2019-09-18 15:14:09 -0400 | [diff] [blame] | 148 | || child == mAppOps |
| 149 | || child == mTransferChip) { |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 150 | iconWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth(); |
| 151 | } else { |
| 152 | totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth(); |
| 153 | } |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 154 | } |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 155 | |
| 156 | // Ensure that there is at least enough space for the icons |
| 157 | int endMargin = Math.max(mHeaderTextMarginEnd, iconWidth); |
| 158 | if (totalWidth > givenWidth - endMargin) { |
| 159 | int overFlow = totalWidth - givenWidth + endMargin; |
Selim Cinek | 0f9dd1e | 2016-04-05 17:03:40 -0700 | [diff] [blame] | 160 | // We are overflowing, lets shrink the app name first |
Selim Cinek | afeed29 | 2017-12-12 17:32:44 -0800 | [diff] [blame] | 161 | overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mAppName, |
| 162 | mChildMinWidth); |
| 163 | |
| 164 | // still overflowing, we shrink the header text |
| 165 | overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mHeaderText, 0); |
| 166 | |
| 167 | // still overflowing, finally we shrink the secondary header text |
| 168 | shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mSecondaryHeaderText, |
| 169 | 0); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 170 | } |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 171 | totalWidth += getPaddingEnd(); |
Lucas Dupin | bd9798f | 2017-10-24 18:04:51 -0700 | [diff] [blame] | 172 | mTotalWidth = Math.min(totalWidth, givenWidth); |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 173 | setMeasuredDimension(givenWidth, givenHeight); |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 174 | } |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 175 | |
Selim Cinek | afeed29 | 2017-12-12 17:32:44 -0800 | [diff] [blame] | 176 | private int shrinkViewForOverflow(int heightSpec, int overFlow, View targetView, |
| 177 | int minimumWidth) { |
| 178 | final int oldWidth = targetView.getMeasuredWidth(); |
| 179 | if (overFlow > 0 && targetView.getVisibility() != GONE && oldWidth > minimumWidth) { |
| 180 | // we're still too big |
| 181 | int newSize = Math.max(minimumWidth, oldWidth - overFlow); |
| 182 | int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST); |
| 183 | targetView.measure(childWidthSpec, heightSpec); |
| 184 | overFlow -= oldWidth - newSize; |
| 185 | } |
| 186 | return overFlow; |
| 187 | } |
| 188 | |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 189 | @Override |
| 190 | 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] | 191 | int left = getPaddingStart(); |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 192 | int end = getMeasuredWidth(); |
Lucas Dupin | bd9798f | 2017-10-24 18:04:51 -0700 | [diff] [blame] | 193 | final boolean centerAligned = (mGravity & Gravity.CENTER_HORIZONTAL) != 0; |
| 194 | if (centerAligned) { |
| 195 | left += getMeasuredWidth() / 2 - mTotalWidth / 2; |
| 196 | } |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 197 | int childCount = getChildCount(); |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 198 | int ownHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 199 | for (int i = 0; i < childCount; i++) { |
| 200 | View child = getChildAt(i); |
| 201 | if (child.getVisibility() == GONE) { |
| 202 | continue; |
| 203 | } |
| 204 | int childHeight = child.getMeasuredHeight(); |
| 205 | MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams(); |
Gus Prevas | c79816b | 2018-12-27 15:46:00 -0500 | [diff] [blame] | 206 | int layoutLeft; |
| 207 | int layoutRight; |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 208 | int top = (int) (getPaddingTop() + (ownHeight - childHeight) / 2.0f); |
| 209 | int bottom = top + childHeight; |
Beth Thibodeau | 750ec58 | 2019-09-18 15:14:09 -0400 | [diff] [blame] | 210 | // Icons that should go at the end |
Gus Prevas | 8771761 | 2018-12-17 14:00:12 -0500 | [diff] [blame] | 211 | if ((child == mExpandButton && mShowExpandButtonAtEnd) |
| 212 | || child == mProfileBadge |
Beth Thibodeau | 750ec58 | 2019-09-18 15:14:09 -0400 | [diff] [blame] | 213 | || child == mAppOps |
| 214 | || child == mTransferChip) { |
Gus Prevas | 8771761 | 2018-12-17 14:00:12 -0500 | [diff] [blame] | 215 | if (end == getMeasuredWidth()) { |
| 216 | layoutRight = end - mContentEndMargin; |
| 217 | } else { |
| 218 | layoutRight = end - params.getMarginEnd(); |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 219 | } |
Gus Prevas | 8771761 | 2018-12-17 14:00:12 -0500 | [diff] [blame] | 220 | layoutLeft = layoutRight - child.getMeasuredWidth(); |
| 221 | end = layoutLeft - params.getMarginStart(); |
Gus Prevas | c79816b | 2018-12-27 15:46:00 -0500 | [diff] [blame] | 222 | } else { |
| 223 | left += params.getMarginStart(); |
| 224 | int right = left + child.getMeasuredWidth(); |
| 225 | layoutLeft = left; |
| 226 | layoutRight = right; |
| 227 | left = right + params.getMarginEnd(); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 228 | } |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 229 | if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) { |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 230 | int ltrLeft = layoutLeft; |
| 231 | layoutLeft = getWidth() - layoutRight; |
| 232 | layoutRight = getWidth() - ltrLeft; |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 233 | } |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 234 | child.layout(layoutLeft, top, layoutRight, bottom); |
Selim Cinek | c848c3a | 2016-01-13 15:27:30 -0800 | [diff] [blame] | 235 | } |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 236 | updateTouchListener(); |
| 237 | } |
| 238 | |
Selim Cinek | cb44568 | 2016-01-29 16:13:12 -0800 | [diff] [blame] | 239 | @Override |
| 240 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 241 | return new ViewGroup.MarginLayoutParams(getContext(), attrs); |
| 242 | } |
| 243 | |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Set a {@link Drawable} to be displayed as a background on the header. |
| 246 | */ |
| 247 | public void setHeaderBackgroundDrawable(Drawable drawable) { |
| 248 | if (drawable != null) { |
| 249 | setWillNotDraw(false); |
| 250 | mBackground = drawable; |
| 251 | mBackground.setCallback(this); |
| 252 | setOutlineProvider(mProvider); |
| 253 | } else { |
| 254 | setWillNotDraw(true); |
| 255 | mBackground = null; |
| 256 | setOutlineProvider(null); |
| 257 | } |
| 258 | invalidate(); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | protected void onDraw(Canvas canvas) { |
| 263 | if (mBackground != null) { |
Selim Cinek | afeed29 | 2017-12-12 17:32:44 -0800 | [diff] [blame] | 264 | mBackground.setBounds(0, 0, getWidth(), getHeight()); |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 265 | mBackground.draw(canvas); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | protected boolean verifyDrawable(Drawable who) { |
| 271 | return super.verifyDrawable(who) || who == mBackground; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | protected void drawableStateChanged() { |
| 276 | if (mBackground != null && mBackground.isStateful()) { |
| 277 | mBackground.setState(getDrawableState()); |
| 278 | } |
| 279 | } |
| 280 | |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 281 | private void updateTouchListener() { |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 282 | if (mExpandClickListener == null && mAppOpsListener == null) { |
| 283 | setOnTouchListener(null); |
| 284 | return; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 285 | } |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 286 | setOnTouchListener(mTouchListener); |
| 287 | mTouchListener.bindTouchRects(); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Sets onclick listener for app ops icons. |
| 292 | */ |
| 293 | public void setAppOpsOnClickListener(OnClickListener l) { |
| 294 | mAppOpsListener = l; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 295 | updateTouchListener(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void setOnClickListener(@Nullable OnClickListener l) { |
| 300 | mExpandClickListener = l; |
Selim Cinek | e9bad24 | 2016-06-15 11:46:37 -0700 | [diff] [blame] | 301 | mExpandButton.setOnClickListener(mExpandClickListener); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 302 | updateTouchListener(); |
| 303 | } |
| 304 | |
Selim Cinek | ea4bef7 | 2015-12-02 15:51:10 -0800 | [diff] [blame] | 305 | public int getOriginalIconColor() { |
Selim Cinek | 79d9863 | 2020-03-24 19:16:02 -0700 | [diff] [blame] | 306 | return mIcon.getOriginalIconColor(); |
Selim Cinek | ea4bef7 | 2015-12-02 15:51:10 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Selim Cinek | ea4bef7 | 2015-12-02 15:51:10 -0800 | [diff] [blame] | 309 | public int getOriginalNotificationColor() { |
Selim Cinek | 20d1ee2 | 2020-02-03 16:04:26 -0500 | [diff] [blame] | 310 | return mExpandButton.getOriginalNotificationColor(); |
Selim Cinek | ea4bef7 | 2015-12-02 15:51:10 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 313 | @RemotableViewMethod |
| 314 | public void setExpanded(boolean expanded) { |
| 315 | mExpanded = expanded; |
| 316 | updateExpandButton(); |
| 317 | } |
| 318 | |
| 319 | private void updateExpandButton() { |
| 320 | int drawableId; |
Selim Cinek | c0ac4af | 2017-03-03 15:13:48 -0800 | [diff] [blame] | 321 | int contentDescriptionId; |
Selim Cinek | 6db5758 | 2016-03-04 19:11:27 -0800 | [diff] [blame] | 322 | if (mExpanded) { |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 323 | drawableId = R.drawable.ic_collapse_notification; |
| 324 | contentDescriptionId = R.string.expand_button_content_description_expanded; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 325 | } else { |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 326 | drawableId = R.drawable.ic_expand_notification; |
| 327 | contentDescriptionId = R.string.expand_button_content_description_collapsed; |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 328 | } |
| 329 | mExpandButton.setImageDrawable(getContext().getDrawable(drawableId)); |
Selim Cinek | 20d1ee2 | 2020-02-03 16:04:26 -0500 | [diff] [blame] | 330 | mExpandButton.setColorFilter(getOriginalNotificationColor()); |
Selim Cinek | c0ac4af | 2017-03-03 15:13:48 -0800 | [diff] [blame] | 331 | mExpandButton.setContentDescription(mContext.getText(contentDescriptionId)); |
Selim Cinek | 7b83639 | 2015-12-04 20:02:59 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 334 | public void setShowWorkBadgeAtEnd(boolean showWorkBadgeAtEnd) { |
| 335 | if (showWorkBadgeAtEnd != mShowWorkBadgeAtEnd) { |
| 336 | setClipToPadding(!showWorkBadgeAtEnd); |
| 337 | mShowWorkBadgeAtEnd = showWorkBadgeAtEnd; |
| 338 | } |
| 339 | } |
| 340 | |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 341 | /** |
| 342 | * Sets whether or not the expand button appears at the end of the NotificationHeaderView. If |
| 343 | * both this and {@link #setShowWorkBadgeAtEnd(boolean)} have been set to true, then the |
| 344 | * expand button will appear closer to the end than the work badge. |
| 345 | */ |
| 346 | public void setShowExpandButtonAtEnd(boolean showExpandButtonAtEnd) { |
| 347 | if (showExpandButtonAtEnd != mShowExpandButtonAtEnd) { |
| 348 | setClipToPadding(!showExpandButtonAtEnd); |
| 349 | mShowExpandButtonAtEnd = showExpandButtonAtEnd; |
| 350 | } |
| 351 | } |
| 352 | |
Selim Cinek | 0d07c7e | 2016-01-27 18:38:31 -0800 | [diff] [blame] | 353 | public View getWorkProfileIcon() { |
| 354 | return mProfileBadge; |
| 355 | } |
| 356 | |
Selim Cinek | 0242fbb | 2016-10-19 13:38:32 -0700 | [diff] [blame] | 357 | public CachingIconView getIcon() { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 358 | return mIcon; |
| 359 | } |
| 360 | |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 361 | /** |
| 362 | * Sets the margin end for the text portion of the header, excluding right-aligned elements |
| 363 | * @param headerTextMarginEnd margin size |
| 364 | */ |
| 365 | @RemotableViewMethod |
| 366 | public void setHeaderTextMarginEnd(int headerTextMarginEnd) { |
| 367 | if (mHeaderTextMarginEnd != headerTextMarginEnd) { |
| 368 | mHeaderTextMarginEnd = headerTextMarginEnd; |
| 369 | requestLayout(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Get the current margin end value for the header text |
| 375 | * @return margin size |
| 376 | */ |
| 377 | public int getHeaderTextMarginEnd() { |
| 378 | return mHeaderTextMarginEnd; |
| 379 | } |
| 380 | |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 381 | public class HeaderTouchListener implements View.OnTouchListener { |
| 382 | |
| 383 | private final ArrayList<Rect> mTouchRects = new ArrayList<>(); |
Selim Cinek | 499c20f | 2017-07-20 14:06:09 -0700 | [diff] [blame] | 384 | private Rect mExpandButtonRect; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 385 | private Rect mAppOpsRect; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 386 | private int mTouchSlop; |
| 387 | private boolean mTrackGesture; |
| 388 | private float mDownX; |
| 389 | private float mDownY; |
| 390 | |
| 391 | public HeaderTouchListener() { |
| 392 | } |
| 393 | |
| 394 | public void bindTouchRects() { |
| 395 | mTouchRects.clear(); |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 396 | addRectAroundView(mIcon); |
Selim Cinek | 499c20f | 2017-07-20 14:06:09 -0700 | [diff] [blame] | 397 | mExpandButtonRect = addRectAroundView(mExpandButton); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 398 | mAppOpsRect = addRectAroundView(mAppOps); |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 399 | addWidthRect(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 400 | mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
| 401 | } |
| 402 | |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 403 | private void addWidthRect() { |
| 404 | Rect r = new Rect(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 405 | r.top = 0; |
| 406 | r.bottom = (int) (32 * getResources().getDisplayMetrics().density); |
Selim Cinek | 4c4c738 | 2016-02-03 16:17:09 -0800 | [diff] [blame] | 407 | r.left = 0; |
| 408 | r.right = getWidth(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 409 | mTouchRects.add(r); |
| 410 | } |
| 411 | |
Selim Cinek | 499c20f | 2017-07-20 14:06:09 -0700 | [diff] [blame] | 412 | private Rect addRectAroundView(View view) { |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 413 | final Rect r = getRectAroundView(view); |
| 414 | mTouchRects.add(r); |
Selim Cinek | 499c20f | 2017-07-20 14:06:09 -0700 | [diff] [blame] | 415 | return r; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | private Rect getRectAroundView(View view) { |
| 419 | float size = 48 * getResources().getDisplayMetrics().density; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 420 | float width = Math.max(size, view.getWidth()); |
| 421 | float height = Math.max(size, view.getHeight()); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 422 | final Rect r = new Rect(); |
| 423 | if (view.getVisibility() == GONE) { |
| 424 | view = getFirstChildNotGone(); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 425 | r.left = (int) (view.getLeft() - width / 2.0f); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 426 | } else { |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 427 | r.left = (int) ((view.getLeft() + view.getRight()) / 2.0f - width / 2.0f); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 428 | } |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 429 | r.top = (int) ((view.getTop() + view.getBottom()) / 2.0f - height / 2.0f); |
| 430 | r.bottom = (int) (r.top + height); |
| 431 | r.right = (int) (r.left + width); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 432 | return r; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public boolean onTouch(View v, MotionEvent event) { |
| 437 | float x = event.getX(); |
| 438 | float y = event.getY(); |
| 439 | switch (event.getActionMasked() & MotionEvent.ACTION_MASK) { |
| 440 | case MotionEvent.ACTION_DOWN: |
| 441 | mTrackGesture = false; |
| 442 | if (isInside(x, y)) { |
Selim Cinek | 1b55439 | 2017-02-28 17:22:49 -0800 | [diff] [blame] | 443 | mDownX = x; |
| 444 | mDownY = y; |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 445 | mTrackGesture = true; |
| 446 | return true; |
| 447 | } |
| 448 | break; |
| 449 | case MotionEvent.ACTION_MOVE: |
| 450 | if (mTrackGesture) { |
| 451 | if (Math.abs(mDownX - x) > mTouchSlop |
| 452 | || Math.abs(mDownY - y) > mTouchSlop) { |
| 453 | mTrackGesture = false; |
| 454 | } |
| 455 | } |
| 456 | break; |
| 457 | case MotionEvent.ACTION_UP: |
| 458 | if (mTrackGesture) { |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 459 | if (mAppOps.isVisibleToUser() && (mAppOpsRect.contains((int) x, (int) y) |
| 460 | || mAppOpsRect.contains((int) mDownX, (int) mDownY))) { |
| 461 | mAppOps.performClick(); |
| 462 | return true; |
| 463 | } |
Selim Cinek | c0ac4af | 2017-03-03 15:13:48 -0800 | [diff] [blame] | 464 | mExpandButton.performClick(); |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 465 | } |
| 466 | break; |
| 467 | } |
| 468 | return mTrackGesture; |
| 469 | } |
| 470 | |
| 471 | private boolean isInside(float x, float y) { |
Selim Cinek | 1b55439 | 2017-02-28 17:22:49 -0800 | [diff] [blame] | 472 | if (mAcceptAllTouches) { |
| 473 | return true; |
| 474 | } |
Selim Cinek | 499c20f | 2017-07-20 14:06:09 -0700 | [diff] [blame] | 475 | if (mExpandOnlyOnButton) { |
| 476 | return mExpandButtonRect.contains((int) x, (int) y); |
| 477 | } |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 478 | for (int i = 0; i < mTouchRects.size(); i++) { |
| 479 | Rect r = mTouchRects.get(i); |
| 480 | if (r.contains((int) x, (int) y)) { |
Selim Cinek | aef6c76 | 2015-11-20 17:00:18 -0800 | [diff] [blame] | 481 | return true; |
| 482 | } |
| 483 | } |
| 484 | return false; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | private View getFirstChildNotGone() { |
| 489 | for (int i = 0; i < getChildCount(); i++) { |
| 490 | final View child = getChildAt(i); |
| 491 | if (child.getVisibility() != GONE) { |
| 492 | return child; |
| 493 | } |
| 494 | } |
| 495 | return this; |
| 496 | } |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 497 | |
| 498 | public ImageView getExpandButton() { |
| 499 | return mExpandButton; |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public boolean hasOverlappingRendering() { |
| 504 | return false; |
| 505 | } |
Selim Cinek | 6183d12 | 2016-01-14 18:48:41 -0800 | [diff] [blame] | 506 | |
| 507 | public boolean isInTouchRect(float x, float y) { |
| 508 | if (mExpandClickListener == null) { |
| 509 | return false; |
| 510 | } |
| 511 | return mTouchListener.isInside(x, y); |
| 512 | } |
Selim Cinek | 1b55439 | 2017-02-28 17:22:49 -0800 | [diff] [blame] | 513 | |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 514 | /** |
| 515 | * Sets whether or not all touches to this header view will register as a click. Note that |
| 516 | * if the config value for {@code config_notificationHeaderClickableForExpand} is {@code true}, |
| 517 | * then calling this method with {@code false} will not override that configuration. |
| 518 | */ |
Selim Cinek | 1b55439 | 2017-02-28 17:22:49 -0800 | [diff] [blame] | 519 | @RemotableViewMethod |
| 520 | public void setAcceptAllTouches(boolean acceptAllTouches) { |
Anthony Chen | 0f6e96c | 2017-04-07 15:48:17 -0700 | [diff] [blame] | 521 | mAcceptAllTouches = mEntireHeaderClickable || acceptAllTouches; |
Selim Cinek | 1b55439 | 2017-02-28 17:22:49 -0800 | [diff] [blame] | 522 | } |
Selim Cinek | 499c20f | 2017-07-20 14:06:09 -0700 | [diff] [blame] | 523 | |
| 524 | /** |
| 525 | * Sets whether only the expand icon itself should serve as the expand target. |
| 526 | */ |
| 527 | @RemotableViewMethod |
| 528 | public void setExpandOnlyOnButton(boolean expandOnlyOnButton) { |
| 529 | mExpandOnlyOnButton = expandOnlyOnButton; |
| 530 | } |
Selim Cinek | 90c8f47 | 2015-11-10 17:44:39 -0500 | [diff] [blame] | 531 | } |