Merge remote-tracking branch 'remotes/upstream/resizeCache' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / ExpirationDatePickerDialogFragment.java
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
21 package com.owncloud.android.ui.dialog;
22
23
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;
32
33 import com.owncloud.android.datamodel.OCFile;
34 import com.owncloud.android.ui.activity.FileActivity;
35
36 import java.util.Calendar;
37 import java.util.Date;
38
39 /**
40 * Dialog requesting a date after today.
41 */
42 public class ExpirationDatePickerDialogFragment
43 extends DialogFragment
44 implements DatePickerDialog.OnDateSetListener {
45
46 /** Tag for FragmentsManager */
47 public static final String DATE_PICKER_DIALOG = "DATE_PICKER_DIALOG";
48
49 /** Parameter constant for {@link OCFile} instance to set the expiration date */
50 private static final String ARG_FILE = "FILE";
51
52 /** Parameter constant for date chosen initially */
53 private static final String ARG_CHOSEN_DATE_IN_MILLIS = "CHOSEN_DATE_IN_MILLIS";
54
55 /** File to bind an expiration date */
56 private OCFile mFile;
57
58 /**
59 * Factory method to create new instances
60 *
61 * @param file File to bind an expiration date
62 * @param chosenDateInMillis Date chosen when the dialog appears
63 * @return New dialog instance
64 */
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);
69
70 ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
71 dialog.setArguments(arguments);
72 return dialog;
73 }
74
75 /**
76 * {@inheritDoc}
77 *
78 * @return A new dialog to let the user choose an expiration date that will be bound to a share link.
79 */
80 @Override
81 public Dialog onCreateDialog(Bundle savedInstanceState) {
82 // Load arguments
83 mFile = getArguments().getParcelable(ARG_FILE);
84
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);
91 } else {
92 chosenDate.setTimeInMillis(tomorrowInMillis);
93 }
94
95 // Create a new instance of DatePickerDialog
96 DatePickerDialog dialog = new DatePickerDialog(
97 getActivity(),
98 this,
99 chosenDate.get(Calendar.YEAR),
100 chosenDate.get(Calendar.MONTH),
101 chosenDate.get(Calendar.DAY_OF_MONTH)
102 );
103
104 // Prevent days in the past may be chosen
105 DatePicker picker = dialog.getDatePicker();
106 picker.setMinDate(tomorrowInMillis - 1000);
107
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
110 // shown by default)
111 picker.setCalendarViewShown(false);
112
113 return dialog;
114 }
115
116 /**
117 * Called when the user choses an expiration date.
118 *
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
123 */
124 @Override
125 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
126
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();
132
133 ((FileActivity)getActivity()).getFileOperationsHelper().setExpirationDateToShareViaLink(
134 mFile,
135 chosenDateInMillis
136 );
137 }
138 }