MessagingLayout.java revision ce8794fbbc49baf2777eb9d078890cbf28c890d5
1/* 2 * Copyright (C) 2017 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.AttrRes; 20import android.annotation.NonNull; 21import android.annotation.Nullable; 22import android.annotation.StyleRes; 23import android.app.Notification; 24import android.app.Person; 25import android.content.Context; 26import android.graphics.Bitmap; 27import android.graphics.Canvas; 28import android.graphics.Color; 29import android.graphics.Paint; 30import android.graphics.Rect; 31import android.graphics.drawable.Icon; 32import android.os.Bundle; 33import android.os.Parcelable; 34import android.text.TextUtils; 35import android.util.ArrayMap; 36import android.util.AttributeSet; 37import android.util.DisplayMetrics; 38import android.view.RemotableViewMethod; 39import android.view.ViewTreeObserver; 40import android.view.animation.Interpolator; 41import android.view.animation.PathInterpolator; 42import android.widget.FrameLayout; 43import android.widget.RemoteViews; 44import android.widget.TextView; 45 46import com.android.internal.R; 47import com.android.internal.graphics.ColorUtils; 48import com.android.internal.util.NotificationColorUtil; 49 50import java.util.ArrayList; 51import java.util.List; 52import java.util.function.Consumer; 53import java.util.regex.Pattern; 54 55/** 56 * A custom-built layout for the Notification.MessagingStyle allows dynamic addition and removal 57 * messages and adapts the layout accordingly. 58 */ 59@RemoteViews.RemoteView 60public class MessagingLayout extends FrameLayout { 61 62 private static final float COLOR_SHIFT_AMOUNT = 60; 63 private static final Pattern SPECIAL_CHAR_PATTERN 64 = Pattern.compile ("[!@#$%&*()_+=|<>?{}\\[\\]~-]"); 65 private static final Consumer<MessagingMessage> REMOVE_MESSAGE 66 = MessagingMessage::removeMessage; 67 public static final Interpolator LINEAR_OUT_SLOW_IN = new PathInterpolator(0f, 0f, 0.2f, 1f); 68 public static final Interpolator FAST_OUT_LINEAR_IN = new PathInterpolator(0.4f, 0f, 1f, 1f); 69 public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f); 70 public static final OnLayoutChangeListener MESSAGING_PROPERTY_ANIMATOR 71 = new MessagingPropertyAnimator(); 72 private List<MessagingMessage> mMessages = new ArrayList<>(); 73 private List<MessagingMessage> mHistoricMessages = new ArrayList<>(); 74 private MessagingLinearLayout mMessagingLinearLayout; 75 private boolean mShowHistoricMessages; 76 private ArrayList<MessagingGroup> mGroups = new ArrayList<>(); 77 private TextView mTitleView; 78 private int mLayoutColor; 79 private int mSenderTextColor; 80 private int mMessageTextColor; 81 private int mAvatarSize; 82 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 83 private Paint mTextPaint = new Paint(); 84 private CharSequence mConversationTitle; 85 private Icon mAvatarReplacement; 86 private boolean mIsOneToOne; 87 private ArrayList<MessagingGroup> mAddedGroups = new ArrayList<>(); 88 private Person mUser; 89 private CharSequence mNameReplacement; 90 private boolean mDisplayImagesAtEnd; 91 92 public MessagingLayout(@NonNull Context context) { 93 super(context); 94 } 95 96 public MessagingLayout(@NonNull Context context, @Nullable AttributeSet attrs) { 97 super(context, attrs); 98 } 99 100 public MessagingLayout(@NonNull Context context, @Nullable AttributeSet attrs, 101 @AttrRes int defStyleAttr) { 102 super(context, attrs, defStyleAttr); 103 } 104 105 public MessagingLayout(@NonNull Context context, @Nullable AttributeSet attrs, 106 @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { 107 super(context, attrs, defStyleAttr, defStyleRes); 108 } 109 110 @Override 111 protected void onFinishInflate() { 112 super.onFinishInflate(); 113 mMessagingLinearLayout = findViewById(R.id.notification_messaging); 114 mMessagingLinearLayout.setMessagingLayout(this); 115 // We still want to clip, but only on the top, since views can temporarily out of bounds 116 // during transitions. 117 DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); 118 int size = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels); 119 Rect rect = new Rect(0, 0, size, size); 120 mMessagingLinearLayout.setClipBounds(rect); 121 mTitleView = findViewById(R.id.title); 122 mAvatarSize = getResources().getDimensionPixelSize(R.dimen.messaging_avatar_size); 123 mTextPaint.setTextAlign(Paint.Align.CENTER); 124 mTextPaint.setAntiAlias(true); 125 } 126 127 @RemotableViewMethod 128 public void setAvatarReplacement(Icon icon) { 129 mAvatarReplacement = icon; 130 } 131 132 @RemotableViewMethod 133 public void setNameReplacement(CharSequence nameReplacement) { 134 mNameReplacement = nameReplacement; 135 } 136 137 @RemotableViewMethod 138 public void setDisplayImagesAtEnd(boolean atEnd) { 139 mDisplayImagesAtEnd = atEnd; 140 } 141 142 @RemotableViewMethod 143 public void setData(Bundle extras) { 144 Parcelable[] messages = extras.getParcelableArray(Notification.EXTRA_MESSAGES); 145 List<Notification.MessagingStyle.Message> newMessages 146 = Notification.MessagingStyle.Message.getMessagesFromBundleArray(messages); 147 Parcelable[] histMessages = extras.getParcelableArray(Notification.EXTRA_HISTORIC_MESSAGES); 148 List<Notification.MessagingStyle.Message> newHistoricMessages 149 = Notification.MessagingStyle.Message.getMessagesFromBundleArray(histMessages); 150 setUser(extras.getParcelable(Notification.EXTRA_MESSAGING_PERSON)); 151 mConversationTitle = null; 152 TextView headerText = findViewById(R.id.header_text); 153 if (headerText != null) { 154 mConversationTitle = headerText.getText(); 155 } 156 addRemoteInputHistoryToMessages(newMessages, 157 extras.getCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY)); 158 boolean showSpinner = 159 extras.getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false); 160 bind(newMessages, newHistoricMessages, showSpinner); 161 } 162 163 private void addRemoteInputHistoryToMessages( 164 List<Notification.MessagingStyle.Message> newMessages, 165 CharSequence[] remoteInputHistory) { 166 if (remoteInputHistory == null || remoteInputHistory.length == 0) { 167 return; 168 } 169 for (int i = remoteInputHistory.length - 1; i >= 0; i--) { 170 CharSequence message = remoteInputHistory[i]; 171 newMessages.add(new Notification.MessagingStyle.Message( 172 message, 0, (Person) null, true /* remoteHistory */)); 173 } 174 } 175 176 private void bind(List<Notification.MessagingStyle.Message> newMessages, 177 List<Notification.MessagingStyle.Message> newHistoricMessages, 178 boolean showSpinner) { 179 180 List<MessagingMessage> historicMessages = createMessages(newHistoricMessages, 181 true /* isHistoric */); 182 List<MessagingMessage> messages = createMessages(newMessages, false /* isHistoric */); 183 addMessagesToGroups(historicMessages, messages, showSpinner); 184 185 // Let's remove the remaining messages 186 mMessages.forEach(REMOVE_MESSAGE); 187 mHistoricMessages.forEach(REMOVE_MESSAGE); 188 189 mMessages = messages; 190 mHistoricMessages = historicMessages; 191 192 updateHistoricMessageVisibility(); 193 updateTitleAndNamesDisplay(); 194 } 195 196 private void updateTitleAndNamesDisplay() { 197 ArrayMap<CharSequence, String> uniqueNames = new ArrayMap<>(); 198 ArrayMap<Character, CharSequence> uniqueCharacters = new ArrayMap<>(); 199 for (int i = 0; i < mGroups.size(); i++) { 200 MessagingGroup group = mGroups.get(i); 201 CharSequence senderName = group.getSenderName(); 202 if (!group.needsGeneratedAvatar() || TextUtils.isEmpty(senderName)) { 203 continue; 204 } 205 if (!uniqueNames.containsKey(senderName)) { 206 char c = senderName.charAt(0); 207 if (uniqueCharacters.containsKey(c)) { 208 // this character was already used, lets make it more unique. We first need to 209 // resolve the existing character if it exists 210 CharSequence existingName = uniqueCharacters.get(c); 211 if (existingName != null) { 212 uniqueNames.put(existingName, findNameSplit((String) existingName)); 213 uniqueCharacters.put(c, null); 214 } 215 uniqueNames.put(senderName, findNameSplit((String) senderName)); 216 } else { 217 uniqueNames.put(senderName, Character.toString(c)); 218 uniqueCharacters.put(c, senderName); 219 } 220 } 221 } 222 223 // Now that we have the correct symbols, let's look what we have cached 224 ArrayMap<CharSequence, Icon> cachedAvatars = new ArrayMap<>(); 225 for (int i = 0; i < mGroups.size(); i++) { 226 // Let's now set the avatars 227 MessagingGroup group = mGroups.get(i); 228 boolean isOwnMessage = group.getSender() == mUser; 229 CharSequence senderName = group.getSenderName(); 230 if (!group.needsGeneratedAvatar() || TextUtils.isEmpty(senderName) 231 || (mIsOneToOne && mAvatarReplacement != null && !isOwnMessage)) { 232 continue; 233 } 234 String symbol = uniqueNames.get(senderName); 235 Icon cachedIcon = group.getAvatarSymbolIfMatching(senderName, 236 symbol, mLayoutColor); 237 if (cachedIcon != null) { 238 cachedAvatars.put(senderName, cachedIcon); 239 } 240 } 241 242 for (int i = 0; i < mGroups.size(); i++) { 243 // Let's now set the avatars 244 MessagingGroup group = mGroups.get(i); 245 CharSequence senderName = group.getSenderName(); 246 if (!group.needsGeneratedAvatar() || TextUtils.isEmpty(senderName)) { 247 continue; 248 } 249 if (mIsOneToOne && mAvatarReplacement != null && group.getSender() != mUser) { 250 group.setAvatar(mAvatarReplacement); 251 } else { 252 Icon cachedIcon = cachedAvatars.get(senderName); 253 if (cachedIcon == null) { 254 cachedIcon = createAvatarSymbol(senderName, uniqueNames.get(senderName), 255 mLayoutColor); 256 cachedAvatars.put(senderName, cachedIcon); 257 } 258 group.setCreatedAvatar(cachedIcon, senderName, uniqueNames.get(senderName), 259 mLayoutColor); 260 } 261 } 262 } 263 264 public Icon createAvatarSymbol(CharSequence senderName, String symbol, int layoutColor) { 265 if (symbol.isEmpty() || TextUtils.isDigitsOnly(symbol) || 266 SPECIAL_CHAR_PATTERN.matcher(symbol).find()) { 267 Icon avatarIcon = Icon.createWithResource(getContext(), 268 com.android.internal.R.drawable.messaging_user); 269 avatarIcon.setTint(findColor(senderName, layoutColor)); 270 return avatarIcon; 271 } else { 272 Bitmap bitmap = Bitmap.createBitmap(mAvatarSize, mAvatarSize, Bitmap.Config.ARGB_8888); 273 Canvas canvas = new Canvas(bitmap); 274 float radius = mAvatarSize / 2.0f; 275 int color = findColor(senderName, layoutColor); 276 mPaint.setColor(color); 277 canvas.drawCircle(radius, radius, radius, mPaint); 278 boolean needDarkText = ColorUtils.calculateLuminance(color) > 0.5f; 279 mTextPaint.setColor(needDarkText ? Color.BLACK : Color.WHITE); 280 mTextPaint.setTextSize(symbol.length() == 1 ? mAvatarSize * 0.5f : mAvatarSize * 0.3f); 281 int yPos = (int) (radius - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)); 282 canvas.drawText(symbol, radius, yPos, mTextPaint); 283 return Icon.createWithBitmap(bitmap); 284 } 285 } 286 287 private int findColor(CharSequence senderName, int layoutColor) { 288 double luminance = NotificationColorUtil.calculateLuminance(layoutColor); 289 float shift = Math.abs(senderName.hashCode()) % 5 / 4.0f - 0.5f; 290 291 // we need to offset the range if the luminance is too close to the borders 292 shift += Math.max(COLOR_SHIFT_AMOUNT / 2.0f / 100 - luminance, 0); 293 shift -= Math.max(COLOR_SHIFT_AMOUNT / 2.0f / 100 - (1.0f - luminance), 0); 294 return NotificationColorUtil.getShiftedColor(layoutColor, 295 (int) (shift * COLOR_SHIFT_AMOUNT)); 296 } 297 298 private String findNameSplit(String existingName) { 299 String[] split = existingName.split(" "); 300 if (split.length > 1) { 301 return Character.toString(split[0].charAt(0)) 302 + Character.toString(split[1].charAt(0)); 303 } 304 return existingName.substring(0, 1); 305 } 306 307 @RemotableViewMethod 308 public void setLayoutColor(int color) { 309 mLayoutColor = color; 310 } 311 312 @RemotableViewMethod 313 public void setIsOneToOne(boolean oneToOne) { 314 mIsOneToOne = oneToOne; 315 } 316 317 @RemotableViewMethod 318 public void setSenderTextColor(int color) { 319 mSenderTextColor = color; 320 } 321 322 @RemotableViewMethod 323 public void setMessageTextColor(int color) { 324 mMessageTextColor = color; 325 } 326 327 public void setUser(Person user) { 328 mUser = user; 329 if (mUser.getIcon() == null) { 330 Icon userIcon = Icon.createWithResource(getContext(), 331 com.android.internal.R.drawable.messaging_user); 332 userIcon.setTint(mLayoutColor); 333 mUser = mUser.toBuilder().setIcon(userIcon).build(); 334 } 335 } 336 337 private void addMessagesToGroups(List<MessagingMessage> historicMessages, 338 List<MessagingMessage> messages, boolean showSpinner) { 339 // Let's first find our groups! 340 List<List<MessagingMessage>> groups = new ArrayList<>(); 341 List<Person> senders = new ArrayList<>(); 342 343 // Lets first find the groups 344 findGroups(historicMessages, messages, groups, senders); 345 346 // Let's now create the views and reorder them accordingly 347 createGroupViews(groups, senders, showSpinner); 348 } 349 350 private void createGroupViews(List<List<MessagingMessage>> groups, 351 List<Person> senders, boolean showSpinner) { 352 mGroups.clear(); 353 for (int groupIndex = 0; groupIndex < groups.size(); groupIndex++) { 354 List<MessagingMessage> group = groups.get(groupIndex); 355 MessagingGroup newGroup = null; 356 // we'll just take the first group that exists or create one there is none 357 for (int messageIndex = group.size() - 1; messageIndex >= 0; messageIndex--) { 358 MessagingMessage message = group.get(messageIndex); 359 newGroup = message.getGroup(); 360 if (newGroup != null) { 361 break; 362 } 363 } 364 if (newGroup == null) { 365 newGroup = MessagingGroup.createGroup(mMessagingLinearLayout); 366 mAddedGroups.add(newGroup); 367 } 368 newGroup.setDisplayImagesAtEnd(mDisplayImagesAtEnd); 369 newGroup.setLayoutColor(mLayoutColor); 370 newGroup.setTextColors(mSenderTextColor, mMessageTextColor); 371 Person sender = senders.get(groupIndex); 372 CharSequence nameOverride = null; 373 if (sender != mUser && mNameReplacement != null) { 374 nameOverride = mNameReplacement; 375 } 376 newGroup.setSender(sender, nameOverride); 377 newGroup.setSending(groupIndex == (groups.size() - 1) && showSpinner); 378 mGroups.add(newGroup); 379 380 if (mMessagingLinearLayout.indexOfChild(newGroup) != groupIndex) { 381 mMessagingLinearLayout.removeView(newGroup); 382 mMessagingLinearLayout.addView(newGroup, groupIndex); 383 } 384 newGroup.setMessages(group); 385 } 386 } 387 388 private void findGroups(List<MessagingMessage> historicMessages, 389 List<MessagingMessage> messages, List<List<MessagingMessage>> groups, 390 List<Person> senders) { 391 CharSequence currentSenderKey = null; 392 List<MessagingMessage> currentGroup = null; 393 int histSize = historicMessages.size(); 394 for (int i = 0; i < histSize + messages.size(); i++) { 395 MessagingMessage message; 396 if (i < histSize) { 397 message = historicMessages.get(i); 398 } else { 399 message = messages.get(i - histSize); 400 } 401 boolean isNewGroup = currentGroup == null; 402 Person sender = message.getMessage().getSenderPerson(); 403 CharSequence key = sender == null ? null 404 : sender.getKey() == null ? sender.getName() : sender.getKey(); 405 isNewGroup |= !TextUtils.equals(key, currentSenderKey); 406 if (isNewGroup) { 407 currentGroup = new ArrayList<>(); 408 groups.add(currentGroup); 409 if (sender == null) { 410 sender = mUser; 411 } 412 senders.add(sender); 413 currentSenderKey = key; 414 } 415 currentGroup.add(message); 416 } 417 } 418 419 /** 420 * Creates new messages, reusing existing ones if they are available. 421 * 422 * @param newMessages the messages to parse. 423 */ 424 private List<MessagingMessage> createMessages( 425 List<Notification.MessagingStyle.Message> newMessages, boolean historic) { 426 List<MessagingMessage> result = new ArrayList<>();; 427 for (int i = 0; i < newMessages.size(); i++) { 428 Notification.MessagingStyle.Message m = newMessages.get(i); 429 MessagingMessage message = findAndRemoveMatchingMessage(m); 430 if (message == null) { 431 message = MessagingMessage.createMessage(this, m); 432 } 433 message.setIsHistoric(historic); 434 result.add(message); 435 } 436 return result; 437 } 438 439 private MessagingMessage findAndRemoveMatchingMessage(Notification.MessagingStyle.Message m) { 440 for (int i = 0; i < mMessages.size(); i++) { 441 MessagingMessage existing = mMessages.get(i); 442 if (existing.sameAs(m)) { 443 mMessages.remove(i); 444 return existing; 445 } 446 } 447 for (int i = 0; i < mHistoricMessages.size(); i++) { 448 MessagingMessage existing = mHistoricMessages.get(i); 449 if (existing.sameAs(m)) { 450 mHistoricMessages.remove(i); 451 return existing; 452 } 453 } 454 return null; 455 } 456 457 public void showHistoricMessages(boolean show) { 458 mShowHistoricMessages = show; 459 updateHistoricMessageVisibility(); 460 } 461 462 private void updateHistoricMessageVisibility() { 463 int numHistoric = mHistoricMessages.size(); 464 for (int i = 0; i < numHistoric; i++) { 465 MessagingMessage existing = mHistoricMessages.get(i); 466 existing.setVisibility(mShowHistoricMessages ? VISIBLE : GONE); 467 } 468 int numGroups = mGroups.size(); 469 for (int i = 0; i < numGroups; i++) { 470 MessagingGroup group = mGroups.get(i); 471 int visibleChildren = 0; 472 List<MessagingMessage> messages = group.getMessages(); 473 int numGroupMessages = messages.size(); 474 for (int j = 0; j < numGroupMessages; j++) { 475 MessagingMessage message = messages.get(j); 476 if (message.getVisibility() != GONE) { 477 visibleChildren++; 478 } 479 } 480 if (visibleChildren > 0 && group.getVisibility() == GONE) { 481 group.setVisibility(VISIBLE); 482 } else if (visibleChildren == 0 && group.getVisibility() != GONE) { 483 group.setVisibility(GONE); 484 } 485 } 486 } 487 488 @Override 489 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 490 super.onLayout(changed, left, top, right, bottom); 491 if (!mAddedGroups.isEmpty()) { 492 getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 493 @Override 494 public boolean onPreDraw() { 495 for (MessagingGroup group : mAddedGroups) { 496 if (!group.isShown()) { 497 continue; 498 } 499 MessagingPropertyAnimator.fadeIn(group.getAvatar()); 500 MessagingPropertyAnimator.fadeIn(group.getSenderView()); 501 MessagingPropertyAnimator.startLocalTranslationFrom(group, 502 group.getHeight(), LINEAR_OUT_SLOW_IN); 503 } 504 mAddedGroups.clear(); 505 getViewTreeObserver().removeOnPreDrawListener(this); 506 return true; 507 } 508 }); 509 } 510 } 511 512 public MessagingLinearLayout getMessagingLinearLayout() { 513 return mMessagingLinearLayout; 514 } 515 516 public ArrayList<MessagingGroup> getMessagingGroups() { 517 return mGroups; 518 } 519} 520