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