Get users/group from Server, to fill in 'Share with' list
[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
26 import com.owncloud.android.MainApp;
27 import com.owncloud.android.datamodel.FileDataStorageManager;
28 import com.owncloud.android.datamodel.OCFile;
29 import com.owncloud.android.lib.common.OwnCloudAccount;
30 import com.owncloud.android.lib.common.OwnCloudClient;
31 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
32 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
33 import com.owncloud.android.lib.common.utils.Log_OC;
34 import com.owncloud.android.lib.resources.shares.OCShare;
35 import com.owncloud.android.operations.GetSharesForFileOperation;
36
37 import java.lang.ref.WeakReference;
38 import java.util.ArrayList;
39
40 /**
41 * Async Task to get the users and groups which a file is shared with
42 */
43 public class GetShareWithUsersAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
44
45 private final String TAG = GetShareWithUsersAsyncTask.class.getSimpleName();
46 private final WeakReference<OnGetSharesWithUsersTaskListener> mListener;
47 private ArrayList<OCShare> mShares;
48
49 public ArrayList<OCShare> getShares(){
50 return mShares;
51 }
52
53 public GetShareWithUsersAsyncTask(OnGetSharesWithUsersTaskListener listener) {
54 mListener = new WeakReference<OnGetSharesWithUsersTaskListener>(listener);
55 }
56
57 @Override
58 protected RemoteOperationResult doInBackground(Object... params) {
59
60 RemoteOperationResult result = null;
61
62 if (params != null && params.length == 3) {
63 OCFile file = (OCFile) params[0];
64 Account account = (Account) params[1];
65 FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
66
67 try {
68 // Get shares request
69 GetSharesForFileOperation operation =
70 new GetSharesForFileOperation(file.getRemotePath(), false, false);
71 OwnCloudAccount ocAccount = new OwnCloudAccount(account,
72 MainApp.getAppContext());
73 OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
74 getClientFor(ocAccount, MainApp.getAppContext());
75 result = operation.execute(client, fileDataStorageManager);
76
77 } catch (Exception e) {
78 result = new RemoteOperationResult(e);
79 Log_OC.e(TAG, "Exception while getting shares", e);
80 }
81 } else {
82 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
83 }
84
85 return result;
86 }
87
88 @Override
89 protected void onPostExecute(RemoteOperationResult result) {
90
91 if (result!= null)
92 {
93 OnGetSharesWithUsersTaskListener listener = mListener.get();
94 if (listener!= null)
95 {
96 listener.onGetDataShareWithFinish(result);
97 }
98 }
99 }
100
101 /*
102 * Interface to retrieve data from get shares task
103 */
104 public interface OnGetSharesWithUsersTaskListener{
105
106 void onGetDataShareWithFinish(RemoteOperationResult result);
107 }
108 }