Export voice

This commit is contained in:
Dima-1 2020-10-29 18:09:09 +02:00
parent 248bc052e8
commit b5f06cf3c1
8 changed files with 143 additions and 21 deletions

View file

@ -77,4 +77,6 @@ public class IndexConstants {
public static final String TEMP_DIR = "temp/";
public static final String ROUTING_PROFILES_DIR = "routing/";
public static final String PLUGINS_DIR = "plugins/";
public static final String TTS_DIR_SUFFIX = "-tts";
}

View file

@ -214,7 +214,7 @@ public class LocalIndexHelper {
return result;
}
private void loadVoiceData(File voiceDir, List<LocalIndexInfo> result, boolean backup, AbstractLoadLocalIndexTask loadTask) {
public void loadVoiceData(File voiceDir, List<LocalIndexInfo> result, boolean backup, AbstractLoadLocalIndexTask loadTask) {
if (voiceDir.canRead()) {
//First list TTS files, they are preferred
for (File voiceF : listFilesSorted(voiceDir)) {

View file

@ -13,5 +13,7 @@ public enum ExportSettingsType {
GLOBAL,
OSM_NOTES,
OSM_EDITS,
OFFLINE_MAPS
OFFLINE_MAPS,
TTS_VOICE,
VOICE
}

View file

@ -33,6 +33,7 @@ public class FileSettingsItem extends StreamSettingsItem {
OBF_MAP("obf_map", IndexConstants.MAPS_PATH),
TILES_MAP("tiles_map", IndexConstants.TILES_INDEX_DIR),
GPX("gpx", IndexConstants.GPX_INDEX_DIR),
TTS_VOICE("tts_voice", IndexConstants.VOICE_INDEX_DIR),
VOICE("voice", IndexConstants.VOICE_INDEX_DIR),
TRAVEL("travel", IndexConstants.WIKIVOYAGE_INDEX_DIR),
MULTIMEDIA_NOTES("multimedia_notes", IndexConstants.AV_INDEX_DIR);
@ -66,6 +67,11 @@ public class FileSettingsItem extends StreamSettingsItem {
return null;
}
public static FileSubtype getSubtypeByPath(@NonNull OsmandApplication app, @NonNull String fileName) {
fileName = fileName.replace(app.getAppPath(null).getPath(), "");
return getSubtypeByFileName(fileName);
}
public static FileSubtype getSubtypeByFileName(@NonNull String fileName) {
String name = fileName;
if (fileName.startsWith(File.separator)) {
@ -91,6 +97,11 @@ public class FileSettingsItem extends StreamSettingsItem {
return subtype;
}
break;
case TTS_VOICE:
if (name.startsWith(subtype.subtypeFolder) && name.endsWith(IndexConstants.TTS_DIR_SUFFIX)) {
return subtype;
}
break;
default:
if (name.startsWith(subtype.subtypeFolder)) {
return subtype;
@ -109,9 +120,10 @@ public class FileSettingsItem extends StreamSettingsItem {
}
protected File file;
private File appPath;
private final File appPath;
protected FileSubtype subtype;
private long size;
private boolean subFolders;
public FileSettingsItem(@NonNull OsmandApplication app, @NonNull File file) throws IllegalArgumentException {
super(app, file.getPath().replace(app.getAppPath(null).getPath(), ""));
@ -198,6 +210,14 @@ public class FileSettingsItem extends StreamSettingsItem {
this.size = size;
}
public boolean isSubFolders() {
return subFolders;
}
public void setSubFolders(boolean subFolders) {
this.subFolders = subFolders;
}
@NonNull
public File getFile() {
return file;
@ -259,7 +279,9 @@ public class FileSettingsItem extends StreamSettingsItem {
@Override
public SettingsItemWriter<? extends SettingsItem> getWriter() {
try {
if (!file.isDirectory()) {
setInputStream(new FileInputStream(file));
}
} catch (FileNotFoundException e) {
warnings.add(app.getString(R.string.settings_item_read_error, file.getName()));
SettingsHelper.LOG.error("Failed to set input stream from file: " + file.getName(), e);

View file

@ -8,6 +8,7 @@ import org.json.JSONObject;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@ -67,6 +68,10 @@ class SettingsExporter {
if (Algorithms.isEmpty(fileName)) {
fileName = item.getDefaultFileName();
}
if (item instanceof FileSettingsItem && ((FileSettingsItem) item).isSubFolders()) {
File file = ((FileSettingsItem) item).getFile();
zipDirsWithFiles(file, writer, zos);
} else {
ZipEntry entry = new ZipEntry(fileName);
zos.putNextEntry(entry);
writer.writeToStream(zos);
@ -74,6 +79,32 @@ class SettingsExporter {
}
}
}
}
public void zipDirsWithFiles(File f, SettingsItemWriter<? extends SettingsItem> writer, ZipOutputStream zos)
throws IOException {
if (f == null) {
return;
}
if (f.isDirectory()) {
File[] fs = f.listFiles();
if (fs != null) {
for (File c : fs) {
zipDirsWithFiles(c, writer, zos);
}
}
} else {
FileSettingsItem item = (FileSettingsItem) writer.getItem();
String zipEntryName = Algorithms.isEmpty(item.getSubtype().getSubtypeFolder())
? f.getName()
: f.getPath().substring(f.getPath().indexOf(item.getSubtype().getSubtypeFolder()));
ZipEntry entry = new ZipEntry(zipEntryName);
zos.putNextEntry(entry);
item.setInputStream(new FileInputStream(f));
writer.writeToStream(zos);
zos.closeEntry();
}
}
private JSONObject createItemsJson() throws JSONException {
JSONObject json = new JSONObject();

View file

@ -48,6 +48,7 @@ import java.util.Set;
import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT;
import static net.osmand.plus.activities.LocalIndexHelper.*;
import static net.osmand.plus.settings.backend.backup.FileSettingsItem.*;
/*
Usage:
@ -535,9 +536,41 @@ public class SettingsHelper {
if (!files.isEmpty()) {
dataList.put(ExportSettingsType.OFFLINE_MAPS, files);
}
List<LocalIndexInfo> localVoiceFileList = getVoiceIndexInfo();
files = getFilesByType(localVoiceFileList, LocalIndexType.TTS_VOICE_DATA);
if (!files.isEmpty()) {
dataList.put(ExportSettingsType.TTS_VOICE, files);
}
files = getFilesByType(localVoiceFileList, LocalIndexType.VOICE_DATA);
if (!files.isEmpty()) {
dataList.put(ExportSettingsType.VOICE, files);
}
return dataList;
}
private List<LocalIndexInfo> getVoiceIndexInfo() {
LocalIndexHelper helper = new LocalIndexHelper(app);
List<LocalIndexInfo> localVoiceInfoList = new ArrayList<>();
helper.loadVoiceData(app.getAppPath(IndexConstants.VOICE_INDEX_DIR), localVoiceInfoList, false,
new AbstractLoadLocalIndexTask() {
@Override
public void loadFile(LocalIndexInfo... loaded) {
}
});
return localVoiceInfoList;
}
private List<File> getFilesByType(List<LocalIndexInfo> localVoiceFileList, LocalIndexType localIndexType) {
List<File> files = new ArrayList<>();
for (LocalIndexInfo map : localVoiceFileList) {
File file = new File(map.getPathToData());
if (file.exists() && map.getType() == localIndexType) {
files.add(file);
}
}
return files;
}
private List<File> getLocalMapFiles() {
List<File> files = new ArrayList<>();
LocalIndexHelper helper = new LocalIndexHelper(app);
@ -548,7 +581,8 @@ public class SettingsHelper {
});
for (LocalIndexInfo map : localMapFileList) {
File file = new File(map.getPathToData());
if (file != null && file.exists() && map.getType() != LocalIndexType.TTS_VOICE_DATA) {
if (file.exists() && map.getType() != LocalIndexType.TTS_VOICE_DATA
&& map.getType() != LocalIndexType.VOICE_DATA) {
files.add(file);
}
}
@ -574,7 +608,11 @@ public class SettingsHelper {
tileSourceTemplates.add((ITileSource) object);
} else if (object instanceof File) {
try {
settingsItems.add(new FileSettingsItem(app, (File) object));
FileSettingsItem fileItem = new FileSettingsItem(app, (File) object);
settingsItems.add(fileItem);
if (FileSubtype.getSubtypeByPath(app, ((File) object).getPath()) == FileSubtype.VOICE) {
fileItem.setSubFolders(true);
}
} catch (IllegalArgumentException e) {
LOG.warn("Trying to export unsuported file type", e);
}
@ -627,6 +665,8 @@ public class SettingsHelper {
List<File> renderFilesList = new ArrayList<>();
List<File> multimediaFilesList = new ArrayList<>();
List<File> tracksFilesList = new ArrayList<>();
List<File> ttsVoiceFilesList = new ArrayList<>();
List<File> voiceFilesList = new ArrayList<>();
List<FileSettingsItem> mapFilesList = new ArrayList<>();
List<AvoidRoadInfo> avoidRoads = new ArrayList<>();
List<GlobalSettingsItem> globalSettingsItems = new ArrayList<>();
@ -639,19 +679,23 @@ public class SettingsHelper {
break;
case FILE:
FileSettingsItem fileItem = (FileSettingsItem) item;
if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.RENDERING_STYLE) {
if (fileItem.getSubtype() == FileSubtype.RENDERING_STYLE) {
renderFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.ROUTING_CONFIG) {
} else if (fileItem.getSubtype() == FileSubtype.ROUTING_CONFIG) {
routingFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.MULTIMEDIA_NOTES) {
} else if (fileItem.getSubtype() == FileSubtype.MULTIMEDIA_NOTES) {
multimediaFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.GPX) {
} else if (fileItem.getSubtype() == FileSubtype.GPX) {
tracksFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.OBF_MAP
|| fileItem.getSubtype() == FileSettingsItem.FileSubtype.WIKI_MAP
|| fileItem.getSubtype() == FileSettingsItem.FileSubtype.SRTM_MAP
|| fileItem.getSubtype() == FileSettingsItem.FileSubtype.TILES_MAP) {
} else if (fileItem.getSubtype() == FileSubtype.OBF_MAP
|| fileItem.getSubtype() == FileSubtype.WIKI_MAP
|| fileItem.getSubtype() == FileSubtype.SRTM_MAP
|| fileItem.getSubtype() == FileSubtype.TILES_MAP) {
mapFilesList.add(fileItem);
} else if (fileItem.getSubtype() == FileSubtype.TTS_VOICE) {
ttsVoiceFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSubtype.VOICE) {
voiceFilesList.add(fileItem.getFile());
}
break;
case QUICK_ACTIONS:
@ -749,6 +793,12 @@ public class SettingsHelper {
if (!mapFilesList.isEmpty()) {
settingsToOperate.put(ExportSettingsType.OFFLINE_MAPS, mapFilesList);
}
if (!ttsVoiceFilesList.isEmpty()) {
settingsToOperate.put(ExportSettingsType.TTS_VOICE, ttsVoiceFilesList);
}
if (!voiceFilesList.isEmpty()) {
settingsToOperate.put(ExportSettingsType.VOICE, voiceFilesList);
}
return settingsToOperate;
}
}

View file

@ -88,7 +88,7 @@ public abstract class SettingsItem {
public boolean applyFileName(@NonNull String fileName) {
String n = getFileName();
return n != null && n.endsWith(fileName);
return n != null && (n.endsWith(fileName) || fileName.startsWith(n));
}
public boolean shouldReadOnCollecting() {

View file

@ -287,8 +287,7 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
title.setText(FileNameTranslationHelper.getFileName(app,
app.getResourceManager().getOsmandRegions(),
file.getName()));
FileSubtype subtype = FileSubtype.getSubtypeByFileName(file.getPath().replace(
app.getAppPath(null).getPath(), ""));
FileSubtype subtype = FileSubtype.getSubtypeByPath(app, file.getPath());
switch (subtype) {
case SRTM_MAP:
iconId = R.drawable.ic_plugin_srtm;
@ -303,6 +302,18 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
subText.setText(AndroidUtils.formatSize(app, size));
subText.setVisibility(View.VISIBLE);
break;
case TTS_VOICE:
file = (File) currentItem;
title.setText(FileNameTranslationHelper.getFileName(app,
app.getResourceManager().getOsmandRegions(),
file.getName()));
setupIcon(icon, R.drawable.ic_action_volume_up, itemSelected);
case VOICE:
file = (File) currentItem;
title.setText(FileNameTranslationHelper.getFileName(app,
app.getResourceManager().getOsmandRegions(),
file.getName()));
setupIcon(icon, R.drawable.ic_action_volume_up, itemSelected);
default:
return child;
}
@ -393,6 +404,10 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
return R.string.osm_edit_modified_poi;
case OFFLINE_MAPS:
return R.string.shared_string_local_maps;
case TTS_VOICE:
return R.string.local_indexes_cat_tts;
case VOICE:
return R.string.local_indexes_cat_voice;
default:
return R.string.access_empty_list;
}