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