Hide UI elements about public share when this is disabled in the server
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ShareFileFragment.java
index d9a9b44..18c8d08 100644 (file)
@@ -33,7 +33,9 @@ import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.CompoundButton;
 import android.widget.ImageView;
 import android.widget.Button;
 import android.widget.CompoundButton;
 import android.widget.ImageView;
+import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.ListView;
+import android.widget.ScrollView;
 import android.widget.Switch;
 import android.widget.TextView;
 import android.widget.Toast;
 import android.widget.Switch;
 import android.widget.TextView;
 import android.widget.Toast;
@@ -45,6 +47,7 @@ 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.lib.resources.shares.ShareType;
 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.lib.resources.status.OCCapability;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
 import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
 import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
@@ -52,6 +55,7 @@ import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
 import java.text.SimpleDateFormat;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
 import java.text.SimpleDateFormat;
+
 import java.util.ArrayList;
 import java.util.Date;
 
 import java.util.ArrayList;
 import java.util.Date;
 
@@ -73,10 +77,13 @@ public class ShareFileFragment extends Fragment
 
     private static final String TAG = ShareFileFragment.class.getSimpleName();
 
 
     private static final String TAG = ShareFileFragment.class.getSimpleName();
 
-    // the fragment initialization parameters
+    /** The fragment initialization parameters */
     private static final String ARG_FILE = "FILE";
     private static final String ARG_ACCOUNT = "ACCOUNT";
 
     private static final String ARG_FILE = "FILE";
     private static final String ARG_ACCOUNT = "ACCOUNT";
 
+//    /** Tag for dialog */
+//    private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
+
     /** File to share, received as a parameter in construction time */
     private OCFile mFile;
 
     /** File to share, received as a parameter in construction time */
     private OCFile mFile;
 
@@ -89,6 +96,9 @@ public class ShareFileFragment extends Fragment
     /** List of private shares bound to the file */
     private ArrayList<OCShare> mPrivateShares;
 
     /** List of private shares bound to the file */
     private ArrayList<OCShare> mPrivateShares;
 
+    /** Capabilities of the server */
+    private OCCapability mCapabilities;
+
     /** Adapter to show private shares */
     private ShareUserListAdapter mUserGroupsAdapter = null;
 
     /** Adapter to show private shares */
     private ShareUserListAdapter mUserGroupsAdapter = null;
 
@@ -108,6 +118,7 @@ public class ShareFileFragment extends Fragment
      */
     private OnExpirationDateInteractionListener mOnExpirationDateInteractionListener = null;
 
      */
     private OnExpirationDateInteractionListener mOnExpirationDateInteractionListener = null;
 
