Fixed crash in TimeDialog caused by invalid input

This commit is contained in:
daniel oeh 2012-08-26 13:27:11 +02:00
parent 625a043644
commit 40e0950c23
2 changed files with 62 additions and 9 deletions

View File

@ -179,6 +179,7 @@
<string name="download_error_malformed_url">Malformed URL</string>
<string name="download_error_io_error">IO Error</string>
<string name="download_error_device_not_found">External storage unavailable</string>
<string name="time_dialog_invalid_input">Invalid input, time has to be an integer</string>
</resources>

View File

@ -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
@ -43,9 +52,11 @@ public abstract class TimeDialog extends Dialog {
butConfirm.setText(R.string.set_sleeptimer_label);
butCancel.setText(R.string.cancel_label);
setTitle(R.string.set_sleeptimer_label);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_item,
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
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) {
try {
long input = readTimeMillis();
onTimeEntered(input);
dismiss();
onTimeEntered(readTimeMillis());
} 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);