Merge remote-tracking branch 'origin/enable_cookies' into enable_cookies
authormasensio <masensio@solidgear.es>
Fri, 13 Feb 2015 13:39:35 +0000 (14:39 +0100)
committermasensio <masensio@solidgear.es>
Fri, 13 Feb 2015 13:39:35 +0000 (14:39 +0100)
Conflicts:
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/authentication/AuthenticatorAsyncTask.java

1  2 
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/authentication/AuthenticatorAsyncTask.java

@@@ -1,6 -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
@@@ -33,7 -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
@@@ -65,7 -67,10 +65,10 @@@ import com.actionbarsherlock.app.Sherlo
  import com.owncloud.android.MainApp;\r
  import com.owncloud.android.R;\r
  import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;\r
+ import com.owncloud.android.lib.common.OwnCloudAccount;\r
+ import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;\r
  import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
+ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;\r
  import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
  import com.owncloud.android.lib.common.network.CertificateCombinedException;\r
  import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;\r
@@@ -134,9 -139,6 +137,9 @@@ public class AuthenticatorActivity exte
      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
      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
                      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
          /// 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
              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
                  if (!mUsernameInput.getText().toString().equals(username)) {\r
                      // fail - not a new account, but an existing one; disallow\r
                      result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME);\r
-                     /*\r
-                     OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(\r
-                             new OwnCloudAccount(\r
-                                     Uri.parse(mServerInfo.mBaseUrl),\r
-                                     OwnCloudCredentialsFactory.newSamlSsoCredentials(mAuthToken))\r
-                             );\r
-                             */\r
                      mAuthToken = "";\r
                      updateAuthStatusIconAndText(result);\r
                      showAuthStatus();\r
                      Log_OC.d(TAG, result.getLogMessage());\r
                  } else {\r
-                     updateToken();\r
-                     success = true;\r
+                     try {\r
+                         updateAccountAuthentication();\r
+                         success = true;\r
\r
+                     } catch (AccountNotFoundException e) {\r
+                         Log_OC.e(TAG, "Account " + mAccount + " was removed!", e);\r
+                         Toast.makeText(this, R.string.auth_account_does_not_exist, Toast.LENGTH_SHORT).show();\r
+                         finish();\r
+                     }\r
                  }\r
              }\r
  \r
                  success = createAccount();\r
  \r
              } else {\r
-                 updateToken();\r
-                 success = true;\r
+                 try {\r
+                     updateAccountAuthentication();\r
+                     success = true;\r
\r
+                 } catch (AccountNotFoundException e) {\r
+                     Log_OC.e(TAG, "Account " + mAccount + " was removed!", e);\r
+                     Toast.makeText(this, R.string.auth_account_does_not_exist, Toast.LENGTH_SHORT).show();\r
+                     finish();\r
+                 }\r
              }\r
  \r
              if (success) {\r
  \r
  \r
      /**\r
-      * Sets the proper response to get that the Account Authenticator that started this activity \r
+      * Updates the authentication token.\r
+      *\r
+      * Sets the proper response so that the AccountAuthenticator that started this activity\r
       * saves a new authorization token for mAccount.\r
+      *\r
+      * Kills the session kept by OwnCloudClientManager so that a new one will created with\r
+      * the new credentials when needed.\r
       */\r
-     private void updateToken() {\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
          response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);\r
@@@ -1,19 -1,3 +1,19 @@@
 +/* ownCloud Android client application
 + *   Copyright (C) 2012-2015 ownCloud Inc.
 + *
 + *   This program is free software: you can redistribute it and/or modify
 + *   it under the terms of the GNU General Public License version 2,
 + *   as published by the Free Software Foundation.
 + *
 + *   This program is distributed in the hope that it will be useful,
 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + *   GNU General Public License for more details.
 + *
 + *   You should have received a copy of the GNU General Public License
 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 + *
 + */
  package com.owncloud.android.authentication;
  
  import android.app.Activity;
@@@ -40,17 -24,16 +40,17 @@@ import java.lang.ref.WeakReference
   */
  public class AuthenticatorAsyncTask  extends AsyncTask<String, Void, RemoteOperationResult> {
  
 -
      private static String REMOTE_PATH = "/";
      private static boolean SUCCESS_IF_ABSENT = false;
  
      private Context mContext;
      private final WeakReference<OnAuthenticatorTaskListener> mListener;
 +    protected Activity mActivity;
  
      public AuthenticatorAsyncTask(Activity activity) {
          mContext = activity.getApplicationContext();
-         mListener = new WeakReference<>((OnAuthenticatorTaskListener)activity);
+         mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
 +        mActivity = activity;
      }
  
      @Override
              }
          }
      }
 -
      /*
       * Interface to retrieve data from recognition task
       */