Skip to content

Commit adebdbc

Browse files
committed
better input validation for bookmarks dialog. fixes #4337, #4336, #4335 & #4334
Signed-off-by: VishnuSanal <t.v.s10123@gmail.com>
1 parent 62b9545 commit adebdbc

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

app/src/main/java/com/amaze/filemanager/ui/fragments/preferencefragments/BookmarksPrefsFragment.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class BookmarksPrefsFragment : BasePrefsFragment() {
152152
path: String,
153153
): Pair<Boolean, Int> {
154154
return when {
155-
name.isEmpty() -> Pair(false, R.string.invalid_name)
155+
name.isEmpty() || path.isEmpty() -> Pair(false, R.string.invalid_name)
156156
dataUtils.containsBooks(arrayOf(name, path)) != -1 -> Pair(false, R.string.bookmark_exists)
157157
!FileUtils.isPathAccessible(path, activity.prefs) -> Pair(false, R.string.ftp_path_change_error_invalid)
158158
else -> Pair(true, 0)
@@ -190,6 +190,17 @@ class BookmarksPrefsFragment : BasePrefsFragment() {
190190
.setOnClickListener {
191191
val oldName = p.title.toString()
192192
val oldPath = p.summary.toString()
193+
194+
val result = isValidBookmark(editText1.text.toString(), editText2.text.toString())
195+
if (!result.first) {
196+
Toast.makeText(
197+
requireContext(),
198+
requireContext().getString(result.second),
199+
Toast.LENGTH_SHORT,
200+
).show()
201+
return@setOnClickListener
202+
}
203+
193204
dataUtils.removeBook(position[p]!!)
194205
position.remove(p)
195206
bookmarksList?.removePreference(p)

app/src/main/java/com/amaze/filemanager/utils/DataUtils.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,25 @@ public void registerOnDataChangedListener(DataChangeListener l) {
284284
clear();
285285
}
286286

287-
int contains(String a, ArrayList<String[]> b) {
288-
int i = 0;
289-
for (String[] x : b) {
290-
if (x[1].equals(a)) return i;
291-
i++;
292-
}
293-
return -1;
294-
}
295-
296-
int contains(String[] a, ArrayList<String[]> b) {
297-
if (b == null) return -1;
287+
/**
288+
* Searches pair list of {@code {name, path}} pairs for an entry that matches the given pair by
289+
* <em>either</em> name or path. An entry is considered pair match when its name (index 0) equals
290+
* {@code pair[0]} or its path (index 1) equals {@code pair[1]}.
291+
*
292+
* <p>Both should be unique since we cannot have two items with the same name. But this prevents
293+
* two bookmarks / servers pointing to the same path with different names.
294+
*
295+
* @param pair the {@code {name, path}} pair to search for
296+
* @param list the list to search; each element is expected to be pair two-element {@code {name,
297+
* path}} array. May be {@code null}.
298+
* @return the index of the first matching entry, or {@code -1} if {@code list} is {@code null} or
299+
* no entry matches
300+
*/
301+
int contains(String[] pair, ArrayList<String[]> list) {
302+
if (list == null) return -1;
298303
int i = 0;
299-
for (String[] x : b) {
300-
if (x[0].equals(a[0]) && x[1].equals(a[1])) return i;
304+
for (String[] item : list) {
305+
if (item[0].equals(pair[0]) || item[1].equals(pair[1])) return i;
301306
i++;
302307
}
303308
return -1;

0 commit comments

Comments
 (0)