8e3260a5009068279974c5199fd90d5e0a1ac922
[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 /** Constructor arguments */
50 private static final String ARG_FILE = "ARG_FILE";
51
52 /** File to bind an expiration date */
53 private OCFile mFile;
54
55 /**
56 * Factory method to create new instances
57 *
58 * @param file File to bind an expiration date
59 * @return New dialog instance
60 */
61 public static ExpirationDatePickerDialogFragment newInstance(OCFile file) {
62 Bundle arguments = new Bundle();
63 arguments.putParcelable(ARG_FILE, file);
64
65 ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
66 dialog.setArguments(arguments);
67 return dialog;
68 }
69
70 /**
71 * {@inheritDoc}
72 *
73 * @return A new dialog to let the user choose an expiration date that will be bound to a share link.
74 */
75 @Override
76 public Dialog onCreateDialog(Bundle savedInstanceState) {
77 // Load arguments
78 mFile = getArguments().getParcelable(ARG_FILE);
79
80 // Get current day
81 final Calendar c = Calendar.getInstance();
82 int year = c.get(Calendar.YEAR);
83 int month = c.get(Calendar.MONTH);
84 int day = c.get(Calendar.DAY_OF_MONTH);
85
86 // Create a new instance of DatePickerDialog, highlighting "tomorrow" as chosen day
87 DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day + 1);
88
89 // Prevent days in the past may be chosen
90 DatePicker picker = dialog.getDatePicker();
91 picker.setMinDate(System.currentTimeMillis() + DateUtils.DAY_IN_MILLIS - 1000);
92
93 // Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
94 // in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
95 // shown by default)
96 picker.setCalendarViewShown(false);
97
98 return dialog;
99 }
100
101 /**
102 * Called when the user choses an expiration date.
103 *
104 * @param view View instance where the date was chosen
105 * @param year Year of the date chosen.
106 * @param monthOfYear Month of the date chosen [0, 11]
107 * @param dayOfMonth Day of the date chosen
108 */
109 @Override
110 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
111
112 Calendar chosenDate = Calendar.getInstance();
113 chosenDate.set(Calendar.YEAR, year);
114 chosenDate.set(Calendar.MONTH, monthOfYear);
115 chosenDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
116
117 ((FileActivity)getActivity()).getFileOperationsHelper().setExpirationDateToShareViaLink(
118 mFile,
119 year,
120 monthOfYear,
121 dayOfMonth
122 );
123 }
124 }