From 14a09c725ffd5fc78acaa00831b28962995ea10b Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Fri, 24 Sep 2021 21:28:11 -0400 Subject: [PATCH] Use NotificationChannelCompat features --- .../services/DownloadMediaService.java | 20 +++++++--------- .../services/DownloadRedditVideoService.java | 20 +++++++--------- .../services/MaterialYouService.java | 23 +++++++++---------- .../services/SubmitPostService.java | 21 ++++++++--------- .../utils/NotificationUtils.java | 22 +++++++++--------- 5 files changed, 47 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java index e5534ef9..f0a551fe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java @@ -1,8 +1,6 @@ package ml.docilealligator.infinityforreddit.services; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.ContentResolver; @@ -22,6 +20,7 @@ import android.os.Message; import android.os.Process; import android.provider.MediaStore; +import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.documentfile.provider.DocumentFile; @@ -367,16 +366,13 @@ public class DownloadMediaService extends Service { int mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE); builder = new NotificationCompat.Builder(this, getNotificationChannelId(mediaType)); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel; - serviceChannel = new NotificationChannel( - getNotificationChannelId(mediaType), - getNotificationChannel(mediaType), - NotificationManager.IMPORTANCE_LOW - ); - - notificationManager.createNotificationChannel(serviceChannel); - } + NotificationChannelCompat serviceChannel = + new NotificationChannelCompat.Builder( + getNotificationChannelId(mediaType), + NotificationManagerCompat.IMPORTANCE_LOW) + .setName(getNotificationChannel(mediaType)) + .build(); + notificationManager.createNotificationChannel(serviceChannel); int randomNotificationIdOffset = new Random().nextInt(10000); switch (intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE)) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java index a398871e..2079e024 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java @@ -1,8 +1,6 @@ package ml.docilealligator.infinityforreddit.services; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.ContentResolver; @@ -26,6 +24,7 @@ import android.os.Message; import android.os.Process; import android.provider.MediaStore; +import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.documentfile.provider.DocumentFile; @@ -501,16 +500,13 @@ public class DownloadRedditVideoService extends Service { String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT); String fileNameWithoutExtension = subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel; - serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, - NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO, - NotificationManager.IMPORTANCE_LOW - ); - - notificationManager.createNotificationChannel(serviceChannel); - } + NotificationChannelCompat serviceChannel = + new NotificationChannelCompat.Builder( + NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, + NotificationManagerCompat.IMPORTANCE_LOW) + .setName(NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO) + .build(); + notificationManager.createNotificationChannel(serviceChannel); int randomNotificationIdOffset = new Random().nextInt(10000); startForeground( diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java index 66bd4e0b..40059987 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java @@ -1,16 +1,14 @@ package ml.docilealligator.infinityforreddit.services; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; -import android.os.Build; import android.os.Handler; import android.os.IBinder; import androidx.annotation.Nullable; +import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -60,16 +58,17 @@ public class MaterialYouService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_ID_MATERIAL_YOU, - NotificationUtils.CHANNEL_MATERIAL_YOU, - NotificationManager.IMPORTANCE_LOW - ); - NotificationManagerCompat manager = NotificationManagerCompat.from(this); - manager.createNotificationChannel(serviceChannel); - } + NotificationChannelCompat serviceChannel = + new NotificationChannelCompat.Builder( + NotificationUtils.CHANNEL_ID_MATERIAL_YOU, + NotificationManagerCompat.IMPORTANCE_LOW) + .setName(NotificationUtils.CHANNEL_MATERIAL_YOU) + .build(); + + NotificationManagerCompat manager = NotificationManagerCompat.from(this); + manager.createNotificationChannel(serviceChannel); + Notification notification = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_MATERIAL_YOU) .setContentTitle(getString(R.string.material_you_notification_title)) .setContentText(getString(R.string.please_wait)) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java index e22bc541..f1ba75f3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java @@ -1,13 +1,10 @@ package ml.docilealligator.infinityforreddit.services; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; @@ -19,6 +16,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -164,16 +162,15 @@ public class SubmitPostService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_SUBMIT_POST, - NotificationUtils.CHANNEL_SUBMIT_POST, - NotificationManager.IMPORTANCE_LOW - ); + NotificationChannelCompat serviceChannel = + new NotificationChannelCompat.Builder( + NotificationUtils.CHANNEL_SUBMIT_POST, + NotificationManagerCompat.IMPORTANCE_LOW) + .setName(NotificationUtils.CHANNEL_SUBMIT_POST) + .build(); - NotificationManagerCompat manager = NotificationManagerCompat.from(this); - manager.createNotificationChannel(serviceChannel); - } + NotificationManagerCompat manager = NotificationManagerCompat.from(this); + manager.createNotificationChannel(serviceChannel); int randomNotificationIdOffset = new Random().nextInt(10000); int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java index 5552271f..459a0170 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java @@ -1,10 +1,8 @@ package ml.docilealligator.infinityforreddit.utils; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.content.Context; -import android.os.Build; +import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -40,10 +38,11 @@ public class NotificationUtils { Context context, String title, String content, String summary, String channelId, String channelName, String group, int color) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT); - notificationManager.createNotificationChannel(channel); - } + NotificationChannelCompat channel = + new NotificationChannelCompat.Builder(channelId, NotificationManagerCompat.IMPORTANCE_DEFAULT) + .setName(channelName) + .build(); + notificationManager.createNotificationChannel(channel); return new NotificationCompat.Builder(context.getApplicationContext(), channelId) .setContentTitle(title) @@ -60,10 +59,11 @@ public class NotificationUtils { public static NotificationCompat.Builder buildSummaryNotification(Context context, NotificationManagerCompat notificationManager, String title, String content, String channelId, String channelName, String group, int color) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT); - notificationManager.createNotificationChannel(channel); - } + NotificationChannelCompat channel = + new NotificationChannelCompat.Builder(channelId, NotificationManagerCompat.IMPORTANCE_DEFAULT) + .setName(channelName) + .build(); + notificationManager.createNotificationChannel(channel); return new NotificationCompat.Builder(context, channelId) .setContentTitle(title)