Files
FairEmail/app/src/main/java/eu/faircode/email/DialogDuration.java

110 lines
4.5 KiB
Java
Raw Normal View History

2019-01-15 13:31:31 +00:00
package eu.faircode.email;
2019-05-04 22:49:22 +02:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
2019-01-15 13:31:31 +00:00
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
2019-01-15 13:31:31 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
2019-02-28 15:50:37 +00:00
import android.widget.TextView;
import android.widget.TimePicker;
2019-01-15 13:31:31 +00:00
import androidx.lifecycle.LifecycleOwner;
2019-02-28 15:50:37 +00:00
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
2019-01-15 13:31:31 +00:00
import java.util.Date;
public class DialogDuration {
static void show(Context context, LifecycleOwner owner, int title, final IDialogDuration intf) {
final View dview = LayoutInflater.from(context).inflate(R.layout.dialog_duration, null);
2019-02-28 15:50:37 +00:00
final TextView tvDuration = dview.findViewById(R.id.tvDuration);
final TimePicker timePicker = dview.findViewById(R.id.timePicker);
final DatePicker datePicker = dview.findViewById(R.id.datePicker);
2019-01-15 13:31:31 +00:00
final Calendar cal = Calendar.getInstance();
2019-02-28 15:53:29 +00:00
cal.setTimeInMillis((new Date().getTime() / (3600 * 1000L) + 1) * (3600 * 1000L));
Log.i("Set init=" + new Date(cal.getTimeInMillis()));
2019-01-15 13:31:31 +00:00
2019-02-28 15:50:37 +00:00
final DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.SHORT);
tvDuration.setText(df.format(cal.getTime()));
timePicker.setIs24HourView(android.text.format.DateFormat.is24HourFormat(context));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(cal.get(Calendar.MINUTE));
} else {
timePicker.setHour(cal.get(Calendar.HOUR_OF_DAY));
timePicker.setMinute(cal.get(Calendar.MINUTE));
}
2019-01-15 13:31:31 +00:00
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
2019-01-15 13:31:31 +00:00
@Override
public void onTimeChanged(TimePicker view, int hour, int minute) {
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
2019-02-28 15:50:37 +00:00
tvDuration.setText(df.format(cal.getTime()));
Log.i("Set hour=" + hour + " minute=" + minute +
" time=" + new Date(cal.getTimeInMillis()));
2019-01-15 13:31:31 +00:00
}
});
2019-01-15 13:31:31 +00:00
datePicker.init(
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH),
new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
2019-02-28 15:50:37 +00:00
tvDuration.setText(df.format(cal.getTime()));
Log.i("Set year=" + year + " month=" + month + " day=" + day +
" time=" + new Date(cal.getTimeInMillis()));
}
}
);
2019-01-15 13:31:31 +00:00
new DialogBuilderLifecycle(context, owner)
.setTitle(title)
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
long now = new Date().getTime();
long duration = (cal.getTimeInMillis() - now);
if (duration < 0)
duration = 0;
Log.i("Set duration=" + duration + " time=" + new Date(cal.getTimeInMillis()));
intf.onDurationSelected(duration, cal.getTimeInMillis());
2019-01-15 13:31:31 +00:00
}
})
.show();
}
interface IDialogDuration {
void onDurationSelected(long duration, long time);
}
}