77490ebab692c06e7cda9995bedf4acbefba037b
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / 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 eu.alefzero.owncloud.ui.activity;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23
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;
54
55 /**
56 * This Activity is used to add an ownCloud account to the App
57 *
58 * @author Bartek Przybylski
59 *
60 */
61 public class AuthenticatorActivity extends AccountAuthenticatorActivity
62 implements OnAuthenticationResultListener, OnConnectCheckListener,
63 OnFocusChangeListener, OnClickListener {
64 private static final int DIALOG_LOGIN_PROGRESS = 0;
65
66 private static final String TAG = "AuthActivity";
67
68 private Thread mAuthThread;
69 private AuthenticationRunnable mAuthRunnable;
70 private ConnectionCheckerRunnable mConnChkRunnable;
71 private final Handler mHandler = new Handler();
72 private String mBaseUrl;
73
74 private static final String STATUS_TEXT = "STATUS_TEXT";
75 private static final String STATUS_ICON = "STATUS_ICON";
76 private static final String STATUS_CORRECT = "STATUS_CORRECT";
77 private static final String IS_SSL_CONN = "IS_SSL_CONN";
78 private int mStatusText, mStatusIcon;
79 private boolean mStatusCorrect, mIsSslConn;
80
81 public static final String PARAM_USERNAME = "param_Username";
82 public static final String PARAM_HOSTNAME = "param_Hostname";
83
84 @Override
85 protected void onCreate(Bundle savedInstanceState) {
86 super.onCreate(savedInstanceState);
87 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
88 setContentView(R.layout.account_setup);
89 ImageView iv = (ImageView) findViewById(R.id.refreshButton);
90 ImageView iv2 = (ImageView) findViewById(R.id.viewPassword);
91 TextView tv = (TextView) findViewById(R.id.host_URL);
92 TextView tv2 = (TextView) findViewById(R.id.account_password);
93
94 if (savedInstanceState != null) {
95 mStatusIcon = savedInstanceState.getInt(STATUS_ICON);
96 mStatusText = savedInstanceState.getInt(STATUS_TEXT);
97 mStatusCorrect = savedInstanceState.getBoolean(STATUS_CORRECT);
98 mIsSslConn = savedInstanceState.getBoolean(IS_SSL_CONN);
99 setResultIconAndText(mStatusIcon, mStatusText);
100 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
101 if (!mStatusCorrect)
102 iv.setVisibility(View.VISIBLE);
103 else
104 iv.setVisibility(View.INVISIBLE);
105
106 } else {
107 mStatusText = mStatusIcon = 0;
108 mStatusCorrect = false;
109 mIsSslConn = false;
110 }
111 iv.setOnClickListener(this);
112 iv2.setOnClickListener(this);
113 tv.setOnFocusChangeListener(this);
114 tv2.setOnFocusChangeListener(this);
115 }
116
117 @Override
118 protected void onSaveInstanceState(Bundle outState) {
119 outState.putInt(STATUS_ICON, mStatusIcon);
120 outState.putInt(STATUS_TEXT, mStatusText);
121 outState.putBoolean(STATUS_CORRECT, mStatusCorrect);
122 super.onSaveInstanceState(outState);
123 }
124
125 @Override
126 protected Dialog onCreateDialog(int id) {
127 Dialog dialog = null;
128 switch (id) {
129 case DIALOG_LOGIN_PROGRESS: {
130 ProgressDialog working_dialog = new ProgressDialog(this);
131 working_dialog.setMessage(getResources().getString(
132 R.string.auth_trying_to_login));
133 working_dialog.setIndeterminate(true);
134 working_dialog.setCancelable(true);
135 working_dialog
136 .setOnCancelListener(new DialogInterface.OnCancelListener() {
137 @Override
138 public void onCancel(DialogInterface dialog) {
139 Log.i(TAG, "Login canceled");
140 if (mAuthThread != null) {
141 mAuthThread.interrupt();
142 finish();
143 }
144 }
145 });
146 dialog = working_dialog;
147 break;
148 }
149 default:
150 Log.e(TAG, "Incorrect dialog called with id = " + id);
151 }
152 return dialog;
153 }
154
155 public void onAuthenticationResult(boolean success, String message) {
156 if (success) {
157 TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);
158
159 URL url;
160 try {
161 url = new URL(message);
162 } catch (MalformedURLException e) {
163 // should never happen
164 Log.e(getClass().getName(), "Malformed URL: " + message);
165 return;
166 }
167
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()
174 .toString(), null);
175
176 // Add this account as default in the preferences, if there is none
177 // already
178 Account defaultAccount = AccountUtils
179 .getCurrentOwnCloudAccount(this);
180 if (defaultAccount == null) {
181 SharedPreferences.Editor editor = PreferenceManager
182 .getDefaultSharedPreferences(this).edit();
183 editor.putString("select_oc_account", accountName);
184 editor.commit();
185 }
186
187 final Intent intent = new Intent();
188 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,
189 AccountAuthenticator.ACCOUNT_TYPE);
190 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
191 intent.putExtra(AccountManager.KEY_AUTHTOKEN,
192 AccountAuthenticator.ACCOUNT_TYPE);
193 intent.putExtra(AccountManager.KEY_USERDATA, username);
194
195 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,
196 url.toString());
197 accManager.setUserData(account,
198 AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable
199 .getDiscoveredVersion().toString());
200 accManager.setUserData(account,
201 AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);
202
203 setAccountAuthenticatorResult(intent.getExtras());
204 setResult(RESULT_OK, intent);
205 Bundle bundle = new Bundle();
206 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
207 getContentResolver().startSync(ProviderTableMeta.CONTENT_URI,
208 bundle);
209
210 /*
211 * if
212 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
213 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
214 * ExtensionsAvailableActivity.class); startActivity(i); }
215 */
216
217 finish();
218 } else {
219 dismissDialog(DIALOG_LOGIN_PROGRESS);
220 TextView tv = (TextView) findViewById(R.id.account_username);
221 tv.setError(message);
222 }
223 }
224
225 public void onOkClick(View view) {
226 String prefix = "";
227 String url = ((TextView) findViewById(R.id.host_URL)).getText()
228 .toString();
229 if (mIsSslConn) {
230 prefix = "https://";
231 } else {
232 prefix = "http://";
233 }
234 if (url.toLowerCase().startsWith("http://")
235 || url.toLowerCase().startsWith("https://")) {
236 prefix = "";
237 }
238 continueConnection(prefix);
239 }
240
241 private void continueConnection(String prefix) {
242 String url = ((TextView) findViewById(R.id.host_URL)).getText()
243 .toString();
244 String username = ((TextView) findViewById(R.id.account_username))
245 .getText().toString();
246 String password = ((TextView) findViewById(R.id.account_password))
247 .getText().toString();
248 if (url.endsWith("/"))
249 url = url.substring(0, url.length() - 1);
250
251 URL uri = null;
252 String webdav_path = AccountUtils.getWebdavPath(mConnChkRunnable
253 .getDiscoveredVersion());
254
255 try {
256 mBaseUrl = prefix + url;
257 String url_str = prefix + url + webdav_path;
258 uri = new URL(url_str);
259 } catch (MalformedURLException e) {
260 // should not happend
261 e.printStackTrace();
262 }
263
264 showDialog(DIALOG_LOGIN_PROGRESS);
265 mAuthRunnable = new AuthenticationRunnable(uri, username, password);
266 mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);
267 mAuthThread = new Thread(mAuthRunnable);
268 mAuthThread.start();
269 }
270
271 @Override
272 public void onConnectionCheckResult(ResultType type) {
273 mStatusText = mStatusIcon = 0;
274 mStatusCorrect = false;
275 String t_url = ((TextView) findViewById(R.id.host_URL)).getText()
276 .toString().toLowerCase();
277
278 switch (type) {
279 case OK:
280 // ugly as hell
281 if (t_url.startsWith("http://") || t_url.startsWith("https://")) {
282 mIsSslConn = t_url.startsWith("http://") ? false : true;
283 mStatusIcon = R.drawable.ic_ok;
284 mStatusText = R.string.auth_connection_established;
285 mStatusCorrect = true;
286 } else {
287 mIsSslConn = true;
288 mStatusIcon = android.R.drawable.ic_secure;
289 mStatusText = R.string.auth_secure_connection;
290 mStatusCorrect = true;
291 }
292 break;
293 case OK_NO_SSL:
294 mStatusIcon = android.R.drawable.ic_secure;
295 mStatusText = R.string.auth_nossl_plain_ok_title;
296 mStatusCorrect = true;
297 mIsSslConn = false;
298 break;
299 case TIMEOUT:
300 case INORRECT_ADDRESS:
301 case SSL_INIT_ERROR:
302 case HOST_NOT_AVAILABLE:
303 mStatusIcon = R.drawable.common_error;
304 mStatusText = R.string.auth_unknow_host_title;
305 break;
306 case NO_NETWORK_CONNECTION:
307 mStatusIcon = R.drawable.no_network;
308 mStatusText = R.string.auth_no_net_conn_title;
309 break;
310 case INSTANCE_NOT_CONFIGURED:
311 mStatusIcon = R.drawable.common_error;
312 mStatusText = R.string.auth_not_configured_title;
313 break;
314 case UNKNOWN_ERROR:
315 mStatusIcon = R.drawable.common_error;
316 mStatusText = R.string.auth_unknow_error;
317 break;
318 case FILE_NOT_FOUND:
319 mStatusIcon = R.drawable.common_error;
320 mStatusText = R.string.auth_incorrect_path_title;
321 break;
322 default:
323 Log.e(TAG, "Incorrect connection checker result type: " + type);
324 }
325 setResultIconAndText(mStatusIcon, mStatusText);
326 if (!mStatusCorrect)
327 findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
328 else
329 findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
330 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
331 }
332
333 @Override
334 public void onFocusChange(View view, boolean hasFocus) {
335 if (view.getId() == R.id.host_URL) {
336 if (!hasFocus) {
337 TextView tv = ((TextView) findViewById(R.id.host_URL));
338 String uri = tv.getText().toString();
339 if (uri.length() != 0) {
340 setResultIconAndText(R.drawable.progress_small,
341 R.string.auth_testing_connection);
342 mConnChkRunnable = new ConnectionCheckerRunnable(uri, this);
343 mConnChkRunnable.setListener(this, mHandler);
344 mAuthThread = new Thread(mConnChkRunnable);
345 mAuthThread.start();
346 } else {
347 findViewById(R.id.refreshButton).setVisibility(
348 View.INVISIBLE);
349 setResultIconAndText(0, 0);
350 }
351 }
352 } else if (view.getId() == R.id.account_password) {
353 ImageView iv = (ImageView) findViewById(R.id.viewPassword);
354 if (hasFocus) {
355 iv.setVisibility(View.VISIBLE);
356 } else {
357 TextView v = (TextView) findViewById(R.id.account_password);
358 int input_type = InputType.TYPE_CLASS_TEXT
359 | InputType.TYPE_TEXT_VARIATION_PASSWORD;
360 v.setInputType(input_type);
361 iv.setVisibility(View.INVISIBLE);
362 }
363 }
364 }
365
366 private void setResultIconAndText(int drawable_id, int text_id) {
367 ImageView iv = (ImageView) findViewById(R.id.action_indicator);
368 TextView tv = (TextView) findViewById(R.id.status_text);
369
370 if (drawable_id == 0 && text_id == 0) {
371 iv.setVisibility(View.INVISIBLE);
372 tv.setVisibility(View.INVISIBLE);
373 } else {
374 iv.setImageResource(drawable_id);
375 tv.setText(text_id);
376 iv.setVisibility(View.VISIBLE);
377 tv.setVisibility(View.VISIBLE);
378 }
379 }
380
381 @Override
382 public void onClick(View v) {
383 if (v.getId() == R.id.refreshButton) {
384 onFocusChange(findViewById(R.id.host_URL), false);
385 } else if (v.getId() == R.id.viewPassword) {
386 TextView view = (TextView) findViewById(R.id.account_password);
387 int input_type = InputType.TYPE_CLASS_TEXT
388 | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
389 view.setInputType(input_type);
390 }
391 }
392 }