diff --git a/app/src/main/java/org/fdroid/fdroid/AppDetails.java b/app/src/main/java/org/fdroid/fdroid/AppDetails.java index 5bdb405ef..ab1ac8630 100644 --- a/app/src/main/java/org/fdroid/fdroid/AppDetails.java +++ b/app/src/main/java/org/fdroid/fdroid/AppDetails.java @@ -842,7 +842,7 @@ public class AppDetails extends AppCompatActivity { @Override public void onClick(DialogInterface dialog, int whichButton) { - startDownload(apk); + initiateInstall(apk); } }); builder.setNegativeButton(R.string.no, @@ -871,10 +871,10 @@ public class AppDetails extends AppCompatActivity { alert.show(); return; } - startDownload(apk); + initiateInstall(apk); } - private void startDownload(Apk apk) { + private void initiateInstall(Apk apk) { activeDownloadUrlString = apk.getUrl(); registerDownloaderReceivers(); headerFragment.startProgress(); 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 6aa83b599..127445e52 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java @@ -15,6 +15,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; +import android.util.Log; import org.fdroid.fdroid.AppDetails; import org.fdroid.fdroid.R; @@ -65,20 +66,20 @@ public class InstallManagerService extends Service { * matching the {@link App}s in {@code ACTIVE_APPS}. The key is the download URL, as * in {@link Apk#getUrl()} or {@code urlString}. */ - private static final HashMap<String, Apk> ACTIVE_APKS = new HashMap<String, Apk>(3); + private static final HashMap<String, Apk> ACTIVE_APKS = new HashMap<>(3); /** * The collection of {@link App}s that are actively going through this whole process, * matching the {@link Apk}s in {@code ACTIVE_APKS}. The key is the * {@code packageName} of the app. */ - private static final HashMap<String, App> ACTIVE_APPS = new HashMap<String, App>(3); + private static final HashMap<String, App> ACTIVE_APPS = new HashMap<>(3); /** * The array of active {@link BroadcastReceiver}s for each active APK. The key is the * download URL, as in {@link Apk#getUrl()} or {@code urlString}. */ - private final HashMap<String, BroadcastReceiver[]> receivers = new HashMap<String, BroadcastReceiver[]>(3); + private final HashMap<String, BroadcastReceiver[]> receivers = new HashMap<>(3); /** * Get the app name based on a {@code urlString} key. The app name needs @@ -90,7 +91,7 @@ public class InstallManagerService extends Service { * <p> * TODO <b>delete me once InstallerService exists</b> */ - private static final HashMap<String, String> TEMP_HACK_APP_NAMES = new HashMap<String, String>(3); + private static final HashMap<String, String> TEMP_HACK_APP_NAMES = new HashMap<>(3); private LocalBroadcastManager localBroadcastManager; private NotificationManager notificationManager; @@ -132,6 +133,12 @@ public class InstallManagerService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Utils.debugLog(TAG, "onStartCommand " + intent); + + if (!ACTION_INSTALL.equals(intent.getAction())) { + Log.i(TAG, "Ignoring " + intent + " as it is not an " + ACTION_INSTALL + " intent"); + return START_NOT_STICKY; + } + String urlString = intent.getDataString(); Apk apk = ACTIVE_APKS.get(urlString); 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 a85d311ab..8dc833d29 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -30,6 +30,7 @@ import android.os.Looper; import android.os.Message; import android.os.PatternMatcher; import android.os.Process; +import android.support.v4.content.IntentCompat; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; @@ -109,14 +110,14 @@ public class DownloaderService extends Service { } @Override - public void onStart(Intent intent, int startId) { - super.onStart(intent, startId); + public int onStartCommand(Intent intent, int flags, int startId) { Utils.debugLog(TAG, "Received Intent for downloading: " + intent + " (with a startId of " + startId + ")"); String uriString = intent.getDataString(); if (uriString == null) { Log.e(TAG, "Received Intent with no URI: " + intent); - return; + return START_STICKY; } + if (ACTION_CANCEL.equals(intent.getAction())) { Utils.debugLog(TAG, "Cancelling download of " + uriString); Integer whatToRemove = uriString.hashCode(); @@ -139,26 +140,21 @@ public class DownloaderService extends Service { } else { Log.e(TAG, "Received Intent with unknown action: " + intent); } + + return START_REDELIVER_INTENT; // if killed before completion, retry Intent } public static PendingIntent getCancelPendingIntent(Context context, String urlString) { Intent cancelIntent = new Intent(context.getApplicationContext(), DownloaderService.class) .setData(Uri.parse(urlString)) .setAction(ACTION_CANCEL) - .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK); return PendingIntent.getService(context.getApplicationContext(), urlString.hashCode(), cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT); } - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - Utils.debugLog(TAG, "onStartCommand " + intent); - onStart(intent, startId); - return START_REDELIVER_INTENT; // if killed before completion, retry Intent - } - @Override public void onDestroy() { Utils.debugLog(TAG, "Destroying downloader service. Will move to background and stop our Looper.");