reuse Notification.Builder instances

This is the recommended way to deal with updating Notifications. Each new
update should be a new Notification, but the same Builder instance should
be used to generate each new Notification instance.
This commit is contained in:
Hans-Christoph Steiner 2016-05-23 16:13:17 +02:00
parent 478538690e
commit f9a30d2e1c

View File

@ -165,10 +165,10 @@ public class InstallManagerService extends Service {
Apk apk = new Apk(intent.getParcelableExtra(EXTRA_APK)); Apk apk = new Apk(intent.getParcelableExtra(EXTRA_APK));
addToActive(urlString, app, apk); addToActive(urlString, app, apk);
Notification notification = createNotification(intent.getDataString(), apk).build(); NotificationCompat.Builder builder = createNotificationBuilder(intent.getDataString(), apk);
notificationManager.notify(urlString.hashCode(), notification); notificationManager.notify(urlString.hashCode(), builder.build());
registerDownloaderReceivers(urlString); registerDownloaderReceivers(urlString, builder);
File apkFilePath = Utils.getApkDownloadPath(this, intent.getData()); File apkFilePath = Utils.getApkDownloadPath(this, intent.getData());
long apkFileSize = apkFilePath.length(); long apkFileSize = apkFilePath.length();
@ -215,7 +215,7 @@ public class InstallManagerService extends Service {
} }
} }
private void registerDownloaderReceivers(String urlString) { private void registerDownloaderReceivers(String urlString, final NotificationCompat.Builder builder) {
BroadcastReceiver startedReceiver = new BroadcastReceiver() { BroadcastReceiver startedReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -225,13 +225,10 @@ public class InstallManagerService extends Service {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String urlString = intent.getDataString(); String urlString = intent.getDataString();
Apk apk = ACTIVE_APKS.get(urlString);
int bytesRead = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, 0); int bytesRead = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, 0);
int totalBytes = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, 0); int totalBytes = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, 0);
Notification notification = createNotification(urlString, apk) builder.setProgress(totalBytes, bytesRead, false);
.setProgress(totalBytes, bytesRead, false) notificationManager.notify(urlString.hashCode(), builder.build());
.build();
notificationManager.notify(urlString.hashCode(), notification);
} }
}; };
BroadcastReceiver completeReceiver = new BroadcastReceiver() { BroadcastReceiver completeReceiver = new BroadcastReceiver() {
@ -243,7 +240,7 @@ public class InstallManagerService extends Service {
if (AppDetails.isAppVisible(apk.packageName)) { if (AppDetails.isAppVisible(apk.packageName)) {
cancelNotification(urlString); cancelNotification(urlString);
} else { } else {
notifyDownloadComplete(apk, urlString); notifyDownloadComplete(urlString, builder, apk);
} }
unregisterDownloaderReceivers(urlString); unregisterDownloaderReceivers(urlString);
} }
@ -270,7 +267,7 @@ public class InstallManagerService extends Service {
}); });
} }
private NotificationCompat.Builder createNotification(String urlString, Apk apk) { private NotificationCompat.Builder createNotificationBuilder(String urlString, Apk apk) {
int downloadUrlId = urlString.hashCode(); int downloadUrlId = urlString.hashCode();
return new NotificationCompat.Builder(this) return new NotificationCompat.Builder(this)
.setAutoCancel(false) .setAutoCancel(false)
@ -317,7 +314,7 @@ public class InstallManagerService extends Service {
* Post a notification about a completed download. {@code packageName} must be a valid * Post a notification about a completed download. {@code packageName} must be a valid
* and currently in the app index database. * and currently in the app index database.
*/ */
private void notifyDownloadComplete(Apk apk, String urlString) { private void notifyDownloadComplete(String urlString, NotificationCompat.Builder builder, Apk apk) {
String title; String title;
try { try {
PackageManager pm = getPackageManager(); PackageManager pm = getPackageManager();
@ -328,15 +325,12 @@ public class InstallManagerService extends Service {
} }
int downloadUrlId = urlString.hashCode(); int downloadUrlId = urlString.hashCode();
NotificationCompat.Builder builder = builder.setAutoCancel(true)
new NotificationCompat.Builder(this) .setOngoing(false)
.setAutoCancel(true)
.setContentTitle(title) .setContentTitle(title)
.setSmallIcon(android.R.drawable.stat_sys_download_done) .setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(getAppDetailsIntent(downloadUrlId, apk))
.setContentText(getString(R.string.tap_to_install)); .setContentText(getString(R.string.tap_to_install));
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(downloadUrlId, builder.build());
nm.notify(downloadUrlId, builder.build());
} }
/** /**