ExpirationDatePickerDialogFragment.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.dialog;
  21. import android.app.DatePickerDialog;
  22. import android.app.Dialog;
  23. import android.os.Bundle;
  24. import android.support.annotation.NonNull;
  25. import android.support.v4.app.DialogFragment;
  26. import android.text.format.DateUtils;
  27. import android.widget.DatePicker;
  28. import com.owncloud.android.R;
  29. import com.owncloud.android.datamodel.OCFile;
  30. import com.owncloud.android.ui.activity.FileActivity;
  31. import java.util.Calendar;
  32. /**
  33. * Dialog requesting a date after today.
  34. */
  35. public class ExpirationDatePickerDialogFragment
  36. extends DialogFragment
  37. implements DatePickerDialog.OnDateSetListener {
  38. /** Tag for FragmentsManager */
  39. public static final String DATE_PICKER_DIALOG = "DATE_PICKER_DIALOG";
  40. /** Parameter constant for {@link OCFile} instance to set the expiration date */
  41. private static final String ARG_FILE = "FILE";
  42. /** Parameter constant for date chosen initially */
  43. private static final String ARG_CHOSEN_DATE_IN_MILLIS = "CHOSEN_DATE_IN_MILLIS";
  44. /** File to bind an expiration date */
  45. private OCFile file;
  46. /**
  47. * Factory method to create new instances
  48. *
  49. * @param file File to bind an expiration date
  50. * @param chosenDateInMillis Date chosen when the dialog appears
  51. * @return New dialog instance
  52. */
  53. public static ExpirationDatePickerDialogFragment newInstance(OCFile file, long chosenDateInMillis) {
  54. Bundle arguments = new Bundle();
  55. arguments.putParcelable(ARG_FILE, file);
  56. arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
  57. ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
  58. dialog.setArguments(arguments);
  59. return dialog;
  60. }
  61. /**
  62. * {@inheritDoc}
  63. *
  64. * @return A new dialog to let the user choose an expiration date that will be bound to a share link.
  65. */
  66. @Override
  67. @NonNull
  68. public Dialog onCreateDialog(Bundle savedInstanceState) {
  69. // Load arguments
  70. file = getArguments().getParcelable(ARG_FILE);
  71. // Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
  72. final Calendar chosenDate = Calendar.getInstance();
  73. long tomorrowInMillis = chosenDate.getTimeInMillis() + DateUtils.DAY_IN_MILLIS;
  74. long chosenDateInMillis = getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
  75. if (chosenDateInMillis > tomorrowInMillis) {
  76. chosenDate.setTimeInMillis(chosenDateInMillis);
  77. } else {
  78. chosenDate.setTimeInMillis(tomorrowInMillis);
  79. }
  80. // Create a new instance of DatePickerDialog
  81. DatePickerDialog dialog = new DatePickerDialog(
  82. getActivity(),
  83. this,
  84. chosenDate.get(Calendar.YEAR),
  85. chosenDate.get(Calendar.MONTH),
  86. chosenDate.get(Calendar.DAY_OF_MONTH)
  87. );
  88. dialog.setButton(
  89. Dialog.BUTTON_NEUTRAL,
  90. getText(R.string.share_via_link_unset_password),
  91. (dialog1, which) -> {
  92. ((FileActivity) getActivity()).getFileOperationsHelper()
  93. .setExpirationDateToShareViaLink(file, 0);
  94. });
  95. // Prevent days in the past may be chosen
  96. DatePicker picker = dialog.getDatePicker();
  97. picker.setMinDate(tomorrowInMillis - 1000);
  98. // Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
  99. // in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
  100. // shown by default)
  101. picker.setCalendarViewShown(false);
  102. return dialog;
  103. }
  104. /**
  105. * Called when the user choses an expiration date.
  106. *
  107. * @param view View instance where the date was chosen
  108. * @param year Year of the date chosen.
  109. * @param monthOfYear Month of the date chosen [0, 11]
  110. * @param dayOfMonth Day of the date chosen
  111. */
  112. @Override
  113. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  114. Calendar chosenDate = Calendar.getInstance();
  115. chosenDate.set(Calendar.YEAR, year);
  116. chosenDate.set(Calendar.MONTH, monthOfYear);
  117. chosenDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  118. long chosenDateInMillis = chosenDate.getTimeInMillis();
  119. ((FileActivity) getActivity()).getFileOperationsHelper()
  120. .setExpirationDateToShareViaLink(file, chosenDateInMillis);
  121. }
  122. }