Adapt behaviour Authentication AyncTask + rotations
authormasensio <masensio@solidgear.es>
Fri, 13 Feb 2015 13:29:15 +0000 (14:29 +0100)
committermasensio <masensio@solidgear.es>
Fri, 13 Feb 2015 13:29:15 +0000 (14:29 +0100)
src/com/owncloud/android/MainApp.java
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/authentication/AuthenticatorAsyncTask.java

index 072469d..f7b4720 100644 (file)
@@ -1,5 +1,5 @@
 /* ownCloud Android client application
- *   Copyright (C) 2012-2013 ownCloud Inc.
+ *   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,
index a7774ff..6d092a2 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
@@ -67,9 +65,6 @@ import com.actionbarsherlock.app.SherlockDialogFragment;
 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.OwnCloudClient;\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.Constants;\r
 import com.owncloud.android.lib.common.network.CertificateCombinedException;\r
@@ -139,6 +134,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 +180,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 +422,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 +588,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 +917,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
index 26525a7..bf2355c 100644 (file)
@@ -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;
@@ -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);
+        mActivity = activity;
     }
 
     @Override
@@ -94,7 +111,6 @@ public class AuthenticatorAsyncTask  extends AsyncTask<String, Void, RemoteOpera
             }
         }
     }
-
     /*
      * Interface to retrieve data from recognition task
      */