OC-2633: Fixes for comments in code. PR #347
authormasensio <masensio@solidgear.es>
Fri, 17 Jan 2014 13:20:00 +0000 (14:20 +0100)
committermasensio <masensio@solidgear.es>
Fri, 17 Jan 2014 13:20:00 +0000 (14:20 +0100)
oc_framework/src/com/owncloud/android/oc_framework/accounts/SsoWebViewClient.java [deleted file]
oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java
oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/authentication/SsoWebViewClient.java [new file with mode: 0644]
src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java

diff --git a/oc_framework/src/com/owncloud/android/oc_framework/accounts/SsoWebViewClient.java b/oc_framework/src/com/owncloud/android/oc_framework/accounts/SsoWebViewClient.java
deleted file mode 100644 (file)
index 90e8e6e..0000000
+++ /dev/null
@@ -1,180 +0,0 @@
-/* ownCloud Android client application
- *   Copyright (C) 2012-2013 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.oc_framework.accounts;
-
-import java.lang.ref.WeakReference;
-
-import android.graphics.Bitmap;
-import android.net.http.SslError;
-import android.os.Handler;
-import android.os.Message;
-import android.util.Log;
-import android.view.KeyEvent;
-import android.view.View;
-import android.webkit.CookieManager;
-import android.webkit.HttpAuthHandler;
-import android.webkit.SslErrorHandler;
-import android.webkit.WebResourceResponse;
-import android.webkit.WebView;
-import android.webkit.WebViewClient;
-
-
-/**
- * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process 
- * running in the {@link WebView} that is attached to.
- * 
- * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
- * authentication process.
- *   
- * @author David A. Velasco
- */
-public class SsoWebViewClient extends WebViewClient {
-        
-    private static final String TAG = SsoWebViewClient.class.getSimpleName();
-    
-    public interface SsoWebViewClientListener {
-        public void onSsoFinished(String sessionCookie);
-    }
-    
-    private Handler mListenerHandler;
-    private WeakReference<SsoWebViewClientListener> mListenerRef;
-    private String mTargetUrl;
-    private String mLastReloadedUrlAtError;
-    
-    public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
-        mListenerHandler = listenerHandler;
-        mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
-        mTargetUrl = "fake://url.to.be.set";
-        mLastReloadedUrlAtError = null;
-    }
-    
-    public String getTargetUrl() {
-        return mTargetUrl;
-    }
-    
-    public void setTargetUrl(String targetUrl) {
-        mTargetUrl = targetUrl;
-    }
-
-    @Override
-    public void onPageStarted (WebView view, String url, Bitmap favicon) {
-        Log.d(TAG, "onPageStarted : " + url);
-        super.onPageStarted(view, url, favicon);
-    }
-    
-    @Override
-    public void onFormResubmission (WebView view, Message dontResend, Message resend) {
-        Log.d(TAG, "onFormResubMission ");
-
-        // necessary to grant reload of last page when device orientation is changed after sending a form
-        resend.sendToTarget();
-    }
-
-    @Override
-    public boolean shouldOverrideUrlLoading(WebView view, String url) {
-        return false;
-    }
-    
-    @Override
-    public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
-        Log.e(TAG, "onReceivedError : " + failingUrl + ", code " + errorCode + ", description: " + description);
-        if (!failingUrl.equals(mLastReloadedUrlAtError)) {
-            view.reload();
-            mLastReloadedUrlAtError = failingUrl;
-        } else {
-            mLastReloadedUrlAtError = null;
-            super.onReceivedError(view, errorCode, description, failingUrl);
-        }
-    }
-    
-    @Override
-    public void onPageFinished (WebView view, String url) {
-        Log.d(TAG, "onPageFinished : " + url);
-        mLastReloadedUrlAtError = null;
-        if (url.startsWith(mTargetUrl)) {
-            view.setVisibility(View.GONE);
-            CookieManager cookieManager = CookieManager.getInstance();
-            final String cookies = cookieManager.getCookie(url);
-            Log.d(TAG, "Cookies: " + cookies);
-            if (mListenerHandler != null && mListenerRef != null) {
-                // this is good idea because onPageFinished is not running in the UI thread
-                mListenerHandler.post(new Runnable() {
-                    @Override
-                    public void run() {
-                        SsoWebViewClientListener listener = mListenerRef.get();
-                        if (listener != null) {
-                               // Send Cookies to the listener
-                            listener.onSsoFinished(cookies);
-                        }
-                    }
-                });
-            }
-        } 
-    }
-    
-    
-    @Override
-    public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
-        Log.d(TAG, "doUpdateVisitedHistory : " + url);
-    }
-    
-    @Override
-    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
-        Log.d(TAG, "onReceivedSslError : " + error);
-        handler.proceed();
-    }
-    
-    @Override
-    public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
-        Log.d(TAG, "onReceivedHttpAuthRequest : " + host);
-    }
-
-    @Override
-    public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
-        Log.d(TAG, "shouldInterceptRequest : " + url);
-        return null;
-    }
-    
-    @Override
-    public void onLoadResource (WebView view, String url) {
-        Log.d(TAG, "onLoadResource : " + url);   
-    }
-    
-    @Override
-    public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
-        Log.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
-    }
-    
-    @Override
-    public void onScaleChanged (WebView view, float oldScale, float newScale) {
-        Log.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
-        super.onScaleChanged(view, oldScale, newScale);
-    }
-
-    @Override
-    public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
-        Log.d(TAG, "onUnhandledKeyEvent : " + event);
-    }
-    
-    @Override
-    public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
-        Log.d(TAG, "shouldOverrideKeyEvent : " + event);
-        return false;
-    }
-
-}
index ada12d2..630e3e9 100644 (file)
@@ -52,9 +52,9 @@ import android.util.Log;
  * @author David A. Velasco
  */
 public class RemoteOperationResult implements Serializable {
  * @author David A. Velasco
  */
 public class RemoteOperationResult implements Serializable {
-
+       
        /** Generated - should be refreshed every time the class changes!! */
        /** Generated - should be refreshed every time the class changes!! */
-       private static final long serialVersionUID = -2469951225222759283L;
+       private static final long serialVersionUID = -8257349554488668693L;
     
     private static final String TAG = "RemoteOperationResult";
     
     
     private static final String TAG = "RemoteOperationResult";
     
@@ -91,7 +91,6 @@ public class RemoteOperationResult implements Serializable {
         ACCOUNT_NOT_NEW, 
         ACCOUNT_NOT_THE_SAME,
         INVALID_CHARACTER_IN_NAME,
         ACCOUNT_NOT_NEW, 
         ACCOUNT_NOT_THE_SAME,
         INVALID_CHARACTER_IN_NAME,
-        JSON_EXCEPTION
     }
 
     private boolean mSuccess = false;
     }
 
     private boolean mSuccess = false;
@@ -101,13 +100,11 @@ public class RemoteOperationResult implements Serializable {
     private String mRedirectedLocation;
 
     private ArrayList<RemoteFile> mFiles;
     private String mRedirectedLocation;
 
     private ArrayList<RemoteFile> mFiles;
-    private String mUserName;
     
     public RemoteOperationResult(ResultCode code) {
         mCode = code;
         mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
         mFiles = null;
     
     public RemoteOperationResult(ResultCode code) {
         mCode = code;
         mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
         mFiles = null;
-        setUserName("");
     }
 
     private RemoteOperationResult(boolean success, int httpCode) {
     }
 
     private RemoteOperationResult(boolean success, int httpCode) {
@@ -196,9 +193,6 @@ public class RemoteOperationResult implements Serializable {
                 mCode = ResultCode.SSL_ERROR;
             }
 
                 mCode = ResultCode.SSL_ERROR;
             }
 
-        } else if (e instanceof JSONException) {
-               mCode = ResultCode.JSON_EXCEPTION;
-               
         } else {
             mCode = ResultCode.UNKNOWN_ERROR;
         }
         } else {
             mCode = ResultCode.UNKNOWN_ERROR;
         }
@@ -303,6 +297,7 @@ public class RemoteOperationResult implements Serializable {
                 
             } else if (mException instanceof JSONException) {
                return "JSON exception";
                 
             } else if (mException instanceof JSONException) {
                return "JSON exception";
+               
             } else {
                 return "Unexpected exception";
             }
             } else {
                 return "Unexpected exception";
             }
