Updating translation files...
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / AuthenticationRunnable.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.authenticator;
21
22 import java.net.URL;
23
24 import org.apache.commons.httpclient.HttpStatus;
25
26 import com.owncloud.android.R;
27 import com.owncloud.android.network.OwnCloudClientUtils;
28
29 import eu.alefzero.webdav.WebdavClient;
30
31 import android.content.Context;
32 import android.net.Uri;
33 import android.os.Handler;
34
35 public class AuthenticationRunnable implements Runnable {
36
37 private OnAuthenticationResultListener mListener;
38 private Handler mHandler;
39 private URL mUrl;
40 private String mUsername;
41 private String mPassword;
42 private Context mContext;
43
44 public AuthenticationRunnable(URL url, String username, String password, Context context) {
45 mListener = null;
46 mUrl = url;
47 mUsername = username;
48 mPassword = password;
49 mContext = context;
50 }
51
52 public void setOnAuthenticationResultListener(
53 OnAuthenticationResultListener listener, Handler handler) {
54 mListener = listener;
55 mHandler = handler;
56 }
57
58 @Override
59 public void run() {
60 Uri uri;
61 uri = Uri.parse(mUrl.toString());
62 WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(uri, mUsername, mPassword, mContext);
63 int login_result = wdc.tryToLogin();
64 switch (login_result) {
65 case HttpStatus.SC_OK:
66 postResult(true, uri.toString());
67 break;
68 case HttpStatus.SC_UNAUTHORIZED:
69 postResult(false, mContext.getString(R.string.auth_unauthorized));
70 break;
71 case HttpStatus.SC_NOT_FOUND:
72 postResult(false, mContext.getString(R.string.auth_not_found));
73 break;
74 default:
75 postResult(false, String.format(mContext.getString(R.string.auth_internal), login_result));
76 }
77 }
78
79 private void postResult(final boolean success, final String message) {
80 if (mHandler != null && mListener != null) {
81 mHandler.post(new Runnable() {
82 @Override
83 public void run() {
84 mListener.onAuthenticationResult(success, message);
85 }
86 });
87 }
88 }
89 }