blob: 0c50cb782c2460c345e04e98ac11923bba66a132 [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;
Selim Cinek99104832017-01-25 14:47:33 -080020import android.app.Notification;
Artur Satayevad9254c2019-12-10 17:47:54 +000021import android.compat.annotation.UnsupportedAppUsage;
Selim Cinek90c8f472015-11-10 17:44:39 -050022import android.content.Context;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070023import android.content.res.Resources;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070024import android.content.res.TypedArray;
Mady Mellorb0a82462016-04-30 17:31:02 -070025import android.graphics.Canvas;
26import android.graphics.Outline;
Selim Cinekaef6c762015-11-20 17:00:18 -080027import android.graphics.Rect;
Mady Mellorb0a82462016-04-30 17:31:02 -070028import android.graphics.drawable.Drawable;
Selim Cinek90c8f472015-11-10 17:44:39 -050029import android.util.AttributeSet;
Selim Cinek7b836392015-12-04 20:02:59 -080030import android.widget.ImageView;
Beth Thibodeau750ec582019-09-18 15:14:09 -040031import android.widget.LinearLayout;
Selim Cinek90c8f472015-11-10 17:44:39 -050032import android.widget.RemoteViews;
33
Anthony Chen0f6e96c2017-04-07 15:48:17 -070034import com.android.internal.R;
Selim Cinek0242fbb2016-10-19 13:38:32 -070035import com.android.internal.widget.CachingIconView;
Selim Cinek20d1ee22020-02-03 16:04:26 -050036import com.android.internal.widget.NotificationExpandButton;
Selim Cinek0242fbb2016-10-19 13:38:32 -070037
Selim Cinekaef6c762015-11-20 17:00:18 -080038import java.util.ArrayList;
Selim Cinek90c8f472015-11-10 17:44:39 -050039
40/**
41 * A header of a notification view
42 *
43 * @hide
44 */
45@RemoteViews.RemoteView
Selim Cinekcb445682016-01-29 16:13:12 -080046public class NotificationHeaderView extends ViewGroup {
Selim Cinek99104832017-01-25 14:47:33 -080047 public static final int NO_COLOR = Notification.COLOR_INVALID;
Selim Cinek413142a2016-02-03 10:58:13 -080048 private final int mChildMinWidth;
Selim Cinek6ecc8102016-01-26 18:26:19 -080049 private final int mContentEndMargin;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070050 private final int mGravity;
Selim Cinek90c8f472015-11-10 17:44:39 -050051 private View mAppName;
Selim Cinek0f9dd1e2016-04-05 17:03:40 -070052 private View mHeaderText;
Selim Cinekafeed292017-12-12 17:32:44 -080053 private View mSecondaryHeaderText;
Selim Cinekaef6c762015-11-20 17:00:18 -080054 private OnClickListener mExpandClickListener;
Julia Reynoldsb5867452018-02-28 16:31:35 -050055 private OnClickListener mAppOpsListener;
Selim Cinekaef6c762015-11-20 17:00:18 -080056 private HeaderTouchListener mTouchListener = new HeaderTouchListener();
Beth Thibodeau750ec582019-09-18 15:14:09 -040057 private LinearLayout mTransferChip;
Selim Cinek20d1ee22020-02-03 16:04:26 -050058 private NotificationExpandButton mExpandButton;
Selim Cinek0242fbb2016-10-19 13:38:32 -070059 private CachingIconView mIcon;
Selim Cinekc848c3a2016-01-13 15:27:30 -080060 private View mProfileBadge;
Julia Reynoldsfc640012018-02-21 12:25:27 -050061 private View mAppOps;
Selim Cinek7b836392015-12-04 20:02:59 -080062 private boolean mExpanded;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070063 private boolean mShowExpandButtonAtEnd;
Selim Cinek6ecc8102016-01-26 18:26:19 -080064 private boolean mShowWorkBadgeAtEnd;
Beth Thibodeau837bfc22019-02-19 12:26:53 -050065 private int mHeaderTextMarginEnd;
Mady Mellorb0a82462016-04-30 17:31:02 -070066 private Drawable mBackground;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070067 private boolean mEntireHeaderClickable;
Selim Cinek499c20f2017-07-20 14:06:09 -070068 private boolean mExpandOnlyOnButton;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070069 private boolean mAcceptAllTouches;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070070 private int mTotalWidth;
Mady Mellorb0a82462016-04-30 17:31:02 -070071
72 ViewOutlineProvider mProvider = new ViewOutlineProvider() {
73 @Override
74 public void getOutline(View view, Outline outline) {
75 if (mBackground != null) {
Selim Cinekafeed292017-12-12 17:32:44 -080076 outline.setRect(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -070077 outline.setAlpha(1f);
78 }
79 }
80 };
Selim Cinek90c8f472015-11-10 17:44:39 -050081
82 public NotificationHeaderView(Context context) {
83 this(context, null);
84 }
85
Mathew Inwooda570dee2018-08-17 14:56:00 +010086 @UnsupportedAppUsage
Selim Cinek90c8f472015-11-10 17:44:39 -050087 public NotificationHeaderView(Context context, @Nullable AttributeSet attrs) {
88 this(context, attrs, 0);
89 }
90
91 public NotificationHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
92 this(context, attrs, defStyleAttr, 0);
93 }
94
95 public NotificationHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
96 super(context, attrs, defStyleAttr, defStyleRes);
Anthony Chen0f6e96c2017-04-07 15:48:17 -070097 Resources res = getResources();
98 mChildMinWidth = res.getDimensionPixelSize(R.dimen.notification_header_shrink_min_width);
99 mContentEndMargin = res.getDimensionPixelSize(R.dimen.notification_content_margin_end);
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700100 mEntireHeaderClickable = res.getBoolean(R.bool.config_notificationHeaderClickableForExpand);
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700101
102 int[] attrIds = { android.R.attr.gravity };
103 TypedArray ta = context.obtainStyledAttributes(attrs, attrIds, defStyleAttr, defStyleRes);
104 mGravity = ta.getInt(0, 0);
105 ta.recycle();
Selim Cinek90c8f472015-11-10 17:44:39 -0500106 }
107
108 @Override
109 protected void onFinishInflate() {
110 super.onFinishInflate();
111 mAppName = findViewById(com.android.internal.R.id.app_name_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700112 mHeaderText = findViewById(com.android.internal.R.id.header_text);
Selim Cinekafeed292017-12-12 17:32:44 -0800113 mSecondaryHeaderText = findViewById(com.android.internal.R.id.header_text_secondary);
Beth Thibodeau750ec582019-09-18 15:14:09 -0400114 mTransferChip = findViewById(com.android.internal.R.id.media_seamless);
Alan Viverette51efddb2017-04-05 10:00:01 -0400115 mExpandButton = findViewById(com.android.internal.R.id.expand_button);
116 mIcon = findViewById(com.android.internal.R.id.icon);
Selim Cinekc848c3a2016-01-13 15:27:30 -0800117 mProfileBadge = findViewById(com.android.internal.R.id.profile_badge);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500118 mAppOps = findViewById(com.android.internal.R.id.app_ops);
Selim Cinek90c8f472015-11-10 17:44:39 -0500119 }
120
121 @Override
122 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
123 final int givenWidth = MeasureSpec.getSize(widthMeasureSpec);
124 final int givenHeight = MeasureSpec.getSize(heightMeasureSpec);
125 int wrapContentWidthSpec = MeasureSpec.makeMeasureSpec(givenWidth,
126 MeasureSpec.AT_MOST);
127 int wrapContentHeightSpec = MeasureSpec.makeMeasureSpec(givenHeight,
128 MeasureSpec.AT_MOST);
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500129 int totalWidth = getPaddingStart();
130 int iconWidth = getPaddingEnd();
Selim Cinek90c8f472015-11-10 17:44:39 -0500131 for (int i = 0; i < getChildCount(); i++) {
132 final View child = getChildAt(i);
133 if (child.getVisibility() == GONE) {
134 // We'll give it the rest of the space in the end
135 continue;
136 }
137 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
138 int childWidthSpec = getChildMeasureSpec(wrapContentWidthSpec,
139 lp.leftMargin + lp.rightMargin, lp.width);
140 int childHeightSpec = getChildMeasureSpec(wrapContentHeightSpec,
141 lp.topMargin + lp.bottomMargin, lp.height);
142 child.measure(childWidthSpec, childHeightSpec);
Beth Thibodeau750ec582019-09-18 15:14:09 -0400143 // Icons that should go at the end
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500144 if ((child == mExpandButton && mShowExpandButtonAtEnd)
145 || child == mProfileBadge
Beth Thibodeau750ec582019-09-18 15:14:09 -0400146 || child == mAppOps
147 || child == mTransferChip) {
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500148 iconWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
149 } else {
150 totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
151 }
Selim Cinek90c8f472015-11-10 17:44:39 -0500152 }
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500153
154 // Ensure that there is at least enough space for the icons
155 int endMargin = Math.max(mHeaderTextMarginEnd, iconWidth);
156 if (totalWidth > givenWidth - endMargin) {
157 int overFlow = totalWidth - givenWidth + endMargin;
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700158 // We are overflowing, lets shrink the app name first
Selim Cinekafeed292017-12-12 17:32:44 -0800159 overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mAppName,
160 mChildMinWidth);
161
162 // still overflowing, we shrink the header text
163 overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mHeaderText, 0);
164
165 // still overflowing, finally we shrink the secondary header text
166 shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mSecondaryHeaderText,
167 0);
Selim Cinek90c8f472015-11-10 17:44:39 -0500168 }
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500169 totalWidth += getPaddingEnd();
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700170 mTotalWidth = Math.min(totalWidth, givenWidth);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800171 setMeasuredDimension(givenWidth, givenHeight);
Selim Cinek90c8f472015-11-10 17:44:39 -0500172 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800173
Selim Cinekafeed292017-12-12 17:32:44 -0800174 private int shrinkViewForOverflow(int heightSpec, int overFlow, View targetView,
175 int minimumWidth) {
176 final int oldWidth = targetView.getMeasuredWidth();
177 if (overFlow > 0 && targetView.getVisibility() != GONE && oldWidth > minimumWidth) {
178 // we're still too big
179 int newSize = Math.max(minimumWidth, oldWidth - overFlow);
180 int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST);
181 targetView.measure(childWidthSpec, heightSpec);
182 overFlow -= oldWidth - newSize;
183 }
184 return overFlow;
185 }
186
Selim Cinekaef6c762015-11-20 17:00:18 -0800187 @Override
188 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Selim Cinekcb445682016-01-29 16:13:12 -0800189 int left = getPaddingStart();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700190 int end = getMeasuredWidth();
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700191 final boolean centerAligned = (mGravity & Gravity.CENTER_HORIZONTAL) != 0;
192 if (centerAligned) {
193 left += getMeasuredWidth() / 2 - mTotalWidth / 2;
194 }
Selim Cinekcb445682016-01-29 16:13:12 -0800195 int childCount = getChildCount();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700196 int ownHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Selim Cinekcb445682016-01-29 16:13:12 -0800197 for (int i = 0; i < childCount; i++) {
198 View child = getChildAt(i);
199 if (child.getVisibility() == GONE) {
200 continue;
201 }
202 int childHeight = child.getMeasuredHeight();
203 MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
Gus Prevasc79816b2018-12-27 15:46:00 -0500204 int layoutLeft;
205 int layoutRight;
Selim Cinekcb445682016-01-29 16:13:12 -0800206 int top = (int) (getPaddingTop() + (ownHeight - childHeight) / 2.0f);
207 int bottom = top + childHeight;
Beth Thibodeau750ec582019-09-18 15:14:09 -0400208 // Icons that should go at the end
Gus Prevas87717612018-12-17 14:00:12 -0500209 if ((child == mExpandButton && mShowExpandButtonAtEnd)
210 || child == mProfileBadge
Beth Thibodeau750ec582019-09-18 15:14:09 -0400211 || child == mAppOps
212 || child == mTransferChip) {
Gus Prevas87717612018-12-17 14:00:12 -0500213 if (end == getMeasuredWidth()) {
214 layoutRight = end - mContentEndMargin;
215 } else {
216 layoutRight = end - params.getMarginEnd();
Selim Cinekcb445682016-01-29 16:13:12 -0800217 }
Gus Prevas87717612018-12-17 14:00:12 -0500218 layoutLeft = layoutRight - child.getMeasuredWidth();
219 end = layoutLeft - params.getMarginStart();
Gus Prevasc79816b2018-12-27 15:46:00 -0500220 } else {
221 left += params.getMarginStart();
222 int right = left + child.getMeasuredWidth();
223 layoutLeft = left;
224 layoutRight = right;
225 left = right + params.getMarginEnd();
Julia Reynoldsfc640012018-02-21 12:25:27 -0500226 }
Selim Cinekc848c3a2016-01-13 15:27:30 -0800227 if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Selim Cinekcb445682016-01-29 16:13:12 -0800228 int ltrLeft = layoutLeft;
229 layoutLeft = getWidth() - layoutRight;
230 layoutRight = getWidth() - ltrLeft;
Selim Cinekc848c3a2016-01-13 15:27:30 -0800231 }
Selim Cinekcb445682016-01-29 16:13:12 -0800232 child.layout(layoutLeft, top, layoutRight, bottom);
Selim Cinekc848c3a2016-01-13 15:27:30 -0800233 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800234 updateTouchListener();
235 }
236
Selim Cinekcb445682016-01-29 16:13:12 -0800237 @Override
238 public LayoutParams generateLayoutParams(AttributeSet attrs) {
239 return new ViewGroup.MarginLayoutParams(getContext(), attrs);
240 }
241
Mady Mellorb0a82462016-04-30 17:31:02 -0700242 /**
243 * Set a {@link Drawable} to be displayed as a background on the header.
244 */
245 public void setHeaderBackgroundDrawable(Drawable drawable) {
246 if (drawable != null) {
247 setWillNotDraw(false);
248 mBackground = drawable;
249 mBackground.setCallback(this);
250 setOutlineProvider(mProvider);
251 } else {
252 setWillNotDraw(true);
253 mBackground = null;
254 setOutlineProvider(null);
255 }
256 invalidate();
257 }
258
259 @Override
260 protected void onDraw(Canvas canvas) {
261 if (mBackground != null) {
Selim Cinekafeed292017-12-12 17:32:44 -0800262 mBackground.setBounds(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -0700263 mBackground.draw(canvas);
264 }
265 }
266
267 @Override
268 protected boolean verifyDrawable(Drawable who) {
269 return super.verifyDrawable(who) || who == mBackground;
270 }
271
272 @Override
273 protected void drawableStateChanged() {
274 if (mBackground != null && mBackground.isStateful()) {
275 mBackground.setState(getDrawableState());
276 }
277 }
278
Selim Cinekaef6c762015-11-20 17:00:18 -0800279 private void updateTouchListener() {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500280 if (mExpandClickListener == null && mAppOpsListener == null) {
281 setOnTouchListener(null);
282 return;
Selim Cinekaef6c762015-11-20 17:00:18 -0800283 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500284 setOnTouchListener(mTouchListener);
285 mTouchListener.bindTouchRects();
286 }
287
288 /**
289 * Sets onclick listener for app ops icons.
290 */
291 public void setAppOpsOnClickListener(OnClickListener l) {
292 mAppOpsListener = l;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500293 updateTouchListener();
Selim Cinekaef6c762015-11-20 17:00:18 -0800294 }
295
296 @Override
297 public void setOnClickListener(@Nullable OnClickListener l) {
298 mExpandClickListener = l;
Selim Cineke9bad242016-06-15 11:46:37 -0700299 mExpandButton.setOnClickListener(mExpandClickListener);
Selim Cinekaef6c762015-11-20 17:00:18 -0800300 updateTouchListener();
301 }
302
Selim Cinekea4bef72015-12-02 15:51:10 -0800303 public int getOriginalIconColor() {
Selim Cinek79d98632020-03-24 19:16:02 -0700304 return mIcon.getOriginalIconColor();
Selim Cinekea4bef72015-12-02 15:51:10 -0800305 }
306
Selim Cinekea4bef72015-12-02 15:51:10 -0800307 public int getOriginalNotificationColor() {
Selim Cinek20d1ee22020-02-03 16:04:26 -0500308 return mExpandButton.getOriginalNotificationColor();
Selim Cinekea4bef72015-12-02 15:51:10 -0800309 }
310
Selim Cinek7b836392015-12-04 20:02:59 -0800311 @RemotableViewMethod
312 public void setExpanded(boolean expanded) {
313 mExpanded = expanded;
314 updateExpandButton();
315 }
316
317 private void updateExpandButton() {
318 int drawableId;
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800319 int contentDescriptionId;
Selim Cinek6db57582016-03-04 19:11:27 -0800320 if (mExpanded) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700321 drawableId = R.drawable.ic_collapse_notification;
322 contentDescriptionId = R.string.expand_button_content_description_expanded;
Selim Cinek7b836392015-12-04 20:02:59 -0800323 } else {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700324 drawableId = R.drawable.ic_expand_notification;
325 contentDescriptionId = R.string.expand_button_content_description_collapsed;
Selim Cinek7b836392015-12-04 20:02:59 -0800326 }
327 mExpandButton.setImageDrawable(getContext().getDrawable(drawableId));
Selim Cinek20d1ee22020-02-03 16:04:26 -0500328 mExpandButton.setColorFilter(getOriginalNotificationColor());
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800329 mExpandButton.setContentDescription(mContext.getText(contentDescriptionId));
Selim Cinek7b836392015-12-04 20:02:59 -0800330 }
331
Selim Cinek6ecc8102016-01-26 18:26:19 -0800332 public void setShowWorkBadgeAtEnd(boolean showWorkBadgeAtEnd) {
333 if (showWorkBadgeAtEnd != mShowWorkBadgeAtEnd) {
334 setClipToPadding(!showWorkBadgeAtEnd);
335 mShowWorkBadgeAtEnd = showWorkBadgeAtEnd;
336 }
337 }
338
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700339 /**
340 * Sets whether or not the expand button appears at the end of the NotificationHeaderView. If
341 * both this and {@link #setShowWorkBadgeAtEnd(boolean)} have been set to true, then the
342 * expand button will appear closer to the end than the work badge.
343 */
344 public void setShowExpandButtonAtEnd(boolean showExpandButtonAtEnd) {
345 if (showExpandButtonAtEnd != mShowExpandButtonAtEnd) {
346 setClipToPadding(!showExpandButtonAtEnd);
347 mShowExpandButtonAtEnd = showExpandButtonAtEnd;
348 }
349 }
350
Selim Cinek0d07c7e2016-01-27 18:38:31 -0800351 public View getWorkProfileIcon() {
352 return mProfileBadge;
353 }
354
Selim Cinek0242fbb2016-10-19 13:38:32 -0700355 public CachingIconView getIcon() {
Selim Cinek281c2022016-10-13 19:14:43 -0700356 return mIcon;
357 }
358
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500359 /**
360 * Sets the margin end for the text portion of the header, excluding right-aligned elements
361 * @param headerTextMarginEnd margin size
362 */
363 @RemotableViewMethod
364 public void setHeaderTextMarginEnd(int headerTextMarginEnd) {
365 if (mHeaderTextMarginEnd != headerTextMarginEnd) {
366 mHeaderTextMarginEnd = headerTextMarginEnd;
367 requestLayout();
368 }
369 }
370
371 /**
372 * Get the current margin end value for the header text
373 * @return margin size
374 */
375 public int getHeaderTextMarginEnd() {
376 return mHeaderTextMarginEnd;
377 }
378
Selim Cinekaef6c762015-11-20 17:00:18 -0800379 public class HeaderTouchListener implements View.OnTouchListener {
380
381 private final ArrayList<Rect> mTouchRects = new ArrayList<>();
Selim Cinek499c20f2017-07-20 14:06:09 -0700382 private Rect mExpandButtonRect;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500383 private Rect mAppOpsRect;
Selim Cinekaef6c762015-11-20 17:00:18 -0800384 private int mTouchSlop;
385 private boolean mTrackGesture;
386 private float mDownX;
387 private float mDownY;
388
389 public HeaderTouchListener() {
390 }
391
392 public void bindTouchRects() {
393 mTouchRects.clear();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700394 addRectAroundView(mIcon);
Selim Cinek499c20f2017-07-20 14:06:09 -0700395 mExpandButtonRect = addRectAroundView(mExpandButton);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500396 mAppOpsRect = addRectAroundView(mAppOps);
Beverly717dead2020-04-15 13:46:14 -0400397 setTouchDelegate(new TouchDelegate(mAppOpsRect, mAppOps));
Selim Cinek4c4c7382016-02-03 16:17:09 -0800398 addWidthRect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800399 mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
400 }
401
Selim Cinek4c4c7382016-02-03 16:17:09 -0800402 private void addWidthRect() {
403 Rect r = new Rect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800404 r.top = 0;
405 r.bottom = (int) (32 * getResources().getDisplayMetrics().density);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800406 r.left = 0;
407 r.right = getWidth();
Selim Cinekaef6c762015-11-20 17:00:18 -0800408 mTouchRects.add(r);
409 }
410
Selim Cinek499c20f2017-07-20 14:06:09 -0700411 private Rect addRectAroundView(View view) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800412 final Rect r = getRectAroundView(view);
413 mTouchRects.add(r);
Selim Cinek499c20f2017-07-20 14:06:09 -0700414 return r;
Selim Cinekaef6c762015-11-20 17:00:18 -0800415 }
416
417 private Rect getRectAroundView(View view) {
418 float size = 48 * getResources().getDisplayMetrics().density;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500419 float width = Math.max(size, view.getWidth());
420 float height = Math.max(size, view.getHeight());
Selim Cinekaef6c762015-11-20 17:00:18 -0800421 final Rect r = new Rect();
422 if (view.getVisibility() == GONE) {
423 view = getFirstChildNotGone();
Julia Reynoldsb5867452018-02-28 16:31:35 -0500424 r.left = (int) (view.getLeft() - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800425 } else {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500426 r.left = (int) ((view.getLeft() + view.getRight()) / 2.0f - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800427 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500428 r.top = (int) ((view.getTop() + view.getBottom()) / 2.0f - height / 2.0f);
429 r.bottom = (int) (r.top + height);
430 r.right = (int) (r.left + width);
Selim Cinekaef6c762015-11-20 17:00:18 -0800431 return r;
432 }
433
434 @Override
435 public boolean onTouch(View v, MotionEvent event) {
436 float x = event.getX();
437 float y = event.getY();
438 switch (event.getActionMasked() & MotionEvent.ACTION_MASK) {
439 case MotionEvent.ACTION_DOWN:
440 mTrackGesture = false;
441 if (isInside(x, y)) {
Selim Cinek1b554392017-02-28 17:22:49 -0800442 mDownX = x;
443 mDownY = y;
Selim Cinekaef6c762015-11-20 17:00:18 -0800444 mTrackGesture = true;
445 return true;
446 }
447 break;
448 case MotionEvent.ACTION_MOVE:
449 if (mTrackGesture) {
450 if (Math.abs(mDownX - x) > mTouchSlop
451 || Math.abs(mDownY - y) > mTouchSlop) {
452 mTrackGesture = false;
453 }
454 }
455 break;
456 case MotionEvent.ACTION_UP:
457 if (mTrackGesture) {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500458 if (mAppOps.isVisibleToUser() && (mAppOpsRect.contains((int) x, (int) y)
459 || mAppOpsRect.contains((int) mDownX, (int) mDownY))) {
460 mAppOps.performClick();
461 return true;
462 }
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800463 mExpandButton.performClick();
Selim Cinekaef6c762015-11-20 17:00:18 -0800464 }
465 break;
466 }
467 return mTrackGesture;
468 }
469
470 private boolean isInside(float x, float y) {
Selim Cinek1b554392017-02-28 17:22:49 -0800471 if (mAcceptAllTouches) {
472 return true;
473 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700474 if (mExpandOnlyOnButton) {
475 return mExpandButtonRect.contains((int) x, (int) y);
476 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800477 for (int i = 0; i < mTouchRects.size(); i++) {
478 Rect r = mTouchRects.get(i);
479 if (r.contains((int) x, (int) y)) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800480 return true;
481 }
482 }
483 return false;
484 }
485 }
486
487 private View getFirstChildNotGone() {
488 for (int i = 0; i < getChildCount(); i++) {
489 final View child = getChildAt(i);
490 if (child.getVisibility() != GONE) {
491 return child;
492 }
493 }
494 return this;
495 }
Selim Cinek4ffd6362015-12-29 15:12:23 +0100496
497 public ImageView getExpandButton() {
498 return mExpandButton;
499 }
500
501 @Override
502 public boolean hasOverlappingRendering() {
503 return false;
504 }
Selim Cinek6183d122016-01-14 18:48:41 -0800505
506 public boolean isInTouchRect(float x, float y) {
507 if (mExpandClickListener == null) {
508 return false;
509 }
510 return mTouchListener.isInside(x, y);
511 }
Selim Cinek1b554392017-02-28 17:22:49 -0800512
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700513 /**
514 * Sets whether or not all touches to this header view will register as a click. Note that
515 * if the config value for {@code config_notificationHeaderClickableForExpand} is {@code true},
516 * then calling this method with {@code false} will not override that configuration.
517 */
Selim Cinek1b554392017-02-28 17:22:49 -0800518 @RemotableViewMethod
519 public void setAcceptAllTouches(boolean acceptAllTouches) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700520 mAcceptAllTouches = mEntireHeaderClickable || acceptAllTouches;
Selim Cinek1b554392017-02-28 17:22:49 -0800521 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700522
523 /**
524 * Sets whether only the expand icon itself should serve as the expand target.
525 */
526 @RemotableViewMethod
527 public void setExpandOnlyOnButton(boolean expandOnlyOnButton) {
528 mExpandOnlyOnButton = expandOnlyOnButton;
529 }
Selim Cinek90c8f472015-11-10 17:44:39 -0500530}