X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/2d6826fe8b060e2bf4573e909e917af4741c9f14..08aa08ce603e65a51f0dea6976b4b8d3a1f5f3a2:/src/com/owncloud/android/authentication/AuthenticatorActivity.java diff --git a/src/com/owncloud/android/authentication/AuthenticatorActivity.java b/src/com/owncloud/android/authentication/AuthenticatorActivity.java index 9dcddcee..d9e1bea5 100644 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -25,8 +25,11 @@ import android.accounts.AccountManager; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; +import android.content.ComponentName; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.ServiceConnection; import android.content.SharedPreferences; import android.graphics.Rect; import android.graphics.drawable.Drawable; @@ -34,6 +37,7 @@ import android.net.Uri; import android.net.http.SslError; import android.os.Bundle; import android.os.Handler; +import android.os.IBinder; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -54,6 +58,7 @@ import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; +import android.widget.Toast; import com.actionbarsherlock.app.SherlockDialogFragment; import com.owncloud.android.MainApp; @@ -63,17 +68,20 @@ import com.owncloud.android.lib.common.accounts.AccountTypeUtils; import com.owncloud.android.lib.common.accounts.AccountUtils.Constants; import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod; +import com.owncloud.android.operations.GetServerInfoOperation; import com.owncloud.android.operations.OAuth2GetAccessToken; import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; -import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation; import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation; - + +import com.owncloud.android.services.OperationsService; +import com.owncloud.android.services.OperationsService.OperationsServiceBinder; import com.owncloud.android.ui.dialog.SamlWebViewDialog; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener; @@ -87,8 +95,8 @@ import com.owncloud.android.lib.resources.status.OwnCloudVersion; * @author David A. Velasco */ public class AuthenticatorActivity extends AccountAuthenticatorActivity - implements OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener, - SsoWebViewClientListener, OnSslUntrustedCertListener { +implements OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener, +SsoWebViewClientListener, OnSslUntrustedCertListener { private static final String TAG = AuthenticatorActivity.class.getSimpleName(); @@ -115,11 +123,14 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON"; private static final String KEY_REFRESH_BUTTON_ENABLED = "KEY_REFRESH_BUTTON_ENABLED"; //private static final String KEY_IS_SHARED_SUPPORTED = "KEY_IS_SHARE_SUPPORTED"; + private static final String KEY_SERVER_AUTH_METHOD = "KEY_SERVER_AUTH_METHOD"; + private static final String KEY_DETECT_AUTH_OP_ID = "KEY_DETECT_AUTH_OP_ID"; + private static final String AUTH_ON = "on"; private static final String AUTH_OFF = "off"; private static final String AUTH_OPTIONAL = "optional"; - + private static final int DIALOG_LOGIN_PROGRESS = 0; private static final int DIALOG_CERT_NOT_SAVED = 1; private static final int DIALOG_OAUTH2_LOGIN_PROGRESS = 2; @@ -128,19 +139,23 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity public static final byte ACTION_UPDATE_TOKEN = 1; private static final String TAG_SAML_DIALOG = "samlWebViewDialog"; - - private String mHostBaseUrl; - private OwnCloudVersion mDiscoveredVersion; + + private String mHostBaseUrl; // TODO remove + private OwnCloudVersion mDiscoveredVersion; // TODO remove private String mAuthMessageText; private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon; private boolean mServerIsChecked, mServerIsValid, mIsSslConn; + private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN; + private int mGetServerInfoOpId = -1; + private int mAuthStatusText, mAuthStatusIcon; private TextView mAuthStatusLayout; private final Handler mHandler = new Handler(); private Thread mOperationThread; - private GetRemoteStatusOperation mOcServerChkOperation; + //private GetRemoteStatusOperation mOcServerChkOperation; + private GetServerInfoOperation mServerInfoOperation; private ExistenceCheckRemoteOperation mAuthCheckOperation; private Uri mNewCapturedUriFromOAuth2Redirection; @@ -151,31 +166,36 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private Account mAccount; private TextView mAuthMessage; - + private EditText mHostUrlInput; private boolean mHostUrlInputEnabled; private View mRefreshButton; private String mAuthTokenType; - + private EditText mUsernameInput; private EditText mPasswordInput; - + private CheckBox mOAuth2Check; - + private TextView mOAuthAuthEndpointText; private TextView mOAuthTokenEndpointText; - + private SamlWebViewDialog mSamlDialog; - + private View mOkButton; - + private String mAuthToken; - + private boolean mResumed; // Control if activity is resumed public static String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT"; + + private ServiceConnection mOperationsServiceConnection = null; + + private OperationsServiceBinder mOperationsServiceBinder = null; + private GetServerInfoOperation.ServerInfo mServerInfo; /** * {@inheritDoc} @@ -187,6 +207,18 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_NO_TITLE); + // bind to Operations Service + mOperationsServiceConnection = new OperationsServiceConnection(); + if (!bindService(new Intent(this, OperationsService.class), + mOperationsServiceConnection, + Context.BIND_AUTO_CREATE)) { + Toast.makeText(this, + R.string.error_cant_bind_to_operations_service, + Toast.LENGTH_LONG) + .show(); + finish(); + } + /// set view and get references to view elements setContentView(R.layout.account_setup); mAuthMessage = (TextView) findViewById(R.id.auth_message); @@ -199,10 +231,10 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mOAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check); mOkButton = findViewById(R.id.buttonOK); mAuthStatusLayout = (TextView) findViewById(R.id.auth_status_text); - + /// set Host Url Input Enabled mHostUrlInputEnabled = getResources().getBoolean(R.bool.show_server_url_input); - + /// set visibility of link for new users boolean accountRegisterVisibility = getResources().getBoolean(R.bool.show_welcome_link); Button welcomeLink = (Button) findViewById(R.id.welcome_link); @@ -222,7 +254,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAccount = null; mHostBaseUrl = ""; boolean refreshButtonEnabled = false; - + // URL input configuration applied if (!mHostUrlInputEnabled) { @@ -255,15 +287,15 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mHostUrlInput.setText(mHostBaseUrl); String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@')); mUsernameInput.setText(userName); - + } initAuthorizationMethod(); // checks intent and setup.xml to determine mCurrentAuthorizationMethod mJustCreated = true; - + if (mAction == ACTION_UPDATE_TOKEN || !mHostUrlInputEnabled) { checkOcServer(); } - + } else { mResumed = true; /// connection state and info @@ -279,7 +311,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity if (savedInstanceState.getBoolean(KEY_PASSWORD_VISIBLE, false)) { showPassword(); } - + /// server data String ocVersion = savedInstanceState.getString(KEY_OC_VERSION); String ocVersionString = savedInstanceState.getString(KEY_OC_VERSION_STRING); @@ -293,17 +325,21 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAuthTokenType = savedInstanceState.getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE); if (mAuthTokenType == null) { mAuthTokenType = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); - + } // check if server check was interrupted by a configuration change if (savedInstanceState.getBoolean(KEY_SERVER_CHECK_IN_PROGRESS, false)) { checkOcServer(); } - + // refresh button enabled refreshButtonEnabled = savedInstanceState.getBoolean(KEY_REFRESH_BUTTON_ENABLED); - + + + mServerAuthMethod = AuthenticationMethod.valueOf( + savedInstanceState.getString(KEY_SERVER_AUTH_METHOD)); + mGetServerInfoOpId = savedInstanceState.getInt(KEY_DETECT_AUTH_OP_ID); } @@ -316,7 +352,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity adaptViewAccordingToAuthenticationMethod(); showServerStatus(); showAuthStatus(); - + if (mAction == ACTION_UPDATE_TOKEN) { /// lock things that should not change mHostUrlInput.setEnabled(false); @@ -325,9 +361,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mUsernameInput.setFocusable(false); mOAuth2Check.setVisibility(View.GONE); } - - //if (mServerIsChecked && !mServerIsValid && mRefreshButtonEnabled) showRefreshButton(); - if (mServerIsChecked && !mServerIsValid && refreshButtonEnabled) showRefreshButton(); + + showRefreshButton(mServerIsChecked && !mServerIsValid && refreshButtonEnabled); mOkButton.setEnabled(mServerIsValid); // state not automatically recovered in configuration changes if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType) || @@ -364,7 +399,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mResumed = false; } }); - + mPasswordInput.setOnFocusChangeListener(this); mPasswordInput.setImeOptions(EditorInfo.IME_ACTION_DONE); mPasswordInput.setOnEditorActionListener(this); @@ -377,7 +412,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity return true; } }); - + findViewById(R.id.scroll).setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { @@ -390,9 +425,10 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity return false; } }); + } - - + + private void initAuthorizationMethod() { boolean oAuthRequired = false; @@ -400,15 +436,15 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAuthTokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE); mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT); - + // TODO could be a good moment to validate the received token type, if not null - + if (mAuthTokenType == null) { if (mAccount != null) { /// same authentication method than the one used to create the account to update oAuthRequired = (mAccountMgr.getUserData(mAccount, Constants.KEY_SUPPORTS_OAUTH2) != null); samlWebSsoRequired = (mAccountMgr.getUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null); - + } else { /// use the one set in setup.xml oAuthRequired = AUTH_ON.equals(getString(R.string.auth_method_oauth2)); @@ -422,14 +458,14 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAuthTokenType = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); } } - + if (mAccount != null) { String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@')); mUsernameInput.setText(userName); } - + mOAuth2Check.setChecked(AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)); - + } /** @@ -442,6 +478,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity */ @Override protected void onSaveInstanceState(Bundle outState) { + //Log.wtf(TAG, "onSaveInstanceState init" ); super.onSaveInstanceState(outState); /// connection state and info @@ -451,7 +488,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity outState.putInt(KEY_SERVER_STATUS_ICON, mServerStatusIcon); outState.putBoolean(KEY_SERVER_VALID, mServerIsValid); outState.putBoolean(KEY_SERVER_CHECKED, mServerIsChecked); - outState.putBoolean(KEY_SERVER_CHECK_IN_PROGRESS, (!mServerIsValid && mOcServerChkOperation != null)); + outState.putBoolean(KEY_SERVER_CHECK_IN_PROGRESS, (!mServerIsValid && mServerInfoOperation != null)); outState.putBoolean(KEY_IS_SSL_CONN, mIsSslConn); outState.putBoolean(KEY_PASSWORD_VISIBLE, isPasswordVisible()); outState.putInt(KEY_AUTH_STATUS_ICON, mAuthStatusIcon); @@ -469,11 +506,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity outState.putParcelable(KEY_ACCOUNT, mAccount); } outState.putString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE, mAuthTokenType); - + // refresh button enabled outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE)); - + outState.putString(KEY_SERVER_AUTH_METHOD, mServerAuthMethod.name()); + outState.putInt(KEY_DETECT_AUTH_OP_ID, mGetServerInfoOpId); + //Log.wtf(TAG, "onSaveInstanceState end" ); } @@ -500,7 +539,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity */ @Override protected void onResume() { + //Log.wtf(TAG, "onResume init" ); super.onResume(); + if (mAction == ACTION_UPDATE_TOKEN && mJustCreated && getIntent().getBooleanExtra(EXTRA_ENFORCED_UPDATE, false)) { if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) { //Toast.makeText(this, R.string.auth_expired_oauth_token_toast, Toast.LENGTH_LONG).show(); @@ -517,9 +558,35 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity if (mNewCapturedUriFromOAuth2Redirection != null) { getOAuth2AccessTokenFromCapturedRedirection(); } - + mJustCreated = false; + + if (mOperationsServiceBinder != null) { + doOnResumeAndBound(); + } + //Log.wtf(TAG, "onResume end" ); + } + + + @Override + protected void onPause() { + //Log.wtf(TAG, "onPause init" ); + if (mOperationsServiceBinder != null) { + //Log.wtf(TAG, "unregistering to listen for operation callbacks" ); + mOperationsServiceBinder.removeOperationListener(this); + } + super.onPause(); + //Log.wtf(TAG, "onPause end" ); + } + + @Override + protected void onDestroy() { + if (mOperationsServiceConnection != null) { + unbindService(mOperationsServiceConnection); + mOperationsServiceBinder = null; + } + super.onDestroy(); } @@ -556,7 +623,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity onUrlInputFocusLost((TextView) view); } else { - hideRefreshButton(); + showRefreshButton(false); } } else if (view.getId() == R.id.account_password) { @@ -581,32 +648,45 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity checkOcServer(); } else { mOkButton.setEnabled(mServerIsValid); - if (!mServerIsValid) { - showRefreshButton(); - } + showRefreshButton(!mServerIsValid); } } private void checkOcServer() { - String uri = trimUrlWebdav(mHostUrlInput.getText().toString().trim()); - + String uri = mHostUrlInput.getText().toString().trim(); + if (!mHostUrlInputEnabled){ - uri = getString(R.string.server_url); + uri = getString(R.string.server_url).trim(); } - + mServerIsValid = false; mServerIsChecked = false; mOkButton.setEnabled(false); mDiscoveredVersion = null; - hideRefreshButton(); + mServerAuthMethod = AuthenticationMethod.UNKNOWN; + showRefreshButton(false); + if (uri.length() != 0) { mServerStatusText = R.string.auth_testing_connection; mServerStatusIcon = R.drawable.progress_small; showServerStatus(); - mOcServerChkOperation = new GetRemoteStatusOperation(uri, this); + + /* + mServerInfoOperation = new GetServerInfoOperation(uri, mAuthTokenType, this); OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(uri), this, true); - mOperationThread = mOcServerChkOperation.execute(client, this, mHandler); + mServerInfoOperation.execute(client, this, mHandler); + */ + + Intent getServerInfoIntent = new Intent(); + getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO); + getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri); + getServerInfoIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN_TYPE, mAuthTokenType); + if (mOperationsServiceBinder != null) { + //Log.wtf(TAG, "checking server..." ); + mGetServerInfoOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent); + } + } else { mServerStatusText = 0; mServerStatusIcon = 0; @@ -648,7 +728,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private boolean isPasswordVisible() { return ((mPasswordInput.getInputType() & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); } - + private void hidePasswordButton() { mPasswordInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } @@ -657,13 +737,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mPasswordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); showViewPasswordButton(); } - + private void hidePassword() { mPasswordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); showViewPasswordButton(); } - - + + /** * Cancels the authenticator activity * @@ -747,7 +827,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAuthStatusIcon = R.drawable.progress_small; mAuthStatusText = R.string.oauth_login_connection; showAuthStatus(); - + // GET AUTHORIZATION request //Uri uri = Uri.parse(getString(R.string.oauth2_url_endpoint_auth)); @@ -775,7 +855,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAuthStatusText = R.string.auth_connecting_auth_server; showAuthStatus(); showDialog(DIALOG_LOGIN_PROGRESS); - + /// get the path to the root folder through WebDAV from the version server String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType); @@ -783,7 +863,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false); OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, false); mOperationThread = mAuthCheckOperation.execute(client, this, mHandler); - + } /** @@ -794,8 +874,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { - if (operation instanceof GetRemoteStatusOperation) { - onOcServerCheckFinish((GetRemoteStatusOperation) operation, result); + if (operation instanceof GetServerInfoOperation) { + if (operation.hashCode() == mGetServerInfoOpId) { + onGetServerInfoFinish(result); + } // else nothing ; only the last check operation is considered; + // multiple can be started if the user amends a URL quickly } else if (operation instanceof OAuth2GetAccessToken) { onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result); @@ -803,28 +886,28 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } else if (operation instanceof ExistenceCheckRemoteOperation) { if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) { onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result); - + } else { onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result); } } else if (operation instanceof GetRemoteUserNameOperation) { onGetUserNameFinish((GetRemoteUserNameOperation) operation, result); - + } - + } private void onGetUserNameFinish(GetRemoteUserNameOperation operation, RemoteOperationResult result) { - + if (result.isSuccess()) { boolean success = false; String username = operation.getUserName(); - + if ( mAction == ACTION_CREATE) { mUsernameInput.setText(username); success = createAccount(); } else { - + if (!mUsernameInput.getText().toString().equals(username)) { // fail - not a new account, but an existing one; disallow result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME); @@ -832,11 +915,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity showAuthStatus(); Log_OC.d(TAG, result.getLogMessage()); } else { - updateToken(); - success = true; + updateToken(); + success = true; } } - + if (success) finish(); } else { @@ -844,7 +927,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity showAuthStatus(); Log_OC.e(TAG, "Access to user name failed: " + result.getLogMessage()); } - + } private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) { @@ -853,23 +936,23 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } catch (IllegalArgumentException e) { // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens } - + //if (result.isTemporalRedirection() && result.isIdPRedirection()) { if (result.isIdPRedirection()) { String url = result.getRedirectedLocation(); String targetUrl = mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType); - + // Show dialog mSamlDialog = SamlWebViewDialog.newInstance(url, targetUrl); mSamlDialog.show(getSupportFragmentManager(), TAG_SAML_DIALOG); - + mAuthStatusIcon = 0; mAuthStatusText = 0; - + } else { mAuthStatusIcon = R.drawable.common_error; mAuthStatusText = R.string.auth_unsupported_auth_method; - + } showAuthStatus(); } @@ -882,40 +965,68 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * @param operation Server check performed. * @param result Result of the check. */ - private void onOcServerCheckFinish(GetRemoteStatusOperation operation, RemoteOperationResult result) { - if (operation.equals(mOcServerChkOperation)) { - /// save result state - mServerIsChecked = true; - mServerIsValid = result.isSuccess(); - mIsSslConn = (result.getCode() == ResultCode.OK_SSL); - mOcServerChkOperation = null; + private void onGetServerInfoFinish(RemoteOperationResult result) { + /// update activity state + mServerIsChecked = true; + mIsSslConn = (result.getCode() == ResultCode.OK_SSL); + mServerInfoOperation = null; + mGetServerInfoOpId = -1; + + // update server status, but don't show it yet + updateServerStatusIconAndText(result); + + if (result.isSuccess()) { + /// SUCCESS means: + // 1. connection succeeded, and we know if it's SSL or not + // 2. server is installed + // 3. we got the server version + // 4. we got the authentication method required by the server + mServerInfo = (GetServerInfoOperation.ServerInfo) (result.getData().get(0)); + mDiscoveredVersion = mServerInfo.mVersion; + mHostBaseUrl = mServerInfo.mBaseUrl; + mServerAuthMethod = mServerInfo.mAuthMethod; + + if (!authSupported(mServerAuthMethod)) { + + updateServerStatusIconNoRegularAuth(); // overrides updateServerStatusIconAndText() + mServerIsValid = false; - /// update status icon and text - if (mServerIsValid) { - hideRefreshButton(); } else { - showRefreshButton(); + mServerIsValid = true; } - updateServerStatusIconAndText(result); - showServerStatus(); + + } else { + mServerIsValid = false; + } - /// very special case (TODO: move to a common place for all the remote operations) - if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) { - showUntrustedCertDialog(result); - } + // refresh UI + showRefreshButton(!mServerIsValid); + showServerStatus(); + mOkButton.setEnabled(mServerIsValid); + + /// very special case (TODO: move to a common place for all the remote operations) + if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) { + showUntrustedCertDialog(result); + } + } - /// retrieve discovered version and normalize server URL - mDiscoveredVersion = operation.getDiscoveredVersion(); - mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString()); - /// allow or not the user try to access the server - mOkButton.setEnabled(mServerIsValid); - - } // else nothing ; only the last check operation is considered; - // multiple can be triggered if the user amends a URL before a previous check can be triggered + private boolean authSupported(AuthenticationMethod authMethod) { + String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); + String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()); + String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()); + + return (( mAuthTokenType.equals(basic) && + authMethod.equals(AuthenticationMethod.BASIC_HTTP_AUTH) ) || + ( mAuthTokenType.equals(oAuth) && + authMethod.equals(AuthenticationMethod.BEARER_TOKEN)) || + ( mAuthTokenType.equals(saml) && + authMethod.equals(AuthenticationMethod.SAML_WEB_SSO)) + ); } + // TODO remove, if possible private String normalizeUrl(String url) { if (url != null && url.length() > 0) { url = url.trim(); @@ -940,6 +1051,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } + // TODO remove, if possible private String trimUrlWebdav(String url){ if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0)){ url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0.length()); @@ -950,8 +1062,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } return (url != null ? url : ""); } - - + + /** * Chooses the right icon and text to show to the user for the received operation result. * @@ -1113,10 +1225,15 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private void updateStatusIconFailUserName(){ - mAuthStatusIcon = android.R.drawable.ic_secure; + mAuthStatusIcon = R.drawable.common_error; mAuthStatusText = R.string.auth_fail_get_user_name; } - + + private void updateServerStatusIconNoRegularAuth(){ + mServerStatusIcon = R.drawable.common_error; + mServerStatusText = R.string.auth_can_not_auth_against_server; + } + /** * Processes the result of the request for and access token send * to an OAuth authorization server. @@ -1182,13 +1299,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity if (success) { finish(); } - - } else if (result.isServerFail() || result.isException()) { + + } else if (result.isServerFail() || result.isException()) { /// if server fail or exception in authorization, the UI is updated as when a server check failed mServerIsChecked = true; mServerIsValid = false; mIsSslConn = false; - mOcServerChkOperation = null; + mServerInfoOperation = null; mDiscoveredVersion = null; mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString()); @@ -1198,9 +1315,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAuthStatusIcon = 0; mAuthStatusText = 0; showAuthStatus(); - + // update input controls state - showRefreshButton(); + showRefreshButton(true); mOkButton.setEnabled(false); // very special case (TODO: move to a common place for all the remote operations) (dangerous here?) @@ -1213,10 +1330,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity showAuthStatus(); Log_OC.d(TAG, "Access failed: " + result.getLogMessage()); } - } + + /** * Sets the proper response to get that the Account Authenticator that started this activity saves * a new authorization token for mAccount. @@ -1225,24 +1343,24 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity Bundle response = new Bundle(); response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name); response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type); - + if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) { response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken); // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken); - + } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) { - + response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken); // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken); - + } else { response.putString(AccountManager.KEY_AUTHTOKEN, mPasswordInput.getText().toString()); mAccountMgr.setPassword(mAccount, mPasswordInput.getText().toString()); } setAccountAuthenticatorResult(response); - + } @@ -1275,15 +1393,15 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity showAuthStatus(); Log_OC.d(TAG, result.getLogMessage()); return false; - + } else { - + if (isOAuth || isSaml) { mAccountMgr.addAccountExplicitly(mAccount, "", null); // with external authorizations, the password is never input in the app } else { mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null); } - + /// add the new account as default in preferences, if there is none already Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this); if (defaultAccount == null) { @@ -1292,7 +1410,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity editor.putString("select_oc_account", accountName); editor.commit(); } - + /// prepare result to return to the Authenticator // TODO check again what the Authenticator makes with it; probably has the same effect as addAccountExplicitly, but it's not well done final Intent intent = new Intent(); @@ -1308,16 +1426,16 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION, mDiscoveredVersion.getVersion()); mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION_STRING, mDiscoveredVersion.getVersionString()); mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL, mHostBaseUrl); - + if (isSaml) { mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); } else if (isOAuth) { mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE"); } - + setAccountAuthenticatorResult(intent.getExtras()); setResult(RESULT_OK, intent); - + return true; } } @@ -1454,12 +1572,12 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } - private void showRefreshButton() { - mRefreshButton.setVisibility(View.VISIBLE); - } - - private void hideRefreshButton() { - mRefreshButton.setVisibility(View.GONE); + private void showRefreshButton (boolean show) { + if (show) { + mRefreshButton.setVisibility(View.VISIBLE); + } else { + mRefreshButton.setVisibility(View.GONE); + } } /** @@ -1472,8 +1590,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity public void onRefreshClick(View view) { checkOcServer(); } - - + + /** * Called when the eye icon in the password field is clicked. * @@ -1508,7 +1626,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity adaptViewAccordingToAuthenticationMethod(); } - + /** * Changes the visibility of input elements depending on * the current authorization method. @@ -1520,7 +1638,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mOAuthTokenEndpointText.setVisibility(View.VISIBLE); mUsernameInput.setVisibility(View.GONE); mPasswordInput.setVisibility(View.GONE); - + } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) { // SAML-based web Single Sign On mOAuthAuthEndpointText.setVisibility(View.GONE); @@ -1535,7 +1653,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mPasswordInput.setVisibility(View.VISIBLE); } } - + /** * Called when the 'action' button in an IME is pressed ('enter' in software keyboard). * @@ -1548,7 +1666,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity if (mOkButton.isEnabled()) { mOkButton.performClick(); } - + } else if (actionId == EditorInfo.IME_ACTION_NEXT && inputField != null && inputField.equals(mHostUrlInput)) { if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) { checkOcServer(); @@ -1561,7 +1679,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private abstract static class RightDrawableOnTouchListener implements OnTouchListener { private int fuzz = 75; - + /** * {@inheritDoc} */ @@ -1579,8 +1697,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity final int y = (int) event.getY(); final Rect bounds = rightDrawable.getBounds(); if (x >= (view.getRight() - bounds.width() - fuzz) && x <= (view.getRight() - view.getPaddingRight() + fuzz) - && y >= (view.getPaddingTop() - fuzz) && y <= (view.getHeight() - view.getPaddingBottom()) + fuzz) { - + && y >= (view.getPaddingTop() - fuzz) && y <= (view.getHeight() - view.getPaddingBottom()) + fuzz) { + return onDrawableTouch(event); } } @@ -1593,7 +1711,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity public void onSamlDialogSuccess(String sessionCookie) { mAuthToken = sessionCookie; - + if (sessionCookie != null && sessionCookie.length() > 0) { mAuthToken = sessionCookie; @@ -1603,7 +1721,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity getUserOperation.execute(client, this, mHandler); } - + } @@ -1626,18 +1744,18 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity // TODO - show fail Log_OC.d(TAG, "SSO failed"); } - + } - + /** Show auth_message * * @param message */ private void showAuthMessage(String message) { - mAuthMessage.setVisibility(View.VISIBLE); - mAuthMessage.setText(message); + mAuthMessage.setVisibility(View.VISIBLE); + mAuthMessage.setText(message); } - + private void hideAuthMessage() { mAuthMessage.setVisibility(View.GONE); } @@ -1668,7 +1786,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity ft.addToBackStack(null); dialog.show(ft, DIALOG_UNTRUSTED_CERT); } - + /** * Show untrusted cert dialog */ @@ -1679,9 +1797,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity FragmentTransaction ft = fm.beginTransaction(); ft.addToBackStack(null); dialog.show(ft, DIALOG_UNTRUSTED_CERT); - + } - + /** * Dismiss untrusted cert dialog */ @@ -1691,9 +1809,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity SslErrorViewAdapter dialog = (SslErrorViewAdapter) frag; dialog.dismiss(); } - */ + */ } - + /** * Called from SslValidatorDialog when a new server certificate was correctly saved. */ @@ -1719,7 +1837,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity public void onCancelCertificate() { cancelWebView(); } - + public void cancelWebView() { Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG); @@ -1729,7 +1847,51 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity d.dismiss(); } } + + } + + + private void doOnResumeAndBound() { + //Log.wtf(TAG, "registering to listen for operation callbacks" ); + mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler); + if (mGetServerInfoOpId != -1) { + RemoteOperationResult result = + mOperationsServiceBinder.getOperationResultIfFinished(mGetServerInfoOpId); + if (result != null) { + //Log.wtf(TAG, "found result of operation finished while rotating"); + onGetServerInfoFinish(result); + } + } } + + /** + * Implements callback methods for service binding. + */ + private class OperationsServiceConnection implements ServiceConnection { + + @Override + public void onServiceConnected(ComponentName component, IBinder service) { + if (component.equals(new ComponentName(AuthenticatorActivity.this, OperationsService.class))) { + //Log_OC.wtf(TAG, "Operations service connected"); + mOperationsServiceBinder = (OperationsServiceBinder) service; + + doOnResumeAndBound(); + + } else { + return; + } + + } + @Override + public void onServiceDisconnected(ComponentName component) { + if (component.equals(new ComponentName(AuthenticatorActivity.this, OperationsService.class))) { + Log_OC.e(TAG, "Operations service crashed"); + mOperationsServiceBinder = null; + } + } + + } + }