1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.authenticator
;
24 import org
.apache
.commons
.httpclient
.HttpStatus
;
26 import com
.owncloud
.android
.R
;
27 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
29 import eu
.alefzero
.webdav
.WebdavClient
;
31 import android
.content
.Context
;
32 import android
.net
.Uri
;
33 import android
.os
.Handler
;
35 public class AuthenticationRunnable
implements Runnable
{
37 private OnAuthenticationResultListener mListener
;
38 private Handler mHandler
;
40 private String mUsername
;
41 private String mPassword
;
42 private Context mContext
;
44 public AuthenticationRunnable(URL url
, String username
, String password
, Context context
) {
52 public void setOnAuthenticationResultListener(
53 OnAuthenticationResultListener listener
, Handler handler
) {
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());
68 case HttpStatus
.SC_UNAUTHORIZED
:
69 postResult(false
, mContext
.getString(R
.string
.auth_unauthorized
));
71 case HttpStatus
.SC_NOT_FOUND
:
72 postResult(false
, mContext
.getString(R
.string
.auth_not_found
));
75 postResult(false
, String
.format(mContext
.getString(R
.string
.auth_internal
), login_result
));
79 private void postResult(final boolean success
, final String message
) {
80 if (mHandler
!= null
&& mListener
!= null
) {
81 mHandler
.post(new Runnable() {
84 mListener
.onAuthenticationResult(success
, message
);