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.
This commit is contained in:
Hans-Christoph Steiner 2016-05-06 13:22:40 +02:00
parent 08988f2369
commit dded004321
2 changed files with 7 additions and 8 deletions

View File

@ -179,12 +179,13 @@ public class InstallManagerService extends Service {
} }
private NotificationCompat.Builder createNotification(String urlString, @Nullable String packageName) { private NotificationCompat.Builder createNotification(String urlString, @Nullable String packageName) {
int downloadUrlId = urlString.hashCode();
return new NotificationCompat.Builder(this) return new NotificationCompat.Builder(this)
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent(createAppDetailsIntent(0, packageName)) .setContentIntent(getAppDetailsIntent(downloadUrlId, apk.packageName))
.setContentTitle(getNotificationTitle(packageName)) .setContentTitle(getNotificationTitle(packageName))
.addAction(R.drawable.ic_cancel_black_24dp, getString(R.string.cancel), .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) .setSmallIcon(android.R.drawable.stat_sys_download)
.setContentText(urlString) .setContentText(urlString)
.setProgress(100, 0, true); .setProgress(100, 0, true);
@ -207,7 +208,7 @@ public class InstallManagerService extends Service {
return title; return title;
} }
private PendingIntent createAppDetailsIntent(int requestCode, String packageName) { private PendingIntent getAppDetailsIntent(int requestCode, String packageName) {
TaskStackBuilder stackBuilder; TaskStackBuilder stackBuilder;
if (packageName != null) { if (packageName != null) {
Intent notifyIntent = new Intent(getApplicationContext(), AppDetails.class) Intent notifyIntent = new Intent(getApplicationContext(), AppDetails.class)
@ -252,7 +253,7 @@ public class InstallManagerService extends Service {
.setAutoCancel(true) .setAutoCancel(true)
.setContentTitle(title) .setContentTitle(title)
.setSmallIcon(android.R.drawable.stat_sys_download_done) .setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(createAppDetailsIntent(downloadUrlId, packageName)) .setContentIntent(getAppDetailsIntent(downloadUrlId, packageName))
.setContentText(getString(R.string.tap_to_install)); .setContentText(getString(R.string.tap_to_install));
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(downloadUrlId, builder.build()); nm.notify(downloadUrlId, builder.build());

View File

@ -30,7 +30,6 @@ import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.os.PatternMatcher; import android.os.PatternMatcher;
import android.os.Process; import android.os.Process;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -138,13 +137,12 @@ public class DownloaderService extends Service {
} }
} }
public static PendingIntent createCancelDownloadIntent(@NonNull Context context, int public static PendingIntent getCancelPendingIntent(Context context, String urlString) {
requestCode, @NonNull String urlString) {
Intent cancelIntent = new Intent(context.getApplicationContext(), DownloaderService.class) Intent cancelIntent = new Intent(context.getApplicationContext(), DownloaderService.class)
.setData(Uri.parse(urlString)) .setData(Uri.parse(urlString))
.setAction(ACTION_CANCEL); .setAction(ACTION_CANCEL);
return PendingIntent.getService(context.getApplicationContext(), return PendingIntent.getService(context.getApplicationContext(),
requestCode, urlString.hashCode(),
cancelIntent, cancelIntent,
PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent.FLAG_CANCEL_CURRENT);
} }