blob: 81da76d7d0ec52cd305f0f0a6df6b4baf42c4736 [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;
Mathew Inwoode5ad5982018-08-17 15:07:52 +010020import android.annotation.UnsupportedAppUsage;
Julia Reynoldsfc640012018-02-21 12:25:27 -050021import android.app.AppOpsManager;
Selim Cinek99104832017-01-25 14:47:33 -080022import android.app.Notification;
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;
Selim Cinek90c8f472015-11-10 17:44:39 -050033import android.widget.RemoteViews;
34
Anthony Chen0f6e96c2017-04-07 15:48:17 -070035import com.android.internal.R;
Selim Cinek0242fbb2016-10-19 13:38:32 -070036import com.android.internal.widget.CachingIconView;
37
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();
Selim Cinek7b836392015-12-04 20:02:59 -080057 private ImageView mExpandButton;
Selim Cinek0242fbb2016-10-19 13:38:32 -070058 private CachingIconView mIcon;
Selim Cinekc848c3a2016-01-13 15:27:30 -080059 private View mProfileBadge;
Julia Reynoldsfc640012018-02-21 12:25:27 -050060 private View mOverlayIcon;
61 private View mCameraIcon;
62 private View mMicIcon;
63 private View mAppOps;
Selim Cinekea4bef72015-12-02 15:51:10 -080064 private int mIconColor;
65 private int mOriginalNotificationColor;
Selim Cinek7b836392015-12-04 20:02:59 -080066 private boolean mExpanded;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070067 private boolean mShowExpandButtonAtEnd;
Selim Cinek6ecc8102016-01-26 18:26:19 -080068 private boolean mShowWorkBadgeAtEnd;
Mady Mellorb0a82462016-04-30 17:31:02 -070069 private Drawable mBackground;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070070 private boolean mEntireHeaderClickable;
Selim Cinek499c20f2017-07-20 14:06:09 -070071 private boolean mExpandOnlyOnButton;
Anthony Chen0f6e96c2017-04-07 15:48:17 -070072 private boolean mAcceptAllTouches;
Lucas Dupinbd9798f2017-10-24 18:04:51 -070073 private int mTotalWidth;
Mady Mellorb0a82462016-04-30 17:31:02 -070074
75 ViewOutlineProvider mProvider = new ViewOutlineProvider() {
76 @Override
77 public void getOutline(View view, Outline outline) {
78 if (mBackground != null) {
Selim Cinekafeed292017-12-12 17:32:44 -080079 outline.setRect(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -070080 outline.setAlpha(1f);
81 }
82 }
83 };
Selim Cinek90c8f472015-11-10 17:44:39 -050084
85 public NotificationHeaderView(Context context) {
86 this(context, null);
87 }
88
Mathew Inwoode5ad5982018-08-17 15:07:52 +010089 @UnsupportedAppUsage
Selim Cinek90c8f472015-11-10 17:44:39 -050090 public NotificationHeaderView(Context context, @Nullable AttributeSet attrs) {
91 this(context, attrs, 0);
92 }
93
94 public NotificationHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
95 this(context, attrs, defStyleAttr, 0);
96 }
97
98 public NotificationHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
99 super(context, attrs, defStyleAttr, defStyleRes);
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700100 Resources res = getResources();
101 mChildMinWidth = res.getDimensionPixelSize(R.dimen.notification_header_shrink_min_width);
102 mContentEndMargin = res.getDimensionPixelSize(R.dimen.notification_content_margin_end);
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700103 mEntireHeaderClickable = res.getBoolean(R.bool.config_notificationHeaderClickableForExpand);
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700104
105 int[] attrIds = { android.R.attr.gravity };
106 TypedArray ta = context.obtainStyledAttributes(attrs, attrIds, defStyleAttr, defStyleRes);
107 mGravity = ta.getInt(0, 0);
108 ta.recycle();
Selim Cinek90c8f472015-11-10 17:44:39 -0500109 }
110
111 @Override
112 protected void onFinishInflate() {
113 super.onFinishInflate();
114 mAppName = findViewById(com.android.internal.R.id.app_name_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700115 mHeaderText = findViewById(com.android.internal.R.id.header_text);
Selim Cinekafeed292017-12-12 17:32:44 -0800116 mSecondaryHeaderText = findViewById(com.android.internal.R.id.header_text_secondary);
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 mCameraIcon = findViewById(com.android.internal.R.id.camera);
121 mMicIcon = findViewById(com.android.internal.R.id.mic);
122 mOverlayIcon = findViewById(com.android.internal.R.id.overlay);
123 mAppOps = findViewById(com.android.internal.R.id.app_ops);
Selim Cinek90c8f472015-11-10 17:44:39 -0500124 }
125
126 @Override
127 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
128 final int givenWidth = MeasureSpec.getSize(widthMeasureSpec);
129 final int givenHeight = MeasureSpec.getSize(heightMeasureSpec);
130 int wrapContentWidthSpec = MeasureSpec.makeMeasureSpec(givenWidth,
131 MeasureSpec.AT_MOST);
132 int wrapContentHeightSpec = MeasureSpec.makeMeasureSpec(givenHeight,
133 MeasureSpec.AT_MOST);
134 int totalWidth = getPaddingStart() + getPaddingEnd();
135 for (int i = 0; i < getChildCount(); i++) {
136 final View child = getChildAt(i);
137 if (child.getVisibility() == GONE) {
138 // We'll give it the rest of the space in the end
139 continue;
140 }
141 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
142 int childWidthSpec = getChildMeasureSpec(wrapContentWidthSpec,
143 lp.leftMargin + lp.rightMargin, lp.width);
144 int childHeightSpec = getChildMeasureSpec(wrapContentHeightSpec,
145 lp.topMargin + lp.bottomMargin, lp.height);
146 child.measure(childWidthSpec, childHeightSpec);
147 totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
148 }
149 if (totalWidth > givenWidth) {
150 int overFlow = totalWidth - givenWidth;
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700151 // We are overflowing, lets shrink the app name first
Selim Cinekafeed292017-12-12 17:32:44 -0800152 overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mAppName,
153 mChildMinWidth);
154
155 // still overflowing, we shrink the header text
156 overFlow = shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mHeaderText, 0);
157
158 // still overflowing, finally we shrink the secondary header text
159 shrinkViewForOverflow(wrapContentHeightSpec, overFlow, mSecondaryHeaderText,
160 0);
Selim Cinek90c8f472015-11-10 17:44:39 -0500161 }
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700162 mTotalWidth = Math.min(totalWidth, givenWidth);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800163 setMeasuredDimension(givenWidth, givenHeight);
Selim Cinek90c8f472015-11-10 17:44:39 -0500164 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800165
Selim Cinekafeed292017-12-12 17:32:44 -0800166 private int shrinkViewForOverflow(int heightSpec, int overFlow, View targetView,
167 int minimumWidth) {
168 final int oldWidth = targetView.getMeasuredWidth();
169 if (overFlow > 0 && targetView.getVisibility() != GONE && oldWidth > minimumWidth) {
170 // we're still too big
171 int newSize = Math.max(minimumWidth, oldWidth - overFlow);
172 int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST);
173 targetView.measure(childWidthSpec, heightSpec);
174 overFlow -= oldWidth - newSize;
175 }
176 return overFlow;
177 }
178
Selim Cinekaef6c762015-11-20 17:00:18 -0800179 @Override
180 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Selim Cinekcb445682016-01-29 16:13:12 -0800181 int left = getPaddingStart();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700182 int end = getMeasuredWidth();
Lucas Dupinbd9798f2017-10-24 18:04:51 -0700183 final boolean centerAligned = (mGravity & Gravity.CENTER_HORIZONTAL) != 0;
184 if (centerAligned) {
185 left += getMeasuredWidth() / 2 - mTotalWidth / 2;
186 }
Selim Cinekcb445682016-01-29 16:13:12 -0800187 int childCount = getChildCount();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700188 int ownHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Selim Cinekcb445682016-01-29 16:13:12 -0800189 for (int i = 0; i < childCount; i++) {
190 View child = getChildAt(i);
191 if (child.getVisibility() == GONE) {
192 continue;
193 }
194 int childHeight = child.getMeasuredHeight();
195 MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
196 left += params.getMarginStart();
197 int right = left + child.getMeasuredWidth();
198 int top = (int) (getPaddingTop() + (ownHeight - childHeight) / 2.0f);
199 int bottom = top + childHeight;
200 int layoutLeft = left;
201 int layoutRight = right;
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700202 if (child == mExpandButton && mShowExpandButtonAtEnd) {
203 layoutRight = end - mContentEndMargin;
204 end = layoutLeft = layoutRight - child.getMeasuredWidth();
205 }
Selim Cinekcb445682016-01-29 16:13:12 -0800206 if (child == mProfileBadge) {
207 int paddingEnd = getPaddingEnd();
208 if (mShowWorkBadgeAtEnd) {
209 paddingEnd = mContentEndMargin;
210 }
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700211 layoutRight = end - paddingEnd;
212 end = layoutLeft = layoutRight - child.getMeasuredWidth();
Selim Cinek6ecc8102016-01-26 18:26:19 -0800213 }
Julia Reynoldsfc640012018-02-21 12:25:27 -0500214 if (child == mAppOps) {
215 int paddingEnd = mContentEndMargin;
216 layoutRight = end - paddingEnd;
217 end = layoutLeft = layoutRight - child.getMeasuredWidth();
218 }
Selim Cinekc848c3a2016-01-13 15:27:30 -0800219 if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Selim Cinekcb445682016-01-29 16:13:12 -0800220 int ltrLeft = layoutLeft;
221 layoutLeft = getWidth() - layoutRight;
222 layoutRight = getWidth() - ltrLeft;
Selim Cinekc848c3a2016-01-13 15:27:30 -0800223 }
Selim Cinekcb445682016-01-29 16:13:12 -0800224 child.layout(layoutLeft, top, layoutRight, bottom);
225 left = right + params.getMarginEnd();
Selim Cinekc848c3a2016-01-13 15:27:30 -0800226 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800227 updateTouchListener();
228 }
229
Selim Cinekcb445682016-01-29 16:13:12 -0800230 @Override
231 public LayoutParams generateLayoutParams(AttributeSet attrs) {
232 return new ViewGroup.MarginLayoutParams(getContext(), attrs);
233 }
234
Mady Mellorb0a82462016-04-30 17:31:02 -0700235 /**
236 * Set a {@link Drawable} to be displayed as a background on the header.
237 */
238 public void setHeaderBackgroundDrawable(Drawable drawable) {
239 if (drawable != null) {
240 setWillNotDraw(false);
241 mBackground = drawable;
242 mBackground.setCallback(this);
243 setOutlineProvider(mProvider);
244 } else {
245 setWillNotDraw(true);
246 mBackground = null;
247 setOutlineProvider(null);
248 }
249 invalidate();
250 }
251
252 @Override
253 protected void onDraw(Canvas canvas) {
254 if (mBackground != null) {
Selim Cinekafeed292017-12-12 17:32:44 -0800255 mBackground.setBounds(0, 0, getWidth(), getHeight());
Mady Mellorb0a82462016-04-30 17:31:02 -0700256 mBackground.draw(canvas);
257 }
258 }
259
260 @Override
261 protected boolean verifyDrawable(Drawable who) {
262 return super.verifyDrawable(who) || who == mBackground;
263 }
264
265 @Override
266 protected void drawableStateChanged() {
267 if (mBackground != null && mBackground.isStateful()) {
268 mBackground.setState(getDrawableState());
269 }
270 }
271
Selim Cinekaef6c762015-11-20 17:00:18 -0800272 private void updateTouchListener() {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500273 if (mExpandClickListener == null && mAppOpsListener == null) {
274 setOnTouchListener(null);
275 return;
Selim Cinekaef6c762015-11-20 17:00:18 -0800276 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500277 setOnTouchListener(mTouchListener);
278 mTouchListener.bindTouchRects();
279 }
280
281 /**
282 * Sets onclick listener for app ops icons.
283 */
284 public void setAppOpsOnClickListener(OnClickListener l) {
285 mAppOpsListener = l;
286 mAppOps.setOnClickListener(mAppOpsListener);
Dieter Hsu3a9b5a92018-05-15 19:02:52 +0800287 mCameraIcon.setOnClickListener(mAppOpsListener);
288 mMicIcon.setOnClickListener(mAppOpsListener);
289 mOverlayIcon.setOnClickListener(mAppOpsListener);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500290 updateTouchListener();
Selim Cinekaef6c762015-11-20 17:00:18 -0800291 }
292
293 @Override
294 public void setOnClickListener(@Nullable OnClickListener l) {
295 mExpandClickListener = l;
Selim Cineke9bad242016-06-15 11:46:37 -0700296 mExpandButton.setOnClickListener(mExpandClickListener);
Selim Cinekaef6c762015-11-20 17:00:18 -0800297 updateTouchListener();
298 }
299
Selim Cinekea4bef72015-12-02 15:51:10 -0800300 @RemotableViewMethod
301 public void setOriginalIconColor(int color) {
302 mIconColor = color;
303 }
304
305 public int getOriginalIconColor() {
306 return mIconColor;
307 }
308
309 @RemotableViewMethod
310 public void setOriginalNotificationColor(int color) {
311 mOriginalNotificationColor = color;
312 }
313
314 public int getOriginalNotificationColor() {
315 return mOriginalNotificationColor;
316 }
317
Selim Cinek7b836392015-12-04 20:02:59 -0800318 @RemotableViewMethod
319 public void setExpanded(boolean expanded) {
320 mExpanded = expanded;
321 updateExpandButton();
322 }
323
Julia Reynoldsfc640012018-02-21 12:25:27 -0500324 /**
325 * Shows or hides 'app op in use' icons based on app usage.
326 */
327 public void showAppOpsIcons(ArraySet<Integer> appOps) {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500328 if (mOverlayIcon == null || mCameraIcon == null || mMicIcon == null || appOps == null) {
Julia Reynoldsfc640012018-02-21 12:25:27 -0500329 return;
330 }
331
332 mOverlayIcon.setVisibility(appOps.contains(AppOpsManager.OP_SYSTEM_ALERT_WINDOW)
333 ? View.VISIBLE : View.GONE);
334 mCameraIcon.setVisibility(appOps.contains(AppOpsManager.OP_CAMERA)
335 ? View.VISIBLE : View.GONE);
336 mMicIcon.setVisibility(appOps.contains(AppOpsManager.OP_RECORD_AUDIO)
337 ? View.VISIBLE : View.GONE);
338 }
339
Selim Cinek7b836392015-12-04 20:02:59 -0800340 private void updateExpandButton() {
341 int drawableId;
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800342 int contentDescriptionId;
Selim Cinek6db57582016-03-04 19:11:27 -0800343 if (mExpanded) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700344 drawableId = R.drawable.ic_collapse_notification;
345 contentDescriptionId = R.string.expand_button_content_description_expanded;
Selim Cinek7b836392015-12-04 20:02:59 -0800346 } else {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700347 drawableId = R.drawable.ic_expand_notification;
348 contentDescriptionId = R.string.expand_button_content_description_collapsed;
Selim Cinek7b836392015-12-04 20:02:59 -0800349 }
350 mExpandButton.setImageDrawable(getContext().getDrawable(drawableId));
351 mExpandButton.setColorFilter(mOriginalNotificationColor);
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800352 mExpandButton.setContentDescription(mContext.getText(contentDescriptionId));
Selim Cinek7b836392015-12-04 20:02:59 -0800353 }
354
Selim Cinek6ecc8102016-01-26 18:26:19 -0800355 public void setShowWorkBadgeAtEnd(boolean showWorkBadgeAtEnd) {
356 if (showWorkBadgeAtEnd != mShowWorkBadgeAtEnd) {
357 setClipToPadding(!showWorkBadgeAtEnd);
358 mShowWorkBadgeAtEnd = showWorkBadgeAtEnd;
359 }
360 }
361
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700362 /**
363 * Sets whether or not the expand button appears at the end of the NotificationHeaderView. If
364 * both this and {@link #setShowWorkBadgeAtEnd(boolean)} have been set to true, then the
365 * expand button will appear closer to the end than the work badge.
366 */
367 public void setShowExpandButtonAtEnd(boolean showExpandButtonAtEnd) {
368 if (showExpandButtonAtEnd != mShowExpandButtonAtEnd) {
369 setClipToPadding(!showExpandButtonAtEnd);
370 mShowExpandButtonAtEnd = showExpandButtonAtEnd;
371 }
372 }
373
Selim Cinek0d07c7e2016-01-27 18:38:31 -0800374 public View getWorkProfileIcon() {
375 return mProfileBadge;
376 }
377
Selim Cinek0242fbb2016-10-19 13:38:32 -0700378 public CachingIconView getIcon() {
Selim Cinek281c2022016-10-13 19:14:43 -0700379 return mIcon;
380 }
381
Selim Cinekaef6c762015-11-20 17:00:18 -0800382 public class HeaderTouchListener implements View.OnTouchListener {
383
384 private final ArrayList<Rect> mTouchRects = new ArrayList<>();
Selim Cinek499c20f2017-07-20 14:06:09 -0700385 private Rect mExpandButtonRect;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500386 private Rect mAppOpsRect;
Selim Cinekaef6c762015-11-20 17:00:18 -0800387 private int mTouchSlop;
388 private boolean mTrackGesture;
389 private float mDownX;
390 private float mDownY;
391
392 public HeaderTouchListener() {
393 }
394
395 public void bindTouchRects() {
396 mTouchRects.clear();
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700397 addRectAroundView(mIcon);
Selim Cinek499c20f2017-07-20 14:06:09 -0700398 mExpandButtonRect = addRectAroundView(mExpandButton);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500399 mAppOpsRect = addRectAroundView(mAppOps);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800400 addWidthRect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800401 mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
402 }
403
Selim Cinek4c4c7382016-02-03 16:17:09 -0800404 private void addWidthRect() {
405 Rect r = new Rect();
Selim Cinekaef6c762015-11-20 17:00:18 -0800406 r.top = 0;
407 r.bottom = (int) (32 * getResources().getDisplayMetrics().density);
Selim Cinek4c4c7382016-02-03 16:17:09 -0800408 r.left = 0;
409 r.right = getWidth();
Selim Cinekaef6c762015-11-20 17:00:18 -0800410 mTouchRects.add(r);
411 }
412
Selim Cinek499c20f2017-07-20 14:06:09 -0700413 private Rect addRectAroundView(View view) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800414 final Rect r = getRectAroundView(view);
415 mTouchRects.add(r);
Selim Cinek499c20f2017-07-20 14:06:09 -0700416 return r;
Selim Cinekaef6c762015-11-20 17:00:18 -0800417 }
418
419 private Rect getRectAroundView(View view) {
420 float size = 48 * getResources().getDisplayMetrics().density;
Julia Reynoldsb5867452018-02-28 16:31:35 -0500421 float width = Math.max(size, view.getWidth());
422 float height = Math.max(size, view.getHeight());
Selim Cinekaef6c762015-11-20 17:00:18 -0800423 final Rect r = new Rect();
424 if (view.getVisibility() == GONE) {
425 view = getFirstChildNotGone();
Julia Reynoldsb5867452018-02-28 16:31:35 -0500426 r.left = (int) (view.getLeft() - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800427 } else {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500428 r.left = (int) ((view.getLeft() + view.getRight()) / 2.0f - width / 2.0f);
Selim Cinekaef6c762015-11-20 17:00:18 -0800429 }
Julia Reynoldsb5867452018-02-28 16:31:35 -0500430 r.top = (int) ((view.getTop() + view.getBottom()) / 2.0f - height / 2.0f);
431 r.bottom = (int) (r.top + height);
432 r.right = (int) (r.left + width);
Selim Cinekaef6c762015-11-20 17:00:18 -0800433 return r;
434 }
435
436 @Override
437 public boolean onTouch(View v, MotionEvent event) {
438 float x = event.getX();
439 float y = event.getY();
440 switch (event.getActionMasked() & MotionEvent.ACTION_MASK) {
441 case MotionEvent.ACTION_DOWN:
442 mTrackGesture = false;
443 if (isInside(x, y)) {
Selim Cinek1b554392017-02-28 17:22:49 -0800444 mDownX = x;
445 mDownY = y;
Selim Cinekaef6c762015-11-20 17:00:18 -0800446 mTrackGesture = true;
447 return true;
448 }
449 break;
450 case MotionEvent.ACTION_MOVE:
451 if (mTrackGesture) {
452 if (Math.abs(mDownX - x) > mTouchSlop
453 || Math.abs(mDownY - y) > mTouchSlop) {
454 mTrackGesture = false;
455 }
456 }
457 break;
458 case MotionEvent.ACTION_UP:
459 if (mTrackGesture) {
Julia Reynoldsb5867452018-02-28 16:31:35 -0500460 if (mAppOps.isVisibleToUser() && (mAppOpsRect.contains((int) x, (int) y)
461 || mAppOpsRect.contains((int) mDownX, (int) mDownY))) {
462 mAppOps.performClick();
463 return true;
464 }
Selim Cinekc0ac4af2017-03-03 15:13:48 -0800465 mExpandButton.performClick();
Selim Cinekaef6c762015-11-20 17:00:18 -0800466 }
467 break;
468 }
469 return mTrackGesture;
470 }
471
472 private boolean isInside(float x, float y) {
Selim Cinek1b554392017-02-28 17:22:49 -0800473 if (mAcceptAllTouches) {
474 return true;
475 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700476 if (mExpandOnlyOnButton) {
477 return mExpandButtonRect.contains((int) x, (int) y);
478 }
Selim Cinekaef6c762015-11-20 17:00:18 -0800479 for (int i = 0; i < mTouchRects.size(); i++) {
480 Rect r = mTouchRects.get(i);
481 if (r.contains((int) x, (int) y)) {
Selim Cinekaef6c762015-11-20 17:00:18 -0800482 return true;
483 }
484 }
485 return false;
486 }
487 }
488
489 private View getFirstChildNotGone() {
490 for (int i = 0; i < getChildCount(); i++) {
491 final View child = getChildAt(i);
492 if (child.getVisibility() != GONE) {
493 return child;
494 }
495 }
496 return this;
497 }
Selim Cinek4ffd6362015-12-29 15:12:23 +0100498
499 public ImageView getExpandButton() {
500 return mExpandButton;
501 }
502
503 @Override
504 public boolean hasOverlappingRendering() {
505 return false;
506 }
Selim Cinek6183d122016-01-14 18:48:41 -0800507
508 public boolean isInTouchRect(float x, float y) {
509 if (mExpandClickListener == null) {
510 return false;
511 }
512 return mTouchListener.isInside(x, y);
513 }
Selim Cinek1b554392017-02-28 17:22:49 -0800514
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700515 /**
516 * Sets whether or not all touches to this header view will register as a click. Note that
517 * if the config value for {@code config_notificationHeaderClickableForExpand} is {@code true},
518 * then calling this method with {@code false} will not override that configuration.
519 */
Selim Cinek1b554392017-02-28 17:22:49 -0800520 @RemotableViewMethod
521 public void setAcceptAllTouches(boolean acceptAllTouches) {
Anthony Chen0f6e96c2017-04-07 15:48:17 -0700522 mAcceptAllTouches = mEntireHeaderClickable || acceptAllTouches;
Selim Cinek1b554392017-02-28 17:22:49 -0800523 }
Selim Cinek499c20f2017-07-20 14:06:09 -0700524
525 /**
526 * Sets whether only the expand icon itself should serve as the expand target.
527 */
528 @RemotableViewMethod
529 public void setExpandOnlyOnButton(boolean expandOnlyOnButton) {
530 mExpandOnlyOnButton = expandOnlyOnButton;
531 }
Selim Cinek90c8f472015-11-10 17:44:39 -0500532}