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; 24 import android.widget.FrameLayout; 25 import android.widget.ImageView; 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 34 public class MediaNotificationView extends FrameLayout { 35 36 private final int mNotificationContentMarginEnd; 37 private final int mNotificationContentImageMarginEnd; 38 private ImageView mRightIcon; 39 private View mActions; 40 private View mHeader; 41 private View mMainColumn; 42 private int mImagePushIn; 43 44 public MediaNotificationView(Context context) { 45 this(context, null); 46 } 47 48 public MediaNotificationView(Context context, @Nullable AttributeSet attrs) { 49 this(context, attrs, 0); 50 } 51 52 public MediaNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 53 this(context, attrs, defStyleAttr, 0); 54 } 55 56 @Override 57 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 58 boolean hasIcon = mRightIcon.getVisibility() != GONE; 59 if (!hasIcon) { 60 resetHeaderIndention(); 61 } 62 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 63 int mode = MeasureSpec.getMode(widthMeasureSpec); 64 boolean reMeasure = false; 65 mImagePushIn = 0; 66 if (hasIcon && mode != MeasureSpec.UNSPECIFIED) { 67 int size = MeasureSpec.getSize(widthMeasureSpec); 68 size = size - mActions.getMeasuredWidth(); 69 ViewGroup.MarginLayoutParams layoutParams = 70 (MarginLayoutParams) mRightIcon.getLayoutParams(); 71 int imageEndMargin = layoutParams.getMarginEnd(); 72 size -= imageEndMargin; 73 int fullHeight = getMeasuredHeight(); 74 if (size > fullHeight) { 75 size = fullHeight; 76 } else if (size < fullHeight) { 77 size = Math.max(0, size); 78 mImagePushIn = fullHeight - size; 79 } 80 if (layoutParams.width != fullHeight || layoutParams.height != fullHeight) { 81 layoutParams.width = fullHeight; 82 layoutParams.height = fullHeight; 83 mRightIcon.setLayoutParams(layoutParams); 84 reMeasure = true; 85 } 86 87 // lets ensure that the main column doesn't run into the image 88 ViewGroup.MarginLayoutParams params 89 = (MarginLayoutParams) mMainColumn.getLayoutParams(); 90 int marginEnd = size + imageEndMargin + mNotificationContentMarginEnd; 91 if (marginEnd != params.getMarginEnd()) { 92 params.setMarginEnd(marginEnd); 93 mMainColumn.setLayoutParams(params); 94 reMeasure = true; 95 } 96 int headerMarginEnd = size + imageEndMargin; 97 params = (MarginLayoutParams) mHeader.getLayoutParams(); 98 if (params.getMarginEnd() != headerMarginEnd) { 99 params.setMarginEnd(headerMarginEnd); 100 mHeader.setLayoutParams(params); 101 reMeasure = true; 102 } 103 if (mHeader.getPaddingEnd() != mNotificationContentImageMarginEnd) { 104 mHeader.setPaddingRelative(mHeader.getPaddingStart(), 105 mHeader.getPaddingTop(), 106 mNotificationContentImageMarginEnd, 107 mHeader.getPaddingBottom()); 108 reMeasure = true; 109 } 110 } 111 if (reMeasure) { 112 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 113 } 114 } 115 116 @Override 117 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 118 super.onLayout(changed, left, top, right, bottom); 119 if (mImagePushIn > 0) { 120 mRightIcon.layout(mRightIcon.getLeft() + mImagePushIn, mRightIcon.getTop(), 121 mRightIcon.getRight() + mImagePushIn, mRightIcon.getBottom()); 122 } 123 } 124 125 private void resetHeaderIndention() { 126 if (mHeader.getPaddingEnd() != mNotificationContentMarginEnd) { 127 mHeader.setPaddingRelative(mHeader.getPaddingStart(), 128 mHeader.getPaddingTop(), 129 mNotificationContentMarginEnd, 130 mHeader.getPaddingBottom()); 131 } 132 ViewGroup.MarginLayoutParams headerParams = 133 (MarginLayoutParams) mHeader.getLayoutParams(); 134 headerParams.setMarginEnd(0); 135 if (headerParams.getMarginEnd() != 0) { 136 headerParams.setMarginEnd(0); 137 mHeader.setLayoutParams(headerParams); 138 } 139 } 140 141 public MediaNotificationView(Context context, AttributeSet attrs, int defStyleAttr, 142 int defStyleRes) { 143 super(context, attrs, defStyleAttr, defStyleRes); 144 mNotificationContentMarginEnd = context.getResources().getDimensionPixelSize( 145 com.android.internal.R.dimen.notification_content_margin_end); 146 mNotificationContentImageMarginEnd = context.getResources().getDimensionPixelSize( 147 com.android.internal.R.dimen.notification_content_image_margin_end); 148 } 149 150 @Override 151 protected void onFinishInflate() { 152 super.onFinishInflate(); 153 mRightIcon = findViewById(com.android.internal.R.id.right_icon); 154 mActions = findViewById(com.android.internal.R.id.media_actions); 155 mHeader = findViewById(com.android.internal.R.id.notification_header); 156 mMainColumn = findViewById(com.android.internal.R.id.notification_main_column); 157 } 158 } 159