Merge branch 'master' of https://github.com/owncloud/android into material_buttons
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index d741929..3db9b7a 100644 (file)
@@ -23,9 +23,6 @@
 \r
 package com.owncloud.android.authentication;\r
 \r
 \r
 package com.owncloud.android.authentication;\r
 \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.app.Dialog;\r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.app.Dialog;\r
@@ -54,7 +51,6 @@ import android.view.MotionEvent;
 import android.view.View;\r
 import android.view.View.OnFocusChangeListener;\r
 import android.view.View.OnTouchListener;\r
 import android.view.View;\r
 import android.view.View.OnFocusChangeListener;\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.view.inputmethod.EditorInfo;\r
 import android.webkit.HttpAuthHandler;\r
 import android.webkit.SslErrorHandler;\r
@@ -94,6 +90,9 @@ import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;\r
 import com.owncloud.android.utils.DisplayUtils;\r
 \r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;\r
 import com.owncloud.android.utils.DisplayUtils;\r
 \r
+import java.security.cert.X509Certificate;\r
+import java.util.Map;\r
+\r
 /**\r
  * This Activity is used to add an ownCloud account to the App\r
  */\r
 /**\r
  * This Activity is used to add an ownCloud account to the App\r
  */\r
@@ -177,6 +176,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private EditText mUsernameInput;\r
     private EditText mPasswordInput;\r
     private View mOkButton;\r
     private EditText mUsernameInput;\r
     private EditText mPasswordInput;\r
     private View mOkButton;\r
+    private View mCenteredRefreshButton;\r
     private TextView mAuthStatusView;\r
 \r
     private int mAuthStatusText = 0, mAuthStatusIcon = 0;\r
     private TextView mAuthStatusView;\r
 \r
     private int mAuthStatusText = 0, mAuthStatusIcon = 0;\r
@@ -251,8 +251,43 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         \r
         /// initialize general UI elements\r
         initOverallUi();\r
         \r
         /// initialize general UI elements\r
         initOverallUi();\r
+\r
+        mOkButton = findViewById(R.id.buttonOK);\r
+        mOkButton.setOnClickListener(new View.OnClickListener() {\r
+\r
+            @Override\r
+            public void onClick(View v) {\r
+                onOkClick();\r
+            }\r
+        });\r
+\r
+        mCenteredRefreshButton = findViewById(R.id.centeredRefreshButton);\r
+        mCenteredRefreshButton.setOnClickListener(new View.OnClickListener() {\r
+\r
+            @Override\r
+            public void onClick(View v) {\r
+                checkOcServer();\r
+            }\r
+        });\r
         \r
         mOkButton = findViewById(R.id.buttonOK);\r
         \r
         mOkButton = findViewById(R.id.buttonOK);\r
+        mOkButton.setOnClickListener(new View.OnClickListener() {\r
+\r
+            @Override\r
+            public void onClick(View v) {\r
+                onOkClick();\r
+            }\r
+        });\r
+\r
+        mCenteredRefreshButton = findViewById(R.id.centeredRefreshButton);\r
+        mCenteredRefreshButton.setOnClickListener(new View.OnClickListener() {\r
+\r
+            @Override\r
+            public void onClick(View v) {\r
+                checkOcServer();\r
+            }\r
+        });\r
+\r
 \r
         /// initialize block to be moved to single Fragment to check server and get info about it \r
         initServerPreFragment(savedInstanceState);\r
 \r
         /// initialize block to be moved to single Fragment to check server and get info about it \r
         initServerPreFragment(savedInstanceState);\r
@@ -348,11 +383,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             if (mAccount != null) {\r
                 mServerInfo.mBaseUrl = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_BASE_URL);\r
                 // TODO do next in a setter for mBaseUrl\r
             if (mAccount != null) {\r
                 mServerInfo.mBaseUrl = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_BASE_URL);\r
                 // TODO do next in a setter for mBaseUrl\r
