Browse Source

fix npe on setting expiration date on public share

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 4 years ago
parent
commit
898708cfc8

+ 22 - 56
src/main/java/com/owncloud/android/ui/dialog/ExpirationDatePickerDialogFragment.java

@@ -29,7 +29,6 @@ import android.text.format.DateUtils;
 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 com.owncloud.android.ui.helpers.FileOperationsHelper;
@@ -50,46 +49,23 @@ public class ExpirationDatePickerDialogFragment
     /** Tag for FragmentsManager */
     public static final String DATE_PICKER_DIALOG = "DATE_PICKER_DIALOG";
 
-    /** 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
-     *
-     *  @param file                 File to bind an expiration date
-     *  @param chosenDateInMillis   Date chosen when the dialog appears
-     *  @return New dialog instance
-     */
-    public static ExpirationDatePickerDialogFragment newInstance(OCFile file, long chosenDateInMillis) {
-        Bundle arguments = new Bundle();
-        arguments.putParcelable(ARG_FILE, file);
-        arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
-
-        ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
-        dialog.setArguments(arguments);
-        return dialog;
-    }
-
-    /**
-     *  Factory method to create new instances
+     * 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
+     * @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) {
+    public static ExpirationDatePickerDialogFragment newInstance(@NonNull OCShare share, long chosenDateInMillis) {
         Bundle arguments = new Bundle();
         arguments.putParcelable(ARG_SHARE, share);
         arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
@@ -108,37 +84,29 @@ public class ExpirationDatePickerDialogFragment
     @NonNull
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         // Load arguments
-        file = getArguments().getParcelable(ARG_FILE);
-        share = getArguments().getParcelable(ARG_SHARE);
+        share = requireArguments().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();
         long tomorrowInMillis = chosenDate.getTimeInMillis() + DateUtils.DAY_IN_MILLIS;
-        long chosenDateInMillis = getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
-        if (chosenDateInMillis > tomorrowInMillis) {
-            chosenDate.setTimeInMillis(chosenDateInMillis);
-        } else {
-            chosenDate.setTimeInMillis(tomorrowInMillis);
-        }
+        long chosenDateInMillis = requireArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
+        chosenDate.setTimeInMillis(Math.max(chosenDateInMillis, tomorrowInMillis));
 
         // Create a new instance of DatePickerDialog
         DatePickerDialog dialog = new DatePickerDialog(
-                getActivity(),
-                R.style.FallbackDatePickerDialogTheme,
-                this,
-                chosenDate.get(Calendar.YEAR),
-                chosenDate.get(Calendar.MONTH),
-                chosenDate.get(Calendar.DAY_OF_MONTH)
+            requireActivity(),
+            R.style.FallbackDatePickerDialogTheme,
+            this,
+            chosenDate.get(Calendar.YEAR),
+            chosenDate.get(Calendar.MONTH),
+            chosenDate.get(Calendar.DAY_OF_MONTH)
         );
         dialog.setButton(
                 Dialog.BUTTON_NEUTRAL,
                 getText(R.string.share_via_link_unset_password),
                 (dialog1, which) -> {
-                    if (file != null) {
-                        ((FileActivity) getActivity()).getFileOperationsHelper().setExpirationDateToPublicShare(share,
-                                                                                                                -1);
-                    } else if (share != null) {
-                        ((FileActivity) getActivity()).getFileOperationsHelper().setExpirationDateToShare(share,-1);
+                    if (share != null) {
+                        ((FileActivity) requireActivity()).getFileOperationsHelper().setExpirationDateToShare(share, -1);
                     }
                 });
 
@@ -160,12 +128,12 @@ public class ExpirationDatePickerDialogFragment
     }
 
     /**
-     * Called when the user choses an expiration date.
+     * Called when the user chooses an expiration date.
      *
-     * @param view              View instance where the date was chosen
-     * @param year              Year of the date chosen.
-     * @param monthOfYear       Month of the date chosen [0, 11]
-     * @param dayOfMonth        Day of the date chosen
+     * @param view        View instance where the date was chosen
+     * @param year        Year of the date chosen.
+     * @param monthOfYear Month of the date chosen [0, 11]
+     * @param dayOfMonth  Day of the date chosen
      */
     @Override
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
@@ -177,9 +145,7 @@ public class ExpirationDatePickerDialogFragment
         long chosenDateInMillis = chosenDate.getTimeInMillis();
 
         FileOperationsHelper operationsHelper = ((FileActivity) requireActivity()).getFileOperationsHelper();
-        if (file != null) {
-            operationsHelper.setExpirationDateToPublicShare(share, chosenDateInMillis);
-        } else if (share != null) {
+        if (share != null) {
             operationsHelper.setExpirationDateToShare(share, chosenDateInMillis);
         }
     }

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

@@ -421,7 +421,7 @@ public class FileDetailSharingFragment extends Fragment implements ShareeListAda
             }
             case R.id.action_share_expiration_date: {
                 ExpirationDatePickerDialogFragment expirationDialog = ExpirationDatePickerDialogFragment
-                    .newInstance(file, publicShare.getExpirationDate());
+                    .newInstance(publicShare, publicShare.getExpirationDate());
                 expirationDialog.show(fileActivity.getSupportFragmentManager(),
                                       ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG);
                 return true;