+
     /**
      * Public factory method to create new ShareFileFragment instances.
      *
     /**
      * Public factory method to create new ShareFileFragment instances.
      *
@@ -221,7 +232,6 @@ public class ShareFileFragment extends Fragment
         return view;
     }
 
         return view;
     }
 
-
     /**
      * Binds listener for user actions that start any update on a expiration date
      * for the public link to the views receiving the user events.
     /**
      * Binds listener for user actions that start any update on a expiration date
      * for the public link to the views receiving the user events.
@@ -379,6 +389,9 @@ public class ShareFileFragment extends Fragment
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
+        // Load known capabilities of the server from DB
+        refreshCapabilitiesFromDB();
+
         // Load data into the list of private shares
         refreshUsersOrGroupsListFromDB();
 
         // Load data into the list of private shares
         refreshUsersOrGroupsListFromDB();
 
@@ -403,6 +416,21 @@ public class ShareFileFragment extends Fragment
         mListener = null;
     }
 
         mListener = null;
     }
 
+
+    /**
+     * Get known server capabilities from DB
+     *
+     * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
+     * instance ready to use. If not ready, does nothing.
+     */
+    public void refreshCapabilitiesFromDB() {
+        if (((FileActivity)mListener).getStorageManager() != null) {
+            mCapabilities = ((FileActivity)mListener).getStorageManager().
+                    getCapability(mAccount.name);
+        }
+    }
+
+
     /**
      * Get users and groups from the DB to fill in the "share with" list.
      *
     /**
      * Get users and groups from the DB to fill in the "share with" list.
      *
@@ -440,11 +468,15 @@ public class ShareFileFragment extends Fragment
             noShares.setVisibility(View.GONE);
             usersList.setVisibility(View.VISIBLE);
             usersList.setAdapter(mUserGroupsAdapter);
             noShares.setVisibility(View.GONE);
             usersList.setVisibility(View.VISIBLE);
             usersList.setAdapter(mUserGroupsAdapter);
-
+            setListViewHeightBasedOnChildren(usersList);
         } else {
             noShares.setVisibility(View.VISIBLE);
             usersList.setVisibility(View.GONE);
         }
         } else {
             noShares.setVisibility(View.VISIBLE);
             usersList.setVisibility(View.GONE);
         }
+
+        // Set Scroll to initial position
+        ScrollView scrollView = (ScrollView) getView().findViewById(R.id.shareScroll);
+        scrollView.scrollTo(0, 0);
     }
 
     @Override
     }
 
     @Override
@@ -459,11 +491,16 @@ public class ShareFileFragment extends Fragment
     /**
      * Get public link from the DB to fill in the "Share link" section in the UI.
      *
     /**
      * Get public link from the DB to fill in the "Share link" section in the UI.
      *
+     * Takes into account server capabilities before reading database.
+     *
      * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
      * instance ready to use. If not ready, does nothing.
      */
     public void refreshPublicShareFromDB() {
      * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
      * instance ready to use. If not ready, does nothing.
      */
     public void refreshPublicShareFromDB() {
-        if (((FileActivity) mListener).getStorageManager() != null) {
+        if (isPublicShareDisabled()) {
+            hidePublicShare();
+
+        } else if (((FileActivity) mListener).getStorageManager() != null) {
             // Get public share
             mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType(
                     mFile.getRemotePath(),
             // Get public share
             mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType(
                     mFile.getRemotePath(),
@@ -477,6 +514,15 @@ public class ShareFileFragment extends Fragment
     }
 
     /**
     }
 
     /**
+     * @return  'True' when public share is disabled in the server
+     */
+    private boolean isPublicShareDisabled() {
+        return (mCapabilities != null &&
+                mCapabilities.getFilesSharingPublicEnabled().isFalse()
+        );
+    }
+
+    /**
      * Updates in the UI the section about public share with the information in the current
      * public share bound to mFile, if any
      */
      * Updates in the UI the section about public share with the information in the current
      * public share bound to mFile, if any
      */
@@ -494,9 +540,19 @@ public class ShareFileFragment extends Fragment
             }
             getExpirationDateSection().setVisibility(View.VISIBLE);
             getPasswordSection().setVisibility(View.VISIBLE);
             }
             getExpirationDateSection().setVisibility(View.VISIBLE);
             getPasswordSection().setVisibility(View.VISIBLE);
-            getGetLinkButton().setVisibility(View.VISIBLE);
+            // GetLink button
+            AppCompatButton getLinkButton = getGetLinkButton();
+            getLinkButton.setVisibility(View.VISIBLE);
+            getLinkButton.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    //GetLink from the server and show ShareLinkToDialog
+                    ((FileActivity) getActivity()).getFileOperationsHelper().
+                            getFileWithLink(mFile);
+
+                }
+            });
 
 
-            /// update state of expiration date switch and message depending on expiration date
             /// update state of expiration date switch and message depending on expiration date
             Switch expirationDateSwitch = getExpirationDateSwitch();
             // set null listener before setChecked() to prevent infinite loop of calls
             /// update state of expiration date switch and message depending on expiration date
             Switch expirationDateSwitch = getExpirationDateSwitch();
             // set null listener before setChecked() to prevent infinite loop of calls
@@ -591,9 +647,40 @@ public class ShareFileFragment extends Fragment
     }
 
     private AppCompatButton getGetLinkButton() {
     }
 
     private AppCompatButton getGetLinkButton() {
-        return (AppCompatButton) getView().findViewById(R.id.shareViewLinkGetLinkButton);
+        return (AppCompatButton) getView().findViewById(R.id.shareViaLinkGetLinkButton);
+    }
+
+    /**
+     * Hides all the UI elements related to public share
+     */
+    private void hidePublicShare() {
+        getShareViaLinkSwitch().setVisibility(View.GONE);
+        getExpirationDateSection().setVisibility(View.GONE);
+        getPasswordSection().setVisibility(View.GONE);
+        getGetLinkButton().setVisibility(View.GONE);
     }
 
     }
 
+    public static void setListViewHeightBasedOnChildren(ListView listView) {
+        ListAdapter listAdapter = listView.getAdapter();
+        if (listAdapter == null) {
+            return;
+        }
+        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
+        int totalHeight = 0;
+        View view = null;
+        for (int i = 0; i < listAdapter.getCount(); i++) {
+            view = listAdapter.getView(i, view, listView);
+            if (i == 0) {
+                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
+            }
+            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
+            totalHeight += view.getMeasuredHeight();
+        }
+        ViewGroup.LayoutParams params = listView.getLayoutParams();
+        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
+        listView.setLayoutParams(params);
+        listView.requestLayout();
+    }
 
     /**
      * This interface must be implemented by activities that contain this
 
     /**
      * This interface must be implemented by activities that contain this