GetShareWithUsersAsyncTask.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.utils;
  20. import android.accounts.Account;
  21. import android.os.AsyncTask;
  22. import android.util.Pair;
  23. import com.owncloud.android.MainApp;
  24. import com.owncloud.android.datamodel.FileDataStorageManager;
  25. import com.owncloud.android.datamodel.OCFile;
  26. import com.owncloud.android.lib.common.OwnCloudAccount;
  27. import com.owncloud.android.lib.common.OwnCloudClient;
  28. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  29. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  30. import com.owncloud.android.lib.common.operations.RemoteOperation;
  31. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. import com.owncloud.android.operations.GetSharesForFileOperation;
  34. import java.lang.ref.WeakReference;
  35. /**
  36. * Async Task to get the users and groups which a file is shared with
  37. */
  38. public class GetShareWithUsersAsyncTask extends AsyncTask<Object, Void, Pair<RemoteOperation, RemoteOperationResult>> {
  39. private final String TAG = GetShareWithUsersAsyncTask.class.getSimpleName();
  40. private final WeakReference<OnRemoteOperationListener> mListener;
  41. public GetShareWithUsersAsyncTask(OnRemoteOperationListener listener) {
  42. mListener = new WeakReference<OnRemoteOperationListener>(listener);
  43. }
  44. @Override
  45. protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
  46. GetSharesForFileOperation operation = null;
  47. RemoteOperationResult result = null;
  48. if (params != null && params.length == 3) {
  49. OCFile file = (OCFile) params[0];
  50. Account account = (Account) params[1];
  51. FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
  52. try {
  53. // Get shares request
  54. operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
  55. OwnCloudAccount ocAccount = new OwnCloudAccount(
  56. account,
  57. MainApp.getAppContext()
  58. );
  59. OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
  60. getClientFor(ocAccount, MainApp.getAppContext());
  61. result = operation.execute(client, fileDataStorageManager);
  62. } catch (Exception e) {
  63. result = new RemoteOperationResult(e);
  64. Log_OC.e(TAG, "Exception while getting shares", e);
  65. }
  66. } else {
  67. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
  68. }
  69. return new Pair(operation, result);
  70. }
  71. @Override
  72. protected void onPostExecute(Pair<RemoteOperation, RemoteOperationResult> result) {
  73. if (result != null) {
  74. OnRemoteOperationListener listener = mListener.get();
  75. if (listener != null) {
  76. listener.onRemoteOperationFinish(result.first, result.second);
  77. }
  78. }
  79. }
  80. }