From 7385d320b42af960be63c9c179e1cbf186c1398a Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 30 May 2016 12:05:19 +0200 Subject: [PATCH] fix crash when UpdateService receives null Intent IntentServices can get a null Intent if they are restarted after being killed. So this should be properly handled. "[The intent] may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY." https://developer.android.com/reference/android/app/IntentService.html#onStartCommand(android.content.Intent,%20int,%20int) ANDROID_VERSION=5.1.1 APP_VERSION_NAME=0.99.2 BRAND=samsung PHONE_MODEL=SM-G901F java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference at org.fdroid.fdroid.UpdateService.onHandleIntent(UpdateService.java:342) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.os.HandlerThread.run(HandlerThread.java:61) --- .../org/fdroid/fdroid/CleanCacheService.java | 3 +++ .../java/org/fdroid/fdroid/UpdateService.java | 8 ++++-- .../fdroid/fdroid/net/DownloaderService.java | 25 +++++++++++++------ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java index 1f817f03e..cea679d9f 100644 --- a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java +++ b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java @@ -47,6 +47,9 @@ public class CleanCacheService extends IntentService { @Override protected void onHandleIntent(Intent intent) { + if (intent == null) { + return; + } Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); Utils.clearOldFiles(Utils.getApkCacheDir(this), Preferences.get().getKeepCacheTime()); deleteStrayIndexFiles(); diff --git a/app/src/main/java/org/fdroid/fdroid/UpdateService.java b/app/src/main/java/org/fdroid/fdroid/UpdateService.java index bfe7f1ff8..be0565afa 100644 --- a/app/src/main/java/org/fdroid/fdroid/UpdateService.java +++ b/app/src/main/java/org/fdroid/fdroid/UpdateService.java @@ -313,8 +313,12 @@ public class UpdateService extends IntentService { Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); final long startTime = System.currentTimeMillis(); - String address = intent.getStringExtra(EXTRA_ADDRESS); - boolean manualUpdate = intent.getBooleanExtra(EXTRA_MANUAL_UPDATE, false); + boolean manualUpdate = false; + String address = null; + if (intent != null) { + address = intent.getStringExtra(EXTRA_ADDRESS); + manualUpdate = intent.getBooleanExtra(EXTRA_MANUAL_UPDATE, false); + } try { // See if it's time to actually do anything yet... 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 8dc833d29..d3e7e6e90 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -33,7 +33,6 @@ import android.os.Process; import android.support.v4.content.IntentCompat; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; -import android.util.Log; import org.fdroid.fdroid.ProgressListener; import org.fdroid.fdroid.Utils; @@ -46,9 +45,14 @@ import java.net.URL; /** * DownloaderService is a service that handles asynchronous download requests * (expressed as {@link Intent}s) on demand. Clients send download requests - * through {@link android.content.Context#startService(Intent)} calls; the - * service is started as needed, handles each Intent in turn using a worker - * thread, and stops itself when it runs out of work. + * through {@link #queue(Context, String)} calls. The + * service is started as needed, it handles each {@code Intent} using a worker + * thread, and stops itself when it runs out of work. Requests can be canceled + * using {@link #cancel(Context, String)}. If this service is killed during + * operation, it will receive the queued {@link #queue(Context, String)} and + * {@link #cancel(Context, String)} requests again due to + * {@link Service#START_REDELIVER_INTENT}. Bad requests will be ignored, + * including on restart after killing via {@link Service#START_NOT_STICKY}. *

* This "work queue processor" pattern is commonly used to offload tasks * from an application's main thread. The DownloaderService class exists to @@ -112,10 +116,15 @@ public class DownloaderService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Utils.debugLog(TAG, "Received Intent for downloading: " + intent + " (with a startId of " + startId + ")"); + + if (intent == null) { + return START_NOT_STICKY; + } + String uriString = intent.getDataString(); if (uriString == null) { - Log.e(TAG, "Received Intent with no URI: " + intent); - return START_STICKY; + Utils.debugLog(TAG, "Received Intent with no URI: " + intent); + return START_NOT_STICKY; } if (ACTION_CANCEL.equals(intent.getAction())) { @@ -128,7 +137,7 @@ public class DownloaderService extends Service { } else if (isActive(uriString)) { downloader.cancelDownload(); } else { - Log.e(TAG, "ACTION_CANCEL called on something not queued or running (expected to find message with ID of " + whatToRemove + " in queue)."); + Utils.debugLog(TAG, "ACTION_CANCEL called on something not queued or running (expected to find message with ID of " + whatToRemove + " in queue)."); } } else if (ACTION_QUEUE.equals(intent.getAction())) { Message msg = serviceHandler.obtainMessage(); @@ -138,7 +147,7 @@ public class DownloaderService extends Service { serviceHandler.sendMessage(msg); Utils.debugLog(TAG, "Queued download of " + uriString); } else { - Log.e(TAG, "Received Intent with unknown action: " + intent); + Utils.debugLog(TAG, "Received Intent with unknown action: " + intent); } return START_REDELIVER_INTENT; // if killed before completion, retry Intent