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 c0626cc..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
@@ -23,7 +24,6 @@ package com.owncloud.android.ui.fragment;
 import android.accounts.Account;
 import android.app.Activity;
 import android.graphics.Bitmap;
-import android.net.Uri;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
@@ -31,25 +31,39 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageView;
+import android.widget.ListView;
 import android.widget.TextView;
+import android.widget.Toast;
 
 import com.owncloud.android.R;
+import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
+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.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 {
+public class ShareFileFragment extends Fragment
+        implements ShareUserListAdapter.ShareUserAdapterListener{
+
     private static final String TAG = ShareFileFragment.class.getSimpleName();
 
     // the fragment initialization parameters
@@ -60,16 +74,19 @@ public class ShareFileFragment extends Fragment {
     private OCFile mFile;
     private Account mAccount;
 
+    // other members
+    private ArrayList<OCShare> mShares;
+    private ShareUserListAdapter mUserGroupsAdapter = null;
     private OnShareFragmentInteractionListener mListener;
 
     /**
      * Public factory method to create new ShareFileFragment instances.
      *
-     * @param fileToShare   An {@link OCFile} to show in the fragment
-     * @param account       An ownCloud account
+     * @param fileToShare An {@link OCFile} to show in the fragment
+     * @param account     An ownCloud account
      * @return A new instance of fragment ShareFileFragment.
      */
-    public static ShareFileFragment  newInstance(OCFile fileToShare, Account account) {
+    public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
         ShareFileFragment fragment = new ShareFileFragment();
         Bundle args = new Bundle();
         args.putParcelable(ARG_FILE, fileToShare);
@@ -82,6 +99,9 @@ public class ShareFileFragment extends Fragment {
         // Required empty public constructor
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -91,6 +111,9 @@ public class ShareFileFragment extends Fragment {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
@@ -105,7 +128,7 @@ public class ShareFileFragment extends Fragment {
         if (mFile.isImage()) {
             String remoteId = String.valueOf(mFile.getRemoteId());
             Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
-            if (thumbnail != null){
+            if (thumbnail != null) {
                 icon.setImageBitmap(thumbnail);
             }
         }
@@ -114,7 +137,7 @@ public class ShareFileFragment extends Fragment {
         filename.setText(mFile.getFileName());
         // Size
         TextView size = (TextView) view.findViewById(R.id.shareFileSize);
-        if (mFile.isFolder()){
+        if (mFile.isFolder()) {
             size.setVisibility(View.GONE);
         } else {
             size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
@@ -126,19 +149,26 @@ 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();
+                }
             }
         });
 
         return view;
     }
 
-    // TODO: Rename method, update argument and hook method into UI event
-    public void onButtonPressed(Uri uri) {
-        if (mListener != null) {
-            mListener.onShareFragmentInteraction(uri);
-        }
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+
+        // Load data into the list
+        refreshUsersOrGroupsListFromDB();
     }
 
     @Override
@@ -158,7 +188,58 @@ public class ShareFileFragment extends Fragment {
         mListener = null;
     }
 
-    // TODO: review if it is necessary
+    /**
+     * 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 (){
+        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
+        // 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);
+        ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
+
+        if (mShares.size() > 0) {
+            noShares.setVisibility(View.GONE);
+            usersList.setVisibility(View.VISIBLE);
+            usersList.setAdapter(mUserGroupsAdapter);
+
+        } else {
+            noShares.setVisibility(View.VISIBLE);
+            usersList.setVisibility(View.GONE);
+        }
+    }
+
+    @Override
+    public void unshareButtonPressed(OCShare share) {
+        // Unshare
+        mListener.unshareWith(share);
+        Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
+    }
+
+
     /**
      * This interface must be implemented by activities that contain this
      * fragment to allow an interaction in this fragment to be communicated
@@ -170,9 +251,9 @@ public class ShareFileFragment extends Fragment {
      * >Communicating with Other Fragments</a> for more information.
      */
     public interface OnShareFragmentInteractionListener {
-        public void showSearchUsersAndGroups();
-
-        public void onShareFragmentInteraction(Uri uri);
+        void showSearchUsersAndGroups();
+        void refreshUsersOrGroupsListFromServer();
+        void unshareWith(OCShare share);
     }
 
 }