Merged request #3
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / AuthenticatorActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.ui.activity;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23
24 import android.accounts.Account;
25 import android.accounts.AccountAuthenticatorActivity;
26 import android.accounts.AccountManager;
27 import android.app.Dialog;
28 import android.app.ProgressDialog;
29 import android.content.ContentResolver;
30 import android.content.DialogInterface;
31 import android.content.Intent;
32 import android.graphics.Color;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.text.InputType;
36 import android.util.Log;
37 import android.view.View;
38 import android.view.Window;
39 import android.widget.CheckBox;
40 import android.widget.TextView;
41 import android.widget.Toast;
42 import eu.alefzero.owncloud.R;
43 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
44 import eu.alefzero.owncloud.authenticator.AuthUtils;
45 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
46
47 /**
48 * This Activity is used to add an ownCloud account to the App
49 * @author Bartek Przybylski
50 *
51 */
52 public class AuthenticatorActivity extends AccountAuthenticatorActivity {
53 private Thread mAuthThread;
54 private final Handler mHandler = new Handler();
55
56 public static final String PARAM_USERNAME = "param_Username";
57 public static final String PARAM_HOSTNAME = "param_Hostname";
58
59 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
63 setContentView(R.layout.account_setup);
64 if (getIntent().hasExtra(PARAM_USERNAME)) {
65 String username = getIntent().getStringExtra(PARAM_HOSTNAME);
66 TextView host_text, user_text;
67 host_text = (TextView) findViewById(R.id.host_URL);
68 user_text = (TextView) findViewById(R.id.account_username);
69 host_text.setText(host_text.getText() + username.substring(username.lastIndexOf('@')));
70 user_text.setText(user_text.getText() + username.substring(0, username.lastIndexOf('@') - 1));
71 }
72 }
73
74 @Override
75 protected Dialog onCreateDialog(int id) {
76 final ProgressDialog dialog = new ProgressDialog(this);
77 dialog.setMessage("Trying to login");
78 dialog.setIndeterminate(true);
79 dialog.setCancelable(true);
80 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
81 public void onCancel(DialogInterface dialog) {
82 Log.i(getClass().getName(), "Login canceled");
83 if (mAuthThread != null) {
84 mAuthThread.interrupt();
85 finish();
86 }
87 }
88 });
89 return dialog;
90 }
91
92 public void onAuthenticationResult(boolean result, String message) {
93 if (result) {
94 TextView username_text = (TextView) findViewById(R.id.account_username),
95 password_text = (TextView) findViewById(R.id.account_password);
96
97 URL url;
98 try {
99 url = new URL(message);
100 } catch (MalformedURLException e) {
101 // should never happend
102 Log.e(getClass().getName(), "Malformed URL: " + message);
103 return;
104 }
105
106 String username = username_text.getText().toString().trim();
107 Account account = new Account(username + "@" + url.getHost(), AccountAuthenticator.ACCOUNT_TYPE);
108 AccountManager accManager = AccountManager.get(this);
109 accManager.addAccountExplicitly(account, password_text.getText().toString(), null);
110
111 final Intent intent = new Intent();
112 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);
113 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
114 intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE);
115 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL, url.toString());
116
117 // TODO prepare this URL during a central service
118 intent.putExtra(AccountManager.KEY_USERDATA, username);
119 accManager.setUserData(account, AccountAuthenticator.KEY_CONTACT_URL,
120 url.toString().replace(AuthUtils.WEBDAV_PATH_2_0, AuthUtils.CARDDAV_PATH_2_0)
121 );
122
123 setAccountAuthenticatorResult(intent.getExtras());
124 setResult(RESULT_OK, intent);
125 Bundle bundle = new Bundle();
126 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
127 getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle);
128
129 dismissDialog(0);
130 finish();
131 } else {
132 Toast.makeText(this, message, Toast.LENGTH_LONG).show();
133 dismissDialog(0);
134 }
135 }
136
137 public void onCancelClick(View view) {
138 Log.i(getClass().getName(), "Account creating canceled");
139 this.finish();
140 }
141
142 public void onOkClick(View view) {
143 TextView url_text = (TextView) findViewById(R.id.host_URL);
144 TextView username_text = (TextView) findViewById(R.id.account_username);
145 TextView password_text = (TextView) findViewById(R.id.account_password);
146 Log.i(getClass().getName(), "OK clicked");
147 boolean hasErrors = false;
148
149 URL uri = null;
150 if (url_text.getText().toString().trim().length() == 0) {
151 url_text.setTextColor(Color.RED);
152 hasErrors = true;
153 } else {
154 url_text.setTextColor(Color.BLACK);
155 }
156 try {
157 String url_str = url_text.getText().toString();
158 if (!url_str.startsWith("http://") &&
159 !url_str.startsWith("https://")) {
160 url_str = "http://" + url_str;
161 }
162 uri = new URL(url_str);
163 } catch (MalformedURLException e) {
164 url_text.setTextColor(Color.RED);
165 e.printStackTrace();
166 hasErrors = true;
167 }
168
169 if (username_text.getText().toString().contains(" ") ||
170 username_text.getText().toString().trim().length() == 0) {
171 username_text.setTextColor(Color.RED);
172 hasErrors = true;
173 } else {
174 username_text.setTextColor(Color.BLACK);
175 }
176
177 if (password_text.getText().toString().trim().length() == 0) {
178 password_text.setTextColor(Color.RED);
179 hasErrors = true;
180 } else {
181 password_text.setTextColor(Color.BLACK);
182 }
183 if (hasErrors) {
184 return;
185 }
186 showDialog(0);
187 mAuthThread = AuthUtils.attemptAuth(uri,
188 username_text.getText().toString(),
189 password_text.getText().toString(),
190 mHandler,
191 AuthenticatorActivity.this);
192 }
193
194 /**
195 * Handles the show password checkbox
196 * @author robstar
197 * @param view
198 */
199 public void onCheckboxClick(View view) {
200 CheckBox checkbox = (CheckBox) findViewById(R.id.show_password);
201 TextView password_text = (TextView) findViewById(R.id.account_password);
202
203 if(checkbox.isChecked()) {
204 password_text.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
205 } else {
206 password_text.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
207 }
208
209 }
210 }