X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/ad7666370f248124a6c49846f78cd3cef71f19f0..030eaffcfc52c5a71821a01ded865beade50e314:/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 838635ae..e7a76cb8 100644 --- a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java +++ b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java @@ -23,8 +23,10 @@ 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; @@ -33,7 +35,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; @@ -42,6 +46,7 @@ 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; @@ -52,6 +57,7 @@ import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.MimetypeIconUtil; import java.text.SimpleDateFormat; + import java.util.ArrayList; import java.util.Date; @@ -73,10 +79,13 @@ public class ShareFileFragment extends Fragment 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"; +// /** 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; @@ -99,15 +108,14 @@ public class ShareFileFragment extends Fragment private CompoundButton.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener; /** - * Listener for changes on switch to set / clear password on public link + * Listener for user actions to set, update or clear password on public link */ - private CompoundButton.OnCheckedChangeListener mOnPasswordSwitchCheckedChangeListener; + private OnPasswordInteractionListener mOnPasswordInteractionListener = null; /** - * Listener for changes on switch to set / clear expiration date on public link + * Listener for user actions to set, update or clear expiration date on public link */ - private CompoundButton.OnCheckedChangeListener mOnExpirationDateSwitchCheckedChangeListener; - + private OnExpirationDateInteractionListener mOnExpirationDateInteractionListener = null; /** * Public factory method to create new ShareFileFragment instances. @@ -141,6 +149,7 @@ public class ShareFileFragment extends Fragment } } + /** * {@inheritDoc} */ @@ -212,66 +221,169 @@ public class ShareFileFragment extends Fragment Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch); shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); - // Switch for expiration date - mOnExpirationDateSwitchCheckedChangeListener = 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) { - ExpirationDatePickerDialogFragment dialog = - ExpirationDatePickerDialogFragment.newInstance(mFile); - dialog.show( - getActivity().getSupportFragmentManager(), - ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG - ); + // Set listener for user actions on expiration date + initExpirationListener(view); - } else { - ((FileActivity) getActivity()).getFileOperationsHelper(). - setExpirationDateToShareViaLink(mFile, -1, -1, -1); - } + // Set listener for user actions on password + initPasswordListener(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. + * + * @param shareView Root view in the fragment. + */ + private void initExpirationListener(View shareView) { + mOnExpirationDateInteractionListener = new OnExpirationDateInteractionListener(); + + ((Switch) shareView.findViewById(R.id.shareViaLinkExpirationSwitch)). + setOnCheckedChangeListener(mOnExpirationDateInteractionListener); + + shareView.findViewById(R.id.shareViaLinkExpirationLabel). + setOnClickListener(mOnExpirationDateInteractionListener); + + shareView.findViewById(R.id.shareViaLinkExpirationValue). + setOnClickListener(mOnExpirationDateInteractionListener); + } - // undo the toggle to grant the view will be correct if the dialog is cancelled - buttonView.setOnCheckedChangeListener(null); - buttonView.toggle(); - buttonView.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener); + /** + * Listener for user actions that start any update on the expiration date for the public link. + */ + private class OnExpirationDateInteractionListener + implements CompoundButton.OnCheckedChangeListener, View.OnClickListener { + + /** + * Called by R.id.shareViaLinkExpirationSwitch to set or clear the expiration date. + * + * @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkExpirationSwitch + * @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; } - }; - Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch); - shareViaLinkExpirationSwitch.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener); + if (isChecked) { + ExpirationDatePickerDialogFragment dialog = + ExpirationDatePickerDialogFragment.newInstance(mFile, -1); + dialog.show( + getActivity().getSupportFragmentManager(), + ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG + ); - // Switch for password - mOnPasswordSwitchCheckedChangeListener = 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(). - requestPasswordForShareViaLink(mFile); - } else { - ((FileActivity) getActivity()).getFileOperationsHelper(). - setPasswordToShareViaLink(mFile, ""); // "" clears + } else { + ((FileActivity) getActivity()).getFileOperationsHelper(). + setExpirationDateToShareViaLink(mFile, -1); + } + + // undo the toggle to grant the view will be correct if the dialog is cancelled + switchView.setOnCheckedChangeListener(null); + switchView.toggle(); + switchView.setOnCheckedChangeListener(mOnExpirationDateInteractionListener); + } + + /** + * Called by R.id.shareViaLinkExpirationLabel or R.id.shareViaLinkExpirationValue + * to change the current expiration date. + * + * @param expirationView Label or value view touched by the user. + */ + @Override + public void onClick(View expirationView) { + if (mPublicShare != null && mPublicShare.getExpirationDate() > 0) { + long chosenDateInMillis = -1; + if (mPublicShare != null) { + chosenDateInMillis = mPublicShare.getExpirationDate(); } + ExpirationDatePickerDialogFragment dialog = + ExpirationDatePickerDialogFragment.newInstance( + mFile, + chosenDateInMillis + ); + dialog.show( + getActivity().getSupportFragmentManager(), + ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG + ); + } + } + } + - // undo the toggle to grant the view will be correct if the dialog is cancelled - buttonView.setOnCheckedChangeListener(null); - buttonView.toggle(); - buttonView.setOnCheckedChangeListener(mOnPasswordSwitchCheckedChangeListener); + /** + * Binds listener for user actions that start any update on a password for the public link + * to the views receiving the user events. + * + * @param shareView Root view in the fragment. + */ + private void initPasswordListener(View shareView) { + mOnPasswordInteractionListener = new OnPasswordInteractionListener(); + + ((Switch) shareView.findViewById(R.id.shareViaLinkPasswordSwitch)). + setOnCheckedChangeListener(mOnPasswordInteractionListener); + + shareView.findViewById(R.id.shareViaLinkPasswordLabel). + setOnClickListener(mOnPasswordInteractionListener); + + shareView.findViewById(R.id.shareViaLinkPasswordValue). + setOnClickListener(mOnPasswordInteractionListener); + } + + + /** + * Listener for user actions that start any update on a password for the public link. + */ + private class OnPasswordInteractionListener + implements CompoundButton.OnCheckedChangeListener, View.OnClickListener { + + /** + * Called by R.id.shareViaLinkPasswordSwitch to set or clear the password. + * + * @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkPasswordSwitch + * @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) { + ((FileActivity) getActivity()).getFileOperationsHelper(). + requestPasswordForShareViaLink(mFile); + } else { + ((FileActivity) getActivity()).getFileOperationsHelper(). + setPasswordToShareViaLink(mFile, ""); // "" clears } - }; - Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch); - shareViaLinkPasswordSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); - return view; + // undo the toggle to grant the view will be correct if the dialog is cancelled + switchView.setOnCheckedChangeListener(null); + switchView.toggle(); + switchView.setOnCheckedChangeListener(mOnPasswordInteractionListener); + } + + /** + * Called by R.id.shareViaLinkPasswordLabel or R.id.shareViaLinkPasswordValue + * to change the current password. + * + * @param passwordView Label or value view touched by the user. + */ + @Override + public void onClick(View passwordView) { + if (mPublicShare != null && mPublicShare.isPasswordProtected()) { + ((FileActivity) getActivity()).getFileOperationsHelper(). + requestPasswordForShareViaLink(mFile); + } + } } + @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); @@ -337,11 +449,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 @@ -391,7 +507,18 @@ public class ShareFileFragment extends Fragment } 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 @@ -416,7 +543,7 @@ public class ShareFileFragment extends Fragment } // recover listener expirationDateSwitch.setOnCheckedChangeListener( - mOnExpirationDateSwitchCheckedChangeListener + mOnExpirationDateInteractionListener ); /// update state of password switch and message depending on password protection @@ -436,7 +563,7 @@ public class ShareFileFragment extends Fragment } // recover listener passwordSwitch.setOnCheckedChangeListener( - mOnPasswordSwitchCheckedChangeListener + mOnPasswordInteractionListener ); @@ -491,7 +618,28 @@ public class ShareFileFragment extends Fragment return (AppCompatButton) getView().findViewById(R.id.shareViewLinkGetLinkButton); } - + 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 * fragment to allow an interaction in this fragment to be communicated