blob: a9f3e04036b57e719565158006dd2092efaa94f0 [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 mAppOps;
Selim Cinek7b836392015-12-04 20:02:59 -080064 private boolean mExpanded;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070065 private boolean mShowExpandButtonAtEnd;
Selim Cinek6ecc8102016-01-26 18:26:19 -080066 private boolean mShowWorkBadgeAtEnd;
Beth Thibodeau837bfc22019-02-19 12:26:53 -050067 private int mHeaderTextMarginEnd;
Mady Mellorb0a82462016-04-30 17:31:02 -070068 private Drawable mBackground;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070069 private boolean mEntireHeaderClickable;
Selim Cinek499c20f2017-07-20 14:06:09 -070070 private boolean mExpandOnlyOnButton;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070071 private boolean mAcceptAllTouches;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070072 private int mTotalWidth;
Mady Mellorb0a82462016-04-30 17:31:02 -070073
74 ViewOutlineProvider mProvider = new ViewOutlineProvider() {
75 @Override
76 public void getOutline(View view, Outline outline) {
77 if (mBackground != null) {
Selim Cinekafeed292017-12-12 17:32:44 -080078 outline.setRect(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -070079 outline.setAlpha(1f);
80 }
81 }
82 };
Selim Cinek90c8f472015-11-10 17:44:39 -050083
84 public NotificationHeaderView(Context context) {
85 this(context, null);
86 }
87
Mathew Inwooda570dee2018-08-17 14:56:00 +010088 @UnsupportedAppUsage
Selim Cinek90c8f472015-11-10 17:44:39 -050089 public NotificationHeaderView(Context context, @Nullable AttributeSet attrs) {
90 this(context, attrs, 0);
91 }
92
93 public NotificationHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
94 this(context, attrs, defStyleAttr, 0);
95 }
96
97 public NotificationHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
98 super(context, attrs, defStyleAttr, defStyleRes);
Anthony Chen0f6e96c2017-04-07 15:48:17 -070099 Resources res = getResources();
100 mChildMinWidth = res.getDimensionPixelSize(R.dimen.notification_header_shrink_min_width);
101 mContentEndMargin = res.getDimensionPixelSize(R.dimen.notification_content_margin_end);
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700102 mEntireHeaderClickable = res.getBoolean(R.bool.config_notificationHeaderClickableForExpand);
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700103
104 int[] attrIds = { android.R.attr.gravity };
105 TypedArray ta = context.obtainStyledAttributes(attrs, attrIds, defStyleAttr, defStyleRes);
106 mGravity = ta.getInt(0, 0);
107 ta.recycle();
Selim Cinek90c8f472015-11-10 17:44:39 -0500108 }
109
110 @Override
111 protected void onFinishInflate() {
112 super.onFinishInflate();
113 mAppName = findViewById(com.android.internal.R.id.app_name_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700114 mHeaderText = findViewById(com.android.internal.R.id.header_text);
Selim Cinekafeed292017-12-12 17:32:44 -0800115 mSecondaryHeaderText = findViewById(com.android.internal.R.id.header_text_secondary);
Beth Thibodeau750ec582019-09-18 15:14:09 -0400116 mTransferChip = findViewById(com.android.internal.R.id.media_seamless);
Alan Viverette51efddb2017-04-05 10:00:01 -0400117 mExpandButton = findViewById(com.android.internal.R.id.expand_button);
118 mIcon = findViewById(com.android.internal.R.id.icon);
Selim Cinekc848c3a2016-01-13 15:27:30 -0800119 mProfileBadge = findViewById(com.android.internal.R.id.profile_badge);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500120 mAppOps = findViewById(com.android.internal.R.id.app_ops);
Selim Cinek90c8f472015-11-10 17:44:39 -0500121 }
122
123 @Override
124 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
125 final int givenWidth = MeasureSpec.getSize(widthMeasureSpec);
126 final int givenHeight = MeasureSpec.getSize(heightMeasureSpec);
127 int wrapContentWidthSpec = MeasureSpec.makeMeasureSpec(givenWidth,
128 MeasureSpec.AT_MOST);
129 int wrapContentHeightSpec = MeasureSpec.makeMeasureSpec(givenHeight,
130 MeasureSpec.AT_MOST);
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500131 int totalWidth = getPaddingStart();
132 int iconWidth = getPaddingEnd();
Selim Cinek90c8f472015-11-10 17:44:39 -0500133 for (int i = 0; i < getChildCount(); i++) {
134 final View child = getChildAt(i);
135 if (child.getVisibility() == GONE) {
136 // We'll give it the rest of the space in the end
137 continue;
138 }
139 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
140 int childWidthSpec = getChildMeasureSpec(wrapContentWidthSpec,
141 lp.leftMargin + lp.rightMargin, lp.width);
142 int childHeightSpec = getChildMeasureSpec(wrapContentHeightSpec,
143 lp.topMargin + lp.bottomMargin, lp.height);
144 child.measure(childWidthSpec, childHeightSpec);
Beth Thibodeau750ec582019-09-18 15:14:09 -0400145 // Icons that should go at the end
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500146 if ((child == mExpandButton && mShowExpandButtonAtEnd)
147 || child == mProfileBadge
Beth Thibodeau750ec582019-09-18 15:14:09 -0400148 || child == mAppOps
149 || child == mTransferChip) {
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500150 iconWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
151 } else {
152 totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
153 }
Selim Cinek90c8f472015-11-10 17:44:39 -0500154 }
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500155
156 // Ensure that there is at least enough space for the icons
157 int endMargin = Math.max(mHeaderTextMarginEnd, iconWidth);
158 if (totalWidth > givenWidth - endMargin) {
159 int overFlow = totalWidth - givenWidth + endMargin;
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700160 // We are overflowing, lets shrink the app name first
Selim Cinekafeed292017-12-12 17:32:44 -0800161 overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mAppName,
162 mChildMinWidth);
163
164 // still overflowing, we shrink the header text
165 overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mHeaderText, 0);
166
167 // still overflowing, finally we shrink the secondary header text
168 shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mSecondaryHeaderText,
169 0);
Selim Cinek90c8f472015-11-10 17:44:39 -0500170 }
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500171 totalWidth += getPaddingEnd();
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700172 mTotalWidth = Math.min(totalWidth, givenWidth);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800173 setMeasuredDimension(givenWidth, givenHeight);
Selim Cinek90c8f472015-11-10 17:44:39 -0500174 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800175
Selim Cinekafeed292017-12-12 17:32:44 -0800176 private int shrinkViewForOverflow(int heightSpec, int overFlow, View targetView,
177 int minimumWidth) {
178 final int oldWidth = targetView.getMeasuredWidth();
179 if (overFlow > 0 && targetView.getVisibility() != GONE && oldWidth > minimumWidth) {
180 // we're still too big
181 int newSize = Math.max(minimumWidth, oldWidth - overFlow);
182 int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST);
183 targetView.measure(childWidthSpec, heightSpec);
184 overFlow -= oldWidth - newSize;
185 }
186 return overFlow;
187 }
188
Selim Cinekaef6c762015-11-20 17:00:18 -0800189 @Override
190 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Selim Cinekcb445682016-01-29 16:13:12 -0800191 int left = getPaddingStart();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700192 int end = getMeasuredWidth();
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700193 final boolean centerAligned = (mGravity & Gravity.CENTER_HORIZONTAL) != 0;
194 if (centerAligned) {
195 left += getMeasuredWidth() / 2 - mTotalWidth / 2;
196 }
Selim Cinekcb445682016-01-29 16:13:12 -0800197 int childCount = getChildCount();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700198 int ownHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Selim Cinekcb445682016-01-29 16:13:12 -0800199 for (int i = 0; i < childCount; i++) {
200 View child = getChildAt(i);
201 if (child.getVisibility() == GONE) {
202 continue;
203 }
204 int childHeight = child.getMeasuredHeight();
205 MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
Gus Prevasc79816b2018-12-27 15:46:00 -0500206 int layoutLeft;
207 int layoutRight;
Selim Cinekcb445682016-01-29 16:13:12 -0800208 int top = (int) (getPaddingTop() + (ownHeight - childHeight) / 2.0f);
209 int bottom = top + childHeight;
Beth Thibodeau750ec582019-09-18 15:14:09 -0400210 // Icons that should go at the end
Gus Prevas87717612018-12-17 14:00:12 -0500211 if ((child == mExpandButton && mShowExpandButtonAtEnd)
212 || child == mProfileBadge
Beth Thibodeau750ec582019-09-18 15:14:09 -0400213 || child == mAppOps
214 || child == mTransferChip) {
Gus Prevas87717612018-12-17 14:00:12 -0500215 if (end == getMeasuredWidth()) {
216 layoutRight = end - mContentEndMargin;
217 } else {
218 layoutRight = end - params.getMarginEnd();
Selim Cinekcb445682016-01-29 16:13:12 -0800219 }
Gus Prevas87717612018-12-17 14:00:12 -0500220 layoutLeft = layoutRight - child.getMeasuredWidth();
221 end = layoutLeft - params.getMarginStart();
Gus Prevasc79816b2018-12-27 15:46:00 -0500222 } else {
223 left += params.getMarginStart();
224 int right = left + child.getMeasuredWidth();
225 layoutLeft = left;
226 layoutRight = right;
227 left = right + params.getMarginEnd();
Julia Reynoldsfc640012018-02-21 12:25:27 -0500228 }
Selim Cinekc848c3a2016-01-13 15:27:30 -0800229 if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Selim Cinekcb445682016-01-29 16:13:12 -0800230 int ltrLeft = layoutLeft;
231 layoutLeft = getWidth() - layoutRight;
232 layoutRight = getWidth() - ltrLeft;
Selim Cinekc848c3a2016-01-13 15:27:30 -0800233 }
Selim Cinekcb445682016-01-29 16:13:12 -0800234 child.layout(layoutLeft, top, layoutRight, bottom);
Selim Cinekc848c3a2016-01-13 15:27:30 -0800235 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800236 updateTouchListener();
237 }
238
Selim Cinekcb445682016-01-29 16:13:12 -0800239 @Override
240 public LayoutParams generateLayoutParams(AttributeSet attrs) {
241 return new ViewGroup.MarginLayoutParams(getContext(), attrs);
242 }
243
Mady Mellorb0a82462016-04-30 17:31:02 -0700244 /**
245 * Set a {@link Drawable} to be displayed as a background on the header.
246 */
247 public void setHeaderBackgroundDrawable(Drawable drawable) {
248 if (drawable != null) {
249 setWillNotDraw(false);
250 mBackground = drawable;
251 mBackground.setCallback(this);
252 setOutlineProvider(mProvider);
253 } else {
254 setWillNotDraw(true);
255 mBackground = null;
256 setOutlineProvider(null);
257 }
258 invalidate();
259 }
260
261 @Override
262 protected void onDraw(Canvas canvas) {
263 if (mBackground != null) {
Selim Cinekafeed292017-12-12 17:32:44 -0800264 mBackground.setBounds(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -0700265 mBackground.draw(canvas);
266 }
267 }
268
269 @Override
270 protected boolean verifyDrawable(Drawable who) {
271 return super.verifyDrawable(who) || who == mBackground;
272 }
273
274 @Override
275 protected void drawableStateChanged() {
276 if (mBackground != null && mBackground.isStateful()) {
277 mBackground.setState(getDrawableState());
278 }
279 }
280
Selim Cinekaef6c762015-11-20 17:00:18 -0800281 private void updateTouchListener() {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500282 if (mExpandClickListener == null && mAppOpsListener == null) {
283 setOnTouchListener(null);
284 return;
Selim Cinekaef6c762015-11-20 17:00:18 -0800285 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500286 setOnTouchListener(mTouchListener);
287 mTouchListener.bindTouchRects();
288 }
289
290 /**
291 * Sets onclick listener for app ops icons.
292 */
293 public void setAppOpsOnClickListener(OnClickListener l) {
294 mAppOpsListener = l;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500295 updateTouchListener();
Selim Cinekaef6c762015-11-20 17:00:18 -0800296 }
297
298 @Override
299 public void setOnClickListener(@Nullable OnClickListener l) {
300 mExpandClickListener = l;
Selim Cineke9bad242016-06-15 11:46:37 -0700301 mExpandButton.setOnClickListener(mExpandClickListener);
Selim Cinekaef6c762015-11-20 17:00:18 -0800302 updateTouchListener();
303 }
304
Selim Cinekea4bef72015-12-02 15:51:10 -0800305 public int getOriginalIconColor() {
Selim Cinek79d98632020-03-24 19:16:02 -0700306 return mIcon.getOriginalIconColor();
Selim Cinekea4bef72015-12-02 15:51:10 -0800307 }
308
Selim Cinekea4bef72015-12-02 15:51:10 -0800309 public int getOriginalNotificationColor() {
Selim Cinek20d1ee22020-02-03 16:04:26 -0500310 return mExpandButton.getOriginalNotificationColor();
Selim Cinekea4bef72015-12-02 15:51:10 -0800311 }
312
Selim Cinek7b836392015-12-04 20:02:59 -0800313 @RemotableViewMethod
314 public void setExpanded(boolean expanded) {
315 mExpanded = expanded;
316 updateExpandButton();
317 }
318
319 private void updateExpandButton() {
320 int drawableId;
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800321 int contentDescriptionId;
Selim Cinek6db57582016-03-04 19:11:27 -0800322 if (mExpanded) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700323 drawableId = R.drawable.ic_collapse_notification;
324 contentDescriptionId = R.string.expand_button_content_description_expanded;
Selim Cinek7b836392015-12-04 20:02:59 -0800325 } else {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700326 drawableId = R.drawable.ic_expand_notification;
327 contentDescriptionId = R.string.expand_button_content_description_collapsed;
Selim Cinek7b836392015-12-04 20:02:59 -0800328 }
329 mExpandButton.setImageDrawable(getContext().getDrawable(drawableId));
Selim Cinek20d1ee22020-02-03 16:04:26 -0500330 mExpandButton.setColorFilter(getOriginalNotificationColor());
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800331 mExpandButton.setContentDescription(mContext.getText(contentDescriptionId));
Selim Cinek7b836392015-12-04 20:02:59 -0800332 }
333
Selim Cinek6ecc8102016-01-26 18:26:19 -0800334 public void setShowWorkBadgeAtEnd(boolean showWorkBadgeAtEnd) {
335 if (showWorkBadgeAtEnd != mShowWorkBadgeAtEnd) {
336 setClipToPadding(!showWorkBadgeAtEnd);
337 mShowWorkBadgeAtEnd = showWorkBadgeAtEnd;
338 }
339 }
340
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700341 /**
342 * Sets whether or not the expand button appears at the end of the NotificationHeaderView. If
343 * both this and {@link #setShowWorkBadgeAtEnd(boolean)} have been set to true, then the
344 * expand button will appear closer to the end than the work badge.
345 */
346 public void setShowExpandButtonAtEnd(boolean showExpandButtonAtEnd) {
347 if (showExpandButtonAtEnd != mShowExpandButtonAtEnd) {
348 setClipToPadding(!showExpandButtonAtEnd);
349 mShowExpandButtonAtEnd = showExpandButtonAtEnd;
350 }
351 }
352
Selim Cinek0d07c7e2016-01-27 18:38:31 -0800353 public View getWorkProfileIcon() {
354 return mProfileBadge;
355 }
356
Selim Cinek0242fbb2016-10-19 13:38:32 -0700357 public CachingIconView getIcon() {
Selim Cinek281c2022016-10-13 19:14:43 -0700358 return mIcon;
359 }
360
Beth Thibodeau837bfc22019-02-19 12:26:53 -0500361 /**
362 * Sets the margin end for the text portion of the header, excluding right-aligned elements
363 * @param headerTextMarginEnd margin size
364 */
365 @RemotableViewMethod
366 public void setHeaderTextMarginEnd(int headerTextMarginEnd) {
367 if (mHeaderTextMarginEnd != headerTextMarginEnd) {
368 mHeaderTextMarginEnd = headerTextMarginEnd;
369 requestLayout();
370 }
371 }
372
373 /**
374 * Get the current margin end value for the header text
375 * @return margin size
376 */
377 public int getHeaderTextMarginEnd() {
378 return mHeaderTextMarginEnd;
379 }
380
Selim Cinekaef6c762015-11-20 17:00:18 -0800381 public class HeaderTouchListener implements View.OnTouchListener {
382
383 private final ArrayList<Rect> mTouchRects = new ArrayList<>();
Selim Cinek499c20f2017-07-20 14:06:09 -0700384 private Rect mExpandButtonRect;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500385 private Rect mAppOpsRect;
Selim Cinekaef6c762015-11-20 17:00:18 -0800386 private int mTouchSlop;
387 private boolean mTrackGesture;
388 private float mDownX;
389 private float mDownY;
390
391 public HeaderTouchListener() {
392 }
393
394 public void bindTouchRects() {
395 mTouchRects.clear();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700396 addRectAroundView(mIcon);
Selim Cinek499c20f2017-07-20 14:06:09 -0700397 mExpandButtonRect = addRectAroundView(mExpandButton);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500398 mAppOpsRect = addRectAroundView(mAppOps);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800399 addWidthRect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800400 mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
401 }
402
Selim Cinek4c4c7382016-02-03 16:17:09 -0800403 private void addWidthRect() {
404 Rect r = new Rect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800405 r.top = 0;
406 r.bottom = (int) (32 * getResources().getDisplayMetrics().density);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800407 r.left = 0;
408 r.right = getWidth();
Selim Cinekaef6c762015-11-20 17:00:18 -0800409 mTouchRects.add(r);
410 }
411
Selim Cinek499c20f2017-07-20 14:06:09 -0700412 private Rect addRectAroundView(View view) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800413 final Rect r = getRectAroundView(view);
414 mTouchRects.add(r);
Selim Cinek499c20f2017-07-20 14:06:09 -0700415 return r;
Selim Cinekaef6c762015-11-20 17:00:18 -0800416 }
417
418 private Rect getRectAroundView(View view) {
419 float size = 48 * getResources().getDisplayMetrics().density;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500420 float width = Math.max(size, view.getWidth());
421 float height = Math.max(size, view.getHeight());
Selim Cinekaef6c762015-11-20 17:00:18 -0800422 final Rect r = new Rect();
423 if (view.getVisibility() == GONE) {
424 view = getFirstChildNotGone();
Julia Reynoldsb5867452018-02-28 16:31:35 -0500425 r.left = (int) (view.getLeft() - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800426 } else {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500427 r.left = (int) ((view.getLeft() + view.getRight()) / 2.0f - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800428 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500429 r.top = (int) ((view.getTop() + view.getBottom()) / 2.0f - height / 2.0f);
430 r.bottom = (int) (r.top + height);
431 r.right = (int) (r.left + width);
Selim Cinekaef6c762015-11-20 17:00:18 -0800432 return r;
433 }
434
435 @Override
436 public boolean onTouch(View v, MotionEvent event) {
437 float x = event.getX();
438 float y = event.getY();
439 switch (event.getActionMasked() & MotionEvent.ACTION_MASK) {
440 case MotionEvent.ACTION_DOWN:
441 mTrackGesture = false;
442 if (isInside(x, y)) {
Selim Cinek1b554392017-02-28 17:22:49 -0800443 mDownX = x;
444 mDownY = y;
Selim Cinekaef6c762015-11-20 17:00:18 -0800445 mTrackGesture = true;
446 return true;
447 }
448 break;
449 case MotionEvent.ACTION_MOVE:
450 if (mTrackGesture) {
451 if (Math.abs(mDownX - x) > mTouchSlop
452 || Math.abs(mDownY - y) > mTouchSlop) {
453 mTrackGesture = false;
454 }
455 }
456 break;
457 case MotionEvent.ACTION_UP:
458 if (mTrackGesture) {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500459 if (mAppOps.isVisibleToUser() && (mAppOpsRect.contains((int) x, (int) y)
460 || mAppOpsRect.contains((int) mDownX, (int) mDownY))) {
461 mAppOps.performClick();
462 return true;
463 }
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800464 mExpandButton.performClick();
Selim Cinekaef6c762015-11-20 17:00:18 -0800465 }
466 break;
467 }
468 return mTrackGesture;
469 }
470
471 private boolean isInside(float x, float y) {
Selim Cinek1b554392017-02-28 17:22:49 -0800472 if (mAcceptAllTouches) {
473 return true;
474 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700475 if (mExpandOnlyOnButton) {
476 return mExpandButtonRect.contains((int) x, (int) y);
477 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800478 for (int i = 0; i < mTouchRects.size(); i++) {
479 Rect r = mTouchRects.get(i);
480 if (r.contains((int) x, (int) y)) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800481 return true;
482 }
483 }
484 return false;
485 }
486 }
487
488 private View getFirstChildNotGone() {
489 for (int i = 0; i < getChildCount(); i++) {
490 final View child = getChildAt(i);
491 if (child.getVisibility() != GONE) {
492 return child;
493 }
494 }
495 return this;
496 }
Selim Cinek4ffd6362015-12-29 15:12:23 +0100497
498 public ImageView getExpandButton() {
499 return mExpandButton;
500 }
501
502 @Override
503 public boolean hasOverlappingRendering() {
504 return false;
505 }
Selim Cinek6183d122016-01-14 18:48:41 -0800506
507 public boolean isInTouchRect(float x, float y) {
508 if (mExpandClickListener == null) {
509 return false;
510 }
511 return mTouchListener.isInside(x, y);
512 }
Selim Cinek1b554392017-02-28 17:22:49 -0800513
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700514 /**
515 * Sets whether or not all touches to this header view will register as a click. Note that
516 * if the config value for {@code config_notificationHeaderClickableForExpand} is {@code true},
517 * then calling this method with {@code false} will not override that configuration.
518 */
Selim Cinek1b554392017-02-28 17:22:49 -0800519 @RemotableViewMethod
520 public void setAcceptAllTouches(boolean acceptAllTouches) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700521 mAcceptAllTouches = mEntireHeaderClickable || acceptAllTouches;
Selim Cinek1b554392017-02-28 17:22:49 -0800522 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700523
524 /**
525 * Sets whether only the expand icon itself should serve as the expand target.
526 */
527 @RemotableViewMethod
528 public void setExpandOnlyOnButton(boolean expandOnlyOnButton) {
529 mExpandOnlyOnButton = expandOnlyOnButton;
530 }
Selim Cinek90c8f472015-11-10 17:44:39 -0500531}