From 40e0950c23e57bfcacce6b84b2b9389005843a8f Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 26 Aug 2012 13:27:11 +0200 Subject: [PATCH] Fixed crash in TimeDialog caused by invalid input --- res/values/strings.xml | 1 + .../danoeh/antennapod/dialog/TimeDialog.java | 70 ++++++++++++++++--- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 2670a78ed..c32359dd8 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -179,6 +179,7 @@ Malformed URL IO Error External storage unavailable + Invalid input, time has to be an integer \ No newline at end of file diff --git a/src/de/danoeh/antennapod/dialog/TimeDialog.java b/src/de/danoeh/antennapod/dialog/TimeDialog.java index de4ccabd1..fb5a72931 100644 --- a/src/de/danoeh/antennapod/dialog/TimeDialog.java +++ b/src/de/danoeh/antennapod/dialog/TimeDialog.java @@ -2,32 +2,41 @@ package de.danoeh.antennapod.dialog; import java.util.concurrent.TimeUnit; -import de.danoeh.antennapod.R; import android.app.Dialog; import android.content.Context; -import android.content.DialogInterface; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; import android.view.View; import android.view.Window; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; +import android.widget.Toast; +import de.danoeh.antennapod.AppConfig; +import de.danoeh.antennapod.R; public abstract class TimeDialog extends Dialog { + private static final String TAG = "TimeDialog"; private static final int DEFAULT_SPINNER_POSITION = 1; - + + private Context context; + private EditText etxtTime; private Spinner spTimeUnit; private Button butConfirm; private Button butCancel; private String[] spinnerContent = { "s", "min", "h" }; - private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS }; + private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, + TimeUnit.HOURS }; public TimeDialog(Context context, int titleTextId, int leftButtonTextId) { super(context); + this.context = context; } @Override @@ -39,13 +48,15 @@ public abstract class TimeDialog extends Dialog { spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit); butConfirm = (Button) findViewById(R.id.butConfirm); butCancel = (Button) findViewById(R.id.butCancel); - + butConfirm.setText(R.string.set_sleeptimer_label); butCancel.setText(R.string.cancel_label); setTitle(R.string.set_sleeptimer_label); - ArrayAdapter spinnerAdapter = new ArrayAdapter(this.getContext(), android.R.layout.simple_spinner_item, + ArrayAdapter spinnerAdapter = new ArrayAdapter( + this.getContext(), android.R.layout.simple_spinner_item, spinnerContent); - spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinnerAdapter + .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spTimeUnit.setAdapter(spinnerAdapter); spTimeUnit.setSelection(DEFAULT_SPINNER_POSITION); butCancel.setOnClickListener(new View.OnClickListener() { @@ -59,11 +70,52 @@ public abstract class TimeDialog extends Dialog { @Override public void onClick(View v) { - dismiss(); - onTimeEntered(readTimeMillis()); + try { + long input = readTimeMillis(); + onTimeEntered(input); + dismiss(); + } catch (NumberFormatException e) { + e.printStackTrace(); + Toast toast = Toast.makeText(context, + R.string.time_dialog_invalid_input, + Toast.LENGTH_LONG); + toast.show(); + } } }); + etxtTime.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + checkInputLength(s.length()); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, + int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, + int count) { + + } + }); + checkInputLength(etxtTime.getText().length()); + + } + + private void checkInputLength(int length) { + if (length > 0) { + if (AppConfig.DEBUG) + Log.d(TAG, "Length is larger than 0, enabling confirm button"); + butConfirm.setEnabled(true); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Length is smaller than 0, disabling confirm button"); + butConfirm.setEnabled(false); + } } public abstract void onTimeEntered(long millis);