1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package com
.owncloud
.android
.ui
.activity
;
21 import java
.net
.MalformedURLException
;
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
.OnAuthenticationResultListener
;
28 import com
.owncloud
.android
.authenticator
.OnConnectCheckListener
;
29 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
;
30 import com
.owncloud
.android
.ui
.dialog
.SslValidatorDialog
.OnSslValidatorListener
;
31 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
32 import com
.owncloud
.android
.operations
.ConnectionCheckOperation
;
33 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
34 import com
.owncloud
.android
.operations
.RemoteOperation
;
35 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
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
;
61 import eu
.alefzero
.webdav
.WebdavClient
;
64 * This Activity is used to add an ownCloud account to the App
66 * @author Bartek Przybylski
69 public class AuthenticatorActivity
extends AccountAuthenticatorActivity
70 implements OnAuthenticationResultListener
, OnConnectCheckListener
, OnRemoteOperationListener
, OnSslValidatorListener
,
71 OnFocusChangeListener
, OnClickListener
{
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;
77 private static final String TAG
= "AuthActivity";
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
;
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 mLastSslUntrustedServerResult
;
94 public static final String PARAM_USERNAME
= "param_Username";
95 public static final String PARAM_HOSTNAME
= "param_Hostname";
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
);
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
);
115 iv
.setVisibility(View
.VISIBLE
);
117 iv
.setVisibility(View
.INVISIBLE
);
120 mStatusText
= mStatusIcon
= 0;
121 mStatusCorrect
= false
;
124 iv
.setOnClickListener(this);
125 iv2
.setOnClickListener(this);
126 tv
.setOnFocusChangeListener(this);
127 tv2
.setOnFocusChangeListener(this);
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
);
139 protected Dialog
onCreateDialog(int id
) {
140 Dialog dialog
= null
;
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
);
149 .setOnCancelListener(new DialogInterface
.OnCancelListener() {
151 public void onCancel(DialogInterface dialog
) {
152 Log
.i(TAG
, "Login canceled");
153 if (mAuthThread
!= null
) {
154 mAuthThread
.interrupt();
159 dialog
= working_dialog
;
162 case DIALOG_SSL_VALIDATOR
: {
163 dialog
= SslValidatorDialog
.newInstance(this, mLastSslUntrustedServerResult
, this);
166 case DIALOG_CERT_NOT_SAVED
: {
167 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
168 builder
.setMessage(getResources().getString(R
.string
.ssl_validator_not_saved
));
169 builder
.setCancelable(false
);
170 builder
.setPositiveButton(R
.string
.common_ok
, new DialogInterface
.OnClickListener() {
172 public void onClick(DialogInterface dialog
, int which
) {
176 dialog
= builder
.create();
180 Log
.e(TAG
, "Incorrect dialog called with id = " + id
);
186 protected void onPrepareDialog(int id
, Dialog dialog
, Bundle args
) {
188 case DIALOG_LOGIN_PROGRESS
:
189 case DIALOG_CERT_NOT_SAVED
:
191 case DIALOG_SSL_VALIDATOR
: {
192 ((SslValidatorDialog
)dialog
).updateResult(mLastSslUntrustedServerResult
);
196 Log
.e(TAG
, "Incorrect dialog called with id = " + id
);
200 public void onAuthenticationResult(boolean success
, String message
) {
202 TextView username_text
= (TextView
) findViewById(R
.id
.account_username
), password_text
= (TextView
) findViewById(R
.id
.account_password
);
206 url
= new URL(message
);
207 } catch (MalformedURLException e
) {
208 // should never happen
209 Log
.e(getClass().getName(), "Malformed URL: " + message
);
213 String username
= username_text
.getText().toString().trim();
214 String accountName
= username
+ "@" + url
.getHost();
215 if (url
.getPort() >= 0) {
216 accountName
+= ":" + url
.getPort();
218 Account account
= new Account(accountName
,
219 AccountAuthenticator
.ACCOUNT_TYPE
);
220 AccountManager accManager
= AccountManager
.get(this);
221 accManager
.addAccountExplicitly(account
, password_text
.getText()
224 // Add this account as default in the preferences, if there is none
226 Account defaultAccount
= AccountUtils
227 .getCurrentOwnCloudAccount(this);
228 if (defaultAccount
== null
) {
229 SharedPreferences
.Editor editor
= PreferenceManager
230 .getDefaultSharedPreferences(this).edit();
231 editor
.putString("select_oc_account", accountName
);
235 final Intent intent
= new Intent();
236 intent
.putExtra(AccountManager
.KEY_ACCOUNT_TYPE
,
237 AccountAuthenticator
.ACCOUNT_TYPE
);
238 intent
.putExtra(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
239 intent
.putExtra(AccountManager
.KEY_AUTHTOKEN
,
240 AccountAuthenticator
.ACCOUNT_TYPE
);
241 intent
.putExtra(AccountManager
.KEY_USERDATA
, username
);
243 accManager
.setUserData(account
,
244 AccountAuthenticator
.KEY_OC_VERSION
, mConnChkRunnable
245 .getDiscoveredVersion().toString());
247 accManager
.setUserData(account
,
248 AccountAuthenticator
.KEY_OC_BASE_URL
, mBaseUrl
);
250 setAccountAuthenticatorResult(intent
.getExtras());
251 setResult(RESULT_OK
, intent
);
252 Bundle bundle
= new Bundle();
253 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
254 //getContentResolver().startSync(ProviderTableMeta.CONTENT_URI,
256 ContentResolver
.requestSync(account
, "org.owncloud", bundle
);
260 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
261 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
262 * ExtensionsAvailableActivity.class); startActivity(i); }
268 dismissDialog(DIALOG_LOGIN_PROGRESS
);
269 } catch (IllegalArgumentException e
) {
270 // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens
272 TextView tv
= (TextView
) findViewById(R
.id
.account_username
);
273 tv
.setError(message
+ " "); // the extra spaces are a workaround for an ugly bug:
274 // 1. insert wrong credentials and connect
275 // 2. put the focus on the user name field with using hardware controls (don't touch the screen); the error is shown UNDER the field
276 // 3. touch the user name field; the software keyboard appears; the error popup is moved OVER the field and SHRINKED in width, losing the last word
277 // Seen, at least, in Android 2.x devices
280 public void onCancelClick(View view
) {
281 setResult(RESULT_CANCELED
);
285 public void onOkClick(View view
) {
287 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
294 if (url
.toLowerCase().startsWith("http://")
295 || url
.toLowerCase().startsWith("https://")) {
298 continueConnection(prefix
);
301 public void onRegisterClick(View view
) {
302 Intent register
= new Intent(Intent
.ACTION_VIEW
, Uri
.parse("https://owncloud.com/mobile/new"));
303 setResult(RESULT_CANCELED
);
304 startActivity(register
);
307 private void continueConnection(String prefix
) {
308 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
310 String username
= ((TextView
) findViewById(R
.id
.account_username
))
311 .getText().toString();
312 String password
= ((TextView
) findViewById(R
.id
.account_password
))
313 .getText().toString();
314 if (url
.endsWith("/"))
315 url
= url
.substring(0, url
.length() - 1);
318 String webdav_path
= AccountUtils
.getWebdavPath(mConnChkRunnable
319 .getDiscoveredVersion());
321 if (webdav_path
== null
) {
322 onAuthenticationResult(false
, getString(R
.string
.auth_bad_oc_version_title
));
327 mBaseUrl
= prefix
+ url
;
328 String url_str
= prefix
+ url
+ webdav_path
;
329 uri
= new URL(url_str
);
330 } catch (MalformedURLException e
) {
331 // should never happen
332 onAuthenticationResult(false
, getString(R
.string
.auth_incorrect_address_title
));
336 showDialog(DIALOG_LOGIN_PROGRESS
);
337 mAuthRunnable
= new AuthenticationRunnable(uri
, username
, password
, this);
338 mAuthRunnable
.setOnAuthenticationResultListener(this, mHandler
);
339 mAuthThread
= new Thread(mAuthRunnable
);
344 public void onConnectionCheckResult(ResultType type
) {
345 mStatusText
= mStatusIcon
= 0;
346 mStatusCorrect
= false
;
347 String t_url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
348 .toString().trim().toLowerCase();
353 mStatusIcon
= android
.R
.drawable
.ic_secure
;
354 mStatusText
= R
.string
.auth_secure_connection
;
355 mStatusCorrect
= true
;
359 mStatusCorrect
= true
;
360 if (t_url
.startsWith("http://") ) {
361 mStatusText
= R
.string
.auth_connection_established
;
362 mStatusIcon
= R
.drawable
.ic_ok
;
364 mStatusText
= R
.string
.auth_nossl_plain_ok_title
;
365 mStatusIcon
= android
.R
.drawable
.ic_partial_secure
;
369 mStatusIcon
= R
.drawable
.common_error
;
370 mStatusText
= R
.string
.auth_bad_oc_version_title
;
372 case WRONG_CONNECTION
:
373 mStatusIcon
= R
.drawable
.common_error
;
374 mStatusText
= R
.string
.auth_wrong_connection_title
;
377 mStatusIcon
= R
.drawable
.common_error
;
378 mStatusText
= R
.string
.auth_timeout_title
;
380 case INCORRECT_ADDRESS
:
381 mStatusIcon
= R
.drawable
.common_error
;
382 mStatusText
= R
.string
.auth_incorrect_address_title
;
384 case SSL_UNVERIFIED_SERVER
:
385 mStatusIcon
= R
.drawable
.common_error
;
386 mStatusText
= R
.string
.auth_ssl_unverified_server_title
;
389 mStatusIcon
= R
.drawable
.common_error
;
390 mStatusText
= R
.string
.auth_ssl_general_error_title
;
392 case HOST_NOT_AVAILABLE
:
393 mStatusIcon
= R
.drawable
.common_error
;
394 mStatusText
= R
.string
.auth_unknown_host_title
;
396 case NO_NETWORK_CONNECTION
:
397 mStatusIcon
= R
.drawable
.no_network
;
398 mStatusText
= R
.string
.auth_no_net_conn_title
;
400 case INSTANCE_NOT_CONFIGURED
:
401 mStatusIcon
= R
.drawable
.common_error
;
402 mStatusText
= R
.string
.auth_not_configured_title
;
405 mStatusIcon
= R
.drawable
.common_error
;
406 mStatusText
= R
.string
.auth_unknown_error_title
;
409 mStatusIcon
= R
.drawable
.common_error
;
410 mStatusText
= R
.string
.auth_incorrect_path_title
;
413 Log
.e(TAG
, "Incorrect connection checker result type: " + type
);
415 setResultIconAndText(mStatusIcon
, mStatusText
);
417 findViewById(R
.id
.refreshButton
).setVisibility(View
.VISIBLE
);
419 findViewById(R
.id
.refreshButton
).setVisibility(View
.INVISIBLE
);
420 findViewById(R
.id
.buttonOK
).setEnabled(mStatusCorrect
);
424 public void onFocusChange(View view
, boolean hasFocus
) {
425 if (view
.getId() == R
.id
.host_URL
) {
427 TextView tv
= ((TextView
) findViewById(R
.id
.host_URL
));
428 String uri
= tv
.getText().toString().trim();
429 if (uri
.length() != 0) {
430 setResultIconAndText(R
.drawable
.progress_small
,
431 R
.string
.auth_testing_connection
);
432 //mConnChkRunnable = new ConnectionCheckerRunnable(uri, this);
433 mConnChkRunnable
= new ConnectionCheckOperation(uri
, this);
434 //mConnChkRunnable.setListener(this, mHandler);
435 //mAuthThread = new Thread(mConnChkRunnable);
436 //mAuthThread.start();
437 WebdavClient client
= OwnCloudClientUtils
.createOwnCloudClient(Uri
.parse(uri
), this);
438 mAuthThread
= mConnChkRunnable
.execute(client
, this, mHandler
);
440 findViewById(R
.id
.refreshButton
).setVisibility(
442 setResultIconAndText(0, 0);
445 // avoids that the 'connect' button can be clicked if the test was previously passed
446 findViewById(R
.id
.buttonOK
).setEnabled(false
);
448 } else if (view
.getId() == R
.id
.account_password
) {
449 ImageView iv
= (ImageView
) findViewById(R
.id
.viewPassword
);
451 iv
.setVisibility(View
.VISIBLE
);
453 TextView v
= (TextView
) findViewById(R
.id
.account_password
);
454 int input_type
= InputType
.TYPE_CLASS_TEXT
455 | InputType
.TYPE_TEXT_VARIATION_PASSWORD
;
456 v
.setInputType(input_type
);
457 iv
.setVisibility(View
.INVISIBLE
);
462 private void setResultIconAndText(int drawable_id
, int text_id
) {
463 ImageView iv
= (ImageView
) findViewById(R
.id
.action_indicator
);
464 TextView tv
= (TextView
) findViewById(R
.id
.status_text
);
466 if (drawable_id
== 0 && text_id
== 0) {
467 iv
.setVisibility(View
.INVISIBLE
);
468 tv
.setVisibility(View
.INVISIBLE
);
470 iv
.setImageResource(drawable_id
);
472 iv
.setVisibility(View
.VISIBLE
);
473 tv
.setVisibility(View
.VISIBLE
);
478 public void onClick(View v
) {
479 if (v
.getId() == R
.id
.refreshButton
) {
480 onFocusChange(findViewById(R
.id
.host_URL
), false
);
481 } else if (v
.getId() == R
.id
.viewPassword
) {
482 TextView view
= (TextView
) findViewById(R
.id
.account_password
);
483 int input_type
= view
.getInputType();
484 if ((input_type
& InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
) == InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
) {
485 input_type
= InputType
.TYPE_CLASS_TEXT
486 | InputType
.TYPE_TEXT_VARIATION_PASSWORD
;
488 input_type
= InputType
.TYPE_CLASS_TEXT
489 | InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
;
491 view
.setInputType(input_type
);
496 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
497 if (operation
.equals(mConnChkRunnable
)) {
499 mStatusText
= mStatusIcon
= 0;
500 mStatusCorrect
= false
;
501 String t_url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
502 .toString().trim().toLowerCase();
504 switch (result
.getCode()) {
507 mStatusIcon
= android
.R
.drawable
.ic_secure
;
508 mStatusText
= R
.string
.auth_secure_connection
;
509 mStatusCorrect
= true
;
515 mStatusCorrect
= true
;
516 if (t_url
.startsWith("http://") ) {
517 mStatusText
= R
.string
.auth_connection_established
;
518 mStatusIcon
= R
.drawable
.ic_ok
;
520 mStatusText
= R
.string
.auth_nossl_plain_ok_title
;
521 mStatusIcon
= android
.R
.drawable
.ic_partial_secure
;
527 mStatusIcon
= R
.drawable
.common_error
;
528 mStatusText
= R
.string
.auth_bad_oc_version_title
;
530 case WRONG_CONNECTION
:
531 mStatusIcon
= R
.drawable
.common_error
;
532 mStatusText
= R
.string
.auth_wrong_connection_title
;
535 mStatusIcon
= R
.drawable
.common_error
;
536 mStatusText
= R
.string
.auth_timeout_title
;
538 case INCORRECT_ADDRESS
:
539 mStatusIcon
= R
.drawable
.common_error
;
540 mStatusText
= R
.string
.auth_incorrect_address_title
;
543 case SSL_RECOVERABLE_PEER_UNVERIFIED
:
544 mStatusIcon
= R
.drawable
.common_error
;
545 mStatusText
= R
.string
.auth_ssl_unverified_server_title
;
546 mLastSslUntrustedServerResult
= result
;
547 showDialog(DIALOG_SSL_VALIDATOR
);
551 mStatusIcon
= R
.drawable
.common_error
;
552 mStatusText
= R
.string
.auth_ssl_general_error_title
;
555 case HOST_NOT_AVAILABLE
:
556 mStatusIcon
= R
.drawable
.common_error
;
557 mStatusText
= R
.string
.auth_unknown_host_title
;
559 case NO_NETWORK_CONNECTION
:
560 mStatusIcon
= R
.drawable
.no_network
;
561 mStatusText
= R
.string
.auth_no_net_conn_title
;
563 case INSTANCE_NOT_CONFIGURED
:
564 mStatusIcon
= R
.drawable
.common_error
;
565 mStatusText
= R
.string
.auth_not_configured_title
;
568 mStatusIcon
= R
.drawable
.common_error
;
569 mStatusText
= R
.string
.auth_incorrect_path_title
;
571 case UNHANDLED_HTTP_CODE
:
573 mStatusIcon
= R
.drawable
.common_error
;
574 mStatusText
= R
.string
.auth_unknown_error_title
;
577 Log
.e(TAG
, "Incorrect connection checker result type: " + result
.getHttpCode());
579 setResultIconAndText(mStatusIcon
, mStatusText
);
581 findViewById(R
.id
.refreshButton
).setVisibility(View
.VISIBLE
);
583 findViewById(R
.id
.refreshButton
).setVisibility(View
.INVISIBLE
);
584 findViewById(R
.id
.buttonOK
).setEnabled(mStatusCorrect
);
589 public void onSavedCertificate() {
590 mAuthThread
= mConnChkRunnable
.retry(this, mHandler
);
594 public void onFailedSavingCertificate() {
595 showDialog(DIALOG_CERT_NOT_SAVED
);