-                mServerInfo.mIsSslConn = mServerInfo.mBaseUrl.startsWith("https://");   \r
-                String ocVersion = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION);\r
-                if (ocVersion != null) {\r
-                    mServerInfo.mVersion = new OwnCloudVersion(ocVersion);\r
-                }\r
+                mServerInfo.mIsSslConn = mServerInfo.mBaseUrl.startsWith("https://");\r
+                mServerInfo.mVersion = AccountUtils.getServerVersion(mAccount);\r
             } else {\r
                 mServerInfo.mBaseUrl = getString(R.string.server_url).trim();\r
                 mServerInfo.mIsSslConn = mServerInfo.mBaseUrl.startsWith("https://");\r
             } else {\r
                 mServerInfo.mBaseUrl = getString(R.string.server_url).trim();\r
                 mServerInfo.mIsSslConn = mServerInfo.mBaseUrl.startsWith("https://");\r
@@ -570,7 +602,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * intended to defer the processing of the redirection caught in \r
      * {@link #onNewIntent(Intent)} until {@link #onResume()} \r
      * \r
      * intended to defer the processing of the redirection caught in \r
      * {@link #onNewIntent(Intent)} until {@link #onResume()} \r
      * \r
-     * See {@link #onSaveInstanceState(Bundle)}\r
+     * See {@link super#onSaveInstanceState(Bundle)}\r
      */\r
     @Override\r
     protected void onSaveInstanceState(Bundle outState) {\r
      */\r
     @Override\r
     protected void onSaveInstanceState(Bundle outState) {\r
@@ -687,7 +719,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         \r
         mHostUrlInput.removeTextChangedListener(mHostUrlInputWatcher);\r
         mHostUrlInput.setOnFocusChangeListener(null);\r
         \r
         mHostUrlInput.removeTextChangedListener(mHostUrlInputWatcher);\r
         mHostUrlInput.setOnFocusChangeListener(null);\r
-        \r
+\r
         super.onPause();\r
     }\r
     \r
         super.onPause();\r
     }\r
     \r
@@ -796,8 +828,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             Intent getServerInfoIntent = new Intent();\r
             getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);\r
             getServerInfoIntent.putExtra(\r
             Intent getServerInfoIntent = new Intent();\r
             getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);\r
             getServerInfoIntent.putExtra(\r
-                OperationsService.EXTRA_SERVER_URL,\r
-                normalizeUrlSuffix(uri)\r
+                    OperationsService.EXTRA_SERVER_URL,\r
+                    normalizeUrlSuffix(uri)\r
             );\r
             if (mOperationsServiceBinder != null) {\r
                 mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(getServerInfoIntent);\r
             );\r
             if (mOperationsServiceBinder != null) {\r
                 mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(getServerInfoIntent);\r
@@ -874,10 +906,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * is postponed until it is available.\r
      * \r
      * IMPORTANT ENTRY POINT 4\r
      * is postponed until it is available.\r
      * \r
      * IMPORTANT ENTRY POINT 4\r
-     * \r
-     * @param view      OK button\r
      */\r
      */\r
-    public void onOkClick(View view) {\r
+    public void onOkClick() {\r
         // this check should be unnecessary\r
         if (mServerInfo.mVersion == null || \r
                 !mServerInfo.mVersion.isVersionValid()  || \r
         // this check should be unnecessary\r
         if (mServerInfo.mVersion == null || \r
                 !mServerInfo.mVersion.isVersionValid()  || \r
@@ -1011,7 +1041,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
             if ( mAction == ACTION_CREATE) {\r
                 mUsernameInput.setText(username);\r
 \r
             if ( mAction == ACTION_CREATE) {\r
                 mUsernameInput.setText(username);\r
-                success = createAccount();\r
+                success = createAccount(result);\r
             } else {\r
 \r
                 if (!mUsernameInput.getText().toString().equals(username)) {\r
             } else {\r
 \r
                 if (!mUsernameInput.getText().toString().equals(username)) {\r
@@ -1136,7 +1166,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0_AND_LATER)){\r
             url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0_AND_LATER.length());\r
         }\r
         if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0_AND_LATER)){\r
             url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0_AND_LATER.length());\r
         }\r
-        return (url != null ? url : "");\r
+        return url;\r
     }\r
 \r
 \r
     }\r
 \r
 \r
@@ -1150,7 +1180,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
         switch (result.getCode()) {\r
         case OK_SSL:\r
 \r
         switch (result.getCode()) {\r
         case OK_SSL:\r
-            mServerStatusIcon = android.R.drawable.ic_secure;\r
+            mServerStatusIcon = R.drawable.ic_lock;\r
             mServerStatusText = R.string.auth_secure_connection;\r
             break;\r
 \r
             mServerStatusText = R.string.auth_secure_connection;\r
             break;\r
 \r
@@ -1161,7 +1191,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                 mServerStatusIcon = R.drawable.ic_ok;\r
             } else {\r
                 mServerStatusText = R.string.auth_nossl_plain_ok_title;\r
                 mServerStatusIcon = R.drawable.ic_ok;\r
             } else {\r
                 mServerStatusText = R.string.auth_nossl_plain_ok_title;\r
-                mServerStatusIcon = android.R.drawable.ic_partial_secure;\r
+                mServerStatusIcon = R.drawable.ic_lock_open;\r
             }\r
             break;\r
 \r
             }\r
             break;\r
 \r
@@ -1211,7 +1241,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mServerStatusText = R.string.auth_unknown_error_title;\r
             break;\r
         case OK_REDIRECT_TO_NON_SECURE_CONNECTION:\r
             mServerStatusText = R.string.auth_unknown_error_title;\r
             break;\r
         case OK_REDIRECT_TO_NON_SECURE_CONNECTION:\r
-            mServerStatusIcon = android.R.drawable.ic_partial_secure;\r
+            mServerStatusIcon = R.drawable.ic_lock_open;\r
             mServerStatusText = R.string.auth_redirect_non_secure_connection_title;\r
             break;\r
         default:\r
             mServerStatusText = R.string.auth_redirect_non_secure_connection_title;\r
             break;\r
         default:\r
@@ -1231,7 +1261,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
         switch (result.getCode()) {\r
         case OK_SSL:\r
 \r
         switch (result.getCode()) {\r
         case OK_SSL:\r
-            mAuthStatusIcon = android.R.drawable.ic_secure;\r
+            mAuthStatusIcon = R.drawable.ic_lock;\r
             mAuthStatusText = R.string.auth_secure_connection;\r
             break;\r
 \r
             mAuthStatusText = R.string.auth_secure_connection;\r
             break;\r
 \r
@@ -1242,7 +1272,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                 mAuthStatusIcon = R.drawable.ic_ok;\r
             } else {\r
                 mAuthStatusText = R.string.auth_nossl_plain_ok_title;\r
                 mAuthStatusIcon = R.drawable.ic_ok;\r
             } else {\r
                 mAuthStatusText = R.string.auth_nossl_plain_ok_title;\r
-                mAuthStatusIcon = android.R.drawable.ic_partial_secure;\r
+                mAuthStatusIcon = R.drawable.ic_lock_open;\r
             }\r
             break;\r
 \r
             }\r
             break;\r
 \r
@@ -1365,8 +1395,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             Log_OC.d(TAG, "Successful access - time to save the account");\r
 \r
             boolean success = false;\r
             Log_OC.d(TAG, "Successful access - time to save the account");\r
 \r
             boolean success = false;\r
+\r
             if (mAction == ACTION_CREATE) {\r
             if (mAction == ACTION_CREATE) {\r
-                success = createAccount();\r
+                success = createAccount(result);\r
 \r
             } else {\r
                 try {\r
 \r
             } else {\r
                 try {\r
@@ -1464,13 +1495,18 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * \r
      * TODO Decide how to name the OAuth accounts\r
      */\r
      * \r
      * TODO Decide how to name the OAuth accounts\r
      */\r
-    private boolean createAccount() {\r
+    private boolean createAccount(RemoteOperationResult authResult) {\r
         /// create and save new ownCloud account\r
         boolean isOAuth = AccountTypeUtils.\r
                 getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType);\r
         boolean isSaml =  AccountTypeUtils.\r
                 getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType);\r
 \r
         /// create and save new ownCloud account\r
         boolean isOAuth = AccountTypeUtils.\r
                 getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType);\r
         boolean isSaml =  AccountTypeUtils.\r
                 getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType);\r
 \r
+        String lastPermanentLocation = authResult.getLastPermanentLocation();\r
+        if (lastPermanentLocation != null) {\r
+            mServerInfo.mBaseUrl = AccountUtils.trimWebdavSuffix(lastPermanentLocation);\r
+        }\r
+\r
         Uri uri = Uri.parse(mServerInfo.mBaseUrl);\r
         String username = mUsernameInput.getText().toString().trim();\r
         if (isOAuth) {\r
         Uri uri = Uri.parse(mServerInfo.mBaseUrl);\r
         String username = mUsernameInput.getText().toString().trim();\r
         if (isOAuth) {\r
@@ -1528,7 +1564,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             /// add user data to the new account; TODO probably can be done in the last parameter \r
             //      addAccountExplicitly, or in KEY_USERDATA\r
             mAccountMgr.setUserData(\r
             /// add user data to the new account; TODO probably can be done in the last parameter \r
             //      addAccountExplicitly, or in KEY_USERDATA\r
             mAccountMgr.setUserData(\r
-                    mAccount, Constants.KEY_OC_VERSION,    mServerInfo.mVersion.getVersion()\r
+                    mAccount, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion()\r
             );\r
             mAccountMgr.setUserData(\r
                     mAccount, Constants.KEY_OC_BASE_URL,   mServerInfo.mBaseUrl\r
             );\r
             mAccountMgr.setUserData(\r
                     mAccount, Constants.KEY_OC_BASE_URL,   mServerInfo.mBaseUrl\r
@@ -1605,18 +1641,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     }\r
 \r
     /**\r
     }\r
 \r
     /**\r
-     * Called when the refresh button in the input field for ownCloud host is clicked.\r
-     * \r
-     * Performs a new check on the URL in the input field.\r
-     * \r
-     * @param view      Refresh 'button'\r
-     */\r
-    public void onRefreshClick(View view) {\r
-        checkOcServer();\r
-    }\r
-\r
-\r
-    /**\r
      * Called when the eye icon in the password field is clicked.\r
      * \r
      * Toggles the visibility of the password in the field. \r
      * Called when the eye icon in the password field is clicked.\r
      * \r
      * Toggles the visibility of the password in the field. \r
@@ -1711,7 +1735,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     }\r
 \r
 \r
     }\r
 \r
 \r
-    private void getRemoteUserNameOperation(String sessionCookie, boolean followRedirects) {\r
+    private void getRemoteUserNameOperation(String sessionCookie) {\r
         \r
         Intent getUserNameIntent = new Intent();\r
         getUserNameIntent.setAction(OperationsService.ACTION_GET_USER_NAME);\r
         \r
         Intent getUserNameIntent = new Intent();\r
         getUserNameIntent.setAction(OperationsService.ACTION_GET_USER_NAME);\r
@@ -1729,7 +1753,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if (sessionCookie != null && sessionCookie.length() > 0) {\r
             Log_OC.d(TAG, "Successful SSO - time to save the account");\r
             mAuthToken = sessionCookie;\r
         if (sessionCookie != null && sessionCookie.length() > 0) {\r
             Log_OC.d(TAG, "Successful SSO - time to save the account");\r
             mAuthToken = sessionCookie;\r
-            getRemoteUserNameOperation(sessionCookie, true);\r
+            getRemoteUserNameOperation(sessionCookie);\r
             Fragment fd = getSupportFragmentManager().findFragmentByTag(SAML_DIALOG_TAG);\r
             if (fd != null && fd instanceof DialogFragment) {\r
                 Dialog d = ((DialogFragment)fd).getDialog();\r
             Fragment fd = getSupportFragmentManager().findFragmentByTag(SAML_DIALOG_TAG);\r
             if (fd != null && fd instanceof DialogFragment) {\r
                 Dialog d = ((DialogFragment)fd).getDialog();\r
@@ -1763,7 +1787,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             X509Certificate x509Certificate, SslError error, SslErrorHandler handler\r
         ) {\r
         // Show a dialog with the certificate info\r
             X509Certificate x509Certificate, SslError error, SslErrorHandler handler\r
         ) {\r
         // Show a dialog with the certificate info\r
-        SslUntrustedCertDialog dialog = null;\r
+        SslUntrustedCertDialog dialog;\r
         if (x509Certificate == null) {\r
             dialog = SslUntrustedCertDialog.newInstanceForEmptySslError(error, handler);\r
         } else {\r
         if (x509Certificate == null) {\r
             dialog = SslUntrustedCertDialog.newInstanceForEmptySslError(error, handler);\r
         } else {\r
@@ -1855,8 +1879,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                 \r
                 doOnResumeAndBound();\r
                 \r
                 \r
                 doOnResumeAndBound();\r
                 \r
-            } else {\r
-                return;\r
             }\r
             \r
         }\r
             }\r
             \r
         }\r
@@ -1875,8 +1897,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
     /**\r
      * Create and show dialog for request authentication to the user\r
 \r
     /**\r
      * Create and show dialog for request authentication to the user\r
-     * @param webView\r
-     * @param handler\r
+     * @param webView   Web view to emebd into the authentication dialog.\r
+     * @param handler   Object responsible for catching and recovering HTTP authentication fails.\r
      */\r
     public void createAuthenticationDialog(WebView webView, HttpAuthHandler handler) {\r
 \r
      */\r
     public void createAuthenticationDialog(WebView webView, HttpAuthHandler handler) {\r
 \r