+
+ /**
+ * 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);
+ }
+ }
+
+