Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [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 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.RemotableViewMethod; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.RemoteViews; |
| 28 | |
Selim Cinek | 9281e5e | 2017-03-08 17:29:55 -0800 | [diff] [blame] | 29 | import com.android.internal.R; |
| 30 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 31 | /** |
| 32 | * A custom-built layout for the Notification.MessagingStyle. |
| 33 | * |
| 34 | * Evicts children until they all fit. |
| 35 | */ |
| 36 | @RemoteViews.RemoteView |
| 37 | public class MessagingLinearLayout extends ViewGroup { |
| 38 | |
| 39 | /** |
| 40 | * Spacing to be applied between views. |
| 41 | */ |
| 42 | private int mSpacing; |
| 43 | |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame] | 44 | private int mMaxDisplayedLines = Integer.MAX_VALUE; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 45 | |
Selim Cinek | 1d6b50e | 2017-10-27 16:10:57 -0700 | [diff] [blame] | 46 | private MessagingLayout mMessagingLayout; |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 47 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 48 | public MessagingLinearLayout(Context context, @Nullable AttributeSet attrs) { |
| 49 | super(context, attrs); |
| 50 | |
| 51 | final TypedArray a = context.obtainStyledAttributes(attrs, |
| 52 | R.styleable.MessagingLinearLayout, 0, |
| 53 | 0); |
| 54 | |
| 55 | final int N = a.getIndexCount(); |
| 56 | for (int i = 0; i < N; i++) { |
| 57 | int attr = a.getIndex(i); |
| 58 | switch (attr) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 59 | case R.styleable.MessagingLinearLayout_spacing: |
| 60 | mSpacing = a.getDimensionPixelSize(i, 0); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 65 | a.recycle(); |
| 66 | } |
| 67 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 68 | @Override |
| 69 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 70 | // This is essentially a bottom-up linear layout that only adds children that fit entirely |
| 71 | // up to a maximum height. |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 72 | int targetHeight = MeasureSpec.getSize(heightMeasureSpec); |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 73 | switch (MeasureSpec.getMode(heightMeasureSpec)) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 74 | case MeasureSpec.UNSPECIFIED: |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 75 | targetHeight = Integer.MAX_VALUE; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 76 | break; |
| 77 | } |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 78 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 79 | |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 80 | // Now that we know which views to take, fix up the indents and see what width we get. |
| 81 | int measuredWidth = mPaddingLeft + mPaddingRight; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 82 | final int count = getChildCount(); |
Selim Cinek | dae97c9 | 2017-10-23 16:02:18 -0700 | [diff] [blame] | 83 | int totalHeight; |
| 84 | for (int i = 0; i < count; ++i) { |
| 85 | final View child = getChildAt(i); |
| 86 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 87 | lp.hide = true; |
| 88 | } |
| 89 | |
| 90 | totalHeight = mPaddingTop + mPaddingBottom; |
| 91 | boolean first = true; |
| 92 | int linesRemaining = mMaxDisplayedLines; |
| 93 | |
| 94 | // Starting from the bottom: we measure every view as if it were the only one. If it still |
| 95 | // fits, we take it, otherwise we stop there. |
| 96 | for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) { |
| 97 | if (getChildAt(i).getVisibility() == GONE) { |
| 98 | continue; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 99 | } |
Selim Cinek | dae97c9 | 2017-10-23 16:02:18 -0700 | [diff] [blame] | 100 | final View child = getChildAt(i); |
| 101 | LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); |
| 102 | MessagingChild messagingChild = null; |
| 103 | if (child instanceof MessagingChild) { |
| 104 | messagingChild = (MessagingChild) child; |
| 105 | messagingChild.setMaxDisplayedLines(linesRemaining); |
| 106 | } |
| 107 | int spacing = first ? 0 : mSpacing; |
| 108 | measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, totalHeight |
| 109 | - mPaddingTop - mPaddingBottom + spacing); |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 110 | |
Selim Cinek | dae97c9 | 2017-10-23 16:02:18 -0700 | [diff] [blame] | 111 | final int childHeight = child.getMeasuredHeight(); |
| 112 | int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin + |
| 113 | lp.bottomMargin + spacing); |
| 114 | first = false; |
| 115 | int measureType = MessagingChild.MEASURED_NORMAL; |
| 116 | if (messagingChild != null) { |
| 117 | measureType = messagingChild.getMeasuredType(); |
| 118 | linesRemaining -= messagingChild.getConsumedLines(); |
| 119 | } |
| 120 | boolean isShortened = measureType == MessagingChild.MEASURED_SHORTENED; |
| 121 | boolean isTooSmall = measureType == MessagingChild.MEASURED_TOO_SMALL; |
| 122 | if (newHeight <= targetHeight && !isTooSmall) { |
| 123 | totalHeight = newHeight; |
| 124 | measuredWidth = Math.max(measuredWidth, |
| 125 | child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin |
| 126 | + mPaddingLeft + mPaddingRight); |
| 127 | lp.hide = false; |
| 128 | if (isShortened || linesRemaining <= 0) { |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 129 | break; |
| 130 | } |
Selim Cinek | dae97c9 | 2017-10-23 16:02:18 -0700 | [diff] [blame] | 131 | } else { |
| 132 | break; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 136 | setMeasuredDimension( |
| 137 | resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth), |
| 138 | widthMeasureSpec), |
Selim Cinek | dae97c9 | 2017-10-23 16:02:18 -0700 | [diff] [blame] | 139 | Math.max(getSuggestedMinimumHeight(), totalHeight)); |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 144 | final int paddingLeft = mPaddingLeft; |
| 145 | |
| 146 | int childTop; |
| 147 | |
| 148 | // Where right end of child should go |
| 149 | final int width = right - left; |
| 150 | final int childRight = width - mPaddingRight; |
| 151 | |
| 152 | final int layoutDirection = getLayoutDirection(); |
| 153 | final int count = getChildCount(); |
| 154 | |
| 155 | childTop = mPaddingTop; |
| 156 | |
| 157 | boolean first = true; |
Selim Cinek | d380999 | 2017-10-27 16:09:20 -0700 | [diff] [blame] | 158 | final boolean shown = isShown(); |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 159 | for (int i = 0; i < count; i++) { |
| 160 | final View child = getChildAt(i); |
Selim Cinek | d380999 | 2017-10-27 16:09:20 -0700 | [diff] [blame] | 161 | if (child.getVisibility() == GONE) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 162 | continue; |
| 163 | } |
Selim Cinek | d380999 | 2017-10-27 16:09:20 -0700 | [diff] [blame] | 164 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 165 | MessagingChild messagingChild = (MessagingChild) child; |
| 166 | if (lp.hide) { |
| 167 | if (shown && lp.visibleBefore) { |
| 168 | messagingChild.hideAnimated(); |
| 169 | } |
| 170 | lp.visibleBefore = false; |
| 171 | continue; |
| 172 | } else { |
| 173 | lp.visibleBefore = true; |
| 174 | } |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 175 | |
| 176 | final int childWidth = child.getMeasuredWidth(); |
| 177 | final int childHeight = child.getMeasuredHeight(); |
| 178 | |
| 179 | int childLeft; |
| 180 | if (layoutDirection == LAYOUT_DIRECTION_RTL) { |
| 181 | childLeft = childRight - childWidth - lp.rightMargin; |
| 182 | } else { |
| 183 | childLeft = paddingLeft + lp.leftMargin; |
| 184 | } |
| 185 | |
| 186 | if (!first) { |
| 187 | childTop += mSpacing; |
| 188 | } |
| 189 | |
| 190 | childTop += lp.topMargin; |
| 191 | child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); |
| 192 | |
| 193 | childTop += childHeight + lp.bottomMargin; |
| 194 | |
| 195 | first = false; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 201 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 202 | if (lp.hide) { |
Selim Cinek | d380999 | 2017-10-27 16:09:20 -0700 | [diff] [blame] | 203 | MessagingChild messagingChild = (MessagingChild) child; |
| 204 | if (!messagingChild.isHidingAnimated()) { |
| 205 | return true; |
| 206 | } |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 207 | } |
| 208 | return super.drawChild(canvas, child, drawingTime); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 213 | return new LayoutParams(mContext, attrs); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | protected LayoutParams generateDefaultLayoutParams() { |
| 218 | return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 224 | LayoutParams copy = new LayoutParams(lp.width, lp.height); |
| 225 | if (lp instanceof MarginLayoutParams) { |
| 226 | copy.copyMarginsFrom((MarginLayoutParams) lp); |
| 227 | } |
| 228 | return copy; |
| 229 | } |
| 230 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 231 | /** |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame] | 232 | * Sets how many lines should be displayed at most |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 233 | */ |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 234 | @RemotableViewMethod |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame] | 235 | public void setMaxDisplayedLines(int numberLines) { |
| 236 | mMaxDisplayedLines = numberLines; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Selim Cinek | 1d6b50e | 2017-10-27 16:10:57 -0700 | [diff] [blame] | 239 | public void setMessagingLayout(MessagingLayout layout) { |
| 240 | mMessagingLayout = layout; |
| 241 | } |
| 242 | |
| 243 | public MessagingLayout getMessagingLayout() { |
| 244 | return mMessagingLayout; |
| 245 | } |
| 246 | |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 247 | public interface MessagingChild { |
| 248 | int MEASURED_NORMAL = 0; |
| 249 | int MEASURED_SHORTENED = 1; |
| 250 | int MEASURED_TOO_SMALL = 2; |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame] | 251 | |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 252 | int getMeasuredType(); |
| 253 | int getConsumedLines(); |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame] | 254 | void setMaxDisplayedLines(int lines); |
Selim Cinek | d380999 | 2017-10-27 16:09:20 -0700 | [diff] [blame] | 255 | void hideAnimated(); |
| 256 | boolean isHidingAnimated(); |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 259 | public static class LayoutParams extends MarginLayoutParams { |
| 260 | |
Selim Cinek | d380999 | 2017-10-27 16:09:20 -0700 | [diff] [blame] | 261 | public boolean hide = false; |
| 262 | public boolean visibleBefore = false; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 263 | |
| 264 | public LayoutParams(Context c, AttributeSet attrs) { |
| 265 | super(c, attrs); |
| 266 | } |
| 267 | |
| 268 | public LayoutParams(int width, int height) { |
| 269 | super(width, height); |
| 270 | } |
| 271 | } |
| 272 | } |