mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-22 21:19:31 +01:00
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:
parent
2f73433170
commit
3b7ca4a31a
@ -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,67 +377,69 @@ 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
Person.Builder person =
|
|
||||||
new Person.Builder().setKey(signatureVerification.getUserEntity().getId() +
|
|
||||||
"@" + decryptedPushMessage.getNotificationUser().getId()).setName(EmojiCompat.get().process(decryptedPushMessage.getNotificationUser().getName())).setBot(decryptedPushMessage.getNotificationUser().getType().equals("bot"));
|
|
||||||
|
|
||||||
notificationBuilder.setOnlyAlertOnce(true);
|
|
||||||
addReplyAction(notificationBuilder, notificationId);
|
|
||||||
|
|
||||||
if (decryptedPushMessage.getNotificationUser().getType().equals("user") || decryptedPushMessage.getNotificationUser().getType().equals("guest")) {
|
|
||||||
String avatarUrl = ApiUtils.getUrlForAvatar(signatureVerification.getUserEntity().getBaseUrl(),
|
|
||||||
decryptedPushMessage.getNotificationUser().getId(), false);
|
|
||||||
|
|
||||||
if (decryptedPushMessage.getNotificationUser().getType().equals("guest")) {
|
|
||||||
avatarUrl = ApiUtils.getUrlForGuestAvatar(signatureVerification.getUserEntity().getBaseUrl(),
|
|
||||||
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() {
|
|
||||||
@Override
|
|
||||||
protected void onNewResultImpl(Bitmap bitmap) {
|
|
||||||
if (bitmap != null) {
|
|
||||||
new RoundAsCirclePostprocessor(true).process(bitmap);
|
|
||||||
person.setIcon(IconCompat.createWithBitmap(bitmap));
|
|
||||||
notificationBuilder.setStyle(getStyle(person.build(),
|
|
||||||
finalStyle));
|
|
||||||
sendNotificationWithId(notificationId, notificationBuilder.build());
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
|
|
||||||
notificationBuilder.setStyle(getStyle(person.build(), finalStyle));
|
|
||||||
sendNotificationWithId(notificationId, notificationBuilder.build());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
UiThreadImmediateExecutorService.getInstance());
|
|
||||||
} else {
|
|
||||||
notificationBuilder.setStyle(getStyle(person.build(), style));
|
|
||||||
sendNotificationWithId(notificationId, notificationBuilder.build());
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
sendNotificationWithId(notificationId, notificationBuilder.build());
|
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 =
|
||||||
|
new Person.Builder()
|
||||||
|
.setKey(signatureVerification.getUserEntity().getId() + "@" + notificationUser.getId())
|
||||||
|
.setName(EmojiCompat.get().process(notificationUser.getName()))
|
||||||
|
.setBot("bot".equals(userType));
|
||||||
|
|
||||||
|
notificationBuilder.setOnlyAlertOnce(true);
|
||||||
|
addReplyAction(notificationBuilder, notificationId);
|
||||||
|
|
||||||
|
if ("user".equals(userType) || "guest".equals(userType)) {
|
||||||
|
String baseUrl = signatureVerification.getUserEntity().getBaseUrl();
|
||||||
|
String avatarUrl = "user".equals(userType) ?
|
||||||
|
ApiUtils.getUrlForAvatar(baseUrl, notificationUser.getId(), false) :
|
||||||
|
ApiUtils.getUrlForGuestAvatar(baseUrl, notificationUser.getName(), false);
|
||||||
|
|
||||||
|
ImageRequest imageRequest = DisplayUtils.getImageRequestForUrl(avatarUrl, null);
|
||||||
|
Fresco.getImagePipeline().fetchDecodedImage(imageRequest, context).subscribe(
|
||||||
|
new BaseBitmapDataSubscriber() {
|
||||||
|
@Override
|
||||||
|
protected void onNewResultImpl(Bitmap bitmap) {
|
||||||
|
if (bitmap != null) {
|
||||||
|
new RoundAsCirclePostprocessor(true).process(bitmap);
|
||||||
|
person.setIcon(IconCompat.createWithBitmap(bitmap));
|
||||||
|
notificationBuilder.setStyle(getStyle(person.build(), style));
|
||||||
|
sendNotificationWithId(notificationId, notificationBuilder.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
|
||||||
|
notificationBuilder.setStyle(getStyle(person.build(), style));
|
||||||
|
sendNotificationWithId(notificationId, notificationBuilder.build());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
UiThreadImmediateExecutorService.getInstance());
|
||||||
|
} else {
|
||||||
|
notificationBuilder.setStyle(getStyle(person.build(), style));
|
||||||
|
sendNotificationWithId(notificationId, notificationBuilder.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addReplyAction(NotificationCompat.Builder notificationBuilder, int notificationId) {
|
private void addReplyAction(NotificationCompat.Builder notificationBuilder, int notificationId) {
|
||||||
String replyLabel = context.getResources().getString(R.string.nc_reply);
|
String replyLabel = context.getResources().getString(R.string.nc_reply);
|
||||||
|
|
||||||
@ -469,24 +472,19 @@ 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);
|
|
||||||
return newStyle;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we'll never come here
|
newStyle.addMessage(decryptedPushMessage.getText(), decryptedPushMessage.getTimestamp(), person);
|
||||||
return style;
|
return newStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendNotificationWithId(int notificationId, Notification notification) {
|
private void sendNotificationWithId(int notificationId, Notification notification) {
|
||||||
|
Loading…
Reference in New Issue
Block a user