X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/68550e9977754ebd2cf13eb45627f5841767c4ba..a99a28cc61d48eca63803c3d661ea1be14f99aec:/src/com/owncloud/android/ui/fragment/ShareFileFragment.java diff --git a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java index f30eaf2b..46382a65 100644 --- a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java +++ b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java @@ -23,10 +23,8 @@ package com.owncloud.android.ui.fragment; import android.accounts.Account; import android.app.Activity; -import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; -import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v7.widget.AppCompatButton; import android.view.LayoutInflater; @@ -35,7 +33,9 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.CompoundButton; import android.widget.ImageView; +import android.widget.ListAdapter; import android.widget.ListView; +import android.widget.ScrollView; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; @@ -44,10 +44,10 @@ 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.files.FileOperationsHelper; 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; @@ -96,6 +96,9 @@ public class ShareFileFragment extends Fragment /** List of private shares bound to the file */ private ArrayList mPrivateShares; + /** Capabilities of the server */ + private OCCapability mCapabilities; + /** Adapter to show private shares */ private ShareUserListAdapter mUserGroupsAdapter = null; @@ -115,6 +118,7 @@ public class ShareFileFragment extends Fragment */ private OnExpirationDateInteractionListener mOnExpirationDateInteractionListener = null; + /** * Public factory method to create new ShareFileFragment instances. * @@ -141,6 +145,7 @@ public class ShareFileFragment extends Fragment @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Log_OC.d(TAG, "onCreate"); if (getArguments() != null) { mFile = getArguments().getParcelable(ARG_FILE); mAccount = getArguments().getParcelable(ARG_ACCOUNT); @@ -154,6 +159,8 @@ public class ShareFileFragment extends Fragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + Log_OC.d(TAG, "onCreateView"); + // Inflate the layout for this fragment View view = inflater.inflate(R.layout.share_file_layout, container, false); @@ -200,24 +207,12 @@ public class ShareFileFragment extends Fragment // Switch to create public share mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (!isResumed()) { - // very important, setCheched(...) is called automatically during - // Fragment recreation on device rotations - return; - } - if (isChecked) { - ((FileActivity) getActivity()).getFileOperationsHelper(). - shareFileViaLink(mFile); - - } else { - ((FileActivity) getActivity()).getFileOperationsHelper(). - unshareFileViaLink(mFile); - } + public void onCheckedChanged(CompoundButton switchView, boolean isChecked) { } }; - Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch); - shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); + + // Set listener for user actions on switch for sharing/unsharing via link + initShareViaLinkListener(view); // Set listener for user actions on expiration date initExpirationListener(view); @@ -230,6 +225,67 @@ public class ShareFileFragment extends Fragment /** + * Binds listener for user actions to create or delete a public share + * to the views receiving the user events. + * + * @param shareView Root view in the fragment. + */ + private void initShareViaLinkListener(View shareView) { + mOnShareViaLinkSwitchCheckedChangeListener = new OnShareViaLinkListener(); + Switch shareViaLinkSwitch = (Switch) shareView.findViewById(R.id.shareViaLinkSectionSwitch); + shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); + } + + /** + * Listener for user actions that create or delete a public share. + */ + private class OnShareViaLinkListener + implements CompoundButton.OnCheckedChangeListener { + + /** + * Called by R.id.shareViaLinkSectionSwitch to create or delete a public link. + * + * @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkSectionSwitch + * @param isChecked New switch state. + */ + @Override + public void onCheckedChanged(CompoundButton switchView, boolean isChecked) { + if (!isResumed()) { + // very important, setCheched(...) is called automatically during + // Fragment recreation on device rotations + return; + } + if (isChecked) { + if (mCapabilities != null && + mCapabilities.getFilesSharingPublicPasswordEnforced().isTrue()) { + // password enforced by server, request to the user before trying to create + ((FileActivity) getActivity()).getFileOperationsHelper(). + requestPasswordForShareViaLink(mFile, true); + + } else { + // create without password if not enforced by server or we don't know if enforced; + ((FileActivity) getActivity()).getFileOperationsHelper(). + shareFileViaLink(mFile, null); + + // FileActivtiy#onCreateShareViaLinkOperationFinish still handles the guess of enforcement + // for server in versions previous to OwnCloudVersion#MINIMUM_VERSION_CAPABILITIES_API + } + + } else { + ((FileActivity) getActivity()).getFileOperationsHelper(). + unshareFileViaLink(mFile); + } + + // undo the toggle to grant the view will be correct if any intermediate dialog is cancelled or + // the create/delete operation fails + switchView.setOnCheckedChangeListener(null); + switchView.toggle(); + switchView.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); + } + } + + + /** * Binds listener for user actions that start any update on a expiration date * for the public link to the views receiving the user events. * @@ -354,7 +410,7 @@ public class ShareFileFragment extends Fragment } if (isChecked) { ((FileActivity) getActivity()).getFileOperationsHelper(). - requestPasswordForShareViaLink(mFile); + requestPasswordForShareViaLink(mFile, false); } else { ((FileActivity) getActivity()).getFileOperationsHelper(). setPasswordToShareViaLink(mFile, ""); // "" clears @@ -376,7 +432,7 @@ public class ShareFileFragment extends Fragment public void onClick(View passwordView) { if (mPublicShare != null && mPublicShare.isPasswordProtected()) { ((FileActivity) getActivity()).getFileOperationsHelper(). - requestPasswordForShareViaLink(mFile); + requestPasswordForShareViaLink(mFile, false); } } } @@ -385,6 +441,10 @@ public class ShareFileFragment extends Fragment @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); + Log_OC.d(TAG, "onActivityCreated"); + + // Load known capabilities of the server from DB + refreshCapabilitiesFromDB(); // Load data into the list of private shares refreshUsersOrGroupsListFromDB(); @@ -410,6 +470,21 @@ public class ShareFileFragment extends Fragment 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. * @@ -447,11 +522,15 @@ public class ShareFileFragment extends Fragment noShares.setVisibility(View.GONE); usersList.setVisibility(View.VISIBLE); usersList.setAdapter(mUserGroupsAdapter); - + setListViewHeightBasedOnChildren(usersList); } 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 @@ -466,11 +545,16 @@ public class ShareFileFragment extends Fragment /** * 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() { - if (((FileActivity) mListener).getStorageManager() != null) { + if (isPublicShareDisabled()) { + hidePublicShare(); + + } else if (((FileActivity) mListener).getStorageManager() != null) { // Get public share mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType( mFile.getRemotePath(), @@ -484,6 +568,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 */ @@ -515,7 +608,6 @@ public class ShareFileFragment extends Fragment }); /// 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 expirationDateSwitch.setOnCheckedChangeListener(null); @@ -609,7 +701,39 @@ public class ShareFileFragment extends Fragment } 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(); } /**