blob: 27cd6e13d86c00c26ce9ef280e25515cda11e13a [file] [log] [blame]
Selim Cinek88188f22017-09-19 16:46:56 -07001/*
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
Selim Cinek514b52c2020-03-17 18:42:12 -070019import static com.android.internal.widget.MessagingGroup.IMAGE_DISPLAY_LOCATION_AT_END;
20import static com.android.internal.widget.MessagingGroup.IMAGE_DISPLAY_LOCATION_INLINE;
21
Selim Cinek88188f22017-09-19 16:46:56 -070022import android.annotation.AttrRes;
23import android.annotation.NonNull;
24import android.annotation.Nullable;
25import android.annotation.StyleRes;
26import android.app.Notification;
Selim Cinek9acd6732018-03-23 16:39:02 -070027import android.app.Person;
Aran Ink2e3cc412019-12-13 16:30:17 -050028import android.app.RemoteInputHistoryItem;
Selim Cinek88188f22017-09-19 16:46:56 -070029import android.content.Context;
30import android.graphics.Bitmap;
31import android.graphics.Canvas;
32import android.graphics.Color;
33import android.graphics.Paint;
Selim Cinekd3809992017-10-27 16:09:20 -070034import android.graphics.Rect;
Selim Cinek88188f22017-09-19 16:46:56 -070035import android.graphics.drawable.Icon;
36import android.os.Bundle;
37import android.os.Parcelable;
38import android.text.TextUtils;
39import android.util.ArrayMap;
40import android.util.AttributeSet;
Selim Cinekd3809992017-10-27 16:09:20 -070041import android.util.DisplayMetrics;
Selim Cinek88188f22017-09-19 16:46:56 -070042import android.view.RemotableViewMethod;
Selim Cinekd3809992017-10-27 16:09:20 -070043import android.view.ViewTreeObserver;
44import android.view.animation.Interpolator;
45import android.view.animation.PathInterpolator;
Selim Cinek88188f22017-09-19 16:46:56 -070046import android.widget.FrameLayout;
47import android.widget.RemoteViews;
48import android.widget.TextView;
49
50import com.android.internal.R;
51import com.android.internal.graphics.ColorUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070052import com.android.internal.util.ContrastColorUtil;
Selim Cinek88188f22017-09-19 16:46:56 -070053
54import java.util.ArrayList;
55import java.util.List;
56import java.util.function.Consumer;
Tony Huang45523682018-05-02 11:42:27 +080057import java.util.regex.Pattern;
Selim Cinek88188f22017-09-19 16:46:56 -070058
59/**
60 * A custom-built layout for the Notification.MessagingStyle allows dynamic addition and removal
61 * messages and adapts the layout accordingly.
62 */
63@RemoteViews.RemoteView
Selim Cinek20d1ee22020-02-03 16:04:26 -050064public class MessagingLayout extends FrameLayout
65 implements ImageMessageConsumer, IMessagingLayout {
Selim Cinek88188f22017-09-19 16:46:56 -070066
67 private static final float COLOR_SHIFT_AMOUNT = 60;
Tony Huang1250cd12018-06-06 15:40:47 +080068 /**
69 * Pattren for filter some ingonable characters.
70 * p{Z} for any kind of whitespace or invisible separator.
71 * p{C} for any kind of punctuation character.
72 */
73 private static final Pattern IGNORABLE_CHAR_PATTERN
74 = Pattern.compile("[\\p{C}\\p{Z}]");
Tony Huang45523682018-05-02 11:42:27 +080075 private static final Pattern SPECIAL_CHAR_PATTERN
76 = Pattern.compile ("[!@#$%&*()_+=|<>?{}\\[\\]~-]");
Selim Cinek88188f22017-09-19 16:46:56 -070077 private static final Consumer<MessagingMessage> REMOVE_MESSAGE
78 = MessagingMessage::removeMessage;
Selim Cinekd3809992017-10-27 16:09:20 -070079 public static final Interpolator LINEAR_OUT_SLOW_IN = new PathInterpolator(0f, 0f, 0.2f, 1f);
80 public static final Interpolator FAST_OUT_LINEAR_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);
81 public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
82 public static final OnLayoutChangeListener MESSAGING_PROPERTY_ANIMATOR
83 = new MessagingPropertyAnimator();
Selim Cinek88188f22017-09-19 16:46:56 -070084 private List<MessagingMessage> mMessages = new ArrayList<>();
85 private List<MessagingMessage> mHistoricMessages = new ArrayList<>();
86 private MessagingLinearLayout mMessagingLinearLayout;
Selim Cinek88188f22017-09-19 16:46:56 -070087 private boolean mShowHistoricMessages;
88 private ArrayList<MessagingGroup> mGroups = new ArrayList<>();
89 private TextView mTitleView;
90 private int mLayoutColor;
Kenny Guy14d035c2018-05-02 19:10:36 +010091 private int mSenderTextColor;
92 private int mMessageTextColor;
Selim Cinek88188f22017-09-19 16:46:56 -070093 private int mAvatarSize;
94 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
95 private Paint mTextPaint = new Paint();
96 private CharSequence mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -070097 private Icon mAvatarReplacement;
Selim Cinek88188f22017-09-19 16:46:56 -070098 private boolean mIsOneToOne;
Selim Cinekd3809992017-10-27 16:09:20 -070099 private ArrayList<MessagingGroup> mAddedGroups = new ArrayList<>();
Selim Cinek9acd6732018-03-23 16:39:02 -0700100 private Person mUser;
Selim Cinek2dd3e722018-01-19 11:06:06 -0800101 private CharSequence mNameReplacement;
Selim Cinek85d0e6e2018-03-23 18:08:32 -0700102 private boolean mDisplayImagesAtEnd;
Ahan Wude396fa2018-05-08 20:42:24 +0800103 private ImageResolver mImageResolver;
Selim Cinek88188f22017-09-19 16:46:56 -0700104
105 public MessagingLayout(@NonNull Context context) {
106 super(context);
107 }
108
109 public MessagingLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
110 super(context, attrs);
111 }
112
113 public MessagingLayout(@NonNull Context context, @Nullable AttributeSet attrs,
114 @AttrRes int defStyleAttr) {
115 super(context, attrs, defStyleAttr);
116 }
117
118 public MessagingLayout(@NonNull Context context, @Nullable AttributeSet attrs,
119 @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
120 super(context, attrs, defStyleAttr, defStyleRes);
121 }
122
123 @Override
124 protected void onFinishInflate() {
125 super.onFinishInflate();
126 mMessagingLinearLayout = findViewById(R.id.notification_messaging);
Selim Cinekd3809992017-10-27 16:09:20 -0700127 // We still want to clip, but only on the top, since views can temporarily out of bounds
128 // during transitions.
129 DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
Selim Cinek1110fd72018-05-01 10:53:26 -0700130 int size = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels);
131 Rect rect = new Rect(0, 0, size, size);
Selim Cinekd3809992017-10-27 16:09:20 -0700132 mMessagingLinearLayout.setClipBounds(rect);
Selim Cinek88188f22017-09-19 16:46:56 -0700133 mTitleView = findViewById(R.id.title);
134 mAvatarSize = getResources().getDimensionPixelSize(R.dimen.messaging_avatar_size);
135 mTextPaint.setTextAlign(Paint.Align.CENTER);
136 mTextPaint.setAntiAlias(true);
137 }
138
139 @RemotableViewMethod
Selim Cinekce8794f2018-05-23 16:46:05 -0700140 public void setAvatarReplacement(Icon icon) {
141 mAvatarReplacement = icon;
Selim Cinek88188f22017-09-19 16:46:56 -0700142 }
143
144 @RemotableViewMethod
Selim Cinek2dd3e722018-01-19 11:06:06 -0800145 public void setNameReplacement(CharSequence nameReplacement) {
146 mNameReplacement = nameReplacement;
147 }
148
Selim Cinek20d1ee22020-02-03 16:04:26 -0500149 /**
150 * Set this layout to show the collapsed representation.
151 *
152 * @param isCollapsed is it collapsed
153 */
Selim Cinek2dd3e722018-01-19 11:06:06 -0800154 @RemotableViewMethod
Selim Cinek20d1ee22020-02-03 16:04:26 -0500155 public void setIsCollapsed(boolean isCollapsed) {
156 mDisplayImagesAtEnd = isCollapsed;
157 }
158
159 @RemotableViewMethod
160 public void setLargeIcon(Icon largeIcon) {
161 // Unused
Selim Cinek7199ed92018-01-26 13:36:43 -0800162 }
163
Selim Cinek2f7f7b82020-03-10 15:41:13 -0700164 /**
165 * Sets the conversation title of this conversation.
166 *
167 * @param conversationTitle the conversation title
168 */
169 @RemotableViewMethod
170 public void setConversationTitle(CharSequence conversationTitle) {
171 // Unused
172 }
173
Selim Cinek7199ed92018-01-26 13:36:43 -0800174 @RemotableViewMethod
Selim Cinek88188f22017-09-19 16:46:56 -0700175 public void setData(Bundle extras) {
176 Parcelable[] messages = extras.getParcelableArray(Notification.EXTRA_MESSAGES);
177 List<Notification.MessagingStyle.Message> newMessages
178 = Notification.MessagingStyle.Message.getMessagesFromBundleArray(messages);
179 Parcelable[] histMessages = extras.getParcelableArray(Notification.EXTRA_HISTORIC_MESSAGES);
180 List<Notification.MessagingStyle.Message> newHistoricMessages
181 = Notification.MessagingStyle.Message.getMessagesFromBundleArray(histMessages);
Selim Cinekafeed292017-12-12 17:32:44 -0800182 setUser(extras.getParcelable(Notification.EXTRA_MESSAGING_PERSON));
Selim Cinek88188f22017-09-19 16:46:56 -0700183 mConversationTitle = null;
184 TextView headerText = findViewById(R.id.header_text);
185 if (headerText != null) {
186 mConversationTitle = headerText.getText();
187 }
Aran Ink2e3cc412019-12-13 16:30:17 -0500188 RemoteInputHistoryItem[] history = (RemoteInputHistoryItem[])
189 extras.getParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS);
190 addRemoteInputHistoryToMessages(newMessages, history);
Kenny Guya0f6de82018-04-06 16:20:16 +0100191 boolean showSpinner =
192 extras.getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false);
193 bind(newMessages, newHistoricMessages, showSpinner);
Selim Cinek88188f22017-09-19 16:46:56 -0700194 }
195
Ahan Wude396fa2018-05-08 20:42:24 +0800196 @Override
197 public void setImageResolver(ImageResolver resolver) {
198 mImageResolver = resolver;
199 }
200
Selim Cinek1397ea32018-01-16 17:34:52 -0800201 private void addRemoteInputHistoryToMessages(
202 List<Notification.MessagingStyle.Message> newMessages,
Aran Ink2e3cc412019-12-13 16:30:17 -0500203 RemoteInputHistoryItem[] remoteInputHistory) {
Selim Cinek1397ea32018-01-16 17:34:52 -0800204 if (remoteInputHistory == null || remoteInputHistory.length == 0) {
205 return;
206 }
207 for (int i = remoteInputHistory.length - 1; i >= 0; i--) {
Aran Ink2e3cc412019-12-13 16:30:17 -0500208 RemoteInputHistoryItem historyMessage = remoteInputHistory[i];
209 Notification.MessagingStyle.Message message = new Notification.MessagingStyle.Message(
210 historyMessage.getText(), 0, (Person) null, true /* remoteHistory */);
211 if (historyMessage.getUri() != null) {
212 message.setData(historyMessage.getMimeType(), historyMessage.getUri());
213 }
214 newMessages.add(message);
Selim Cinek1397ea32018-01-16 17:34:52 -0800215 }
216 }
217
Selim Cinek88188f22017-09-19 16:46:56 -0700218 private void bind(List<Notification.MessagingStyle.Message> newMessages,
Kenny Guya0f6de82018-04-06 16:20:16 +0100219 List<Notification.MessagingStyle.Message> newHistoricMessages,
220 boolean showSpinner) {
Selim Cinek88188f22017-09-19 16:46:56 -0700221
222 List<MessagingMessage> historicMessages = createMessages(newHistoricMessages,
223 true /* isHistoric */);
224 List<MessagingMessage> messages = createMessages(newMessages, false /* isHistoric */);
Selim Cinekf68af9b2018-05-24 16:37:22 -0700225
226 ArrayList<MessagingGroup> oldGroups = new ArrayList<>(mGroups);
Kenny Guya0f6de82018-04-06 16:20:16 +0100227 addMessagesToGroups(historicMessages, messages, showSpinner);
Selim Cinek88188f22017-09-19 16:46:56 -0700228
Selim Cinekf68af9b2018-05-24 16:37:22 -0700229 // Let's first check which groups were removed altogether and remove them in one animation
230 removeGroups(oldGroups);
231
Selim Cinek88188f22017-09-19 16:46:56 -0700232 // Let's remove the remaining messages
233 mMessages.forEach(REMOVE_MESSAGE);
234 mHistoricMessages.forEach(REMOVE_MESSAGE);
235
236 mMessages = messages;
237 mHistoricMessages = historicMessages;
238
Selim Cinek88188f22017-09-19 16:46:56 -0700239 updateHistoricMessageVisibility();
240 updateTitleAndNamesDisplay();
241 }
242
Selim Cinekf68af9b2018-05-24 16:37:22 -0700243 private void removeGroups(ArrayList<MessagingGroup> oldGroups) {
244 int size = oldGroups.size();
245 for (int i = 0; i < size; i++) {
246 MessagingGroup group = oldGroups.get(i);
247 if (!mGroups.contains(group)) {
248 List<MessagingMessage> messages = group.getMessages();
249 Runnable endRunnable = () -> {
250 mMessagingLinearLayout.removeTransientView(group);
251 group.recycle();
252 };
253
254 boolean wasShown = group.isShown();
255 mMessagingLinearLayout.removeView(group);
256 if (wasShown && !MessagingLinearLayout.isGone(group)) {
257 mMessagingLinearLayout.addTransientView(group, 0);
258 group.removeGroupAnimated(endRunnable);
259 } else {
260 endRunnable.run();
261 }
262 mMessages.removeAll(messages);
263 mHistoricMessages.removeAll(messages);
264 }
265 }
266 }
267
Selim Cinek88188f22017-09-19 16:46:56 -0700268 private void updateTitleAndNamesDisplay() {
269 ArrayMap<CharSequence, String> uniqueNames = new ArrayMap<>();
270 ArrayMap<Character, CharSequence> uniqueCharacters = new ArrayMap<>();
271 for (int i = 0; i < mGroups.size(); i++) {
272 MessagingGroup group = mGroups.get(i);
273 CharSequence senderName = group.getSenderName();
Selim Cinekafeed292017-12-12 17:32:44 -0800274 if (!group.needsGeneratedAvatar() || TextUtils.isEmpty(senderName)) {
Selim Cinek88188f22017-09-19 16:46:56 -0700275 continue;
276 }
Selim Cinekafeed292017-12-12 17:32:44 -0800277 if (!uniqueNames.containsKey(senderName)) {
Tony Huang1250cd12018-06-06 15:40:47 +0800278 // Only use visible characters to get uniqueNames
279 String pureSenderName = IGNORABLE_CHAR_PATTERN
280 .matcher(senderName).replaceAll("" /* replacement */);
281 char c = pureSenderName.charAt(0);
Selim Cinek88188f22017-09-19 16:46:56 -0700282 if (uniqueCharacters.containsKey(c)) {
283 // this character was already used, lets make it more unique. We first need to
284 // resolve the existing character if it exists
285 CharSequence existingName = uniqueCharacters.get(c);
286 if (existingName != null) {
287 uniqueNames.put(existingName, findNameSplit((String) existingName));
288 uniqueCharacters.put(c, null);
289 }
290 uniqueNames.put(senderName, findNameSplit((String) senderName));
291 } else {
292 uniqueNames.put(senderName, Character.toString(c));
Tony Huang1250cd12018-06-06 15:40:47 +0800293 uniqueCharacters.put(c, pureSenderName);
Selim Cinek88188f22017-09-19 16:46:56 -0700294 }
295 }
296 }
297
298 // Now that we have the correct symbols, let's look what we have cached
299 ArrayMap<CharSequence, Icon> cachedAvatars = new ArrayMap<>();
300 for (int i = 0; i < mGroups.size(); i++) {
301 // Let's now set the avatars
302 MessagingGroup group = mGroups.get(i);
Selim Cinek1397ea32018-01-16 17:34:52 -0800303 boolean isOwnMessage = group.getSender() == mUser;
Selim Cinek88188f22017-09-19 16:46:56 -0700304 CharSequence senderName = group.getSenderName();
Selim Cinekafeed292017-12-12 17:32:44 -0800305 if (!group.needsGeneratedAvatar() || TextUtils.isEmpty(senderName)
Selim Cinekce8794f2018-05-23 16:46:05 -0700306 || (mIsOneToOne && mAvatarReplacement != null && !isOwnMessage)) {
Selim Cinek88188f22017-09-19 16:46:56 -0700307 continue;
308 }
309 String symbol = uniqueNames.get(senderName);
310 Icon cachedIcon = group.getAvatarSymbolIfMatching(senderName,
311 symbol, mLayoutColor);
312 if (cachedIcon != null) {
313 cachedAvatars.put(senderName, cachedIcon);
314 }
315 }
316
317 for (int i = 0; i < mGroups.size(); i++) {
318 // Let's now set the avatars
319 MessagingGroup group = mGroups.get(i);
320 CharSequence senderName = group.getSenderName();
Selim Cinekafeed292017-12-12 17:32:44 -0800321 if (!group.needsGeneratedAvatar() || TextUtils.isEmpty(senderName)) {
Selim Cinek88188f22017-09-19 16:46:56 -0700322 continue;
323 }
Selim Cinekce8794f2018-05-23 16:46:05 -0700324 if (mIsOneToOne && mAvatarReplacement != null && group.getSender() != mUser) {
325 group.setAvatar(mAvatarReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -0700326 } else {
327 Icon cachedIcon = cachedAvatars.get(senderName);
328 if (cachedIcon == null) {
329 cachedIcon = createAvatarSymbol(senderName, uniqueNames.get(senderName),
330 mLayoutColor);
331 cachedAvatars.put(senderName, cachedIcon);
332 }
333 group.setCreatedAvatar(cachedIcon, senderName, uniqueNames.get(senderName),
334 mLayoutColor);
335 }
336 }
337 }
338
339 public Icon createAvatarSymbol(CharSequence senderName, String symbol, int layoutColor) {
Tony Huang45523682018-05-02 11:42:27 +0800340 if (symbol.isEmpty() || TextUtils.isDigitsOnly(symbol) ||
341 SPECIAL_CHAR_PATTERN.matcher(symbol).find()) {
342 Icon avatarIcon = Icon.createWithResource(getContext(),
343 com.android.internal.R.drawable.messaging_user);
344 avatarIcon.setTint(findColor(senderName, layoutColor));
345 return avatarIcon;
346 } else {
347 Bitmap bitmap = Bitmap.createBitmap(mAvatarSize, mAvatarSize, Bitmap.Config.ARGB_8888);
348 Canvas canvas = new Canvas(bitmap);
349 float radius = mAvatarSize / 2.0f;
350 int color = findColor(senderName, layoutColor);
351 mPaint.setColor(color);
352 canvas.drawCircle(radius, radius, radius, mPaint);
353 boolean needDarkText = ColorUtils.calculateLuminance(color) > 0.5f;
354 mTextPaint.setColor(needDarkText ? Color.BLACK : Color.WHITE);
355 mTextPaint.setTextSize(symbol.length() == 1 ? mAvatarSize * 0.5f : mAvatarSize * 0.3f);
356 int yPos = (int) (radius - ((mTextPaint.descent() + mTextPaint.ascent()) / 2));
357 canvas.drawText(symbol, radius, yPos, mTextPaint);
358 return Icon.createWithBitmap(bitmap);
359 }
Selim Cinek88188f22017-09-19 16:46:56 -0700360 }
361
362 private int findColor(CharSequence senderName, int layoutColor) {
Lucas Dupina291d192018-06-07 13:59:42 -0700363 double luminance = ContrastColorUtil.calculateLuminance(layoutColor);
Selim Cinek88188f22017-09-19 16:46:56 -0700364 float shift = Math.abs(senderName.hashCode()) % 5 / 4.0f - 0.5f;
365
366 // we need to offset the range if the luminance is too close to the borders
367 shift += Math.max(COLOR_SHIFT_AMOUNT / 2.0f / 100 - luminance, 0);
368 shift -= Math.max(COLOR_SHIFT_AMOUNT / 2.0f / 100 - (1.0f - luminance), 0);
Lucas Dupina291d192018-06-07 13:59:42 -0700369 return ContrastColorUtil.getShiftedColor(layoutColor,
Selim Cinek88188f22017-09-19 16:46:56 -0700370 (int) (shift * COLOR_SHIFT_AMOUNT));
371 }
372
373 private String findNameSplit(String existingName) {
374 String[] split = existingName.split(" ");
375 if (split.length > 1) {
376 return Character.toString(split[0].charAt(0))
377 + Character.toString(split[1].charAt(0));
378 }
379 return existingName.substring(0, 1);
380 }
381
382 @RemotableViewMethod
383 public void setLayoutColor(int color) {
384 mLayoutColor = color;
385 }
386
387 @RemotableViewMethod
388 public void setIsOneToOne(boolean oneToOne) {
389 mIsOneToOne = oneToOne;
390 }
391
Kenny Guy14d035c2018-05-02 19:10:36 +0100392 @RemotableViewMethod
393 public void setSenderTextColor(int color) {
394 mSenderTextColor = color;
395 }
396
Selim Cineke9714eb2020-03-09 11:21:49 -0700397
398 /**
399 * @param color the color of the notification background
400 */
401 @RemotableViewMethod
402 public void setNotificationBackgroundColor(int color) {
403 // Nothing to do with this
404 }
405
Kenny Guy14d035c2018-05-02 19:10:36 +0100406 @RemotableViewMethod
407 public void setMessageTextColor(int color) {
408 mMessageTextColor = color;
409 }
410
Selim Cinek9acd6732018-03-23 16:39:02 -0700411 public void setUser(Person user) {
Selim Cinekafeed292017-12-12 17:32:44 -0800412 mUser = user;
Selim Cinek1397ea32018-01-16 17:34:52 -0800413 if (mUser.getIcon() == null) {
414 Icon userIcon = Icon.createWithResource(getContext(),
415 com.android.internal.R.drawable.messaging_user);
416 userIcon.setTint(mLayoutColor);
Selim Cinek9acd6732018-03-23 16:39:02 -0700417 mUser = mUser.toBuilder().setIcon(userIcon).build();
Selim Cinek1397ea32018-01-16 17:34:52 -0800418 }
Selim Cinekafeed292017-12-12 17:32:44 -0800419 }
420
Selim Cinek88188f22017-09-19 16:46:56 -0700421 private void addMessagesToGroups(List<MessagingMessage> historicMessages,
Kenny Guya0f6de82018-04-06 16:20:16 +0100422 List<MessagingMessage> messages, boolean showSpinner) {
Selim Cinek88188f22017-09-19 16:46:56 -0700423 // Let's first find our groups!
424 List<List<MessagingMessage>> groups = new ArrayList<>();
Selim Cinek9acd6732018-03-23 16:39:02 -0700425 List<Person> senders = new ArrayList<>();
Selim Cinek88188f22017-09-19 16:46:56 -0700426
427 // Lets first find the groups
428 findGroups(historicMessages, messages, groups, senders);
429
430 // Let's now create the views and reorder them accordingly
Kenny Guya0f6de82018-04-06 16:20:16 +0100431 createGroupViews(groups, senders, showSpinner);
Selim Cinek88188f22017-09-19 16:46:56 -0700432 }
433
Selim Cinekafeed292017-12-12 17:32:44 -0800434 private void createGroupViews(List<List<MessagingMessage>> groups,
Kenny Guya0f6de82018-04-06 16:20:16 +0100435 List<Person> senders, boolean showSpinner) {
Selim Cinek88188f22017-09-19 16:46:56 -0700436 mGroups.clear();
437 for (int groupIndex = 0; groupIndex < groups.size(); groupIndex++) {
438 List<MessagingMessage> group = groups.get(groupIndex);
439 MessagingGroup newGroup = null;
440 // we'll just take the first group that exists or create one there is none
441 for (int messageIndex = group.size() - 1; messageIndex >= 0; messageIndex--) {
442 MessagingMessage message = group.get(messageIndex);
443 newGroup = message.getGroup();
444 if (newGroup != null) {
445 break;
446 }
447 }
448 if (newGroup == null) {
449 newGroup = MessagingGroup.createGroup(mMessagingLinearLayout);
Selim Cinekd3809992017-10-27 16:09:20 -0700450 mAddedGroups.add(newGroup);
Selim Cinek88188f22017-09-19 16:46:56 -0700451 }
Selim Cinek514b52c2020-03-17 18:42:12 -0700452 newGroup.setImageDisplayLocation(mDisplayImagesAtEnd
453 ? IMAGE_DISPLAY_LOCATION_AT_END
454 : IMAGE_DISPLAY_LOCATION_INLINE);
Selim Cineka91778a32020-03-13 17:30:34 -0700455 newGroup.setIsInConversation(false);
Selim Cinek88188f22017-09-19 16:46:56 -0700456 newGroup.setLayoutColor(mLayoutColor);
Kenny Guy14d035c2018-05-02 19:10:36 +0100457 newGroup.setTextColors(mSenderTextColor, mMessageTextColor);
Selim Cinek9acd6732018-03-23 16:39:02 -0700458 Person sender = senders.get(groupIndex);
Selim Cinek2dd3e722018-01-19 11:06:06 -0800459 CharSequence nameOverride = null;
460 if (sender != mUser && mNameReplacement != null) {
461 nameOverride = mNameReplacement;
462 }
463 newGroup.setSender(sender, nameOverride);
Kenny Guya0f6de82018-04-06 16:20:16 +0100464 newGroup.setSending(groupIndex == (groups.size() - 1) && showSpinner);
Selim Cinek88188f22017-09-19 16:46:56 -0700465 mGroups.add(newGroup);
466
467 if (mMessagingLinearLayout.indexOfChild(newGroup) != groupIndex) {
468 mMessagingLinearLayout.removeView(newGroup);
469 mMessagingLinearLayout.addView(newGroup, groupIndex);
470 }
471 newGroup.setMessages(group);
472 }
473 }
474
475 private void findGroups(List<MessagingMessage> historicMessages,
476 List<MessagingMessage> messages, List<List<MessagingMessage>> groups,
Selim Cinek9acd6732018-03-23 16:39:02 -0700477 List<Person> senders) {
Selim Cinekcb8b9852017-12-15 18:01:52 -0800478 CharSequence currentSenderKey = null;
Selim Cinek88188f22017-09-19 16:46:56 -0700479 List<MessagingMessage> currentGroup = null;
480 int histSize = historicMessages.size();
481 for (int i = 0; i < histSize + messages.size(); i++) {
482 MessagingMessage message;
483 if (i < histSize) {
484 message = historicMessages.get(i);
485 } else {
486 message = messages.get(i - histSize);
487 }
488 boolean isNewGroup = currentGroup == null;
Selim Cinek9acd6732018-03-23 16:39:02 -0700489 Person sender = message.getMessage().getSenderPerson();
Selim Cinekafeed292017-12-12 17:32:44 -0800490 CharSequence key = sender == null ? null
491 : sender.getKey() == null ? sender.getName() : sender.getKey();
Selim Cinekcb8b9852017-12-15 18:01:52 -0800492 isNewGroup |= !TextUtils.equals(key, currentSenderKey);
Selim Cinek88188f22017-09-19 16:46:56 -0700493 if (isNewGroup) {
494 currentGroup = new ArrayList<>();
495 groups.add(currentGroup);
Selim Cinekafeed292017-12-12 17:32:44 -0800496 if (sender == null) {
497 sender = mUser;
498 }
499 senders.add(sender);
Selim Cinekcb8b9852017-12-15 18:01:52 -0800500 currentSenderKey = key;
Selim Cinek88188f22017-09-19 16:46:56 -0700501 }
502 currentGroup.add(message);
503 }
504 }
505
Selim Cinek88188f22017-09-19 16:46:56 -0700506 /**
507 * Creates new messages, reusing existing ones if they are available.
508 *
509 * @param newMessages the messages to parse.
510 */
511 private List<MessagingMessage> createMessages(
512 List<Notification.MessagingStyle.Message> newMessages, boolean historic) {
Ahan Wude396fa2018-05-08 20:42:24 +0800513 List<MessagingMessage> result = new ArrayList<>();
Selim Cinek88188f22017-09-19 16:46:56 -0700514 for (int i = 0; i < newMessages.size(); i++) {
515 Notification.MessagingStyle.Message m = newMessages.get(i);
516 MessagingMessage message = findAndRemoveMatchingMessage(m);
517 if (message == null) {
Ahan Wude396fa2018-05-08 20:42:24 +0800518 message = MessagingMessage.createMessage(this, m, mImageResolver);
Selim Cinek88188f22017-09-19 16:46:56 -0700519 }
520 message.setIsHistoric(historic);
521 result.add(message);
522 }
523 return result;
524 }
525
526 private MessagingMessage findAndRemoveMatchingMessage(Notification.MessagingStyle.Message m) {
527 for (int i = 0; i < mMessages.size(); i++) {
528 MessagingMessage existing = mMessages.get(i);
529 if (existing.sameAs(m)) {
530 mMessages.remove(i);
531 return existing;
532 }
533 }
534 for (int i = 0; i < mHistoricMessages.size(); i++) {
535 MessagingMessage existing = mHistoricMessages.get(i);
536 if (existing.sameAs(m)) {
537 mHistoricMessages.remove(i);
538 return existing;
539 }
540 }
541 return null;
542 }
543
544 public void showHistoricMessages(boolean show) {
545 mShowHistoricMessages = show;
546 updateHistoricMessageVisibility();
547 }
548
549 private void updateHistoricMessageVisibility() {
Selim Cinekf45ca9b2018-05-04 11:37:24 -0700550 int numHistoric = mHistoricMessages.size();
551 for (int i = 0; i < numHistoric; i++) {
Selim Cinek88188f22017-09-19 16:46:56 -0700552 MessagingMessage existing = mHistoricMessages.get(i);
553 existing.setVisibility(mShowHistoricMessages ? VISIBLE : GONE);
554 }
Selim Cinekf45ca9b2018-05-04 11:37:24 -0700555 int numGroups = mGroups.size();
556 for (int i = 0; i < numGroups; i++) {
557 MessagingGroup group = mGroups.get(i);
558 int visibleChildren = 0;
559 List<MessagingMessage> messages = group.getMessages();
560 int numGroupMessages = messages.size();
561 for (int j = 0; j < numGroupMessages; j++) {
562 MessagingMessage message = messages.get(j);
563 if (message.getVisibility() != GONE) {
564 visibleChildren++;
565 }
566 }
567 if (visibleChildren > 0 && group.getVisibility() == GONE) {
568 group.setVisibility(VISIBLE);
569 } else if (visibleChildren == 0 && group.getVisibility() != GONE) {
570 group.setVisibility(GONE);
571 }
572 }
Selim Cinek88188f22017-09-19 16:46:56 -0700573 }
574
Selim Cinekd3809992017-10-27 16:09:20 -0700575 @Override
576 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
577 super.onLayout(changed, left, top, right, bottom);
578 if (!mAddedGroups.isEmpty()) {
579 getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
580 @Override
581 public boolean onPreDraw() {
582 for (MessagingGroup group : mAddedGroups) {
583 if (!group.isShown()) {
584 continue;
585 }
586 MessagingPropertyAnimator.fadeIn(group.getAvatar());
Selim Cinek1397ea32018-01-16 17:34:52 -0800587 MessagingPropertyAnimator.fadeIn(group.getSenderView());
Selim Cinekd3809992017-10-27 16:09:20 -0700588 MessagingPropertyAnimator.startLocalTranslationFrom(group,
589 group.getHeight(), LINEAR_OUT_SLOW_IN);
590 }
591 mAddedGroups.clear();
592 getViewTreeObserver().removeOnPreDrawListener(this);
593 return true;
594 }
595 });
596 }
597 }
598
Selim Cinek88188f22017-09-19 16:46:56 -0700599 public MessagingLinearLayout getMessagingLinearLayout() {
600 return mMessagingLinearLayout;
601 }
Selim Cinek1d6b50e2017-10-27 16:10:57 -0700602
603 public ArrayList<MessagingGroup> getMessagingGroups() {
604 return mGroups;
605 }
Selim Cinek514b52c2020-03-17 18:42:12 -0700606
607 @Override
608 public void setMessagingClippingDisabled(boolean clippingDisabled) {
609 // Don't do anything, this is only used for the ConversationLayout
610 }
Selim Cinek88188f22017-09-19 16:46:56 -0700611}