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