SSL connections update: notice about untrusted certificates and allow the user save...
[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
24 import com.owncloud.android.AccountUtils;
25 import com.owncloud.android.authenticator.AccountAuthenticator;
26 import com.owncloud.android.authenticator.AuthenticationRunnable;
27 import com.owncloud.android.authenticator.ConnectionCheckOperation;
28 import com.owncloud.android.authenticator.OnAuthenticationResultListener;
29 import com.owncloud.android.authenticator.OnConnectCheckListener;
30 import com.owncloud.android.ui.dialog.SslValidatorDialog;
31 import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;
32 import com.owncloud.android.network.OwnCloudClientUtils;
33 import com.owncloud.android.operations.OnRemoteOperationListener;
34 import com.owncloud.android.operations.RemoteOperation;
35 import com.owncloud.android.operations.RemoteOperationResult;
36
37 import android.accounts.Account;
38 import android.accounts.AccountAuthenticatorActivity;
39 import android.accounts.AccountManager;
40 import android.app.AlertDialog;
41 import android.app.Dialog;
42 import android.app.ProgressDialog;
43 import android.content.ContentResolver;
44 import android.content.DialogInterface;
45 import android.content.Intent;
46 import android.content.SharedPreferences;
47 import android.net.Uri;
48 import android.os.Bundle;
49 import android.os.Handler;
50 import android.preference.PreferenceManager;
51 import android.text.InputType;
52 import android.util.Log;
53 import android.view.View;
54 import android.view.View.OnClickListener;
55 import android.view.View.OnFocusChangeListener;
56 import android.view.Window;
57 import android.widget.ImageView;
58 import android.widget.TextView;
59 import com.owncloud.android.R;
60
61 import eu.alefzero.webdav.WebdavClient;
62
63 /**
64 * This Activity is used to add an ownCloud account to the App
65 *
66 * @author Bartek Przybylski
67 *
68 */
69 public class AuthenticatorActivity extends AccountAuthenticatorActivity
70 implements OnAuthenticationResultListener, OnConnectCheckListener, OnRemoteOperationListener, OnSslValidatorListener,
71 OnFocusChangeListener, OnClickListener {
72
73 private static final int DIALOG_LOGIN_PROGRESS = 0;
74 private static final int DIALOG_SSL_VALIDATOR = 1;
75 private static final int DIALOG_CERT_NOT_SAVED = 2;
76
77 private static final String TAG = "AuthActivity";
78
79 private Thread mAuthThread;
80 private AuthenticationRunnable mAuthRunnable;
81 //private ConnectionCheckerRunnable mConnChkRunnable = null;
82 private ConnectionCheckOperation mConnChkRunnable;
83 private final Handler mHandler = new Handler();
84 private String mBaseUrl;
85
86 private static final String STATUS_TEXT = "STATUS_TEXT";
87 private static final String STATUS_ICON = "STATUS_ICON";
88 private static final String STATUS_CORRECT = "STATUS_CORRECT";
89 private static final String IS_SSL_CONN = "IS_SSL_CONN";
90 private int mStatusText, mStatusIcon;
91 private boolean mStatusCorrect, mIsSslConn;
92 private RemoteOperationResult mLastSslFailedResult;
93
94 public static final String PARAM_USERNAME = "param_Username";
95 public static final String PARAM_HOSTNAME = "param_Hostname";
96
97 @Override
98 protected void onCreate(Bundle savedInstanceState) {
99 super.onCreate(savedInstanceState);
100 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
101 setContentView(R.layout.account_setup);
102 ImageView iv = (ImageView) findViewById(R.id.refreshButton);
103 ImageView iv2 = (ImageView) findViewById(R.id.viewPassword);
104 TextView tv = (TextView) findViewById(R.id.host_URL);
105 TextView tv2 = (TextView) findViewById(R.id.account_password);
106
107 if (savedInstanceState != null) {
108 mStatusIcon = savedInstanceState.getInt(STATUS_ICON);
109 mStatusText = savedInstanceState.getInt(STATUS_TEXT);
110 mStatusCorrect = savedInstanceState.getBoolean(STATUS_CORRECT);
111 mIsSslConn = savedInstanceState.getBoolean(IS_SSL_CONN);
112 setResultIconAndText(mStatusIcon, mStatusText);
113 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
114 if (!mStatusCorrect)
115 iv.setVisibility(View.VISIBLE);
116 else
117 iv.setVisibility(View.INVISIBLE);
118
119 } else {
120 mStatusText = mStatusIcon = 0;
121 mStatusCorrect = false;
122 mIsSslConn = false;
123 }
124 iv.setOnClickListener(this);
125 iv2.setOnClickListener(this);
126 tv.setOnFocusChangeListener(this);
127 tv2.setOnFocusChangeListener(this);
128 }
129
130 @Override
131 protected void onSaveInstanceState(Bundle outState) {
132 outState.putInt(STATUS_ICON, mStatusIcon);
133 outState.putInt(STATUS_TEXT, mStatusText);
134 outState.putBoolean(STATUS_CORRECT, mStatusCorrect);
135 super.onSaveInstanceState(outState);
136 }
137
138 @Override
139 protected Dialog onCreateDialog(int id) {
140 Dialog dialog = null;
141 switch (id) {
142 case DIALOG_LOGIN_PROGRESS: {
143 ProgressDialog working_dialog = new ProgressDialog(this);
144 working_dialog.setMessage(getResources().getString(
145 R.string.auth_trying_to_login));
146 working_dialog.setIndeterminate(true);
147 working_dialog.setCancelable(true);
148 working_dialog
149 .setOnCancelListener(new DialogInterface.OnCancelListener() {
150 @Override
151 public void onCancel(DialogInterface dialog) {
152 Log.i(TAG, "Login canceled");
153 if (mAuthThread != null) {
154 mAuthThread.interrupt();
155 finish();
156 }
157 }
158 });
159 dialog = working_dialog;
160 break;
161 }
162 case DIALOG_SSL_VALIDATOR: {
163 SslValidatorDialog sslValidator = SslValidatorDialog.newInstance(this, mLastSslFailedResult, this);
164 if (sslValidator != null)
165 dialog = sslValidator;
166 // else, mLastSslFailedResult is not an SSL fail recoverable by accepting the server certificate as reliable; dialog will still be null
167 break;
168 }
169 case DIALOG_CERT_NOT_SAVED: {
170 AlertDialog.Builder builder = new AlertDialog.Builder(this);
171 builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));
172 builder.setCancelable(false);
173 builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
174 @Override
175 public void onClick(DialogInterface dialog, int which) {
176 dialog.dismiss();
177 };
178 });
179 dialog = builder.create();
180 break;
181 }
182 default:
183 Log.e(TAG, "Incorrect dialog called with id = " + id);
184 }
185 return dialog;
186 }
187
188 @Override
189 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
190 switch (id) {
191 case DIALOG_LOGIN_PROGRESS:
192 case DIALOG_CERT_NOT_SAVED:
193 break;
194 case DIALOG_SSL_VALIDATOR: {
195 ((SslValidatorDialog)dialog).updateResult(mLastSslFailedResult);
196 break;
197 }
198 default:
199 Log.e(TAG, "Incorrect dialog called with id = " + id);
200 }
201 }
202
203 public void onAuthenticationResult(boolean success, String message) {
204 if (success) {
205 TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);
206
207 URL url;
208 try {
209 url = new URL(message);
210 } catch (MalformedURLException e) {
211 // should never happen
212 Log.e(getClass().getName(), "Malformed URL: " + message);
213 return;
214 }
215
216 String username = username_text.getText().toString().trim();
217 String accountName = username + "@" + url.getHost();
218 if (url.getPort() >= 0) {
219 accountName += ":" + url.getPort();
220 }
221 Account account = new Account(accountName,
222 AccountAuthenticator.ACCOUNT_TYPE);
223 AccountManager accManager = AccountManager.get(this);
224 accManager.addAccountExplicitly(account, password_text.getText()
225 .toString(), null);
226
227 // Add this account as default in the preferences, if there is none
228 // already
229 Account defaultAccount = AccountUtils
230 .getCurrentOwnCloudAccount(this);
231 if (defaultAccount == null) {
232 SharedPreferences.Editor editor = PreferenceManager
233 .getDefaultSharedPreferences(this).edit();
234 editor.putString("select_oc_account", accountName);
235 editor.commit();
236 }
237
238 final Intent intent = new Intent();
239 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,
240 AccountAuthenticator.ACCOUNT_TYPE);
241 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
242 intent.putExtra(AccountManager.KEY_AUTHTOKEN,
243 AccountAuthenticator.ACCOUNT_TYPE);
244 intent.putExtra(AccountManager.KEY_USERDATA, username);
245
246 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,
247 url.toString());
248 accManager.setUserData(account,
249 AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable
250 .getDiscoveredVersion().toString());
251
252 accManager.setUserData(account,
253 AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);
254
255 setAccountAuthenticatorResult(intent.getExtras());
256 setResult(RESULT_OK, intent);
257 Bundle bundle = new Bundle();
258 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
259 //getContentResolver().startSync(ProviderTableMeta.CONTENT_URI,
260 // bundle);
261 ContentResolver.requestSync(account, "org.owncloud", bundle);
262
263 /*
264 * if
265 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
266 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
267 * ExtensionsAvailableActivity.class); startActivity(i); }
268 */
269
270 finish();
271 } else {
272 try {
273 dismissDialog(DIALOG_LOGIN_PROGRESS);
274 } catch (IllegalArgumentException e) {
275 // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens
276 }
277 TextView tv = (TextView) findViewById(R.id.account_username);
278 tv.setError(message);
279 }
280 }
281 public void onCancelClick(View view) {
282 setResult(RESULT_CANCELED);
283 finish();
284 }
285
286 public void onOkClick(View view) {
287 String prefix = "";
288 String url = ((TextView) findViewById(R.id.host_URL)).getText()
289 .toString().trim();
290 if (mIsSslConn) {
291 prefix = "https://";
292 } else {
293 prefix = "http://";
294 }
295 if (url.toLowerCase().startsWith("http://")
296 || url.toLowerCase().startsWith("https://")) {
297 prefix = "";
298 }
299 continueConnection(prefix);
300 }
301
302 public void onRegisterClick(View view) {
303 Intent register = new Intent(Intent.ACTION_VIEW, Uri.parse("https://owncloud.com/mobile/new"));
304 setResult(RESULT_CANCELED);
305 startActivity(register);
306 }
307
308 private void continueConnection(String prefix) {
309 String url = ((TextView) findViewById(R.id.host_URL)).getText()
310 .toString().trim();
311 String username = ((TextView) findViewById(R.id.account_username))
312 .getText().toString();
313 String password = ((TextView) findViewById(R.id.account_password))
314 .getText().toString();
315 if (url.endsWith("/"))
316 url = url.substring(0, url.length() - 1);
317
318 URL uri = null;
319 String webdav_path = AccountUtils.getWebdavPath(mConnChkRunnable
320 .getDiscoveredVersion());
321
322 if (webdav_path == null) {
323 onAuthenticationResult(false, getString(R.string.auth_bad_oc_version_title));
324 return;
325 }
326
327 try {
328 mBaseUrl = prefix + url;
329 String url_str = prefix + url + webdav_path;
330 uri = new URL(url_str);
331 } catch (MalformedURLException e) {
332 // should never happen
333 onAuthenticationResult(false, getString(R.string.auth_incorrect_address_title));
334 return;
335 }
336
337 showDialog(DIALOG_LOGIN_PROGRESS);
338 mAuthRunnable = new AuthenticationRunnable(uri, username, password, this);
339 mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);
340 mAuthThread = new Thread(mAuthRunnable);
341 mAuthThread.start();
342 }
343
344 @Override
345 public void onConnectionCheckResult(ResultType type) {
346 mStatusText = mStatusIcon = 0;
347 mStatusCorrect = false;
348 String t_url = ((TextView) findViewById(R.id.host_URL)).getText()
349 .toString().trim().toLowerCase();
350
351 switch (type) {
352 case OK_SSL:
353 mIsSslConn = true;
354 mStatusIcon = android.R.drawable.ic_secure;
355 mStatusText = R.string.auth_secure_connection;
356 mStatusCorrect = true;
357 break;
358 case OK_NO_SSL:
359 mIsSslConn = false;
360 mStatusCorrect = true;
361 if (t_url.startsWith("http://") ) {
362 mStatusText = R.string.auth_connection_established;
363 mStatusIcon = R.drawable.ic_ok;
364 } else {
365 mStatusText = R.string.auth_nossl_plain_ok_title;
366 mStatusIcon = android.R.drawable.ic_partial_secure;
367 }
368 break;
369 case BAD_OC_VERSION:
370 mStatusIcon = R.drawable.common_error;
371 mStatusText = R.string.auth_bad_oc_version_title;
372 break;
373 case WRONG_CONNECTION:
374 mStatusIcon = R.drawable.common_error;
375 mStatusText = R.string.auth_wrong_connection_title;
376 break;
377 case TIMEOUT:
378 mStatusIcon = R.drawable.common_error;
379 mStatusText = R.string.auth_timeout_title;
380 break;
381 case INCORRECT_ADDRESS:
382 mStatusIcon = R.drawable.common_error;
383 mStatusText = R.string.auth_incorrect_address_title;
384 break;
385 case SSL_UNVERIFIED_SERVER:
386 mStatusIcon = R.drawable.common_error;
387 mStatusText = R.string.auth_ssl_unverified_server_title;
388 break;
389 case SSL_INIT_ERROR:
390 mStatusIcon = R.drawable.common_error;
391 mStatusText = R.string.auth_ssl_general_error_title;
392 break;
393 case HOST_NOT_AVAILABLE:
394 mStatusIcon = R.drawable.common_error;
395 mStatusText = R.string.auth_unknown_host_title;
396 break;
397 case NO_NETWORK_CONNECTION:
398 mStatusIcon = R.drawable.no_network;
399 mStatusText = R.string.auth_no_net_conn_title;
400 break;
401 case INSTANCE_NOT_CONFIGURED:
402 mStatusIcon = R.drawable.common_error;
403 mStatusText = R.string.auth_not_configured_title;
404 break;
405 case UNKNOWN_ERROR:
406 mStatusIcon = R.drawable.common_error;
407 mStatusText = R.string.auth_unknown_error_title;
408 break;
409 case FILE_NOT_FOUND:
410 mStatusIcon = R.drawable.common_error;
411 mStatusText = R.string.auth_incorrect_path_title;
412 break;
413 default:
414 Log.e(TAG, "Incorrect connection checker result type: " + type);
415 }
416 setResultIconAndText(mStatusIcon, mStatusText);
417 if (!mStatusCorrect)
418 findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
419 else
420 findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
421 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
422 }
423
424 @Override
425 public void onFocusChange(View view, boolean hasFocus) {
426 if (view.getId() == R.id.host_URL) {
427 if (!hasFocus) {
428 TextView tv = ((TextView) findViewById(R.id.host_URL));
429 String uri = tv.getText().toString().trim();
430 if (uri.length() != 0) {
431 setResultIconAndText(R.drawable.progress_small,
432 R.string.auth_testing_connection);
433 //mConnChkRunnable = new ConnectionCheckerRunnable(uri, this);
434 mConnChkRunnable = new ConnectionCheckOperation(uri, this);
435 //mConnChkRunnable.setListener(this, mHandler);
436 //mAuthThread = new Thread(mConnChkRunnable);
437 //mAuthThread.start();
438 WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(uri), this);
439 mAuthThread = mConnChkRunnable.execute(client, this, mHandler);
440 } else {
441 findViewById(R.id.refreshButton).setVisibility(
442 View.INVISIBLE);
443 setResultIconAndText(0, 0);
444 }
445 } else {
446 // avoids that the 'connect' button can be clicked if the test was previously passed
447 findViewById(R.id.buttonOK).setEnabled(false);
448 }
449 } else if (view.getId() == R.id.account_password) {
450 ImageView iv = (ImageView) findViewById(R.id.viewPassword);
451 if (hasFocus) {
452 iv.setVisibility(View.VISIBLE);
453 } else {
454 TextView v = (TextView) findViewById(R.id.account_password);
455 int input_type = InputType.TYPE_CLASS_TEXT
456 | InputType.TYPE_TEXT_VARIATION_PASSWORD;
457 v.setInputType(input_type);
458 iv.setVisibility(View.INVISIBLE);
459 }
460 }
461 }
462
463 private void setResultIconAndText(int drawable_id, int text_id) {
464 ImageView iv = (ImageView) findViewById(R.id.action_indicator);
465 TextView tv = (TextView) findViewById(R.id.status_text);
466
467 if (drawable_id == 0 && text_id == 0) {
468 iv.setVisibility(View.INVISIBLE);
469 tv.setVisibility(View.INVISIBLE);
470 } else {
471 iv.setImageResource(drawable_id);
472 tv.setText(text_id);
473 iv.setVisibility(View.VISIBLE);
474 tv.setVisibility(View.VISIBLE);
475 }
476 }
477
478 @Override
479 public void onClick(View v) {
480 if (v.getId() == R.id.refreshButton) {
481 onFocusChange(findViewById(R.id.host_URL), false);
482 } else if (v.getId() == R.id.viewPassword) {
483 TextView view = (TextView) findViewById(R.id.account_password);
484 int input_type = InputType.TYPE_CLASS_TEXT
485 | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
486 view.setInputType(input_type);
487 }
488 }
489
490 @Override
491 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
492 if (operation.equals(mConnChkRunnable)) {
493
494 mStatusText = mStatusIcon = 0;
495 mStatusCorrect = false;
496 String t_url = ((TextView) findViewById(R.id.host_URL)).getText()
497 .toString().trim().toLowerCase();
498
499 switch (result.getCode()) {
500 case OK_SSL:
501 mIsSslConn = true;
502 mStatusIcon = android.R.drawable.ic_secure;
503 mStatusText = R.string.auth_secure_connection;
504 mStatusCorrect = true;
505 break;
506
507 case OK_NO_SSL:
508 case OK:
509 mIsSslConn = false;
510 mStatusCorrect = true;
511 if (t_url.startsWith("http://") ) {
512 mStatusText = R.string.auth_connection_established;
513 mStatusIcon = R.drawable.ic_ok;
514 } else {
515 mStatusText = R.string.auth_nossl_plain_ok_title;
516 mStatusIcon = android.R.drawable.ic_partial_secure;
517 }
518 break;
519
520
521 case BAD_OC_VERSION:
522 mStatusIcon = R.drawable.common_error;
523 mStatusText = R.string.auth_bad_oc_version_title;
524 break;
525 case WRONG_CONNECTION:
526 mStatusIcon = R.drawable.common_error;
527 mStatusText = R.string.auth_wrong_connection_title;
528 break;
529 case TIMEOUT:
530 mStatusIcon = R.drawable.common_error;
531 mStatusText = R.string.auth_timeout_title;
532 break;
533 case INCORRECT_ADDRESS:
534 mStatusIcon = R.drawable.common_error;
535 mStatusText = R.string.auth_incorrect_address_title;
536 break;
537
538 case SSL_ERROR:
539 mStatusIcon = R.drawable.common_error;
540 mStatusText = R.string.auth_ssl_general_error_title;
541 //mStatusText = R.string.auth_ssl_unverified_server_title;
542 mLastSslFailedResult = result;
543 showDialog(DIALOG_SSL_VALIDATOR); // see onCreateDialog(); it does not always show the dialog
544 /*if (InteractiveSslValidatorActivity.isRecoverable(result)) {
545 Intent intent = new Intent(this, InteractiveSslValidatorActivity.class);
546 startActivityForResult(intent, REQUEST_FOR_SSL_CERT);
547 }*/
548 break;
549
550 case HOST_NOT_AVAILABLE:
551 mStatusIcon = R.drawable.common_error;
552 mStatusText = R.string.auth_unknown_host_title;
553 break;
554 case NO_NETWORK_CONNECTION:
555 mStatusIcon = R.drawable.no_network;
556 mStatusText = R.string.auth_no_net_conn_title;
557 break;
558 case INSTANCE_NOT_CONFIGURED:
559 mStatusIcon = R.drawable.common_error;
560 mStatusText = R.string.auth_not_configured_title;
561 break;
562 case FILE_NOT_FOUND:
563 mStatusIcon = R.drawable.common_error;
564 mStatusText = R.string.auth_incorrect_path_title;
565 break;
566 case UNHANDLED_HTTP_CODE:
567 case UNKNOWN_ERROR:
568 mStatusIcon = R.drawable.common_error;
569 mStatusText = R.string.auth_unknown_error_title;
570 break;
571 default:
572 Log.e(TAG, "Incorrect connection checker result type: " + operation);
573 }
574 setResultIconAndText(mStatusIcon, mStatusText);
575 if (!mStatusCorrect)
576 findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
577 else
578 findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
579 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
580 }
581 }
582
583
584 public void onSavedCertificate() {
585 mAuthThread = mConnChkRunnable.retry(this, mHandler);
586 }
587
588 @Override
589 public void onFailedSavingCertificate() {
590 showDialog(DIALOG_CERT_NOT_SAVED);
591 }
592
593 }