+
+ /**
+ * 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.
+ *
+ * @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);
+ }
+
+ /**
+ * 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;
+ }
+ if (isChecked) {
+ ExpirationDatePickerDialogFragment dialog =
+ ExpirationDatePickerDialogFragment.newInstance(mFile, -1);
+ dialog.show(
+ getActivity().getSupportFragmentManager(),
+ ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
+ );
+
+ } 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
+ );
+ }
+ }
+ }
+
+
+ /**
+ * 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, false);
+ } else {
+ ((FileActivity) getActivity()).getFileOperationsHelper().
+ setPasswordToShareViaLink(mFile, ""); // "" clears
+ }
+
+ // 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, false);
+ }
+ }
+ }
+
+