Set or clear expiration date to protect public link in ShareFileFragment
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ShareFileFragment.java
index db1e98d..838635a 100644 (file)
@@ -47,10 +47,13 @@ import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
+import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
 
 /**
  * Fragment for Sharing a file with sharees (users or groups) or creating
@@ -95,6 +98,16 @@ public class ShareFileFragment extends Fragment
     /** Listener for changes on switch to share / unshare publicly */
     private CompoundButton.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener;
 
+    /**
+     * Listener for changes on switch to set / clear password on public link
+     */
+    private CompoundButton.OnCheckedChangeListener mOnPasswordSwitchCheckedChangeListener;
+
+    /**
+     * Listener for changes on switch to set / clear expiration date on public link
+     */
+    private CompoundButton.OnCheckedChangeListener mOnExpirationDateSwitchCheckedChangeListener;
+
 
     /**
      * Public factory method to create new ShareFileFragment instances.
@@ -181,56 +194,80 @@ public class ShareFileFragment extends Fragment
         mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-                if (isResumed()) {
-                    if (isChecked) {
-                        ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(mFile);
-
-                    } else {
-                        ((FileActivity) getActivity()).getFileOperationsHelper().unshareFileViaLink(mFile);
-                    }
-                } // else, nothing; very important, setCheched(...) is called automatically during Fragment
-                  // recreation on device rotations
+                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);
+                }
             }
         };
         Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch);
         shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
 
         // Switch for expiration date
-        Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
-        shareViaLinkExpirationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+        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) {
-                    // TODO real implementation: update share with expiration date
-                    // show value of expiration date
-                    getExpirationDateValue().setText(R.string.placeholder_timestamp);
+                    ExpirationDatePickerDialogFragment dialog =
+                            ExpirationDatePickerDialogFragment.newInstance(mFile);
+                    dialog.show(
+                            getActivity().getSupportFragmentManager(),
+                            ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
+                    );
 
                 } else {
-                    // TODO real implementation: update share without expiration date
-                    // empty value
-                    getExpirationDateValue().setText(R.string.empty);
+                    ((FileActivity) getActivity()).getFileOperationsHelper().
+                            setExpirationDateToShareViaLink(mFile, -1, -1, -1);
                 }
+
+                // undo the toggle to grant the view will be correct if the dialog is cancelled
+                buttonView.setOnCheckedChangeListener(null);
+                buttonView.toggle();
+                buttonView.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener);
             }
-        });
+        };
+        Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
+        shareViaLinkExpirationSwitch.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener);
 
         // Switch for password
-        Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
-        shareViaLinkPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+        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) {
-                    // TODO real implementation: update share with password
-                    // show
-                    getExpirationPasswordValue().setVisibility(View.VISIBLE);
-
+                    ((FileActivity) getActivity()).getFileOperationsHelper().
+                            requestPasswordForShareViaLink(mFile);
                 } else {
-                    // TODO real implementation: update share without password
-                    // empty value
-                    getExpirationPasswordValue().setVisibility(View.INVISIBLE);
+                    ((FileActivity) getActivity()).getFileOperationsHelper().
+                            setPasswordToShareViaLink(mFile, "");   // "" clears
                 }
-            }
-        });
 
+                // undo the toggle to grant the view will be correct if the dialog is cancelled
+                buttonView.setOnCheckedChangeListener(null);
+                buttonView.toggle();
+                buttonView.setOnCheckedChangeListener(mOnPasswordSwitchCheckedChangeListener);
+            }
+        };
+        Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
+        shareViaLinkPasswordSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
 
         return view;
     }
@@ -264,7 +301,7 @@ public class ShareFileFragment extends Fragment
     }
 
     /**
-     * Get users and groups from the DB to fill in the "share with" list
+     * 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.
@@ -317,7 +354,7 @@ public class ShareFileFragment extends Fragment
 
 
     /**
-     * Get public link from the DB to fill in the "Share link" section
+     * Get public link from the DB to fill in the "Share link" section in the UI.
      *
      * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
      * instance ready to use. If not ready, does nothing.
@@ -331,7 +368,7 @@ public class ShareFileFragment extends Fragment
                     ""
             );
 
-            // Update list of users/groups
+            // Update public share section
             updatePublicShareSection();
         }
     }
@@ -342,12 +379,12 @@ public class ShareFileFragment extends Fragment
      */
     private void updatePublicShareSection() {
         if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
-            // public share bound -> expand section
+            /// 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);
-                getShareViaLinkSwitch().setChecked(true);
+                shareViaLinkSwitch.setChecked(true);
                 shareViaLinkSwitch.setOnCheckedChangeListener(
                         mOnShareViaLinkSwitchCheckedChangeListener
                 );
@@ -356,8 +393,55 @@ public class ShareFileFragment extends Fragment
             getPasswordSection().setVisibility(View.VISIBLE);
             getGetLinkButton().setVisibility(View.VISIBLE);
 
+            /// 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);
+            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(
+                    mOnExpirationDateSwitchCheckedChangeListener
+            );
+
+            /// 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(
+                    mOnPasswordSwitchCheckedChangeListener
+            );
+
+
         } else {
-            // no public share -> collapse section
+            /// no public share -> collapse section
             Switch shareViaLinkSwitch = getShareViaLinkSwitch();
             if (shareViaLinkSwitch.isChecked()) {
                 shareViaLinkSwitch.setOnCheckedChangeListener(null);
@@ -372,6 +456,9 @@ public class ShareFileFragment extends Fragment
         }
     }
 
+
+    /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
+
     private Switch getShareViaLinkSwitch() {
         return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch);
     }
@@ -380,6 +467,10 @@ public class ShareFileFragment extends Fragment
         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);
     }
@@ -388,7 +479,11 @@ public class ShareFileFragment extends Fragment
         return getView().findViewById(R.id.shareViaLinkPasswordSection);
     }
 
-    private TextView getExpirationPasswordValue() {
+    private Switch getPasswordSwitch() {
+        return (Switch) getView().findViewById(R.id.shareViaLinkPasswordSwitch);
+    }
+
+    private TextView getPasswordValue() {
         return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue);
     }