schedule notification
This commit is contained in:
@@ -2,6 +2,7 @@ package net.headlezz.notificationlogger.createnotification;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -20,8 +21,9 @@ import java.util.Date;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class NotificationCreationFragment extends Fragment implements View.OnClickListener {
|
||||
public class NotificationCreationFragment extends Fragment implements View.OnClickListener, NotificationScheduleHelper.NotificationScheduleManagerCallback {
|
||||
|
||||
// TODO permissions to vibrate etc?
|
||||
// TODO intent
|
||||
// TODO check id size
|
||||
|
||||
@@ -132,6 +134,44 @@ public class NotificationCreationFragment extends Fragment implements View.OnCli
|
||||
}
|
||||
|
||||
private void sheduleNotification(DispatchableNotification dn) {
|
||||
dn.shedule(new Date(System.currentTimeMillis() + 5 * 1000));
|
||||
new NotificationScheduleHelper(getContext(), dn, this).shedule();
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
public void onNotificationScheduleCreated(Date date, DispatchableNotification dn) {
|
||||
// delayn the shedule to see if user presses the undo button
|
||||
Snackbar snack = Snackbar.make(getView(), getString(R.string.create_shedule_snack, date.toString()), Snackbar.LENGTH_LONG);
|
||||
ScheduleSnackbarListener listener = new ScheduleSnackbarListener(dn, date);
|
||||
snack.setCallback(listener);
|
||||
snack.setAction(getString(R.string.create_shedule_snack_undo), listener);
|
||||
snack.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Snackbar listener to track if the user undoes the schedule operation
|
||||
*/
|
||||
class ScheduleSnackbarListener extends Snackbar.Callback implements View.OnClickListener {
|
||||
|
||||
private DispatchableNotification mNotification;
|
||||
private Date mDispatchDate;
|
||||
private boolean dispatch = true;
|
||||
|
||||
public ScheduleSnackbarListener(DispatchableNotification notification, Date dispatchDate) {
|
||||
mDispatchDate = dispatchDate;
|
||||
mNotification = notification;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
dispatch = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismissed(Snackbar snackbar, int event) {
|
||||
super.onDismissed(snackbar, event);
|
||||
if(dispatch)
|
||||
mNotification.shedule(mDispatchDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package net.headlezz.notificationlogger.createnotification;
|
||||
|
||||
|
||||
import android.app.DatePickerDialog;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.content.Context;
|
||||
import android.widget.DatePicker;
|
||||
import android.widget.TimePicker;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
/**
|
||||
* Helper class to create the date to schedule a notification
|
||||
*/
|
||||
public class NotificationScheduleHelper {
|
||||
|
||||
private Context mContext;
|
||||
private DispatchableNotification mNotification;
|
||||
NotificationScheduleManagerCallback mCallback;
|
||||
|
||||
interface NotificationScheduleManagerCallback {
|
||||
void onNotificationScheduleCreated(Date date, DispatchableNotification dn);
|
||||
}
|
||||
|
||||
public NotificationScheduleHelper(Context context, DispatchableNotification dn, NotificationScheduleManagerCallback cb) {
|
||||
mContext = context;
|
||||
mNotification = dn;
|
||||
mCallback = cb;
|
||||
}
|
||||
|
||||
public void shedule() {
|
||||
openSheduleDialogs();
|
||||
}
|
||||
|
||||
private void openSheduleDialogs() {
|
||||
NotificationSheduleCreationListener mListener = new NotificationSheduleCreationListener();
|
||||
Calendar cal = new GregorianCalendar();
|
||||
DatePickerDialog dialog = new DatePickerDialog(mContext, mListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
public void onTimeSet(int year, int monthOfYear, int dayOfMonth, int hour, int minute) {
|
||||
mCallback.onNotificationScheduleCreated(new Date(year-1900, monthOfYear, dayOfMonth, hour, minute), mNotification);
|
||||
}
|
||||
|
||||
|
||||
class NotificationSheduleCreationListener implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
|
||||
|
||||
private int year;
|
||||
private int monthOfYear;
|
||||
private int dayOfMonth;
|
||||
|
||||
@Override
|
||||
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
|
||||
this.year = year;
|
||||
this.monthOfYear = monthOfYear;
|
||||
this.dayOfMonth = dayOfMonth;
|
||||
Calendar cal = new GregorianCalendar();
|
||||
new TimePickerDialog(mContext, this, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)+1, true).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
|
||||
NotificationScheduleHelper.this.onTimeSet(year, monthOfYear, dayOfMonth, hourOfDay, minute);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,9 @@
|
||||
<string name="create_btSchedule">schedule</string>
|
||||
<string name="create_btDispatch">dispatch</string>
|
||||
|
||||
<string name="create_shedule_snack">Notification sheduled for %1$s</string>
|
||||
<string name="create_shedule_snack_undo">Undo</string>
|
||||
|
||||
<string-array name="create_categories">
|
||||
<item>Alarm</item>
|
||||
<item>Call</item>
|
||||
|
||||
Reference in New Issue
Block a user