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
;
23 import java
.net
.URLEncoder
;
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
;
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
;
58 * This Activity is used to add an ownCloud account to the App
60 * @author Bartek Przybylski
63 public class AuthenticatorActivity
extends AccountAuthenticatorActivity
64 implements OnAuthenticationResultListener
, OnConnectCheckListener
,
65 OnFocusChangeListener
, OnClickListener
{
66 private static final int DIALOG_LOGIN_PROGRESS
= 0;
68 private static final String TAG
= "AuthActivity";
70 private Thread mAuthThread
;
71 private AuthenticationRunnable mAuthRunnable
;
72 private ConnectionCheckerRunnable mConnChkRunnable
;
73 private final Handler mHandler
= new Handler();
74 private String mBaseUrl
;
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
;
83 public static final String PARAM_USERNAME
= "param_Username";
84 public static final String PARAM_HOSTNAME
= "param_Hostname";
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
);
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
);
104 iv
.setVisibility(View
.VISIBLE
);
106 iv
.setVisibility(View
.INVISIBLE
);
109 mStatusText
= mStatusIcon
= 0;
110 mStatusCorrect
= false
;
113 iv
.setOnClickListener(this);
114 iv2
.setOnClickListener(this);
115 tv
.setOnFocusChangeListener(this);
116 tv2
.setOnFocusChangeListener(this);
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
);
128 protected Dialog
onCreateDialog(int id
) {
129 Dialog dialog
= null
;
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
);
138 .setOnCancelListener(new DialogInterface
.OnCancelListener() {
140 public void onCancel(DialogInterface dialog
) {
141 Log
.i(TAG
, "Login canceled");
142 if (mAuthThread
!= null
) {
143 mAuthThread
.interrupt();
148 dialog
= working_dialog
;
152 Log
.e(TAG
, "Incorrect dialog called with id = " + id
);
157 public void onAuthenticationResult(boolean success
, String message
) {
159 TextView username_text
= (TextView
) findViewById(R
.id
.account_username
), password_text
= (TextView
) findViewById(R
.id
.account_password
);
163 url
= new URL(message
);
164 } catch (MalformedURLException e
) {
165 // should never happen
166 Log
.e(getClass().getName(), "Malformed URL: " + message
);
170 String username
= username_text
.getText().toString().trim();
171 String accountName
= username
+ "@" + url
.getHost();
172 if (url
.getPort() >= 0) {
173 accountName
+= ":" + url
.getPort();
175 Account account
= new Account(accountName
,
176 AccountAuthenticator
.ACCOUNT_TYPE
);
177 AccountManager accManager
= AccountManager
.get(this);
178 accManager
.addAccountExplicitly(account
, password_text
.getText()
181 // Add this account as default in the preferences, if there is none
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
);
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
);
200 accManager
.setUserData(account
, AccountAuthenticator
.KEY_OC_URL
,
202 accManager
.setUserData(account
,
203 AccountAuthenticator
.KEY_OC_VERSION
, mConnChkRunnable
204 .getDiscoveredVersion().toString());
205 accManager
.setUserData(account
,
206 AccountAuthenticator
.KEY_OC_BASE_URL
, mBaseUrl
);
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,
214 ContentResolver
.requestSync(account
, "org.owncloud", bundle
);
218 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
219 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
220 * ExtensionsAvailableActivity.class); startActivity(i); }
225 dismissDialog(DIALOG_LOGIN_PROGRESS
);
226 TextView tv
= (TextView
) findViewById(R
.id
.account_username
);
227 tv
.setError(message
);
230 public void onCancelClick(View view
) {
234 public void onOkClick(View view
) {
236 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
243 if (url
.toLowerCase().startsWith("http://")
244 || url
.toLowerCase().startsWith("https://")) {
247 continueConnection(prefix
);
250 private void continueConnection(String prefix
) {
251 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
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);
261 String webdav_path
= AccountUtils
.getWebdavPath(mConnChkRunnable
262 .getDiscoveredVersion());
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
273 showDialog(DIALOG_LOGIN_PROGRESS
);
274 mAuthRunnable
= new AuthenticationRunnable(uri
, username
, password
);
275 mAuthRunnable
.setOnAuthenticationResultListener(this, mHandler
);
276 mAuthThread
= new Thread(mAuthRunnable
);
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();
290 mStatusIcon
= android
.R
.drawable
.ic_secure
;
291 mStatusText
= R
.string
.auth_secure_connection
;
292 mStatusCorrect
= true
;
296 mStatusCorrect
= true
;
297 if (t_url
.startsWith("http://") ) {
298 mStatusText
= R
.string
.auth_connection_established
;
299 mStatusIcon
= R
.drawable
.ic_ok
;
301 mStatusText
= R
.string
.auth_nossl_plain_ok_title
;
302 mStatusIcon
= android
.R
.drawable
.ic_partial_secure
;
306 mStatusIcon
= R
.drawable
.common_error
;
307 mStatusText
= R
.string
.auth_bad_oc_version_title
;
309 case WRONG_CONNECTION
:
310 mStatusIcon
= R
.drawable
.common_error
;
311 mStatusText
= R
.string
.auth_wrong_connection_title
;
314 mStatusIcon
= R
.drawable
.common_error
;
315 mStatusText
= R
.string
.auth_timeout_title
;
317 case INCORRECT_ADDRESS
:
318 mStatusIcon
= R
.drawable
.common_error
;
319 mStatusText
= R
.string
.auth_incorrect_address_title
;
321 case SSL_UNVERIFIED_SERVER
:
322 mStatusIcon
= R
.drawable
.common_error
;
323 mStatusText
= R
.string
.auth_ssl_unverified_server_title
;
326 mStatusIcon
= R
.drawable
.common_error
;
327 mStatusText
= R
.string
.auth_ssl_general_error_title
;
329 case HOST_NOT_AVAILABLE
:
330 mStatusIcon
= R
.drawable
.common_error
;
331 mStatusText
= R
.string
.auth_unknown_host_title
;
333 case NO_NETWORK_CONNECTION
:
334 mStatusIcon
= R
.drawable
.no_network
;
335 mStatusText
= R
.string
.auth_no_net_conn_title
;
337 case INSTANCE_NOT_CONFIGURED
:
338 mStatusIcon
= R
.drawable
.common_error
;
339 mStatusText
= R
.string
.auth_not_configured_title
;
342 mStatusIcon
= R
.drawable
.common_error
;
343 mStatusText
= R
.string
.auth_unknown_error_title
;
346 mStatusIcon
= R
.drawable
.common_error
;
347 mStatusText
= R
.string
.auth_incorrect_path_title
;
350 Log
.e(TAG
, "Incorrect connection checker result type: " + type
);
352 setResultIconAndText(mStatusIcon
, mStatusText
);
354 findViewById(R
.id
.refreshButton
).setVisibility(View
.VISIBLE
);
356 findViewById(R
.id
.refreshButton
).setVisibility(View
.INVISIBLE
);
357 findViewById(R
.id
.buttonOK
).setEnabled(mStatusCorrect
);
361 public void onFocusChange(View view
, boolean hasFocus
) {
362 if (view
.getId() == R
.id
.host_URL
) {
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
);
375 findViewById(R
.id
.refreshButton
).setVisibility(
377 setResultIconAndText(0, 0);
380 } else if (view
.getId() == R
.id
.account_password
) {
381 ImageView iv
= (ImageView
) findViewById(R
.id
.viewPassword
);
383 iv
.setVisibility(View
.VISIBLE
);
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
);
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
);
398 if (drawable_id
== 0 && text_id
== 0) {
399 iv
.setVisibility(View
.INVISIBLE
);
400 tv
.setVisibility(View
.INVISIBLE
);
402 iv
.setImageResource(drawable_id
);
404 iv
.setVisibility(View
.VISIBLE
);
405 tv
.setVisibility(View
.VISIBLE
);
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
);