From 86d20c044ba9e181b4a8379b054ebff83a29d929 Mon Sep 17 00:00:00 2001 From: dkanada Date: Sun, 16 May 2021 12:08:36 +0900 Subject: [PATCH] add notification for download progress --- .../gramophone/service/DownloadService.java | 8 +- .../notifications/DownloadNotification.java | 108 ++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/dkanada/gramophone/service/notifications/DownloadNotification.java diff --git a/app/src/main/java/com/dkanada/gramophone/service/DownloadService.java b/app/src/main/java/com/dkanada/gramophone/service/DownloadService.java index 5d2ee748..92bd11cf 100644 --- a/app/src/main/java/com/dkanada/gramophone/service/DownloadService.java +++ b/app/src/main/java/com/dkanada/gramophone/service/DownloadService.java @@ -10,6 +10,7 @@ import com.dkanada.gramophone.App; import com.dkanada.gramophone.BuildConfig; import com.dkanada.gramophone.database.Cache; import com.dkanada.gramophone.model.Song; +import com.dkanada.gramophone.service.notifications.DownloadNotification; import com.dkanada.gramophone.util.MusicUtil; import com.dkanada.gramophone.util.PreferenceUtil; @@ -30,6 +31,7 @@ public class DownloadService extends Service { private Executor executor; private Handler handler; + private DownloadNotification notification; @Override public void onCreate() { @@ -42,6 +44,7 @@ public class DownloadService extends Service { executor = Executors.newFixedThreadPool(4); handler = new Handler(looper); + notification = new DownloadNotification(this); } @Override @@ -68,11 +71,13 @@ public class DownloadService extends Service { connection.connect(); - byte[] data = new byte[262144]; + byte[] data = new byte[1048576]; int count; + notification.start(song, connection.getContentLength()); while ((count = input.read(data)) != -1) { output.write(data, 0, count); + notification.update(count); } input.close(); @@ -90,6 +95,7 @@ public class DownloadService extends Service { download.delete(); App.getDatabase().cacheDao().insertCache(new Cache(song)); + notification.stop(song); } catch (Exception e) { e.printStackTrace(); } diff --git a/app/src/main/java/com/dkanada/gramophone/service/notifications/DownloadNotification.java b/app/src/main/java/com/dkanada/gramophone/service/notifications/DownloadNotification.java new file mode 100644 index 00000000..145cfbfb --- /dev/null +++ b/app/src/main/java/com/dkanada/gramophone/service/notifications/DownloadNotification.java @@ -0,0 +1,108 @@ +package com.dkanada.gramophone.service.notifications; + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.os.Build; + +import androidx.annotation.RequiresApi; +import androidx.core.app.NotificationCompat; + +import com.dkanada.gramophone.R; +import com.dkanada.gramophone.activities.MainActivity; +import com.dkanada.gramophone.model.Song; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import static android.content.Context.NOTIFICATION_SERVICE; + +public class DownloadNotification { + private static final String CHANNEL_ID = DownloadNotification.class.getSimpleName(); + private static final int NOTIFICATION_ID = 2; + + private final Context context; + private final NotificationManager notificationManager; + + private final List songs; + + private int current; + private int maximum; + + public DownloadNotification(Context context) { + this.notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); + this.context = context; + + this.songs = new ArrayList<>(); + } + + public synchronized void start(Song song, int maximum) { + this.songs.add(song); + + this.maximum += maximum; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createNotificationChannel(); + } + } + + public synchronized void update(int current) { + this.current += current; + + Intent action = new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); + PendingIntent clickIntent = PendingIntent.getActivity(context, 0, action, 0); + + NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); + for (Song item : songs.stream().limit(5).collect(Collectors.toList())) { + style.addLine(item.title); + } + + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_notification) + .setContentIntent(clickIntent) + .setContentTitle(String.format(context.getString(R.string.downloading_x_songs), songs.size())) + .setProgress(maximum, this.current, false) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setStyle(style) + .setShowWhen(false); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + } + + public synchronized void stop(Song song) { + if (song != null) { + songs.remove(song); + } + + if (songs.size() != 0) { + return; + } + + current = 0; + maximum = 0; + + notificationManager.cancel(NOTIFICATION_ID); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + notificationManager.deleteNotificationChannel(CHANNEL_ID); + } + } + + @RequiresApi(Build.VERSION_CODES.O) + private void createNotificationChannel() { + NotificationChannel notificationChannel = notificationManager.getNotificationChannel(CHANNEL_ID); + + if (notificationChannel == null) { + notificationChannel = new NotificationChannel(CHANNEL_ID, context.getString(R.string.action_download), NotificationManager.IMPORTANCE_LOW); + + notificationChannel.setDescription(context.getString(R.string.playing_notification_description)); + notificationChannel.enableLights(false); + notificationChannel.enableVibration(false); + + notificationManager.createNotificationChannel(notificationChannel); + } + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7e291e5b..ef39550f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -77,6 +77,7 @@ Please disable battery optimizations for media playback while the screen is off. "Added 1 title to the queue." Added %1$d titles to the queue. + Downloading %1$d songs. %1$s?]]> %1$d playlists?]]> %1$s from the playlist?]]>