aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2018-07-26 18:00:58 -0700
committerScott Jackson <daneren2005@gmail.com>2018-07-26 18:00:58 -0700
commiteebdb979a2d6d114ab4d8c2e6f5f6ed762b3ae4c (patch)
tree2204522f145b586c07e69d02872f40f7c0e73720 /app/src/main/java
parent602a5429a0523d518cf95022920fa6dabb0a12b4 (diff)
downloaddsub-eebdb979a2d6d114ab4d8c2e6f5f6ed762b3ae4c.tar.gz
dsub-eebdb979a2d6d114ab4d8c2e6f5f6ed762b3ae4c.tar.bz2
dsub-eebdb979a2d6d114ab4d8c2e6f5f6ed762b3ae4c.zip
Add notification channels for API v26 compliance
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/github/daneren2005/dsub/util/Notifications.java68
1 files changed, 66 insertions, 2 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/util/Notifications.java b/app/src/main/java/github/daneren2005/dsub/util/Notifications.java
index 750ab40c..ac36c99d 100644
--- a/app/src/main/java/github/daneren2005/dsub/util/Notifications.java
+++ b/app/src/main/java/github/daneren2005/dsub/util/Notifications.java
@@ -15,7 +15,9 @@
package github.daneren2005.dsub.util;
+import android.annotation.TargetApi;
import android.app.Notification;
+import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
@@ -56,11 +58,24 @@ public final class Notifications {
private static boolean downloadForeground = false;
private static boolean persistentPlayingShowing = false;
+ private static NotificationChannel playingChannel;
+ private static NotificationChannel downloadingChannel;
+ private static NotificationChannel syncChannel;
+
private final static Pair<Integer, Integer> NOTIFICATION_TEXT_COLORS = new Pair<Integer, Integer>();
public static void showPlayingNotification(final Context context, final DownloadService downloadService, final Handler handler, MusicDirectory.Entry song) {
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ getPlayingNotificationChannel(context);
+ }
+
// Set the icon, scrolling text and timestamp
- final Notification notification = new Notification(R.drawable.stat_notify_playing, song.getTitle(), System.currentTimeMillis());
+ final Notification notification = new NotificationCompat.Builder(context)
+ .setSmallIcon(R.drawable.stat_notify_playing)
+ .setTicker(song.getTitle())
+ .setWhen(System.currentTimeMillis())
+ .setChannelId("now-playing-channel")
+ .build();
final boolean playing = downloadService.getPlayerState() == PlayerState.STARTED;
if(playing) {
@@ -331,7 +346,24 @@ public final class Notifications {
DSubWidgetProvider.notifyInstances(context, downloadService, false);
}
+ @TargetApi(Build.VERSION_CODES.O)
+ private static NotificationChannel getPlayingNotificationChannel(Context context) {
+ if(playingChannel == null) {
+ playingChannel = new NotificationChannel("now-playing-channel", "Now Playing", NotificationManager.IMPORTANCE_DEFAULT);
+ playingChannel.setDescription("Now playing notification");
+
+ NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
+ notificationManager.createNotificationChannel(playingChannel);
+ }
+
+ return playingChannel;
+ }
+
public static void showDownloadingNotification(final Context context, final DownloadService downloadService, Handler handler, DownloadFile file, int size) {
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ getDownloadingNotificationChannel(context);
+ }
+
Intent cancelIntent = new Intent(context, DownloadService.class);
cancelIntent.setAction(DownloadService.CANCEL_DOWNLOADS);
PendingIntent cancelPI = PendingIntent.getService(context, 0, cancelIntent, 0);
@@ -356,7 +388,8 @@ public final class Notifications {
.setOngoing(true)
.addAction(R.drawable.notification_close,
context.getResources().getString(R.string.common_cancel),
- cancelPI);
+ cancelPI)
+ .setChannelId("downloading-channel");
Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class);
notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD_VIEW, true);
@@ -395,6 +428,19 @@ public final class Notifications {
}
}
+ @TargetApi(Build.VERSION_CODES.O)
+ private static NotificationChannel getDownloadingNotificationChannel(Context context) {
+ if(downloadingChannel == null) {
+ downloadingChannel = new NotificationChannel("downloading-channel", "Downloading Notification", NotificationManager.IMPORTANCE_DEFAULT);
+ downloadingChannel.setDescription("Ongoing downloading notification to keep the service alive");
+
+ NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
+ notificationManager.createNotificationChannel(downloadingChannel);
+ }
+
+ return downloadingChannel;
+ }
+
public static void showSyncNotification(final Context context, int stringId, String extra) {
showSyncNotification(context, stringId, extra, null);
}
@@ -404,6 +450,10 @@ public final class Notifications {
extra = "";
}
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ getSyncNotificationChannel(context);
+ }
+
NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.stat_notify_sync)
@@ -413,6 +463,7 @@ public final class Notifications {
.setOngoing(false)
.setGroup(NOTIFICATION_SYNC_GROUP)
.setPriority(NotificationCompat.PRIORITY_LOW)
+ .setChannelId("sync-channel")
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class);
@@ -449,4 +500,17 @@ public final class Notifications {
notificationManager.notify(stringId, builder.build());
}
}
+
+ @TargetApi(Build.VERSION_CODES.O)
+ private static NotificationChannel getSyncNotificationChannel(Context context) {
+ if(syncChannel == null) {
+ syncChannel = new NotificationChannel("sync-channel", "Sync Notifications", NotificationManager.IMPORTANCE_LOW);
+ syncChannel.setDescription("Sync notifications");
+
+ NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
+ notificationManager.createNotificationChannel(syncChannel);
+ }
+
+ return syncChannel;
+ }
}