@@ -358,12 +353,4 @@ public class RemoteOperationResult implements Serializable {
                 mRedirectedLocation.toLowerCase().contains("wayf")));
     }
 
                 mRedirectedLocation.toLowerCase().contains("wayf")));
     }
 
-       public String getUserName() {
-               return mUserName;
-       }
-
-       public void setUserName(String mUserName) {
-               this.mUserName = mUserName;
-       }
-
 }
 }
index d8e7626..717fae2 100644 (file)
@@ -46,25 +46,19 @@ public class GetUserNameRemoteOperation extends RemoteOperation {
        private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName();
 
        // HEADER
        private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName();
 
        // HEADER
-       private static final String TAG_HEADER_OCS_API = "OCS-APIREQUEST";
-       private static final String TAG_HEADER_OCS_API_VALUE = "true";
-       
-       private static final String TAG_HEADER_CONTENT = "Content-Type";
-       private static final String TAG_HEADER_CONTENT_VALUE = "application/xml";
-       private static final String TAG_HEADER_COOKIE = "Cookie";
+       private static final String HEADER_OCS_API = "OCS-APIREQUEST";
+       private static final String HEADER_OCS_API_VALUE = "true";
 
        // OCS Route
 
        // OCS Route
-       private static final String TAG_OCS_ROUTE ="/index.php/ocs/cloud/user?format=json"; 
+       private static final String OCS_ROUTE ="/index.php/ocs/cloud/user?format=json"; 
 
        // JSON Node names
 
        // JSON Node names
-       private static final String TAG_OCS = "ocs";
-       private static final String TAG_DATA = "data";
-       private static final String TAG_ID = "id";
-       private static final String TAG_DISPLAY_NAME= "display-name";
-       private static final String TAG_EMAIL= "email";
-
-       private String mUrl;
-       private String mSessionCookie;
+       private static final String NODE_OCS = "ocs";
+       private static final String NODE_DATA = "data";
+       private static final String NODE_ID = "id";
+       private static final String NODE_DISPLAY_NAME= "display-name";
+       private static final String NODE_EMAIL= "email";
+
        private String mUserName;
 
        public String getUserName() {
        private String mUserName;
 
        public String getUserName() {
@@ -72,9 +66,7 @@ public class GetUserNameRemoteOperation extends RemoteOperation {
        }
 
        
        }
 
        
-       public GetUserNameRemoteOperation(String url, String sessioncookie) {
-               mUrl = url;
-               mSessionCookie = sessioncookie;
+       public GetUserNameRemoteOperation() {
        }
 
        @Override
        }
 
        @Override
@@ -83,12 +75,10 @@ public class GetUserNameRemoteOperation extends RemoteOperation {
         int status = -1;
         
         // Get Method
         int status = -1;
         
         // Get Method
-        GetMethod get = new GetMethod(mUrl + TAG_OCS_ROUTE);
-        Log.d(TAG, "URL ------> " + mUrl + TAG_OCS_ROUTE);
+        GetMethod get = new GetMethod(client.getBaseUri() + OCS_ROUTE);
+        Log.d(TAG, "URL ------> " + client.getBaseUri() + OCS_ROUTE);
         // Add the Header
         // Add the Header
-        get.addRequestHeader(TAG_HEADER_CONTENT, TAG_HEADER_CONTENT_VALUE);
-        get.addRequestHeader(TAG_HEADER_OCS_API, TAG_HEADER_OCS_API_VALUE);
-        get.setRequestHeader(TAG_HEADER_COOKIE, mSessionCookie);
+        get.addRequestHeader(HEADER_OCS_API, HEADER_OCS_API_VALUE);
         
         //Get the user
         try {
         
         //Get the user
         try {
@@ -101,15 +91,15 @@ public class GetUserNameRemoteOperation extends RemoteOperation {
 
                                 // Parse the response
                                 JSONObject respJSON = new JSONObject(response);
 
                                 // Parse the response
                                 JSONObject respJSON = new JSONObject(response);
-                                JSONObject respOCS = respJSON.getJSONObject(TAG_OCS);
-                                JSONObject respData = respOCS.getJSONObject(TAG_DATA);
-                                String id = respData.getString(TAG_ID);
-                                String displayName = respData.getString(TAG_DISPLAY_NAME);
-                                String email = respData.getString(TAG_EMAIL);
+                                JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
+                                JSONObject respData = respOCS.getJSONObject(NODE_DATA);
+                                String id = respData.getString(NODE_ID);
+                                String displayName = respData.getString(NODE_DISPLAY_NAME);
+                                String email = respData.getString(NODE_EMAIL);
                                 
                                 // Result
                                 result = new RemoteOperationResult(isSuccess(status), status, (get != null ? get.getResponseHeaders() : null));
                                 
                                 // Result
                                 result = new RemoteOperationResult(isSuccess(status), status, (get != null ? get.getResponseHeaders() : null));
-                                result.setUserName(displayName);
+                                mUserName =  displayName;
                                 
                                 Log.d(TAG, "Response: " + id + " - " + displayName + " - " + email);
                                 
                                 
                                 Log.d(TAG, "Response: " + id + " - " + displayName + " - " + email);
                                 
index c07b002..b302141 100644 (file)
@@ -18,8 +18,6 @@
 \r
 package com.owncloud.android.authentication;\r
 \r
 \r
 package com.owncloud.android.authentication;\r
 \r
-import java.util.concurrent.ExecutionException;\r
-\r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.app.AlertDialog;\r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.app.AlertDialog;\r
@@ -31,7 +29,6 @@ import android.content.SharedPreferences;
 import android.graphics.Rect;\r
 import android.graphics.drawable.Drawable;\r
 import android.net.Uri;\r
 import android.graphics.Rect;\r
 import android.graphics.drawable.Drawable;\r
 import android.net.Uri;\r
-import android.os.AsyncTask;\r
 import android.os.Bundle;\r
 import android.os.Handler;\r
 import android.preference.PreferenceManager;\r
 import android.os.Bundle;\r
 import android.os.Handler;\r
 import android.preference.PreferenceManager;\r
@@ -55,9 +52,9 @@ import android.widget.TextView.OnEditorActionListener;
 import com.actionbarsherlock.app.SherlockDialogFragment;\r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
 import com.actionbarsherlock.app.SherlockDialogFragment;\r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
+import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;\r
 import com.owncloud.android.oc_framework.accounts.AccountTypeUtils;\r
 import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;\r
 import com.owncloud.android.oc_framework.accounts.AccountTypeUtils;\r
 import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;\r
-import com.owncloud.android.oc_framework.accounts.SsoWebViewClient.SsoWebViewClientListener;\r
 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;\r
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;\r
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;\r
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;\r
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
@@ -794,10 +791,41 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             } else {\r
                 onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result);\r
             }\r
             } else {\r
                 onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result);\r
             }\r
+        } else if (operation instanceof GetUserNameRemoteOperation) {\r
+            onGetUserNameFinish((GetUserNameRemoteOperation) operation, result);\r
+             \r
         }\r
         }\r
