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; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 24 | import android.widget.FrameLayout; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 25 | import android.widget.ImageView; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 26 | import android.widget.RemoteViews; |
| 27 | |
| 28 | /** |
| 29 | * A TextView that can float around an image on the end. |
| 30 | * |
| 31 | * @hide |
| 32 | */ |
| 33 | @RemoteViews.RemoteView |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 34 | public class MediaNotificationView extends FrameLayout { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 35 | |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 36 | private final int mNotificationContentMarginEnd; |
| 37 | private final int mNotificationContentImageMarginEnd; |
| 38 | private ImageView mRightIcon; |
| 39 | private View mActions; |
| 40 | private View mHeader; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 41 | private View mMainColumn; |
Beth Thibodeau | cb39535 | 2019-01-25 15:39:54 -0500 | [diff] [blame] | 42 | private View mMediaContent; |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 43 | private int mImagePushIn; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 44 | |
| 45 | public MediaNotificationView(Context context) { |
| 46 | this(context, null); |
| 47 | } |
| 48 | |
| 49 | public MediaNotificationView(Context context, @Nullable AttributeSet attrs) { |
| 50 | this(context, attrs, 0); |
| 51 | } |
| 52 | |
| 53 | public MediaNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 54 | this(context, attrs, defStyleAttr, 0); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 59 | boolean hasIcon = mRightIcon.getVisibility() != GONE; |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 60 | if (!hasIcon) { |
| 61 | resetHeaderIndention(); |
| 62 | } |
| 63 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 64 | int mode = MeasureSpec.getMode(widthMeasureSpec); |
| 65 | boolean reMeasure = false; |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 66 | mImagePushIn = 0; |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 67 | if (hasIcon && mode != MeasureSpec.UNSPECIFIED) { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 68 | int size = MeasureSpec.getSize(widthMeasureSpec); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 69 | size = size - mActions.getMeasuredWidth(); |
| 70 | ViewGroup.MarginLayoutParams layoutParams = |
| 71 | (MarginLayoutParams) mRightIcon.getLayoutParams(); |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 72 | int imageEndMargin = layoutParams.getMarginEnd(); |
| 73 | size -= imageEndMargin; |
Beth Thibodeau | cb39535 | 2019-01-25 15:39:54 -0500 | [diff] [blame] | 74 | int fullHeight = mMediaContent.getMeasuredHeight(); |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 75 | if (size > fullHeight) { |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 76 | size = fullHeight; |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 77 | } else if (size < fullHeight) { |
| 78 | size = Math.max(0, size); |
| 79 | mImagePushIn = fullHeight - size; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 80 | } |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 81 | if (layoutParams.width != fullHeight || layoutParams.height != fullHeight) { |
| 82 | layoutParams.width = fullHeight; |
| 83 | layoutParams.height = fullHeight; |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 84 | mRightIcon.setLayoutParams(layoutParams); |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 85 | reMeasure = true; |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 86 | } |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 87 | |
| 88 | // lets ensure that the main column doesn't run into the image |
| 89 | ViewGroup.MarginLayoutParams params |
| 90 | = (MarginLayoutParams) mMainColumn.getLayoutParams(); |
| 91 | int marginEnd = size + imageEndMargin + mNotificationContentMarginEnd; |
| 92 | if (marginEnd != params.getMarginEnd()) { |
| 93 | params.setMarginEnd(marginEnd); |
| 94 | mMainColumn.setLayoutParams(params); |
| 95 | reMeasure = true; |
| 96 | } |
| 97 | int headerMarginEnd = size + imageEndMargin; |
| 98 | params = (MarginLayoutParams) mHeader.getLayoutParams(); |
| 99 | if (params.getMarginEnd() != headerMarginEnd) { |
| 100 | params.setMarginEnd(headerMarginEnd); |
| 101 | mHeader.setLayoutParams(params); |
| 102 | reMeasure = true; |
| 103 | } |
| 104 | if (mHeader.getPaddingEnd() != mNotificationContentImageMarginEnd) { |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 105 | mHeader.setPaddingRelative(mHeader.getPaddingStart(), |
| 106 | mHeader.getPaddingTop(), |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 107 | mNotificationContentImageMarginEnd, |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 108 | mHeader.getPaddingBottom()); |
| 109 | reMeasure = true; |
| 110 | } |
| 111 | } |
| 112 | if (reMeasure) { |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 113 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 114 | } |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Selim Cinek | a8818f6 | 2017-07-24 17:27:22 -0700 | [diff] [blame] | 117 | @Override |
| 118 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 119 | super.onLayout(changed, left, top, right, bottom); |
| 120 | if (mImagePushIn > 0) { |
| 121 | mRightIcon.layout(mRightIcon.getLeft() + mImagePushIn, mRightIcon.getTop(), |
| 122 | mRightIcon.getRight() + mImagePushIn, mRightIcon.getBottom()); |
| 123 | } |
| 124 | } |
| 125 | |
Selim Cinek | bea598b | 2017-04-24 19:48:29 -0700 | [diff] [blame] | 126 | private void resetHeaderIndention() { |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 127 | if (mHeader.getPaddingEnd() != mNotificationContentMarginEnd) { |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 128 | mHeader.setPaddingRelative(mHeader.getPaddingStart(), |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 129 | mHeader.getPaddingTop(), |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 130 | mNotificationContentMarginEnd, |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 131 | mHeader.getPaddingBottom()); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 132 | } |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 133 | ViewGroup.MarginLayoutParams headerParams = |
| 134 | (MarginLayoutParams) mHeader.getLayoutParams(); |
| 135 | headerParams.setMarginEnd(0); |
| 136 | if (headerParams.getMarginEnd() != 0) { |
| 137 | headerParams.setMarginEnd(0); |
| 138 | mHeader.setLayoutParams(headerParams); |
Selim Cinek | 50c6e49 | 2016-03-04 16:13:06 -0800 | [diff] [blame] | 139 | } |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public MediaNotificationView(Context context, AttributeSet attrs, int defStyleAttr, |
| 143 | int defStyleRes) { |
| 144 | super(context, attrs, defStyleAttr, defStyleRes); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 145 | mNotificationContentMarginEnd = context.getResources().getDimensionPixelSize( |
| 146 | com.android.internal.R.dimen.notification_content_margin_end); |
| 147 | mNotificationContentImageMarginEnd = context.getResources().getDimensionPixelSize( |
| 148 | com.android.internal.R.dimen.notification_content_image_margin_end); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected void onFinishInflate() { |
| 153 | super.onFinishInflate(); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 154 | mRightIcon = findViewById(com.android.internal.R.id.right_icon); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 155 | mActions = findViewById(com.android.internal.R.id.media_actions); |
| 156 | mHeader = findViewById(com.android.internal.R.id.notification_header); |
Selim Cinek | d2feb63 | 2016-02-17 17:05:41 -0800 | [diff] [blame] | 157 | mMainColumn = findViewById(com.android.internal.R.id.notification_main_column); |
Beth Thibodeau | cb39535 | 2019-01-25 15:39:54 -0500 | [diff] [blame] | 158 | mMediaContent = findViewById(com.android.internal.R.id.notification_media_content); |
Selim Cinek | 6ecc810 | 2016-01-26 18:26:19 -0800 | [diff] [blame] | 159 | } |
| 160 | } |