Cleaned up logging in downloader service.

Most of the logging is purely for debugging purposes during development.
As such, it has been moved to `Utils.debugLog`. Also provided more context
in some of the descriptions, so devs reading the logs without the sourcecode
will hopefully be able to infer more about what is happening. Left the error
logging as `Log.e` as it may be more informative.
This commit is contained in:
Peter Serwylo 2016-04-22 23:22:32 +10:00
parent a149ce54db
commit 3ebeec0b87
2 changed files with 11 additions and 12 deletions

View File

@ -42,12 +42,12 @@ public class DownloadCompleteService extends IntentService {
if (intent != null) { if (intent != null) {
final String action = intent.getAction(); final String action = intent.getAction();
if (!ACTION_NOTIFY.equals(action)) { if (!ACTION_NOTIFY.equals(action)) {
Utils.debugLog(TAG, "intent action is not ACTION_NOTIFY"); Utils.debugLog(TAG, "Intent action is not ACTION_NOTIFY");
return; return;
} }
String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME); String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
if (TextUtils.isEmpty(packageName)) { if (TextUtils.isEmpty(packageName)) {
Utils.debugLog(TAG, "intent is missing EXTRA_PACKAGE_NAME"); Utils.debugLog(TAG, "Intent is missing EXTRA_PACKAGE_NAME");
return; return;
} }

View File

@ -112,7 +112,7 @@ public class DownloaderService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Log.i(TAG, "onCreate"); Utils.debugLog(TAG, "Creating downloader service.");
HandlerThread thread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND); HandlerThread thread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
thread.start(); thread.start();
@ -125,20 +125,20 @@ public class DownloaderService extends Service {
@Override @Override
public void onStart(Intent intent, int startId) { public void onStart(Intent intent, int startId) {
super.onStart(intent, startId); super.onStart(intent, startId);
Log.i(TAG, "onStart " + startId + " " + intent); Utils.debugLog(TAG, "Received Intent for downloading: " + intent + " (with a startId of " + startId + ")");
String uriString = intent.getDataString(); String uriString = intent.getDataString();
if (uriString == null) { if (uriString == null) {
Log.e(TAG, "Received Intent with no URI: " + intent); Log.e(TAG, "Received Intent with no URI: " + intent);
return; return;
} }
if (ACTION_CANCEL.equals(intent.getAction())) { if (ACTION_CANCEL.equals(intent.getAction())) {
Log.i(TAG, "Removed " + intent); Utils.debugLog(TAG, "Cancelling download of " + uriString);
if (isQueued(uriString)) { if (isQueued(uriString)) {
serviceHandler.removeMessages(what); serviceHandler.removeMessages(what);
} else if (isActive(uriString)) { } else if (isActive(uriString)) {
downloader.cancelDownload(); downloader.cancelDownload();
} else { } else {
Log.e(TAG, "CANCEL called on something not queued or running: " + startId + " " + intent); Log.e(TAG, "ACTION_CANCEL called on something not queued or running");
} }
QUEUE_WHATS.remove(uriString); QUEUE_WHATS.remove(uriString);
@ -146,14 +146,13 @@ public class DownloaderService extends Service {
stopForeground(true); stopForeground(true);
} }
} else if (ACTION_QUEUE.equals(intent.getAction())) { } else if (ACTION_QUEUE.equals(intent.getAction())) {
Log.i(TAG, "Queued " + intent);
Message msg = serviceHandler.obtainMessage(); Message msg = serviceHandler.obtainMessage();
msg.arg1 = startId; msg.arg1 = startId;
msg.obj = intent; msg.obj = intent;
msg.what = what++; msg.what = what++;
serviceHandler.sendMessage(msg); serviceHandler.sendMessage(msg);
Log.i(TAG, "QUEUE_WHATS.put(" + uriString + ", " + msg.what);
QUEUE_WHATS.put(uriString, msg.what); QUEUE_WHATS.put(uriString, msg.what);
Utils.debugLog(TAG, "Queued download of " + uriString + ". Now " + QUEUE_WHATS.size() + " downloads in the queue");
} else { } else {
Log.e(TAG, "Received Intent with unknown action: " + intent); Log.e(TAG, "Received Intent with unknown action: " + intent);
} }
@ -215,13 +214,13 @@ public class DownloaderService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId); onStart(intent, startId);
Log.i(TAG, "onStartCommand " + intent); Utils.debugLog(TAG, "onStartCommand " + intent);
return START_REDELIVER_INTENT; // if killed before completion, retry Intent return START_REDELIVER_INTENT; // if killed before completion, retry Intent
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.i(TAG, "onDestroy"); Utils.debugLog(TAG, "Destroying downloader service. Will move to background and stop our Looper.");
stopForeground(true); stopForeground(true);
serviceLooper.quit(); //NOPMD - this is copied from IntentService, no super call needed serviceLooper.quit(); //NOPMD - this is copied from IntentService, no super call needed
} }
@ -331,7 +330,7 @@ public class DownloaderService extends Service {
* @see #cancel(Context, String) * @see #cancel(Context, String)
*/ */
public static void queue(Context context, String packageName, String urlString) { public static void queue(Context context, String packageName, String urlString) {
Log.i(TAG, "queue " + urlString); Utils.debugLog(TAG, "Preparing " + urlString + " to go into the download queue");
Intent intent = new Intent(context, DownloaderService.class); Intent intent = new Intent(context, DownloaderService.class);
intent.setAction(ACTION_QUEUE); intent.setAction(ACTION_QUEUE);
intent.setData(Uri.parse(urlString)); intent.setData(Uri.parse(urlString));
@ -351,7 +350,7 @@ public class DownloaderService extends Service {
* @see #queue(Context, String, String) * @see #queue(Context, String, String)
*/ */
public static void cancel(Context context, String urlString) { public static void cancel(Context context, String urlString) {
Log.i(TAG, "cancel " + urlString); Utils.debugLog(TAG, "Preparing cancellation of " + urlString + " download");
Intent intent = new Intent(context, DownloaderService.class); Intent intent = new Intent(context, DownloaderService.class);
intent.setAction(ACTION_CANCEL); intent.setAction(ACTION_CANCEL);
intent.setData(Uri.parse(urlString)); intent.setData(Uri.parse(urlString));