From: masensio Date: Thu, 19 Feb 2015 11:50:53 +0000 (+0100) Subject: Try to cancel transfers when an account is deleted outside of the app: Done X-Git-Tag: oc-android-1.7.1_signed^2~40^2~8 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/e93016d7e01ba4917fbf89193ac37373350dbb52 Try to cancel transfers when an account is deleted outside of the app: Done --- diff --git a/src/com/owncloud/android/files/services/FileDownloader.java b/src/com/owncloud/android/files/services/FileDownloader.java index 4ae3325d..efe271d8 100644 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@ -235,7 +235,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis ); Log_OC.v( "NOW " + TAG + ", thread " + Thread.currentThread().getName(), "Removing download of " + file.getRemotePath());*/ - Pair removeResult = mPendingDownloads.remove(account, file.getRemotePath()); + Pair removeResult = + mPendingDownloads.remove(account, file.getRemotePath()); DownloadFileOperation download = removeResult.first; if (download != null) { /*Log_OC.v( "NOW " + TAG + ", thread " + Thread.currentThread().getName(), @@ -352,6 +353,17 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName); } } + + /** + * Review downloads and cancel it if its account doesn't exist + */ + public void reviewDownloads() { + if (mCurrentDownload != null && + !AccountUtils.exists(mCurrentDownload.getAccount(), getApplicationContext())) { + mCurrentDownload.cancel(); + } + // The rest of downloads are cancelled when they try to start + } } diff --git a/src/com/owncloud/android/files/services/FileUploader.java b/src/com/owncloud/android/files/services/FileUploader.java index b076d6f4..3ce1aace 100644 --- a/src/com/owncloud/android/files/services/FileUploader.java +++ b/src/com/owncloud/android/files/services/FileUploader.java @@ -459,6 +459,16 @@ public class FileUploader extends Service implements OnDatatransferProgressListe } } + /** + * Review uploads and cancel it if its account doesn't exist + */ + public void reviewUploads() { + if (mCurrentUpload != null && + !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) { + mCurrentUpload.cancel(); + } + // The rest of uploads are cancelled when they try to start + } } /** diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 4f0e5bcf..578c5963 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -23,6 +23,7 @@ import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.accounts.AuthenticatorException; +import android.accounts.OnAccountsUpdateListener; import android.accounts.OperationCanceledException; import android.content.ComponentName; import android.content.Context; @@ -68,8 +69,8 @@ import com.owncloud.android.utils.ErrorMessageAdapter; * * @author David A. Velasco */ -public class FileActivity extends SherlockFragmentActivity -implements OnRemoteOperationListener, ComponentsGetter { +public class FileActivity extends SherlockFragmentActivity + implements OnRemoteOperationListener, ComponentsGetter, OnAccountsUpdateListener { public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE"; public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT"; @@ -79,7 +80,7 @@ implements OnRemoteOperationListener, ComponentsGetter { public static final String TAG = FileActivity.class.getSimpleName(); private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; - private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID";; + private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID"; protected static final long DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS = 200; @@ -158,6 +159,10 @@ implements OnRemoteOperationListener, ComponentsGetter { if (mUploadServiceConnection != null) { bindService(new Intent(this, FileUploader.class), mUploadServiceConnection, Context.BIND_AUTO_CREATE); } + + // add AccountsUpdatedListener + AccountManager am = AccountManager.get(getApplicationContext()); + am.addOnAccountsUpdatedListener(this, null, false); } @@ -221,6 +226,11 @@ implements OnRemoteOperationListener, ComponentsGetter { unbindService(mUploadServiceConnection); mUploadServiceConnection = null; } + + // remove AccountsUpdatedListener + AccountManager am = AccountManager.get(getApplicationContext()); + am.removeOnAccountsUpdatedListener(this); + super.onDestroy(); } @@ -256,8 +266,6 @@ implements OnRemoteOperationListener, ComponentsGetter { * to create a new ownCloud {@link Account}. * * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}. - * - * @return 'True' if the checked {@link Account} was valid. */ private void swapToDefaultAccount() { // default to the most recently used account @@ -289,6 +297,7 @@ implements OnRemoteOperationListener, ComponentsGetter { this, new AccountCreationCallback(), null); + am.addOnAccountsUpdatedListener(this, null, true); } @@ -356,7 +365,15 @@ implements OnRemoteOperationListener, ComponentsGetter { protected ServiceConnection newTransferenceServiceConnection() { return null; } - + + @Override + public void onAccountsUpdated(Account[] accounts) { + // detect a change in the list of accounts + Log_OC.d(TAG, "onAccountsUpdated"); + mDownloaderBinder.reviewDownloads(); + mUploaderBinder.reviewUploads(); + } + /** * Helper class handling a callback from the {@link AccountManager} after the creation of