Browse Source

add the ability to set expiration date on shares not just links

AndyScherzinger 6 years ago
parent
commit
5f64479d30

+ 18 - 1
src/main/java/com/owncloud/android/ui/adapter/UserListAdapter.java

@@ -21,9 +21,11 @@
 package com.owncloud.android.ui.adapter;
 
 import android.accounts.Account;
+import android.app.Activity;
 import android.content.Context;
 import android.graphics.drawable.Drawable;
 import android.support.annotation.NonNull;
+import android.support.v4.app.FragmentManager;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -43,6 +45,8 @@ import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.status.OCCapability;
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
 import com.owncloud.android.ui.TextDrawable;
+import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
+import com.owncloud.android.ui.fragment.util.FileDetailSharingFragmentHelper;
 import com.owncloud.android.utils.DisplayUtils;
 
 import java.io.UnsupportedEncodingException;
@@ -56,6 +60,7 @@ public class UserListAdapter extends ArrayAdapter implements DisplayUtils.Avatar
 
     private ShareeListAdapterListener listener;
     private OCCapability capabilities;
+    private FragmentManager fragmentManager;
     private Context context;
     private ArrayList<OCShare> shares;
     private float avatarRadiusDimension;
@@ -63,10 +68,11 @@ public class UserListAdapter extends ArrayAdapter implements DisplayUtils.Avatar
     private OCFile file;
     private FileDataStorageManager storageManager;
 
-    public UserListAdapter(Context context, int resource, ArrayList<OCShare> shares,
+    public UserListAdapter(FragmentManager fragmentManager, Context context, int resource, ArrayList<OCShare> shares,
                            Account account, OCFile file, ShareeListAdapterListener listener) {
         super(context, resource);
         this.context = context;
+        this.fragmentManager = fragmentManager;
         this.shares = shares;
         this.listener = listener;
         this.account = account;
@@ -185,6 +191,9 @@ public class UserListAdapter extends ArrayAdapter implements DisplayUtils.Avatar
             editChangeItem.setVisible(false);
             editDeleteItem.setVisible(false);
         }
+
+        FileDetailSharingFragmentHelper.setupExpirationDateMenuItem(
+                menu.findItem(R.id.action_expiration_date), share.getExpirationDate(), context.getResources());
     }
 
     private boolean optionsItemSelected(Menu menu, MenuItem item, OCShare share) {
@@ -220,6 +229,14 @@ public class UserListAdapter extends ArrayAdapter implements DisplayUtils.Avatar
                 notifyDataSetChanged();
                 return true;
             }
+            case R.id.action_expiration_date: {
+                ExpirationDatePickerDialogFragment dialog = ExpirationDatePickerDialogFragment.newInstance(share, -1);
+                dialog.show(
+                        fragmentManager,
+                        ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
+                );
+                return true;
+            }
             default:
                 return true;
         }

+ 37 - 4
src/main/java/com/owncloud/android/ui/dialog/ExpirationDatePickerDialogFragment.java

@@ -31,6 +31,7 @@ import android.widget.DatePicker;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.ui.activity.FileActivity;
 
 import java.util.Calendar;
@@ -48,12 +49,18 @@ public class ExpirationDatePickerDialogFragment
     /** Parameter constant for {@link OCFile} instance to set the expiration date */
     private static final String ARG_FILE = "FILE";
 
+    /** Parameter constant for {@link OCShare} instance to set the expiration date */
+    private static final String ARG_SHARE = "SHARE";
+
     /** Parameter constant for date chosen initially */
     private static final String ARG_CHOSEN_DATE_IN_MILLIS = "CHOSEN_DATE_IN_MILLIS";
 
     /** File to bind an expiration date */
     private OCFile file;
 
+    /** Share to bind an expiration date */
+    private OCShare share;
+
     /**
      *  Factory method to create new instances
      *
@@ -71,6 +78,23 @@ public class ExpirationDatePickerDialogFragment
         return dialog;
     }
 
+    /**
+     *  Factory method to create new instances
+     *
+     *  @param share                 share to bind an expiration date
+     *  @param chosenDateInMillis   Date chosen when the dialog appears
+     *  @return New dialog instance
+     */
+    public static ExpirationDatePickerDialogFragment newInstance(OCShare share, long chosenDateInMillis) {
+        Bundle arguments = new Bundle();
+        arguments.putParcelable(ARG_SHARE, share);
+        arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
+
+        ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
+        dialog.setArguments(arguments);
+        return dialog;
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -81,6 +105,7 @@ public class ExpirationDatePickerDialogFragment
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         // Load arguments
         file = getArguments().getParcelable(ARG_FILE);
+        share = getArguments().getParcelable(ARG_SHARE);
 
         // Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
         final Calendar chosenDate = Calendar.getInstance();
@@ -104,8 +129,12 @@ public class ExpirationDatePickerDialogFragment
                 Dialog.BUTTON_NEUTRAL,
                 getText(R.string.share_via_link_unset_password),
                 (dialog1, which) -> {
-                    ((FileActivity) getActivity()).getFileOperationsHelper()
-                            .setExpirationDateToShareViaLink(file, -1);
+                    if (file != null) {
+                        ((FileActivity) getActivity()).getFileOperationsHelper()
+                                .setExpirationDateToShareViaLink(file, -1);
+                    } else if (share != null) {
+                        ((FileActivity) getActivity()).getFileOperationsHelper().setExpirationDateToShare(share,-1);
+                    }
                 });
 
         // Prevent days in the past may be chosen
