// Path: app/src/main/java/org/theotech/ceaselessandroid/prefs/TimePickerDialogPreference.java
// public class TimePickerDialogPreference extends DialogPreference {
//     private static final String TAG = TimePickerDialogPreference.class.getSimpleName();
// 
//     private int lastHour = 0;
//     private int lastMinute = 0;
//     private TimePicker picker = null;
//     private TextView timeView = null;
// 
//     public TimePickerDialogPreference(Context ctxt, AttributeSet attrs) {
//         super(ctxt, attrs);
//     }
// 
//     public static int getHour(String time) {
//         String[] pieces = time.split(":");
//         return Integer.parseInt(pieces[0]);
//     }
// 
//     public static int getMinute(String time) {
//         String[] pieces = time.split(":");
//         return Integer.parseInt(pieces[1]);
//     }
// 
//     @Override
//     protected View onCreateDialogView() {
//         picker = new TimePicker(getContext());
//         return picker;
//     }
// 
//     @Override
//     protected void onBindDialogView(View v) {
//         super.onBindDialogView(v);
//         picker.setCurrentHour(lastHour);
//         picker.setCurrentMinute(lastMinute);
//     }
// 
//     @Override
//     protected void onBindView(View v) {
//         super.onBindView(v);
//         timeView = (TextView) v.findViewById(R.id.timeTextView);
//         timeView.setText(getPersistedString(getContext().getString(R.string.default_notification_time)));
//     }
// 
//     @Override
//     protected void onDialogClosed(boolean positiveResult) {
//         super.onDialogClosed(positiveResult);
// 
//         if (positiveResult) {
//             lastHour = picker.getCurrentHour();
//             lastMinute = picker.getCurrentMinute();
// 
//             String time = String.format("%02d", lastHour) + ":" + String.format("%02d", lastMinute);
// 
//             if (callChangeListener(time)) {
//                 persistString(time);
//             }
// 
//             timeView.setText(getPersistedString(getContext().getString(R.string.default_notification_time)));
//         }
//     }
// 
//     @Override
//     protected Object onGetDefaultValue(TypedArray a, int index) {
//         return (a.getString(index));
//     }
// 
//     @Override
//     protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
//         String time = null;
// 
//         if (restoreValue) {
//             if (defaultValue == null) {
//                 time = getPersistedString("00:00");
//             }
//         } else {
//             time = defaultValue.toString();
//         }
// 
//         lastHour = getHour(time);
//         lastMinute = getMinute(time);
//     }
// }


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import android.util.Log;
import org.theotech.ceaselessandroid.prefs.TimePickerDialogPreference;
import java.util.Calendar;
                Log.d(TAG, "setting alarm for same day.");
                setNotificationAlarm(pendingIntent, alarmManager, intendedTime);

            } else {
                // set from next day
                // you might consider using calendar.add() for adding one day to the current day
                Log.d(TAG, "Setting alarm for next day");
                firingCal.add(Calendar.DAY_OF_MONTH, 1);
                intendedTime = firingCal.getTimeInMillis();
                setNotificationAlarm(pendingIntent, alarmManager, intendedTime);
            }
        } else {
            Log.d(TAG, "No repeating alarm set since notifications are off.");
        }
    }

    private void setNotificationAlarm(PendingIntent pendingIntent, AlarmManager alarmManager, long intendedTime) {

        Log.d(TAG, "Setting intended time to " + intendedTime);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                intendedTime, AlarmManager.INTERVAL_DAY,
                pendingIntent);
    }

    @NonNull
    private Calendar getNotificationFiringCalendar(String time) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, TimePickerDialogPreference.getMinute(time));
// Path: app/src/main/java/org/theotech/ceaselessandroid/prefs/TimePickerDialogPreference.java
// public class TimePickerDialogPreference extends DialogPreference {
//     private static final String TAG = TimePickerDialogPreference.class.getSimpleName();
// 
//     private int lastHour = 0;
//     private int lastMinute = 0;
//     private TimePicker picker = null;
//     private TextView timeView = null;
// 
//     public TimePickerDialogPreference(Context ctxt, AttributeSet attrs) {
//         super(ctxt, attrs);
//     }
// 
//     public static int getHour(String time) {
//         String[] pieces = time.split(":");
//         return Integer.parseInt(pieces[0]);
//     }
// 
//     public static int getMinute(String time) {
//         String[] pieces = time.split(":");
//         return Integer.parseInt(pieces[1]);
//     }
// 
//     @Override
//     protected View onCreateDialogView() {
//         picker = new TimePicker(getContext());
//         return picker;
//     }
// 
//     @Override
//     protected void onBindDialogView(View v) {
//         super.onBindDialogView(v);
//         picker.setCurrentHour(lastHour);
//         picker.setCurrentMinute(lastMinute);
//     }
// 
//     @Override
//     protected void onBindView(View v) {
//         super.onBindView(v);
//         timeView = (TextView) v.findViewById(R.id.timeTextView);
//         timeView.setText(getPersistedString(getContext().getString(R.string.default_notification_time)));
//     }
// 
//     @Override
//     protected void onDialogClosed(boolean positiveResult) {
//         super.onDialogClosed(positiveResult);
// 
//         if (positiveResult) {
//             lastHour = picker.getCurrentHour();
//             lastMinute = picker.getCurrentMinute();
// 
//             String time = String.format("%02d", lastHour) + ":" + String.format("%02d", lastMinute);
// 
//             if (callChangeListener(time)) {
//                 persistString(time);
//             }
// 
//             timeView.setText(getPersistedString(getContext().getString(R.string.default_notification_time)));
//         }
//     }
// 
//     @Override
//     protected Object onGetDefaultValue(TypedArray a, int index) {
//         return (a.getString(index));
//     }
// 
//     @Override
//     protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
//         String time = null;
// 
//         if (restoreValue) {
//             if (defaultValue == null) {
//                 time = getPersistedString("00:00");
//             }
//         } else {
//             time = defaultValue.toString();
//         }
// 
//         lastHour = getHour(time);
//         lastMinute = getMinute(time);
//     }
// }


// Path: app/src/main/java/org/theotech/ceaselessandroid/notification/DailyNotificationReceiver.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import android.util.Log;
import org.theotech.ceaselessandroid.prefs.TimePickerDialogPreference;
import java.util.Calendar;

                Log.d(TAG, "setting alarm for same day.");
                setNotificationAlarm(pendingIntent, alarmManager, intendedTime);

            } else {
                // set from next day
                // you might consider using calendar.add() for adding one day to the current day
                Log.d(TAG, "Setting alarm for next day");
                firingCal.add(Calendar.DAY_OF_MONTH, 1);
                intendedTime = firingCal.getTimeInMillis();
                setNotificationAlarm(pendingIntent, alarmManager, intendedTime);
            }
        } else {
            Log.d(TAG, "No repeating alarm set since notifications are off.");
        }
    }

    private void setNotificationAlarm(PendingIntent pendingIntent, AlarmManager alarmManager, long intendedTime) {

        Log.d(TAG, "Setting intended time to " + intendedTime);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                intendedTime, AlarmManager.INTERVAL_DAY,
                pendingIntent);
    }

    @NonNull
    private Calendar getNotificationFiringCalendar(String time) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, TimePickerDialogPreference.getMinute(time));
        calendar.set(Calendar.HOUR_OF_DAY, TimePickerDialogPreference.getHour(time));