fixed nullpointer exception

This commit is contained in:
daniel oeh 2012-07-24 17:59:04 +02:00
parent 7d99031706
commit b51aaf2a92
3 changed files with 51 additions and 19 deletions

View File

@ -43,7 +43,8 @@ public class DownloadActivity extends SherlockListActivity implements
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (AppConfig.DEBUG) Log.d(TAG, "Creating Activity");
if (AppConfig.DEBUG)
Log.d(TAG, "Creating Activity");
requester = DownloadRequester.getInstance();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -61,14 +62,16 @@ public class DownloadActivity extends SherlockListActivity implements
@Override
protected void onResume() {
super.onResume();
if (AppConfig.DEBUG) Log.d(TAG, "Trying to bind service");
if (AppConfig.DEBUG)
Log.d(TAG, "Trying to bind service");
bindService(new Intent(this, DownloadService.class), mConnection, 0);
}
@Override
protected void onStop() {
super.onStop();
if (AppConfig.DEBUG) Log.d(TAG, "Stopping Activity");
if (AppConfig.DEBUG)
Log.d(TAG, "Stopping Activity");
}
@Override
@ -162,7 +165,8 @@ public class DownloadActivity extends SherlockListActivity implements
public void onServiceConnected(ComponentName className, IBinder service) {
downloadService = ((DownloadService.LocalBinder) service)
.getService();
if (AppConfig.DEBUG) Log.d(TAG, "Connection to service established");
if (AppConfig.DEBUG)
Log.d(TAG, "Connection to service established");
dla = new DownloadlistAdapter(DownloadActivity.this, 0,
downloadService.getDownloadObserver().getStatusList());
setListAdapter(dla);
@ -179,13 +183,28 @@ public class DownloadActivity extends SherlockListActivity implements
@Override
public void onProgressUpdate() {
dla.notifyDataSetChanged();
runOnUiThread(new Runnable() {
@Override
public void run() {
dla.notifyDataSetChanged();
}
});
}
@Override
public void onFinish() {
if (AppConfig.DEBUG) Log.d(TAG, "Observer has finished, clearing adapter");
dla.clear();
dla.notifyDataSetInvalidated();
if (AppConfig.DEBUG)
Log.d(TAG, "Observer has finished, clearing adapter");
runOnUiThread(new Runnable() {
@Override
public void run() {
dla.clear();
dla.notifyDataSetInvalidated();
}
});
}
}

View File

@ -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) {
@ -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;
}
}

View File

@ -71,6 +71,9 @@ public class TypeGetter {
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
return reader;
}