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 eu
.alefzero
.owncloud
.ui
.activity
;
21 import java
.net
.MalformedURLException
;
23 import java
.net
.URLEncoder
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountAuthenticatorActivity
;
27 import android
.accounts
.AccountManager
;
28 import android
.app
.Dialog
;
29 import android
.app
.ProgressDialog
;
30 import android
.content
.ContentResolver
;
31 import android
.content
.DialogInterface
;
32 import android
.content
.Intent
;
33 import android
.content
.SharedPreferences
;
34 import android
.os
.Bundle
;
35 import android
.os
.Handler
;
36 import android
.preference
.PreferenceManager
;
37 import android
.text
.InputType
;
38 import android
.util
.Log
;
39 import android
.view
.View
;
40 import android
.view
.View
.OnClickListener
;
41 import android
.view
.View
.OnFocusChangeListener
;
42 import android
.view
.Window
;
43 import android
.widget
.ImageView
;
44 import android
.widget
.TextView
;
45 import eu
.alefzero
.owncloud
.AccountUtils
;
46 import eu
.alefzero
.owncloud
.R
;
47 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
48 import eu
.alefzero
.owncloud
.authenticator
.AuthenticationRunnable
;
49 import eu
.alefzero
.owncloud
.authenticator
.ConnectionCheckerRunnable
;
50 import eu
.alefzero
.owncloud
.authenticator
.OnAuthenticationResultListener
;
51 import eu
.alefzero
.owncloud
.authenticator
.OnConnectCheckListener
;
52 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
53 import eu
.alefzero
.owncloud
.extensions
.ExtensionsAvailableActivity
;
54 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
57 * This Activity is used to add an ownCloud account to the App
59 * @author Bartek Przybylski
62 public class AuthenticatorActivity
extends AccountAuthenticatorActivity
63 implements OnAuthenticationResultListener
, OnConnectCheckListener
,
64 OnFocusChangeListener
, OnClickListener
{
65 private static final int DIALOG_LOGIN_PROGRESS
= 0;
67 private static final String TAG
= "AuthActivity";
69 private Thread mAuthThread
;
70 private AuthenticationRunnable mAuthRunnable
;
71 private ConnectionCheckerRunnable mConnChkRunnable
;
72 private final Handler mHandler
= new Handler();
73 private String mBaseUrl
;
75 private static final String STATUS_TEXT
= "STATUS_TEXT";
76 private static final String STATUS_ICON
= "STATUS_ICON";
77 private static final String STATUS_CORRECT
= "STATUS_CORRECT";
78 private static final String IS_SSL_CONN
= "IS_SSL_CONN";
79 private int mStatusText
, mStatusIcon
;
80 private boolean mStatusCorrect
, mIsSslConn
;
82 public static final String PARAM_USERNAME
= "param_Username";
83 public static final String PARAM_HOSTNAME
= "param_Hostname";
86 protected void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
88 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
89 setContentView(R
.layout
.account_setup
);
90 ImageView iv
= (ImageView
) findViewById(R
.id
.refreshButton
);
91 ImageView iv2
= (ImageView
) findViewById(R
.id
.viewPassword
);
92 TextView tv
= (TextView
) findViewById(R
.id
.host_URL
);
93 TextView tv2
= (TextView
) findViewById(R
.id
.account_password
);
95 if (savedInstanceState
!= null
) {
96 mStatusIcon
= savedInstanceState
.getInt(STATUS_ICON
);
97 mStatusText
= savedInstanceState
.getInt(STATUS_TEXT
);
98 mStatusCorrect
= savedInstanceState
.getBoolean(STATUS_CORRECT
);
99 mIsSslConn
= savedInstanceState
.getBoolean(IS_SSL_CONN
);
100 setResultIconAndText(mStatusIcon
, mStatusText
);
101 findViewById(R
.id
.buttonOK
).setEnabled(mStatusCorrect
);
103 iv
.setVisibility(View
.VISIBLE
);
105 iv
.setVisibility(View
.INVISIBLE
);
108 mStatusText
= mStatusIcon
= 0;
109 mStatusCorrect
= false
;
112 iv
.setOnClickListener(this);
113 iv2
.setOnClickListener(this);
114 tv
.setOnFocusChangeListener(this);
115 tv2
.setOnFocusChangeListener(this);
119 protected void onSaveInstanceState(Bundle outState
) {
120 outState
.putInt(STATUS_ICON
, mStatusIcon
);
121 outState
.putInt(STATUS_TEXT
, mStatusText
);
122 outState
.putBoolean(STATUS_CORRECT
, mStatusCorrect
);
123 super.onSaveInstanceState(outState
);
127 protected Dialog
onCreateDialog(int id
) {
128 Dialog dialog
= null
;
130 case DIALOG_LOGIN_PROGRESS
: {
131 ProgressDialog working_dialog
= new ProgressDialog(this);
132 working_dialog
.setMessage(getResources().getString(
133 R
.string
.auth_trying_to_login
));
134 working_dialog
.setIndeterminate(true
);
135 working_dialog
.setCancelable(true
);
137 .setOnCancelListener(new DialogInterface
.OnCancelListener() {
139 public void onCancel(DialogInterface dialog
) {
140 Log
.i(TAG
, "Login canceled");
141 if (mAuthThread
!= null
) {
142 mAuthThread
.interrupt();
147 dialog
= working_dialog
;
151 Log
.e(TAG
, "Incorrect dialog called with id = " + id
);
156 public void onAuthenticationResult(boolean success
, String message
) {
158 TextView username_text
= (TextView
) findViewById(R
.id
.account_username
), password_text
= (TextView
) findViewById(R
.id
.account_password
);
162 url
= new URL(message
);
163 } catch (MalformedURLException e
) {
164 // should never happen
165 Log
.e(getClass().getName(), "Malformed URL: " + message
);
169 String username
= username_text
.getText().toString().trim();
170 String accountName
= username
+ "@" + url
.getHost();
171 if (url
.getPort() >= 0) {
172 accountName
+= ":" + url
.getPort();
174 Account account
= new Account(accountName
,
175 AccountAuthenticator
.ACCOUNT_TYPE
);
176 AccountManager accManager
= AccountManager
.get(this);
177 accManager
.addAccountExplicitly(account
, password_text
.getText()
180 // Add this account as default in the preferences, if there is none
182 Account defaultAccount
= AccountUtils
183 .getCurrentOwnCloudAccount(this);
184 if (defaultAccount
== null
) {
185 SharedPreferences
.Editor editor
= PreferenceManager
186 .getDefaultSharedPreferences(this).edit();
187 editor
.putString("select_oc_account", accountName
);
191 final Intent intent
= new Intent();
192 intent
.putExtra(AccountManager
.KEY_ACCOUNT_TYPE
,
193 AccountAuthenticator
.ACCOUNT_TYPE
);
194 intent
.putExtra(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
195 intent
.putExtra(AccountManager
.KEY_AUTHTOKEN
,
196 AccountAuthenticator
.ACCOUNT_TYPE
);
197 intent
.putExtra(AccountManager
.KEY_USERDATA
, username
);
199 accManager
.setUserData(account
, AccountAuthenticator
.KEY_OC_URL
,
201 accManager
.setUserData(account
,
202 AccountAuthenticator
.KEY_OC_VERSION
, mConnChkRunnable
203 .getDiscoveredVersion().toString());
204 accManager
.setUserData(account
,
205 AccountAuthenticator
.KEY_OC_BASE_URL
, mBaseUrl
);
207 setAccountAuthenticatorResult(intent
.getExtras());
208 setResult(RESULT_OK
, intent
);
209 Bundle bundle
= new Bundle();
210 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
211 //getContentResolver().startSync(ProviderTableMeta.CONTENT_URI,
213 ContentResolver
.requestSync(account
, "org.owncloud", bundle
);
217 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
218 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
219 * ExtensionsAvailableActivity.class); startActivity(i); }
224 dismissDialog(DIALOG_LOGIN_PROGRESS
);
225 TextView tv
= (TextView
) findViewById(R
.id
.account_username
);
226 tv
.setError(message
);
229 public void onCancelClick(View view
) {
233 public void onOkClick(View view
) {
235 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
242 if (url
.toLowerCase().startsWith("http://")
243 || url
.toLowerCase().startsWith("https://")) {
246 continueConnection(prefix
);
249 private void continueConnection(String prefix
) {
250 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
252 String username
= ((TextView
) findViewById(R
.id
.account_username
))
253 .getText().toString();
254 String password
= ((TextView
) findViewById(R
.id
.account_password
))
255 .getText().toString();
256 if (url
.endsWith("/"))
257 url
= url
.substring(0, url
.length() - 1);
260 String webdav_path
= AccountUtils
.getWebdavPath(mConnChkRunnable
261 .getDiscoveredVersion());
264 mBaseUrl
= prefix
+ url
;
265 String url_str
= prefix
+ url
+ webdav_path
;
266 uri
= new URL(url_str
);
267 } catch (MalformedURLException e
) {
268 // should not happend
272 showDialog(DIALOG_LOGIN_PROGRESS
);
273 mAuthRunnable
= new AuthenticationRunnable(uri
, username
, password
);
274 mAuthRunnable
.setOnAuthenticationResultListener(this, mHandler
);
275 mAuthThread
= new Thread(mAuthRunnable
);
280 public void onConnectionCheckResult(ResultType type
) {
281 mStatusText
= mStatusIcon
= 0;
282 mStatusCorrect
= false
;
283 String t_url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText()
284 .toString().toLowerCase();
289 if (t_url
.startsWith("http://") || t_url
.startsWith("https://")) {
290 mIsSslConn
= t_url
.startsWith("http://") ? false
: true
;
291 mStatusIcon
= R
.drawable
.ic_ok
;
292 mStatusText
= R
.string
.auth_connection_established
;
293 mStatusCorrect
= true
;
296 mStatusIcon
= android
.R
.drawable
.ic_secure
;
297 mStatusText
= R
.string
.auth_secure_connection
;
298 mStatusCorrect
= true
;
302 mStatusIcon
= android
.R
.drawable
.ic_secure
;
303 mStatusText
= R
.string
.auth_nossl_plain_ok_title
;
304 mStatusCorrect
= true
;
308 case INORRECT_ADDRESS
:
310 case HOST_NOT_AVAILABLE
:
311 mStatusIcon
= R
.drawable
.common_error
;
312 mStatusText
= R
.string
.auth_unknow_host_title
;
314 case NO_NETWORK_CONNECTION
:
315 mStatusIcon
= R
.drawable
.no_network
;
316 mStatusText
= R
.string
.auth_no_net_conn_title
;
318 case INSTANCE_NOT_CONFIGURED
:
319 mStatusIcon
= R
.drawable
.common_error
;
320 mStatusText
= R
.string
.auth_not_configured_title
;
323 mStatusIcon
= R
.drawable
.common_error
;
324 mStatusText
= R
.string
.auth_unknow_error
;
327 mStatusIcon
= R
.drawable
.common_error
;
328 mStatusText
= R
.string
.auth_incorrect_path_title
;
331 Log
.e(TAG
, "Incorrect connection checker result type: " + type
);
333 setResultIconAndText(mStatusIcon
, mStatusText
);
335 findViewById(R
.id
.refreshButton
).setVisibility(View
.VISIBLE
);
337 findViewById(R
.id
.refreshButton
).setVisibility(View
.INVISIBLE
);
338 findViewById(R
.id
.buttonOK
).setEnabled(mStatusCorrect
);
342 public void onFocusChange(View view
, boolean hasFocus
) {
343 if (view
.getId() == R
.id
.host_URL
) {
345 TextView tv
= ((TextView
) findViewById(R
.id
.host_URL
));
346 String uri
= tv
.getText().toString();
347 if (uri
.length() != 0) {
348 setResultIconAndText(R
.drawable
.progress_small
,
349 R
.string
.auth_testing_connection
);
350 findViewById(R
.id
.buttonOK
).setEnabled(false
); // avoid connect can be clicked if the test was previously passed
351 mConnChkRunnable
= new ConnectionCheckerRunnable(uri
, this);
352 mConnChkRunnable
.setListener(this, mHandler
);
353 mAuthThread
= new Thread(mConnChkRunnable
);
356 findViewById(R
.id
.refreshButton
).setVisibility(
358 setResultIconAndText(0, 0);
361 } else if (view
.getId() == R
.id
.account_password
) {
362 ImageView iv
= (ImageView
) findViewById(R
.id
.viewPassword
);
364 iv
.setVisibility(View
.VISIBLE
);
366 TextView v
= (TextView
) findViewById(R
.id
.account_password
);
367 int input_type
= InputType
.TYPE_CLASS_TEXT
368 | InputType
.TYPE_TEXT_VARIATION_PASSWORD
;
369 v
.setInputType(input_type
);
370 iv
.setVisibility(View
.INVISIBLE
);
375 private void setResultIconAndText(int drawable_id
, int text_id
) {
376 ImageView iv
= (ImageView
) findViewById(R
.id
.action_indicator
);
377 TextView tv
= (TextView
) findViewById(R
.id
.status_text
);
379 if (drawable_id
== 0 && text_id
== 0) {
380 iv
.setVisibility(View
.INVISIBLE
);
381 tv
.setVisibility(View
.INVISIBLE
);
383 iv
.setImageResource(drawable_id
);
385 iv
.setVisibility(View
.VISIBLE
);
386 tv
.setVisibility(View
.VISIBLE
);
391 public void onClick(View v
) {
392 if (v
.getId() == R
.id
.refreshButton
) {
393 onFocusChange(findViewById(R
.id
.host_URL
), false
);
394 } else if (v
.getId() == R
.id
.viewPassword
) {
395 TextView view
= (TextView
) findViewById(R
.id
.account_password
);
396 int input_type
= InputType
.TYPE_CLASS_TEXT
397 | InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
;
398 view
.setInputType(input_type
);