+        \r
     }\r
 \r
     }\r
 \r
-\r
+    private void onGetUserNameFinish(GetUserNameRemoteOperation operation, RemoteOperationResult result) {\r
+        if (result.isSuccess()) {\r
+            boolean success = false;\r
+            String username = operation.getUserName();\r
+            \r
+            if ( mAction == ACTION_CREATE) {\r
+                mUsernameInput.setText(username);\r
+                createAccount();\r
+                success = true;\r
+            } else {\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
+                    updateAuthStatusIconAndText(result);\r
+                    showAuthStatus();\r
+                    Log_OC.d(TAG, result.getLogMessage());\r
+                } else {\r
+                  updateToken();\r
+                  success = true;\r
+                }\r
+            }\r
+            \r
+            if (success)\r
+                finish();\r
+        }\r
+        \r
+    }\r
 \r
     private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) {\r
         try {\r
 \r
     private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) {\r
         try {\r
@@ -1123,7 +1151,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 success = createAccount();\r
 \r
             } else {\r
                 success = createAccount();\r
 \r
             } else {\r
-                success = updateToken();\r
+                updateToken();\r
+                success = true;\r
             }\r
 \r
             if (success) {\r
             }\r
 \r
             if (success) {\r
@@ -1169,7 +1198,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
      * Sets the proper response to get that the Account Authenticator that started this activity saves \r
      * a new authorization token for mAccount.\r
      */\r
      * Sets the proper response to get that the Account Authenticator that started this activity saves \r
      * a new authorization token for mAccount.\r
      */\r
-    private boolean updateToken() {\r
+    private void updateToken() {\r
         Bundle response = new Bundle();\r
         response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);\r
         response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);\r
         Bundle response = new Bundle();\r
         response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);\r
         response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);\r
@@ -1181,20 +1210,6 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             \r
         } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
             \r
             \r
         } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
             \r
