Added check for file existence and time of modification

This commit is contained in:
madwasp79 2019-02-27 18:18:43 +02:00
parent ec0bb3211b
commit 05ed6b38b3
2 changed files with 17 additions and 12 deletions

View file

@ -35,7 +35,6 @@ import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.List; import java.util.List;
public class GpxSelectionHelper { public class GpxSelectionHelper {
@ -43,7 +42,7 @@ public class GpxSelectionHelper {
private static final String CURRENT_TRACK = "currentTrack"; private static final String CURRENT_TRACK = "currentTrack";
private static final String FILE = "file"; private static final String FILE = "file";
private static final String BACKUP = "backup"; private static final String BACKUP = "backup";
private static final String BACKUPTIME = "backupTime"; private static final String BACKUPMODIFIEDTIME = "backupTime";
private static final String COLOR = "color"; private static final String COLOR = "color";
private static final String SELECTED_BY_USER = "selected_by_user"; private static final String SELECTED_BY_USER = "selected_by_user";
private OsmandApplication app; private OsmandApplication app;
@ -62,7 +61,7 @@ public class GpxSelectionHelper {
public void clearAllGpxFileToShow() { public void clearAllGpxFileToShow() {
selectedGpxFilesBackUp.clear(); selectedGpxFilesBackUp.clear();
for(SelectedGpxFile s : selectedGPXFiles) { for(SelectedGpxFile s : selectedGPXFiles) {
selectedGpxFilesBackUp.put(s.gpxFile, System.currentTimeMillis()); selectedGpxFilesBackUp.put(s.gpxFile, s.modifiedTime);
} }
selectedGPXFiles.clear(); selectedGPXFiles.clear();
saveCurrentSelections(); saveCurrentSelections();
@ -71,17 +70,21 @@ public class GpxSelectionHelper {
public void restoreSelectedGpxFiles() { public void restoreSelectedGpxFiles() {
if (!selectedGpxFilesBackUp.isEmpty()) { if (!selectedGpxFilesBackUp.isEmpty()) {
for(Map.Entry<GPXFile, Long> gpx : selectedGpxFilesBackUp.entrySet()) { for(Map.Entry<GPXFile, Long> gpx : selectedGpxFilesBackUp.entrySet()) {
LOG.debug("file path: " + gpx.getKey().path);
if (!Algorithms.isEmpty(gpx.getKey().path)) {
File file = new File(gpx.getKey().path); File file = new File(gpx.getKey().path);
if (file.exists() && !file.isDirectory() && file.lastModified() < gpx.getValue()) { if (file.exists() && !file.isDirectory()) {
if (file.lastModified() > gpx.getValue()) {
GPXUtilities.loadGPXFile(file);
}
selectGpxFile(gpx.getKey(), true, false); selectGpxFile(gpx.getKey(), true, false);
} }
} }
}
saveCurrentSelections(); saveCurrentSelections();
} }
} }
public boolean isShowingAnyGpxFiles() { public boolean isShowingAnyGpxFiles() {
return !selectedGPXFiles.isEmpty(); return !selectedGPXFiles.isEmpty();
} }
@ -527,7 +530,7 @@ public class GpxSelectionHelper {
} }
obj.put(SELECTED_BY_USER, true); obj.put(SELECTED_BY_USER, true);
obj.put(BACKUP, true); obj.put(BACKUP, true);
obj.put(BACKUPTIME, s.getValue()); obj.put(BACKUPMODIFIEDTIME, s.getValue());
ar.put(obj); ar.put(obj);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -163,11 +163,13 @@ public class ConfigureMapMenu {
} }
Map<GPXUtilities.GPXFile, Long> fls = selectedGpxHelper.getSelectedGpxFilesBackUp(); Map<GPXUtilities.GPXFile, Long> fls = selectedGpxHelper.getSelectedGpxFilesBackUp();
for(Map.Entry<GPXUtilities.GPXFile, Long> f : fls.entrySet()) { for(Map.Entry<GPXUtilities.GPXFile, Long> f : fls.entrySet()) {
if(!Algorithms.isEmpty(f.getKey().path)) {
File file = new File(f.getKey().path); File file = new File(f.getKey().path);
if(file.exists() && !file.isDirectory()) { if(file.exists() && !file.isDirectory()) {
files.add(f.getKey().path); files.add(f.getKey().path);
} }
} }
}
return files; return files;
} }