Merge branch 'material_buttons' of https://github.com/owncloud/android into material_fab
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ShareFileFragment.java
index 81e7772..a029653 100644 (file)
@@ -2,6 +2,7 @@
  *   ownCloud Android client application
  *
  *   @author masensio
+ *   @author David A. Velasco
  *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
@@ -28,7 +29,6 @@ import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.AdapterView;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.ListView;
@@ -36,33 +36,33 @@ import android.widget.TextView;
 import android.widget.Toast;
 
 import com.owncloud.android.R;
-import com.owncloud.android.datamodel.FileDataStorageManager;
+import com.owncloud.android.authentication.AccountUtils;
 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.ui.activity.FileActivity;
 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.GetShareWithUsersAsyncTask;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
 import java.util.ArrayList;
 
 /**
- * Fragment for Sharing a file with users
+ * Fragment for Sharing a file with sharees (users or groups)
  *
  * A simple {@link Fragment} subclass.
+ *
  * Activities that contain this fragment must implement the
  * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
  * to handle interaction events.
+ *
  * Use the {@link ShareFileFragment#newInstance} factory method to
  * create an instance of this fragment.
  */
 public class ShareFileFragment extends Fragment
-        implements GetShareWithUsersAsyncTask.OnGetSharesWithUsersTaskListener {
+        implements ShareUserListAdapter.ShareUserAdapterListener{
 
     private static final String TAG = ShareFileFragment.class.getSimpleName();
 
@@ -74,13 +74,11 @@ public class ShareFileFragment extends Fragment
     private OCFile mFile;
     private Account mAccount;
 
+    // other members
     private ArrayList<OCShare> mShares;
     private ShareUserListAdapter mUserGroupsAdapter = null;
-
     private OnShareFragmentInteractionListener mListener;
 
-    private FileDataStorageManager mFileDataStorageManager;
-
     /**
      * Public factory method to create new ShareFileFragment instances.
      *
@@ -101,16 +99,21 @@ public class ShareFileFragment extends Fragment
         // Required empty public constructor
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if (getArguments() != null) {
             mFile = getArguments().getParcelable(ARG_FILE);
             mAccount = getArguments().getParcelable(ARG_ACCOUNT);
-            mFileDataStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
@@ -146,8 +149,14 @@ public class ShareFileFragment extends Fragment
         addUserGroupButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
-                // Show Search Fragment
-                mListener.showSearchUsersAndGroups();
+                 boolean shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
+                if (shareWithUsersEnable) {
+                    // Show Search Fragment
+                    mListener.showSearchUsersAndGroups();
+                } else {
+                    String message = getString(R.string.share_sharee_unavailable);
+                    Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
+                }
             }
         });
 
@@ -158,8 +167,8 @@ public class ShareFileFragment extends Fragment
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
-        // Load data to the list (start process with an Async Task)
-        refreshUsersOrGroupsListFromServer();
+        // Load data into the list
+        refreshUsersOrGroupsListFromDB();
     }
 
     @Override
@@ -180,21 +189,33 @@ public class ShareFileFragment extends Fragment
     }
 
     /**
-     * Get users and groups fromn the DB to fill in the "share with" list
+     * Get users and groups from the DB to fill in the "share with" list
+     *
+     * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
+     * instance ready to use. If not ready, does nothing.
      */
     public void refreshUsersOrGroupsListFromDB (){
-        // Get Users and Groups
-        mShares = mFileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(),
-                mAccount.name);
-
-        // Update list of users/groups
-        updateListOfUserGroups();
+        if (((FileActivity) mListener).getStorageManager() != null) {
+            // Get Users and Groups
+            mShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
+                    mFile.getRemotePath(),
+                    mAccount.name
+            );
+
+            // Update list of users/groups
+            updateListOfUserGroups();
+        }
     }
 
     private void updateListOfUserGroups() {
         // Update list of users/groups
-        mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
-                R.layout.share_user_item, mShares);
+        // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
+        mUserGroupsAdapter = new ShareUserListAdapter(
+                getActivity(),
+                R.layout.share_user_item,
+                mShares,
+                this
+        );
 
         // Show data
         TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
@@ -205,83 +226,20 @@ public class ShareFileFragment extends Fragment
             usersList.setVisibility(View.VISIBLE);
             usersList.setAdapter(mUserGroupsAdapter);
 
-            // Add unshare options
-            registerLongClickListener(usersList);
-
         } else {
             noShares.setVisibility(View.VISIBLE);
             usersList.setVisibility(View.GONE);
         }
     }
 
-    /**
-     * Get users and groups from the server to fill in the "share with" list
-     */
-    public void refreshUsersOrGroupsListFromServer(){
-        // Show loading
-        ((ShareActivity) getActivity()).showLoadingDialog(getString(R.string.common_loading));
-        // Get Users and Groups
-        GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
-        Object[] params = { mFile, mAccount, mFileDataStorageManager};
-        getTask.execute(params);
-    }
-
-    private void registerLongClickListener(final ListView listView) {
-        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
-            @Override
-            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(mShares.get(position));
-                            Log_OC.d(TAG, "Unshare - " +
-                                    mShares.get(position).getSharedWithDisplayName());
-                        }
-                    });
-
-                } else {
-                    unshareButton.setVisibility(View.GONE);
-                }
-                view.setAlpha(0);
-                view.animate().alpha(1).setDuration(500).start();
-                return false;
-            }
-        });
-    }
-
-    // Call to Unshare operation
-    private void unshareWith(OCShare share){
-        OCFile file = ((FileActivity) getActivity()).getFile();
-
-        ((FileActivity) getActivity()).getFileOperationsHelper().
-                unshareFileWithUserOrGroup(
-                        file, share.getShareType(), share.getShareWith()
-                );
-    }
-
     @Override
-    public void onGetDataShareWithFinish(RemoteOperationResult result) {
-        // Remove loading
-        ((ShareActivity) getActivity()).dismissLoadingDialog();
-        if (result != null && result.isSuccess()) {
-            Log_OC.d(TAG, "Get Data Share With finishes sucessfully");
-
-        } else {
-            Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show();
-        }
-
-        // Data is on Database
-        refreshUsersOrGroupsListFromDB();
+    public void unshareButtonPressed(OCShare share) {
+        // Unshare
+        mListener.unshareWith(share);
+        Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
     }
 
 
-    // TODO: review if it is necessary
     /**
      * This interface must be implemented by activities that contain this
      * fragment to allow an interaction in this fragment to be communicated
@@ -294,6 +252,8 @@ public class ShareFileFragment extends Fragment
      */
     public interface OnShareFragmentInteractionListener {
         void showSearchUsersAndGroups();
+        void refreshUsersOrGroupsListFromServer();
+        void unshareWith(OCShare share);
     }
 
 }