-            String username= getUserNameForSaml();\r
-            if (username == null)\r
-                return false;\r
-            \r
-            if (!mUsernameInput.getText().toString().equals(username)) {\r
-                // fail - not a new account, but an existing one; disallow\r
-                RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME); \r
-                updateAuthStatusIconAndText(result);\r
-                showAuthStatus();\r
-                Log_OC.d(TAG, result.getLogMessage());\r
-                \r
-                return false;\r
-            }\r
-            \r
             response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);\r
             // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention\r
             mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);\r
             response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);\r
             // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention\r
             mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);\r
@@ -1205,7 +1220,6 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         }\r
         setAccountAuthenticatorResult(response);\r
         \r
         }\r
         setAccountAuthenticatorResult(response);\r
         \r
-        return true;\r
     }\r
 \r
 \r
     }\r
 \r
 \r
@@ -1223,12 +1237,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         Uri uri = Uri.parse(mHostBaseUrl);\r
         String username = mUsernameInput.getText().toString().trim();\r
 \r
         Uri uri = Uri.parse(mHostBaseUrl);\r
         String username = mUsernameInput.getText().toString().trim();\r
-        if (isSaml) {\r
-            username = getUserNameForSaml();\r
-            if (username == null)\r
-                return false;\r
-\r
-        } else if (isOAuth) {\r
+        if (isOAuth) {\r
             username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();\r
         }            \r
         String accountName = username + "@" + uri.getHost();\r
             username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();\r
         }            \r
         String accountName = username + "@" + uri.getHost();\r
