X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/2e5b40e357a5ab18b3a04f823b7cd27d5c94dc90..4f524a2043b5946791586d1408226f8de33115c0:/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 cc1b6482..141cd54b 100644 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -3,9 +3,8 @@ * 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 as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. + * 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 @@ -20,6 +19,7 @@ package com.owncloud.android.authentication; import com.owncloud.android.AccountUtils; +import com.owncloud.android.Log_OC; import com.owncloud.android.ui.dialog.SslValidatorDialog; import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener; import com.owncloud.android.utils.OwnCloudVersion; @@ -42,21 +42,27 @@ import android.content.ContentResolver; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.text.InputType; -import android.util.Log; +import android.view.KeyEvent; +import android.view.MotionEvent; import android.view.View; import android.view.View.OnFocusChangeListener; +import android.view.View.OnTouchListener; import android.view.Window; +import android.view.inputmethod.EditorInfo; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; +import android.widget.TextView.OnEditorActionListener; import com.owncloud.android.R; @@ -69,7 +75,7 @@ import eu.alefzero.webdav.WebdavClient; * @author David A. Velasco */ public class AuthenticatorActivity extends AccountAuthenticatorActivity - implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener { + implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener, OnEditorActionListener { private static final String TAG = AuthenticatorActivity.class.getSimpleName(); @@ -121,7 +127,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private byte mAction; private Account mAccount; - private ImageView mRefreshButton; private ImageView mViewPasswordButton; private EditText mHostUrlInput; private EditText mUsernameInput; @@ -147,7 +152,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity /// set view and get references to view elements setContentView(R.layout.account_setup); - mRefreshButton = (ImageView) findViewById(R.id.refreshButton); mViewPasswordButton = (ImageView) findViewById(R.id.viewPasswordButton); mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput); mUsernameInput = (EditText) findViewById(R.id.account_username); @@ -166,7 +170,16 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity /// bind view elements to listeners mHostUrlInput.setOnFocusChangeListener(this); + mHostUrlInput.setOnTouchListener(new RightDrawableOnTouchListener() { + @Override + public boolean onDrawableTouch(final MotionEvent event) { + AuthenticatorActivity.this.onRefreshClick(mHostUrlInput); + return true; + } + }); mPasswordInput.setOnFocusChangeListener(this); + mPasswordInput.setImeOptions(EditorInfo.IME_ACTION_DONE); + mPasswordInput.setOnEditorActionListener(this); /// initialization mAccountMgr = AccountManager.get(this); @@ -179,6 +192,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mStatusText = mStatusIcon = 0; mStatusCorrect = false; mIsSslConn = false; + updateConnStatus(); + updateAuthStatus(); /// retrieve extras from intent String tokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE); @@ -271,11 +286,12 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity updateConnStatus(); /// UI settings depending upon connection - mOkButton.setEnabled(mStatusCorrect); // TODO really necessary? - if (!mStatusCorrect) - mRefreshButton.setVisibility(View.VISIBLE); // seems that setting visibility is necessary - else - mRefreshButton.setVisibility(View.INVISIBLE); + mOkButton.setEnabled(mStatusCorrect); + if (!mStatusCorrect) { + mHostUrlInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_action_refresh_black, 0); + } else { + mHostUrlInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + } /// server data String ocVersion = savedInstanceState.getString(KEY_OC_VERSION); @@ -302,7 +318,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity */ @Override protected void onNewIntent (Intent intent) { - Log.d(TAG, "onNewIntent()"); + Log_OC.d(TAG, "onNewIntent()"); Uri data = intent.getData(); if (data != null && data.toString().startsWith(getString(R.string.oauth2_redirect_uri))) { mNewCapturedUriFromOAuth2Redirection = data; @@ -348,7 +364,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity /// GET ACCESS TOKEN to the oAuth server RemoteOperation operation = new OAuth2GetAccessToken( getString(R.string.oauth2_client_id), - getString(R.string.oauth2_redirect_uri), // TODO check - necessary here? + getString(R.string.oauth2_redirect_uri), getString(R.string.oauth2_grant_type), queryParameters); //WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth2_url_endpoint_access)), getApplicationContext()); @@ -410,7 +426,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mDiscoveredVersion = null; mOperationThread = mOcServerChkOperation.execute(client, this, mHandler); } else { - mRefreshButton.setVisibility(View.INVISIBLE); + mHostUrlInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); mStatusText = 0; mStatusIcon = 0; updateConnStatus(); @@ -477,7 +493,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mStatusText = R.string.auth_wtf_reenter_URL; updateConnStatus(); mOkButton.setEnabled(false); - Log.wtf(TAG, "The user was allowed to click 'connect' to an unchecked server!!"); + Log_OC.wtf(TAG, "The user was allowed to click 'connect' to an unchecked server!!"); return; } @@ -533,7 +549,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity uriBuilder.appendQueryParameter(OAuth2Constants.KEY_SCOPE, getString(R.string.oauth2_scope)); //uriBuilder.appendQueryParameter(OAuth2Constants.KEY_STATE, whateverwewant); uri = uriBuilder.build(); - Log.d(TAG, "Starting browser to view " + uri.toString()); + Log_OC.d(TAG, "Starting browser to view " + uri.toString()); Intent i = new Intent(Intent.ACTION_VIEW, uri); startActivity(i); } @@ -583,10 +599,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } /// update the visibility of the 'retry connection' button - if (!mStatusCorrect) - mRefreshButton.setVisibility(View.VISIBLE); - else - mRefreshButton.setVisibility(View.INVISIBLE); + if (!mStatusCorrect) { + mHostUrlInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_action_refresh_black, 0); + } else { + mHostUrlInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + } /// retrieve discovered version and normalize server URL mDiscoveredVersion = operation.getDiscoveredVersion(); @@ -722,7 +739,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity /// time to test the retrieved access token on the ownCloud server mOAuthAccessToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN); - Log.d(TAG, "Got ACCESS TOKEN: " + mOAuthAccessToken); + Log_OC.d(TAG, "Got ACCESS TOKEN: " + mOAuthAccessToken); mAuthCheckOperation = new ExistenceCheckOperation("", this, false); WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this); client.setBearerCredentials(mOAuthAccessToken); @@ -731,7 +748,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } else { updateStatusIconAndText(result); updateAuthStatus(); - Log.d(TAG, "Access failed: " + result.getLogMessage()); + Log_OC.d(TAG, "Access failed: " + result.getLogMessage()); } } @@ -752,7 +769,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } if (result.isSuccess()) { - Log.d(TAG, "Successful access - time to save the account"); + Log_OC.d(TAG, "Successful access - time to save the account"); if (mAction == ACTION_CREATE) { createAccount(); @@ -766,7 +783,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } else { updateStatusIconAndText(result); updateAuthStatus(); - Log.d(TAG, "Access failed: " + result.getLogMessage()); + Log_OC.d(TAG, "Access failed: " + result.getLogMessage()); } } @@ -874,7 +891,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity break; } default: - Log.e(TAG, "Incorrect dialog called with id = " + id); + Log_OC.e(TAG, "Incorrect dialog called with id = " + id); } } @@ -897,7 +914,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity @Override public void onCancel(DialogInterface dialog) { /// TODO study if this is enough - Log.i(TAG, "Login canceled"); + Log_OC.i(TAG, "Login canceled"); if (mOperationThread != null) { mOperationThread.interrupt(); finish(); @@ -916,7 +933,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity .setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { - Log.i(TAG, "Login canceled"); + Log_OC.i(TAG, "Login canceled"); finish(); } }); @@ -942,7 +959,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity break; } default: - Log.e(TAG, "Incorrect dialog called with id = " + id); + Log_OC.e(TAG, "Incorrect dialog called with id = " + id); } return dialog; } @@ -965,16 +982,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * to the last check on the ownCloud server. */ private void updateConnStatus() { - ImageView iv = (ImageView) findViewById(R.id.action_indicator); - TextView tv = (TextView) findViewById(R.id.status_text); + TextView tv = (TextView) findViewById(R.id.server_status_text); if (mStatusIcon == 0 && mStatusText == 0) { - iv.setVisibility(View.INVISIBLE); tv.setVisibility(View.INVISIBLE); } else { - iv.setImageResource(mStatusIcon); tv.setText(mStatusText); - iv.setVisibility(View.VISIBLE); + tv.setCompoundDrawablesWithIntrinsicBounds(mStatusIcon, 0, 0, 0); tv.setVisibility(View.VISIBLE); } } @@ -1003,7 +1017,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * @param view Refresh 'button' */ public void onRefreshClick(View view) { - onFocusChange(mRefreshButton, false); + onFocusChange(mHostUrlInput, false); } @@ -1083,4 +1097,56 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity showDialog(DIALOG_CERT_NOT_SAVED); } + + /** + * Called when the 'action' button in an IME is pressed ('enter' in software keyboard). + * + * Used to trigger the authorization check when the user presses 'enter' after writing the password. + */ + @Override + public boolean onEditorAction(TextView inputField, int actionId, KeyEvent event) { + if (inputField != null && inputField.equals(mPasswordInput) && + actionId == EditorInfo.IME_ACTION_DONE) { + if (mOkButton.isEnabled()) { + mOkButton.performClick(); + } + } + return false; // always return false to grant that the software keyboard is hidden anyway + } + + + private abstract static class RightDrawableOnTouchListener implements OnTouchListener { + + private int fuzz = 10; + + /** + * {@inheritDoc} + */ + @Override + public boolean onTouch(View view, MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + Drawable rightDrawable = null; + if (view instanceof TextView) { + Drawable[] drawables = ((TextView)view).getCompoundDrawables(); + if (drawables.length > 2) { + rightDrawable = drawables[2]; + } + } + if (rightDrawable != null) { + final int x = (int) event.getX(); + 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) { + + return onDrawableTouch(event); + } + } + } + return false; + } + + public abstract boolean onDrawableTouch(final MotionEvent event); + } + }