new stuff for account authenticator
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / AuthenticationRunnable.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.authenticator;
20
21 import java.net.URL;
22
23 import org.apache.commons.httpclient.HttpStatus;
24
25 import eu.alefzero.webdav.WebdavClient;
26
27 import android.net.Uri;
28 import android.os.Handler;
29
30 public class AuthenticationRunnable implements Runnable {
31
32 private OnAuthenticationResultListener mListener;
33 private Handler mHandler;
34 private URL mUrl;
35 private String mUsername;
36 private String mPassword;
37
38 private static final String WEBDAV_2_0_PATH = "/files/webdav.php";
39
40 public AuthenticationRunnable(URL url, String username, String password) {
41 mListener = null;
42 mUrl = url;
43 mUsername = username;
44 mPassword = password;
45 }
46
47 public void setOnAuthenticationResultListener(OnAuthenticationResultListener listener, Handler handler) {
48 mListener = listener;
49 mHandler = handler;
50 }
51
52 @Override
53 public void run() {
54 Uri uri = Uri.parse(mUrl.toString() + WEBDAV_2_0_PATH);
55 WebdavClient client = new WebdavClient(uri);
56 client.setCredentials(mUsername, mPassword);
57 int login_result = client.tryToLogin();
58 switch (login_result) {
59 case HttpStatus.SC_OK:
60 postResult(true, uri.toString());
61 break;
62 case HttpStatus.SC_UNAUTHORIZED:
63 postResult(false, "Invalid login or/and password");
64 break;
65 case HttpStatus.SC_NOT_FOUND:
66 postResult(false, "Wrong path given");
67 break;
68 default:
69 postResult(false, "Internal server error, code: " + login_result);
70 }
71 }
72
73 private void postResult(final boolean success, final String message) {
74 if (mHandler != null) {
75 mHandler.post(new Runnable() {
76 @Override
77 public void run() {
78 AuthenticationRunnable.this.mListener.onAuthenticationResult(success, message);
79 }
80 });
81 }
82 }
83 }