Uploading files are protected against removal from other apps
[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.utils.OwnCloudClientUtils;
26
27 import eu.alefzero.webdav.WebdavClient;
28
29 import android.net.Uri;
30 import android.os.Handler;
31
32 public class AuthenticationRunnable implements Runnable {
33
34 private OnAuthenticationResultListener mListener;
35 private Handler mHandler;
36 private URL mUrl;
37 private String mUsername;
38 private String mPassword;
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(
48 OnAuthenticationResultListener listener, Handler handler) {
49 mListener = listener;
50 mHandler = handler;
51 }
52
53 @Override
54 public void run() {
55 Uri uri;
56 uri = Uri.parse(mUrl.toString());
57 WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(uri, mUsername, mPassword);
58 int login_result = wdc.tryToLogin();
59 switch (login_result) {
60 case HttpStatus.SC_OK:
61 postResult(true, uri.toString());
62 break;
63 case HttpStatus.SC_UNAUTHORIZED:
64 postResult(false, "Invalid login or/and password");
65 break;
66 case HttpStatus.SC_NOT_FOUND:
67 postResult(false, "Wrong path given");
68 break;
69 default:
70 postResult(false, "Internal server error, code: " + login_result);
71 }
72 }
73
74 private void postResult(final boolean success, final String message) {
75 if (mHandler != null && mListener != null) {
76 mHandler.post(new Runnable() {
77 @Override
78 public void run() {
79 mListener.onAuthenticationResult(success, message);
80 }
81 });
82 }
83 }
84 }