From: Bartek Przybylski Date: Thu, 3 May 2012 13:37:30 +0000 (+0200) Subject: new stuff for account authenticator X-Git-Tag: oc-android-1.4.3~430 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/10c04c6d77de35025f697a2ad9bb8f18874a4acb?hp=--cc new stuff for account authenticator --- 10c04c6d77de35025f697a2ad9bb8f18874a4acb diff --git a/res/layout/account_setup.xml b/res/layout/account_setup.xml index 145576bb..90c02f8e 100644 --- a/res/layout/account_setup.xml +++ b/res/layout/account_setup.xml @@ -150,7 +150,7 @@ android:textColor="@android:color/black" android:text="@string/setup_btn_connect" > - + diff --git a/res/values/strings.xml b/res/values/strings.xml index 31e8b03d..8233e3b0 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -73,4 +73,6 @@ 30 60 + Trying to login + No SSL diff --git a/src/eu/alefzero/owncloud/AccountUtils.java b/src/eu/alefzero/owncloud/AccountUtils.java new file mode 100644 index 00000000..4077775f --- /dev/null +++ b/src/eu/alefzero/owncloud/AccountUtils.java @@ -0,0 +1,61 @@ +/* ownCloud Android client application + * Copyright (C) 2011 Bartek Przybylski + * + * 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 3 of the License, or + * (at your option) any later version. + * + * 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 . + * + */ + +package eu.alefzero.owncloud; + +import eu.alefzero.owncloud.authenticator.AccountAuthenticator; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +public class AccountUtils { + public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; + public static final String WEBDAV_PATH_2_0 = "/files/webdav.php"; + public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php"; + + /** + * Can be used to get the currently selected ownCloud account in the preferences + * + * @param context The current appContext + * @return The current account or null, if there is none yet. + */ + public static Account getCurrentOwnCloudAccount(Context context){ + Account[] ocAccounts = AccountManager.get(context).getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE); + Account defaultAccount = null; + + SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context); + String accountName = appPreferences.getString("select_oc_account", null); + + if(accountName != null){ + for(Account account : ocAccounts){ + if(account.name.equals(accountName)){ + defaultAccount = account; + break; + } + } + } else if (ocAccounts.length != 0) { + // we at least need to take first account as fallback + defaultAccount = ocAccounts[0]; + } + + return defaultAccount; + } +} diff --git a/src/eu/alefzero/owncloud/authenticator/AuthUtils.java b/src/eu/alefzero/owncloud/authenticator/AuthUtils.java deleted file mode 100644 index 8287e128..00000000 --- a/src/eu/alefzero/owncloud/authenticator/AuthUtils.java +++ /dev/null @@ -1,254 +0,0 @@ -/* ownCloud Android client application - * Copyright (C) 2011 Bartek Przybylski - * - * 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 3 of the License, or - * (at your option) any later version. - * - * 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 . - * - */ - -package eu.alefzero.owncloud.authenticator; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.UnknownHostException; -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; - -import org.apache.http.impl.client.DefaultHttpClient; - -import org.apache.commons.httpclient.auth.BasicScheme; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.HttpVersion; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.conn.params.ConnManagerPNames; -import org.apache.http.conn.params.ConnPerRouteBean; -import org.apache.http.conn.scheme.PlainSocketFactory; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; -import org.apache.http.protocol.BasicHttpContext; - -import eu.alefzero.owncloud.ui.activity.AuthenticatorActivity; - - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.content.Context; -import android.content.SharedPreferences; -import android.os.Handler; -import android.preference.PreferenceManager; -import android.util.Log; - -public class AuthUtils { - public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; - public static final String WEBDAV_PATH_2_0 = "/files/webdav.php"; - public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php"; - - private static String mResultMsg = ""; - - public static boolean authenticate(URL url, String username, String password, - Handler handler, Context context) { - String strippedPath = url.toString().endsWith("/") ? - url.toString().substring(0, url.toString().length()-1) : - url.toString(); - String webdatPath = strippedPath + WEBDAV_PATH_2_0; - URL complete_url = null; - try { - complete_url = new URL(webdatPath); - } catch (MalformedURLException e) { - // should never happend - sendResult(false, handler, context, "URL error"); - return false; - } - - // version 2.0 success - if (tryGetWebdav(complete_url, username, password, handler, context)) { - sendResult(true, handler, context, complete_url.toString()); - return true; - } - - if (mResultMsg.equals("401")) { - sendResult(false, handler, context, "Invalid login or/and password"); - return false; - } - - if (!mResultMsg.equals("404")) { - sendResult(false, handler, context, "Server error: " + mResultMsg); - return false; - } - - webdatPath = strippedPath + WEBDAV_PATH_1_2; - try { - complete_url = new URL(webdatPath); - } catch (MalformedURLException e) { - // should never happend - sendResult(false, handler, context, "URL error"); - return false; - } - - // version 1.2 success - if (tryGetWebdav(complete_url, username, password, handler, context)) { - sendResult(true, handler, context, complete_url.toString()); - return true; - } - - if (mResultMsg.equals("401")) { - sendResult(false, handler, context, "Invalid login or/and password"); - return false; - } - - if (mResultMsg.equals("404")) { - sendResult(false, handler, context, "Wrong path given"); - return false; - } - - sendResult(false, handler, context, "Server error: " + mResultMsg); - return false; - } - - public static boolean tryGetWebdav(URL url, String username, String pwd, - Handler handler, Context context) { - SchemeRegistry schemeRegistry = new SchemeRegistry(); - // http scheme - schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - // https scheme - schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); - - HttpParams params = new BasicHttpParams(); - params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); - params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); - params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - - ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); - - DefaultHttpClient c = new DefaultHttpClient(cm, params); - - c.getCredentialsProvider().setCredentials( - new AuthScope(url.getHost(), (url.getPort() == -1)?80:url.getPort()), - new UsernamePasswordCredentials(username, pwd)); - - BasicHttpContext localcontext = new BasicHttpContext(); - BasicScheme basicAuth = new BasicScheme(); - - localcontext.setAttribute("preemptive-auth", basicAuth); - HttpHost targetHost = new HttpHost(url.getHost(), (url.getPort() == -1) - ? 80 - : url.getPort(), (url.getProtocol().equals("https")) ? "https" : "http"); - HttpHead httpget = new HttpHead(url.toString()); - httpget.setHeader("Host", url.getHost()); - HttpResponse response = null; - try { - response = c.execute(targetHost, httpget, localcontext); - } catch (ClientProtocolException e1) { - sendResult(false, handler, context, "Protocol error: " - + e1.getLocalizedMessage()); - return false; - } catch (UnknownHostException e1) { - mResultMsg = "Unknowh host: " + e1.getLocalizedMessage(); - return false; - } catch (IOException e1) { - mResultMsg = "Error: " + e1.getLocalizedMessage(); - return false; - } - String status = response.getStatusLine().toString(); - - status = status.split(" ")[1]; - Log.i("AuthUtils", "Status returned: " + status); - if (status.equals("200")) { - return true; - } else if (status.equals("404")) { - mResultMsg = "404"; - return false; - } else if (status.equals("401")) { - mResultMsg = "401"; - return false; - } - mResultMsg = status; - return false; - } - - public static Thread performOnBackgroundThread(final Runnable r) { - final Thread t = new Thread() { - @Override - public void run() { - try { - r.run(); - } finally {} - } - }; - t.start(); - return t; - } - - public static void sendResult(final Boolean result, - final Handler handler, - final Context context, - final String message) { - if (handler == null || context == null) { - return; - } - handler.post(new Runnable() { - public void run() { - ((AuthenticatorActivity) context).onAuthenticationResult(result, message); - } - }); - } - - public static Thread attemptAuth(final URL url, final String username, - final String password, final Handler handler, - final Context context) { - final Runnable r = new Runnable() { - - public void run() { - authenticate(url, username, password, handler, context); - } - }; - return performOnBackgroundThread(r); - } - - /** - * Can be used to get the currently selected ownCloud account in the preferences - * - * @param context The current appContext - * @return The current account or null, if there is none yet. - */ - public static Account getCurrentOwnCloudAccount(Context context){ - Account[] ocAccounts = AccountManager.get(context).getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE); - Account defaultAccount = null; - - SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context); - String accountName = appPreferences.getString("select_oc_account", null); - - if(accountName != null){ - for(Account account : ocAccounts){ - if(account.name.equals(accountName)){ - defaultAccount = account; - break; - } - } - } else if (ocAccounts.length != 0) { - // we at least need to take first account as fallback - defaultAccount = ocAccounts[0]; - } - - return defaultAccount; - } -} diff --git a/src/eu/alefzero/owncloud/authenticator/AuthenticationRunnable.java b/src/eu/alefzero/owncloud/authenticator/AuthenticationRunnable.java new file mode 100644 index 00000000..445524c2 --- /dev/null +++ b/src/eu/alefzero/owncloud/authenticator/AuthenticationRunnable.java @@ -0,0 +1,83 @@ +/* ownCloud Android client application + * Copyright (C) 2012 Bartek Przybylski + * + * 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 3 of the License, or + * (at your option) any later version. + * + * 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 . + * + */ + +package eu.alefzero.owncloud.authenticator; + +import java.net.URL; + +import org.apache.commons.httpclient.HttpStatus; + +import eu.alefzero.webdav.WebdavClient; + +import android.net.Uri; +import android.os.Handler; + +public class AuthenticationRunnable implements Runnable { + + private OnAuthenticationResultListener mListener; + private Handler mHandler; + private URL mUrl; + private String mUsername; + private String mPassword; + + private static final String WEBDAV_2_0_PATH = "/files/webdav.php"; + + public AuthenticationRunnable(URL url, String username, String password) { + mListener = null; + mUrl = url; + mUsername = username; + mPassword = password; + } + + public void setOnAuthenticationResultListener(OnAuthenticationResultListener listener, Handler handler) { + mListener = listener; + mHandler = handler; + } + + @Override + public void run() { + Uri uri = Uri.parse(mUrl.toString() + WEBDAV_2_0_PATH); + WebdavClient client = new WebdavClient(uri); + client.setCredentials(mUsername, mPassword); + int login_result = client.tryToLogin(); + switch (login_result) { + case HttpStatus.SC_OK: + postResult(true, uri.toString()); + break; + case HttpStatus.SC_UNAUTHORIZED: + postResult(false, "Invalid login or/and password"); + break; + case HttpStatus.SC_NOT_FOUND: + postResult(false, "Wrong path given"); + break; + default: + postResult(false, "Internal server error, code: " + login_result); + } + } + + private void postResult(final boolean success, final String message) { + if (mHandler != null) { + mHandler.post(new Runnable() { + @Override + public void run() { + AuthenticationRunnable.this.mListener.onAuthenticationResult(success, message); + } + }); + } + } +} diff --git a/src/eu/alefzero/owncloud/authenticator/OnAuthenticationResultListener.java b/src/eu/alefzero/owncloud/authenticator/OnAuthenticationResultListener.java new file mode 100644 index 00000000..470484e4 --- /dev/null +++ b/src/eu/alefzero/owncloud/authenticator/OnAuthenticationResultListener.java @@ -0,0 +1,7 @@ +package eu.alefzero.owncloud.authenticator; + +public interface OnAuthenticationResultListener { + + public void onAuthenticationResult(boolean success, String message); + +} diff --git a/src/eu/alefzero/owncloud/syncadapter/ContactSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/ContactSyncAdapter.java index 8ddc4149..56f5c2ba 100644 --- a/src/eu/alefzero/owncloud/syncadapter/ContactSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/ContactSyncAdapter.java @@ -7,8 +7,8 @@ import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ByteArrayEntity; +import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.authenticator.AuthUtils; import eu.alefzero.owncloud.db.ProviderMeta; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; import android.accounts.Account; @@ -73,7 +73,7 @@ public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter { AccountManager am = getAccountManager(); String uri = am.getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL) - .replace(AuthUtils.WEBDAV_PATH_2_0, AuthUtils.CARDDAV_PATH_2_0); + .replace(AccountUtils.WEBDAV_PATH_2_0, AccountUtils.CARDDAV_PATH_2_0); uri += "/addressbooks/" + getAccount().name.substring(0, getAccount().name.lastIndexOf('@')) + "/default/"; mAddrBookUri = uri; diff --git a/src/eu/alefzero/owncloud/ui/activity/AuthenticatorActivity.java b/src/eu/alefzero/owncloud/ui/activity/AuthenticatorActivity.java index c62290b3..3ecd3e79 100644 --- a/src/eu/alefzero/owncloud/ui/activity/AuthenticatorActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/AuthenticatorActivity.java @@ -21,8 +21,6 @@ package eu.alefzero.owncloud.ui.activity; import java.net.MalformedURLException; import java.net.URL; -import com.actionbarsherlock.ActionBarSherlock; - import android.accounts.Account; import android.accounts.AccountAuthenticatorActivity; import android.accounts.AccountManager; @@ -40,202 +38,225 @@ import android.text.InputType; import android.util.Log; import android.view.View; import android.view.Window; -import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; +import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.authenticator.AuthUtils; +import eu.alefzero.owncloud.authenticator.AuthenticationRunnable; +import eu.alefzero.owncloud.authenticator.OnAuthenticationResultListener; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; /** * This Activity is used to add an ownCloud account to the App + * * @author Bartek Przybylski - * + * */ -public class AuthenticatorActivity extends AccountAuthenticatorActivity { - private Thread mAuthThread; - private final Handler mHandler = new Handler(); - private boolean mUseSSLConnection; +public class AuthenticatorActivity extends AccountAuthenticatorActivity implements OnAuthenticationResultListener { + private static final int DIALOG_LOGIN_PROGRESS = 0; + private static final String TAG = "AuthActivity"; - public static final String PARAM_USERNAME = "param_Username"; - public static final String PARAM_HOSTNAME = "param_Hostname"; + private Thread mAuthThread; + private AuthenticationRunnable mAuthRunnable; + private final Handler mHandler = new Handler(); + private boolean mUseSSLConnection; - public AuthenticatorActivity() { - mUseSSLConnection = true; - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().requestFeature(Window.FEATURE_NO_TITLE); - setContentView(R.layout.account_setup); - if (getIntent().hasExtra(PARAM_USERNAME)) { - String username = getIntent().getStringExtra(PARAM_HOSTNAME); - TextView host_text, user_text; - host_text = (TextView) findViewById(R.id.host_URL); - user_text = (TextView) findViewById(R.id.account_username); - host_text.setText(host_text.getText() + username.substring(username.lastIndexOf('@'))); - user_text.setText(user_text.getText() + username.substring(0, username.lastIndexOf('@') - 1)); - } + public static final String PARAM_USERNAME = "param_Username"; + public static final String PARAM_HOSTNAME = "param_Hostname"; + + public AuthenticatorActivity() { + mUseSSLConnection = true; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getWindow().requestFeature(Window.FEATURE_NO_TITLE); + setContentView(R.layout.account_setup); + if (getIntent().hasExtra(PARAM_USERNAME)) { + String username = getIntent().getStringExtra(PARAM_HOSTNAME); + TextView host_text, user_text; + host_text = (TextView) findViewById(R.id.host_URL); + user_text = (TextView) findViewById(R.id.account_username); + host_text.setText(host_text.getText() + + username.substring(username.lastIndexOf('@'))); + user_text.setText(user_text.getText() + + username.substring(0, username.lastIndexOf('@') - 1)); } + } - @Override - protected Dialog onCreateDialog(int id) { - final ProgressDialog dialog = new ProgressDialog(this); - dialog.setMessage("Trying to login"); - dialog.setIndeterminate(true); - dialog.setCancelable(true); - dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { - public void onCancel(DialogInterface dialog) { - Log.i(getClass().getName(), "Login canceled"); - if (mAuthThread != null) { - mAuthThread.interrupt(); - finish(); - } + @Override + protected Dialog onCreateDialog(int id) { + Dialog dialog = null; + switch (id) { + case DIALOG_LOGIN_PROGRESS : { + ProgressDialog working_dialog = new ProgressDialog(this); + dialog = working_dialog; + working_dialog.setMessage(getResources().getString(R.string.auth_trying_to_login)); + working_dialog.setIndeterminate(true); + working_dialog.setCancelable(true); + working_dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + Log.i(getClass().getName(), "Login canceled"); + if (mAuthThread != null) { + mAuthThread.interrupt(); + finish(); } + } }); - return dialog; + break; + } + default : + Log.e(TAG, "Incorrect dialog called with id = " + id); } + return dialog; + } - public void onAuthenticationResult(boolean result, String message) { - if (result) { - TextView username_text = (TextView) findViewById(R.id.account_username), - password_text = (TextView) findViewById(R.id.account_password); - - URL url; - try { - url = new URL(message); - } catch (MalformedURLException e) { - // should never happen - Log.e(getClass().getName(), "Malformed URL: " + message); - return; - } + public void onAuthenticationResult(boolean success, String message) { + if (success) { + TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password); - String username = username_text.getText().toString().trim(); - String accountName = username + "@" + url.getHost(); - Account account = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE); - AccountManager accManager = AccountManager.get(this); - accManager.addAccountExplicitly(account, password_text.getText().toString(), null); - - // Add this account as default in the preferences, if there is none already - Account defaultAccount = AuthUtils.getCurrentOwnCloudAccount(this); - if(defaultAccount == null){ - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); - editor.putString("select_oc_account", accountName); - editor.commit(); - } + URL url; + try { + url = new URL(message); + } catch (MalformedURLException e) { + // should never happen + Log.e(getClass().getName(), "Malformed URL: " + message); + return; + } + + String username = username_text.getText().toString().trim(); + String accountName = username + "@" + url.getHost(); + Account account = new Account(accountName, + AccountAuthenticator.ACCOUNT_TYPE); + AccountManager accManager = AccountManager.get(this); + accManager.addAccountExplicitly(account, password_text.getText() + .toString(), null); + + // Add this account as default in the preferences, if there is none + // already + Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this); + if (defaultAccount == null) { + SharedPreferences.Editor editor = PreferenceManager + .getDefaultSharedPreferences(this).edit(); + editor.putString("select_oc_account", accountName); + editor.commit(); + } + + final Intent intent = new Intent(); + intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, + AccountAuthenticator.ACCOUNT_TYPE); + intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); + intent.putExtra(AccountManager.KEY_AUTHTOKEN, + AccountAuthenticator.ACCOUNT_TYPE); + accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL, + url.toString()); + + // TODO prepare this URL using a central service + intent.putExtra(AccountManager.KEY_USERDATA, username); + accManager.setUserData( + account, + AccountAuthenticator.KEY_CONTACT_URL, + url.toString().replace(AccountUtils.WEBDAV_PATH_2_0, + AccountUtils.CARDDAV_PATH_2_0)); + + setAccountAuthenticatorResult(intent.getExtras()); + setResult(RESULT_OK, intent); + Bundle bundle = new Bundle(); + bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); + getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle); - final Intent intent = new Intent(); - intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE); - intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); - intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE); - accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL, url.toString()); - - // TODO prepare this URL using a central service - intent.putExtra(AccountManager.KEY_USERDATA, username); - accManager.setUserData(account, AccountAuthenticator.KEY_CONTACT_URL, - url.toString().replace(AuthUtils.WEBDAV_PATH_2_0, AuthUtils.CARDDAV_PATH_2_0) - ); - - setAccountAuthenticatorResult(intent.getExtras()); - setResult(RESULT_OK, intent); - Bundle bundle = new Bundle(); - bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); - getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle); - - dismissDialog(0); - finish(); - } else { - Toast.makeText(this, message, Toast.LENGTH_LONG).show(); - dismissDialog(0); - } + dismissDialog(0); + finish(); + } else { + Toast.makeText(this, message, Toast.LENGTH_LONG).show(); + dismissDialog(0); } + } - public void onCancelClick(View view) { - Log.i(getClass().getName(), "Account creating canceled"); - this.finish(); + public void onOkClick(View view) { + TextView url_text = (TextView) findViewById(R.id.host_URL); + TextView username_text = (TextView) findViewById(R.id.account_username); + TextView password_text = (TextView) findViewById(R.id.account_password); + Log.i(getClass().getName(), "OK clicked"); + boolean hasErrors = false; + + URL uri = null; + if (url_text.getText().toString().trim().length() == 0) { + url_text.setTextColor(Color.RED); + hasErrors = true; + } else { + url_text.setTextColor(android.R.color.black); + } + try { + String url_str = url_text.getText().toString(); + if (!url_str.startsWith("http://") && !url_str.startsWith("https://")) { + if (mUseSSLConnection) + url_str = "https://" + url_str; + else + url_str = "http://" + url_str; + } + uri = new URL(url_str); + } catch (MalformedURLException e) { + url_text.setTextColor(Color.RED); + e.printStackTrace(); + hasErrors = true; } - public void onOkClick(View view) { - TextView url_text = (TextView) findViewById(R.id.host_URL); - TextView username_text = (TextView) findViewById(R.id.account_username); - TextView password_text = (TextView) findViewById(R.id.account_password); - Log.i(getClass().getName(), "OK clicked"); - boolean hasErrors = false; - - URL uri = null; - if (url_text.getText().toString().trim().length() == 0) { - url_text.setTextColor(Color.RED); - hasErrors = true; - } else { - url_text.setTextColor(android.R.color.black); - } - try { - String url_str = url_text.getText().toString(); - if (!url_str.startsWith("http://") && - !url_str.startsWith("https://")) { - if (mUseSSLConnection) - url_str = "https://" + url_str; - else - url_str = "http://" + url_str; - } - uri = new URL(url_str); - } catch (MalformedURLException e) { - url_text.setTextColor(Color.RED); - e.printStackTrace(); - hasErrors = true; - } - - if (username_text.getText().toString().contains(" ") || - username_text.getText().toString().trim().length() == 0) { - username_text.setTextColor(Color.RED); - hasErrors = true; - } else { - username_text.setTextColor(android.R.color.black); - } - - if (password_text.getText().toString().trim().length() == 0) { - password_text.setTextColor(Color.RED); - hasErrors = true; - } else { - password_text.setTextColor(android.R.color.black); - } - if (hasErrors) { - return; - } - - int new_port = uri.getPort(); - if (new_port == -1) { - if (mUseSSLConnection) - new_port = 443; - else - new_port = 80; - } - - try { - uri = new URL(uri.getProtocol(), uri.getHost(), new_port, uri.getPath()); - } catch (MalformedURLException e) { - e.printStackTrace(); // should not happend - } - - showDialog(0); - mAuthThread = AuthUtils.attemptAuth(uri, - username_text.getText().toString(), - password_text.getText().toString(), - mHandler, - AuthenticatorActivity.this); + if (username_text.getText().toString().contains(" ") + || username_text.getText().toString().trim().length() == 0) { + username_text.setTextColor(Color.RED); + hasErrors = true; + } else { + username_text.setTextColor(android.R.color.black); } - - public void sslBadgeClick(View view, String val) { - mUseSSLConnection = ((TextView)view).getText().equals("SSL"); + + if (password_text.getText().toString().trim().length() == 0) { + password_text.setTextColor(Color.RED); + hasErrors = true; + } else { + password_text.setTextColor(android.R.color.black); } - - public void passwordBadgeClick(View view, String val) { - if(val.equals("Hide")) { - ((TextView)view).setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); - } else { - ((TextView)view).setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); - } + if (hasErrors) { + return; + } + + int new_port = uri.getPort(); + if (new_port == -1) { + if (mUseSSLConnection) + new_port = 443; + else + new_port = 80; + } + + try { + uri = new URL(uri.getProtocol(), uri.getHost(), new_port, uri.getPath()); + } catch (MalformedURLException e) { + e.printStackTrace(); // should not happend } + + showDialog(DIALOG_LOGIN_PROGRESS); + mAuthRunnable = new AuthenticationRunnable( + uri, + username_text.getText().toString(), + password_text.getText().toString()); + mAuthRunnable.setOnAuthenticationResultListener(this, mHandler); + mAuthThread = new Thread(mAuthRunnable); + mAuthThread.start(); + } + + public void sslBadgeClick(View view, String val) { + mUseSSLConnection = ((TextView) view).getText().equals("SSL"); + } + + public void passwordBadgeClick(View view, String val) { + int input_type = InputType.TYPE_CLASS_TEXT; + input_type |= val.equals("Hide") + ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD + : InputType.TYPE_TEXT_VARIATION_PASSWORD; + + ((TextView) view).setInputType(input_type); + } } diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index 3522ea00..cac6fea5 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -42,9 +42,9 @@ import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; +import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.authenticator.AuthUtils; import eu.alefzero.owncloud.datamodel.DataStorageManager; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; @@ -93,7 +93,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements { builder = new Builder(this); final EditText dirName = new EditText(getBaseContext()); - final Account a = AuthUtils.getCurrentOwnCloudAccount(this); + final Account a = AccountUtils.getCurrentOwnCloudAccount(this); builder.setView(dirName); builder.setTitle(R.string.uploader_info_dirname); dirName.setTextColor(R.color.setup_text_typed); @@ -149,7 +149,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements R.layout.sherlock_spinner_dropdown_item); mDirectories.add("/"); setContentView(R.layout.files); - mStorageManager = new FileDataStorageManager(AuthUtils.getCurrentOwnCloudAccount(this), getContentResolver()); + mStorageManager = new FileDataStorageManager(AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver()); ActionBar action_bar = getSupportActionBar(); action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); action_bar.setDisplayShowTitleEnabled(false); diff --git a/src/eu/alefzero/owncloud/ui/activity/Preferences.java b/src/eu/alefzero/owncloud/ui/activity/Preferences.java index a374b242..953e1a64 100644 --- a/src/eu/alefzero/owncloud/ui/activity/Preferences.java +++ b/src/eu/alefzero/owncloud/ui/activity/Preferences.java @@ -43,10 +43,10 @@ import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; +import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.OwnCloudSession; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.authenticator.AuthUtils; import eu.alefzero.owncloud.db.DbHandler; /** @@ -127,7 +127,7 @@ public class Preferences extends SherlockPreferenceActivity implements mAccountList.setOnPreferenceChangeListener(this); // Display the name of the current account if there is any - Account defaultAccount = AuthUtils.getCurrentOwnCloudAccount(this); + Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this); if (defaultAccount != null) { mAccountList.setSummary(defaultAccount.name); } diff --git a/src/eu/alefzero/owncloud/ui/adapter/LandingScreenAdapter.java b/src/eu/alefzero/owncloud/ui/adapter/LandingScreenAdapter.java index 3483983a..def7a486 100644 --- a/src/eu/alefzero/owncloud/ui/adapter/LandingScreenAdapter.java +++ b/src/eu/alefzero/owncloud/ui/adapter/LandingScreenAdapter.java @@ -25,8 +25,8 @@ import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; +import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; -import eu.alefzero.owncloud.authenticator.AuthUtils; import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; import eu.alefzero.owncloud.ui.activity.Preferences; @@ -74,7 +74,7 @@ public class LandingScreenAdapter extends BaseAdapter { * We will put in the one that is selected in the preferences */ intent.setClass(mContext, FileDisplayActivity.class); - intent.putExtra("ACCOUNT", AuthUtils.getCurrentOwnCloudAccount(mContext)); + intent.putExtra("ACCOUNT", AccountUtils.getCurrentOwnCloudAccount(mContext)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); break; case 5: diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java index 1fb0c57d..7541329b 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java @@ -27,8 +27,8 @@ import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Toast; +import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; -import eu.alefzero.owncloud.authenticator.AuthUtils; import eu.alefzero.owncloud.datamodel.DataStorageManager; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; @@ -56,7 +56,7 @@ public class FileListFragment extends FragmentListView { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mAccount = AuthUtils.getCurrentOwnCloudAccount(getActivity()); + mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity()); populateFileList(); } diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index f03b7a97..1f196292 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -24,13 +24,16 @@ import java.io.IOException; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPut; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnManagerPNames; @@ -155,6 +158,17 @@ public class WebdavClient extends HttpClient { return result; } + + public int tryToLogin() { + int r = 0; + HeadMethod head = new HeadMethod(mUri.toString()); + try { + r = executeMethod(head); + } catch (Exception e) { + Log.e(TAG, "Error: " + e.getMessage()); + } + return r; + } public boolean createDirectory(String path) { HttpMkCol method = new HttpMkCol(mUri.toString() + path + "/");