From 4f6910fca7e8c4b2af177970277b3d5f6f2355be Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Thu, 12 May 2016 12:01:53 +1000 Subject: [PATCH 1/3] Rename 'startDownload' to 'initiateInstall'. This is more apt given the change from `DownloaderService` to `InstallManagerService`. --- app/src/main/java/org/fdroid/fdroid/AppDetails.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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(); From ef75f30701fddb0704ffd1d82a7253419db6418c Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Thu, 12 May 2016 12:11:07 +1000 Subject: [PATCH 2/3] Ignore intents other than ACTION_INSTALL --- .../org/fdroid/fdroid/installer/InstallManagerService.java | 7 +++++++ 1 file changed, 7 insertions(+) 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..ab798de11 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; @@ -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); From da8a142510e523d50913466acbd30f62b7292742 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Sun, 15 May 2016 08:33:29 +1000 Subject: [PATCH 3/3] Fix Android Studio warnings. Type parameters can be ommited if defined and declared in same statement. `onStart()` is deprecated and not required, as we target APIs > 5. `Intent.FLAG_ACTIVITY_CLEAR_TASK` is not supported on APIs < 11 but we target 8. --- .../installer/InstallManagerService.java | 8 ++++---- .../fdroid/fdroid/net/DownloaderService.java | 18 +++++++----------- 2 files changed, 11 insertions(+), 15 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 ab798de11..127445e52 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/InstallManagerService.java @@ -66,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 ACTIVE_APKS = new HashMap(3); + private static final HashMap 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 ACTIVE_APPS = new HashMap(3); + private static final HashMap 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 receivers = new HashMap(3); + private final HashMap receivers = new HashMap<>(3); /** * Get the app name based on a {@code urlString} key. The app name needs @@ -91,7 +91,7 @@ public class InstallManagerService extends Service { *

* TODO delete me once InstallerService exists */ - private static final HashMap TEMP_HACK_APP_NAMES = new HashMap(3); + private static final HashMap TEMP_HACK_APP_NAMES = new HashMap<>(3); private LocalBroadcastManager localBroadcastManager; private NotificationManager notificationManager; 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.");