blob: e7d240a1035e6b7fd1947a58391042c30a02077c [file] [log] [blame]
Selim Cinek6ecc8102016-01-26 18:26:19 -08001/*
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
17package com.android.internal.widget;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.util.AttributeSet;
Beth Thibodeau837bfc22019-02-19 12:26:53 -050022import android.view.NotificationHeaderView;
Selim Cinek6ecc8102016-01-26 18:26:19 -080023import android.view.View;
24import android.view.ViewGroup;
Selim Cinekd2feb632016-02-17 17:05:41 -080025import android.widget.FrameLayout;
Selim Cinek6ecc8102016-01-26 18:26:19 -080026import android.widget.ImageView;
Selim Cinek6ecc8102016-01-26 18:26:19 -080027import 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 Cinekd2feb632016-02-17 17:05:41 -080035public class MediaNotificationView extends FrameLayout {
Selim Cinek6ecc8102016-01-26 18:26:19 -080036
Selim Cinek6ecc8102016-01-26 18:26:19 -080037 private final int mNotificationContentMarginEnd;
38 private final int mNotificationContentImageMarginEnd;
39 private ImageView mRightIcon;
40 private View mActions;
Beth Thibodeau837bfc22019-02-19 12:26:53 -050041 private NotificationHeaderView mHeader;
Selim Cinekd2feb632016-02-17 17:05:41 -080042 private View mMainColumn;
Beth Thibodeaucb395352019-01-25 15:39:54 -050043 private View mMediaContent;
Selim Cineka8818f62017-07-24 17:27:22 -070044 private int mImagePushIn;
Selim Cinek6ecc8102016-01-26 18:26:19 -080045
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 Cinek6ecc8102016-01-26 18:26:19 -080060 boolean hasIcon = mRightIcon.getVisibility() != GONE;
Selim Cinekbea598b2017-04-24 19:48:29 -070061 if (!hasIcon) {
62 resetHeaderIndention();
63 }
64 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
65 int mode = MeasureSpec.getMode(widthMeasureSpec);
66 boolean reMeasure = false;
Selim Cineka8818f62017-07-24 17:27:22 -070067 mImagePushIn = 0;
Selim Cinek6ecc8102016-01-26 18:26:19 -080068 if (hasIcon && mode != MeasureSpec.UNSPECIFIED) {
Selim Cinek6ecc8102016-01-26 18:26:19 -080069 int size = MeasureSpec.getSize(widthMeasureSpec);
Selim Cinek6ecc8102016-01-26 18:26:19 -080070 size = size - mActions.getMeasuredWidth();
71 ViewGroup.MarginLayoutParams layoutParams =
72 (MarginLayoutParams) mRightIcon.getLayoutParams();
Selim Cinekd2feb632016-02-17 17:05:41 -080073 int imageEndMargin = layoutParams.getMarginEnd();
74 size -= imageEndMargin;
Beth Thibodeaucb395352019-01-25 15:39:54 -050075 int fullHeight = mMediaContent.getMeasuredHeight();
Selim Cineka8818f62017-07-24 17:27:22 -070076 if (size > fullHeight) {
Selim Cinekbea598b2017-04-24 19:48:29 -070077 size = fullHeight;
Selim Cineka8818f62017-07-24 17:27:22 -070078 } else if (size < fullHeight) {
79 size = Math.max(0, size);
80 mImagePushIn = fullHeight - size;
Selim Cinekd2feb632016-02-17 17:05:41 -080081 }
Selim Cineka8818f62017-07-24 17:27:22 -070082 if (layoutParams.width != fullHeight || layoutParams.height != fullHeight) {
83 layoutParams.width = fullHeight;
84 layoutParams.height = fullHeight;
Selim Cinekbea598b2017-04-24 19:48:29 -070085 mRightIcon.setLayoutParams(layoutParams);
Selim Cinek50c6e492016-03-04 16:13:06 -080086 reMeasure = true;
Selim Cinekd2feb632016-02-17 17:05:41 -080087 }
Selim Cinekbea598b2017-04-24 19:48:29 -070088
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 Thibodeau837bfc22019-02-19 12:26:53 -050098 // 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 Cinekbea598b2017-04-24 19:48:29 -0700106 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 Cinek50c6e492016-03-04 16:13:06 -0800113 mHeader.setPaddingRelative(mHeader.getPaddingStart(),
114 mHeader.getPaddingTop(),
Selim Cinekbea598b2017-04-24 19:48:29 -0700115 mNotificationContentImageMarginEnd,
Selim Cinek50c6e492016-03-04 16:13:06 -0800116 mHeader.getPaddingBottom());
117 reMeasure = true;
118 }
119 }
120 if (reMeasure) {
Selim Cinekbea598b2017-04-24 19:48:29 -0700121 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Selim Cinekd2feb632016-02-17 17:05:41 -0800122 }
Selim Cinek6ecc8102016-01-26 18:26:19 -0800123 }
124
Selim Cineka8818f62017-07-24 17:27:22 -0700125 @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 Thibodeau52fcd9d2019-03-08 16:51:58 -0500129 if (this.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
130 mImagePushIn *= -1;
131 }
Selim Cineka8818f62017-07-24 17:27:22 -0700132 mRightIcon.layout(mRightIcon.getLeft() + mImagePushIn, mRightIcon.getTop(),
133 mRightIcon.getRight() + mImagePushIn, mRightIcon.getBottom());
134 }
135 }
136
Selim Cinekbea598b2017-04-24 19:48:29 -0700137 private void resetHeaderIndention() {
Selim Cinek6ecc8102016-01-26 18:26:19 -0800138 if (mHeader.getPaddingEnd() != mNotificationContentMarginEnd) {
Selim Cinek50c6e492016-03-04 16:13:06 -0800139 mHeader.setPaddingRelative(mHeader.getPaddingStart(),
Selim Cinek6ecc8102016-01-26 18:26:19 -0800140 mHeader.getPaddingTop(),
Selim Cinek50c6e492016-03-04 16:13:06 -0800141 mNotificationContentMarginEnd,
Selim Cinek6ecc8102016-01-26 18:26:19 -0800142 mHeader.getPaddingBottom());
Selim Cinek6ecc8102016-01-26 18:26:19 -0800143 }
Selim Cinek50c6e492016-03-04 16:13:06 -0800144 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 Cinek50c6e492016-03-04 16:13:06 -0800150 }
Selim Cinek6ecc8102016-01-26 18:26:19 -0800151 }
152
153 public MediaNotificationView(Context context, AttributeSet attrs, int defStyleAttr,
154 int defStyleRes) {
155 super(context, attrs, defStyleAttr, defStyleRes);
Selim Cinek6ecc8102016-01-26 18:26:19 -0800156 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 Viverette51efddb2017-04-05 10:00:01 -0400165 mRightIcon = findViewById(com.android.internal.R.id.right_icon);
Selim Cinek6ecc8102016-01-26 18:26:19 -0800166 mActions = findViewById(com.android.internal.R.id.media_actions);
167 mHeader = findViewById(com.android.internal.R.id.notification_header);
Selim Cinekd2feb632016-02-17 17:05:41 -0800168 mMainColumn = findViewById(com.android.internal.R.id.notification_main_column);
Beth Thibodeaucb395352019-01-25 15:39:54 -0500169 mMediaContent = findViewById(com.android.internal.R.id.notification_media_content);
Selim Cinek6ecc8102016-01-26 18:26:19 -0800170 }
171}