Redirect app to login screen when operations in file details view fail due to bad...
[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.R;
26 import com.owncloud.android.network.OwnCloudClientUtils;
27
28 import eu.alefzero.webdav.WebdavClient;
29
30 import android.content.Context;
31 import android.net.Uri;
32 import android.os.Handler;
33
34 public class AuthenticationRunnable implements Runnable {
35
36 private OnAuthenticationResultListener mListener;
37 private Handler mHandler;
38 private URL mUrl;
39 private String mUsername;
40 private String mPassword;
41 private Context mContext;
42
43 public AuthenticationRunnable(URL url, String username, String password, Context context) {
44 mListener = null;
45 mUrl = url;
46 mUsername = username;
47 mPassword = password;
48 mContext = context;
49 }
50
51 public void setOnAuthenticationResultListener(
52 OnAuthenticationResultListener listener, Handler handler) {
53 mListener = listener;
54 mHandler = handler;
55 }
56
57 @Override
58 public void run() {
59 Uri uri;
60 uri = Uri.parse(mUrl.toString());
61 WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(uri, mUsername, mPassword, mContext);
62 int login_result = wdc.tryToLogin();
63 switch (login_result) {
64 case HttpStatus.SC_OK:
65 postResult(true, uri.toString());
66 break;
67 case HttpStatus.SC_UNAUTHORIZED:
68 postResult(false, mContext.getString(R.string.auth_unauthorized));
69 break;
70 case HttpStatus.SC_NOT_FOUND:
71 postResult(false, mContext.getString(R.string.auth_not_found));
72 break;
73 default:
74 postResult(false, String.format(mContext.getString(R.string.auth_internal), login_result));
75 }
76 }
77
78 private void postResult(final boolean success, final String message) {
79 if (mHandler != null && mListener != null) {
80 mHandler.post(new Runnable() {
81 @Override
82 public void run() {
83 mListener.onAuthenticationResult(success, message);
84 }
85 });
86 }
87 }
88 }