Skip to content

Commit adc446b

Browse files
authored
Merge pull request #16090 from wordpress-mobile/issue/15875-remove-notif-trampoline
Android 12: Performance - Remove notification trampoline
2 parents b81908b + 0a43a35 commit adc446b

File tree

6 files changed

+72
-110
lines changed

6 files changed

+72
-110
lines changed

WordPress/src/main/AndroidManifest.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
<uses-sdk tools:overrideLibrary="org.m4m.android" />
88

99
<!-- Normal permissions, access automatically granted to app -->
10-
<uses-permission
11-
android:name="android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS"
12-
tools:ignore="ProtectedPermissions" />
1310
<uses-permission android:name="android.permission.VIBRATE" />
1411
<uses-permission android:name="android.permission.INTERNET" />
1512
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
@@ -884,7 +881,6 @@
884881
</intent-filter>
885882
</receiver>
886883

887-
<receiver android:name=".ui.notifications.ShareAndDismissNotificationReceiver" />
888884
<receiver
889885
android:name=".ui.stats.refresh.lists.widget.views.StatsViewsWidget"
890886
android:label="@string/stats_widget_weekly_views_name"

WordPress/src/main/java/org/wordpress/android/push/GCMMessageHandler.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ private void buildAndShowNotificationFromNoteData(Context context, Bundle data)
351351
// Try to build the note object from the PN payload, and save it to the DB.
352352
NotificationsUtils.buildNoteObjectFromBundleAndSaveIt(data);
353353
EventBus.getDefault().post(new NotificationEvents.NotificationsChanged(true));
354-
// Always do this, since a note can be updated on the server after a PN is sent
355-
NotificationsActions.downloadNoteAndUpdateDB(wpcomNoteID, null, null);
356354

357355
String noteType = StringUtils.notNullStr(data.getString(PUSH_ARG_TYPE));
358356

@@ -388,23 +386,8 @@ private void buildAndShowNotificationFromNoteData(Context context, Bundle data)
388386
AppPrefs.setLastPushNotificationWpcomNoteId(wpcomNoteID);
389387

390388
// Update notification content for the same noteId if it is already showing
391-
int pushId = 0;
392-
for (Integer id : mGCMMessageHandler.mActiveNotificationsMap.keySet()) {
393-
if (id == null) {
394-
continue;
395-
}
396-
Bundle noteBundle = mGCMMessageHandler.mActiveNotificationsMap.get(id);
397-
if (noteBundle != null && noteBundle.getString(PUSH_ARG_NOTE_ID, "").equals(wpcomNoteID)) {
398-
pushId = id;
399-
mGCMMessageHandler.mActiveNotificationsMap.put(pushId, data);
400-
break;
401-
}
402-
}
403-
404-
if (pushId == 0) {
405-
pushId = PUSH_NOTIFICATION_ID + mGCMMessageHandler.mActiveNotificationsMap.size();
406-
mGCMMessageHandler.mActiveNotificationsMap.put(pushId, data);
407-
}
389+
int pushId = getPushIdForWpcomeNoteID(wpcomNoteID);
390+
mGCMMessageHandler.mActiveNotificationsMap.put(pushId, data);
408391

409392
// Bump Analytics for PNs if "Show notifications" setting is checked (default). Skip otherwise.
410393
if (NotificationsUtils.isNotificationsEnabled(context)) {
@@ -430,13 +413,46 @@ private void buildAndShowNotificationFromNoteData(Context context, Bundle data)
430413
builder.setLargeIcon(largeIconBitmap);
431414
}
432415

416+
// Always do this, since a note can be updated on the server after a PN is sent
417+
NotificationsActions.downloadNoteAndUpdateDB(
418+
wpcomNoteID,
419+
success -> showNotificationForNoteData(context, data, builder),
420+
error -> showNotificationForNoteData(context, data, builder)
421+
);
422+
}
423+
424+
private void showNotificationForNoteData(Context context, Bundle noteData, NotificationCompat.Builder builder) {
425+
String noteType = StringUtils.notNullStr(noteData.getString(PUSH_ARG_TYPE));
426+
String wpcomNoteID = noteData.getString(PUSH_ARG_NOTE_ID, "");
427+
String message = StringEscapeUtils.unescapeHtml4(noteData.getString(PUSH_ARG_MSG));
428+
int pushId = getPushIdForWpcomeNoteID(wpcomNoteID);
429+
433430
showSingleNotificationForBuilder(context, builder, noteType, wpcomNoteID, pushId, true);
434431

435432
// Also add a group summary notification, which is required for non-wearable devices
436433
// Do not need to play the sound again. We've already played it in the individual builder.
437434
showGroupNotificationForBuilder(context, builder, wpcomNoteID, message);
438435
}
439436

