From 3ee64b1bc0e14f69f5f220bb21814b321176cf09 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 3 May 2016 12:25:55 +0200 Subject: [PATCH] fold DownloadCompleteService into DownloaderService Having the notification as its own Service is overkill and really only serves to increase complexity. The notification stuff should not take much time or resources at all. --- app/src/main/AndroidManifest.xml | 3 - .../fdroid/net/DownloadCompleteService.java | 76 ------------------- .../fdroid/fdroid/net/DownloaderService.java | 41 ++++++++-- 3 files changed, 34 insertions(+), 86 deletions(-) delete mode 100644 app/src/main/java/org/fdroid/fdroid/net/DownloadCompleteService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 87d8cf851..ced498306 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -442,9 +442,6 @@ - diff --git a/app/src/main/java/org/fdroid/fdroid/net/DownloadCompleteService.java b/app/src/main/java/org/fdroid/fdroid/net/DownloadCompleteService.java deleted file mode 100644 index fa5029d94..000000000 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloadCompleteService.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.fdroid.fdroid.net; - -import android.app.IntentService; -import android.app.NotificationManager; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Process; -import android.support.v4.app.NotificationCompat; -import android.text.TextUtils; - -import org.fdroid.fdroid.R; -import org.fdroid.fdroid.Utils; -import org.fdroid.fdroid.data.App; -import org.fdroid.fdroid.data.AppProvider; - -public class DownloadCompleteService extends IntentService { - private static final String TAG = "DownloadCompleteService"; - - private static final String ACTION_NOTIFY = "org.fdroid.fdroid.net.action.NOTIFY"; - private static final String EXTRA_PACKAGE_NAME = "org.fdroid.fdroid.net.extra.PACKAGE_NAME"; - - public DownloadCompleteService() { - super("DownloadCompleteService"); - } - - public static void notify(Context context, String packageName, String urlString) { - Intent intent = new Intent(context, DownloadCompleteService.class); - intent.setAction(ACTION_NOTIFY); - intent.setData(Uri.parse(urlString)); - intent.putExtra(EXTRA_PACKAGE_NAME, packageName); - context.startService(intent); - } - - @Override - protected void onHandleIntent(Intent intent) { - Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); - if (intent != null) { - final String action = intent.getAction(); - if (!ACTION_NOTIFY.equals(action)) { - Utils.debugLog(TAG, "Intent action is not ACTION_NOTIFY"); - return; - } - String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME); - if (TextUtils.isEmpty(packageName)) { - Utils.debugLog(TAG, "Intent is missing EXTRA_PACKAGE_NAME"); - return; - } - - String title; - try { - PackageManager pm = getPackageManager(); - title = String.format(getString(R.string.tap_to_update_format), - pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0))); - } catch (PackageManager.NameNotFoundException e) { - App app = AppProvider.Helper.findByPackageName(getContentResolver(), packageName, - new String[]{ - AppProvider.DataColumns.NAME, - }); - title = String.format(getString(R.string.tap_to_install_format), app.name); - } - - int requestCode = Utils.getApkUrlNotificationId(intent.getDataString()); - NotificationCompat.Builder builder = - new NotificationCompat.Builder(this) - .setAutoCancel(true) - .setContentTitle(title) - .setSmallIcon(android.R.drawable.stat_sys_download_done) - .setContentIntent(DownloaderService.createAppDetailsIntent(this, requestCode, packageName)) - .setContentText(getString(R.string.tap_to_install)); - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - nm.notify(Utils.getApkUrlNotificationId(intent.getDataString()), 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 b8d93c0d0..ade4f30de 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -24,6 +24,7 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Handler; import android.os.HandlerThread; @@ -166,7 +167,7 @@ public class DownloaderService extends Service { private NotificationCompat.Builder createNotification(String urlString, @Nullable String packageName) { return new NotificationCompat.Builder(this) .setAutoCancel(true) - .setContentIntent(createAppDetailsIntent(this, 0, packageName)) + .setContentIntent(createAppDetailsIntent(0, packageName)) .setContentTitle(getNotificationTitle(packageName)) .addAction(R.drawable.ic_cancel_black_24dp, getString(R.string.cancel), createCancelDownloadIntent(this, 0, urlString)) @@ -191,20 +192,20 @@ public class DownloaderService extends Service { return getString(R.string.downloading); } - public static PendingIntent createAppDetailsIntent(@NonNull Context context, int requestCode, @Nullable String packageName) { + private PendingIntent createAppDetailsIntent(int requestCode, String packageName) { TaskStackBuilder stackBuilder; if (packageName != null) { - Intent notifyIntent = new Intent(context.getApplicationContext(), AppDetails.class) + Intent notifyIntent = new Intent(getApplicationContext(), AppDetails.class) .putExtra(AppDetails.EXTRA_APPID, packageName); stackBuilder = TaskStackBuilder - .create(context.getApplicationContext()) + .create(getApplicationContext()) .addParentStack(AppDetails.class) .addNextIntent(notifyIntent); } else { - Intent notifyIntent = new Intent(context.getApplicationContext(), FDroid.class); + Intent notifyIntent = new Intent(getApplicationContext(), FDroid.class); stackBuilder = TaskStackBuilder - .create(context.getApplicationContext()) + .create(getApplicationContext()) .addParentStack(FDroid.class) .addNextIntent(notifyIntent); } @@ -298,7 +299,7 @@ public class DownloaderService extends Service { }); downloader.download(); sendBroadcast(uri, Downloader.ACTION_COMPLETE, localFile); - DownloadCompleteService.notify(this, packageName, intent.getDataString()); + notifyDownloadComplete(packageName, intent.getDataString()); } catch (InterruptedException e) { sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, localFile); } catch (IOException e) { @@ -317,6 +318,32 @@ public class DownloaderService extends Service { downloader = null; } + private void notifyDownloadComplete(String packageName, String urlString) { + String title; + try { + PackageManager pm = getPackageManager(); + title = String.format(getString(R.string.tap_to_update_format), + pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0))); + } catch (PackageManager.NameNotFoundException e) { + App app = AppProvider.Helper.findByPackageName(getContentResolver(), packageName, + new String[]{ + AppProvider.DataColumns.NAME, + }); + title = String.format(getString(R.string.tap_to_install_format), app.name); + } + + int requestCode = Utils.getApkUrlNotificationId(urlString); + NotificationCompat.Builder builder = + new NotificationCompat.Builder(this) + .setAutoCancel(true) + .setContentTitle(title) + .setSmallIcon(android.R.drawable.stat_sys_download_done) + .setContentIntent(createAppDetailsIntent(requestCode, packageName)) + .setContentText(getString(R.string.tap_to_install)); + NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + nm.notify(Utils.getApkUrlNotificationId(urlString), builder.build()); + } + private void sendBroadcast(Uri uri, String action, File file) { sendBroadcast(uri, action, file, null); }