2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.ui
.dialog
;
24 import android
.app
.DatePickerDialog
;
25 import android
.app
.Dialog
;
26 import android
.content
.DialogInterface
;
27 import android
.os
.Bundle
;
28 import android
.support
.v4
.app
.DialogFragment
;
29 import android
.text
.format
.DateUtils
;
30 import android
.widget
.DatePicker
;
31 import android
.widget
.Toast
;
33 import com
.owncloud
.android
.datamodel
.OCFile
;
34 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
36 import java
.util
.Calendar
;
37 import java
.util
.Date
;
40 * Dialog requesting a date after today.
42 public class ExpirationDatePickerDialogFragment
43 extends DialogFragment
44 implements DatePickerDialog
.OnDateSetListener
{
46 /** Tag for FragmentsManager */
47 public static final String DATE_PICKER_DIALOG
= "DATE_PICKER_DIALOG";
49 /** Parameter constant for {@link OCFile} instance to set the expiration date */
50 private static final String ARG_FILE
= "FILE";
52 /** Parameter constant for date chosen initially */
53 private static final String ARG_CHOSEN_DATE_IN_MILLIS
= "CHOSEN_DATE_IN_MILLIS";
55 /** File to bind an expiration date */
59 * Factory method to create new instances
61 * @param file File to bind an expiration date
62 * @param chosenDateInMillis Date chosen when the dialog appears
63 * @return New dialog instance
65 public static ExpirationDatePickerDialogFragment
newInstance(OCFile file
, long chosenDateInMillis
) {
66 Bundle arguments
= new Bundle();
67 arguments
.putParcelable(ARG_FILE
, file
);
68 arguments
.putLong(ARG_CHOSEN_DATE_IN_MILLIS
, chosenDateInMillis
);
70 ExpirationDatePickerDialogFragment dialog
= new ExpirationDatePickerDialogFragment();
71 dialog
.setArguments(arguments
);
78 * @return A new dialog to let the user choose an expiration date that will be bound to a share link.
81 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
83 mFile
= getArguments().getParcelable(ARG_FILE
);
85 // Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
86 final Calendar chosenDate
= Calendar
.getInstance();
87 long tomorrowInMillis
= chosenDate
.getTimeInMillis() + DateUtils
.DAY_IN_MILLIS
;
88 long chosenDateInMillis
= getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS
);
89 if (chosenDateInMillis
> tomorrowInMillis
) {
90 chosenDate
.setTimeInMillis(chosenDateInMillis
);
92 chosenDate
.setTimeInMillis(tomorrowInMillis
);
95 // Create a new instance of DatePickerDialog
96 DatePickerDialog dialog
= new DatePickerDialog(
99 chosenDate
.get(Calendar
.YEAR
),
100 chosenDate
.get(Calendar
.MONTH
),
101 chosenDate
.get(Calendar
.DAY_OF_MONTH
)
104 // Prevent days in the past may be chosen
105 DatePicker picker
= dialog
.getDatePicker();
106 picker
.setMinDate(tomorrowInMillis
- 1000);
108 // Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
109 // in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
111 picker
.setCalendarViewShown(false
);
117 * Called when the user choses an expiration date.
119 * @param view View instance where the date was chosen
120 * @param year Year of the date chosen.
121 * @param monthOfYear Month of the date chosen [0, 11]
122 * @param dayOfMonth Day of the date chosen
125 public void onDateSet(DatePicker view
, int year
, int monthOfYear
, int dayOfMonth
) {
127 Calendar chosenDate
= Calendar
.getInstance();
128 chosenDate
.set(Calendar
.YEAR
, year
);
129 chosenDate
.set(Calendar
.MONTH
, monthOfYear
);
130 chosenDate
.set(Calendar
.DAY_OF_MONTH
, dayOfMonth
);
131 long chosenDateInMillis
= chosenDate
.getTimeInMillis();
133 ((FileActivity
)getActivity()).getFileOperationsHelper().setExpirationDateToShareViaLink(