437+
private int getPushIdForWpcomeNoteID(String wpcomNoteID) {
438+
int pushId = 0;
439+
for (Integer id : mGCMMessageHandler.mActiveNotificationsMap.keySet()) {
440+
if (id == null) {
441+
continue;
442+
}
443+
Bundle noteBundle = mGCMMessageHandler.mActiveNotificationsMap.get(id);
444+
if (noteBundle != null && noteBundle.getString(PUSH_ARG_NOTE_ID, "").equals(wpcomNoteID)) {
445+
pushId = id;
446+
break;
447+
}
448+
}
449+
450+
if (pushId == 0) {
451+
pushId = PUSH_NOTIFICATION_ID + mGCMMessageHandler.mActiveNotificationsMap.size();
452+
}
453+
return pushId;
454+
}
455+
440456
private void showSimpleNotification(Context context, String title, String message, Intent resultIntent,
441457
int pushId, NotificationType notificationType) {
442458
NotificationCompat.Builder builder = getNotificationBuilder(context, title, message);

WordPress/src/main/java/org/wordpress/android/push/NativeNotificationsUtils.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.wordpress.android.push;
22

33
import android.content.Context;
4-
import android.content.Intent;
54

65
import androidx.core.app.NotificationCompat;
76
import androidx.core.app.NotificationManagerCompat;
@@ -64,9 +63,4 @@ public static void dismissNotification(int pushId, Context context) {
6463
notificationManager.cancel(pushId);
6564
}
6665
}
67-
68-
public static void hideStatusBar(Context context) {
69-
Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
70-
context.sendBroadcast(closeIntent);
71-
}
7266
}

WordPress/src/main/java/org/wordpress/android/push/NotificationsProcessingService.java

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import org.wordpress.android.fluxc.store.SiteStore;
3737
import org.wordpress.android.models.Note;
3838
import org.wordpress.android.ui.comments.unified.CommentsStoreAdapter;
39-
import org.wordpress.android.ui.main.WPMainActivity;
40-
import org.wordpress.android.ui.notifications.NotificationsListFragment;
4139
import org.wordpress.android.ui.notifications.SystemNotificationsTracker;
4240
import org.wordpress.android.ui.notifications.receivers.NotificationsPendingDraftsReceiver;
4341
import org.wordpress.android.ui.notifications.utils.NotificationsActions;
@@ -612,49 +610,31 @@ private void replyToComment() {
612610
return;
613611
}
614612

615-
if (!TextUtils.isEmpty(mReplyText)) {
616-
SiteModel site = mSiteStore.getSiteBySiteId(mNote.getSiteId());
617-
if (site == null) {
618-
AppLog.e(T.NOTIFS, "Impossible to reply to a comment on a site that is not in the App."
619-
+ " SiteId: " + mNote.getSiteId());
620-
requestFailed(ARG_ACTION_APPROVE);
621-
return;
622-
}
613+
if (TextUtils.isEmpty(mReplyText)) return;
623614

624-
// Pseudo comment (built from the note)
625-
CommentModel comment = mNote.buildComment();
615+
SiteModel site = mSiteStore.getSiteBySiteId(mNote.getSiteId());
616+
if (site == null) {
617+
AppLog.e(T.NOTIFS, "Impossible to reply to a comment on a site that is not in the App."
618+
+ " SiteId: " + mNote.getSiteId());
619+
requestFailed(ARG_ACTION_APPROVE);
620+
return;
621+
}
626622

627-
// Pseudo comment reply
628-
CommentModel reply = new CommentModel();
629-
reply.setContent(mReplyText);
623+
// Pseudo comment (built from the note)
624+
CommentModel comment = mNote.buildComment();
630625

631-
// Push the reply
632-
RemoteCreateCommentPayload payload = new RemoteCreateCommentPayload(site, comment, reply);
633-
mCommentsStoreAdapter.dispatch(CommentActionBuilder.newCreateNewCommentAction(payload));
626+
// Pseudo comment reply
627+
CommentModel reply = new CommentModel();
628+
reply.setContent(mReplyText);
634629

635-
// Bump analytics
636-
AnalyticsUtils.trackCommentReplyWithDetails(true,
637-
site, comment, AnalyticsCommentActionSource.NOTIFICATIONS);
638-
AnalyticsUtils.trackQuickActionTouched(QuickActionTrackPropertyValue.REPLY_TO, site, comment);
639-
} else {
640-
// cancel the current notification
641-
NativeNotificationsUtils.dismissNotification(mPushId, mContext);
642-
NativeNotificationsUtils.hideStatusBar(mContext);
643-
// and just trigger the Activity to allow the user to write a reply
644-
startReplyToCommentActivity();
645-
}
646-
}
630+
// Push the reply
631+
RemoteCreateCommentPayload payload = new RemoteCreateCommentPayload(site, comment, reply);
632+
mCommentsStoreAdapter.dispatch(CommentActionBuilder.newCreateNewCommentAction(payload));
647633

