Show progress bar in download notification.

By providing the same notification id, the existing one is updated.
This commit is contained in:
Peter Serwylo 2016-04-17 09:08:32 +10:00
parent 8d7b5bff5d
commit 6d603ada0d

View File

@ -17,6 +17,8 @@
package org.fdroid.fdroid.net; package org.fdroid.fdroid.net;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -135,7 +137,7 @@ public class DownloaderService extends Service {
} }
} else if (ACTION_QUEUE.equals(intent.getAction())) { } else if (ACTION_QUEUE.equals(intent.getAction())) {
if (Preferences.get().isUpdateNotificationEnabled()) { if (Preferences.get().isUpdateNotificationEnabled()) {
createNotification(intent.getDataString()); startForeground(NOTIFY_DOWNLOADING, createNotification(intent.getDataString()).build());
} }
Log.i(TAG, "Queued " + intent); Log.i(TAG, "Queued " + intent);
Message msg = serviceHandler.obtainMessage(); Message msg = serviceHandler.obtainMessage();
@ -150,14 +152,13 @@ public class DownloaderService extends Service {
} }
} }
private void createNotification(String urlString) { private NotificationCompat.Builder createNotification(String urlString) {
NotificationCompat.Builder builder = return new NotificationCompat.Builder(this)
new NotificationCompat.Builder(this) .setAutoCancel(true)
.setAutoCancel(true) .setContentTitle(getString(R.string.downloading))
.setContentTitle(getString(R.string.downloading)) .setSmallIcon(android.R.drawable.stat_sys_download)
.setSmallIcon(android.R.drawable.stat_sys_download) .setContentText(urlString)
.setContentText(urlString); .setProgress(100, 0, true);
startForeground(NOTIFY_DOWNLOADING, builder.build());
} }
@Override @Override
@ -214,6 +215,12 @@ public class DownloaderService extends Service {
intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead); intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead);
intent.putExtra(Downloader.EXTRA_TOTAL_BYTES, totalBytes); intent.putExtra(Downloader.EXTRA_TOTAL_BYTES, totalBytes);
localBroadcastManager.sendBroadcast(intent); localBroadcastManager.sendBroadcast(intent);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = createNotification(uri.toString())
.setProgress(totalBytes, bytesRead, false)
.build();
nm.notify(NOTIFY_DOWNLOADING, notification);
} }
}); });
downloader.download(); downloader.download();