Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 1 | package com.android.systemui.statusbar.policy; |
| 2 | |
| 3 | import android.app.PendingIntent; |
| 4 | import android.app.RemoteInput; |
| 5 | import android.content.Context; |
| 6 | import android.content.Intent; |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 7 | import android.content.res.TypedArray; |
| 8 | import android.graphics.Canvas; |
| 9 | import android.graphics.drawable.GradientDrawable; |
| 10 | import android.graphics.drawable.RippleDrawable; |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 11 | import android.os.Bundle; |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 12 | import android.text.Layout; |
| 13 | import android.text.TextPaint; |
| 14 | import android.text.method.TransformationMethod; |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 15 | import android.util.AttributeSet; |
| 16 | import android.util.Log; |
| 17 | import android.view.LayoutInflater; |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 18 | import android.view.View; |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 19 | import android.view.ViewGroup; |
| 20 | import android.widget.Button; |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 21 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 22 | import com.android.internal.annotations.VisibleForTesting; |
Milo Sredkov | b0f55e9 | 2018-04-04 16:13:28 +0100 | [diff] [blame] | 23 | import com.android.keyguard.KeyguardHostView.OnDismissAction; |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 24 | import com.android.systemui.Dependency; |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 25 | import com.android.systemui.R; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 26 | import com.android.systemui.statusbar.NotificationData; |
| 27 | import com.android.systemui.statusbar.SmartReplyLogger; |
Milo Sredkov | e7cf498 | 2018-04-09 15:08:26 +0100 | [diff] [blame^] | 28 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Milo Sredkov | b0f55e9 | 2018-04-04 16:13:28 +0100 | [diff] [blame] | 29 | import com.android.systemui.statusbar.phone.KeyguardDismissUtil; |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 30 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 31 | import java.text.BreakIterator; |
| 32 | import java.util.Comparator; |
| 33 | import java.util.PriorityQueue; |
| 34 | |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 35 | /** View which displays smart reply buttons in notifications. */ |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 36 | public class SmartReplyView extends ViewGroup { |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 37 | |
| 38 | private static final String TAG = "SmartReplyView"; |
| 39 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 40 | private static final int MEASURE_SPEC_ANY_WIDTH = |
| 41 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 42 | |
| 43 | private static final Comparator<View> DECREASING_MEASURED_WIDTH_WITHOUT_PADDING_COMPARATOR = |
| 44 | (v1, v2) -> ((v2.getMeasuredWidth() - v2.getPaddingLeft() - v2.getPaddingRight()) |
| 45 | - (v1.getMeasuredWidth() - v1.getPaddingLeft() - v1.getPaddingRight())); |
| 46 | |
| 47 | private static final int SQUEEZE_FAILED = -1; |
| 48 | |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 49 | private final SmartReplyConstants mConstants; |
Milo Sredkov | b0f55e9 | 2018-04-04 16:13:28 +0100 | [diff] [blame] | 50 | private final KeyguardDismissUtil mKeyguardDismissUtil; |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 51 | |
Milo Sredkov | e7cf498 | 2018-04-09 15:08:26 +0100 | [diff] [blame^] | 52 | /** |
| 53 | * The upper bound for the height of this view in pixels. Notifications are automatically |
| 54 | * recreated on density or font size changes so caching this should be fine. |
| 55 | */ |
| 56 | private final int mHeightUpperLimit; |
| 57 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 58 | /** Spacing to be applied between views. */ |
| 59 | private final int mSpacing; |
| 60 | |
| 61 | /** Horizontal padding of smart reply buttons if all of them use only one line of text. */ |
| 62 | private final int mSingleLineButtonPaddingHorizontal; |
| 63 | |
| 64 | /** Horizontal padding of smart reply buttons if at least one of them uses two lines of text. */ |
| 65 | private final int mDoubleLineButtonPaddingHorizontal; |
| 66 | |
| 67 | /** Increase in width of a smart reply button as a result of using two lines instead of one. */ |
| 68 | private final int mSingleToDoubleLineButtonWidthIncrease; |
| 69 | |
| 70 | private final BreakIterator mBreakIterator; |
| 71 | |
| 72 | private PriorityQueue<Button> mCandidateButtonQueueForSqueezing; |
| 73 | |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 74 | public SmartReplyView(Context context, AttributeSet attrs) { |
| 75 | super(context, attrs); |
Petr Cermak | 10011fa | 2018-02-05 19:00:54 +0000 | [diff] [blame] | 76 | mConstants = Dependency.get(SmartReplyConstants.class); |
Milo Sredkov | b0f55e9 | 2018-04-04 16:13:28 +0100 | [diff] [blame] | 77 | mKeyguardDismissUtil = Dependency.get(KeyguardDismissUtil.class); |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 78 | |
Milo Sredkov | e7cf498 | 2018-04-09 15:08:26 +0100 | [diff] [blame^] | 79 | mHeightUpperLimit = NotificationUtils.getFontScaledHeight(mContext, |
| 80 | R.dimen.smart_reply_button_max_height); |
| 81 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 82 | int spacing = 0; |
| 83 | int singleLineButtonPaddingHorizontal = 0; |
| 84 | int doubleLineButtonPaddingHorizontal = 0; |
| 85 | |
| 86 | final TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.SmartReplyView, |
| 87 | 0, 0); |
| 88 | final int length = arr.getIndexCount(); |
| 89 | for (int i = 0; i < length; i++) { |
| 90 | int attr = arr.getIndex(i); |
| 91 | switch (attr) { |
| 92 | case R.styleable.SmartReplyView_spacing: |
| 93 | spacing = arr.getDimensionPixelSize(i, 0); |
| 94 | break; |
| 95 | case R.styleable.SmartReplyView_singleLineButtonPaddingHorizontal: |
| 96 | singleLineButtonPaddingHorizontal = arr.getDimensionPixelSize(i, 0); |
| 97 | break; |
| 98 | case R.styleable.SmartReplyView_doubleLineButtonPaddingHorizontal: |
| 99 | doubleLineButtonPaddingHorizontal = arr.getDimensionPixelSize(i, 0); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | arr.recycle(); |
| 104 | |
| 105 | mSpacing = spacing; |
| 106 | mSingleLineButtonPaddingHorizontal = singleLineButtonPaddingHorizontal; |
| 107 | mDoubleLineButtonPaddingHorizontal = doubleLineButtonPaddingHorizontal; |
| 108 | mSingleToDoubleLineButtonWidthIncrease = |
| 109 | 2 * (doubleLineButtonPaddingHorizontal - singleLineButtonPaddingHorizontal); |
| 110 | |
Milo Sredkov | e7cf498 | 2018-04-09 15:08:26 +0100 | [diff] [blame^] | 111 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 112 | mBreakIterator = BreakIterator.getLineInstance(); |
| 113 | reallocateCandidateButtonQueueForSqueezing(); |
| 114 | } |
| 115 | |
Milo Sredkov | e7cf498 | 2018-04-09 15:08:26 +0100 | [diff] [blame^] | 116 | /** |
| 117 | * Returns an upper bound for the height of this view in pixels. This method is intended to be |
| 118 | * invoked before onMeasure, so it doesn't do any analysis on the contents of the buttons. |
| 119 | */ |
| 120 | public int getHeightUpperLimit() { |
| 121 | return mHeightUpperLimit; |
| 122 | } |
| 123 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 124 | private void reallocateCandidateButtonQueueForSqueezing() { |
| 125 | // Instead of clearing the priority queue, we re-allocate so that it would fit all buttons |
| 126 | // exactly. This avoids (1) wasting memory because PriorityQueue never shrinks and |
| 127 | // (2) growing in onMeasure. |
| 128 | // The constructor throws an IllegalArgument exception if initial capacity is less than 1. |
| 129 | mCandidateButtonQueueForSqueezing = new PriorityQueue<>( |
| 130 | Math.max(getChildCount(), 1), DECREASING_MEASURED_WIDTH_WITHOUT_PADDING_COMPARATOR); |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 133 | public void setRepliesFromRemoteInput(RemoteInput remoteInput, PendingIntent pendingIntent, |
| 134 | SmartReplyLogger smartReplyLogger, NotificationData.Entry entry) { |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 135 | removeAllViews(); |
| 136 | if (remoteInput != null && pendingIntent != null) { |
| 137 | CharSequence[] choices = remoteInput.getChoices(); |
| 138 | if (choices != null) { |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 139 | for (int i = 0; i < choices.length; ++i) { |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 140 | Button replyButton = inflateReplyButton( |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 141 | getContext(), this, i, choices[i], remoteInput, pendingIntent, |
| 142 | smartReplyLogger, entry); |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 143 | addView(replyButton); |
| 144 | } |
| 145 | } |
| 146 | } |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 147 | reallocateCandidateButtonQueueForSqueezing(); |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | public static SmartReplyView inflate(Context context, ViewGroup root) { |
| 151 | return (SmartReplyView) |
| 152 | LayoutInflater.from(context).inflate(R.layout.smart_reply_view, root, false); |
| 153 | } |
| 154 | |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 155 | @VisibleForTesting |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 156 | Button inflateReplyButton(Context context, ViewGroup root, int replyIndex, |
| 157 | CharSequence choice, RemoteInput remoteInput, PendingIntent pendingIntent, |
| 158 | SmartReplyLogger smartReplyLogger, NotificationData.Entry entry) { |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 159 | Button b = (Button) LayoutInflater.from(context).inflate( |
| 160 | R.layout.smart_reply_button, root, false); |
| 161 | b.setText(choice); |
Milo Sredkov | b0f55e9 | 2018-04-04 16:13:28 +0100 | [diff] [blame] | 162 | |
| 163 | OnDismissAction action = () -> { |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 164 | Bundle results = new Bundle(); |
| 165 | results.putString(remoteInput.getResultKey(), choice.toString()); |
| 166 | Intent intent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 167 | RemoteInput.addResultsToIntent(new RemoteInput[]{remoteInput}, intent, results); |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 168 | RemoteInput.setResultsSource(intent, RemoteInput.SOURCE_CHOICE); |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 169 | try { |
| 170 | pendingIntent.send(context, 0, intent); |
| 171 | } catch (PendingIntent.CanceledException e) { |
| 172 | Log.w(TAG, "Unable to send smart reply", e); |
| 173 | } |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 174 | smartReplyLogger.smartReplySent(entry, replyIndex); |
Milo Sredkov | b0f55e9 | 2018-04-04 16:13:28 +0100 | [diff] [blame] | 175 | return false; // do not defer |
| 176 | }; |
| 177 | |
| 178 | b.setOnClickListener(view -> { |
| 179 | mKeyguardDismissUtil.dismissKeyguardThenExecute( |
| 180 | action, null /* cancelAction */, false /* afterKeyguardGone */); |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 181 | }); |
| 182 | return b; |
| 183 | } |
Petr Cermak | 102431d | 2018-01-29 10:36:07 +0000 | [diff] [blame] | 184 | |
| 185 | @Override |
| 186 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 187 | return new LayoutParams(mContext, attrs); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | protected LayoutParams generateDefaultLayoutParams() { |
| 192 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams params) { |
| 197 | return new LayoutParams(params.width, params.height); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 202 | final int targetWidth = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED |
| 203 | ? Integer.MAX_VALUE : MeasureSpec.getSize(widthMeasureSpec); |
| 204 | |
| 205 | // Mark all buttons as hidden and un-squeezed. |
| 206 | resetButtonsLayoutParams(); |
| 207 | |
| 208 | if (!mCandidateButtonQueueForSqueezing.isEmpty()) { |
| 209 | Log.wtf(TAG, "Single line button queue leaked between onMeasure calls"); |
| 210 | mCandidateButtonQueueForSqueezing.clear(); |
| 211 | } |
| 212 | |
| 213 | int measuredWidth = mPaddingLeft + mPaddingRight; |
| 214 | int maxChildHeight = 0; |
| 215 | int displayedChildCount = 0; |
| 216 | int buttonPaddingHorizontal = mSingleLineButtonPaddingHorizontal; |
| 217 | |
| 218 | final int childCount = getChildCount(); |
| 219 | for (int i = 0; i < childCount; i++) { |
| 220 | final View child = getChildAt(i); |
| 221 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 222 | if (child.getVisibility() != View.VISIBLE || !(child instanceof Button)) { |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | child.setPadding(buttonPaddingHorizontal, child.getPaddingTop(), |
| 227 | buttonPaddingHorizontal, child.getPaddingBottom()); |
| 228 | child.measure(MEASURE_SPEC_ANY_WIDTH, heightMeasureSpec); |
| 229 | |
| 230 | final int lineCount = ((Button) child).getLineCount(); |
| 231 | if (lineCount < 1 || lineCount > 2) { |
| 232 | // If smart reply has no text, or more than two lines, then don't show it. |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | if (lineCount == 1) { |
| 237 | mCandidateButtonQueueForSqueezing.add((Button) child); |
| 238 | } |
| 239 | |
| 240 | // Remember the current measurements in case the current button doesn't fit in. |
| 241 | final int originalMaxChildHeight = maxChildHeight; |
| 242 | final int originalMeasuredWidth = measuredWidth; |
| 243 | final int originalButtonPaddingHorizontal = buttonPaddingHorizontal; |
| 244 | |
| 245 | final int spacing = displayedChildCount == 0 ? 0 : mSpacing; |
| 246 | final int childWidth = child.getMeasuredWidth(); |
| 247 | final int childHeight = child.getMeasuredHeight(); |
| 248 | measuredWidth += spacing + childWidth; |
| 249 | maxChildHeight = Math.max(maxChildHeight, childHeight); |
| 250 | |
| 251 | // Do we need to increase the number of lines in smart reply buttons to two? |
| 252 | final boolean increaseToTwoLines = |
| 253 | buttonPaddingHorizontal == mSingleLineButtonPaddingHorizontal |
| 254 | && (lineCount == 2 || measuredWidth > targetWidth); |
| 255 | if (increaseToTwoLines) { |
| 256 | measuredWidth += (displayedChildCount + 1) * mSingleToDoubleLineButtonWidthIncrease; |
| 257 | buttonPaddingHorizontal = mDoubleLineButtonPaddingHorizontal; |
| 258 | } |
| 259 | |
| 260 | // If the last button doesn't fit into the remaining width, try squeezing preceding |
| 261 | // smart reply buttons. |
| 262 | if (measuredWidth > targetWidth) { |
| 263 | // Keep squeezing preceding and current smart reply buttons until they all fit. |
| 264 | while (measuredWidth > targetWidth |
| 265 | && !mCandidateButtonQueueForSqueezing.isEmpty()) { |
| 266 | final Button candidate = mCandidateButtonQueueForSqueezing.poll(); |
| 267 | final int squeezeReduction = squeezeButton(candidate, heightMeasureSpec); |
| 268 | if (squeezeReduction != SQUEEZE_FAILED) { |
| 269 | maxChildHeight = Math.max(maxChildHeight, candidate.getMeasuredHeight()); |
| 270 | measuredWidth -= squeezeReduction; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // If the current button still doesn't fit after squeezing all buttons, undo the |
| 275 | // last squeezing round. |
| 276 | if (measuredWidth > targetWidth) { |
| 277 | measuredWidth = originalMeasuredWidth; |
| 278 | maxChildHeight = originalMaxChildHeight; |
| 279 | buttonPaddingHorizontal = originalButtonPaddingHorizontal; |
| 280 | |
| 281 | // Mark all buttons from the last squeezing round as "failed to squeeze", so |
| 282 | // that they're re-measured without squeezing later. |
| 283 | markButtonsWithPendingSqueezeStatusAs(LayoutParams.SQUEEZE_STATUS_FAILED, i); |
| 284 | |
| 285 | // The current button doesn't fit, so there's no point in measuring further |
| 286 | // buttons. |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | // The current button fits, so mark all squeezed buttons as "successfully squeezed" |
| 291 | // to prevent them from being un-squeezed in a subsequent squeezing round. |
| 292 | markButtonsWithPendingSqueezeStatusAs(LayoutParams.SQUEEZE_STATUS_SUCCESSFUL, i); |
| 293 | } |
| 294 | |
| 295 | lp.show = true; |
| 296 | displayedChildCount++; |
| 297 | } |
| 298 | |
| 299 | // We're done squeezing buttons, so we can clear the priority queue. |
| 300 | mCandidateButtonQueueForSqueezing.clear(); |
| 301 | |
| 302 | // Finally, we need to update corner radius and re-measure some buttons. |
| 303 | updateCornerRadiusAndRemeasureButtonsIfNecessary(buttonPaddingHorizontal, maxChildHeight); |
| 304 | |
| 305 | setMeasuredDimension( |
| 306 | resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth), widthMeasureSpec), |
| 307 | resolveSize(Math.max(getSuggestedMinimumHeight(), |
| 308 | mPaddingTop + maxChildHeight + mPaddingBottom), heightMeasureSpec)); |
| 309 | } |
| 310 | |
| 311 | private void resetButtonsLayoutParams() { |
| 312 | final int childCount = getChildCount(); |
| 313 | for (int i = 0; i < childCount; i++) { |
| 314 | final View child = getChildAt(i); |
| 315 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 316 | lp.show = false; |
| 317 | lp.squeezeStatus = LayoutParams.SQUEEZE_STATUS_NONE; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | private int squeezeButton(Button button, int heightMeasureSpec) { |
| 322 | final int estimatedOptimalTextWidth = estimateOptimalSqueezedButtonTextWidth(button); |
| 323 | if (estimatedOptimalTextWidth == SQUEEZE_FAILED) { |
| 324 | return SQUEEZE_FAILED; |
| 325 | } |
| 326 | return squeezeButtonToTextWidth(button, heightMeasureSpec, estimatedOptimalTextWidth); |
| 327 | } |
| 328 | |
| 329 | private int estimateOptimalSqueezedButtonTextWidth(Button button) { |
| 330 | // Find a line-break point in the middle of the smart reply button text. |
| 331 | final String rawText = button.getText().toString(); |
| 332 | |
| 333 | // The button sometimes has a transformation affecting text layout (e.g. all caps). |
| 334 | final TransformationMethod transformation = button.getTransformationMethod(); |
| 335 | final String text = transformation == null ? |
| 336 | rawText : transformation.getTransformation(rawText, button).toString(); |
| 337 | final int length = text.length(); |
| 338 | mBreakIterator.setText(text); |
| 339 | |
| 340 | if (mBreakIterator.preceding(length / 2) == BreakIterator.DONE) { |
| 341 | if (mBreakIterator.next() == BreakIterator.DONE) { |
| 342 | // Can't find a single possible line break in either direction. |
| 343 | return SQUEEZE_FAILED; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | final TextPaint paint = button.getPaint(); |
| 348 | final int initialPosition = mBreakIterator.current(); |
| 349 | final float initialLeftTextWidth = Layout.getDesiredWidth(text, 0, initialPosition, paint); |
| 350 | final float initialRightTextWidth = |
| 351 | Layout.getDesiredWidth(text, initialPosition, length, paint); |
| 352 | float optimalTextWidth = Math.max(initialLeftTextWidth, initialRightTextWidth); |
| 353 | |
| 354 | if (initialLeftTextWidth != initialRightTextWidth) { |
| 355 | // See if there's a better line-break point (leading to a more narrow button) in |
| 356 | // either left or right direction. |
| 357 | final boolean moveLeft = initialLeftTextWidth > initialRightTextWidth; |
| 358 | final int maxSqueezeRemeasureAttempts = mConstants.getMaxSqueezeRemeasureAttempts(); |
| 359 | for (int i = 0; i < maxSqueezeRemeasureAttempts; i++) { |
| 360 | final int newPosition = |
| 361 | moveLeft ? mBreakIterator.previous() : mBreakIterator.next(); |
| 362 | if (newPosition == BreakIterator.DONE) { |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | final float newLeftTextWidth = Layout.getDesiredWidth(text, 0, newPosition, paint); |
| 367 | final float newRightTextWidth = |
| 368 | Layout.getDesiredWidth(text, newPosition, length, paint); |
| 369 | final float newOptimalTextWidth = Math.max(newLeftTextWidth, newRightTextWidth); |
| 370 | if (newOptimalTextWidth < optimalTextWidth) { |
| 371 | optimalTextWidth = newOptimalTextWidth; |
| 372 | } else { |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | boolean tooFar = moveLeft |
| 377 | ? newLeftTextWidth <= newRightTextWidth |
| 378 | : newLeftTextWidth >= newRightTextWidth; |
| 379 | if (tooFar) { |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return (int) Math.ceil(optimalTextWidth); |
| 386 | } |
| 387 | |
| 388 | private int squeezeButtonToTextWidth(Button button, int heightMeasureSpec, int textWidth) { |
| 389 | int oldWidth = button.getMeasuredWidth(); |
| 390 | if (button.getPaddingLeft() != mDoubleLineButtonPaddingHorizontal) { |
| 391 | // Correct for the fact that the button was laid out with single-line horizontal |
| 392 | // padding. |
| 393 | oldWidth += mSingleToDoubleLineButtonWidthIncrease; |
| 394 | } |
| 395 | |
| 396 | // Re-measure the squeezed smart reply button. |
| 397 | button.setPadding(mDoubleLineButtonPaddingHorizontal, button.getPaddingTop(), |
| 398 | mDoubleLineButtonPaddingHorizontal, button.getPaddingBottom()); |
| 399 | final int widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 400 | 2 * mDoubleLineButtonPaddingHorizontal + textWidth, MeasureSpec.AT_MOST); |
| 401 | button.measure(widthMeasureSpec, heightMeasureSpec); |
| 402 | |
| 403 | final int newWidth = button.getMeasuredWidth(); |
| 404 | |
| 405 | final LayoutParams lp = (LayoutParams) button.getLayoutParams(); |
| 406 | if (button.getLineCount() > 2 || newWidth >= oldWidth) { |
| 407 | lp.squeezeStatus = LayoutParams.SQUEEZE_STATUS_FAILED; |
| 408 | return SQUEEZE_FAILED; |
| 409 | } else { |
| 410 | lp.squeezeStatus = LayoutParams.SQUEEZE_STATUS_PENDING; |
| 411 | return oldWidth - newWidth; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | private void updateCornerRadiusAndRemeasureButtonsIfNecessary( |
| 416 | int buttonPaddingHorizontal, int maxChildHeight) { |
| 417 | final float cornerRadius = ((float) maxChildHeight) / 2; |
| 418 | final int maxChildHeightMeasure = |
| 419 | MeasureSpec.makeMeasureSpec(maxChildHeight, MeasureSpec.EXACTLY); |
| 420 | |
| 421 | final int childCount = getChildCount(); |
| 422 | for (int i = 0; i < childCount; i++) { |
| 423 | final View child = getChildAt(i); |
| 424 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 425 | if (!lp.show) { |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | // Update corner radius. |
| 430 | GradientDrawable backgroundDrawable = |
| 431 | (GradientDrawable) ((RippleDrawable) child.getBackground()).getDrawable(0); |
| 432 | backgroundDrawable.setCornerRadius(cornerRadius); |
| 433 | |
| 434 | boolean requiresNewMeasure = false; |
| 435 | int newWidth = child.getMeasuredWidth(); |
| 436 | |
| 437 | // Re-measure reason 1: The button needs to be un-squeezed (either because it resulted |
| 438 | // in more than two lines or because it was unnecessary). |
| 439 | if (lp.squeezeStatus == LayoutParams.SQUEEZE_STATUS_FAILED) { |
| 440 | requiresNewMeasure = true; |
| 441 | newWidth = Integer.MAX_VALUE; |
| 442 | } |
| 443 | |
| 444 | // Re-measure reason 2: The button's horizontal padding is incorrect (because it was |
| 445 | // measured with the wrong number of lines). |
| 446 | if (child.getPaddingLeft() != buttonPaddingHorizontal) { |
| 447 | requiresNewMeasure = true; |
| 448 | if (buttonPaddingHorizontal == mSingleLineButtonPaddingHorizontal) { |
| 449 | // Decrease padding (2->1 line). |
| 450 | newWidth -= mSingleToDoubleLineButtonWidthIncrease; |
| 451 | } else { |
| 452 | // Increase padding (1->2 lines). |
| 453 | newWidth += mSingleToDoubleLineButtonWidthIncrease; |
| 454 | } |
| 455 | child.setPadding(buttonPaddingHorizontal, child.getPaddingTop(), |
| 456 | buttonPaddingHorizontal, child.getPaddingBottom()); |
| 457 | } |
| 458 | |
| 459 | // Re-measure reason 3: The button's height is less than the max height of all buttons |
| 460 | // (all should have the same height). |
| 461 | if (child.getMeasuredHeight() != maxChildHeight) { |
| 462 | requiresNewMeasure = true; |
| 463 | } |
| 464 | |
| 465 | if (requiresNewMeasure) { |
| 466 | child.measure(MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.AT_MOST), |
| 467 | maxChildHeightMeasure); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | private void markButtonsWithPendingSqueezeStatusAs(int squeezeStatus, int maxChildIndex) { |
| 473 | for (int i = 0; i <= maxChildIndex; i++) { |
| 474 | final View child = getChildAt(i); |
| 475 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 476 | if (lp.squeezeStatus == LayoutParams.SQUEEZE_STATUS_PENDING) { |
| 477 | lp.squeezeStatus = squeezeStatus; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 484 | final boolean isRtl = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; |
| 485 | |
| 486 | final int width = right - left; |
| 487 | int position = isRtl ? width - mPaddingRight : mPaddingLeft; |
| 488 | |
| 489 | final int childCount = getChildCount(); |
| 490 | for (int i = 0; i < childCount; i++) { |
| 491 | final View child = getChildAt(i); |
| 492 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 493 | if (!lp.show) { |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | final int childWidth = child.getMeasuredWidth(); |
| 498 | final int childHeight = child.getMeasuredHeight(); |
| 499 | final int childLeft = isRtl ? position - childWidth : position; |
| 500 | child.layout(childLeft, 0, childLeft + childWidth, childHeight); |
| 501 | |
| 502 | final int childWidthWithSpacing = childWidth + mSpacing; |
| 503 | if (isRtl) { |
| 504 | position -= childWidthWithSpacing; |
| 505 | } else { |
| 506 | position += childWidthWithSpacing; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 513 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 514 | return lp.show && super.drawChild(canvas, child, drawingTime); |
| 515 | } |
| 516 | |
| 517 | @VisibleForTesting |
| 518 | static class LayoutParams extends ViewGroup.LayoutParams { |
| 519 | |
| 520 | /** Button is not squeezed. */ |
| 521 | private static final int SQUEEZE_STATUS_NONE = 0; |
| 522 | |
| 523 | /** |
| 524 | * Button was successfully squeezed, but it might be un-squeezed later if the squeezing |
| 525 | * turns out to have been unnecessary (because there's still not enough space to add another |
| 526 | * button). |
| 527 | */ |
| 528 | private static final int SQUEEZE_STATUS_PENDING = 1; |
| 529 | |
| 530 | /** Button was successfully squeezed and it won't be un-squeezed. */ |
| 531 | private static final int SQUEEZE_STATUS_SUCCESSFUL = 2; |
| 532 | |
| 533 | /** |
| 534 | * Button wasn't successfully squeezed. The squeezing resulted in more than two lines of |
| 535 | * text or it didn't reduce the button's width at all. The button will have to be |
| 536 | * re-measured to use only one line of text. |
| 537 | */ |
| 538 | private static final int SQUEEZE_STATUS_FAILED = 3; |
| 539 | |
| 540 | private boolean show = false; |
| 541 | private int squeezeStatus = SQUEEZE_STATUS_NONE; |
| 542 | |
| 543 | private LayoutParams(Context c, AttributeSet attrs) { |
| 544 | super(c, attrs); |
| 545 | } |
| 546 | |
| 547 | private LayoutParams(int width, int height) { |
| 548 | super(width, height); |
| 549 | } |
| 550 | |
| 551 | @VisibleForTesting |
| 552 | boolean isShown() { |
| 553 | return show; |
| 554 | } |
| 555 | } |
Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame] | 556 | } |