From 7f66558566d1764f974887e39cf2fadf5df35254 Mon Sep 17 00:00:00 2001 From: Unpublished Date: Fri, 22 Feb 2019 17:04:53 +0100 Subject: [PATCH] use NotificationCompat to get rid of some version checks --- .../eu/faircode/email/ServiceExternal.java | 12 +-- .../eu/faircode/email/ServiceSynchronize.java | 76 ++++++------------- 2 files changed, 29 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceExternal.java b/app/src/main/java/eu/faircode/email/ServiceExternal.java index cdb082804b..4be5b9a499 100644 --- a/app/src/main/java/eu/faircode/email/ServiceExternal.java +++ b/app/src/main/java/eu/faircode/email/ServiceExternal.java @@ -4,11 +4,11 @@ import android.app.Notification; import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; -import android.os.Build; import android.os.IBinder; import android.preference.PreferenceManager; import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; public class ServiceExternal extends Service { private final static String ACTION_ENABLE = "eu.faircode.email.ENABLE"; @@ -61,12 +61,8 @@ public class ServiceExternal extends Service { return null; } - private Notification.Builder getNotification() { - Notification.Builder builder; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - builder = new Notification.Builder(this, "service"); - else - builder = new Notification.Builder(this); + private NotificationCompat.Builder getNotification() { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service"); builder .setSmallIcon(R.drawable.baseline_compare_arrows_white_24) @@ -75,7 +71,7 @@ public class ServiceExternal extends Service { .setShowWhen(false) .setPriority(Notification.PRIORITY_MIN) .setCategory(Notification.CATEGORY_STATUS) - .setVisibility(Notification.VISIBILITY_SECRET); + .setVisibility(NotificationCompat.VISIBILITY_SECRET); return builder; } diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 33a83ebb2f..03cb3df1fb 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -23,7 +23,6 @@ import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; -import android.app.Person; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -132,6 +131,7 @@ import javax.net.ssl.SSLException; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; import androidx.core.content.ContextCompat; import androidx.lifecycle.LifecycleService; import androidx.lifecycle.LiveData; @@ -449,7 +449,7 @@ public class ServiceSynchronize extends LifecycleService { return START_STICKY; } - private Notification.Builder getNotificationService(TupleAccountStats stats) { + private NotificationCompat.Builder getNotificationService(TupleAccountStats stats) { if (stats == null) stats = lastStats; if (stats == null) @@ -461,11 +461,7 @@ public class ServiceSynchronize extends LifecycleService { PendingIntent pi = PendingIntent.getService(this, PI_WHY, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Build notification - Notification.Builder builder; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - builder = new Notification.Builder(this, "service"); - else - builder = new Notification.Builder(this); + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service"); builder .setSmallIcon(R.drawable.baseline_compare_arrows_white_24) @@ -476,10 +472,10 @@ public class ServiceSynchronize extends LifecycleService { .setShowWhen(false) .setPriority(Notification.PRIORITY_MIN) .setCategory(Notification.CATEGORY_STATUS) - .setVisibility(Notification.VISIBILITY_SECRET); + .setVisibility(NotificationCompat.VISIBILITY_SECRET); if (stats.operations > 0) - builder.setStyle(new Notification.BigTextStyle().setSummaryText( + builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText( getResources().getQuantityString( R.plurals.title_notification_operations, stats.operations, stats.operations))); @@ -524,11 +520,7 @@ public class ServiceSynchronize extends LifecycleService { String channelName = (account == 0 ? "notification" : EntityAccount.getNotificationChannelName(account)); // Build public notification - Notification.Builder pbuilder; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - pbuilder = new Notification.Builder(this); - else - pbuilder = new Notification.Builder(this, channelName); + NotificationCompat.Builder pbuilder = new NotificationCompat.Builder(this, channelName); pbuilder .setSmallIcon(R.drawable.baseline_email_white_24) @@ -539,17 +531,13 @@ public class ServiceSynchronize extends LifecycleService { .setDeleteIntent(piClear) .setPriority(Notification.PRIORITY_DEFAULT) .setCategory(Notification.CATEGORY_STATUS) - .setVisibility(Notification.VISIBILITY_PUBLIC); + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); if (!TextUtils.isEmpty(accountName)) pbuilder.setSubText(accountName); // Build notification - Notification.Builder builder; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - builder = new Notification.Builder(this); - else - builder = new Notification.Builder(this, channelName); + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelName); builder .setSmallIcon(R.drawable.baseline_email_white_24) @@ -583,7 +571,7 @@ public class ServiceSynchronize extends LifecycleService { builder.setOnlyAlertOnce(true); } else - builder.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN); + builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); if (pro) { DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT); @@ -596,7 +584,7 @@ public class ServiceSynchronize extends LifecycleService { sb.append("
"); } - builder.setStyle(new Notification.BigTextStyle() + builder.setStyle(new NotificationCompat.BigTextStyle() .bigText(HtmlHelper.fromHtml(sb.toString())) .setSummaryText(title)); } @@ -635,26 +623,23 @@ public class ServiceSynchronize extends LifecycleService { PendingIntent piTrash = PendingIntent.getService(this, PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT); - Notification.Action.Builder actionSeen = new Notification.Action.Builder( + NotificationCompat.Action.Builder actionSeen = new NotificationCompat.Action.Builder( R.drawable.baseline_visibility_24, getString(R.string.title_action_seen), piSeen); - Notification.Action.Builder actionArchive = new Notification.Action.Builder( + NotificationCompat.Action.Builder actionArchive = new NotificationCompat.Action.Builder( R.drawable.baseline_archive_24, getString(R.string.title_action_archive), piArchive); - Notification.Action.Builder actionTrash = new Notification.Action.Builder( + NotificationCompat.Action.Builder actionTrash = new NotificationCompat.Action.Builder( R.drawable.baseline_delete_24, getString(R.string.title_action_trash), piTrash); - Notification.Builder mbuilder; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - mbuilder = new Notification.Builder(this); - else - mbuilder = new Notification.Builder(this, channelName); + NotificationCompat.Builder mbuilder; + mbuilder = new NotificationCompat.Builder(this, channelName); String folderName = message.folderDisplay == null ? Helper.localizeFolderName(this, message.folderName) @@ -701,32 +686,25 @@ public class ServiceSynchronize extends LifecycleService { sb.append(text); sb.append(""); } - mbuilder.setStyle(new Notification.BigTextStyle().bigText(HtmlHelper.fromHtml(sb.toString()))); + mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(HtmlHelper.fromHtml(sb.toString()))); } catch (IOException ex) { Log.e(ex); - mbuilder.setStyle(new Notification.BigTextStyle().bigText(ex.toString())); + mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(ex.toString())); } if (info.hasPhoto()) mbuilder.setLargeIcon(info.getPhotoBitmap()); if (info.hasLookupUri()) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) - mbuilder.addPerson(new Person.Builder() - .setUri(info.getLookupUri().toString()) - .build()); - else - mbuilder.addPerson(info.getLookupUri().toString()); + mbuilder.addPerson(info.getLookupUri().toString()); if (message.accountColor != null) { mbuilder.setColor(message.accountColor); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - mbuilder.setColorized(true); + mbuilder.setColorized(true); } } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - mbuilder.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN); + mbuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); notifications.add(mbuilder.build()); } @@ -734,11 +712,11 @@ public class ServiceSynchronize extends LifecycleService { return notifications; } - private Notification.Builder getNotificationError(String title, Throwable ex) { + private NotificationCompat.Builder getNotificationError(String title, Throwable ex) { return getNotificationError("error", title, ex, true); } - private Notification.Builder getNotificationError(String channel, String title, Throwable ex, boolean debug) { + private NotificationCompat.Builder getNotificationError(String channel, String title, Throwable ex, boolean debug) { // Build pending intent Intent intent = new Intent(this, ActivitySetup.class); if (debug) @@ -748,11 +726,7 @@ public class ServiceSynchronize extends LifecycleService { this, ActivitySetup.REQUEST_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Build notification - Notification.Builder builder; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - builder = new Notification.Builder(this, channel); - else - builder = new Notification.Builder(this); + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel); builder .setSmallIcon(R.drawable.baseline_warning_white_24) @@ -764,9 +738,9 @@ public class ServiceSynchronize extends LifecycleService { .setPriority(Notification.PRIORITY_MAX) .setOnlyAlertOnce(true) .setCategory(Notification.CATEGORY_ERROR) - .setVisibility(Notification.VISIBILITY_SECRET); + .setVisibility(NotificationCompat.VISIBILITY_SECRET); - builder.setStyle(new Notification.BigTextStyle() + builder.setStyle(new NotificationCompat.BigTextStyle() .bigText(Helper.formatThrowable(ex, false, "\n"))); return builder;