+
+ /**
+ * 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.
+ *
+ * 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
+ mPrivateShares = ((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,
+ mPrivateShares,
+ this
+ );
+
+ // Show data
+ TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
+ ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
+
+ if (mPrivateShares.size() > 0) {
+ 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
+ public void unshareButtonPressed(OCShare share) {
+ // Unshare
+ mListener.unshareWith(share);
+ Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
+ }
+
+
+
+ /**
+ * 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 (isPublicShareDisabled()) {
+ hidePublicShare();
+
+ } else if (((FileActivity) mListener).getStorageManager() != null) {
+ // Get public share
+ mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType(
+ mFile.getRemotePath(),
+ ShareType.PUBLIC_LINK,
+ ""
+ );
+
+ // Update public share section
+ updatePublicShareSection();
+ }
+ }
+
+ /**
+ * @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
+ */
+ private void updatePublicShareSection() {
+ if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
+ /// public share bound -> expand section
+ Switch shareViaLinkSwitch = getShareViaLinkSwitch();
+ if (!shareViaLinkSwitch.isChecked()) {
+ // set null listener before setChecked() to prevent infinite loop of calls
+ shareViaLinkSwitch.setOnCheckedChangeListener(null);
+ shareViaLinkSwitch.setChecked(true);
+ shareViaLinkSwitch.setOnCheckedChangeListener(
+ mOnShareViaLinkSwitchCheckedChangeListener
+ );
+ }
+ getExpirationDateSection().setVisibility(View.VISIBLE);
+ getPasswordSection().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
+ Switch expirationDateSwitch = getExpirationDateSwitch();
+ // set null listener before setChecked() to prevent infinite loop of calls
+ expirationDateSwitch.setOnCheckedChangeListener(null);
+ long expirationDate = mPublicShare.getExpirationDate();
+ if (expirationDate > 0) {
+ if (!expirationDateSwitch.isChecked()) {
+ expirationDateSwitch.toggle();
+ }
+ String formattedDate =
+ SimpleDateFormat.getDateInstance().format(
+ new Date(expirationDate)
+ );
+ getExpirationDateValue().setText(formattedDate);
+ } else {
+ if (expirationDateSwitch.isChecked()) {
+ expirationDateSwitch.toggle();
+ }
+ getExpirationDateValue().setText(R.string.empty);
+ }
+ // recover listener
+ expirationDateSwitch.setOnCheckedChangeListener(
+ mOnExpirationDateInteractionListener
+ );
+
+ /// update state of password switch and message depending on password protection
+ Switch passwordSwitch = getPasswordSwitch();
+ // set null listener before setChecked() to prevent infinite loop of calls
+ passwordSwitch.setOnCheckedChangeListener(null);
+ if (mPublicShare.isPasswordProtected()) {
+ if (!passwordSwitch.isChecked()) {
+ passwordSwitch.toggle();
+ }
+ getPasswordValue().setVisibility(View.VISIBLE);
+ } else {
+ if (passwordSwitch.isChecked()) {
+ passwordSwitch.toggle();
+ }
+ getPasswordValue().setVisibility(View.INVISIBLE);
+ }
+ // recover listener
+ passwordSwitch.setOnCheckedChangeListener(
+ mOnPasswordInteractionListener
+ );
+
+
+ } else {
+ /// no public share -> collapse section
+ Switch shareViaLinkSwitch = getShareViaLinkSwitch();
+ if (shareViaLinkSwitch.isChecked()) {
+ shareViaLinkSwitch.setOnCheckedChangeListener(null);
+ getShareViaLinkSwitch().setChecked(false);
+ shareViaLinkSwitch.setOnCheckedChangeListener(
+ mOnShareViaLinkSwitchCheckedChangeListener
+ );
+ }
+ getExpirationDateSection().setVisibility(View.GONE);
+ getPasswordSection().setVisibility(View.GONE);
+ getGetLinkButton().setVisibility(View.GONE);
+ }
+ }
+
+
+ /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
+
+ private Switch getShareViaLinkSwitch() {
+ return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch);
+ }
+
+ private View getExpirationDateSection() {
+ return getView().findViewById(R.id.shareViaLinkExpirationSection);
+ }
+
+ private Switch getExpirationDateSwitch() {
+ return (Switch) getView().findViewById(R.id.shareViaLinkExpirationSwitch);
+ }
+
+ private TextView getExpirationDateValue() {
+ return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue);
+ }
+
+ private View getPasswordSection() {
+ return getView().findViewById(R.id.shareViaLinkPasswordSection);
+ }
+
+ private Switch getPasswordSwitch() {
+ return (Switch) getView().findViewById(R.id.shareViaLinkPasswordSwitch);
+ }
+
+ private TextView getPasswordValue() {
+ return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue);
+ }
+
+ private AppCompatButton getGetLinkButton() {
+ 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();
+ }
+