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
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountAuthenticatorActivity
;
26 import android
.accounts
.AccountManager
;
27 import android
.app
.Dialog
;
28 import android
.app
.ProgressDialog
;
29 import android
.content
.ContentResolver
;
30 import android
.content
.DialogInterface
;
31 import android
.content
.Intent
;
32 import android
.content
.SharedPreferences
;
33 import android
.os
.Bundle
;
34 import android
.os
.Handler
;
35 import android
.preference
.PreferenceManager
;
36 import android
.text
.InputType
;
37 import android
.util
.Log
;
38 import android
.view
.View
;
39 import android
.view
.View
.OnClickListener
;
40 import android
.view
.View
.OnFocusChangeListener
;
41 import android
.view
.Window
;
42 import android
.widget
.ImageView
;
43 import android
.widget
.TextView
;
44 import eu
.alefzero
.owncloud
.AccountUtils
;
45 import eu
.alefzero
.owncloud
.R
;
46 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
47 import eu
.alefzero
.owncloud
.authenticator
.AuthenticationRunnable
;
48 import eu
.alefzero
.owncloud
.authenticator
.ConnectionCheckerRunnable
;
49 import eu
.alefzero
.owncloud
.authenticator
.OnAuthenticationResultListener
;
50 import eu
.alefzero
.owncloud
.authenticator
.OnConnectCheckListener
;
51 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
52 import eu
.alefzero
.owncloud
.extensions
.ExtensionsAvailableActivity
;
53 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
56 * This Activity is used to add an ownCloud account to the App
58 * @author Bartek Przybylski
61 public class AuthenticatorActivity
extends AccountAuthenticatorActivity
62 implements OnAuthenticationResultListener
,
63 OnConnectCheckListener
,
64 OnFocusChangeListener
,
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(R
.string
.auth_trying_to_login
));
134 working_dialog
.setIndeterminate(true
);
135 working_dialog
.setCancelable(true
);
136 working_dialog
.setOnCancelListener(new DialogInterface
.OnCancelListener() {
138 public void onCancel(DialogInterface dialog
) {
139 Log
.i(TAG
, "Login canceled");
140 if (mAuthThread
!= null
) {
141 mAuthThread
.interrupt();
146 dialog
= working_dialog
;
150 Log
.e(TAG
, "Incorrect dialog called with id = " + id
);
155 public void onAuthenticationResult(boolean success
, String message
) {
157 TextView username_text
= (TextView
) findViewById(R
.id
.account_username
), password_text
= (TextView
) findViewById(R
.id
.account_password
);
161 url
= new URL(message
);
162 } catch (MalformedURLException e
) {
163 // should never happen
164 Log
.e(getClass().getName(), "Malformed URL: " + message
);
168 String username
= username_text
.getText().toString().trim();
169 String accountName
= username
+ "@" + url
.getHost();
170 Account account
= new Account(accountName
,
171 AccountAuthenticator
.ACCOUNT_TYPE
);
172 AccountManager accManager
= AccountManager
.get(this);
173 accManager
.addAccountExplicitly(account
, password_text
.getText()
176 // Add this account as default in the preferences, if there is none
178 Account defaultAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
179 if (defaultAccount
== null
) {
180 SharedPreferences
.Editor editor
= PreferenceManager
181 .getDefaultSharedPreferences(this).edit();
182 editor
.putString("select_oc_account", accountName
);
186 final Intent intent
= new Intent();
187 intent
.putExtra(AccountManager
.KEY_ACCOUNT_TYPE
, AccountAuthenticator
.ACCOUNT_TYPE
);
188 intent
.putExtra(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
189 intent
.putExtra(AccountManager
.KEY_AUTHTOKEN
, AccountAuthenticator
.ACCOUNT_TYPE
);
190 intent
.putExtra(AccountManager
.KEY_USERDATA
, username
);
192 accManager
.setUserData(account
,
193 AccountAuthenticator
.KEY_OC_URL
,
195 accManager
.setUserData(account
,
196 AccountAuthenticator
.KEY_OC_VERSION
,
197 mConnChkRunnable
.getDiscoveredVersion().toString());
198 accManager
.setUserData(account
,
199 AccountAuthenticator
.KEY_OC_BASE_URL
,
202 setAccountAuthenticatorResult(intent
.getExtras());
203 setResult(RESULT_OK
, intent
);
204 Bundle bundle
= new Bundle();
205 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
206 getContentResolver().startSync(ProviderTableMeta
.CONTENT_URI
, bundle
);
208 /*if (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion.owncloud_v2) >= 0) {
209 Intent i = new Intent(this, ExtensionsAvailableActivity.class);
215 dismissDialog(DIALOG_LOGIN_PROGRESS
);
216 TextView tv
= (TextView
) findViewById(R
.id
.account_username
);
217 tv
.setError(message
);
221 public void onOkClick(View view
) {
223 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText().toString();
229 if (url
.toLowerCase().startsWith("http://") || url
.toLowerCase().startsWith("https://")) {
232 continueConnection(prefix
);
235 private void continueConnection(String prefix
) {
236 String url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText().toString();
237 String username
= ((TextView
) findViewById(R
.id
.account_username
)).getText().toString();
238 String password
= ((TextView
) findViewById(R
.id
.account_password
)).getText().toString();
239 if (url
.endsWith("/"))
240 url
= url
.substring(0, url
.length()-1);
243 String webdav_path
= AccountUtils
.getWebdavPath(mConnChkRunnable
.getDiscoveredVersion());
246 mBaseUrl
= prefix
+ url
;
247 String url_str
= prefix
+ url
+ webdav_path
;
248 uri
= new URL(url_str
);
249 } catch (MalformedURLException e
) {
250 // should not happend
254 showDialog(DIALOG_LOGIN_PROGRESS
);
255 mAuthRunnable
= new AuthenticationRunnable(uri
, username
, password
);
256 mAuthRunnable
.setOnAuthenticationResultListener(this, mHandler
);
257 mAuthThread
= new Thread(mAuthRunnable
);
262 public void onConnectionCheckResult(ResultType type
) {
263 mStatusText
= mStatusIcon
= 0;
264 mStatusCorrect
= false
;
265 String t_url
= ((TextView
) findViewById(R
.id
.host_URL
)).getText().toString().toLowerCase();
270 if (t_url
.startsWith("http://") || t_url
.startsWith("https://")) {
271 mIsSslConn
= t_url
.startsWith("http://") ? false
: true
;
272 mStatusIcon
= R
.drawable
.ic_ok
;
273 mStatusText
= R
.string
.auth_connection_established
;
274 mStatusCorrect
= true
;
277 mStatusIcon
= android
.R
.drawable
.ic_secure
;
278 mStatusText
= R
.string
.auth_secure_connection
;
279 mStatusCorrect
= true
;
283 mStatusIcon
= android
.R
.drawable
.ic_secure
;
284 mStatusText
= R
.string
.auth_nossl_plain_ok_title
;
285 mStatusCorrect
= true
;
289 case INORRECT_ADDRESS
:
291 case HOST_NOT_AVAILABLE
:
292 mStatusIcon
= R
.drawable
.common_error
;
293 mStatusText
= R
.string
.auth_unknow_host_title
;
295 case NO_NETWORK_CONNECTION
:
296 mStatusIcon
= R
.drawable
.no_network
;
297 mStatusText
= R
.string
.auth_no_net_conn_title
;
299 case INSTANCE_NOT_CONFIGURED
:
300 mStatusIcon
= R
.drawable
.common_error
;
301 mStatusText
= R
.string
.auth_not_configured_title
;
304 mStatusIcon
= R
.drawable
.common_error
;
305 mStatusText
= R
.string
.auth_unknow_error
;
308 mStatusIcon
= R
.drawable
.common_error
;
309 mStatusText
= R
.string
.auth_incorrect_path_title
;
312 Log
.e(TAG
, "Incorrect connection checker result type: " + type
);
314 setResultIconAndText(mStatusIcon
, mStatusText
);
316 findViewById(R
.id
.refreshButton
).setVisibility(View
.VISIBLE
);
318 findViewById(R
.id
.refreshButton
).setVisibility(View
.INVISIBLE
);
319 findViewById(R
.id
.buttonOK
).setEnabled(mStatusCorrect
);
323 public void onFocusChange(View view
, boolean hasFocus
) {
324 if (view
.getId() == R
.id
.host_URL
) {
326 TextView tv
= ((TextView
)findViewById(R
.id
.host_URL
));
327 String uri
= tv
.getText().toString();
328 if (uri
.length() != 0) {
329 setResultIconAndText(R
.drawable
.progress_small
, R
.string
.auth_testing_connection
);
330 mConnChkRunnable
= new ConnectionCheckerRunnable(uri
, this);
331 mConnChkRunnable
.setListener(this, mHandler
);
332 mAuthThread
= new Thread(mConnChkRunnable
);
335 findViewById(R
.id
.refreshButton
).setVisibility(View
.INVISIBLE
);
336 setResultIconAndText(0, 0);
339 } else if (view
.getId() == R
.id
.account_password
) {
340 ImageView iv
= (ImageView
) findViewById(R
.id
.viewPassword
);
342 iv
.setVisibility(View
.VISIBLE
);
344 TextView v
= (TextView
) findViewById(R
.id
.account_password
);
345 int input_type
= InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
;
346 v
.setInputType(input_type
);
347 iv
.setVisibility(View
.INVISIBLE
);
352 private void setResultIconAndText(int drawable_id
, int text_id
) {
353 ImageView iv
= (ImageView
) findViewById(R
.id
.action_indicator
);
354 TextView tv
= (TextView
) findViewById(R
.id
.status_text
);
356 if (drawable_id
== 0 && text_id
== 0) {
357 iv
.setVisibility(View
.INVISIBLE
);
358 tv
.setVisibility(View
.INVISIBLE
);
360 iv
.setImageResource(drawable_id
);
362 iv
.setVisibility(View
.VISIBLE
);
363 tv
.setVisibility(View
.VISIBLE
);
368 public void onClick(View v
) {
369 if (v
.getId() == R
.id
.refreshButton
) {
370 onFocusChange(findViewById(R
.id
.host_URL
), false
);
371 } else if (v
.getId() == R
.id
.viewPassword
) {
372 TextView view
= (TextView
) findViewById(R
.id
.account_password
);
373 int input_type
= InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
;
374 view
.setInputType(input_type
);