@@ -1588,16 +1597,11 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         \r
         if (sessionCookie != null && sessionCookie.length() > 0) {\r
             mAuthToken = sessionCookie;\r
         \r
         if (sessionCookie != null && sessionCookie.length() > 0) {\r
             mAuthToken = sessionCookie;\r
-            boolean success = false;\r
-            if (mAction == ACTION_CREATE) {\r
-                success = createAccount();\r
-        \r
-            } else {\r
-                success = updateToken();\r
-            }\r
-            if (success) {\r
-                finish();\r
-            }\r
+\r
+            GetUserNameRemoteOperation getUserOperation = new GetUserNameRemoteOperation();            \r
+            WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl), getApplicationContext(), true);\r
+            client.setSsoSessionCookie(mAuthToken);\r
+            getUserOperation.execute(client, this, mHandler);\r
         }\r
 \r
             \r
         }\r
 \r
             \r
@@ -1648,41 +1652,4 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         return super.onTouchEvent(event);\r
     }\r
     \r
         return super.onTouchEvent(event);\r
     }\r
     \r
-    \r
-    /**\r
-     * Asynchronous task to get the SAML User name from OCS-API\r
-     *\r
-     */\r
-    private class GetUserNameTask extends AsyncTask<Void, Void, String>{\r
-\r
-        @Override\r
-        protected String doInBackground(Void... params) {\r
-            \r
-            GetUserNameRemoteOperation getUserOperation = new GetUserNameRemoteOperation(mHostBaseUrl, mAuthToken);\r
-            WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl), getApplicationContext(), true);\r
-            RemoteOperationResult result = getUserOperation.execute(client);\r
-          \r
-            return result.getUserName();\r
-        }\r
-        \r
-    }\r
-\r
-    /**\r
-     * Get the user name form OCS-API\r
-     * @return username\r
-     */\r
-    private String getUserNameForSaml(){\r
-\r
-        GetUserNameTask getUserTask = new GetUserNameTask();\r
-        String username = null;\r
-        try {\r
-            username = getUserTask.execute().get();\r
-        } catch (InterruptedException e) {\r
-            e.printStackTrace();\r
-        } catch (ExecutionException e) {\r
-            e.printStackTrace();\r
-        }\r
-\r
-        return username;\r
-    }\r
 }\r
 }\r
