Made sure that downloads list is only modified on the UI thread

This commit is contained in:
daniel oeh 2013-08-19 11:27:58 +02:00
parent 467ced31ee
commit ffd1070270

View File

@ -421,52 +421,24 @@ public class DownloadService extends Service {
return null; return null;
} }
@SuppressLint("NewApi")
public void onDownloadCompleted(final Downloader downloader) {
final AsyncTask<Void, Void, Void> handlerTask = new AsyncTask<Void, Void, Void>() {
boolean successful;
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (!successful) {
queryDownloads();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
removeDownload(downloader);
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
};
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
handlerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
handlerTask.execute();
}
}
/** /**
* Remove download from the DownloadRequester list and from the * Remove download from the DownloadRequester list and from the
* DownloadService list. * DownloadService list.
*/ */
private void removeDownload(final Downloader d) { private void removeDownload(final Downloader d) {
if (AppConfig.DEBUG) handler.post(new Runnable() {
Log.d(TAG, "Removing downloader: " @Override
+ d.getDownloadRequest().getSource()); public void run() {
boolean rc = downloads.remove(d); if (AppConfig.DEBUG)
if (AppConfig.DEBUG) Log.d(TAG, "Removing downloader: "
Log.d(TAG, "Result of downloads.remove: " + rc); + d.getDownloadRequest().getSource());
DownloadRequester.getInstance().removeDownload(d.getDownloadRequest()); boolean rc = downloads.remove(d);
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); if (AppConfig.DEBUG)
Log.d(TAG, "Result of downloads.remove: " + rc);
DownloadRequester.getInstance().removeDownload(d.getDownloadRequest());
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
});
} }
/** /**