AuthenticatorAsyncTask.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2015 ownCloud Inc.
  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 version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.authentication;
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.net.Uri;
  21. import android.os.AsyncTask;
  22. import com.owncloud.android.lib.common.OwnCloudClient;
  23. import com.owncloud.android.lib.common.OwnCloudClientFactory;
  24. import com.owncloud.android.lib.common.OwnCloudCredentials;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  26. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  27. import java.lang.ref.WeakReference;
  28. /**
  29. * Async Task to verify the credentials of a user
  30. *
  31. * @author masensio on 09/02/2015.
  32. */
  33. public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
  34. private static String REMOTE_PATH = "/";
  35. private static boolean SUCCESS_IF_ABSENT = false;
  36. private Context mContext;
  37. private final WeakReference<OnAuthenticatorTaskListener> mListener;
  38. protected Activity mActivity;
  39. public AuthenticatorAsyncTask(Activity activity) {
  40. mContext = activity.getApplicationContext();
  41. mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
  42. }
  43. @Override
  44. protected RemoteOperationResult doInBackground(Object... params) {
  45. RemoteOperationResult result;
  46. if (params!= null && params.length==2) {
  47. String url = (String)params[0];
  48. OwnCloudCredentials credentials = (OwnCloudCredentials)params[1];
  49. // Client
  50. Uri uri = Uri.parse(url);
  51. OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
  52. client.setCredentials(credentials);
  53. // Operation
  54. ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(
  55. REMOTE_PATH,
  56. mContext,
  57. SUCCESS_IF_ABSENT
  58. );
  59. result = operation.execute(client);
  60. } else {
  61. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
  62. }
  63. return result;
  64. }
  65. @Override
  66. protected void onPostExecute(RemoteOperationResult result) {
  67. if (result!= null)
  68. {
  69. OnAuthenticatorTaskListener listener = mListener.get();
  70. if (listener!= null)
  71. {
  72. listener.onAuthenticatorTaskCallback(result);
  73. }
  74. }
  75. }
  76. /*
  77. * Interface to retrieve data from recognition task
  78. */
  79. public interface OnAuthenticatorTaskListener{
  80. void onAuthenticatorTaskCallback(RemoteOperationResult result);
  81. }
  82. }