diff --git a/src/com/owncloud/android/authentication/SsoWebViewClient.java b/src/com/owncloud/android/authentication/SsoWebViewClient.java
new file mode 100644 (file)
index 0000000..442ec73
--- /dev/null
@@ -0,0 +1,181 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012-2013 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 java.lang.ref.WeakReference;
+
+import com.owncloud.android.utils.Log_OC;
+
+import android.graphics.Bitmap;
+import android.net.http.SslError;
+import android.os.Handler;
+import android.os.Message;
+import android.view.KeyEvent;
+import android.view.View;
+import android.webkit.CookieManager;
+import android.webkit.HttpAuthHandler;
+import android.webkit.SslErrorHandler;
+import android.webkit.WebResourceResponse;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+
+/**
+ * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process 
+ * running in the {@link WebView} that is attached to.
+ * 
+ * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
+ * authentication process.
+ *   
+ * @author David A. Velasco
+ */
+public class SsoWebViewClient extends WebViewClient {
+        
+    private static final String TAG = SsoWebViewClient.class.getSimpleName();
+    
+    public interface SsoWebViewClientListener {
+        public void onSsoFinished(String sessionCookie);
+    }
+    
+    private Handler mListenerHandler;
+    private WeakReference<SsoWebViewClientListener> mListenerRef;
+    private String mTargetUrl;
+    private String mLastReloadedUrlAtError;
+    
+    public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
+        mListenerHandler = listenerHandler;
+        mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
+        mTargetUrl = "fake://url.to.be.set";
+        mLastReloadedUrlAtError = null;
+    }
+    
+    public String getTargetUrl() {
+        return mTargetUrl;
+    }
+    
+    public void setTargetUrl(String targetUrl) {
+        mTargetUrl = targetUrl;
+    }
+
+    @Override
+    public void onPageStarted (WebView view, String url, Bitmap favicon) {
+        Log_OC.d(TAG, "onPageStarted : " + url);
+        super.onPageStarted(view, url, favicon);
+    }
+    
+    @Override
+    public void onFormResubmission (WebView view, Message dontResend, Message resend) {
+        Log_OC.d(TAG, "onFormResubMission ");
+
+        // necessary to grant reload of last page when device orientation is changed after sending a form
+        resend.sendToTarget();
+    }
+
+    @Override
+    public boolean shouldOverrideUrlLoading(WebView view, String url) {
+        return false;
+    }
+    
+    @Override
+    public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
+        Log_OC.e(TAG, "onReceivedError : " + failingUrl + ", code " + errorCode + ", description: " + description);
+        if (!failingUrl.equals(mLastReloadedUrlAtError)) {
+            view.reload();
+            mLastReloadedUrlAtError = failingUrl;
+        } else {
+            mLastReloadedUrlAtError = null;
+            super.onReceivedError(view, errorCode, description, failingUrl);
+        }
+    }
+    
+    @Override
+    public void onPageFinished (WebView view, String url) {
+        Log_OC.d(TAG, "onPageFinished : " + url);
+        mLastReloadedUrlAtError = null;
+        if (url.startsWith(mTargetUrl)) {
+            view.setVisibility(View.GONE);
+            CookieManager cookieManager = CookieManager.getInstance();
+            final String cookies = cookieManager.getCookie(url);
+            Log_OC.d(TAG, "Cookies: " + cookies);
+            if (mListenerHandler != null && mListenerRef != null) {
+                // this is good idea because onPageFinished is not running in the UI thread
+                mListenerHandler.post(new Runnable() {
+                    @Override
+                    public void run() {
+                        SsoWebViewClientListener listener = mListenerRef.get();
+                        if (listener != null) {
+                               // Send Cookies to the listener
+                            listener.onSsoFinished(cookies);
+                        }
+                    }
+                });
+            }
+        } 
+    }
+    
+    
+    @Override
+    public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
+        Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
+    }
+    
+    @Override
+    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
+        Log_OC.d(TAG, "onReceivedSslError : " + error);
+        handler.proceed();
+    }
+    
+    @Override
+    public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
+        Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
+    }
+
+    @Override
+    public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
+        Log_OC.d(TAG, "shouldInterceptRequest : " + url);
+        return null;
+    }
+    
+    @Override
+    public void onLoadResource (WebView view, String url) {
+        Log_OC.d(TAG, "onLoadResource : " + url);   
+    }
+    
+    @Override
+    public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
+        Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
+    }
+    
+    @Override
+    public void onScaleChanged (WebView view, float oldScale, float newScale) {
+        Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
+        super.onScaleChanged(view, oldScale, newScale);
+    }
+
+    @Override
+    public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
+        Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
+    }
+    
+    @Override
+    public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
+        Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
+        return false;
+    }
+
+}
index 9927dc8..91c607e 100644 (file)
@@ -36,8 +36,8 @@ import android.webkit.WebView;
 
 import com.actionbarsherlock.app.SherlockDialogFragment;
 import com.owncloud.android.R;
 
 import com.actionbarsherlock.app.SherlockDialogFragment;
 import com.owncloud.android.R;
-import com.owncloud.android.oc_framework.accounts.SsoWebViewClient;
-import com.owncloud.android.oc_framework.accounts.SsoWebViewClient.SsoWebViewClientListener;
+import com.owncloud.android.authentication.SsoWebViewClient;
+import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
 import com.owncloud.android.utils.Log_OC;
 
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
 import com.owncloud.android.utils.Log_OC;