Added forgotten trim()s on the content of the URL field
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / AuthenticatorActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 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 com.owncloud.android.ui.activity;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.net.URLEncoder;
24
25 import com.owncloud.android.AccountUtils;
26 import com.owncloud.android.authenticator.AccountAuthenticator;
27 import com.owncloud.android.authenticator.AuthenticationRunnable;
28 import com.owncloud.android.authenticator.ConnectionCheckerRunnable;
29 import com.owncloud.android.authenticator.OnAuthenticationResultListener;
30 import com.owncloud.android.authenticator.OnConnectCheckListener;
31 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
32 import com.owncloud.android.extensions.ExtensionsAvailableActivity;
33 import com.owncloud.android.utils.OwnCloudVersion;
34
35 import android.accounts.Account;
36 import android.accounts.AccountAuthenticatorActivity;
37 import android.accounts.AccountManager;
38 import android.app.Dialog;
39 import android.app.ProgressDialog;
40 import android.content.ContentResolver;
41 import android.content.DialogInterface;
42 import android.content.Intent;
43 import android.content.SharedPreferences;
44 import android.os.Bundle;
45 import android.os.Handler;
46 import android.preference.PreferenceManager;
47 import android.text.InputType;
48 import android.util.Log;
49 import android.view.View;
50 import android.view.View.OnClickListener;
51 import android.view.View.OnFocusChangeListener;
52 import android.view.Window;
53 import android.widget.ImageView;
54 import android.widget.TextView;
55 import com.owncloud.android.R;
56
57 /**
58 * This Activity is used to add an ownCloud account to the App
59 *
60 * @author Bartek Przybylski
61 *
62 */
63 public class AuthenticatorActivity extends AccountAuthenticatorActivity
64 implements OnAuthenticationResultListener, OnConnectCheckListener,
65 OnFocusChangeListener, OnClickListener {
66 private static final int DIALOG_LOGIN_PROGRESS = 0;
67
68 private static final String TAG = "AuthActivity";
69
70 private Thread mAuthThread;
71 private AuthenticationRunnable mAuthRunnable;
72 private ConnectionCheckerRunnable mConnChkRunnable;
73 private final Handler mHandler = new Handler();
74 private String mBaseUrl;
75
76 private static final String STATUS_TEXT = "STATUS_TEXT";
77 private static final String STATUS_ICON = "STATUS_ICON";
78 private static final String STATUS_CORRECT = "STATUS_CORRECT";
79 private static final String IS_SSL_CONN = "IS_SSL_CONN";
80 private int mStatusText, mStatusIcon;
81 private boolean mStatusCorrect, mIsSslConn;
82
83 public static final String PARAM_USERNAME = "param_Username";
84 public static final String PARAM_HOSTNAME = "param_Hostname";
85
86 @Override
87 protected void onCreate(Bundle savedInstanceState) {
88 super.onCreate(savedInstanceState);
89 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
90 setContentView(R.layout.account_setup);
91 ImageView iv = (ImageView) findViewById(R.id.refreshButton);
92 ImageView iv2 = (ImageView) findViewById(R.id.viewPassword);
93 TextView tv = (TextView) findViewById(R.id.host_URL);
94 TextView tv2 = (TextView) findViewById(R.id.account_password);
95
96 if (savedInstanceState != null) {
97 mStatusIcon = savedInstanceState.getInt(STATUS_ICON);
98 mStatusText = savedInstanceState.getInt(STATUS_TEXT);
99 mStatusCorrect = savedInstanceState.getBoolean(STATUS_CORRECT);
100 mIsSslConn = savedInstanceState.getBoolean(IS_SSL_CONN);
101 setResultIconAndText(mStatusIcon, mStatusText);
102 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
103 if (!mStatusCorrect)
104 iv.setVisibility(View.VISIBLE);
105 else
106 iv.setVisibility(View.INVISIBLE);
107
108 } else {
109 mStatusText = mStatusIcon = 0;
110 mStatusCorrect = false;
111 mIsSslConn = false;
112 }
113 iv.setOnClickListener(this);
114 iv2.setOnClickListener(this);
115 tv.setOnFocusChangeListener(this);
116 tv2.setOnFocusChangeListener(this);
117 }
118
119 @Override
120 protected void onSaveInstanceState(Bundle outState) {
121 outState.putInt(STATUS_ICON, mStatusIcon);
122 outState.putInt(STATUS_TEXT, mStatusText);
123 outState.putBoolean(STATUS_CORRECT, mStatusCorrect);
124 super.onSaveInstanceState(outState);
125 }
126
127 @Override
128 protected Dialog onCreateDialog(int id) {
129 Dialog dialog = null;
130 switch (id) {
131 case DIALOG_LOGIN_PROGRESS: {
132 ProgressDialog working_dialog = new ProgressDialog(this);
133 working_dialog.setMessage(getResources().getString(
134 R.string.auth_trying_to_login));
135 working_dialog.setIndeterminate(true);
136 working_dialog.setCancelable(true);
137 working_dialog
138 .setOnCancelListener(new DialogInterface.OnCancelListener() {
139 @Override
140 public void onCancel(DialogInterface dialog) {
141 Log.i(TAG, "Login canceled");
142 if (mAuthThread != null) {
143 mAuthThread.interrupt();
144 finish();
145 }
146 }
147 });
148 dialog = working_dialog;
149 break;
150 }
151 default:
152 Log.e(TAG, "Incorrect dialog called with id = " + id);
153 }
154 return dialog;
155 }
156
157 public void onAuthenticationResult(boolean success, String message) {
158 if (success) {
159 TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);
160
161 URL url;
162 try {
163 url = new URL(message);
164 } catch (MalformedURLException e) {
165 // should never happen
166 Log.e(getClass().getName(), "Malformed URL: " + message);
167 return;
168 }
169
170 String username = username_text.getText().toString().trim();
171 String accountName = username + "@" + url.getHost();
172 if (url.getPort() >= 0) {
173 accountName += ":" + url.getPort();
174 }
175 Account account = new Account(accountName,
176 AccountAuthenticator.ACCOUNT_TYPE);
177 AccountManager accManager = AccountManager.get(this);
178 accManager.addAccountExplicitly(account, password_text.getText()
179 .toString(), null);
180
181 // Add this account as default in the preferences, if there is none
182 // already
183 Account defaultAccount = AccountUtils
184 .getCurrentOwnCloudAccount(this);
185 if (defaultAccount == null) {
186 SharedPreferences.Editor editor = PreferenceManager
187 .getDefaultSharedPreferences(this).edit();
188 editor.putString("select_oc_account", accountName);
189 editor.commit();
190 }
191
192 final Intent intent = new Intent();
193 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,
194 AccountAuthenticator.ACCOUNT_TYPE);
195 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
196 intent.putExtra(AccountManager.KEY_AUTHTOKEN,
197 AccountAuthenticator.ACCOUNT_TYPE);
198 intent.putExtra(AccountManager.KEY_USERDATA, username);
199
200 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,
201 url.toString());
202 accManager.setUserData(account,
203 AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable
204 .getDiscoveredVersion().toString());
205 accManager.setUserData(account,
206 AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);
207
208 setAccountAuthenticatorResult(intent.getExtras());
209 setResult(RESULT_OK, intent);
210 Bundle bundle = new Bundle();
211 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
212 //getContentResolver().startSync(ProviderTableMeta.CONTENT_URI,
213 // bundle);
214 ContentResolver.requestSync(account, "org.owncloud", bundle);
215
216 /*
217 * if
218 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
219 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
220 * ExtensionsAvailableActivity.class); startActivity(i); }
221 */
222
223 finish();
224 } else {
225 dismissDialog(DIALOG_LOGIN_PROGRESS);
226 TextView tv = (TextView) findViewById(R.id.account_username);
227 tv.setError(message);
228 }
229 }
230 public void onCancelClick(View view) {
231 finish();
232 }
233
234 public void onOkClick(View view) {
235 String prefix = "";
236 String url = ((TextView) findViewById(R.id.host_URL)).getText()
237 .toString().trim();
238 if (mIsSslConn) {
239 prefix = "https://";
240 } else {
241 prefix = "http://";
242 }
243 if (url.toLowerCase().startsWith("http://")
244 || url.toLowerCase().startsWith("https://")) {
245 prefix = "";
246 }
247 continueConnection(prefix);
248 }
249
250 private void continueConnection(String prefix) {
251 String url = ((TextView) findViewById(R.id.host_URL)).getText()
252 .toString().trim();
253 String username = ((TextView) findViewById(R.id.account_username))
254 .getText().toString();
255 String password = ((TextView) findViewById(R.id.account_password))
256 .getText().toString();
257 if (url.endsWith("/"))
258 url = url.substring(0, url.length() - 1);
259
260 URL uri = null;
261 String webdav_path = AccountUtils.getWebdavPath(mConnChkRunnable
262 .getDiscoveredVersion());
263
264 try {
265 mBaseUrl = prefix + url;
266 String url_str = prefix + url + webdav_path;
267 uri = new URL(url_str);
268 } catch (MalformedURLException e) {
269 // should not happend
270 e.printStackTrace();
271 }
272
273 showDialog(DIALOG_LOGIN_PROGRESS);
274 mAuthRunnable = new AuthenticationRunnable(uri, username, password);
275 mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);
276 mAuthThread = new Thread(mAuthRunnable);
277 mAuthThread.start();
278 }
279
280 @Override
281 public void onConnectionCheckResult(ResultType type) {
282 mStatusText = mStatusIcon = 0;
283 mStatusCorrect = false;
284 String t_url = ((TextView) findViewById(R.id.host_URL)).getText()
285 .toString().trim().toLowerCase();
286
287 switch (type) {
288 case OK_SSL:
289 mIsSslConn = true;
290 mStatusIcon = android.R.drawable.ic_secure;
291 mStatusText = R.string.auth_secure_connection;
292 mStatusCorrect = true;
293 break;
294 case OK_NO_SSL:
295 mIsSslConn = false;
296 mStatusCorrect = true;
297 if (t_url.startsWith("http://") ) {
298 mStatusText = R.string.auth_connection_established;
299 mStatusIcon = R.drawable.ic_ok;
300 } else {
301 mStatusText = R.string.auth_nossl_plain_ok_title;
302 mStatusIcon = android.R.drawable.ic_partial_secure;
303 }
304 break;
305 case BAD_OC_VERSION:
306 mStatusIcon = R.drawable.common_error;
307 mStatusText = R.string.auth_bad_oc_version_title;
308 break;
309 case WRONG_CONNECTION:
310 mStatusIcon = R.drawable.common_error;
311 mStatusText = R.string.auth_wrong_connection_title;
312 break;
313 case TIMEOUT:
314 mStatusIcon = R.drawable.common_error;
315 mStatusText = R.string.auth_timeout_title;
316 break;
317 case INCORRECT_ADDRESS:
318 mStatusIcon = R.drawable.common_error;
319 mStatusText = R.string.auth_incorrect_address_title;
320 break;
321 case SSL_UNVERIFIED_SERVER:
322 mStatusIcon = R.drawable.common_error;
323 mStatusText = R.string.auth_ssl_unverified_server_title;
324 break;
325 case SSL_INIT_ERROR:
326 mStatusIcon = R.drawable.common_error;
327 mStatusText = R.string.auth_ssl_general_error_title;
328 break;
329 case HOST_NOT_AVAILABLE:
330 mStatusIcon = R.drawable.common_error;
331 mStatusText = R.string.auth_unknown_host_title;
332 break;
333 case NO_NETWORK_CONNECTION:
334 mStatusIcon = R.drawable.no_network;
335 mStatusText = R.string.auth_no_net_conn_title;
336 break;
337 case INSTANCE_NOT_CONFIGURED:
338 mStatusIcon = R.drawable.common_error;
339 mStatusText = R.string.auth_not_configured_title;
340 break;
341 case UNKNOWN_ERROR:
342 mStatusIcon = R.drawable.common_error;
343 mStatusText = R.string.auth_unknown_error_title;
344 break;
345 case FILE_NOT_FOUND:
346 mStatusIcon = R.drawable.common_error;
347 mStatusText = R.string.auth_incorrect_path_title;
348 break;
349 default:
350 Log.e(TAG, "Incorrect connection checker result type: " + type);
351 }
352 setResultIconAndText(mStatusIcon, mStatusText);
353 if (!mStatusCorrect)
354 findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
355 else
356 findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
357 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
358 }
359
360 @Override
361 public void onFocusChange(View view, boolean hasFocus) {
362 if (view.getId() == R.id.host_URL) {
363 if (!hasFocus) {
364 TextView tv = ((TextView) findViewById(R.id.host_URL));
365 String uri = tv.getText().toString().trim();
366 if (uri.length() != 0) {
367 setResultIconAndText(R.drawable.progress_small,
368 R.string.auth_testing_connection);
369 findViewById(R.id.buttonOK).setEnabled(false); // avoid connect can be clicked if the test was previously passed
370 mConnChkRunnable = new ConnectionCheckerRunnable(uri, this);
371 mConnChkRunnable.setListener(this, mHandler);
372 mAuthThread = new Thread(mConnChkRunnable);
373 mAuthThread.start();
374 } else {
375 findViewById(R.id.refreshButton).setVisibility(
376 View.INVISIBLE);
377 setResultIconAndText(0, 0);
378 }
379 }
380 } else if (view.getId() == R.id.account_password) {
381 ImageView iv = (ImageView) findViewById(R.id.viewPassword);
382 if (hasFocus) {
383 iv.setVisibility(View.VISIBLE);
384 } else {
385 TextView v = (TextView) findViewById(R.id.account_password);
386 int input_type = InputType.TYPE_CLASS_TEXT
387 | InputType.TYPE_TEXT_VARIATION_PASSWORD;
388 v.setInputType(input_type);
389 iv.setVisibility(View.INVISIBLE);
390 }
391 }
392 }
393
394 private void setResultIconAndText(int drawable_id, int text_id) {
395 ImageView iv = (ImageView) findViewById(R.id.action_indicator);
396 TextView tv = (TextView) findViewById(R.id.status_text);
397
398 if (drawable_id == 0 && text_id == 0) {
399 iv.setVisibility(View.INVISIBLE);
400 tv.setVisibility(View.INVISIBLE);
401 } else {
402 iv.setImageResource(drawable_id);
403 tv.setText(text_id);
404 iv.setVisibility(View.VISIBLE);
405 tv.setVisibility(View.VISIBLE);
406 }
407 }
408
409 @Override
410 public void onClick(View v) {
411 if (v.getId() == R.id.refreshButton) {
412 onFocusChange(findViewById(R.id.host_URL), false);
413 } else if (v.getId() == R.id.viewPassword) {
414 TextView view = (TextView) findViewById(R.id.account_password);
415 int input_type = InputType.TYPE_CLASS_TEXT
416 | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
417 view.setInputType(input_type);
418 }
419 }
420 }