blob: 18e0132e2c4eed59665ef081310dd27aba49b049 [file] [log] [blame]
Selim Cinek90c8f472015-11-10 17:44:39 -05001/*
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
17package android.view;
18
19import android.annotation.Nullable;
Julia Reynoldsfc640012018-02-21 12:25:27 -050020import android.app.AppOpsManager;
Selim Cinek99104832017-01-25 14:47:33 -080021import android.app.Notification;
Artur Satayevad9254c2019-12-10 17:47:54 +000022import android.compat.annotation.UnsupportedAppUsage;
Selim Cinek90c8f472015-11-10 17:44:39 -050023import android.content.Context;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070024import android.content.res.Resources;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070025import android.content.res.TypedArray;
Mady Mellorb0a82462016-04-30 17:31:02 -070026import android.graphics.Canvas;
27import android.graphics.Outline;
Selim Cinekaef6c762015-11-20 17:00:18 -080028import android.graphics.Rect;
Mady Mellorb0a82462016-04-30 17:31:02 -070029import android.graphics.drawable.Drawable;
Julia Reynoldsfc640012018-02-21 12:25:27 -050030import android.util.ArraySet;
Selim Cinek90c8f472015-11-10 17:44:39 -050031import android.util.AttributeSet;
Selim Cinek7b836392015-12-04 20:02:59 -080032import android.widget.ImageView;
Beth Thibodeau750ec582019-09-18 15:14:09 -040033import android.widget.LinearLayout;
Selim Cinek90c8f472015-11-10 17:44:39 -050034import android.widget.RemoteViews;
35
Anthony Chen0f6e96c2017-04-07 15:48:17 -070036import com.android.internal.R;
Selim Cinek0242fbb2016-10-19 13:38:32 -070037import com.android.internal.widget.CachingIconView;
Selim Cinek20d1ee22020-02-03 16:04:26 -050038import com.android.internal.widget.NotificationExpandButton;
Selim Cinek0242fbb2016-10-19 13:38:32 -070039
Selim Cinekaef6c762015-11-20 17:00:18 -080040import java.util.ArrayList;
Selim Cinek90c8f472015-11-10 17:44:39 -050041
42/**
43 * A header of a notification view
44 *
45 * @hide
46 */
47@RemoteViews.RemoteView
Selim Cinekcb445682016-01-29 16:13:12 -080048public class NotificationHeaderView extends ViewGroup {
Selim Cinek99104832017-01-25 14:47:33 -080049 public static final int NO_COLOR = Notification.COLOR_INVALID;
Selim Cinek413142a2016-02-03 10:58:13 -080050 private final int mChildMinWidth;
Selim Cinek6ecc8102016-01-26 18:26:19 -080051 private final int mContentEndMargin;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070052 private final int mGravity;
Selim Cinek90c8f472015-11-10 17:44:39 -050053 private View mAppName;
Selim Cinek0f9dd1e2016-04-05 17:03:40 -070054 private View mHeaderText;
Selim Cinekafeed292017-12-12 17:32:44 -080055 private View mSecondaryHeaderText;
Selim Cinekaef6c762015-11-20 17:00:18 -080056 private OnClickListener mExpandClickListener;
Julia Reynoldsb5867452018-02-28 16:31:35 -050057 private OnClickListener mAppOpsListener;
Selim Cinekaef6c762015-11-20 17:00:18 -080058 private HeaderTouchListener mTouchListener = new HeaderTouchListener();
Beth Thibodeau750ec582019-09-18 15:14:09 -040059 private LinearLayout mTransferChip;
Selim Cinek20d1ee22020-02-03 16:04:26 -050060 private NotificationExpandButton mExpandButton;
Selim Cinek0242fbb2016-10-19 13:38:32 -070061 private CachingIconView mIcon;
Selim Cinekc848c3a2016-01-13 15:27:30 -080062 private View mProfileBadge;
Julia Reynoldsfc640012018-02-21 12:25:27 -050063 private View mOverlayIcon;
64 private View mCameraIcon;
65 private View mMicIcon;
66 private View mAppOps;
Gus Prevasa3226492018-10-23 11:10:09 -040067 private View mAudiblyAlertedIcon;
Selim Cinekea4bef72015-12-02 15:51:10 -080068 private int mIconColor;
Selim Cinek7b836392015-12-04 20:02:59 -080069 private boolean mExpanded;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070070 private boolean mShowExpandButtonAtEnd;
Selim Cinek6ecc8102016-01-26 18:26:19 -080071 private boolean mShowWorkBadgeAtEnd;
Beth Thibodeau837bfc22019-02-19 12:26:53 -050072 private int mHeaderTextMarginEnd;
Mady Mellorb0a82462016-04-30 17:31:02 -070073 private Drawable mBackground;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070074 private boolean mEntireHeaderClickable;
Selim Cinek499c20f2017-07-20 14:06:09 -070075 private boolean mExpandOnlyOnButton;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070076 private boolean mAcceptAllTouches;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070077 private int mTotalWidth;
Mady Mellorb0a82462016-04-30 17:31:02 -070078
79 ViewOutlineProvider mProvider = new ViewOutlineProvider() {
80 @Override
81 public void getOutline(View view, Outline outline) {
82 if (mBackground != null) {
Selim Cinekafeed292017-12-12 17:32:44 -080083 outline.setRect(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -070084 outline.setAlpha(1f);
85 }
86 }
87 };
Selim Cinek90c8f472015-11-10 17:44:39 -050088
89 public NotificationHeaderView(Context context) {
90 this(context, null);
91 }
92
Mathew Inwooda570dee2018-08-17 14:56:00 +010093 @UnsupportedAppUsage
Selim Cinek90c8f472015-11-10 17:44:39 -050094 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 Chen0f6e96c2017-04-07 15:48:17 -0700104 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 Chen0f6e96c2017-04-07 15:48:17 -0700107 mEntireHeaderClickable = res.getBoolean(R.bool.config_notificationHeaderClickableForExpand);
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700108
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 Cinek90c8f472015-11-10 17:44:39 -0500113 }
114
115 @Override
116 protected void onFinishInflate() {
117 super.onFinishInflate();
118 mAppName = findViewById(com.android.internal.R.id.app_name_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700119 mHeaderText = findViewById(com.android.internal.R.id.header_text);
Selim Cinekafeed292017-12-12 17:32:44 -0800120 mSecondaryHeaderText = findViewById(com.android.internal.R.id.header_text_secondary);
Beth Thibodeau750ec582019-09-18 15:14:09 -0400121 mTransferChip = findViewById(com.android.internal.R.id.media_seamless);
Alan Viverette51efddb2017-04-05 10:00:01 -0400122 mExpandButton = findViewById(com.android.internal.R.id.expand_button);
123 mIcon = findViewById(com.android.internal.R.id.icon);
Selim Cinekc848c3a2016-01-13 15:27:30 -0800124 mProfileBadge = findViewById(com.android.internal.R.id.profile_badge);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500125 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 Prevasa3226492018-10-23 11:10:09 -0400129 mAudiblyAlertedIcon = findViewById(com.android.internal.R.id.alerted_icon);
Selim Cinek90c8f472015-11-10 17:44:39 -0500130 }
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 Thibodeau837bfc22019-02-19 12:26:53 -0500140 int totalWidth = getPaddingStart();
141 int iconWidth = getPaddingEnd();
Selim Cinek90c8f472015-11-10 17:44:39 -0500142 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 Thibodeau750ec582019-09-18 15:14:09 -0400154 // Icons that should go at the end
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500155 if ((child == mExpandButton && mShowExpandButtonAtEnd)
156 || child == mProfileBadge
Beth Thibodeau750ec582019-09-18 15:14:09 -0400157 || child == mAppOps
158 || child == mTransferChip) {
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500159 iconWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
160 } else {
161 totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
162 }
Selim Cinek90c8f472015-11-10 17:44:39 -0500163 }
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500164
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 Cinek0f9dd1e2016-04-05 17:03:40 -0700169 // We are overflowing, lets shrink the app name first
Selim Cinekafeed292017-12-12 17:32:44 -0800170 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 Cinek90c8f472015-11-10 17:44:39 -0500179 }
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500180 totalWidth += getPaddingEnd();
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700181 mTotalWidth = Math.min(totalWidth, givenWidth);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800182 setMeasuredDimension(givenWidth, givenHeight);
Selim Cinek90c8f472015-11-10 17:44:39 -0500183 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800184
Selim Cinekafeed292017-12-12 17:32:44 -0800185 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 Cinekaef6c762015-11-20 17:00:18 -0800198 @Override
199 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Selim Cinekcb445682016-01-29 16:13:12 -0800200 int left = getPaddingStart();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700201 int end = getMeasuredWidth();
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700202 final boolean centerAligned = (mGravity & Gravity.CENTER_HORIZONTAL) != 0;
203 if (centerAligned) {
204 left += getMeasuredWidth() / 2 - mTotalWidth / 2;
205 }
Selim Cinekcb445682016-01-29 16:13:12 -0800206 int childCount = getChildCount();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700207 int ownHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Selim Cinekcb445682016-01-29 16:13:12 -0800208 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 Prevasc79816b2018-12-27 15:46:00 -0500215 int layoutLeft;
216 int layoutRight;
Selim Cinekcb445682016-01-29 16:13:12 -0800217 int top = (int) (getPaddingTop() + (ownHeight - childHeight) / 2.0f);
218 int bottom = top + childHeight;
Beth Thibodeau750ec582019-09-18 15:14:09 -0400219 // Icons that should go at the end
Gus Prevas87717612018-12-17 14:00:12 -0500220 if ((child == mExpandButton && mShowExpandButtonAtEnd)
221 || child == mProfileBadge
Beth Thibodeau750ec582019-09-18 15:14:09 -0400222 || child == mAppOps
223 || child == mTransferChip) {
Gus Prevas87717612018-12-17 14:00:12 -0500224 if (end == getMeasuredWidth()) {
225 layoutRight = end - mContentEndMargin;
226 } else {
227 layoutRight = end - params.getMarginEnd();
Selim Cinekcb445682016-01-29 16:13:12 -0800228 }
Gus Prevas87717612018-12-17 14:00:12 -0500229 layoutLeft = layoutRight - child.getMeasuredWidth();
230 end = layoutLeft - params.getMarginStart();
Gus Prevasc79816b2018-12-27 15:46:00 -0500231 } else {
232 left += params.getMarginStart();
233 int right = left + child.getMeasuredWidth();
234 layoutLeft = left;
235 layoutRight = right;
236 left = right + params.getMarginEnd();
Julia Reynoldsfc640012018-02-21 12:25:27 -0500237 }
Selim Cinekc848c3a2016-01-13 15:27:30 -0800238 if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Selim Cinekcb445682016-01-29 16:13:12 -0800239 int ltrLeft = layoutLeft;
240 layoutLeft = getWidth() - layoutRight;
241 layoutRight = getWidth() - ltrLeft;
Selim Cinekc848c3a2016-01-13 15:27:30 -0800242 }
Selim Cinekcb445682016-01-29 16:13:12 -0800243 child.layout(layoutLeft, top, layoutRight, bottom);
Selim Cinekc848c3a2016-01-13 15:27:30 -0800244 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800245 updateTouchListener();
246 }
247
Selim Cinekcb445682016-01-29 16:13:12 -0800248 @Override
249 public LayoutParams generateLayoutParams(AttributeSet attrs) {
250 return new ViewGroup.MarginLayoutParams(getContext(), attrs);
251 }
252
Mady Mellorb0a82462016-04-30 17:31:02 -0700253 /**
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 Cinekafeed292017-12-12 17:32:44 -0800273 mBackground.setBounds(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -0700274 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 Cinekaef6c762015-11-20 17:00:18 -0800290 private void updateTouchListener() {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500291 if (mExpandClickListener == null && mAppOpsListener == null) {
292 setOnTouchListener(null);
293 return;
Selim Cinekaef6c762015-11-20 17:00:18 -0800294 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500295 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 Hsu3a9b5a92018-05-15 19:02:52 +0800305 mCameraIcon.setOnClickListener(mAppOpsListener);
306 mMicIcon.setOnClickListener(mAppOpsListener);
307 mOverlayIcon.setOnClickListener(mAppOpsListener);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500308 updateTouchListener();
Selim Cinekaef6c762015-11-20 17:00:18 -0800309 }
310
311 @Override
312 public void setOnClickListener(@Nullable OnClickListener l) {
313 mExpandClickListener = l;
Selim Cineke9bad242016-06-15 11:46:37 -0700314 mExpandButton.setOnClickListener(mExpandClickListener);
Selim Cinekaef6c762015-11-20 17:00:18 -0800315 updateTouchListener();
316 }
317
Selim Cinekea4bef72015-12-02 15:51:10 -0800318 @RemotableViewMethod
319 public void setOriginalIconColor(int color) {
320 mIconColor = color;
321 }
322
323 public int getOriginalIconColor() {
324 return mIconColor;
325 }
326
Selim Cinekea4bef72015-12-02 15:51:10 -0800327 public int getOriginalNotificationColor() {
Selim Cinek20d1ee22020-02-03 16:04:26 -0500328 return mExpandButton.getOriginalNotificationColor();
Selim Cinekea4bef72015-12-02 15:51:10 -0800329 }
330
Selim Cinek7b836392015-12-04 20:02:59 -0800331 @RemotableViewMethod
332 public void setExpanded(boolean expanded) {
333 mExpanded = expanded;
334 updateExpandButton();
335 }
336
Julia Reynoldsfc640012018-02-21 12:25:27 -0500337 /**
338 * Shows or hides 'app op in use' icons based on app usage.
339 */
340 public void showAppOpsIcons(ArraySet<Integer> appOps) {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500341 if (mOverlayIcon == null || mCameraIcon == null || mMicIcon == null || appOps == null) {
Julia Reynoldsfc640012018-02-21 12:25:27 -0500342 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 Prevasa3226492018-10-23 11:10:09 -0400353 /** Updates icon visibility based on the noisiness of the notification. */
Gus Prevas7306b902018-12-11 10:57:06 -0500354 public void setRecentlyAudiblyAlerted(boolean audiblyAlerted) {
Gus Prevasa3226492018-10-23 11:10:09 -0400355 mAudiblyAlertedIcon.setVisibility(audiblyAlerted ? View.VISIBLE : View.GONE);
356 }
357
Selim Cinek7b836392015-12-04 20:02:59 -0800358 private void updateExpandButton() {
359 int drawableId;
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800360 int contentDescriptionId;
Selim Cinek6db57582016-03-04 19:11:27 -0800361 if (mExpanded) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700362 drawableId = R.drawable.ic_collapse_notification;
363 contentDescriptionId = R.string.expand_button_content_description_expanded;
Selim Cinek7b836392015-12-04 20:02:59 -0800364 } else {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700365 drawableId = R.drawable.ic_expand_notification;
366 contentDescriptionId = R.string.expand_button_content_description_collapsed;
Selim Cinek7b836392015-12-04 20:02:59 -0800367 }
368 mExpandButton.setImageDrawable(getContext().getDrawable(drawableId));
Selim Cinek20d1ee22020-02-03 16:04:26 -0500369 mExpandButton.setColorFilter(getOriginalNotificationColor());
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800370 mExpandButton.setContentDescription(mContext.getText(contentDescriptionId));
Selim Cinek7b836392015-12-04 20:02:59 -0800371 }
372
Selim Cinek6ecc8102016-01-26 18:26:19 -0800373 public void setShowWorkBadgeAtEnd(boolean showWorkBadgeAtEnd) {
374 if (showWorkBadgeAtEnd != mShowWorkBadgeAtEnd) {
375 setClipToPadding(!showWorkBadgeAtEnd);
376 mShowWorkBadgeAtEnd = showWorkBadgeAtEnd;
377 }
378 }
379
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700380 /**
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 Cinek0d07c7e2016-01-27 18:38:31 -0800392 public View getWorkProfileIcon() {
393 return mProfileBadge;
394 }
395
Selim Cinek0242fbb2016-10-19 13:38:32 -0700396 public CachingIconView getIcon() {
Selim Cinek281c2022016-10-13 19:14:43 -0700397 return mIcon;
398 }
399
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500400 /**
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 Cinekaef6c762015-11-20 17:00:18 -0800420 public class HeaderTouchListener implements View.OnTouchListener {
421
422 private final ArrayList<Rect> mTouchRects = new ArrayList<>();
Selim Cinek499c20f2017-07-20 14:06:09 -0700423 private Rect mExpandButtonRect;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500424 private Rect mAppOpsRect;
Selim Cinekaef6c762015-11-20 17:00:18 -0800425 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 Chen0f6e96c2017-04-07 15:48:17 -0700435 addRectAroundView(mIcon);
Selim Cinek499c20f2017-07-20 14:06:09 -0700436 mExpandButtonRect = addRectAroundView(mExpandButton);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500437 mAppOpsRect = addRectAroundView(mAppOps);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800438 addWidthRect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800439 mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
440 }
441
Selim Cinek4c4c7382016-02-03 16:17:09 -0800442 private void addWidthRect() {
443 Rect r = new Rect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800444 r.top = 0;
445 r.bottom = (int) (32 * getResources().getDisplayMetrics().density);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800446 r.left = 0;
447 r.right = getWidth();
Selim Cinekaef6c762015-11-20 17:00:18 -0800448 mTouchRects.add(r);
449 }
450
Selim Cinek499c20f2017-07-20 14:06:09 -0700451 private Rect addRectAroundView(View view) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800452 final Rect r = getRectAroundView(view);
453 mTouchRects.add(r);
Selim Cinek499c20f2017-07-20 14:06:09 -0700454 return r;
Selim Cinekaef6c762015-11-20 17:00:18 -0800455 }
456
457 private Rect getRectAroundView(View view) {
458 float size = 48 * getResources().getDisplayMetrics().density;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500459 float width = Math.max(size, view.getWidth());
460 float height = Math.max(size, view.getHeight());
Selim Cinekaef6c762015-11-20 17:00:18 -0800461 final Rect r = new Rect();
462 if (view.getVisibility() == GONE) {
463 view = getFirstChildNotGone();
Julia Reynoldsb5867452018-02-28 16:31:35 -0500464 r.left = (int) (view.getLeft() - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800465 } else {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500466 r.left = (int) ((view.getLeft() + view.getRight()) / 2.0f - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800467 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500468 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 Cinekaef6c762015-11-20 17:00:18 -0800471 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 Cinek1b554392017-02-28 17:22:49 -0800482 mDownX = x;
483 mDownY = y;
Selim Cinekaef6c762015-11-20 17:00:18 -0800484 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 Reynoldsb5867452018-02-28 16:31:35 -0500498 if (mAppOps.isVisibleToUser() && (mAppOpsRect.contains((int) x, (int) y)
499 || mAppOpsRect.contains((int) mDownX, (int) mDownY))) {
500 mAppOps.performClick();
501 return true;
502 }
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800503 mExpandButton.performClick();
Selim Cinekaef6c762015-11-20 17:00:18 -0800504 }
505 break;
506 }
507 return mTrackGesture;
508 }
509
510 private boolean isInside(float x, float y) {
Selim Cinek1b554392017-02-28 17:22:49 -0800511 if (mAcceptAllTouches) {
512 return true;
513 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700514 if (mExpandOnlyOnButton) {
515 return mExpandButtonRect.contains((int) x, (int) y);
516 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800517 for (int i = 0; i < mTouchRects.size(); i++) {
518 Rect r = mTouchRects.get(i);
519 if (r.contains((int) x, (int) y)) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800520 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 Cinek4ffd6362015-12-29 15:12:23 +0100536
537 public ImageView getExpandButton() {
538 return mExpandButton;
539 }
540
541 @Override
542 public boolean hasOverlappingRendering() {
543 return false;
544 }
Selim Cinek6183d122016-01-14 18:48:41 -0800545
546 public boolean isInTouchRect(float x, float y) {
547 if (mExpandClickListener == null) {
548 return false;
549 }
550 return mTouchListener.isInside(x, y);
551 }
Selim Cinek1b554392017-02-28 17:22:49 -0800552
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700553 /**
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 Cinek1b554392017-02-28 17:22:49 -0800558 @RemotableViewMethod
559 public void setAcceptAllTouches(boolean acceptAllTouches) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700560 mAcceptAllTouches = mEntireHeaderClickable || acceptAllTouches;
Selim Cinek1b554392017-02-28 17:22:49 -0800561 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700562
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 Cinek90c8f472015-11-10 17:44:39 -0500570}