Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.widget; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
| 21 | import android.util.AttributeSet; |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 22 | import android.view.NotificationHeaderView; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 25 | import android.widget.FrameLayout; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 26 | import android.widget.ImageView; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 27 | import android.widget.RemoteViews; |
| 28 | |
| 29 | /** |
| 30 | * A TextView that can float around an image on the end. |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
| 34 | @RemoteViews.RemoteView |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 35 | public class MediaNotificationView extends FrameLayout { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 36 | |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 37 | private final int mNotificationContentMarginEnd; |
| 38 | private final int mNotificationContentImageMarginEnd; |
| 39 | private ImageView mRightIcon; |
| 40 | private View mActions; |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 41 | private NotificationHeaderView mHeader; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 42 | private View mMainColumn; |
Beth Thibodeau | cb39535 | 2019-01-25 15:39:54 -0500 | [diff] [blame] | 43 | private View mMediaContent; |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 44 | private int mImagePushIn; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 45 | |
| 46 | public MediaNotificationView(Context context) { |
| 47 | this(context, null); |
| 48 | } |
| 49 | |
| 50 | public MediaNotificationView(Context context, @Nullable AttributeSet attrs) { |
| 51 | this(context, attrs, 0); |
| 52 | } |
| 53 | |
| 54 | public MediaNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 55 | this(context, attrs, defStyleAttr, 0); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 60 | boolean hasIcon = mRightIcon.getVisibility() != GONE; |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 61 | if (!hasIcon) { |
| 62 | resetHeaderIndention(); |
| 63 | } |
| 64 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 65 | int mode = MeasureSpec.getMode(widthMeasureSpec); |
| 66 | boolean reMeasure = false; |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 67 | mImagePushIn = 0; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 68 | if (hasIcon && mode != MeasureSpec.UNSPECIFIED) { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 69 | int size = MeasureSpec.getSize(widthMeasureSpec); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 70 | size = size - mActions.getMeasuredWidth(); |
| 71 | ViewGroup.MarginLayoutParams layoutParams = |
| 72 | (MarginLayoutParams) mRightIcon.getLayoutParams(); |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 73 | int imageEndMargin = layoutParams.getMarginEnd(); |
| 74 | size -= imageEndMargin; |
Beth Thibodeau | cb39535 | 2019-01-25 15:39:54 -0500 | [diff] [blame] | 75 | int fullHeight = mMediaContent.getMeasuredHeight(); |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 76 | if (size > fullHeight) { |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 77 | size = fullHeight; |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 78 | } else if (size < fullHeight) { |
| 79 | size = Math.max(0, size); |
| 80 | mImagePushIn = fullHeight - size; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 81 | } |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 82 | if (layoutParams.width != fullHeight || layoutParams.height != fullHeight) { |
| 83 | layoutParams.width = fullHeight; |
| 84 | layoutParams.height = fullHeight; |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 85 | mRightIcon.setLayoutParams(layoutParams); |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 86 | reMeasure = true; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 87 | } |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 88 | |
| 89 | // lets ensure that the main column doesn't run into the image |
| 90 | ViewGroup.MarginLayoutParams params |
| 91 | = (MarginLayoutParams) mMainColumn.getLayoutParams(); |
| 92 | int marginEnd = size + imageEndMargin + mNotificationContentMarginEnd; |
| 93 | if (marginEnd != params.getMarginEnd()) { |
| 94 | params.setMarginEnd(marginEnd); |
| 95 | mMainColumn.setLayoutParams(params); |
| 96 | reMeasure = true; |
| 97 | } |
Beth Thibodeau | 837bfc2 | 2019-02-19 12:26:53 -0500 | [diff] [blame] | 98 | // margin for the entire header line |
| 99 | int headerMarginEnd = imageEndMargin; |
| 100 | // margin for the header text (not including the expand button and other icons) |
| 101 | int headerTextMarginEnd = size + imageEndMargin; |
| 102 | if (headerTextMarginEnd != mHeader.getHeaderTextMarginEnd()) { |
| 103 | mHeader.setHeaderTextMarginEnd(headerTextMarginEnd); |
| 104 | reMeasure = true; |
| 105 | } |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 106 | params = (MarginLayoutParams) mHeader.getLayoutParams(); |
| 107 | if (params.getMarginEnd() != headerMarginEnd) { |
| 108 | params.setMarginEnd(headerMarginEnd); |
| 109 | mHeader.setLayoutParams(params); |
| 110 | reMeasure = true; |
| 111 | } |
| 112 | if (mHeader.getPaddingEnd() != mNotificationContentImageMarginEnd) { |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 113 | mHeader.setPaddingRelative(mHeader.getPaddingStart(), |
| 114 | mHeader.getPaddingTop(), |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 115 | mNotificationContentImageMarginEnd, |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 116 | mHeader.getPaddingBottom()); |
| 117 | reMeasure = true; |
| 118 | } |
| 119 | } |
| 120 | if (reMeasure) { |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 121 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 122 | } |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 125 | @Override |
| 126 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 127 | super.onLayout(changed, left, top, right, bottom); |
| 128 | if (mImagePushIn > 0) { |
Beth Thibodeau | 52fcd9d | 2019-03-08 16:51:58 -0500 | [diff] [blame] | 129 | if (this.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { |
| 130 | mImagePushIn *= -1; |
| 131 | } |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 132 | mRightIcon.layout(mRightIcon.getLeft() + mImagePushIn, mRightIcon.getTop(), |
| 133 | mRightIcon.getRight() + mImagePushIn, mRightIcon.getBottom()); |
| 134 | } |
| 135 | } |
| 136 | |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 137 | private void resetHeaderIndention() { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 138 | if (mHeader.getPaddingEnd() != mNotificationContentMarginEnd) { |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 139 | mHeader.setPaddingRelative(mHeader.getPaddingStart(), |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 140 | mHeader.getPaddingTop(), |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 141 | mNotificationContentMarginEnd, |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 142 | mHeader.getPaddingBottom()); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 143 | } |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 144 | ViewGroup.MarginLayoutParams headerParams = |
| 145 | (MarginLayoutParams) mHeader.getLayoutParams(); |
| 146 | headerParams.setMarginEnd(0); |
| 147 | if (headerParams.getMarginEnd() != 0) { |
| 148 | headerParams.setMarginEnd(0); |
| 149 | mHeader.setLayoutParams(headerParams); |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 150 | } |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public MediaNotificationView(Context context, AttributeSet attrs, int defStyleAttr, |
| 154 | int defStyleRes) { |
| 155 | super(context, attrs, defStyleAttr, defStyleRes); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 156 | mNotificationContentMarginEnd = context.getResources().getDimensionPixelSize( |
| 157 | com.android.internal.R.dimen.notification_content_margin_end); |
| 158 | mNotificationContentImageMarginEnd = context.getResources().getDimensionPixelSize( |
| 159 | com.android.internal.R.dimen.notification_content_image_margin_end); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void onFinishInflate() { |
| 164 | super.onFinishInflate(); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 165 | mRightIcon = findViewById(com.android.internal.R.id.right_icon); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 166 | mActions = findViewById(com.android.internal.R.id.media_actions); |
| 167 | mHeader = findViewById(com.android.internal.R.id.notification_header); |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 168 | mMainColumn = findViewById(com.android.internal.R.id.notification_main_column); |
Beth Thibodeau | cb39535 | 2019-01-25 15:39:54 -0500 | [diff] [blame] | 169 | mMediaContent = findViewById(com.android.internal.R.id.notification_media_content); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 170 | } |
| 171 | } |