Skip to content

Commit

Permalink
Download service: abort connection in a new thread (fix #245)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Jan 11, 2015
1 parent 76d8cac commit 2e97f86
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions app/src/main/java/org/kontalk/service/DownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

Expand Down Expand Up @@ -106,30 +107,28 @@ public int onStartCommand(Intent intent, int flags, int startId) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (ACTION_DOWNLOAD_ABORT.equals(intent.getAction())) {
String url = intent.getData().toString();
Long msgId = sQueue.get(url);
if (msgId != null) {
// interrupt worker if running
if (msgId.longValue() == mMessageId) {
mDownloadClient.abort();
mCanceled = true;
final Uri uri = intent.getData();
new Thread(new Runnable() {
@Override
public void run() {
onDownloadAbort(uri);
}
// remove from queue - will never be processed
else
sQueue.remove(url);
}
return START_NOT_STICKY;
}).start();
}

return super.onStartCommand(intent, flags, startId);
}

@Override
protected void onHandleIntent(Intent intent) {
// unknown action
if (!ACTION_DOWNLOAD_URL.equals(intent.getAction())) return;
String action = intent.getAction();

Uri uri = intent.getData();
if (ACTION_DOWNLOAD_URL.equals(action)) {
onDownloadURL(intent.getData(), intent.getExtras());
}
}

private void onDownloadURL(Uri uri, Bundle args) {
String url = uri.toString();

// check if download has already been queued
Expand Down Expand Up @@ -160,16 +159,16 @@ protected void onHandleIntent(Intent intent) {
// check if external storage is available
if (!MediaStorage.isExternalStorageAvailable()) {
errorNotification(getString(R.string.notify_ticker_external_storage),
getString(R.string.notify_text_external_storage));
getString(R.string.notify_text_external_storage));
return;
}

// make sure storage directory is present
MediaStorage.MEDIA_ROOT.mkdirs();

mMessageId = intent.getLongExtra(CompositeMessage.MSG_ID, 0);
mPeer = intent.getStringExtra(CompositeMessage.MSG_SENDER);
mEncrypted = intent.getBooleanExtra(CompositeMessage.MSG_ENCRYPTED, false);
mMessageId = args.getLong(CompositeMessage.MSG_ID, 0);
mPeer = args.getString(CompositeMessage.MSG_SENDER);
mEncrypted = args.getBoolean(CompositeMessage.MSG_ENCRYPTED, false);
sQueue.put(url, mMessageId);

// download content
Expand All @@ -185,6 +184,21 @@ protected void onHandleIntent(Intent intent) {
}
}

private void onDownloadAbort(Uri uri) {
String url = uri.toString();
Long msgId = sQueue.get(url);
if (msgId != null) {
// interrupt worker if running
if (msgId == mMessageId) {
mDownloadClient.abort();
mCanceled = true;
}
// remove from queue - will never be processed
else
sQueue.remove(url);
}
}

public void startForeground(long totalBytes) {
Log.d(TAG, "starting foreground progress notification");
mTotalBytes = totalBytes;
Expand Down

0 comments on commit 2e97f86

Please sign in to comment.