Browse Source

Add unshare with users/groups, from Share wiht view

masensio 9 years ago
parent
commit
1bdd672295

+ 2 - 15
src/com/owncloud/android/ui/adapter/ShareUserListAdapter.java

@@ -29,7 +29,6 @@ import android.widget.ImageView;
 import android.widget.TextView;
 
 import com.owncloud.android.R;
-import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.resources.shares.ShareType;
 
@@ -43,8 +42,6 @@ public class ShareUserListAdapter extends ArrayAdapter {
     private Context mContext;
     private ArrayList<OCShare> mShares;
 
-    private ImageView mUnshareButton;
-
     public ShareUserListAdapter(Context context, int resource, ArrayList<OCShare>shares) {
         super(context, resource);
         mContext= context;
@@ -82,18 +79,8 @@ public class ShareUserListAdapter extends ArrayAdapter {
             }
             userName.setText(name);
 
-            mUnshareButton = (ImageView) view.findViewById(R.id.unshareButton);
-            mUnshareButton.setVisibility(View.GONE);
-            mUnshareButton.setOnClickListener(new View.OnClickListener() {
-                @Override
-                public void onClick(View v) {
-                    // TODO: Unshare
-
-                    Log_OC.d("TAG - ShareUserListAdapter", "TODO Unshare - " +
-                            mShares.get(position).getSharedWithDisplayName());
-                }
-            });
-
+            ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
+            unshareButton.setVisibility(View.GONE);
 
         }
         return view;

+ 40 - 4
src/com/owncloud/android/ui/fragment/ShareFileFragment.java

@@ -41,13 +41,13 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.shares.OCShare;
-import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.ui.activity.ShareActivity;
 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
 import com.owncloud.android.utils.DisplayUtils;
-import com.owncloud.android.utils.GetShareWithUserAsyncTask;
 import com.owncloud.android.utils.MimetypeIconUtil;
+import com.owncloud.android.utils.UnshareWithUserAsyncTask;
 
 import java.util.ArrayList;
 
@@ -61,7 +61,8 @@ import java.util.ArrayList;
  * Use the {@link ShareFileFragment#newInstance} factory method to
  * create an instance of this fragment.
  */
-public class ShareFileFragment extends Fragment {
+public class ShareFileFragment extends Fragment
+        implements UnshareWithUserAsyncTask.OnUnshareWithUserTaskListener{
 
     private static final String TAG = ShareFileFragment.class.getSimpleName();
 
@@ -226,12 +227,22 @@ public class ShareFileFragment extends Fragment {
     private void registerLongClickListener(final ListView listView) {
         listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
             @Override
-            public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
+            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
                                            long id) {
                 // Show unshare button
                 ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
                 if (unshareButton.getVisibility() == View.GONE) {
                     unshareButton.setVisibility(View.VISIBLE);
+                    unshareButton.setOnClickListener(new View.OnClickListener() {
+                        @Override
+                        public void onClick(View v) {
+                            // Unshare
+                            unshareWith((int)(mShares.get(position).getIdRemoteShared()));
+                            Log_OC.d(TAG, "Unshare - " +
+                                    mShares.get(position).getSharedWithDisplayName());
+                        }
+                    });
+
                 } else {
                     unshareButton.setVisibility(View.GONE);
                 }
@@ -242,6 +253,31 @@ public class ShareFileFragment extends Fragment {
         });
     }
 
+    private void unshareWith(int shareId){
+        ( (ShareActivity) getActivity()).showWaitingLoadDialog();
+        // Remove Share with id
+        UnshareWithUserAsyncTask unshareTask = new UnshareWithUserAsyncTask(this);
+        FileDataStorageManager fileDataStorageManager =
+                new FileDataStorageManager(mAccount, getActivity().getContentResolver());
+        Object[] params = { shareId, mAccount, fileDataStorageManager};
+        unshareTask.execute(params);
+    }
+
+    @Override
+    public void onUnshareWithFinish(RemoteOperationResult result) {
+        // Remove loading
+        ((ShareActivity) getActivity()).dismissWaitingLoadDialog();
+
+        if (result != null && result.isSuccess()) {
+            // Refresh data
+            //TODO: Refresh file or delete the user from the list
+            updateListOfUserGroups();
+
+        } else {
+            Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show();
+        }
+    }
+
     // TODO: review if it is necessary
     /**
      * This interface must be implemented by activities that contain this

+ 15 - 19
src/com/owncloud/android/utils/UnshareWithUserAsyncTask.java

@@ -32,26 +32,22 @@ import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation;
 import com.owncloud.android.operations.GetSharesForFileOperation;
 
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 
 /**
- * Async Task to get the users and groups which a file is shared with
+ * Async Task to delete a share
  */
 public class UnshareWithUserAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
 
     private final String TAG = UnshareWithUserAsyncTask.class.getSimpleName();
-    private final WeakReference<OnGetSharesWithUserTaskListener> mListener;
-    private ArrayList<OCShare> mShares;
+    private final WeakReference<OnUnshareWithUserTaskListener> mListener;
 
-    public ArrayList<OCShare> getShares(){
-        return mShares;
-    }
-
-    public UnshareWithUserAsyncTask(OnGetSharesWithUserTaskListener listener) {
-        mListener = new WeakReference<OnGetSharesWithUserTaskListener>(listener);
+    public UnshareWithUserAsyncTask(OnUnshareWithUserTaskListener listener) {
+        mListener = new WeakReference<OnUnshareWithUserTaskListener>(listener);
     }
 
     @Override
@@ -60,23 +56,23 @@ public class UnshareWithUserAsyncTask extends AsyncTask<Object, Void, RemoteOper
         RemoteOperationResult result = null;
 
         if (params != null && params.length == 3) {
-            OCFile file = (OCFile) params[0];
+            int shareId = (int) params[0];
             Account account = (Account) params[1];
             FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
 
             try {
                 // Get shares request
-                GetSharesForFileOperation operation =
-                        new GetSharesForFileOperation(file.getRemotePath(), false, false);
+                RemoveRemoteShareOperation operation =
+                        new RemoveRemoteShareOperation(shareId);
                 OwnCloudAccount ocAccount = new OwnCloudAccount(account,
                         MainApp.getAppContext());
                 OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
                         getClientFor(ocAccount, MainApp.getAppContext());
-                result = operation.execute(client, fileDataStorageManager);
+                result = operation.execute(client);
 
             } catch (Exception e) {
                 result = new RemoteOperationResult(e);
-                Log_OC.e(TAG, "Exception while getting shares", e);
+                Log_OC.e(TAG, "Exception while unshare", e);
             }
         } else {
             result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
@@ -90,19 +86,19 @@ public class UnshareWithUserAsyncTask extends AsyncTask<Object, Void, RemoteOper
 
         if (result!= null)
         {
-            OnGetSharesWithUserTaskListener listener = mListener.get();
+            OnUnshareWithUserTaskListener listener = mListener.get();
             if (listener!= null)
             {
-                listener.onGetDataShareWithFinish(result);
+                listener.onUnshareWithFinish(result);
             }
         }
     }
 
     /*
-     * Interface to retrieve data from get shares task
+     * Interface to retrieve the result
      */
-    public interface OnGetSharesWithUserTaskListener{
+    public interface OnUnshareWithUserTaskListener {
 
-        void onGetDataShareWithFinish(RemoteOperationResult result);
+        void onUnshareWithFinish(RemoteOperationResult result);
     }
 }