Check if file really exists before asking user to overwrite it

This commit is contained in:
Stypox 2021-03-17 20:28:15 +01:00
parent b78ac7d2e9
commit 21b8df0375
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
3 changed files with 30 additions and 16 deletions

View file

@ -757,15 +757,14 @@ public class DownloadDialog extends DialogFragment
return;
}
// check if is our file
// get state of potential mission referring to the same file
final MissionState state = downloadManager.checkForExistingMission(storage);
@StringRes
final int msgBtn;
@StringRes
final int msgBody;
@StringRes final int msgBtn;
@StringRes final int msgBody;
// this switch checks if there is already a mission referring to the same file
switch (state) {
case Finished:
case Finished: // there is already a finished mission
msgBtn = R.string.overwrite;
msgBody = R.string.overwrite_finished_warning;
break;
@ -777,7 +776,7 @@ public class DownloadDialog extends DialogFragment
msgBtn = R.string.generate_unique_name;
msgBody = R.string.download_already_running;
break;
case None:
case None: // there is no mission referring to the same file
if (mainStorage == null) {
// This part is called if:
// * using SAF on older android version
@ -812,7 +811,7 @@ public class DownloadDialog extends DialogFragment
msgBody = R.string.overwrite_unrelated_warning;
break;
default:
return;
return; // unreachable
}
final AlertDialog.Builder askDialog = new AlertDialog.Builder(context)

View file

@ -290,11 +290,10 @@ public class StoredFileHelper implements Serializable {
}
// WARNING: DocumentFile.exists() and DocumentFile.isFile() methods are slow
final boolean exists = docFile == null ? ioFile.exists() : docFile.exists();
// ¿docFile.isVirtual() means is no-physical?
final boolean isFile = docFile == null ? ioFile.isFile() : docFile.isFile();
return exists && isFile;
// docFile.isVirtual() means it is non-physical?
return docFile == null
? (ioFile.exists() && ioFile.isFile())
: (docFile.exists() && docFile.isFile());
}
public boolean create() {

View file

@ -106,7 +106,8 @@ public class DownloadManager {
}
/**
* Loads finished missions from the data source
* Loads finished missions from the data source and forgets finished missions whose file does
* not exist anymore.
*/
private ArrayList<FinishedMission> loadFinishedMissions() {
ArrayList<FinishedMission> finishedMissions = mFinishedMissionStore.loadFinishedMissions();
@ -331,14 +332,29 @@ public class DownloadManager {
}
/**
* Get a finished mission by its path
* Get the index into {@link #mMissionsFinished} of a finished mission by its path, return
* {@code -1} if there is no such mission. This function also checks if the matched mission's
* file exists, and, if it does not, the related mission is forgotten about (like in {@link
* #loadFinishedMissions()}) and {@code -1} is returned.
*
* @param storage where the file possible is stored
* @param storage where the file would be stored
* @return the mission index or -1 if no such mission exists
*/
private int getFinishedMissionIndex(StoredFileHelper storage) {
for (int i = 0; i < mMissionsFinished.size(); i++) {
if (mMissionsFinished.get(i).storage.equals(storage)) {
// If the file does not exist the mission is not valid anymore. Also checking if
// length == 0 since the file picker may create an empty file before yielding it,
// but that does not mean the file really belonged to a previous mission.
if (!storage.existsAsFile() || storage.length() == 0) {
if (DEBUG) {
Log.d(TAG, "matched downloaded file removed: " + storage.getName());
}
mFinishedMissionStore.deleteMission(mMissionsFinished.get(i));
mMissionsFinished.remove(i);
return -1; // finished mission whose associated file was removed
}
return i;
}
}