Create dialog fragment for showing authentication dialog
authorjabarros <jabarros@solidgear.es>
Tue, 2 Sep 2014 12:03:56 +0000 (14:03 +0200)
committerjabarros <jabarros@solidgear.es>
Tue, 2 Sep 2014 12:03:56 +0000 (14:03 +0200)
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/authentication/SsoWebViewClient.java
src/com/owncloud/android/ui/dialog/CredentialsDialogFragment.java [new file with mode: 0644]

index 07c4d79..716bf7f 100644 (file)
@@ -50,7 +50,9 @@ import android.view.View.OnFocusChangeListener;
 import android.view.View.OnTouchListener;\r
 import android.view.Window;\r
 import android.view.inputmethod.EditorInfo;\r
 import android.view.View.OnTouchListener;\r
 import android.view.Window;\r
 import android.view.inputmethod.EditorInfo;\r
+import android.webkit.HttpAuthHandler;\r
 import android.webkit.SslErrorHandler;\r
 import android.webkit.SslErrorHandler;\r
+import android.webkit.WebView;\r
 import android.widget.Button;\r
 import android.widget.CheckBox;\r
 import android.widget.EditText;\r
 import android.widget.Button;\r
 import android.widget.CheckBox;\r
 import android.widget.EditText;\r
@@ -77,6 +79,7 @@ import com.owncloud.android.operations.GetServerInfoOperation;
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
 import com.owncloud.android.services.OperationsService;\r
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;\r
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
 import com.owncloud.android.services.OperationsService;\r
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;\r
+import com.owncloud.android.ui.dialog.CredentialsDialogFragment;\r
 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;\r
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;\r
 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;\r
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;\r
@@ -125,6 +128,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private static final String UNTRUSTED_CERT_DIALOG_TAG = "UNTRUSTED_CERT_DIALOG";\r
     private static final String SAML_DIALOG_TAG = "SAML_DIALOG";\r
     private static final String WAIT_DIALOG_TAG = "WAIT_DIALOG";\r
     private static final String UNTRUSTED_CERT_DIALOG_TAG = "UNTRUSTED_CERT_DIALOG";\r
     private static final String SAML_DIALOG_TAG = "SAML_DIALOG";\r
     private static final String WAIT_DIALOG_TAG = "WAIT_DIALOG";\r
+    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
     \r
     /// parameters from EXTRAs in starter Intent\r
 \r
     \r
     /// parameters from EXTRAs in starter Intent\r
@@ -170,6 +175,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     \r
     private String mAuthToken = "";\r
 \r
     \r
     private String mAuthToken = "";\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
     \r
     /// Identifier of operation in progress which result shouldn't be lost \r
     private long mWaitingForOpId = Long.MAX_VALUE;\r
@@ -186,6 +193,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         super.onCreate(savedInstanceState);\r
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
 \r
         super.onCreate(savedInstanceState);\r
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
 \r
+        mIsFirstAuthAttempt = true;\r
+\r
         // bind to Operations Service\r
         mOperationsServiceConnection = new OperationsServiceConnection();\r
         if (!bindService(new Intent(this, OperationsService.class), \r
         // bind to Operations Service\r
         mOperationsServiceConnection = new OperationsServiceConnection();\r
         if (!bindService(new Intent(this, OperationsService.class), \r
@@ -210,6 +219,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         } else {\r
             mAuthTokenType = savedInstanceState.getString(KEY_AUTH_TOKEN_TYPE);\r
             mWaitingForOpId = savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID);\r
         } else {\r
             mAuthTokenType = savedInstanceState.getString(KEY_AUTH_TOKEN_TYPE);\r
             mWaitingForOpId = savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID);\r
+            mIsFirstAuthAttempt = savedInstanceState.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG);\r
         }\r
         \r
         /// load user interface\r
         }\r
         \r
         /// load user interface\r
