2012-09-25 21:31:06 +00:00
|
|
|
package com.dougkeen.bart.activities;
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2015-08-12 00:56:05 +00:00
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.app.Dialog;
|
2012-09-07 05:47:27 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
|
import android.os.Bundle;
|
2015-08-12 00:56:05 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.v4.app.DialogFragment;
|
2012-09-07 05:47:27 +00:00
|
|
|
import android.support.v4.app.FragmentActivity;
|
2015-08-12 00:56:05 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.view.LayoutInflater;
|
2012-10-03 19:29:31 +00:00
|
|
|
import android.view.View;
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2015-08-12 00:56:05 +00:00
|
|
|
import com.codetroopers.betterpickers.numberpicker.NumberPicker;
|
2012-09-25 21:31:06 +00:00
|
|
|
import com.dougkeen.bart.BartRunnerApplication;
|
|
|
|
import com.dougkeen.bart.R;
|
2012-10-02 04:03:08 +00:00
|
|
|
import com.dougkeen.bart.model.Departure;
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2012-10-02 04:03:08 +00:00
|
|
|
public class TrainAlarmDialogFragment extends DialogFragment {
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2015-08-12 00:56:05 +00:00
|
|
|
public static final String TAG = "TRAIN_ALARM_DIALOG_FRAGMENT_TAG";
|
2015-08-12 01:56:23 +00:00
|
|
|
private static final String KEY_LAST_ALARM_LEAD_TIME = "lastAlarmLeadTime";
|
|
|
|
|
|
|
|
public TrainAlarmDialogFragment() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setShowsDialog(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
setUpNumberPickerValues(getDialog());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setUpNumberPickerValues(Dialog dialog) {
|
|
|
|
SharedPreferences preferences = getActivity().getPreferences(
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
int lastAlarmLeadTime = preferences.getInt(KEY_LAST_ALARM_LEAD_TIME, 5);
|
|
|
|
|
|
|
|
NumberPicker numberPicker = (NumberPicker) dialog
|
|
|
|
.findViewById(R.id.numberPicker);
|
|
|
|
|
|
|
|
BartRunnerApplication application = (BartRunnerApplication) getActivity()
|
|
|
|
.getApplication();
|
|
|
|
|
|
|
|
final Departure boardedDeparture = application.getBoardedDeparture();
|
|
|
|
final int maxValue = boardedDeparture.getMeanSecondsLeft() / 60;
|
|
|
|
|
2015-08-12 00:56:05 +00:00
|
|
|
numberPicker.setMin(1);
|
|
|
|
numberPicker.setMax(maxValue);
|
2015-08-12 01:56:23 +00:00
|
|
|
|
|
|
|
if (boardedDeparture.isAlarmPending()) {
|
2015-08-12 00:56:05 +00:00
|
|
|
setNumber(numberPicker, boardedDeparture.getAlarmLeadTimeMinutes());
|
2015-08-12 01:56:23 +00:00
|
|
|
} else if (maxValue >= lastAlarmLeadTime) {
|
2015-08-12 00:56:05 +00:00
|
|
|
setNumber(numberPicker, lastAlarmLeadTime);
|
2015-08-12 01:56:23 +00:00
|
|
|
} else if (maxValue >= 5) {
|
2015-08-12 00:56:05 +00:00
|
|
|
setNumber(numberPicker, 5);
|
2015-08-12 01:56:23 +00:00
|
|
|
} else if (maxValue >= 3) {
|
2015-08-12 00:56:05 +00:00
|
|
|
setNumber(numberPicker, 3);
|
2015-08-12 01:56:23 +00:00
|
|
|
} else {
|
2015-08-12 00:56:05 +00:00
|
|
|
setNumber(numberPicker, 1);
|
2015-08-12 01:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 00:56:05 +00:00
|
|
|
private void setNumber(NumberPicker numberPicker, int value) {
|
|
|
|
// Passing in null for the decimalPart and sign doesn't change them.
|
|
|
|
numberPicker.setNumber(value, null /* decimalPart */, null /* sign */);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
2015-08-12 01:56:23 +00:00
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
final FragmentActivity activity = getActivity();
|
|
|
|
|
2015-08-12 00:56:05 +00:00
|
|
|
@SuppressLint("InflateParams")
|
|
|
|
final View dialogView = LayoutInflater.from(activity)
|
|
|
|
.inflate(R.layout.train_alarm_dialog, null /* root */);
|
2015-08-12 01:56:23 +00:00
|
|
|
|
|
|
|
return new AlertDialog.Builder(activity)
|
|
|
|
.setTitle(R.string.set_up_departure_alarm)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setView(dialogView)
|
|
|
|
.setPositiveButton(R.string.ok,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog,
|
2015-08-12 00:56:05 +00:00
|
|
|
int which) {
|
2015-08-12 01:56:23 +00:00
|
|
|
NumberPicker numberPicker = (NumberPicker) getDialog()
|
|
|
|
.findViewById(R.id.numberPicker);
|
2015-08-12 00:56:05 +00:00
|
|
|
final int alarmLeadTime = numberPicker.getNumber();
|
2015-08-12 01:56:23 +00:00
|
|
|
|
|
|
|
// Save most recent selection
|
|
|
|
Editor editor = getActivity().getPreferences(
|
|
|
|
Context.MODE_PRIVATE).edit();
|
|
|
|
editor.putInt(KEY_LAST_ALARM_LEAD_TIME,
|
|
|
|
alarmLeadTime);
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
((BartRunnerApplication) getActivity()
|
|
|
|
.getApplication())
|
|
|
|
.getBoardedDeparture().setUpAlarm(
|
|
|
|
alarmLeadTime);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.cancel,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog,
|
|
|
|
int whichButton) {
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
}
|
2012-09-07 05:47:27 +00:00
|
|
|
}
|