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

View file

@ -39,18 +39,17 @@ public class DownloadNotification {
this.songs = new ArrayList<>();
}
public synchronized void start(Song song, int maximum) {
public synchronized void start(Song song) {
this.songs.add(song);
this.maximum += maximum;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
}
public synchronized void update(int current) {
public synchronized void update(int current, int maximum) {
this.current += current;
this.maximum += maximum;
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);
@ -64,7 +63,7 @@ public class DownloadNotification {
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(clickIntent)
.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)
.setStyle(style)
.setShowWhen(false);