@@ -556,6 +566,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         outState.putInt(KEY_AUTH_STATUS_TEXT, mAuthStatusText);\r
         outState.putString(KEY_AUTH_TOKEN, mAuthToken);\r
 \r
         outState.putInt(KEY_AUTH_STATUS_TEXT, mAuthStatusText);\r
         outState.putString(KEY_AUTH_TOKEN, mAuthToken);\r
 \r
+        /// authentication\r
+        outState.putBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG, mIsFirstAuthAttempt);\r
+\r
         //Log_OC.wtf(TAG, "onSaveInstanceState end" );\r
     }\r
 \r
         //Log_OC.wtf(TAG, "onSaveInstanceState end" );\r
     }\r
 \r
@@ -1685,6 +1698,15 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     }\r
 \r
     /**\r
     }\r
 \r
     /**\r
+     * Show authentication dialog \r
+     */\r
+    public void showAuthenticationDialog(WebView webView, HttpAuthHandler handler) {\r
+        // Show a dialog for the authentication\r
+        createAuthenticationDialog(webView, handler);\r
+        \r
+    }\r
+\r
+    /**\r
      * Show untrusted cert dialog \r
      */\r
     private void showUntrustedCertDialog(RemoteOperationResult result) {\r
      * Show untrusted cert dialog \r
      */\r
     private void showUntrustedCertDialog(RemoteOperationResult result) {\r
@@ -1774,5 +1796,33 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         }\r
     \r
     }\r
         }\r
     \r
     }\r
-    \r
+\r
+    /**\r
+     * Create dialog for request authentication to the user\r
+     * @param webView\r
+     * @param handler\r
+     */\r
+    private void createAuthenticationDialog(WebView webView, HttpAuthHandler handler) {\r
+\r
+        // Show a dialog with the certificate info\r
+        CredentialsDialogFragment dialog = CredentialsDialogFragment.newInstanceForCredentials(webView, handler);\r
+        FragmentManager fm = getSupportFragmentManager();\r
+        FragmentTransaction ft = fm.beginTransaction();\r
+        ft.addToBackStack(null);\r
+        dialog.setCancelable(false);\r
+        dialog.show(ft, CREDENTIALS_DIALOG_TAG);\r
+\r
+        if (!mIsFirstAuthAttempt) {\r
+            Toast.makeText(getApplicationContext(), getText(R.string.saml_authentication_wrong_pass), Toast.LENGTH_LONG).show();\r
+        } else {\r
+            mIsFirstAuthAttempt = false;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * For retrieving the clicking on authentication cancel button\r
+     */\r
+    public void doNegativeAuthenticatioDialogClick(){\r
+        mIsFirstAuthAttempt = true;\r
+    }\r
 }\r
 }\r
index 685fb1e..88cea8a 100644 (file)
@@ -24,17 +24,13 @@ import java.security.cert.CertificateException;
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 
-import android.app.AlertDialog;
-import android.app.AlertDialog.Builder;
 import android.content.Context;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.graphics.Bitmap;
 import android.net.http.SslCertificate;
 import android.net.http.SslError;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.graphics.Bitmap;
 import android.net.http.SslCertificate;
 import android.net.http.SslError;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
-import android.text.InputType;
 import android.view.KeyEvent;
 import android.view.View;
 import android.webkit.CookieManager;
 import android.view.KeyEvent;
 import android.view.View;
 import android.webkit.CookieManager;
@@ -43,11 +39,7 @@ import android.webkit.SslErrorHandler;
 import android.webkit.WebResourceResponse;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.webkit.WebResourceResponse;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
-import android.widget.EditText;
-import android.widget.LinearLayout;
-import android.widget.Toast;
 
 
-import com.owncloud.android.R;
 import com.owncloud.android.lib.common.network.NetworkUtils;
 import com.owncloud.android.utils.Log_OC;
 
 import com.owncloud.android.lib.common.network.NetworkUtils;
 import com.owncloud.android.utils.Log_OC;
 
