Merge remote-tracking branch 'origin/enable_cookies' into enable_cookies
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index 0c73540..e2c3c03 100644 (file)
@@ -1,6 +1,6 @@
 /* ownCloud Android client application\r
  *   Copyright (C) 2012  Bartek Przybylski\r
- *   Copyright (C) 2012-2014 ownCloud Inc.\r
+ *   Copyright (C) 2012-2015 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
  *   it under the terms of the GNU General Public License version 2,\r
 \r
 package com.owncloud.android.authentication;\r
 \r
-import java.io.IOException;\r
 import java.security.cert.X509Certificate;\r
 import java.util.Map;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
-import android.accounts.AuthenticatorException;\r
-import android.accounts.OperationCanceledException;\r
 import android.app.Dialog;\r
 import android.content.ComponentName;\r
 import android.content.Context;\r
@@ -36,6 +33,7 @@ import android.graphics.Rect;
 import android.graphics.drawable.Drawable;\r
 import android.net.Uri;\r
 import android.net.http.SslError;\r
+import android.os.AsyncTask;\r
 import android.os.Bundle;\r
 import android.os.Handler;\r
 import android.os.IBinder;\r
@@ -139,6 +137,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private static final String CREDENTIALS_DIALOG_TAG = "CREDENTIALS_DIALOG";\r
     private static final String KEY_AUTH_IS_FIRST_ATTEMPT_TAG = "KEY_AUTH_IS_FIRST_ATTEMPT";\r
 \r
+    private static final String KEY_USERNAME = "USERNAME";\r
+    private static final String KEY_PASSWORD = "PASSWORD";\r
+    private static final String KEY_ASYNC_TASK_IN_PROGRESS = "AUTH_IN_PROGRESS";\r
     \r
     /// parameters from EXTRAs in starter Intent\r
     private byte mAction;\r
@@ -182,9 +183,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private int mAuthStatusText = 0, mAuthStatusIcon = 0;\r
     \r
     private String mAuthToken = "";\r
+    private AuthenticatorAsyncTask mAsyncTask;\r
 \r
     private boolean mIsFirstAuthAttempt;\r
-\r
     \r
     /// Identifier of operation in progress which result shouldn't be lost \r
     private long mWaitingForOpId = Long.MAX_VALUE;\r
@@ -424,9 +425,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                     if (\r
                             AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(\r
                                     MainApp.getAccountType()\r
-                                    ).equals(mAuthTokenType) &&\r
-                            mHostUrlInput.hasFocus()\r
-                    ) {\r
+                            ).equals(mAuthTokenType) &&\r
+                                    mHostUrlInput.hasFocus()\r
+                            ) {\r
                         checkOcServer();\r
                     }\r
                 }\r
@@ -590,9 +591,35 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         /// authentication\r
         outState.putBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG, mIsFirstAuthAttempt);\r
 \r
+        /// AsyncTask (User and password)\r
+        outState.putString(KEY_USERNAME, mUsernameInput.getText().toString());\r
+        outState.putString(KEY_PASSWORD, mPasswordInput.getText().toString());\r
+\r
+        if (mAsyncTask != null) {\r
+            mAsyncTask.cancel(true);\r
+            outState.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS, true);\r
+        } else {\r
+            outState.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS, false);\r
+        }\r
+        mAsyncTask = null;\r
+\r
         //Log_OC.wtf(TAG, "onSaveInstanceState end" );\r
     }\r
 \r
+    @Override\r
+    public void onRestoreInstanceState(Bundle savedInstanceState) {\r
+        super.onRestoreInstanceState(savedInstanceState);\r
+\r
+        // AsyncTask\r
+        boolean inProgress = savedInstanceState.getBoolean(KEY_ASYNC_TASK_IN_PROGRESS);\r
+        if (inProgress){\r
+            mAsyncTask = new AuthenticatorAsyncTask(this);\r
+            String username = savedInstanceState.getString(KEY_USERNAME);\r
+            String password = savedInstanceState.getString(KEY_PASSWORD);\r
+            String[] params = {mServerInfo.mBaseUrl, username, password, mAuthToken, mAuthTokenType};\r
+            mAsyncTask.execute(params);\r
+        }\r
+    }\r
 \r
     /**\r
      * The redirection triggered by the OAuth authentication server as response to the \r
@@ -893,9 +920,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mAccountMgr.setUserData(mAccount, Constants.KEY_COOKIES, null);\r
         }\r
 \r
-        AuthenticatorAsyncTask asyncTask = new AuthenticatorAsyncTask(this);\r
+        mAsyncTask = new AuthenticatorAsyncTask(this);\r
         String[] params = { mServerInfo.mBaseUrl, username, password, mAuthToken, mAuthTokenType};\r
-        asyncTask.execute(params);\r
+        mAsyncTask.execute(params);\r
 \r
     }\r
 \r
@@ -1433,10 +1460,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * the new credentials when needed.\r
      */\r
     private void updateAccountAuthentication() throws AccountNotFoundException {\r
-\r
-        OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(\r
-            new OwnCloudAccount(mAccount, this)\r
-        );\r
+        try {\r
+            OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(\r
+                    new OwnCloudAccount(mAccount, this)\r
+            );\r
+        } catch (Exception e) {\r
+            Log_OC.e(TAG, "Exception", e);\r
+        }\r
 \r
         Bundle response = new Bundle();\r
         response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);\r