From b51aaf2a921bbb6446f55067d706d72087d466d2 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Tue, 24 Jul 2012 17:59:04 +0200 Subject: fixed nullpointer exception --- .../antennapod/storage/DownloadRequester.java | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src/de/danoeh/antennapod/storage/DownloadRequester.java') diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index 356201a2d..90c481ebb 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -57,16 +57,20 @@ public class DownloadRequester {// TODO handle externalstorage missing private long download(Context context, FeedFile item, File dest) { if (!isDownloadingFile(item)) { if (dest.exists()) { - if (AppConfig.DEBUG) Log.d(TAG, "File already exists. Deleting !"); + if (AppConfig.DEBUG) + Log.d(TAG, "File already exists. Deleting !"); dest.delete(); } - if (AppConfig.DEBUG) Log.d(TAG, "Requesting download of url " + item.getDownload_url()); + if (AppConfig.DEBUG) + Log.d(TAG, + "Requesting download of url " + item.getDownload_url()); downloads.add(item); item.setDownload_url(URLChecker.prepareURL(item.getDownload_url())); DownloadManager.Request request = new DownloadManager.Request( Uri.parse(item.getDownload_url())).setDestinationUri(Uri .fromFile(dest)); - if (AppConfig.DEBUG) Log.d(TAG, "Version is " + currentApi); + if (AppConfig.DEBUG) + Log.d(TAG, "Version is " + currentApi); if (currentApi >= 11) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); } else { @@ -85,7 +89,8 @@ public class DownloadRequester {// TODO handle externalstorage missing context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED)); return downloadId; } else { - Log.e(TAG, "URL " + item.getDownload_url() + " is already being downloaded"); + Log.e(TAG, "URL " + item.getDownload_url() + + " is already being downloaded"); return 0; } } @@ -115,7 +120,8 @@ public class DownloadRequester {// TODO handle externalstorage missing * ID of the download to cancel * */ public void cancelDownload(final Context context, final long id) { - if (AppConfig.DEBUG) Log.d(TAG, "Cancelling download with id " + id); + if (AppConfig.DEBUG) + Log.d(TAG, "Cancelling download with id " + id); DownloadManager dm = (DownloadManager) context .getSystemService(Context.DOWNLOAD_SERVICE); int removed = dm.remove(id); @@ -132,7 +138,8 @@ public class DownloadRequester {// TODO handle externalstorage missing /** Cancels all running downloads */ public void cancelAllDownloads(Context context) { - if (AppConfig.DEBUG) Log.d(TAG, "Cancelling all running downloads"); + if (AppConfig.DEBUG) + Log.d(TAG, "Cancelling all running downloads"); DownloadManager dm = (DownloadManager) context .getSystemService(Context.DOWNLOAD_SERVICE); for (FeedFile f : downloads) { @@ -173,11 +180,11 @@ public class DownloadRequester {// TODO handle externalstorage missing } return false; } - + public boolean hasNoDownloads() { return downloads.isEmpty(); } - + public FeedFile getDownloadAt(int index) { return downloads.get(index); } @@ -235,9 +242,12 @@ public class DownloadRequester {// TODO handle externalstorage missing private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { mService = ((DownloadService.LocalBinder) service).getService(); - if (AppConfig.DEBUG) Log.d(TAG, "Connection to service established"); + if (AppConfig.DEBUG) + Log.d(TAG, "Connection to service established"); mService.queryDownloads(); - mContext.unbindService(mConnection); + if (mContext != null) { + mContext.unbindService(mConnection); + } } public void onServiceDisconnected(ComponentName className) { @@ -250,9 +260,9 @@ public class DownloadRequester {// TODO handle externalstorage missing /** Notifies the DownloadService to check if there are any Downloads left */ public void notifyDownloadService(Context context) { + mContext = context; context.bindService(new Intent(context, DownloadService.class), mConnection, Context.BIND_AUTO_CREATE); - mContext = context; mIsBound = true; } } -- cgit v1.2.3