@@ -75,7 +67,6 @@ public class SsoWebViewClient extends WebViewClient {
     private String mTargetUrl;
     private String mLastReloadedUrlAtError;
 
     private String mTargetUrl;
     private String mLastReloadedUrlAtError;
 
-    private boolean mIsFirstAttempt;
     
     public SsoWebViewClient (Context context, Handler listenerHandler, SsoWebViewClientListener listener) {
         mContext = context;
     
     public SsoWebViewClient (Context context, Handler listenerHandler, SsoWebViewClientListener listener) {
         mContext = context;
@@ -83,7 +74,6 @@ public class SsoWebViewClient extends WebViewClient {
         mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
         mTargetUrl = "fake://url.to.be.set";
         mLastReloadedUrlAtError = null;
         mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
         mTargetUrl = "fake://url.to.be.set";
         mLastReloadedUrlAtError = null;
-        mIsFirstAttempt = true;
     }
     
     public String getTargetUrl() {
     }
     
     public String getTargetUrl() {
@@ -207,13 +197,7 @@ public class SsoWebViewClient extends WebViewClient {
     public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
         Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
 
     public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
         Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
 
-        createAuthenticationDialog(view, handler);
-
-        if (!mIsFirstAttempt) {
-            Toast.makeText(mContext, mContext.getText(R.string.saml_authentication_wrong_pass), Toast.LENGTH_LONG).show();
-        } else {
-            mIsFirstAttempt = false;
-        }
+        ((AuthenticatorActivity)mContext).showAuthenticationDialog(view, handler);
     }
 
     @Override
     }
 
     @Override
@@ -248,60 +232,4 @@ public class SsoWebViewClient extends WebViewClient {
         Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
         return false;
     }
         Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
         return false;
     }
-
-    /**
-     * Create dialog for request authentication to the user
-     * @param webView
-     * @param handler
-     */
-    private void createAuthenticationDialog(WebView webView, HttpAuthHandler handler) {
-        final WebView mWebView = webView;
-        final HttpAuthHandler mHandler = handler;
-
-        // Create field for username
-        final EditText usernameET = new EditText(mContext);
-        usernameET.setHint(mContext.getText(R.string.auth_username));
-
-        // Create field for password
-        final EditText passwordET = new EditText(mContext);
-        passwordET.setHint(mContext.getText(R.string.auth_password));
-        passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
-
-        // Prepare LinearLayout for dialog
-        LinearLayout ll = new LinearLayout(mContext);
-        ll.setOrientation(LinearLayout.VERTICAL);
-        ll.addView(usernameET);
-        ll.addView(passwordET);
-
-        Builder authDialog = new AlertDialog
-                .Builder(mContext)
-                .setTitle(mContext.getText(R.string.saml_authentication_required_text))
-                .setView(ll)
-                .setCancelable(false)
-                .setPositiveButton(mContext.getText(R.string.common_ok),
-                        new DialogInterface.OnClickListener() {
-                    public void onClick(DialogInterface dialog, int whichButton) {
-
-                        String username = usernameET.getText().toString().trim();
-                        String password = passwordET.getText().toString().trim();
-
-                        // Proceed with the authentication
-                        mHandler.proceed(username, password);
-                        dialog.dismiss();
-                    }
-                })
-                .setNegativeButton(mContext.getText(R.string.common_cancel),
-                        new DialogInterface.OnClickListener() {
-                    public void onClick(DialogInterface dialog, int whichButton) {
-                        dialog.dismiss();
-                        mWebView.stopLoading();
-                        mIsFirstAttempt = true;
-                    }
-                });
-
-        if (mWebView!=null) {
-            authDialog.show();
-        }
-
-    }
 }
 }
