Merge tag 'oc-android-1-3-22' into oauth_login
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / FileSyncAdapter.java
index 6da3e81..ed9e84a 100644 (file)
@@ -20,7 +20,6 @@
 package com.owncloud.android.syncadapter;\r
 \r
 import java.io.IOException;\r
-import java.net.UnknownHostException;\r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
 import java.util.List;\r
@@ -36,8 +35,11 @@ import com.owncloud.android.operations.RemoteOperationResult;
 import com.owncloud.android.operations.SynchronizeFolderOperation;\r
 import com.owncloud.android.operations.UpdateOCVersionOperation;\r
 import com.owncloud.android.operations.RemoteOperationResult.ResultCode;\r
+import com.owncloud.android.ui.activity.AuthenticatorActivity;\r
 import com.owncloud.android.ui.activity.ErrorsWhileCopyingHandlerActivity;\r
+\r
 import android.accounts.Account;\r
+import android.accounts.AccountsException;\r
 import android.app.Notification;\r
 import android.app.NotificationManager;\r
 import android.app.PendingIntent;\r
@@ -103,7 +105,12 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));\r
         try {\r
             this.initClientForCurrentAccount();\r
-        } catch (UnknownHostException e) {\r
+        } catch (IOException e) {\r
+            /// the account is unknown for the Synchronization Manager, or unreachable for this context; don't try this again\r
+            mSyncResult.tooManyRetries = true;\r
+            notifyFailedSynchronization();\r
+            return;\r
+        } catch (AccountsException e) {\r
             /// the account is unknown for the Synchronization Manager, or unreachable for this context; don't try this again\r
             mSyncResult.tooManyRetries = true;\r
             notifyFailedSynchronization();\r
@@ -242,6 +249,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             RemoteOperationResult.ResultCode code = failedResult.getCode();\r
             return (code.equals(RemoteOperationResult.ResultCode.SSL_ERROR) ||\r
                     code.equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) ||\r
+                    code.equals(RemoteOperationResult.ResultCode.UNAUTHORIZED) ||\r
                     code.equals(RemoteOperationResult.ResultCode.BAD_OC_VERSION) ||\r
                     code.equals(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED));\r
         }\r
@@ -292,12 +300,29 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
     private void notifyFailedSynchronization() {\r
         Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_fail_ticker), System.currentTimeMillis());\r
         notification.flags |= Notification.FLAG_AUTO_CANCEL;\r
-        // TODO put something smart in the contentIntent below\r
+        boolean needsToUpdateCredentials = (mLastFailedResult != null && mLastFailedResult.getCode() == ResultCode.UNAUTHORIZED);\r
+        // TODO put something smart in the contentIntent below for all the possible errors\r
         notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);\r
-        notification.setLatestEventInfo(getContext().getApplicationContext(), \r
-                                        getContext().getString(R.string.sync_fail_ticker), \r
-                                        String.format(getContext().getString(R.string.sync_fail_content), getAccount().name), \r
-                                        notification.contentIntent);\r
+        if (needsToUpdateCredentials) {\r
+            // let the user update credentials with one click\r
+            Intent updateAccountCredentials = new Intent(getContext(), AuthenticatorActivity.class);\r
+            updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());\r
+            updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);\r
+            updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\r
+            updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);\r
+            updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);\r
+            notification.contentIntent = PendingIntent.getActivity(getContext(), (int)System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT);\r
+            notification.setLatestEventInfo(getContext().getApplicationContext(), \r
+                    getContext().getString(R.string.sync_fail_ticker), \r
+                    String.format(getContext().getString(R.string.sync_fail_content_unauthorized), getAccount().name), \r
+                    notification.contentIntent);\r
+            Log.e(TAG, "NEEDS TO UPDATE CREDENTIALS");\r
+        } else {\r
+            notification.setLatestEventInfo(getContext().getApplicationContext(), \r
+                    getContext().getString(R.string.sync_fail_ticker), \r
+                    String.format(getContext().getString(R.string.sync_fail_content), getAccount().name), \r
+                    notification.contentIntent);\r
+        }\r
         ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_ticker, notification);\r
     }\r
 \r