From c9a6cc305111557b68b87f828b4d493f2c89cd49 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 31 Aug 2016 21:52:52 +0200 Subject: [PATCH] handle install broadcasts after InstallManagerService was killed If InstallManagerService was killed, it'll forget all of its state. If it is killed while an install process is running, and that install fails, InstallManagerService will receive a broadcast about the error but then it can't find anything about the app in question besides its download URL. That is enough to control the notification, but not enough to get the name of the app in question. This is a workaround by showing the APK filename when the app name cannot be found. Ideally, the packageName would somehow magically be delivered to InstallManagerService in this case, but the Installer stuff doesn't always have it to send. With android-23, there is getActiveNotifications(), which we might be able to use to stash the packageName and fetch it as needed. --- .../installer/InstallManagerService.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 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 d8a5d8bc0..eb77f757e 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java @@ -16,7 +16,6 @@ import android.support.v4.app.TaskStackBuilder; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; -import org.acra.ACRA; import org.fdroid.fdroid.AppDetails; import org.fdroid.fdroid.R; import org.fdroid.fdroid.Utils; @@ -249,22 +248,12 @@ public class InstallManagerService extends Service { // show notification if app details is not visible if (!TextUtils.isEmpty(errorMessage)) { - try { - // temp setup to debug https://gitlab.com/fdroid/fdroidclient/issues/698 - App app = getAppFromActive(downloadUrl); - - // show notification if app details is not visible - if (AppDetails.isAppVisible(app.packageName)) { - cancelNotification(downloadUrl); - } else { - String title = String.format( - getString(R.string.install_error_notify_title), - app.name); - notifyError(downloadUrl, title, errorMessage); - } - } catch (NullPointerException e) { //NOPMD - ACRA.getErrorReporter().handleException( - new IllegalStateException(errorMessage, e)); + App app = getAppFromActive(downloadUrl); + // show notification if app details is not visible + if (app != null && AppDetails.isAppVisible(app.packageName)) { + cancelNotification(downloadUrl); + } else { + notifyError(downloadUrl, app, errorMessage); } } removeFromActive(downloadUrl); @@ -358,9 +347,19 @@ public class InstallManagerService extends Service { notificationManager.notify(downloadUrlId, notification); } - private void notifyError(String urlString, String title, String text) { + private void notifyError(String urlString, App app, String text) { int downloadUrlId = urlString.hashCode(); + String name; + if (app == null) { + // if we have nothing else, show the APK filename + String path = Uri.parse(urlString).getPath(); + name = path.substring(path.lastIndexOf('/'), path.length()); + } else { + name = app.name; + } + String title = String.format(getString(R.string.install_error_notify_title), name); + Intent errorDialogIntent = new Intent(this, ErrorDialogActivity.class); errorDialogIntent.putExtra( ErrorDialogActivity.EXTRA_TITLE, title); @@ -397,8 +396,16 @@ public class InstallManagerService extends Service { ACTIVE_APPS.put(app.packageName, app); } + /** + * Always returns an {@link Apk} instance to avoid annoying null guards. + */ private static Apk getApkFromActive(String urlString) { - return ACTIVE_APKS.get(urlString); + Apk apk = ACTIVE_APKS.get(urlString); + if (apk == null) { + return new Apk(); + } else { + return apk; + } } /**