From 478538690e5989b36835050c590530bba445ed71 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Sat, 21 May 2016 00:00:29 +0200 Subject: [PATCH 1/2] fix issue where first time installs do not work New installs where being caught up in the logic to check whether a download is still in progress after InstallManagerService got killed. Also checking whether Intent was just redelivered lets the new installs through while screening out the inactive Intents that were redelivered. This logic also cancels the notification for any download that was in progress when the InstallManagerService was killed. #660 --- .../org/fdroid/fdroid/installer/InstallManagerService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java index 7a2091a9c..5b74a2217 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java @@ -154,7 +154,8 @@ public class InstallManagerService extends Service { return START_NOT_STICKY; } - if (!DownloaderService.isQueuedOrActive(urlString)) { + if ((flags & START_FLAG_REDELIVERY) == START_FLAG_REDELIVERY + && !DownloaderService.isQueuedOrActive(urlString)) { Utils.debugLog(TAG, urlString + " finished downloading while InstallManagerService was killed."); cancelNotification(urlString); return START_NOT_STICKY; From f9a30d2e1c9d95302e8f4dd0e733d019f70bed66 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 23 May 2016 16:13:17 +0200 Subject: [PATCH 2/2] 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. --- .../installer/InstallManagerService.java | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java index 5b74a2217..809825921 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java @@ -165,10 +165,10 @@ public class InstallManagerService extends Service { Apk apk = new Apk(intent.getParcelableExtra(EXTRA_APK)); addToActive(urlString, app, apk); - Notification notification = createNotification(intent.getDataString(), apk).build(); - notificationManager.notify(urlString.hashCode(), notification); + NotificationCompat.Builder builder = createNotificationBuilder(intent.getDataString(), apk); + notificationManager.notify(urlString.hashCode(), builder.build()); - registerDownloaderReceivers(urlString); + registerDownloaderReceivers(urlString, builder); File apkFilePath = Utils.getApkDownloadPath(this, intent.getData()); 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() { @Override public void onReceive(Context context, Intent intent) { @@ -225,13 +225,10 @@ public class InstallManagerService extends Service { @Override public void onReceive(Context context, Intent intent) { String urlString = intent.getDataString(); - Apk apk = ACTIVE_APKS.get(urlString); int bytesRead = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, 0); int totalBytes = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, 0); - Notification notification = createNotification(urlString, apk) - .setProgress(totalBytes, bytesRead, false) - .build(); - notificationManager.notify(urlString.hashCode(), notification); + builder.setProgress(totalBytes, bytesRead, false); + notificationManager.notify(urlString.hashCode(), builder.build()); } }; BroadcastReceiver completeReceiver = new BroadcastReceiver() { @@ -243,7 +240,7 @@ public class InstallManagerService extends Service { if (AppDetails.isAppVisible(apk.packageName)) { cancelNotification(urlString); } else { - notifyDownloadComplete(apk, urlString); + notifyDownloadComplete(urlString, builder, apk); } 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(); return new NotificationCompat.Builder(this) .setAutoCancel(false) @@ -317,7 +314,7 @@ public class InstallManagerService extends Service { * Post a notification about a completed download. {@code packageName} must be a valid * 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; try { PackageManager pm = getPackageManager(); @@ -328,15 +325,12 @@ public class InstallManagerService extends Service { } int downloadUrlId = urlString.hashCode(); - NotificationCompat.Builder builder = - new NotificationCompat.Builder(this) - .setAutoCancel(true) - .setContentTitle(title) - .setSmallIcon(android.R.drawable.stat_sys_download_done) - .setContentIntent(getAppDetailsIntent(downloadUrlId, apk)) - .setContentText(getString(R.string.tap_to_install)); - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - nm.notify(downloadUrlId, builder.build()); + builder.setAutoCancel(true) + .setOngoing(false) + .setContentTitle(title) + .setSmallIcon(android.R.drawable.stat_sys_download_done) + .setContentText(getString(R.string.tap_to_install)); + notificationManager.notify(downloadUrlId, builder.build()); } /**