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.
This commit is contained in:
Hans-Christoph Steiner 2016-05-03 12:25:55 +02:00
parent ac151716c9
commit 3ee64b1bc0
3 changed files with 34 additions and 86 deletions

View File

@ -442,9 +442,6 @@
<service
android:name=".net.DownloaderService"
android:exported="false" />
<service
android:name=".net.DownloadCompleteService"
android:exported="false" />
<service
android:name=".CleanCacheService"
android:exported="false" />

View File

@ -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());
}
}
}

View File

@ -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);
}