Use NotificationChannelCompat features

This commit is contained in:
TacoTheDank 2021-09-24 21:28:11 -04:00
parent c78cc8aaec
commit 14a09c725f
5 changed files with 47 additions and 59 deletions

View File

@ -1,8 +1,6 @@
package ml.docilealligator.infinityforreddit.services; package ml.docilealligator.infinityforreddit.services;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -22,6 +20,7 @@ import android.os.Message;
import android.os.Process; import android.os.Process;
import android.provider.MediaStore; import android.provider.MediaStore;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.documentfile.provider.DocumentFile; 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); int mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE);
builder = new NotificationCompat.Builder(this, getNotificationChannelId(mediaType)); builder = new NotificationCompat.Builder(this, getNotificationChannelId(mediaType));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannelCompat serviceChannel =
NotificationChannel serviceChannel; new NotificationChannelCompat.Builder(
serviceChannel = new NotificationChannel( getNotificationChannelId(mediaType),
getNotificationChannelId(mediaType), NotificationManagerCompat.IMPORTANCE_LOW)
getNotificationChannel(mediaType), .setName(getNotificationChannel(mediaType))
NotificationManager.IMPORTANCE_LOW .build();
); notificationManager.createNotificationChannel(serviceChannel);
notificationManager.createNotificationChannel(serviceChannel);
}
int randomNotificationIdOffset = new Random().nextInt(10000); int randomNotificationIdOffset = new Random().nextInt(10000);
switch (intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE)) { switch (intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE)) {

View File

@ -1,8 +1,6 @@
package ml.docilealligator.infinityforreddit.services; package ml.docilealligator.infinityforreddit.services;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -26,6 +24,7 @@ import android.os.Message;
import android.os.Process; import android.os.Process;
import android.provider.MediaStore; import android.provider.MediaStore;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
@ -501,16 +500,13 @@ public class DownloadRedditVideoService extends Service {
String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT); String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT);
String fileNameWithoutExtension = subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID); String fileNameWithoutExtension = subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannelCompat serviceChannel =
NotificationChannel serviceChannel; new NotificationChannelCompat.Builder(
serviceChannel = new NotificationChannel( NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO,
NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, NotificationManagerCompat.IMPORTANCE_LOW)
NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO, .setName(NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO)
NotificationManager.IMPORTANCE_LOW .build();
); notificationManager.createNotificationChannel(serviceChannel);
notificationManager.createNotificationChannel(serviceChannel);
}
int randomNotificationIdOffset = new Random().nextInt(10000); int randomNotificationIdOffset = new Random().nextInt(10000);
startForeground( startForeground(

View File

@ -1,16 +1,14 @@
package ml.docilealligator.infinityforreddit.services; package ml.docilealligator.infinityforreddit.services;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
@ -60,16 +58,17 @@ public class MaterialYouService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
((Infinity) getApplication()).getAppComponent().inject(this); ((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); NotificationChannelCompat serviceChannel =
manager.createNotificationChannel(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) Notification notification = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_MATERIAL_YOU)
.setContentTitle(getString(R.string.material_you_notification_title)) .setContentTitle(getString(R.string.material_you_notification_title))
.setContentText(getString(R.string.please_wait)) .setContentText(getString(R.string.please_wait))

View File

@ -1,13 +1,10 @@
package ml.docilealligator.infinityforreddit.services; package ml.docilealligator.infinityforreddit.services;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
@ -19,6 +16,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
@ -164,16 +162,15 @@ public class SubmitPostService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannelCompat serviceChannel =
NotificationChannel serviceChannel = new NotificationChannel( new NotificationChannelCompat.Builder(
NotificationUtils.CHANNEL_SUBMIT_POST, NotificationUtils.CHANNEL_SUBMIT_POST,
NotificationUtils.CHANNEL_SUBMIT_POST, NotificationManagerCompat.IMPORTANCE_LOW)
NotificationManager.IMPORTANCE_LOW .setName(NotificationUtils.CHANNEL_SUBMIT_POST)
); .build();
NotificationManagerCompat manager = NotificationManagerCompat.from(this); NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.createNotificationChannel(serviceChannel); manager.createNotificationChannel(serviceChannel);
}
int randomNotificationIdOffset = new Random().nextInt(10000); int randomNotificationIdOffset = new Random().nextInt(10000);
int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK);

View File

@ -1,10 +1,8 @@
package ml.docilealligator.infinityforreddit.utils; package ml.docilealligator.infinityforreddit.utils;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.os.Build;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
@ -40,10 +38,11 @@ public class NotificationUtils {
Context context, String title, String content, Context context, String title, String content,
String summary, String channelId, String channelName, String summary, String channelId, String channelName,
String group, int color) { String group, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannelCompat channel =
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT); new NotificationChannelCompat.Builder(channelId, NotificationManagerCompat.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel); .setName(channelName)
} .build();
notificationManager.createNotificationChannel(channel);
return new NotificationCompat.Builder(context.getApplicationContext(), channelId) return new NotificationCompat.Builder(context.getApplicationContext(), channelId)
.setContentTitle(title) .setContentTitle(title)
@ -60,10 +59,11 @@ public class NotificationUtils {
public static NotificationCompat.Builder buildSummaryNotification(Context context, NotificationManagerCompat notificationManager, public static NotificationCompat.Builder buildSummaryNotification(Context context, NotificationManagerCompat notificationManager,
String title, String content, String channelId, String title, String content, String channelId,
String channelName, String group, int color) { String channelName, String group, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannelCompat channel =
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT); new NotificationChannelCompat.Builder(channelId, NotificationManagerCompat.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel); .setName(channelName)
} .build();
notificationManager.createNotificationChannel(channel);
return new NotificationCompat.Builder(context, channelId) return new NotificationCompat.Builder(context, channelId)
.setContentTitle(title) .setContentTitle(title)