From 396a46ad129a8db2a9cbe1eb26db84b7150bf701 Mon Sep 17 00:00:00 2001 From: Lode Hoste Date: Sat, 28 Mar 2015 22:14:15 +0100 Subject: [PATCH 1/2] Catch invalid input numbers (fixes #324). --- .../fragments/FolderSettingsFragment.java | 35 ++++++++++++++----- .../fragments/SettingsFragment.java | 21 ++++++----- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java index ce7ed458..96fd3d6a 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java @@ -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; diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java index 56f4629e..8ef63c64 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java @@ -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; } From 87bf40b6a195b65fe1c499411d086cffc96a1bd1 Mon Sep 17 00:00:00 2001 From: Lode Hoste Date: Tue, 31 Mar 2015 19:02:53 +0200 Subject: [PATCH 2/2] Remove the rescan interval setting from the GUI --- .../fragments/FolderSettingsFragment.java | 18 +----------------- src/main/res/values-cs/strings.xml | 2 -- src/main/res/values-de/strings.xml | 2 -- src/main/res/values-es/strings.xml | 2 -- src/main/res/values-fr/strings.xml | 2 -- src/main/res/values-it/strings.xml | 2 -- src/main/res/values-ja/strings.xml | 2 -- src/main/res/values-nl/strings.xml | 2 -- src/main/res/values-pl/strings.xml | 2 -- src/main/res/values-pt-rBR/strings.xml | 2 -- src/main/res/values-pt/strings.xml | 2 -- src/main/res/values-ru/strings.xml | 2 -- src/main/res/values-tr/strings.xml | 2 -- src/main/res/values-zh-rCN/strings.xml | 2 -- src/main/res/values-zh-rTW/strings.xml | 2 -- src/main/res/values/strings.xml | 3 --- src/main/res/xml/folder_settings_create.xml | 6 ------ src/main/res/xml/folder_settings_edit.xml | 6 ------ 18 files changed, 1 insertion(+), 60 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java index 96fd3d6a..53b0de63 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java @@ -57,8 +57,6 @@ public class FolderSettingsFragment extends PreferenceFragment private Preference mDirectory; - private EditTextPreference mRescanInterval; - private CheckBoxPreference mFolderMaster; private PreferenceScreen mDevices; @@ -88,8 +86,6 @@ public class FolderSettingsFragment extends PreferenceFragment mFolderId.setOnPreferenceChangeListener(this); mDirectory = findPreference("directory"); mDirectory.setOnPreferenceClickListener(this); - mRescanInterval = (EditTextPreference) findPreference("rescan_interval"); - mRescanInterval.setOnPreferenceChangeListener(this); mFolderMaster = (CheckBoxPreference) findPreference("folder_master"); mFolderMaster.setOnPreferenceChangeListener(this); mDevices = (PreferenceScreen) findPreference("devices"); @@ -107,7 +103,7 @@ public class FolderSettingsFragment extends PreferenceFragment mFolder = new RestApi.Folder(); mFolder.ID = ""; mFolder.Path = ""; - mFolder.RescanIntervalS = 0; + mFolder.RescanIntervalS = 259200; // Scan every 3 days (in case inotify dropped some changes) mFolder.DeviceIds = new ArrayList<>(); mFolder.Versioning = new RestApi.Versioning(); } @@ -157,8 +153,6 @@ public class FolderSettingsFragment extends PreferenceFragment mFolderId.setText(mFolder.ID); mFolderId.setSummary(mFolder.ID); mDirectory.setSummary(mFolder.Path); - mRescanInterval.setText(Integer.toString(mFolder.RescanIntervalS)); - mRescanInterval.setSummary(Integer.toString(mFolder.RescanIntervalS)); mFolderMaster.setChecked(mFolder.ReadOnly); List devicesList = mSyncthingService.getApi().getDevices(false); for (RestApi.Device n : devicesList) { @@ -262,16 +256,6 @@ public class FolderSettingsFragment extends PreferenceFragment mFolder.Path = (String) o; folderUpdated(); return true; - } else if (preference.equals(mRescanInterval)) { - 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(); diff --git a/src/main/res/values-cs/strings.xml b/src/main/res/values-cs/strings.xml index 9000c748..5548faef 100644 --- a/src/main/res/values-cs/strings.xml +++ b/src/main/res/values-cs/strings.xml @@ -52,8 +52,6 @@ Adresář - Interval opakování skenování - Master adresář Přístroje diff --git a/src/main/res/values-de/strings.xml b/src/main/res/values-de/strings.xml index 02b8ebf2..6011c842 100644 --- a/src/main/res/values-de/strings.xml +++ b/src/main/res/values-de/strings.xml @@ -52,8 +52,6 @@ Verzeichnis - Suchintervall - Originalverzeichnis Geräte diff --git a/src/main/res/values-es/strings.xml b/src/main/res/values-es/strings.xml index 7834b114..06978937 100644 --- a/src/main/res/values-es/strings.xml +++ b/src/main/res/values-es/strings.xml @@ -51,8 +51,6 @@ Directorio - Intervalo de Reescanéo - Carpeta Maestra Dispositivos diff --git a/src/main/res/values-fr/strings.xml b/src/main/res/values-fr/strings.xml index 7ac669a6..b95b533a 100644 --- a/src/main/res/values-fr/strings.xml +++ b/src/main/res/values-fr/strings.xml @@ -52,8 +52,6 @@ Dossier - Intervalle d\'analyse - Dossier maitre Machines diff --git a/src/main/res/values-it/strings.xml b/src/main/res/values-it/strings.xml index 01675688..6e52fc6f 100644 --- a/src/main/res/values-it/strings.xml +++ b/src/main/res/values-it/strings.xml @@ -50,8 +50,6 @@ Cartella - Intervallo Scansione - Cartella Principale Dispositivi diff --git a/src/main/res/values-ja/strings.xml b/src/main/res/values-ja/strings.xml index 8eae1345..390a34a6 100644 --- a/src/main/res/values-ja/strings.xml +++ b/src/main/res/values-ja/strings.xml @@ -52,8 +52,6 @@ ディレクトリー - 再スキャン間隔 - フォルダーマスター デバイス diff --git a/src/main/res/values-nl/strings.xml b/src/main/res/values-nl/strings.xml index 77db1fb0..20ab9c1b 100644 --- a/src/main/res/values-nl/strings.xml +++ b/src/main/res/values-nl/strings.xml @@ -52,8 +52,6 @@ Map - Herscanningstussenpose - Maphoofd Apparaten diff --git a/src/main/res/values-pl/strings.xml b/src/main/res/values-pl/strings.xml index 730cde8a..3c8c2dc2 100644 --- a/src/main/res/values-pl/strings.xml +++ b/src/main/res/values-pl/strings.xml @@ -51,8 +51,6 @@ Katalog - Czas między skanowaniem - Folder nadrzędny Urządzenia diff --git a/src/main/res/values-pt-rBR/strings.xml b/src/main/res/values-pt-rBR/strings.xml index edcc5f1a..518ba1b4 100644 --- a/src/main/res/values-pt-rBR/strings.xml +++ b/src/main/res/values-pt-rBR/strings.xml @@ -52,8 +52,6 @@ Pasta - Intervalo entre verificações - Pasta Mestre Dispositivos diff --git a/src/main/res/values-pt/strings.xml b/src/main/res/values-pt/strings.xml index 939d3ae7..daa082fd 100644 --- a/src/main/res/values-pt/strings.xml +++ b/src/main/res/values-pt/strings.xml @@ -52,8 +52,6 @@ Pasta - Intervalo entre verificações - Pasta Mestre Dispositivos diff --git a/src/main/res/values-ru/strings.xml b/src/main/res/values-ru/strings.xml index 906e6231..bfd2d560 100644 --- a/src/main/res/values-ru/strings.xml +++ b/src/main/res/values-ru/strings.xml @@ -51,8 +51,6 @@ Папка - Интервал Сканирования - Главная папка Устройства diff --git a/src/main/res/values-tr/strings.xml b/src/main/res/values-tr/strings.xml index bac7e529..736d928a 100644 --- a/src/main/res/values-tr/strings.xml +++ b/src/main/res/values-tr/strings.xml @@ -52,8 +52,6 @@ Dizin - Yeniden Tarama Aralığı - Ana Klasör Cihazlar diff --git a/src/main/res/values-zh-rCN/strings.xml b/src/main/res/values-zh-rCN/strings.xml index 6f8a14d1..d4a73d41 100644 --- a/src/main/res/values-zh-rCN/strings.xml +++ b/src/main/res/values-zh-rCN/strings.xml @@ -52,8 +52,6 @@ 目录 - 扫描间隔 - 主文件夹 设备 diff --git a/src/main/res/values-zh-rTW/strings.xml b/src/main/res/values-zh-rTW/strings.xml index b7aef6c9..e30744e3 100644 --- a/src/main/res/values-zh-rTW/strings.xml +++ b/src/main/res/values-zh-rTW/strings.xml @@ -51,8 +51,6 @@ 資料夾 - 重新掃描間隔 - 主資料夾 裝置 diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index cb451038..6202b933 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -89,9 +89,6 @@ Directory - - Rescan Interval - Folder Master diff --git a/src/main/res/xml/folder_settings_create.xml b/src/main/res/xml/folder_settings_create.xml index 3757ebc4..7af54062 100644 --- a/src/main/res/xml/folder_settings_create.xml +++ b/src/main/res/xml/folder_settings_create.xml @@ -11,12 +11,6 @@ android:key="directory" android:title="@string/directory" /> - - - -