Add unshare with users/groups, from Share wiht view
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / UnshareWithUserAsyncTask.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.lib.resources.shares.RemoveRemoteShareOperation;
36 import com.owncloud.android.operations.GetSharesForFileOperation;
37
38 import java.lang.ref.WeakReference;
39 import java.util.ArrayList;
40
41 /**
42 * Async Task to delete a share
43 */
44 public class UnshareWithUserAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
45
46 private final String TAG = UnshareWithUserAsyncTask.class.getSimpleName();
47 private final WeakReference<OnUnshareWithUserTaskListener> mListener;
48
49 public UnshareWithUserAsyncTask(OnUnshareWithUserTaskListener listener) {
50 mListener = new WeakReference<OnUnshareWithUserTaskListener>(listener);
51 }
52
53 @Override
54 protected RemoteOperationResult doInBackground(Object... params) {
55
56 RemoteOperationResult result = null;
57
58 if (params != null && params.length == 3) {
59 int shareId = (int) params[0];
60 Account account = (Account) params[1];
61 FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
62
63 try {
64 // Get shares request
65 RemoveRemoteShareOperation operation =
66 new RemoveRemoteShareOperation(shareId);
67 OwnCloudAccount ocAccount = new OwnCloudAccount(account,
68 MainApp.getAppContext());
69 OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
70 getClientFor(ocAccount, MainApp.getAppContext());
71 result = operation.execute(client);
72
73 } catch (Exception e) {
74 result = new RemoteOperationResult(e);
75 Log_OC.e(TAG, "Exception while unshare", e);
76 }
77 } else {
78 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
79 }
80
81 return result;
82 }
83
84 @Override
85 protected void onPostExecute(RemoteOperationResult result) {
86
87 if (result!= null)
88 {
89 OnUnshareWithUserTaskListener listener = mListener.get();
90 if (listener!= null)
91 {
92 listener.onUnshareWithFinish(result);
93 }
94 }
95 }
96
97 /*
98 * Interface to retrieve the result
99 */
100 public interface OnUnshareWithUserTaskListener {
101
102 void onUnshareWithFinish(RemoteOperationResult result);
103 }
104 }