Data storage refactoring, step 3

This commit is contained in:
nazar-kutz 2021-02-02 09:41:34 +02:00
parent 7041c4ecc9
commit 8f302f8558
4 changed files with 41 additions and 38 deletions

View file

@ -462,9 +462,9 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
}
@Override
public void onFinishUpdating(String taskKey) {
public void onFinishUpdating(String tag) {
updateAllSettings();
if (taskKey != null && taskKey.equals(TILES_MEMORY)) {
if (tag != null && tag.equals(TILES_MEMORY)) {
app.getSettings().OSMAND_USAGE_SPACE.set(dataStorageHelper.getTotalUsedBytes());
}
}

View file

@ -67,6 +67,11 @@ public class DataStorageHelper {
}
private void prepareData() {
initStorageItems();
initUsedMemoryItems();
}
private void initStorageItems() {
OsmandSettings settings = app.getSettings();
if (settings.getExternalStorageDirectoryTypeV19() >= 0) {
currentStorageType = settings.getExternalStorageDirectoryTypeV19();
@ -169,20 +174,18 @@ public class DataStorageHelper {
if (currentDataStorage == null) {
currentDataStorage = manuallySpecified;
}
initMemoryUsed();
}
private void initMemoryUsed() {
private void initUsedMemoryItems() {
mapsMemory = MemoryItem.builder()
.setKey(MAPS_MEMORY)
.setExtensions(IndexConstants.BINARY_MAP_INDEX_EXT)
.setDirectories(
createDirectory((MAPS_PATH), false, EXTENSIONS, false),
createDirectory((ROADS_INDEX_DIR), true, EXTENSIONS, false),
createDirectory((WIKI_INDEX_DIR), true, EXTENSIONS, false),
createDirectory((WIKIVOYAGE_INDEX_DIR), true, EXTENSIONS, false),
createDirectory((BACKUP_INDEX_DIR), true, EXTENSIONS, false))
createDirectory(MAPS_PATH, false, EXTENSIONS, true),
createDirectory(ROADS_INDEX_DIR, true, EXTENSIONS, true),
createDirectory(WIKI_INDEX_DIR, true, EXTENSIONS, true),
createDirectory(WIKIVOYAGE_INDEX_DIR, true, EXTENSIONS, true),
createDirectory(BACKUP_INDEX_DIR, true, EXTENSIONS, true))
.createItem();
memoryItems.add(mapsMemory);
@ -190,8 +193,8 @@ public class DataStorageHelper {
.setKey(TERRAIN_MEMORY)
.setExtensions(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)
.setDirectories(
createDirectory((SRTM_INDEX_DIR), true, EXTENSIONS, false),
createDirectory((TILES_INDEX_DIR), false, PREFIX, true))
createDirectory(SRTM_INDEX_DIR, true, EXTENSIONS, true),
createDirectory(TILES_INDEX_DIR, false, PREFIX, false))
.setPrefixes("Hillshade")
.createItem();
memoryItems.add(terrainMemory);
@ -200,7 +203,7 @@ public class DataStorageHelper {
.setKey(TRACKS_MEMORY)
// .setExtensions(IndexConstants.GPX_FILE_EXT, ".gpx.bz2")
.setDirectories(
createDirectory((GPX_INDEX_DIR), true, EXTENSIONS, false))
createDirectory(GPX_INDEX_DIR, true, EXTENSIONS, true))
.createItem();
memoryItems.add(tracksMemory);
@ -208,7 +211,7 @@ public class DataStorageHelper {
.setKey(NOTES_MEMORY)
// .setExtensions("")
.setDirectories(
createDirectory((AV_INDEX_DIR), true, EXTENSIONS, false))
createDirectory(AV_INDEX_DIR, true, EXTENSIONS, true))
.createItem();
memoryItems.add(notesMemory);
@ -216,7 +219,7 @@ public class DataStorageHelper {
.setKey(TILES_MEMORY)
// .setExtensions("")
.setDirectories(
createDirectory((TILES_INDEX_DIR), true, EXTENSIONS, false))
createDirectory(TILES_INDEX_DIR, true, EXTENSIONS, true))
.createItem();
memoryItems.add(tilesMemory);
@ -264,9 +267,9 @@ public class DataStorageHelper {
return memoryItems;
}
public RefreshUsedMemoryTask calculateMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener) {
public RefreshUsedMemoryTask calculateMemoryUsedInfo(UpdateMemoryInfoUIAdapter uiAdapter) {
File rootDir = new File(currentStoragePath);
RefreshUsedMemoryTask task = new RefreshUsedMemoryTask(listener, otherMemory, rootDir, null, null, OTHER_MEMORY);
RefreshUsedMemoryTask task = new RefreshUsedMemoryTask(uiAdapter, otherMemory, rootDir, null, null, OTHER_MEMORY);
task.execute(mapsMemory, terrainMemory, tracksMemory, notesMemory);
return task;
}
@ -292,9 +295,9 @@ public class DataStorageHelper {
public DirectoryItem createDirectory(@NonNull String relativePath,
boolean processInternalDirectories,
CheckingType checkingType,
boolean skipUnmatchedInDirectory) {
boolean addUnmatchedToOtherMemory) {
String path = app.getAppPath(relativePath).getAbsolutePath();
return new DirectoryItem(path, processInternalDirectories, checkingType, skipUnmatchedInDirectory);
return new DirectoryItem(path, processInternalDirectories, checkingType, addUnmatchedToOtherMemory);
}
public static String getFormattedMemoryInfo(long bytes, String[] formatStrings) {
@ -312,7 +315,7 @@ public class DataStorageHelper {
void onMemoryInfoUpdate();
void onFinishUpdating(String taskKey);
void onFinishUpdating(String tag);
}
}

View file

@ -5,7 +5,7 @@ public class DirectoryItem {
private final String absolutePath;
private final boolean processInternalDirectories;
private final CheckingType checkingType;
private final boolean skipUnmatchedInDirectory;
private final boolean addUnmatchedToOtherMemory;
public enum CheckingType {
EXTENSIONS,
@ -15,11 +15,11 @@ public class DirectoryItem {
public DirectoryItem(String absolutePath,
boolean processInternalDirectories,
CheckingType checkingType,
boolean skipUnmatchedInDirectory) {
boolean addUnmatchedToOtherMemory) {
this.absolutePath = absolutePath;
this.processInternalDirectories = processInternalDirectories;
this.checkingType = checkingType;
this.skipUnmatchedInDirectory = skipUnmatchedInDirectory;
this.addUnmatchedToOtherMemory = addUnmatchedToOtherMemory;
}
public String getAbsolutePath() {
@ -34,7 +34,7 @@ public class DirectoryItem {
return checkingType;
}
public boolean shouldSkipUnmatchedInDirectory() {
return skipUnmatchedInDirectory;
public boolean shouldAddUnmatchedToOtherMemory() {
return addUnmatchedToOtherMemory;
}
}

View file

@ -97,16 +97,16 @@ public class RefreshUsedMemoryTask extends AsyncTask<MemoryItem, Void, Void> {
@NonNull MemoryItem... items) {
String directoryPath = directory.getAbsolutePath();
for (MemoryItem memoryItem : items) {
DirectoryItem[] allowedDirectories = memoryItem.getDirectories();
if (allowedDirectories != null) {
for (DirectoryItem allowedDir : allowedDirectories) {
String allowedDirPath = allowedDir.getAbsolutePath();
DirectoryItem[] targetDirectories = memoryItem.getDirectories();
if (targetDirectories != null) {
for (DirectoryItem targetDirectory : targetDirectories) {
String allowedDirPath = targetDirectory.getAbsolutePath();
if (objectEquals(directoryPath, allowedDirPath)
|| (directoryPath.startsWith(allowedDirPath))) {
if (allowedDir.shouldProcessInternalDirectories()) {
if (targetDirectory.shouldProcessInternalDirectories()) {
calculateMultiTypes(directory, items);
return;
} else if (allowedDir.shouldSkipUnmatchedInDirectory()) {
} else if (!targetDirectory.shouldAddUnmatchedToOtherMemory()) {
return;
}
}
@ -121,16 +121,16 @@ public class RefreshUsedMemoryTask extends AsyncTask<MemoryItem, Void, Void> {
@NonNull File file,
@NonNull MemoryItem... items) {
for (MemoryItem item : items) {
DirectoryItem[] allowedDirectories = item.getDirectories();
if (allowedDirectories == null) continue;
DirectoryItem[] targetDirectories = item.getDirectories();
if (targetDirectories == null) continue;
String rootDirPath = rootDir.getAbsolutePath();
for (DirectoryItem allowedDir : allowedDirectories) {
String allowedDirPath = allowedDir.getAbsolutePath();
boolean processInternal = allowedDir.shouldProcessInternalDirectories();
for (DirectoryItem targetDirectory : targetDirectories) {
String allowedDirPath = targetDirectory.getAbsolutePath();
boolean processInternal = targetDirectory.shouldProcessInternalDirectories();
if (objectEquals(rootDirPath, allowedDirPath)
|| (rootDirPath.startsWith(allowedDirPath) && processInternal)) {
CheckingType checkingType = allowedDir.getCheckingType();
CheckingType checkingType = targetDirectory.getCheckingType();
switch (checkingType) {
case EXTENSIONS: {
if (isSuitableExtension(file, item)) {
@ -147,7 +147,7 @@ public class RefreshUsedMemoryTask extends AsyncTask<MemoryItem, Void, Void> {
break;
}
}
if (allowedDir.shouldSkipUnmatchedInDirectory()) {
if (!targetDirectory.shouldAddUnmatchedToOtherMemory()) {
return;
}
}