1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 22:31:16 +00:00

Catch invalid input numbers (fixes #324).

This commit is contained in:
Lode Hoste 2015-03-28 22:14:15 +01:00
parent 46d914bc1e
commit 396a46ad12
2 changed files with 37 additions and 19 deletions

View file

@ -10,6 +10,7 @@ import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.support.v4.preference.PreferenceFragment;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
@ -241,6 +242,15 @@ public class FolderSettingsFragment extends PreferenceFragment
public boolean onPreferenceChange(Preference preference, Object o) {
if (preference instanceof EditTextPreference) {
EditTextPreference pref = (EditTextPreference) preference;
if ((pref.getEditText().getInputType() & InputType.TYPE_CLASS_NUMBER) > 0) {
try {
o = Integer.parseInt((String) o);
o = o.toString();
} catch (NumberFormatException e) {
Log.w(TAG, "Invalid number: " + o);
return false;
}
}
pref.setSummary((String) o);
}
@ -253,10 +263,15 @@ public class FolderSettingsFragment extends PreferenceFragment
folderUpdated();
return true;
} else if (preference.equals(mRescanInterval)) {
mFolder.RescanIntervalS = Integer.parseInt((String) o);
mRescanInterval.setSummary((String) o);
folderUpdated();
return true;
try {
mFolder.RescanIntervalS = Integer.parseInt((String) o);
mRescanInterval.setSummary((String) o);
folderUpdated();
return true;
} catch (NumberFormatException e) {
Log.w(TAG, "Invalid rescan interval: "+ o);
}
return false;
} else if (preference.equals(mFolderMaster)) {
mFolder.ReadOnly = (Boolean) o;
folderUpdated();
@ -291,10 +306,14 @@ public class FolderSettingsFragment extends PreferenceFragment
folderUpdated();
return true;
} else if (preference.equals(mVersioningKeep)) {
((RestApi.SimpleVersioning) mFolder.Versioning)
.setParams(Integer.parseInt((String) o));
folderUpdated();
return true;
try {
((RestApi.SimpleVersioning) mFolder.Versioning)
.setParams(Integer.parseInt((String) o));
folderUpdated();
return true;
} catch (NumberFormatException e) {
Log.w(TAG, "Invalid versioning option: "+ o);
}
}
return false;

View file

@ -172,10 +172,17 @@ public class SettingsFragment extends PreferenceFragment
public boolean onPreferenceChange(Preference preference, Object o) {
// Convert new value to integer if input type is number.
if (preference instanceof EditTextPreference) {
EditTextPreference etp = (EditTextPreference) preference;
if (etp.getEditText().getInputType() == InputType.TYPE_CLASS_NUMBER) {
o = Integer.parseInt((String) o);
EditTextPreference pref = (EditTextPreference) preference;
if ((pref.getEditText().getInputType() & InputType.TYPE_CLASS_NUMBER) > 0) {
try {
o = Integer.parseInt((String) o);
o = o.toString();
} catch (NumberFormatException e) {
Log.w(TAG, "Invalid number: " + o);
return false;
}
}
pref.setSummary((String) o);
}
if (preference.equals(mSyncOnlyCharging) || preference.equals(mSyncOnlyWifi)) {
@ -216,14 +223,6 @@ public class SettingsFragment extends PreferenceFragment
((SyncthingActivity) getActivity()).getApi().requireRestart(getActivity());
}
// Set the preference value as summary.
if (preference instanceof EditTextPreference) {
String value = (o instanceof String)
? (String) o
: Integer.toString((Integer) o);
preference.setSummary(value);
}
return true;
}