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 TEMP_DIR = "temp/";
public static final String ROUTING_PROFILES_DIR = "routing/"; public static final String ROUTING_PROFILES_DIR = "routing/";
public static final String PLUGINS_DIR = "plugins/"; 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; 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()) { if (voiceDir.canRead()) {
//First list TTS files, they are preferred //First list TTS files, they are preferred
for (File voiceF : listFilesSorted(voiceDir)) { for (File voiceF : listFilesSorted(voiceDir)) {

View file

@ -13,5 +13,7 @@ public enum ExportSettingsType {
GLOBAL, GLOBAL,
OSM_NOTES, OSM_NOTES,
OSM_EDITS, 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), OBF_MAP("obf_map", IndexConstants.MAPS_PATH),
TILES_MAP("tiles_map", IndexConstants.TILES_INDEX_DIR), TILES_MAP("tiles_map", IndexConstants.TILES_INDEX_DIR),
GPX("gpx", IndexConstants.GPX_INDEX_DIR), GPX("gpx", IndexConstants.GPX_INDEX_DIR),
TTS_VOICE("tts_voice", IndexConstants.VOICE_INDEX_DIR),
VOICE("voice", IndexConstants.VOICE_INDEX_DIR), VOICE("voice", IndexConstants.VOICE_INDEX_DIR),
TRAVEL("travel", IndexConstants.WIKIVOYAGE_INDEX_DIR), TRAVEL("travel", IndexConstants.WIKIVOYAGE_INDEX_DIR),
MULTIMEDIA_NOTES("multimedia_notes", IndexConstants.AV_INDEX_DIR); MULTIMEDIA_NOTES("multimedia_notes", IndexConstants.AV_INDEX_DIR);
@ -66,6 +67,11 @@ public class FileSettingsItem extends StreamSettingsItem {
return null; 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) { public static FileSubtype getSubtypeByFileName(@NonNull String fileName) {
String name = fileName; String name = fileName;
if (fileName.startsWith(File.separator)) { if (fileName.startsWith(File.separator)) {
@ -91,6 +97,11 @@ public class FileSettingsItem extends StreamSettingsItem {
return subtype; return subtype;
} }
break; break;
case TTS_VOICE:
if (name.startsWith(subtype.subtypeFolder) && name.endsWith(IndexConstants.TTS_DIR_SUFFIX)) {
return subtype;
}
break;
default: default:
if (name.startsWith(subtype.subtypeFolder)) { if (name.startsWith(subtype.subtypeFolder)) {
return subtype; return subtype;
@ -109,9 +120,10 @@ public class FileSettingsItem extends StreamSettingsItem {
} }
protected File file; protected File file;
private File appPath; private final File appPath;
protected FileSubtype subtype; protected FileSubtype subtype;
private long size; private long size;
private boolean subFolders;
public FileSettingsItem(@NonNull OsmandApplication app, @NonNull File file) throws IllegalArgumentException { public FileSettingsItem(@NonNull OsmandApplication app, @NonNull File file) throws IllegalArgumentException {
super(app, file.getPath().replace(app.getAppPath(null).getPath(), "")); super(app, file.getPath().replace(app.getAppPath(null).getPath(), ""));
@ -198,6 +210,14 @@ public class FileSettingsItem extends StreamSettingsItem {
this.size = size; this.size = size;
} }
public boolean isSubFolders() {
return subFolders;
}
public void setSubFolders(boolean subFolders) {
this.subFolders = subFolders;
}
@NonNull @NonNull
public File getFile() { public File getFile() {
return file; return file;
@ -259,7 +279,9 @@ public class FileSettingsItem extends StreamSettingsItem {
@Override @Override
public SettingsItemWriter<? extends SettingsItem> getWriter() { public SettingsItemWriter<? extends SettingsItem> getWriter() {
try { try {
if (!file.isDirectory()) {
setInputStream(new FileInputStream(file)); setInputStream(new FileInputStream(file));
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
warnings.add(app.getString(R.string.settings_item_read_error, file.getName())); 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); 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.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -67,6 +68,10 @@ class SettingsExporter {
if (Algorithms.isEmpty(fileName)) { if (Algorithms.isEmpty(fileName)) {
fileName = item.getDefaultFileName(); 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); ZipEntry entry = new ZipEntry(fileName);
zos.putNextEntry(entry); zos.putNextEntry(entry);
writer.writeToStream(zos); 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 { private JSONObject createItemsJson() throws JSONException {
JSONObject json = new JSONObject(); 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.IndexConstants.OSMAND_SETTINGS_FILE_EXT;
import static net.osmand.plus.activities.LocalIndexHelper.*; import static net.osmand.plus.activities.LocalIndexHelper.*;
import static net.osmand.plus.settings.backend.backup.FileSettingsItem.*;
/* /*
Usage: Usage:
@ -535,9 +536,41 @@ public class SettingsHelper {
if (!files.isEmpty()) { if (!files.isEmpty()) {
dataList.put(ExportSettingsType.OFFLINE_MAPS, files); 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; 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() { private List<File> getLocalMapFiles() {
List<File> files = new ArrayList<>(); List<File> files = new ArrayList<>();
LocalIndexHelper helper = new LocalIndexHelper(app); LocalIndexHelper helper = new LocalIndexHelper(app);
@ -548,7 +581,8 @@ public class SettingsHelper {
}); });
for (LocalIndexInfo map : localMapFileList) { for (LocalIndexInfo map : localMapFileList) {
File file = new File(map.getPathToData()); 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); files.add(file);
} }
} }
@ -574,7 +608,11 @@ public class SettingsHelper {
tileSourceTemplates.add((ITileSource) object); tileSourceTemplates.add((ITileSource) object);
} else if (object instanceof File) { } else if (object instanceof File) {
try { 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) { } catch (IllegalArgumentException e) {
LOG.warn("Trying to export unsuported file type", e); LOG.warn("Trying to export unsuported file type", e);
} }
@ -627,6 +665,8 @@ public class SettingsHelper {
List<File> renderFilesList = new ArrayList<>(); List<File> renderFilesList = new ArrayList<>();
List<File> multimediaFilesList = new ArrayList<>(); List<File> multimediaFilesList = new ArrayList<>();
List<File> tracksFilesList = new ArrayList<>(); List<File> tracksFilesList = new ArrayList<>();
List<File> ttsVoiceFilesList = new ArrayList<>();
List<File> voiceFilesList = new ArrayList<>();
List<FileSettingsItem> mapFilesList = new ArrayList<>(); List<FileSettingsItem> mapFilesList = new ArrayList<>();
List<AvoidRoadInfo> avoidRoads = new ArrayList<>(); List<AvoidRoadInfo> avoidRoads = new ArrayList<>();
List<GlobalSettingsItem> globalSettingsItems = new ArrayList<>(); List<GlobalSettingsItem> globalSettingsItems = new ArrayList<>();
@ -639,19 +679,23 @@ public class SettingsHelper {
break; break;
case FILE: case FILE:
FileSettingsItem fileItem = (FileSettingsItem) item; FileSettingsItem fileItem = (FileSettingsItem) item;
if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.RENDERING_STYLE) { if (fileItem.getSubtype() == FileSubtype.RENDERING_STYLE) {
renderFilesList.add(fileItem.getFile()); renderFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.ROUTING_CONFIG) { } else if (fileItem.getSubtype() == FileSubtype.ROUTING_CONFIG) {
routingFilesList.add(fileItem.getFile()); routingFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.MULTIMEDIA_NOTES) { } else if (fileItem.getSubtype() == FileSubtype.MULTIMEDIA_NOTES) {
multimediaFilesList.add(fileItem.getFile()); multimediaFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.GPX) { } else if (fileItem.getSubtype() == FileSubtype.GPX) {
tracksFilesList.add(fileItem.getFile()); tracksFilesList.add(fileItem.getFile());
} else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.OBF_MAP } else if (fileItem.getSubtype() == FileSubtype.OBF_MAP
|| fileItem.getSubtype() == FileSettingsItem.FileSubtype.WIKI_MAP || fileItem.getSubtype() == FileSubtype.WIKI_MAP
|| fileItem.getSubtype() == FileSettingsItem.FileSubtype.SRTM_MAP || fileItem.getSubtype() == FileSubtype.SRTM_MAP
|| fileItem.getSubtype() == FileSettingsItem.FileSubtype.TILES_MAP) { || fileItem.getSubtype() == FileSubtype.TILES_MAP) {
mapFilesList.add(fileItem); 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; break;
case QUICK_ACTIONS: case QUICK_ACTIONS:
@ -749,6 +793,12 @@ public class SettingsHelper {
if (!mapFilesList.isEmpty()) { if (!mapFilesList.isEmpty()) {
settingsToOperate.put(ExportSettingsType.OFFLINE_MAPS, mapFilesList); 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; return settingsToOperate;
} }
} }

View file

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

View file

@ -287,8 +287,7 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
title.setText(FileNameTranslationHelper.getFileName(app, title.setText(FileNameTranslationHelper.getFileName(app,
app.getResourceManager().getOsmandRegions(), app.getResourceManager().getOsmandRegions(),
file.getName())); file.getName()));
FileSubtype subtype = FileSubtype.getSubtypeByFileName(file.getPath().replace( FileSubtype subtype = FileSubtype.getSubtypeByPath(app, file.getPath());
app.getAppPath(null).getPath(), ""));
switch (subtype) { switch (subtype) {
case SRTM_MAP: case SRTM_MAP:
iconId = R.drawable.ic_plugin_srtm; iconId = R.drawable.ic_plugin_srtm;
@ -303,6 +302,18 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
subText.setText(AndroidUtils.formatSize(app, size)); subText.setText(AndroidUtils.formatSize(app, size));
subText.setVisibility(View.VISIBLE); subText.setVisibility(View.VISIBLE);
break; 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: default:
return child; return child;
} }
@ -393,6 +404,10 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
return R.string.osm_edit_modified_poi; return R.string.osm_edit_modified_poi;
case OFFLINE_MAPS: case OFFLINE_MAPS:
return R.string.shared_string_local_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: default:
return R.string.access_empty_list; return R.string.access_empty_list;
} }