ExpirationDatePickerDialogFragment.java 6.7 KB

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