improve download notification state

This commit is contained in:
dkanada 2021-05-30 16:23:10 +09:00
commit 0e44c4f7f0
2 changed files with 8 additions and 7 deletions

View file

@ -48,6 +48,7 @@ public class DownloadService extends Service {
List<Song> songs = intent.getParcelableArrayListExtra(EXTRA_SONGS); List<Song> songs = intent.getParcelableArrayListExtra(EXTRA_SONGS);
for (Song song : songs) { for (Song song : songs) {
download(song); download(song);
notification.start(song);
} }
return super.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId);
@ -58,6 +59,7 @@ public class DownloadService extends Service {
return null; return null;
} }
@SuppressWarnings("ConstantConditions")
public void download(Song song) { public void download(Song song) {
executor.execute(() -> { executor.execute(() -> {
try { try {
@ -81,10 +83,10 @@ public class DownloadService extends Service {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
int count; int count;
notification.start(song, connection.getContentLength()); notification.update(0, connection.getContentLength());
while ((count = input.read(data)) != -1) { while ((count = input.read(data)) != -1) {
output.write(data, 0, count); output.write(data, 0, count);
notification.update(count); notification.update(count, 0);
} }
input.close(); input.close();

View file

@ -39,18 +39,17 @@ public class DownloadNotification {
this.songs = new ArrayList<>(); this.songs = new ArrayList<>();
} }
public synchronized void start(Song song, int maximum) { public synchronized void start(Song song) {
this.songs.add(song); this.songs.add(song);
this.maximum += maximum;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(); createNotificationChannel();
} }
} }
public synchronized void update(int current) { public synchronized void update(int current, int maximum) {
this.current += current; this.current += current;
this.maximum += maximum;
Intent action = new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 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); PendingIntent clickIntent = PendingIntent.getActivity(context, 0, action, 0);
@ -64,7 +63,7 @@ public class DownloadNotification {
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setContentIntent(clickIntent) .setContentIntent(clickIntent)
.setContentTitle(String.format(context.getString(R.string.downloading_x_songs), songs.size())) .setContentTitle(String.format(context.getString(R.string.downloading_x_songs), songs.size()))
.setProgress(maximum, this.current, false) .setProgress(this.maximum, this.current, false)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(style) .setStyle(style)
.setShowWhen(false); .setShowWhen(false);