BartRunnerAndroid/src/com/dougkeen/bart/TrainAlertDialogFragment.java

104 lines
3.1 KiB
Java
Raw Normal View History

package com.dougkeen.bart;
import net.simonvt.widget.NumberPicker;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
2012-09-17 15:13:50 +00:00
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import com.WazaBe.HoloEverywhere.AlertDialog;
import com.dougkeen.bart.model.Departure;
2012-09-17 15:13:50 +00:00
import com.dougkeen.bart.model.StationPair;
public class TrainAlertDialogFragment extends DialogFragment {
2012-09-17 15:13:50 +00:00
private static final String KEY_LAST_ALERT_LEAD_TIME = "lastAlertLeadTime";
private Departure mDeparture;
2012-09-17 15:13:50 +00:00
private StationPair mStationPair;
2012-09-17 15:13:50 +00:00
public TrainAlertDialogFragment(Departure departure, StationPair stationPair) {
super();
2012-09-17 15:13:50 +00:00
this.mDeparture = departure;
this.mStationPair = stationPair;
}
@Override
public void onStart() {
super.onStart();
SharedPreferences preferences = getActivity().getPreferences(
Context.MODE_PRIVATE);
2012-09-17 15:13:50 +00:00
int lastAlertLeadTime = preferences.getInt(KEY_LAST_ALERT_LEAD_TIME, 5);
NumberPicker numberPicker = (NumberPicker) getDialog().findViewById(
R.id.numberPicker);
final int maxValue = mDeparture.getMeanSecondsLeft() / 60;
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-09-17 15:13:50 +00:00
if (maxValue >= lastAlertLeadTime) {
numberPicker.setValue(lastAlertLeadTime);
} 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();
return new AlertDialog.Builder(activity)
.setTitle(R.string.set_up_departure_alert)
.setCancelable(true)
.setView(R.layout.train_alert_dialog)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
NumberPicker numberPicker = (NumberPicker) getDialog()
.findViewById(R.id.numberPicker);
2012-09-17 15:13:50 +00:00
final int alertLeadTime = numberPicker
.getValue();
2012-09-17 15:13:50 +00:00
// Save most recent selection
Editor editor = getActivity().getPreferences(
Context.MODE_PRIVATE).edit();
2012-09-17 15:13:50 +00:00
editor.putInt(KEY_LAST_ALERT_LEAD_TIME,
alertLeadTime);
editor.commit();
2012-09-17 15:13:50 +00:00
Intent intent = new Intent(getActivity(),
NotificationService.class);
intent.putExtra("departure", mDeparture);
intent.putExtra("stationPair", mStationPair);
intent.putExtra("alertLeadTime", alertLeadTime);
getActivity().startService(intent);
}
})
.setNegativeButton(R.string.skip_alert,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.cancel();
}
}).create();
}
}