ExpirationDatePickerDialogFragment.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.lib.resources.shares.OCShare;
  31. import com.owncloud.android.ui.activity.FileActivity;
  32. import java.util.Calendar;
  33. /**
  34. * Dialog requesting a date after today.
  35. */
  36. public class ExpirationDatePickerDialogFragment
  37. extends DialogFragment
  38. implements DatePickerDialog.OnDateSetListener {
  39. /** Tag for FragmentsManager */
  40. public static final String DATE_PICKER_DIALOG = "DATE_PICKER_DIALOG";
  41. /** Parameter constant for {@link OCFile} instance to set the expiration date */
  42. private static final String ARG_FILE = "FILE";
  43. /** Parameter constant for {@link OCShare} instance to set the expiration date */
  44. private static final String ARG_SHARE = "SHARE";
  45. /** Parameter constant for date chosen initially */
  46. private static final String ARG_CHOSEN_DATE_IN_MILLIS = "CHOSEN_DATE_IN_MILLIS";
  47. /** File to bind an expiration date */
  48. private OCFile file;
  49. /** Share to bind an expiration date */
  50. private OCShare share;
  51. /**
  52. * Factory method to create new instances
  53. *
  54. * @param file File to bind an expiration date
  55. * @param chosenDateInMillis Date chosen when the dialog appears
  56. * @return New dialog instance
  57. */
  58. public static ExpirationDatePickerDialogFragment newInstance(OCFile file, long chosenDateInMillis) {
  59. Bundle arguments = new Bundle();
  60. arguments.putParcelable(ARG_FILE, file);
  61. arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
  62. ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
  63. dialog.setArguments(arguments);
  64. return dialog;
  65. }
  66. /**
  67. * Factory method to create new instances
  68. *
  69. * @param share share to bind an expiration date
  70. * @param chosenDateInMillis Date chosen when the dialog appears
  71. * @return New dialog instance
  72. */
  73. public static ExpirationDatePickerDialogFragment newInstance(OCShare share, long chosenDateInMillis) {
  74. Bundle arguments = new Bundle();
  75. arguments.putParcelable(ARG_SHARE, share);
  76. arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
  77. ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
  78. dialog.setArguments(arguments);
  79. return dialog;
  80. }
  81. /**
  82. * {@inheritDoc}
  83. *
  84. * @return A new dialog to let the user choose an expiration date that will be bound to a share link.
  85. */
  86. @Override
  87. @NonNull
  88. public Dialog onCreateDialog(Bundle savedInstanceState) {
  89. // Load arguments
  90. file = getArguments().getParcelable(ARG_FILE);
  91. share = getArguments().getParcelable(ARG_SHARE);
  92. // Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
  93. final Calendar chosenDate = Calendar.getInstance();
  94. long tomorrowInMillis = chosenDate.getTimeInMillis() + DateUtils.DAY_IN_MILLIS;
  95. long chosenDateInMillis = getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
  96. if (chosenDateInMillis > tomorrowInMillis) {
  97. chosenDate.setTimeInMillis(chosenDateInMillis);
  98. } else {
  99. chosenDate.setTimeInMillis(tomorrowInMillis);
  100. }
  101. // Create a new instance of DatePickerDialog
  102. DatePickerDialog dialog = new DatePickerDialog(
  103. getActivity(),
  104. this,
  105. chosenDate.get(Calendar.YEAR),
  106. chosenDate.get(Calendar.MONTH),
  107. chosenDate.get(Calendar.DAY_OF_MONTH)
  108. );
  109. dialog.setButton(
  110. Dialog.BUTTON_NEUTRAL,
  111. getText(R.string.share_via_link_unset_password),
  112. (dialog1, which) -> {
  113. if (file != null) {
  114. ((FileActivity) getActivity()).getFileOperationsHelper()
  115. .setExpirationDateToShareViaLink(file, -1);
  116. } else if (share != null) {
  117. ((FileActivity) getActivity()).getFileOperationsHelper().setExpirationDateToShare(share,-1);
  118. }
  119. });
  120. // Prevent days in the past may be chosen
  121. DatePicker picker = dialog.getDatePicker();
  122. picker.setMinDate(tomorrowInMillis - 1000);
  123. // Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
  124. // in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
  125. // shown by default)
  126. picker.setCalendarViewShown(false);
  127. return dialog;
  128. }
  129. /**
  130. * Called when the user choses an expiration date.
  131. *
  132. * @param view View instance where the date was chosen
  133. * @param year Year of the date chosen.
  134. * @param monthOfYear Month of the date chosen [0, 11]
  135. * @param dayOfMonth Day of the date chosen
  136. */
  137. @Override
  138. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  139. Calendar chosenDate = Calendar.getInstance();
  140. chosenDate.set(Calendar.YEAR, year);
  141. chosenDate.set(Calendar.MONTH, monthOfYear);
  142. chosenDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  143. long chosenDateInMillis = chosenDate.getTimeInMillis();
  144. if (file != null) {
  145. ((FileActivity) getActivity()).getFileOperationsHelper()
  146. .setExpirationDateToShareViaLink(file, chosenDateInMillis);
  147. } else if (share != null) {
  148. ((FileActivity) getActivity()).getFileOperationsHelper().setExpirationDateToShare(share,chosenDateInMillis);
  149. }
  150. }
  151. }