Refactor NotificationWorker - extracted sendChatNotification method

plus some minor modifications, hopefully ;-) without changing behaviour

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
This commit is contained in:
Dariusz Olszewski 2022-05-07 00:07:08 +02:00 committed by Andy Scherzinger
parent 2f73433170
commit 3b7ca4a31a
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B

View File

@ -41,7 +41,6 @@ import com.facebook.common.executors.UiThreadImmediateExecutorService;
import com.facebook.common.references.CloseableReference; import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource; import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber; import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage; import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.postprocessors.RoundAsCirclePostprocessor; import com.facebook.imagepipeline.postprocessors.RoundAsCirclePostprocessor;
@ -88,7 +87,9 @@ import javax.inject.Inject;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.MessagingStyle;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person; import androidx.core.app.Person;
import androidx.core.app.RemoteInput; import androidx.core.app.RemoteInput;
@ -376,53 +377,59 @@ public class NotificationWorker extends Worker {
notificationId = (int) crc32.getValue(); notificationId = (int) crc32.getValue();
} }
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N && decryptedPushMessage.getNotificationUser() != null && decryptedPushMessage.getType().equals("chat")) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
NotificationCompat.MessagingStyle style = null; CHAT.equals(decryptedPushMessage.getType()) &&
if (activeStatusBarNotification != null) { decryptedPushMessage.getNotificationUser() != null) {
Notification activeNotification = activeStatusBarNotification.getNotification(); sendChatNotification(notificationBuilder, activeStatusBarNotification, notificationId);
style = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(activeNotification); } else {
sendNotificationWithId(notificationId, notificationBuilder.build());
} }
}
@RequiresApi(api = Build.VERSION_CODES.N)
private void sendChatNotification(NotificationCompat.Builder notificationBuilder,
StatusBarNotification activeStatusBarNotification,
int notificationId) {
final NotificationUser notificationUser = decryptedPushMessage.getNotificationUser();
final String userType = notificationUser.getType();
MessagingStyle style = activeStatusBarNotification != null ?
MessagingStyle.extractMessagingStyleFromNotification(activeStatusBarNotification.getNotification()) :
null;
Person.Builder person = Person.Builder person =
new Person.Builder().setKey(signatureVerification.getUserEntity().getId() + new Person.Builder()
"@" + decryptedPushMessage.getNotificationUser().getId()).setName(EmojiCompat.get().process(decryptedPushMessage.getNotificationUser().getName())).setBot(decryptedPushMessage.getNotificationUser().getType().equals("bot")); .setKey(signatureVerification.getUserEntity().getId() + "@" + notificationUser.getId())
.setName(EmojiCompat.get().process(notificationUser.getName()))
.setBot("bot".equals(userType));
notificationBuilder.setOnlyAlertOnce(true); notificationBuilder.setOnlyAlertOnce(true);
addReplyAction(notificationBuilder, notificationId); addReplyAction(notificationBuilder, notificationId);
if (decryptedPushMessage.getNotificationUser().getType().equals("user") || decryptedPushMessage.getNotificationUser().getType().equals("guest")) { if ("user".equals(userType) || "guest".equals(userType)) {
String avatarUrl = ApiUtils.getUrlForAvatar(signatureVerification.getUserEntity().getBaseUrl(), String baseUrl = signatureVerification.getUserEntity().getBaseUrl();
decryptedPushMessage.getNotificationUser().getId(), false); String avatarUrl = "user".equals(userType) ?
ApiUtils.getUrlForAvatar(baseUrl, notificationUser.getId(), false) :
ApiUtils.getUrlForGuestAvatar(baseUrl, notificationUser.getName(), false);
if (decryptedPushMessage.getNotificationUser().getType().equals("guest")) { ImageRequest imageRequest = DisplayUtils.getImageRequestForUrl(avatarUrl, null);
avatarUrl = ApiUtils.getUrlForGuestAvatar(signatureVerification.getUserEntity().getBaseUrl(), Fresco.getImagePipeline().fetchDecodedImage(imageRequest, context).subscribe(
decryptedPushMessage.getNotificationUser().getName(),
false);
}
ImageRequest imageRequest =
DisplayUtils.getImageRequestForUrl(avatarUrl, null);
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);
NotificationCompat.MessagingStyle finalStyle = style;
dataSource.subscribe(
new BaseBitmapDataSubscriber() { new BaseBitmapDataSubscriber() {
@Override @Override
protected void onNewResultImpl(Bitmap bitmap) { protected void onNewResultImpl(Bitmap bitmap) {
if (bitmap != null) { if (bitmap != null) {
new RoundAsCirclePostprocessor(true).process(bitmap); new RoundAsCirclePostprocessor(true).process(bitmap);
person.setIcon(IconCompat.createWithBitmap(bitmap)); person.setIcon(IconCompat.createWithBitmap(bitmap));
notificationBuilder.setStyle(getStyle(person.build(), notificationBuilder.setStyle(getStyle(person.build(), style));
finalStyle));
sendNotificationWithId(notificationId, notificationBuilder.build()); sendNotificationWithId(notificationId, notificationBuilder.build());
} }
} }
@Override @Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) { protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
notificationBuilder.setStyle(getStyle(person.build(), finalStyle)); notificationBuilder.setStyle(getStyle(person.build(), style));
sendNotificationWithId(notificationId, notificationBuilder.build()); sendNotificationWithId(notificationId, notificationBuilder.build());
} }
}, },
@ -431,10 +438,6 @@ public class NotificationWorker extends Worker {
notificationBuilder.setStyle(getStyle(person.build(), style)); notificationBuilder.setStyle(getStyle(person.build(), style));
sendNotificationWithId(notificationId, notificationBuilder.build()); sendNotificationWithId(notificationId, notificationBuilder.build());
} }
} else {
sendNotificationWithId(notificationId, notificationBuilder.build());
}
} }
private void addReplyAction(NotificationCompat.Builder notificationBuilder, int notificationId) { private void addReplyAction(NotificationCompat.Builder notificationBuilder, int notificationId) {
@ -469,26 +472,21 @@ public class NotificationWorker extends Worker {
notificationBuilder.addAction(replyAction); notificationBuilder.addAction(replyAction);
} }
private NotificationCompat.MessagingStyle getStyle(Person person, @Nullable NotificationCompat.MessagingStyle style) { @RequiresApi(api = Build.VERSION_CODES.N)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { private MessagingStyle getStyle(Person person, @Nullable MessagingStyle style) {
NotificationCompat.MessagingStyle newStyle = MessagingStyle newStyle = new MessagingStyle(person);
new NotificationCompat.MessagingStyle(person);
newStyle.setConversationTitle(decryptedPushMessage.getSubject()); newStyle.setConversationTitle(decryptedPushMessage.getSubject());
newStyle.setGroupConversation(!conversationType.equals("one2one")); newStyle.setGroupConversation(!conversationType.equals("one2one"));
if (style != null) { if (style != null) {
style.getMessages().forEach(message -> newStyle.addMessage(new NotificationCompat.MessagingStyle.Message(message.getText(), message.getTimestamp(), message.getPerson()))); style.getMessages().forEach(message -> newStyle.addMessage(new MessagingStyle.Message(message.getText(), message.getTimestamp(), message.getPerson())));
} }
newStyle.addMessage(decryptedPushMessage.getText(), decryptedPushMessage.getTimestamp(), person); newStyle.addMessage(decryptedPushMessage.getText(), decryptedPushMessage.getTimestamp(), person);
return newStyle; return newStyle;
} }
// we'll never come here
return style;
}
private void sendNotificationWithId(int notificationId, Notification notification) { private void sendNotificationWithId(int notificationId, Notification notification) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId, notification); notificationManager.notify(notificationId, notification);