diff --git a/src/com/owncloud/android/ui/dialog/CredentialsDialogFragment.java b/src/com/owncloud/android/ui/dialog/CredentialsDialogFragment.java
new file mode 100644 (file)
index 0000000..1b35057
--- /dev/null
@@ -0,0 +1,152 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2014 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.ui.dialog;
+
+import com.actionbarsherlock.app.SherlockDialogFragment;
+import com.owncloud.android.R;
+import com.owncloud.android.authentication.AuthenticatorActivity;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.AlertDialog.Builder;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.text.InputType;
+import android.view.WindowManager.LayoutParams;
+import android.webkit.HttpAuthHandler;
+import android.webkit.WebView;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+
+
+
+/**
+ *  Dialog to input authentication credentials
+ * 
+ */
+public class CredentialsDialogFragment extends SherlockDialogFragment
+    implements DialogInterface.OnClickListener {
+
+    private WebView mWebView = null;
+    private HttpAuthHandler mHandler = null;
+
+    private EditText mUsernameET;
+    private EditText mPasswordET;
+    
+    private String mUsernameStr;
+    private String mPasswordStr;
+
+
+    /**
+     * Public factory method to create new CredentialsDialogFragment instances.
+     * @param webView       WebView that is being loaded
+     * @param handler       HttpAuthHandler
+     * @return              Dialog ready to show
+     */
+    public static CredentialsDialogFragment newInstanceForCredentials(WebView webView, HttpAuthHandler handler) {
+        if (handler == null) {
+            throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
+        }
+        CredentialsDialogFragment frag = new CredentialsDialogFragment();
+        frag.mHandler = handler;
+        frag.mWebView = webView;
+        return frag;
+    }
+
+
+    @Override
+    public Dialog onCreateDialog(Bundle savedInstanceState) {
+
+        // Create field for username
+        mUsernameET = new EditText(getSherlockActivity());
+        mUsernameET.setHint(getSherlockActivity().getText(R.string.auth_username));
+
+        // Create field for password
+        mPasswordET = new EditText(getSherlockActivity());
+        mPasswordET.setHint(getSherlockActivity().getText(R.string.auth_password));
+        mPasswordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
+
+        // Prepare LinearLayout for dialog
+        LinearLayout ll = new LinearLayout(getSherlockActivity());
+        ll.setOrientation(LinearLayout.VERTICAL);
+        ll.addView(mUsernameET);
+        ll.addView(mPasswordET);
+        
+        ll.requestFocus();
+        
+        setRetainInstance(true);
+
+        Builder authDialog = new AlertDialog
+                .Builder(getSherlockActivity())
+                .setTitle(getSherlockActivity().getText(R.string.saml_authentication_required_text))
+                .setView(ll)
+                .setCancelable(false)
+                .setPositiveButton(R.string.common_ok, this)
+                .setNegativeButton(R.string.common_cancel, this);
+
+        Dialog d = authDialog.create();
+        d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        return d;
+    }
+
+
+    @Override
+    public void onPause() {
+        super.onPause();
+        // Due to the use of setRetainInstance(true) for keep the dialog over the rest of dialogs,
+        // we need to save the inputs text for being injected in onResume()
+        mUsernameStr = mUsernameET.getText().toString();
+        mPasswordStr = mPasswordET.getText().toString();
+    }
+
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        mUsernameET.setText(mUsernameStr);
+        mPasswordET.setText(mPasswordStr);
+    }
+
+
+    @Override
+    public void onClick(DialogInterface dialog, int which) {
+        if (which == AlertDialog.BUTTON_POSITIVE) { 
+
+            String username = mUsernameET.getText().toString().trim();
+            String password = mPasswordET.getText().toString().trim();
+
+            // Proceed with the authentication
+            mHandler.proceed(username, password);
+            dialog.dismiss();
+
+        } else if (which == AlertDialog.BUTTON_NEGATIVE) {
+            dialog.dismiss();
+            mWebView.stopLoading();
+            ((AuthenticatorActivity)getActivity()).doNegativeAuthenticatioDialogClick();
+        }
+    }
+    
+    
+    @Override
+    public void onDestroyView() {
+      if (getDialog() != null && getRetainInstance())
+        getDialog().setDismissMessage(null);
+      super.onDestroyView();
+    }
+
+}