2012-09-25 21:31:06 +00:00
|
|
|
package com.dougkeen.bart.activities;
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2013-07-29 00:40:23 +00:00
|
|
|
import org.holoeverywhere.LayoutInflater;
|
|
|
|
import org.holoeverywhere.app.AlertDialog;
|
|
|
|
import org.holoeverywhere.app.Dialog;
|
|
|
|
import org.holoeverywhere.app.DialogFragment;
|
|
|
|
import org.holoeverywhere.widget.NumberPicker;
|
|
|
|
|
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;
|
|
|
|
import android.support.v4.app.FragmentActivity;
|
2012-10-03 19:29:31 +00:00
|
|
|
import android.view.View;
|
2012-09-07 05:47:27 +00:00
|
|
|
|
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
|
|
|
|
2012-10-02 04:03:08 +00:00
|
|
|
private static final String KEY_LAST_ALARM_LEAD_TIME = "lastAlarmLeadTime";
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2012-10-02 04:03:08 +00:00
|
|
|
public TrainAlarmDialogFragment() {
|
2012-09-07 05:47:27 +00:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2012-10-03 19:29:31 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setShowsDialog(true);
|
|
|
|
}
|
|
|
|
|
2012-09-07 05:47:27 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
2012-10-03 19:29:31 +00:00
|
|
|
setUpNumberPickerValues(getDialog());
|
|
|
|
}
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2012-10-03 19:29:31 +00:00
|
|
|
private void setUpNumberPickerValues(Dialog dialog) {
|
2012-09-07 05:47:27 +00:00
|
|
|
SharedPreferences preferences = getActivity().getPreferences(
|
|
|
|
Context.MODE_PRIVATE);
|
2012-10-02 04:03:08 +00:00
|
|
|
int lastAlarmLeadTime = preferences.getInt(KEY_LAST_ALARM_LEAD_TIME, 5);
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2012-10-03 19:29:31 +00:00
|
|
|
NumberPicker numberPicker = (NumberPicker) dialog
|
|
|
|
.findViewById(R.id.numberPicker);
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2012-09-18 17:11:52 +00:00
|
|
|
BartRunnerApplication application = (BartRunnerApplication) getActivity()
|
|
|
|
.getApplication();
|
|
|
|
|
2012-10-02 04:03:08 +00:00
|
|
|
final Departure boardedDeparture = application.getBoardedDeparture();
|
|
|
|
final int maxValue = boardedDeparture.getMeanSecondsLeft() / 60;
|
2012-09-07 05:47:27 +00:00
|
|
|
|
|
|
|
String[] displayedValues = new String[maxValue];
|
|
|
|
for (int i = 1; i <= maxValue; i++) {
|
|
|
|
displayedValues[i - 1] = String.valueOf(i);
|
|
|
|
}
|
|
|
|
numberPicker.setMinValue(1);
|
|
|
|
numberPicker.setMaxValue(maxValue);
|
|
|
|
numberPicker.setDisplayedValues(displayedValues);
|
|
|
|
|
2012-10-02 04:03:08 +00:00
|
|
|
if (boardedDeparture.isAlarmPending()) {
|
|
|
|
numberPicker.setValue(boardedDeparture.getAlarmLeadTimeMinutes());
|
|
|
|
} else if (maxValue >= lastAlarmLeadTime) {
|
|
|
|
numberPicker.setValue(lastAlarmLeadTime);
|
2012-09-07 05:47:27 +00:00
|
|
|
} else if (maxValue >= 5) {
|
|
|
|
numberPicker.setValue(5);
|
|
|
|
} else if (maxValue >= 3) {
|
|
|
|
numberPicker.setValue(3);
|
|
|
|
} else {
|
|
|
|
numberPicker.setValue(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
final FragmentActivity activity = getActivity();
|
|
|
|
|
2012-10-03 19:29:31 +00:00
|
|
|
final View dialogView = LayoutInflater.inflate(activity,
|
|
|
|
R.layout.train_alarm_dialog);
|
|
|
|
|
2012-09-07 05:47:27 +00:00
|
|
|
return new AlertDialog.Builder(activity)
|
2012-10-02 04:03:08 +00:00
|
|
|
.setTitle(R.string.set_up_departure_alarm)
|
2012-09-07 05:47:27 +00:00
|
|
|
.setCancelable(true)
|
2012-10-03 19:29:31 +00:00
|
|
|
.setView(dialogView)
|
2012-09-07 05:47:27 +00:00
|
|
|
.setPositiveButton(R.string.ok,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog,
|
|
|
|
int which) {
|
|
|
|
NumberPicker numberPicker = (NumberPicker) getDialog()
|
|
|
|
.findViewById(R.id.numberPicker);
|
2012-10-02 04:03:08 +00:00
|
|
|
final int alarmLeadTime = numberPicker
|
2012-09-17 15:13:50 +00:00
|
|
|
.getValue();
|
2012-09-07 05:47:27 +00:00
|
|
|
|
2012-09-17 15:13:50 +00:00
|
|
|
// Save most recent selection
|
2012-09-07 05:47:27 +00:00
|
|
|
Editor editor = getActivity().getPreferences(
|
|
|
|
Context.MODE_PRIVATE).edit();
|
2012-10-02 04:03:08 +00:00
|
|
|
editor.putInt(KEY_LAST_ALARM_LEAD_TIME,
|
|
|
|
alarmLeadTime);
|
2012-09-07 05:47:27 +00:00
|
|
|
editor.commit();
|
2012-09-17 15:13:50 +00:00
|
|
|
|
2012-09-28 17:10:54 +00:00
|
|
|
((BartRunnerApplication) getActivity()
|
|
|
|
.getApplication())
|
|
|
|
.getBoardedDeparture().setUpAlarm(
|
2012-10-02 04:03:08 +00:00
|
|
|
alarmLeadTime);
|
2012-09-07 05:47:27 +00:00
|
|
|
}
|
|
|
|
})
|
2012-10-02 04:03:08 +00:00
|
|
|
.setNegativeButton(R.string.cancel,
|
2012-09-07 05:47:27 +00:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog,
|
|
|
|
int whichButton) {
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
}
|
|
|
|
}
|