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