From: David A. Velasco Date: Mon, 18 Mar 2013 14:05:35 +0000 (+0100) Subject: Refresh authorization token on failed downloads / uploads when notification error... X-Git-Tag: oc-android-1.4.3~29^2~10 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/4861636185cb8b589fa1e5dfa51ea3f135757d55?ds=inline;hp=--cc Refresh authorization token on failed downloads / uploads when notification error is clicked --- 4861636185cb8b589fa1e5dfa51ea3f135757d55 diff --git a/src/com/owncloud/android/files/services/FileDownloader.java b/src/com/owncloud/android/files/services/FileDownloader.java index d813aaf3..0d7a26b1 100644 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@ -34,6 +34,8 @@ import eu.alefzero.webdav.OnDatatransferProgressListener; import com.owncloud.android.network.OwnCloudClientUtils; import com.owncloud.android.operations.DownloadFileOperation; import com.owncloud.android.operations.RemoteOperationResult; +import com.owncloud.android.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.ui.activity.AuthenticatorActivity; import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.ui.fragment.FileDetailFragment; @@ -387,9 +389,27 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis int contentId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content; Notification finalNotification = new Notification(R.drawable.icon, getString(tickerId), System.currentTimeMillis()); finalNotification.flags |= Notification.FLAG_AUTO_CANCEL; - // TODO put something smart in the contentIntent below - finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); - finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), new File(download.getSavePath()).getName()), finalNotification.contentIntent); + boolean needsToUpdateCredentials = (downloadResult.getCode() == ResultCode.UNAUTHORIZED); + if (needsToUpdateCredentials) { + // let the user update credentials with one click + Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class); + updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, download.getAccount()); + updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN); + updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND); + finalNotification.contentIntent = PendingIntent.getActivity(this, (int)System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT); + finalNotification.setLatestEventInfo( getApplicationContext(), + getString(tickerId), + String.format(getString(contentId), new File(download.getSavePath()).getName()), + finalNotification.contentIntent); + mDownloadClient = null; // grant that future retries on the same account will get the fresh credentials + + } else { + // TODO put something smart in the contentIntent below + finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); + finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), new File(download.getSavePath()).getName()), finalNotification.contentIntent); + } mNotificationManager.notify(tickerId, finalNotification); } } diff --git a/src/com/owncloud/android/files/services/FileUploader.java b/src/com/owncloud/android/files/services/FileUploader.java index 32aa576b..09988fba 100644 --- a/src/com/owncloud/android/files/services/FileUploader.java +++ b/src/com/owncloud/android/files/services/FileUploader.java @@ -41,6 +41,7 @@ import com.owncloud.android.operations.RemoteOperation; import com.owncloud.android.operations.RemoteOperationResult; import com.owncloud.android.operations.UploadFileOperation; import com.owncloud.android.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.ui.activity.AuthenticatorActivity; import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.ui.fragment.FileDetailFragment; import com.owncloud.android.utils.OwnCloudVersion; @@ -662,8 +663,20 @@ public class FileUploader extends Service implements OnDatatransferProgressListe mNotificationManager.cancel(R.string.uploader_upload_in_progress_ticker); Notification finalNotification = new Notification(R.drawable.icon, getString(R.string.uploader_upload_failed_ticker), System.currentTimeMillis()); finalNotification.flags |= Notification.FLAG_AUTO_CANCEL; - // TODO put something smart in the contentIntent below - finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); + if (uploadResult.getCode() == ResultCode.UNAUTHORIZED) { + // let the user update credentials with one click + Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class); + updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, upload.getAccount()); + updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN); + updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND); + finalNotification.contentIntent = PendingIntent.getActivity(this, (int)System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT); + mUploadClient = null; // grant that future retries on the same account will get the fresh credentials + } else { + // TODO put something smart in the contentIntent below + finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); + } String content = null; if (uploadResult.getCode() == ResultCode.LOCAL_STORAGE_FULL || diff --git a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java index ed9e84ae..f78ed242 100644 --- a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java @@ -316,7 +316,6 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { getContext().getString(R.string.sync_fail_ticker), String.format(getContext().getString(R.string.sync_fail_content_unauthorized), getAccount().name), notification.contentIntent); - Log.e(TAG, "NEEDS TO UPDATE CREDENTIALS"); } else { notification.setLatestEventInfo(getContext().getApplicationContext(), getContext().getString(R.string.sync_fail_ticker),