@@ -137,7 +166,11 @@ public class ExpirationDatePickerDialogFragment
         chosenDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
         long chosenDateInMillis = chosenDate.getTimeInMillis();
 
-        ((FileActivity) getActivity()).getFileOperationsHelper()
-                .setExpirationDateToShareViaLink(file, chosenDateInMillis);
+        if (file != null) {
+            ((FileActivity) getActivity()).getFileOperationsHelper()
+                    .setExpirationDateToShareViaLink(file, chosenDateInMillis);
+        } else if (share != null) {
+            ((FileActivity) getActivity()).getFileOperationsHelper().setExpirationDateToShare(share,chosenDateInMillis);
+        }
     }
 }

+ 1 - 1
src/main/java/com/owncloud/android/ui/fragment/FileDetailSharingFragment.java

@@ -201,7 +201,7 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt
 
     private void updateListOfUserGroups() {
         // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
-        UserListAdapter mUserGroupsAdapter = new UserListAdapter(getActivity().getApplicationContext(),
+        UserListAdapter mUserGroupsAdapter = new UserListAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext(),
                 R.layout.share_user_item, shares, account, file, this);
 
         if (shares.size() > 0) {

+ 2 - 2
src/main/java/com/owncloud/android/ui/fragment/util/FileDetailSharingFragmentHelper.java

@@ -80,12 +80,12 @@ public class FileDetailSharingFragmentHelper {
                             new Date(expirationDateValue)
                     );
             expirationDate.setTitle(res.getString(
-                    R.string.share_via_link_menu_expiration_date_label,
+                    R.string.share_expiration_date_label,
                     formattedDate
             ));
         } else {
             expirationDate.setTitle(res.getString(
-                    R.string.share_via_link_menu_expiration_date_label,
+                    R.string.share_expiration_date_label,
                     res.getString(R.string.share_via_link_menu_expiration_date_never)
             ));
         }

+ 19 - 0
src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -537,6 +537,25 @@ public class FileOperationsHelper {
         queueShareIntent(updateShareIntent);
     }
 
+    /**
+     * Updates a public share on a file to set its expiration date.
+     * Starts a request to do it in {@link OperationsService}
+     *
+     * @param share                  {@link OCShare} instance which permissions will be updated.
+     * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
+     *                               date, leaving the link unrestricted. Zero makes no change.
+     */
+    public void setExpirationDateToShare(OCShare share, long expirationTimeInMillis) {
+        Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
+        updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
+        updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
+        updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_ID, share.getId());
+        updateShareIntent.putExtra(
+                OperationsService.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS,
+                expirationTimeInMillis
+        );
+        queueShareIntent(updateShareIntent);
+    }
 
     /**
      * Updates a share on a file to set its access permissions.

+ 1 - 1
src/main/res/menu/file_detail_sharing_link_menu.xml

@@ -36,7 +36,7 @@
     <item
         android:id="@+id/action_share_link_expiration_date"
         android:showAsAction="never"
-        android:title="@string/share_via_link_menu_expiration_date_label"
+        android:title="@string/share_expiration_date_label"
         app:showAsAction="never" />
 
 </menu>

+ 5 - 0
src/main/res/menu/file_detail_sharing_menu.xml

@@ -52,6 +52,11 @@
         android:showAsAction="never"
         android:title="@string/share_privilege_can_share"
         app:showAsAction="never" />
+    <item
+        android:id="@+id/action_expiration_date"
+        android:showAsAction="never"
+        android:title="@string/share_expiration_date_label"
+        app:showAsAction="never" />
     <item
         android:id="@+id/action_unshare"
         android:showAsAction="never"

+ 1 - 1
src/main/res/values/strings.xml

@@ -485,7 +485,7 @@
     <string name="share_via_link_menu_permissions_read_only_label">read-only</string>
     <string name="share_via_link_menu_permissions_upload_only_label">upload-only</string>
     <string name="share_via_link_menu_password_label">Password protect (%1$s)</string>
-    <string name="share_via_link_menu_expiration_date_label">Set expiration date (%1$s)</string>
+    <string name="share_expiration_date_label">Set expiration date (%1$s)</string>
     <string name="share_via_link_menu_expiration_date_never">never</string>
     <string name="share_via_link_section_title">Share link</string>
     <string name="share_via_link_expiration_date_label">Set expiration date</string>