From dded004321b5ac40abba499d4dafeb2ea16d010c Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 6 May 2016 13:22:40 +0200 Subject: [PATCH] use standard URL ID int for Intents used in Notifications This keeps the IDs standard throughout the code: either urlString when it should be a String, or urlString.hashCode() when it should be an int. It also follows the naming convention in DownloaderService helper methods, e.g. getIntentFilter(). "create" to me doesn't necessarily mean also "get" Using @NonNull or @Nullable is fine when it is actually useful, like the compiler can catch errors, but it also adds a lot of noise when reading the code. For example, @NonNull here will just make people avoid thinking. Context can never be null anywhere in Android, that's a given throughout the Android API. And in this code, urlString is the unique ID used throughout the process, so if its ever null, nothing works. --- .../fdroid/fdroid/installer/InstallManagerService.java | 9 +++++---- .../java/org/fdroid/fdroid/net/DownloaderService.java | 6 ++---- 2 files changed, 7 insertions(+), 8 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 7c3a725b8..a617d9505 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java @@ -179,12 +179,13 @@ public class InstallManagerService extends Service { } private NotificationCompat.Builder createNotification(String urlString, @Nullable String packageName) { + int downloadUrlId = urlString.hashCode(); return new NotificationCompat.Builder(this) .setAutoCancel(true) - .setContentIntent(createAppDetailsIntent(0, packageName)) + .setContentIntent(getAppDetailsIntent(downloadUrlId, apk.packageName)) .setContentTitle(getNotificationTitle(packageName)) .addAction(R.drawable.ic_cancel_black_24dp, getString(R.string.cancel), - DownloaderService.createCancelDownloadIntent(this, 0, urlString)) + DownloaderService.getCancelPendingIntent(this, urlString)) .setSmallIcon(android.R.drawable.stat_sys_download) .setContentText(urlString) .setProgress(100, 0, true); @@ -207,7 +208,7 @@ public class InstallManagerService extends Service { return title; } - private PendingIntent createAppDetailsIntent(int requestCode, String packageName) { + private PendingIntent getAppDetailsIntent(int requestCode, String packageName) { TaskStackBuilder stackBuilder; if (packageName != null) { Intent notifyIntent = new Intent(getApplicationContext(), AppDetails.class) @@ -252,7 +253,7 @@ public class InstallManagerService extends Service { .setAutoCancel(true) .setContentTitle(title) .setSmallIcon(android.R.drawable.stat_sys_download_done) - .setContentIntent(createAppDetailsIntent(downloadUrlId, packageName)) + .setContentIntent(getAppDetailsIntent(downloadUrlId, packageName)) .setContentText(getString(R.string.tap_to_install)); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(downloadUrlId, builder.build()); diff --git a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java index 7a5a0f37e..21061f260 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -30,7 +30,6 @@ import android.os.Looper; import android.os.Message; import android.os.PatternMatcher; import android.os.Process; -import android.support.annotation.NonNull; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; @@ -138,13 +137,12 @@ public class DownloaderService extends Service { } } - public static PendingIntent createCancelDownloadIntent(@NonNull Context context, int - requestCode, @NonNull String urlString) { + public static PendingIntent getCancelPendingIntent(Context context, String urlString) { Intent cancelIntent = new Intent(context.getApplicationContext(), DownloaderService.class) .setData(Uri.parse(urlString)) .setAction(ACTION_CANCEL); return PendingIntent.getService(context.getApplicationContext(), - requestCode, + urlString.hashCode(), cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT); }