blob: 498bc5a7d598a01e83abbfab5557ac5a74e7bdeb [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;
22import android.view.View;
23import android.view.ViewGroup;
Selim Cinekd2feb632016-02-17 17:05:41 -080024import android.widget.FrameLayout;
Selim Cinek6ecc8102016-01-26 18:26:19 -080025import android.widget.ImageView;
Selim Cinek6ecc8102016-01-26 18:26:19 -080026import 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 Cinekd2feb632016-02-17 17:05:41 -080034public class MediaNotificationView extends FrameLayout {
Selim Cinek6ecc8102016-01-26 18:26:19 -080035
Selim Cinek6ecc8102016-01-26 18:26:19 -080036 private final int mNotificationContentMarginEnd;
37 private final int mNotificationContentImageMarginEnd;
38 private ImageView mRightIcon;
39 private View mActions;
40 private View mHeader;
Selim Cinekd2feb632016-02-17 17:05:41 -080041 private View mMainColumn;
Beth Thibodeaucb395352019-01-25 15:39:54 -050042 private View mMediaContent;
Selim Cineka8818f62017-07-24 17:27:22 -070043 private int mImagePushIn;
Selim Cinek6ecc8102016-01-26 18:26:19 -080044
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 Cinek6ecc8102016-01-26 18:26:19 -080059 boolean hasIcon = mRightIcon.getVisibility() != GONE;
Selim Cinekbea598b2017-04-24 19:48:29 -070060 if (!hasIcon) {
61 resetHeaderIndention();
62 }
63 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
64 int mode = MeasureSpec.getMode(widthMeasureSpec);
65 boolean reMeasure = false;
Selim Cineka8818f62017-07-24 17:27:22 -070066 mImagePushIn = 0;
Selim Cinek6ecc8102016-01-26 18:26:19 -080067 if (hasIcon && mode != MeasureSpec.UNSPECIFIED) {
Selim Cinek6ecc8102016-01-26 18:26:19 -080068 int size = MeasureSpec.getSize(widthMeasureSpec);
Selim Cinek6ecc8102016-01-26 18:26:19 -080069 size = size - mActions.getMeasuredWidth();
70 ViewGroup.MarginLayoutParams layoutParams =
71 (MarginLayoutParams) mRightIcon.getLayoutParams();
Selim Cinekd2feb632016-02-17 17:05:41 -080072 int imageEndMargin = layoutParams.getMarginEnd();
73 size -= imageEndMargin;
Beth Thibodeaucb395352019-01-25 15:39:54 -050074 int fullHeight = mMediaContent.getMeasuredHeight();
Selim Cineka8818f62017-07-24 17:27:22 -070075 if (size > fullHeight) {
Selim Cinekbea598b2017-04-24 19:48:29 -070076 size = fullHeight;
Selim Cineka8818f62017-07-24 17:27:22 -070077 } else if (size < fullHeight) {
78 size = Math.max(0, size);
79 mImagePushIn = fullHeight - size;
Selim Cinekd2feb632016-02-17 17:05:41 -080080 }
Selim Cineka8818f62017-07-24 17:27:22 -070081 if (layoutParams.width != fullHeight || layoutParams.height != fullHeight) {
82 layoutParams.width = fullHeight;
83 layoutParams.height = fullHeight;
Selim Cinekbea598b2017-04-24 19:48:29 -070084 mRightIcon.setLayoutParams(layoutParams);
Selim Cinek50c6e492016-03-04 16:13:06 -080085 reMeasure = true;
Selim Cinekd2feb632016-02-17 17:05:41 -080086 }
Selim Cinekbea598b2017-04-24 19:48:29 -070087
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 Cinek50c6e492016-03-04 16:13:06 -0800105 mHeader.setPaddingRelative(mHeader.getPaddingStart(),
106 mHeader.getPaddingTop(),
Selim Cinekbea598b2017-04-24 19:48:29 -0700107 mNotificationContentImageMarginEnd,
Selim Cinek50c6e492016-03-04 16:13:06 -0800108 mHeader.getPaddingBottom());
109 reMeasure = true;
110 }
111 }
112 if (reMeasure) {
Selim Cinekbea598b2017-04-24 19:48:29 -0700113 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Selim Cinekd2feb632016-02-17 17:05:41 -0800114 }
Selim Cinek6ecc8102016-01-26 18:26:19 -0800115 }
116
Selim Cineka8818f62017-07-24 17:27:22 -0700117 @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 Cinekbea598b2017-04-24 19:48:29 -0700126 private void resetHeaderIndention() {
Selim Cinek6ecc8102016-01-26 18:26:19 -0800127 if (mHeader.getPaddingEnd() != mNotificationContentMarginEnd) {
Selim Cinek50c6e492016-03-04 16:13:06 -0800128 mHeader.setPaddingRelative(mHeader.getPaddingStart(),
Selim Cinek6ecc8102016-01-26 18:26:19 -0800129 mHeader.getPaddingTop(),
Selim Cinek50c6e492016-03-04 16:13:06 -0800130 mNotificationContentMarginEnd,
Selim Cinek6ecc8102016-01-26 18:26:19 -0800131 mHeader.getPaddingBottom());
Selim Cinek6ecc8102016-01-26 18:26:19 -0800132 }
Selim Cinek50c6e492016-03-04 16:13:06 -0800133 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 Cinek50c6e492016-03-04 16:13:06 -0800139 }
Selim Cinek6ecc8102016-01-26 18:26:19 -0800140 }
141
142 public MediaNotificationView(Context context, AttributeSet attrs, int defStyleAttr,
143 int defStyleRes) {
144 super(context, attrs, defStyleAttr, defStyleRes);
Selim Cinek6ecc8102016-01-26 18:26:19 -0800145 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 Viverette51efddb2017-04-05 10:00:01 -0400154 mRightIcon = findViewById(com.android.internal.R.id.right_icon);
Selim Cinek6ecc8102016-01-26 18:26:19 -0800155 mActions = findViewById(com.android.internal.R.id.media_actions);
156 mHeader = findViewById(com.android.internal.R.id.notification_header);
Selim Cinekd2feb632016-02-17 17:05:41 -0800157 mMainColumn = findViewById(com.android.internal.R.id.notification_main_column);
Beth Thibodeaucb395352019-01-25 15:39:54 -0500158 mMediaContent = findViewById(com.android.internal.R.id.notification_media_content);
Selim Cinek6ecc8102016-01-26 18:26:19 -0800159 }
160}