Allow to have different updates using the same filename

This commit is contained in:
Gabriele M
2017-07-08 21:39:10 +02:00
parent 4979c4d78d
commit 79b44602e0
2 changed files with 25 additions and 0 deletions

View File

@@ -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())

View File

@@ -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();
}
}