Modify current password on touch in password label or value
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ShareFileFragment.java
index 2ed5d0b..b468340 100644 (file)
@@ -23,6 +23,7 @@ package com.owncloud.android.ui.fragment;
 
 import android.accounts.Account;
 import android.app.Activity;
 
 import android.accounts.Account;
 import android.app.Activity;
+import android.content.DialogInterface;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
@@ -47,10 +48,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.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 com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.Date;
 
 /**
  * Fragment for Sharing a file with sharees (users or groups) or creating
 
 /**
  * Fragment for Sharing a file with sharees (users or groups) or creating
@@ -95,8 +99,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 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 user actions to set, update or clear password on public link
+     */
+    //private CompoundButton.OnCheckedChangeListener mOnPasswordSwitchCheckedChangeListener;
+    private OnPasswordInteractionListener mOnPasswordInteractionListener;
+
+    /**
+     * Listener for changes on switch to set / clear expiration date on public link
+     */
+    private CompoundButton.OnCheckedChangeListener mOnExpirationDateSwitchCheckedChangeListener;
 
 
     /**
 
 
     /**
@@ -131,6 +143,7 @@ public class ShareFileFragment extends Fragment
         }
     }
 
         }
     }
 
+
     /**
      * {@inheritDoc}
      */
     /**
      * {@inheritDoc}
      */
@@ -203,8 +216,7 @@ public class ShareFileFragment extends Fragment
         shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
 
         // Switch for expiration date
         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()) {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 if (!isResumed()) {
@@ -213,42 +225,87 @@ public class ShareFileFragment extends Fragment
                     return;
                 }
                 if (isChecked) {
                     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 {
 
                 } else {
-                    // TODO real implementation: update share without expiration date
-                    // empty value
-                    getExpirationDateValue().setText(R.string.empty);
-                }
-            }
-        });
-
-        // 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().
                     ((FileActivity) getActivity()).getFileOperationsHelper().
-                            setPasswordToShareViaLink(mFile, "");   // "" clears
+                            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 shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
-        shareViaLinkPasswordSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
+        Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
+        shareViaLinkExpirationSwitch.setOnCheckedChangeListener(mOnExpirationDateSwitchCheckedChangeListener);
+
+        // Set listener for user actions on password
+        initPasswordListener(view);
 
         return view;
     }
 
 
         return view;
     }
 
+    /**
+     * 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 shareViaLinkPasswordSwitch = (Switch) shareView.findViewById(R.id.shareViaLinkPasswordSwitch);
+        shareViaLinkPasswordSwitch.setOnCheckedChangeListener(mOnPasswordInteractionListener);
+        TextView shareViaLinkPasswordLabel = (TextView) shareView.findViewById(R.id.shareViaLinkPasswordLabel);
+        shareViaLinkPasswordLabel.setOnClickListener(mOnPasswordInteractionListener);
+        TextView shareViaLinkPasswordValue = (TextView) shareView.findViewById(R.id.shareViaLinkPasswordValue);
+        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 {
+
+        @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
+            }
+
+            // undo the toggle to grant the view will be correct if the dialog is cancelled
+            buttonView.setOnCheckedChangeListener(null);
+            buttonView.toggle();
+            buttonView.setOnCheckedChangeListener(mOnPasswordInteractionListener);
+        }
+
+        @Override
+        public void onClick(View v) {
+            if (mPublicShare != null && mPublicShare.isPasswordProtected()) {
+                ((FileActivity) getActivity()).getFileOperationsHelper().
+                        requestPasswordForShareViaLink(mFile);
+            }
+        }
+    }
+
+
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
@@ -278,7 +335,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.
      *
      * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
      * instance ready to use. If not ready, does nothing.
@@ -331,7 +388,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.
      *
      * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
      * instance ready to use. If not ready, does nothing.
@@ -345,7 +402,7 @@ public class ShareFileFragment extends Fragment
                     ""
             );
 
                     ""
             );
 
-            // Update list of users/groups
+            // Update public share section
             updatePublicShareSection();
         }
     }
             updatePublicShareSection();
         }
     }
@@ -370,6 +427,32 @@ public class ShareFileFragment extends Fragment
             getPasswordSection().setVisibility(View.VISIBLE);
             getGetLinkButton().setVisibility(View.VISIBLE);
 
             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
             /// 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
@@ -387,7 +470,7 @@ public class ShareFileFragment extends Fragment
             }
             // recover listener
             passwordSwitch.setOnCheckedChangeListener(
             }
             // recover listener
             passwordSwitch.setOnCheckedChangeListener(
-                    mOnPasswordSwitchCheckedChangeListener
+                    mOnPasswordInteractionListener
             );
 
 
             );
 
 
@@ -407,6 +490,7 @@ public class ShareFileFragment extends Fragment
         }
     }
 
         }
     }
 
+
     /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
 
     private Switch getShareViaLinkSwitch() {
     /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
 
     private Switch getShareViaLinkSwitch() {
@@ -417,6 +501,10 @@ public class ShareFileFragment extends Fragment
         return getView().findViewById(R.id.shareViaLinkExpirationSection);
     }
 
         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 TextView getExpirationDateValue() {
         return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue);
     }