--- /dev/null
+/* ownCloud Android client application\r
+ * Copyright (C) 2011 Bartek Przybylski\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ */\r
+\r
+package eu.alefzero.owncloud.ui.activity;\r
+\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+\r
+import android.accounts.Account;\r
+import android.accounts.AccountAuthenticatorActivity;\r
+import android.accounts.AccountManager;\r
+import android.app.Dialog;\r
+import android.app.ProgressDialog;\r
+import android.content.ContentResolver;\r
+import android.content.DialogInterface;\r
+import android.content.Intent;\r
+import android.graphics.Color;\r
+import android.os.Bundle;\r
+import android.os.Handler;\r
+import android.util.Log;\r
+import android.view.View;\r
+import android.view.Window;\r
+import android.widget.TextView;\r
+import android.widget.Toast;\r
+import eu.alefzero.owncloud.R;\r
+import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
+import eu.alefzero.owncloud.authenticator.AuthUtils;\r
+import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\r
+\r
+/**\r
+ * This Activity is used to add an ownCloud account to the App\r
+ * @author Bartek Przybylski\r
+ *\r
+ */\r
+public class AuthenticatorActivity extends AccountAuthenticatorActivity {\r
+ private Thread mAuthThread;\r
+ private final Handler mHandler = new Handler();\r
+\r
+ public static final String PARAM_USERNAME = "param_Username";\r
+ public static final String PARAM_HOSTNAME = "param_Hostname";\r
+\r
+ @Override\r
+ protected void onCreate(Bundle savedInstanceState) {\r
+ super.onCreate(savedInstanceState);\r
+ getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
+ setContentView(R.layout.account_setup);\r
+ if (getIntent().hasExtra(PARAM_USERNAME)) {\r
+ String username = getIntent().getStringExtra(PARAM_HOSTNAME);\r
+ TextView host_text, user_text;\r
+ host_text = (TextView) findViewById(R.id.host_URL);\r
+ user_text = (TextView) findViewById(R.id.account_username);\r
+ host_text.setText(host_text.getText() + username.substring(username.lastIndexOf('@')));\r
+ user_text.setText(user_text.getText() + username.substring(0, username.lastIndexOf('@') - 1));\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected Dialog onCreateDialog(int id) {\r
+ final ProgressDialog dialog = new ProgressDialog(this);\r
+ dialog.setMessage("Trying to login");\r
+ dialog.setIndeterminate(true);\r
+ dialog.setCancelable(true);\r
+ dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {\r
+ public void onCancel(DialogInterface dialog) {\r
+ Log.i(getClass().getName(), "Login canceled");\r
+ if (mAuthThread != null) {\r
+ mAuthThread.interrupt();\r
+ finish();\r
+ }\r
+ }\r
+ });\r
+ return dialog;\r
+ }\r
+\r
+ public void onAuthenticationResult(boolean result, String message) {\r
+ if (result) {\r
+ TextView username_text = (TextView) findViewById(R.id.account_username),\r
+ password_text = (TextView) findViewById(R.id.account_password);\r
+\r
+ URL url;\r
+ try {\r
+ url = new URL(message);\r
+ } catch (MalformedURLException e) {\r
+ // should never happend\r
+ Log.e(getClass().getName(), "Malformed URL: " + message);\r
+ return;\r
+ }\r
+\r
+ String username = username_text.getText().toString().trim();\r
+ Account account = new Account(username + "@" + url.getHost(), AccountAuthenticator.ACCOUNT_TYPE);\r
+ AccountManager accManager = AccountManager.get(this);\r
+ accManager.addAccountExplicitly(account, password_text.getText().toString(), null);\r
+\r
+ final Intent intent = new Intent();\r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);\r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
+ intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE);\r
+ accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL, url.toString());\r
+\r
+ // TODO prepare this URL during a central service\r
+ intent.putExtra(AccountManager.KEY_USERDATA, username);\r
+ accManager.setUserData(account, AccountAuthenticator.KEY_CONTACT_URL,\r
+ url.toString().replace(AuthUtils.WEBDAV_PATH_2_0, AuthUtils.CARDDAV_PATH_2_0)\r
+ );\r
+\r
+ setAccountAuthenticatorResult(intent.getExtras());\r
+ setResult(RESULT_OK, intent);\r
+ Bundle bundle = new Bundle();\r
+ bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
+ getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle);\r
+\r
+ dismissDialog(0);\r
+ finish();\r
+ } else {\r
+ Toast.makeText(this, message, Toast.LENGTH_LONG).show();\r
+ dismissDialog(0);\r
+ }\r
+ }\r
+\r
+ public void onCancelClick(View view) {\r
+ Log.i(getClass().getName(), "Account creating canceled");\r
+ this.finish();\r
+ }\r
+\r
+ public void onOkClick(View view) {\r
+ TextView url_text = (TextView) findViewById(R.id.host_URL);\r
+ TextView username_text = (TextView) findViewById(R.id.account_username);\r
+ TextView password_text = (TextView) findViewById(R.id.account_password);\r
+ Log.i(getClass().getName(), "OK clicked");\r
+ boolean hasErrors = false;\r
+\r
+ URL uri = null;\r
+ if (url_text.getText().toString().trim().length() == 0) {\r
+ url_text.setTextColor(Color.RED);\r
+ hasErrors = true;\r
+ } else {\r
+ url_text.setTextColor(Color.BLACK);\r
+ }\r
+ try {\r
+ String url_str = url_text.getText().toString();\r
+ if (!url_str.startsWith("http://") &&\r
+ !url_str.startsWith("https://")) {\r
+ url_str = "http://" + url_str;\r
+ }\r
+ uri = new URL(url_str);\r
+ } catch (MalformedURLException e) {\r
+ url_text.setTextColor(Color.RED);\r
+ e.printStackTrace();\r
+ hasErrors = true;\r
+ }\r
+\r
+ if (username_text.getText().toString().contains(" ") ||\r
+ username_text.getText().toString().trim().length() == 0) {\r
+ username_text.setTextColor(Color.RED);\r
+ hasErrors = true;\r
+ } else {\r
+ username_text.setTextColor(Color.BLACK);\r
+ }\r
+\r
+ if (password_text.getText().toString().trim().length() == 0) {\r
+ password_text.setTextColor(Color.RED);\r
+ hasErrors = true;\r
+ } else {\r
+ password_text.setTextColor(Color.BLACK);\r
+ }\r
+ if (hasErrors) {\r
+ return;\r
+ }\r
+ showDialog(0);\r
+ mAuthThread = AuthUtils.attemptAuth(uri,\r
+ username_text.getText().toString(),\r
+ password_text.getText().toString(),\r
+ mHandler,\r
+ AuthenticatorActivity.this);\r
+ }\r
+}\r