From 770ec6050d5f273ec243f4080422391f6b1b972c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 29 May 2017 12:47:41 +0900 Subject: [PATCH] Set random folder id when creating new folder (fixes #895) --- .../activities/FolderActivity.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java index b710259c..b181c98b 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java @@ -34,6 +34,7 @@ import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.util.TextWatcherAdapter; import java.util.List; +import java.util.Random; import static android.support.v4.view.MarginLayoutParamsCompat.setMarginEnd; import static android.support.v4.view.MarginLayoutParamsCompat.setMarginStart; @@ -371,9 +372,24 @@ public class FolderActivity extends SyncthingActivity } } + private String generateRandomFolderId() { + char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray(); + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < 10; i++) { + if (i == 5) { + sb.append("-"); + } + char c = chars[random.nextInt(chars.length)]; + sb.append(c); + } + return sb.toString(); + } private void initFolder() { mFolder = new Folder(); - mFolder.id = getIntent().getStringExtra(EXTRA_FOLDER_ID); + mFolder.id = (getIntent().hasExtra(EXTRA_FOLDER_ID)) + ? getIntent().getStringExtra(EXTRA_FOLDER_ID) + : generateRandomFolderId(); mFolder.label = getIntent().getStringExtra(EXTRA_FOLDER_LABEL); if (Build.VERSION.SDK_INT != Build.VERSION_CODES.M) { // Scan every 3 days (in case inotify dropped some changes)