648-
private void startReplyToCommentActivity() {
649-
Intent intent = new Intent(mContext, WPMainActivity.class);
650-
intent.putExtra(WPMainActivity.ARG_OPENED_FROM_PUSH, true);
651-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
652-
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
653-
intent.setAction("android.intent.action.MAIN");
654-
intent.addCategory("android.intent.category.LAUNCHER");
655-
intent.putExtra(NotificationsListFragment.NOTE_ID_EXTRA, mNoteId);
656-
intent.putExtra(NotificationsListFragment.NOTE_INSTANT_REPLY_EXTRA, true);
657-
startActivity(intent);
634+
// Bump analytics
635+
AnalyticsUtils.trackCommentReplyWithDetails(true,
636+
site, comment, AnalyticsCommentActionSource.NOTIFICATIONS);
637+
AnalyticsUtils.trackQuickActionTouched(QuickActionTrackPropertyValue.REPLY_TO, site, comment);
658638
}
659639

660640
private void resetOriginalNotification() {

WordPress/src/main/java/org/wordpress/android/ui/notifications/ShareAndDismissNotificationReceiver.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

WordPress/src/main/java/org/wordpress/android/ui/uploads/PostUploadNotifier.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.wordpress.android.ui.uploads;
22

3-
import android.annotation.SuppressLint;
43
import android.app.Notification;
54
import android.app.NotificationManager;
65
import android.app.PendingIntent;
@@ -12,6 +11,7 @@
1211
import androidx.annotation.Nullable;
1312
import androidx.collection.SparseArrayCompat;
1413
import androidx.core.app.NotificationCompat;
14+
import androidx.core.app.TaskStackBuilder;
1515

1616
import org.greenrobot.eventbus.EventBus;
1717
import org.wordpress.android.R;
@@ -25,7 +25,6 @@
2525
import org.wordpress.android.push.NotificationsProcessingService;
2626
import org.wordpress.android.ui.RequestCodes;
2727
import org.wordpress.android.ui.media.MediaBrowserActivity;
28-
import org.wordpress.android.ui.notifications.ShareAndDismissNotificationReceiver;
2928
import org.wordpress.android.ui.notifications.SystemNotificationsTracker;
3029
import org.wordpress.android.ui.pages.PagesActivity;
3130
import org.wordpress.android.ui.posts.EditPostActivity;
@@ -289,7 +288,6 @@ static void cancelFinalNotificationForMedia(Context context, @NonNull SiteModel
289288
}
290289
}
291290

292-
@SuppressLint("NotificationTrampoline")
293291
void updateNotificationSuccessForPost(@NonNull PostImmutableModel post, @NonNull SiteModel site,
294292
boolean isFirstTimePublish) {
295293
if (!WordPress.Companion.getAppIsInTheBackground()) {
@@ -366,14 +364,8 @@ void updateNotificationSuccessForPost(@NonNull PostImmutableModel post, @NonNull
366364

367365
// Share intent - started if the user tap the share link button - only if the link exist
368366
if (shareableUrl != null && PostStatus.fromPost(post) == PostStatus.PUBLISHED) {
369-
Intent shareIntent = new Intent(mContext, ShareAndDismissNotificationReceiver.class);
370-
shareIntent.putExtra(ShareAndDismissNotificationReceiver.NOTIFICATION_ID_KEY, notificationId);
371-
shareIntent.putExtra(Intent.EXTRA_TEXT, shareableUrl);
372-
shareIntent.putExtra(Intent.EXTRA_SUBJECT, post.getTitle());
373-
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, shareIntent,
374-
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
375367
notificationBuilder.addAction(R.drawable.ic_share_white_24dp, mContext.getString(R.string.share_action),
376-
pendingIntent);
368+
getSharePendingIntent(post, shareableUrl));
377369
}
378370

379371
// add draft Publish action for drafts
@@ -570,6 +562,21 @@ private Intent getNotificationIntent(@NonNull PostImmutableModel post, @NonNull
570562
return notificationIntent;
571563
}
572564

565+
@Nullable
566+
private PendingIntent getSharePendingIntent(@NonNull PostImmutableModel post, String shareableUrl) {
567+
Intent shareIntent = new Intent(Intent.ACTION_SEND);
568+
shareIntent.setType("text/plain");
569+
shareIntent.putExtra(Intent.EXTRA_TEXT, shareableUrl);
570+
shareIntent.putExtra(Intent.EXTRA_SUBJECT, post.getTitle());
571+
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
572+
573+
TaskStackBuilder builder = TaskStackBuilder.create(mContext);
574+
builder.addNextIntentWithParentStack(shareIntent);
575+
PendingIntent pendingIntent = builder
576+
.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
577+
return pendingIntent;
578+
}
579+
573580
void updateNotificationErrorForMedia(@NonNull List<MediaModel> mediaList, @NonNull SiteModel site,
574581
String errorMessage) {
575582
AppLog.d(AppLog.T.MEDIA, "updateNotificationErrorForMedia: " + errorMessage);

0 commit comments

Comments
 (0)