AuthenticationRunnable.java 2.8 KB

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