ExpirationDatePickerDialogFragment.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.v4.app.DialogFragment;
  25. import android.text.format.DateUtils;
  26. import android.widget.DatePicker;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.ui.activity.FileActivity;
  29. import java.util.Calendar;
  30. /**
  31. * Dialog requesting a date after today.
  32. */
  33. public class ExpirationDatePickerDialogFragment
  34. extends DialogFragment
  35. implements DatePickerDialog.OnDateSetListener {
  36. /** Tag for FragmentsManager */
  37. public static final String DATE_PICKER_DIALOG = "DATE_PICKER_DIALOG";
  38. /** Parameter constant for {@link OCFile} instance to set the expiration date */
  39. private static final String ARG_FILE = "FILE";
  40. /** Parameter constant for date chosen initially */
  41. private static final String ARG_CHOSEN_DATE_IN_MILLIS = "CHOSEN_DATE_IN_MILLIS";
  42. /** File to bind an expiration date */
  43. private OCFile mFile;
  44. /**
  45. * Factory method to create new instances
  46. *
  47. * @param file File to bind an expiration date
  48. * @param chosenDateInMillis Date chosen when the dialog appears
  49. * @return New dialog instance
  50. */
  51. public static ExpirationDatePickerDialogFragment newInstance(OCFile file, long chosenDateInMillis) {
  52. Bundle arguments = new Bundle();
  53. arguments.putParcelable(ARG_FILE, file);
  54. arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
  55. ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
  56. dialog.setArguments(arguments);
  57. return dialog;
  58. }
  59. /**
  60. * {@inheritDoc}
  61. *
  62. * @return A new dialog to let the user choose an expiration date that will be bound to a share link.
  63. */
  64. @Override
  65. public Dialog onCreateDialog(Bundle savedInstanceState) {
  66. // Load arguments
  67. mFile = getArguments().getParcelable(ARG_FILE);
  68. // Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
  69. final Calendar chosenDate = Calendar.getInstance();
  70. long tomorrowInMillis = chosenDate.getTimeInMillis() + DateUtils.DAY_IN_MILLIS;
  71. long chosenDateInMillis = getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
  72. if (chosenDateInMillis > tomorrowInMillis) {
  73. chosenDate.setTimeInMillis(chosenDateInMillis);
  74. } else {
  75. chosenDate.setTimeInMillis(tomorrowInMillis);
  76. }
  77. // Create a new instance of DatePickerDialog
  78. DatePickerDialog dialog = new DatePickerDialog(
  79. getActivity(),
  80. this,
  81. chosenDate.get(Calendar.YEAR),
  82. chosenDate.get(Calendar.MONTH),
  83. chosenDate.get(Calendar.DAY_OF_MONTH)
  84. );
  85. // Prevent days in the past may be chosen
  86. DatePicker picker = dialog.getDatePicker();
  87. picker.setMinDate(tomorrowInMillis - 1000);
  88. // Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
  89. // in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
  90. // shown by default)
  91. picker.setCalendarViewShown(false);
  92. return dialog;
  93. }
  94. /**
  95. * Called when the user choses an expiration date.
  96. *
  97. * @param view View instance where the date was chosen
  98. * @param year Year of the date chosen.
  99. * @param monthOfYear Month of the date chosen [0, 11]
  100. * @param dayOfMonth Day of the date chosen
  101. */
  102. @Override
  103. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  104. Calendar chosenDate = Calendar.getInstance();
  105. chosenDate.set(Calendar.YEAR, year);
  106. chosenDate.set(Calendar.MONTH, monthOfYear);
  107. chosenDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  108. long chosenDateInMillis = chosenDate.getTimeInMillis();
  109. ((FileActivity)getActivity()).getFileOperationsHelper().setExpirationDateToShareViaLink(
  110. mFile,
  111. chosenDateInMillis
  112. );
  113. }
  114. }