From fbc8112104d1e7f275ac9ecc737132f72048e334 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 27 Mar 2021 18:54:55 +0100 Subject: [PATCH] Immutable pending intents --- .../java/eu/faircode/email/ActivityView.java | 2 +- app/src/main/java/eu/faircode/email/Core.java | 46 ++++++++++++------- .../java/eu/faircode/email/EntityMessage.java | 3 +- .../faircode/email/PendingIntentCompat.java | 18 +++++++- .../java/eu/faircode/email/ServiceSend.java | 3 +- .../eu/faircode/email/ServiceSynchronize.java | 6 ++- .../java/eu/faircode/email/ServiceUI.java | 8 ++-- .../main/java/eu/faircode/email/Widget.java | 9 ++-- .../java/eu/faircode/email/WidgetSync.java | 3 +- .../java/eu/faircode/email/WidgetUnified.java | 5 +- 10 files changed, 72 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index c040456835..309dff17a1 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -968,7 +968,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB Intent update = new Intent(Intent.ACTION_VIEW, Uri.parse(info.html_url)); update.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent piUpdate = PendingIntent.getActivity( + PendingIntent piUpdate = PendingIntentCompat.getActivity( ActivityView.this, REQUEST_UPDATE, update, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(piUpdate); diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index fe4ce51144..9631e6e0b8 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -1707,7 +1707,7 @@ class Core { Intent intent = new Intent(context, ServiceUI.class); intent.setAction("exists:" + message.id); - PendingIntent piExists = PendingIntent.getService( + PendingIntent piExists = PendingIntentCompat.getService( context, ServiceUI.PI_EXISTS, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); @@ -4084,10 +4084,12 @@ class Core { content = new Intent(context, ActivityView.class) .setAction("unified" + (notify_remove ? ":" + group : "")); content.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - PendingIntent piContent = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, content, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piContent = PendingIntentCompat.getActivity( + context, ActivityView.REQUEST_UNIFIED, content, PendingIntent.FLAG_UPDATE_CURRENT); Intent clear = new Intent(context, ServiceUI.class).setAction("clear:" + group); - PendingIntent piClear = PendingIntent.getService(context, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piClear = PendingIntentCompat.getService( + context, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT); // Build title String title = context.getResources().getQuantityString( @@ -4196,7 +4198,8 @@ class Core { Intent thread = new Intent(context, ServiceUI.class); thread.setAction("ignore:" + message.id); thread.putExtra("view", true); - piContent = PendingIntent.getService(context, ServiceUI.PI_THREAD, thread, PendingIntent.FLAG_UPDATE_CURRENT); + piContent = PendingIntentCompat.getService( + context, ServiceUI.PI_THREAD, thread, PendingIntent.FLAG_UPDATE_CURRENT); } else { Intent thread = new Intent(context, ActivityView.class); thread.setAction("thread:" + message.id); @@ -4205,11 +4208,13 @@ class Core { thread.putExtra("folder", message.folder); thread.putExtra("thread", message.thread); thread.putExtra("filter_archive", !EntityFolder.ARCHIVE.equals(message.folderType)); - piContent = PendingIntent.getActivity(context, ActivityView.REQUEST_THREAD, thread, PendingIntent.FLAG_UPDATE_CURRENT); + piContent = PendingIntentCompat.getActivity( + context, ActivityView.REQUEST_THREAD, thread, PendingIntent.FLAG_UPDATE_CURRENT); } Intent ignore = new Intent(context, ServiceUI.class).setAction("ignore:" + message.id); - PendingIntent piIgnore = PendingIntent.getService(context, ServiceUI.PI_IGNORED, ignore, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piIgnore = PendingIntentCompat.getService( + context, ServiceUI.PI_IGNORED, ignore, PendingIntent.FLAG_UPDATE_CURRENT); // Get channel name String channelName = EntityAccount.getNotificationChannelId(0); @@ -4313,7 +4318,8 @@ class Core { Intent trash = new Intent(context, ServiceUI.class) .setAction("trash:" + message.id) .putExtra("group", group); - PendingIntent piTrash = PendingIntent.getService(context, ServiceUI.PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piTrash = PendingIntentCompat.getService( + context, ServiceUI.PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionTrash = new NotificationCompat.Action.Builder( R.drawable.twotone_delete_24, context.getString(R.string.title_advanced_notify_action_trash), @@ -4332,7 +4338,8 @@ class Core { Intent junk = new Intent(context, ServiceUI.class) .setAction("junk:" + message.id) .putExtra("group", group); - PendingIntent piJunk = PendingIntent.getService(context, ServiceUI.PI_JUNK, junk, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piJunk = PendingIntentCompat.getService( + context, ServiceUI.PI_JUNK, junk, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionJunk = new NotificationCompat.Action.Builder( R.drawable.twotone_report_problem_24, context.getString(R.string.title_advanced_notify_action_junk), @@ -4350,7 +4357,8 @@ class Core { Intent archive = new Intent(context, ServiceUI.class) .setAction("archive:" + message.id) .putExtra("group", group); - PendingIntent piArchive = PendingIntent.getService(context, ServiceUI.PI_ARCHIVE, archive, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piArchive = PendingIntentCompat.getService( + context, ServiceUI.PI_ARCHIVE, archive, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionArchive = new NotificationCompat.Action.Builder( R.drawable.twotone_archive_24, context.getString(R.string.title_advanced_notify_action_archive), @@ -4372,7 +4380,8 @@ class Core { Intent move = new Intent(context, ServiceUI.class) .setAction("move:" + message.id) .putExtra("group", group); - PendingIntent piMove = PendingIntent.getService(context, ServiceUI.PI_MOVE, move, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piMove = PendingIntentCompat.getService( + context, ServiceUI.PI_MOVE, move, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionMove = new NotificationCompat.Action.Builder( R.drawable.twotone_folder_24, folder.getDisplayName(context), @@ -4394,7 +4403,8 @@ class Core { .putExtra("reference", message.id) .putExtra("group", group); reply.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent piReply = PendingIntent.getActivity(context, ActivityCompose.PI_REPLY, reply, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piReply = PendingIntentCompat.getActivity( + context, ActivityCompose.PI_REPLY, reply, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionReply = new NotificationCompat.Action.Builder( R.drawable.twotone_reply_24, context.getString(R.string.title_advanced_notify_action_reply), @@ -4414,7 +4424,8 @@ class Core { Intent reply = new Intent(context, ServiceUI.class) .setAction("reply:" + message.id) .putExtra("group", group); - PendingIntent piReply = PendingIntent.getService(context, ServiceUI.PI_REPLY_DIRECT, reply, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piReply = PendingIntentCompat.getService( + context, ServiceUI.PI_REPLY_DIRECT, reply, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionReply = new NotificationCompat.Action.Builder( R.drawable.twotone_reply_24, context.getString(R.string.title_advanced_notify_action_reply_direct), @@ -4433,7 +4444,8 @@ class Core { Intent flag = new Intent(context, ServiceUI.class) .setAction("flag:" + message.id) .putExtra("group", group); - PendingIntent piFlag = PendingIntent.getService(context, ServiceUI.PI_FLAG, flag, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piFlag = PendingIntentCompat.getService( + context, ServiceUI.PI_FLAG, flag, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionFlag = new NotificationCompat.Action.Builder( R.drawable.baseline_star_24, context.getString(R.string.title_advanced_notify_action_flag), @@ -4450,7 +4462,8 @@ class Core { Intent seen = new Intent(context, ServiceUI.class) .setAction("seen:" + message.id) .putExtra("group", group); - PendingIntent piSeen = PendingIntent.getService(context, ServiceUI.PI_SEEN, seen, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piSeen = PendingIntentCompat.getService( + context, ServiceUI.PI_SEEN, seen, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionSeen = new NotificationCompat.Action.Builder( R.drawable.twotone_visibility_24, context.getString(R.string.title_advanced_notify_action_seen), @@ -4467,7 +4480,8 @@ class Core { Intent snooze = new Intent(context, ServiceUI.class) .setAction("snooze:" + message.id) .putExtra("group", group); - PendingIntent piSnooze = PendingIntent.getService(context, ServiceUI.PI_SNOOZE, snooze, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piSnooze = PendingIntentCompat.getService( + context, ServiceUI.PI_SNOOZE, snooze, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder actionSnooze = new NotificationCompat.Action.Builder( R.drawable.twotone_timelapse_24, context.getString(R.string.title_advanced_notify_action_snooze), @@ -4604,7 +4618,7 @@ class Core { Intent intent = new Intent(context, ActivityView.class); intent.setAction("error"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent pi = PendingIntent.getActivity( + PendingIntent pi = PendingIntentCompat.getActivity( context, ActivityView.REQUEST_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Build notification diff --git a/app/src/main/java/eu/faircode/email/EntityMessage.java b/app/src/main/java/eu/faircode/email/EntityMessage.java index 60118531ce..ecde085fcd 100644 --- a/app/src/main/java/eu/faircode/email/EntityMessage.java +++ b/app/src/main/java/eu/faircode/email/EntityMessage.java @@ -454,7 +454,8 @@ public class EntityMessage implements Serializable { static void snooze(Context context, long id, Long wakeup) { Intent snoozed = new Intent(context, ServiceUI.class); snoozed.setAction("wakeup:" + id); - PendingIntent pi = PendingIntent.getService(context, ServiceUI.PI_WAKEUP, snoozed, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pi = PendingIntentCompat.getService( + context, ServiceUI.PI_WAKEUP, snoozed, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); if (wakeup == null || wakeup == Long.MAX_VALUE) { diff --git a/app/src/main/java/eu/faircode/email/PendingIntentCompat.java b/app/src/main/java/eu/faircode/email/PendingIntentCompat.java index a721d89ceb..5f6c45a40c 100644 --- a/app/src/main/java/eu/faircode/email/PendingIntentCompat.java +++ b/app/src/main/java/eu/faircode/email/PendingIntentCompat.java @@ -30,10 +30,26 @@ public class PendingIntentCompat { private PendingIntentCompat() { } + public static int FLAG_MUTABLE = 0x02000000; + + public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + return PendingIntent.getActivity(context, requestCode, intent, flags); + else + return PendingIntent.getActivity(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE); + } + + public static PendingIntent getService(Context context, int requestCode, @NonNull Intent intent, int flags) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + return PendingIntent.getService(context, requestCode, intent, flags); + else + return PendingIntent.getService(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE); + } + static PendingIntent getForegroundService(Context context, int requestCode, @NonNull Intent intent, int flags) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return PendingIntent.getService(context, requestCode, intent, flags); else - return PendingIntent.getForegroundService(context, requestCode, intent, flags); + return PendingIntent.getForegroundService(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE); } } diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index d6138b7f14..f8873d779a 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -250,7 +250,8 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar Intent intent = new Intent(context, ActivityView.class); intent.setAction("outbox"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return PendingIntent.getActivity(context, ActivityView.REQUEST_OUTBOX, intent, PendingIntent.FLAG_UPDATE_CURRENT); + return PendingIntentCompat.getActivity( + context, ActivityView.REQUEST_OUTBOX, intent, PendingIntent.FLAG_UPDATE_CURRENT); } ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 2b07ef1fab..06459be34b 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -893,7 +893,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences Intent why = new Intent(this, ActivityView.class); why.setAction("why"); why.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent piWhy = PendingIntent.getActivity(this, ActivityView.REQUEST_WHY, why, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piWhy = PendingIntentCompat.getActivity( + this, ActivityView.REQUEST_WHY, why, PendingIntent.FLAG_UPDATE_CURRENT); // Build notification NotificationCompat.Builder builder = @@ -929,7 +930,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences Intent alert = new Intent(this, ActivityView.class); alert.setAction("alert"); alert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent piAlert = PendingIntent.getActivity(this, ActivityView.REQUEST_ALERT, alert, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent piAlert = PendingIntentCompat.getActivity( + this, ActivityView.REQUEST_ALERT, alert, PendingIntent.FLAG_UPDATE_CURRENT); // Build notification NotificationCompat.Builder builder = diff --git a/app/src/main/java/eu/faircode/email/ServiceUI.java b/app/src/main/java/eu/faircode/email/ServiceUI.java index 44a84166c4..a276ee70c0 100644 --- a/app/src/main/java/eu/faircode/email/ServiceUI.java +++ b/app/src/main/java/eu/faircode/email/ServiceUI.java @@ -608,7 +608,7 @@ public class ServiceUI extends IntentService { Intent intent = new Intent(context, ServiceUI.class); intent.setAction("sync"); intent.putExtra("reschedule", true); - PendingIntent piSync = PendingIntent.getService( + PendingIntent piSync = PendingIntentCompat.getService( context, ServiceUI.PI_SYNC, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); @@ -636,7 +636,8 @@ public class ServiceUI extends IntentService { private static PendingIntent getBannerIntent(Context context) { Intent intent = new Intent(context, ServiceUI.class); intent.setAction("banner"); - return PendingIntent.getService(context, ServiceUI.PI_BANNER, intent, PendingIntent.FLAG_UPDATE_CURRENT); + return PendingIntentCompat.getService( + context, ServiceUI.PI_BANNER, intent, PendingIntent.FLAG_UPDATE_CURRENT); } static void scheduleBanner(Context context, boolean set) { @@ -660,7 +661,8 @@ public class ServiceUI extends IntentService { private static PendingIntent getProtocolIntent(Context context) { Intent intent = new Intent(context, ServiceUI.class); intent.setAction("protocol"); - return PendingIntent.getService(context, ServiceUI.PI_PROTOCOL, intent, PendingIntent.FLAG_UPDATE_CURRENT); + return PendingIntentCompat.getService( + context, ServiceUI.PI_PROTOCOL, intent, PendingIntent.FLAG_UPDATE_CURRENT); } static void scheduleProtocol(Context context, boolean set) { diff --git a/app/src/main/java/eu/faircode/email/Widget.java b/app/src/main/java/eu/faircode/email/Widget.java index b8a084342e..9b1bd9471e 100644 --- a/app/src/main/java/eu/faircode/email/Widget.java +++ b/app/src/main/java/eu/faircode/email/Widget.java @@ -73,20 +73,23 @@ public class Widget extends AppWidgetProvider { view.putExtra("type", folders.get(0).type); view.putExtra("refresh", true); view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - pi = PendingIntent.getActivity(context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); + pi = PendingIntentCompat.getActivity( + context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); } else { if (account < 0) { Intent view = new Intent(context, ActivityView.class); view.setAction("unified"); view.putExtra("refresh", true); view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - pi = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, view, PendingIntent.FLAG_UPDATE_CURRENT); + pi = PendingIntentCompat.getActivity( + context, ActivityView.REQUEST_UNIFIED, view, PendingIntent.FLAG_UPDATE_CURRENT); } else { Intent view = new Intent(context, ActivityView.class); view.setAction("folders:" + account); view.putExtra("refresh", true); view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - pi = PendingIntent.getActivity(context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); + pi = PendingIntentCompat.getActivity( + context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); } } diff --git a/app/src/main/java/eu/faircode/email/WidgetSync.java b/app/src/main/java/eu/faircode/email/WidgetSync.java index 313b28dca5..aca89ca28c 100644 --- a/app/src/main/java/eu/faircode/email/WidgetSync.java +++ b/app/src/main/java/eu/faircode/email/WidgetSync.java @@ -40,7 +40,8 @@ public class WidgetSync extends AppWidgetProvider { try { Intent intent = new Intent(enabled ? ServiceExternal.ACTION_DISABLE : ServiceExternal.ACTION_ENABLE); - PendingIntent pi = PendingIntent.getService(context, ServiceExternal.PI_WIDGET_ENABLE, intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pi = PendingIntentCompat.getService( + context, ServiceExternal.PI_WIDGET_ENABLE, intent, PendingIntent.FLAG_UPDATE_CURRENT); for (int appWidgetId : appWidgetIds) { boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); diff --git a/app/src/main/java/eu/faircode/email/WidgetUnified.java b/app/src/main/java/eu/faircode/email/WidgetUnified.java index 4a2408510f..9c7f3d3907 100644 --- a/app/src/main/java/eu/faircode/email/WidgetUnified.java +++ b/app/src/main/java/eu/faircode/email/WidgetUnified.java @@ -55,7 +55,8 @@ public class WidgetUnified extends AppWidgetProvider { view.putExtra("type", type); view.putExtra("refresh", true); view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - PendingIntent pi = PendingIntent.getActivity(context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pi = PendingIntentCompat.getActivity( + context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_unified); @@ -88,7 +89,7 @@ public class WidgetUnified extends AppWidgetProvider { thread.putExtra("filter_archive", !EntityFolder.ARCHIVE.equals(type)); thread.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent piItem = PendingIntent.getActivity( - context, ActivityView.REQUEST_WIDGET, thread, PendingIntent.FLAG_UPDATE_CURRENT); + context, ActivityView.REQUEST_WIDGET, thread, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntentCompat.FLAG_MUTABLE); views.setPendingIntentTemplate(R.id.lv, piItem);