From 79b44602e00813cdc5b02a4ce28027d0c891819f Mon Sep 17 00:00:00 2001 From: Gabriele M Date: Sat, 8 Jul 2017 21:39:10 +0200 Subject: [PATCH] Allow to have different updates using the same filename --- .../updater/controller/UpdaterController.java | 4 ++++ src/org/lineageos/updater/misc/Utils.java | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/org/lineageos/updater/controller/UpdaterController.java b/src/org/lineageos/updater/controller/UpdaterController.java index 92549b6..3e51565 100644 --- a/src/org/lineageos/updater/controller/UpdaterController.java +++ b/src/org/lineageos/updater/controller/UpdaterController.java @@ -356,6 +356,10 @@ public class UpdaterController implements UpdaterControllerInt { } UpdateDownload update = mDownloads.get(downloadId).mUpdate; File destination = new File(mDownloadRoot, update.getName()); + if (destination.exists()) { + destination = Utils.appendSequentialNumber(destination); + Log.d(TAG, "Changing name with " + destination.getName()); + } update.setFile(destination); DownloadClient downloadClient = new DownloadClient.Builder() .setUrl(update.getDownloadUrl()) diff --git a/src/org/lineageos/updater/misc/Utils.java b/src/org/lineageos/updater/misc/Utils.java index 87e7fd5..cf91443 100644 --- a/src/org/lineageos/updater/misc/Utils.java +++ b/src/org/lineageos/updater/misc/Utils.java @@ -247,4 +247,25 @@ public class Utils { preferences.edit().putBoolean(DOWNLOADS_CLEANUP_DONE, true).apply(); } + + public static File appendSequentialNumber(final File file) { + String name; + String extension; + int extensionPosition = file.getName().lastIndexOf("."); + if (extensionPosition > 0) { + name = file.getName().substring(0, extensionPosition); + extension = file.getName().substring(extensionPosition); + } else { + name = file.getName(); + extension = ""; + } + final File parent = file.getParentFile(); + for (int i = 1; i < Integer.MAX_VALUE; i++) { + File newFile = new File(parent, name + "-" + i + extension); + if (!newFile.exists()) { + return newFile; + } + } + throw new IllegalStateException(); + } }