[tx-robot] updated from transifex
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / GetShareWithUsersAsyncTask.java
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 */
20
21 package com.owncloud.android.utils;
22
23 import android.accounts.Account;
24 import android.os.AsyncTask;
25 import android.util.Pair;
26
27 import com.owncloud.android.MainApp;
28 import com.owncloud.android.datamodel.FileDataStorageManager;
29 import com.owncloud.android.datamodel.OCFile;
30 import com.owncloud.android.lib.common.OwnCloudAccount;
31 import com.owncloud.android.lib.common.OwnCloudClient;
32 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
33 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
34 import com.owncloud.android.lib.common.operations.RemoteOperation;
35 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
36 import com.owncloud.android.lib.common.utils.Log_OC;
37 import com.owncloud.android.operations.GetSharesForFileOperation;
38
39 import java.lang.ref.WeakReference;
40
41 /**
42 * Async Task to get the users and groups which a file is shared with
43 */
44 public class GetShareWithUsersAsyncTask extends AsyncTask<Object, Void, Pair<RemoteOperation, RemoteOperationResult>> {
45
46 private final String TAG = GetShareWithUsersAsyncTask.class.getSimpleName();
47 private final WeakReference<OnRemoteOperationListener> mListener;
48
49 public GetShareWithUsersAsyncTask(OnRemoteOperationListener listener) {
50 mListener = new WeakReference<OnRemoteOperationListener>(listener);
51 }
52
53 @Override
54 protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
55
56 GetSharesForFileOperation operation = null;
57 RemoteOperationResult result = null;
58
59 if (params != null && params.length == 3) {
60 OCFile file = (OCFile) params[0];
61 Account account = (Account) params[1];
62 FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
63
64 try {
65 // Get shares request
66 operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
67 OwnCloudAccount ocAccount = new OwnCloudAccount(account,
68 MainApp.getAppContext());
69 OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
70 getClientFor(ocAccount, MainApp.getAppContext());
71 result = operation.execute(client, fileDataStorageManager);
72
73 } catch (Exception e) {
74 result = new RemoteOperationResult(e);
75 Log_OC.e(TAG, "Exception while getting shares", e);
76 }
77 } else {
78 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
79 }
80
81 return new Pair(operation, result);
82 }
83
84 @Override
85 protected void onPostExecute(Pair<RemoteOperation, RemoteOperationResult> result) {
86
87 if (result!= null)
88 {
89 OnRemoteOperationListener listener = mListener.get();
90 if (listener!= null)
91 {
92 listener.onRemoteOperationFinish(result.first, result.second);
93 }
94 }
95 }
96
97 }