moving from eu.alefzero.eu to com.owncloud.android
[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 import java.net.URLEncoder;
24
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;
34
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;
56
57 /**
58 * This Activity is used to add an ownCloud account to the App
59 *
60 * @author Bartek Przybylski
61 *
62 */
63 public class AuthenticatorActivity extends AccountAuthenticatorActivity
64 implements OnAuthenticationResultListener, OnConnectCheckListener,
65 OnFocusChangeListener, OnClickListener {
66 private static final int DIALOG_LOGIN_PROGRESS = 0;
67
68 private static final String TAG = "AuthActivity";
69
70 private Thread mAuthThread;
71 private AuthenticationRunnable mAuthRunnable;
72 private ConnectionCheckerRunnable mConnChkRunnable;
73 private final Handler mHandler = new Handler();
74 private String mBaseUrl;
75
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;
82
83 public static final String PARAM_USERNAME = "param_Username";
84 public static final String PARAM_HOSTNAME = "param_Hostname";
85
86 @Override
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);
95
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);
103 if (!mStatusCorrect)
104 iv.setVisibility(View.VISIBLE);
105 else
106 iv.setVisibility(View.INVISIBLE);
107
108 } else {
109 mStatusText = mStatusIcon = 0;
110 mStatusCorrect = false;
111 mIsSslConn = false;
112 }
113 iv.setOnClickListener(this);
114 iv2.setOnClickListener(this);
115 tv.setOnFocusChangeListener(this);
116 tv2.setOnFocusChangeListener(this);
117 }
118
119 @Override
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);
125 }
126
127 @Override
128 protected Dialog onCreateDialog(int id) {
129 Dialog dialog = null;
130 switch (id) {
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);
137 working_dialog
138 .setOnCancelListener(new DialogInterface.OnCancelListener() {
139 @Override
140 public void onCancel(DialogInterface dialog) {
141 Log.i(TAG, "Login canceled");
142 if (mAuthThread != null) {
143 mAuthThread.interrupt();
144 finish();
145 }
146 }
147 });
148 dialog = working_dialog;
149 break;
150 }
151 default:
152 Log.e(TAG, "Incorrect dialog called with id = " + id);
153 }
154 return dialog;
155 }
156
157 public void onAuthenticationResult(boolean success, String message) {
158 if (success) {
159 TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);
160
161 URL url;
162 try {
163 url = new URL(message);
164 } catch (MalformedURLException e) {
165 // should never happen
166 Log.e(getClass().getName(), "Malformed URL: " + message);
167 return;
168 }
169
170 String username = username_text.getText().toString().trim();
171 String accountName = username + "@" + url.getHost();
172 if (url.getPort() >= 0) {
173 accountName += ":" + url.getPort();
174 }
175 Account account = new Account(accountName,
176 AccountAuthenticator.ACCOUNT_TYPE);
177 AccountManager accManager = AccountManager.get(this);
178 accManager.addAccountExplicitly(account, password_text.getText()
179 .toString(), null);
180
181 // Add this account as default in the preferences, if there is none
182 // already
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);
189 editor.commit();
190 }
191
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);
199
200 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,
201 url.toString());
202 accManager.setUserData(account,
203 AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable
204 .getDiscoveredVersion().toString());
205 accManager.setUserData(account,
206 AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);
207
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,
213 // bundle);
214 ContentResolver.requestSync(account, "org.owncloud", bundle);
215
216 /*
217 * if
218 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
219 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
220 * ExtensionsAvailableActivity.class); startActivity(i); }
221 */
222
223 finish();
224 } else {
225 dismissDialog(DIALOG_LOGIN_PROGRESS);
226 TextView tv = (TextView) findViewById(R.id.account_username);
227 tv.setError(message);
228 }
229 }
230 public void onCancelClick(View view) {
231 finish();
232 }
233
234 public void onOkClick(View view) {
235 String prefix = "";
236 String url = ((TextView) findViewById(R.id.host_URL)).getText()
237 .toString();
238 if (mIsSslConn) {
239 prefix = "https://";
240 } else {
241 prefix = "http://";
242 }
243 if (url.toLowerCase().startsWith("http://")
244 || url.toLowerCase().startsWith("https://")) {
245 prefix = "";
246 }
247 continueConnection(prefix);
248 }
249
250 private void continueConnection(String prefix) {
251 String url = ((TextView) findViewById(R.id.host_URL)).getText()
252 .toString();
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);
259
260 URL uri = null;
261 String webdav_path = AccountUtils.getWebdavPath(mConnChkRunnable
262 .getDiscoveredVersion());
263
264 try {
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
270 e.printStackTrace();
271 }
272
273 showDialog(DIALOG_LOGIN_PROGRESS);
274 mAuthRunnable = new AuthenticationRunnable(uri, username, password);
275 mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);
276 mAuthThread = new Thread(mAuthRunnable);
277 mAuthThread.start();
278 }
279
280 @Override
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().toLowerCase();
286
287 switch (type) {
288 case OK_SSL:
289 mIsSslConn = true;
290 mStatusIcon = android.R.drawable.ic_secure;
291 mStatusText = R.string.auth_secure_connection;
292 mStatusCorrect = true;
293 break;
294 case OK_NO_SSL:
295 mIsSslConn = false;
296 mStatusCorrect = true;
297 if (t_url.startsWith("http://") ) {
298 mStatusText = R.string.auth_connection_established;
299 mStatusIcon = R.drawable.ic_ok;
300 } else {
301 mStatusText = R.string.auth_nossl_plain_ok_title;
302 mStatusIcon = android.R.drawable.ic_partial_secure;
303 }
304 break;
305 case TIMEOUT:
306 case INORRECT_ADDRESS:
307 case SSL_INIT_ERROR:
308 case HOST_NOT_AVAILABLE:
309 mStatusIcon = R.drawable.common_error;
310 mStatusText = R.string.auth_unknow_host_title;
311 break;
312 case NO_NETWORK_CONNECTION:
313 mStatusIcon = R.drawable.no_network;
314 mStatusText = R.string.auth_no_net_conn_title;
315 break;
316 case INSTANCE_NOT_CONFIGURED:
317 mStatusIcon = R.drawable.common_error;
318 mStatusText = R.string.auth_not_configured_title;
319 break;
320 case UNKNOWN_ERROR:
321 mStatusIcon = R.drawable.common_error;
322 mStatusText = R.string.auth_unknow_error;
323 break;
324 case FILE_NOT_FOUND:
325 mStatusIcon = R.drawable.common_error;
326 mStatusText = R.string.auth_incorrect_path_title;
327 break;
328 default:
329 Log.e(TAG, "Incorrect connection checker result type: " + type);
330 }
331 setResultIconAndText(mStatusIcon, mStatusText);
332 if (!mStatusCorrect)
333 findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
334 else
335 findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
336 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
337 }
338
339 @Override
340 public void onFocusChange(View view, boolean hasFocus) {
341 if (view.getId() == R.id.host_URL) {
342 if (!hasFocus) {
343 TextView tv = ((TextView) findViewById(R.id.host_URL));
344 String uri = tv.getText().toString();
345 if (uri.length() != 0) {
346 setResultIconAndText(R.drawable.progress_small,
347 R.string.auth_testing_connection);
348 findViewById(R.id.buttonOK).setEnabled(false); // avoid connect can be clicked if the test was previously passed
349 mConnChkRunnable = new ConnectionCheckerRunnable(uri, this);
350 mConnChkRunnable.setListener(this, mHandler);
351 mAuthThread = new Thread(mConnChkRunnable);
352 mAuthThread.start();
353 } else {
354 findViewById(R.id.refreshButton).setVisibility(
355 View.INVISIBLE);
356 setResultIconAndText(0, 0);
357 }
358 }
359 } else if (view.getId() == R.id.account_password) {
360 ImageView iv = (ImageView) findViewById(R.id.viewPassword);
361 if (hasFocus) {
362 iv.setVisibility(View.VISIBLE);
363 } else {
364 TextView v = (TextView) findViewById(R.id.account_password);
365 int input_type = InputType.TYPE_CLASS_TEXT
366 | InputType.TYPE_TEXT_VARIATION_PASSWORD;
367 v.setInputType(input_type);
368 iv.setVisibility(View.INVISIBLE);
369 }
370 }
371 }
372
373 private void setResultIconAndText(int drawable_id, int text_id) {
374 ImageView iv = (ImageView) findViewById(R.id.action_indicator);
375 TextView tv = (TextView) findViewById(R.id.status_text);
376
377 if (drawable_id == 0 && text_id == 0) {
378 iv.setVisibility(View.INVISIBLE);
379 tv.setVisibility(View.INVISIBLE);
380 } else {
381 iv.setImageResource(drawable_id);
382 tv.setText(text_id);
383 iv.setVisibility(View.VISIBLE);
384 tv.setVisibility(View.VISIBLE);
385 }
386 }
387
388 @Override
389 public void onClick(View v) {
390 if (v.getId() == R.id.refreshButton) {
391 onFocusChange(findViewById(R.id.host_URL), false);
392 } else if (v.getId() == R.id.viewPassword) {
393 TextView view = (TextView) findViewById(R.id.account_password);
394 int input_type = InputType.TYPE_CLASS_TEXT
395 | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
396 view.setInputType(input_type);
397 }
398 }
399 }