Fix & refactoring

This commit is contained in:
Dima-1 2020-11-02 16:22:49 +02:00
parent 3a6e8ce977
commit f3c4b59362
21 changed files with 110 additions and 117 deletions

View file

@ -78,5 +78,5 @@ public class IndexConstants {
public static final String ROUTING_PROFILES_DIR = "routing/";
public static final String PLUGINS_DIR = "plugins/";
public static final String TTS_DIR_SUFFIX = "-tts";
public static final String VOICE_PROVIDER_SUFFIX = "-tts";
}

View file

@ -138,10 +138,10 @@ public class DownloadIndexesThread {
String setTts = null;
for (String s : OsmandSettings.TTS_AVAILABLE_VOICES) {
if (lng.startsWith(s)) {
setTts = s + "-tts";
setTts = s + IndexConstants.VOICE_PROVIDER_SUFFIX;
break;
} else if (lng.contains("," + s)) {
setTts = s + "-tts";
setTts = s + IndexConstants.VOICE_PROVIDER_SUFFIX;
}
}
if (setTts != null) {
@ -544,7 +544,7 @@ public class DownloadIndexesThread {
// validate enough space
if (asz != -1 && cs > asz) {
String breakDownloadMessage = app.getString(R.string.download_files_not_enough_space,
cs, asz);
String.valueOf(cs), String.valueOf(asz));
publishProgress(breakDownloadMessage);
return false;
}

View file

@ -8,9 +8,7 @@ import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import net.osmand.AndroidUtils;
@ -159,10 +157,13 @@ public class DownloadOsmandIndexesHelper {
List<AssetEntry> mapping = getBundledAssets(amanager);
for (AssetEntry asset : mapping) {
String target = asset.destination;
if (target.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS) && target.startsWith("voice/") && target.contains("-tts")) {
String lang = target.substring("voice/".length(), target.indexOf("-tts"));
File destFile = new File(voicePath, target.substring("voice/".length(),
target.indexOf("/", "voice/".length())) + "/" + lang + "_tts.js");
if (target.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)
&& target.startsWith(IndexConstants.VOICE_INDEX_DIR)
&& target.contains(IndexConstants.VOICE_PROVIDER_SUFFIX)) {
String lang = target.substring(IndexConstants.VOICE_INDEX_DIR.length(),
target.indexOf(IndexConstants.VOICE_PROVIDER_SUFFIX));
File destFile = new File(voicePath, target.substring(IndexConstants.VOICE_INDEX_DIR.length(),
target.indexOf("/", IndexConstants.VOICE_INDEX_DIR.length())) + "/" + lang + "_tts.js");
result.add(new AssetIndexItem(lang + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS,
"voice", date, dateModified, "0.1", destFile.length(), asset.source,
destFile.getPath(), DownloadActivityType.VOICE_FILE));

View file

@ -85,7 +85,7 @@ public class FileNameTranslationHelper {
public static String getVoiceName(Context ctx, String fileName) {
try {
String nm = fileName.replace('-', '_').replace(' ', '_');
if (nm.endsWith("_tts") || nm.endsWith("-tts")) {
if (nm.endsWith("_tts") || nm.endsWith(IndexConstants.VOICE_PROVIDER_SUFFIX)) {
nm = nm.substring(0, nm.length() - 4);
}
Field f = R.string.class.getField("lang_" + nm);

View file

@ -413,7 +413,7 @@ public class ResourceManager {
java.text.DateFormat dateFormat = getDateFormat();
for (File f : lf) {
if (f.isDirectory()) {
String lang = f.getName().replace("-tts", "");
String lang = f.getName().replace(IndexConstants.VOICE_PROVIDER_SUFFIX, "");
File conf = new File(f, lang + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS);
if (!conf.exists()) {
conf = new File(f, "_config.p");
@ -454,9 +454,10 @@ public class ResourceManager {
if (appPath.canWrite()) {
for (AssetEntry asset : assets) {
File jsFile = new File(appPath, asset.destination);
if (asset.destination.contains("-tts") && asset.destination
if (asset.destination.contains(IndexConstants.VOICE_PROVIDER_SUFFIX) && asset.destination
.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) {
File oggFile = new File(appPath, asset.destination.replace("-tts", ""));
File oggFile = new File(appPath, asset.destination.replace(
IndexConstants.VOICE_PROVIDER_SUFFIX, ""));
if (oggFile.getParentFile().exists() && !oggFile.exists()) {
copyAssets(context.getAssets(), asset.source, oggFile);
}

View file

@ -2543,7 +2543,7 @@ public class OsmandSettings {
Configuration config = ctx.getResources().getConfiguration();
for (String lang : TTS_AVAILABLE_VOICES) {
if (lang.equals(config.locale.getLanguage())) {
return lang + "-tts";
return lang + IndexConstants.VOICE_PROVIDER_SUFFIX;
}
}
return "en-tts";

View file

@ -64,7 +64,7 @@ public class DataSettingsItem extends StreamSettingsItem {
SettingsItemReader<? extends SettingsItem> getReader() {
return new StreamSettingsItemReader(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
public void readFromStream(@NonNull InputStream inputStream, File destination) throws IOException, IllegalArgumentException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[SettingsHelper.BUFFER];

View file

@ -16,6 +16,7 @@ import net.osmand.plus.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -145,7 +146,7 @@ public class FavoritesSettingsItem extends CollectionSettingsItem<FavoriteGroup>
return new SettingsItemReader<FavoritesSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IllegalArgumentException {
public void readFromStream(@NonNull InputStream inputStream, File destination) throws IllegalArgumentException {
GPXFile gpxFile = GPXUtilities.loadGPXFile(inputStream);
if (gpxFile.error != null) {
warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));

View file

@ -18,6 +18,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;
public class FileSettingsItem extends StreamSettingsItem {
@ -100,7 +101,7 @@ public class FileSettingsItem extends StreamSettingsItem {
}
break;
case TTS_VOICE:
if (name.startsWith(subtype.subtypeFolder) && name.endsWith(IndexConstants.TTS_DIR_SUFFIX)) {
if (name.startsWith(subtype.subtypeFolder) && name.endsWith(IndexConstants.VOICE_PROVIDER_SUFFIX)) {
return subtype;
}
break;
@ -244,9 +245,8 @@ public class FileSettingsItem extends StreamSettingsItem {
SettingsItemReader<? extends SettingsItem> getReader() {
return new StreamSettingsItemReader(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
public void readFromStream(@NonNull InputStream inputStream, File dest) throws IOException, IllegalArgumentException {
OutputStream output;
File dest = getDestination();
if (dest.exists() && !shouldReplace) {
dest = renameFile(dest);
}
@ -279,6 +279,38 @@ public class FileSettingsItem extends StreamSettingsItem {
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);
}
return super.getWriter();
return new StreamSettingsItemWriter(this) {
@Override
public void writeEntry(String fileName, @NonNull ZipOutputStream zos) throws IOException {
if (getSubtype().isDirectory()) {
File file = getFile();
zipDirsWithFiles(file, zos);
} else {
super.writeEntry(fileName, zos);
}
}
public void zipDirsWithFiles(File f, ZipOutputStream zos)
throws IOException {
if (f == null) {
return;
}
if (f.isDirectory()) {
File[] fs = f.listFiles();
if (fs != null) {
for (File c : fs) {
zipDirsWithFiles(c, zos);
}
}
} else {
String zipEntryName = Algorithms.isEmpty(getSubtype().getSubtypeFolder())
? f.getName()
: f.getPath().substring(f.getPath().indexOf(getSubtype().getSubtypeFolder()) - 1);
setInputStream(new FileInputStream(f));
super.writeEntry(zipEntryName, zos);
}
}
};
}
}

View file

@ -10,6 +10,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -29,7 +30,7 @@ public abstract class OsmandSettingsItemReader<T extends OsmandSettingsItem> ext
@NonNull JSONObject json) throws JSONException;
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
public void readFromStream(@NonNull InputStream inputStream, File destination) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

View file

@ -8,7 +8,6 @@ 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;
@ -68,43 +67,11 @@ class SettingsExporter {
if (Algorithms.isEmpty(fileName)) {
fileName = item.getDefaultFileName();
}
if (item instanceof FileSettingsItem && ((FileSettingsItem) item).getSubtype().isDirectory()) {
File file = ((FileSettingsItem) item).getFile();
zipDirsWithFiles(file, writer, zos);
} else {
ZipEntry entry = new ZipEntry(fileName);
zos.putNextEntry(entry);
writer.writeToStream(zos);
zos.closeEntry();
}
writer.writeEntry(fileName, zos);
}
}
}
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()) - 1);
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

@ -533,20 +533,22 @@ public class SettingsHelper {
dataList.put(ExportSettingsType.OSM_EDITS, editsPointList);
}
}
List<File> files = getLocalMapFiles();
if (!files.isEmpty()) {
dataList.put(ExportSettingsType.OFFLINE_MAPS, files);
}
List<FavoriteGroup> favoriteGroups = app.getFavorites().getFavoriteGroups();
if (!favoriteGroups.isEmpty()) {
dataList.put(ExportSettingsType.FAVORITES, favoriteGroups);
}
List<LocalIndexInfo> localVoiceFileList = getVoiceIndexInfo();
files = getFilesByType(localVoiceFileList, LocalIndexType.TTS_VOICE_DATA);
List<LocalIndexInfo> localIndexInfoList = getVoiceIndexInfo();
List<File> files;
files = getFilesByType(localIndexInfoList, LocalIndexType.MAP_DATA, LocalIndexType.TILES_DATA,
LocalIndexType.SRTM_DATA, LocalIndexType.WIKI_DATA);
if (!files.isEmpty()) {
dataList.put(ExportSettingsType.OFFLINE_MAPS, files);
}
files = getFilesByType(localIndexInfoList, LocalIndexType.TTS_VOICE_DATA);
if (!files.isEmpty()) {
dataList.put(ExportSettingsType.TTS_VOICE, files);
}
files = getFilesByType(localVoiceFileList, LocalIndexType.VOICE_DATA);
files = getFilesByType(localIndexInfoList, LocalIndexType.VOICE_DATA);
if (!files.isEmpty()) {
dataList.put(ExportSettingsType.VOICE, files);
}
@ -554,40 +556,25 @@ public class SettingsHelper {
}
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() {
return new LocalIndexHelper(app).getLocalIndexData(new AbstractLoadLocalIndexTask() {
@Override
public void loadFile(LocalIndexInfo... loaded) {
}
});
return localVoiceInfoList;
}
private List<File> getFilesByType(List<LocalIndexInfo> localVoiceFileList, LocalIndexType localIndexType) {
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);
boolean filtered = false;
for (LocalIndexType type : localIndexType) {
if (map.getType() == type) {
filtered = true;
break;
}
}
return files;
}
private List<File> getLocalMapFiles() {
List<File> files = new ArrayList<>();
LocalIndexHelper helper = new LocalIndexHelper(app);
List<LocalIndexInfo> localMapFileList = helper.getLocalIndexData(new AbstractLoadLocalIndexTask() {
@Override
public void loadFile(LocalIndexInfo... loaded) {
}
});
for (LocalIndexInfo map : localMapFileList) {
File file = new File(map.getPathToData());
if (file.exists() && map.getType() != LocalIndexType.TTS_VOICE_DATA
&& map.getType() != LocalIndexType.VOICE_DATA) {
if (file.exists() && filtered) {
files.add(file);
}
}

View file

@ -124,8 +124,7 @@ class SettingsImporter {
try {
SettingsItemReader<? extends SettingsItem> reader = item.getReader();
if (reader != null) {
reader.setDestination(app.getAppPath(fileName));
reader.readFromStream(ois);
reader.readFromStream(ois, app.getAppPath(fileName));
}
} catch (IllegalArgumentException e) {
item.warnings.add(app.getString(R.string.settings_item_read_error, item.getName()));

View file

@ -169,7 +169,7 @@ public abstract class SettingsItem {
SettingsItemReader<? extends SettingsItem> getJsonReader() {
return new SettingsItemReader<SettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
public void readFromStream(@NonNull InputStream inputStream, File destination) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

View file

@ -10,19 +10,9 @@ public abstract class SettingsItemReader<T extends SettingsItem> {
private T item;
File destination;
public SettingsItemReader(@NonNull T item) {
this.item = item;
}
public void setDestination(File destination) {
this.destination = destination;
}
public File getDestination() {
return destination;
}
public abstract void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException;
public abstract void readFromStream(@NonNull InputStream inputStream, File destination) throws IOException, IllegalArgumentException;
}

View file

@ -4,6 +4,8 @@ import androidx.annotation.NonNull;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public abstract class SettingsItemWriter<T extends SettingsItem> {
@ -18,4 +20,11 @@ public abstract class SettingsItemWriter<T extends SettingsItem> {
}
public abstract boolean writeToStream(@NonNull OutputStream outputStream) throws IOException;
public void writeEntry(String fileName, @NonNull ZipOutputStream zos) throws IOException {
ZipEntry entry = new ZipEntry(fileName);
zos.putNextEntry(entry);
writeToStream(zos);
zos.closeEntry();
}
}

View file

@ -368,8 +368,12 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
for (Object item : listItems) {
if (data.contains(item)) {
amount++;
if (type == OFFLINE_MAPS && item instanceof FileSettingsItem) {
if (type == OFFLINE_MAPS) {
if (item instanceof FileSettingsItem) {
amountSize += ((FileSettingsItem) item).getSize();
} else {
amountSize += ((File) item).length();
}
}
}
}
@ -403,9 +407,9 @@ class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter {
case OSM_NOTES:
return R.string.osm_notes;
case OSM_EDITS:
return R.string.osm_edit_modified_poi;
return R.string.osm_edits;
case OFFLINE_MAPS:
return R.string.shared_string_local_maps;
return R.string.shared_string_maps;
case FAVORITES:
return R.string.shared_string_favorites;
case TTS_VOICE:

View file

@ -295,7 +295,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
progress.dismiss();
}
progress = new ProgressDialog(context);
progress.setTitle(app.getString(R.string.export_profile));
progress.setTitle(app.getString(R.string.shared_string_export));
progress.setMessage(app.getString(R.string.shared_string_preparing));
progress.setCancelable(false);
progress.show();

View file

@ -120,7 +120,7 @@ public class ImportedSettingsItemsAdapter extends
break;
case OSM_EDITS:
holder.icon.setImageDrawable(uiUtils.getIcon(R.drawable.ic_action_info_dark, activeColorRes));
holder.title.setText(R.string.osm_edit_modified_poi);
holder.title.setText(R.string.osm_edits);
break;
case FAVORITES:
holder.icon.setImageDrawable(uiUtils.getIcon(R.drawable.ic_action_favorite, activeColorRes));
@ -128,7 +128,7 @@ public class ImportedSettingsItemsAdapter extends
break;
case OFFLINE_MAPS:
holder.icon.setImageDrawable(uiUtils.getIcon(R.drawable.ic_map, activeColorRes));
holder.title.setText(R.string.shared_string_local_maps);
holder.title.setText(R.string.shared_string_maps);
break;
case TTS_VOICE:
holder.icon.setImageDrawable(uiUtils.getIcon(R.drawable.ic_action_volume_up, activeColorRes));

View file

@ -95,7 +95,8 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat
language = ((Struct) langVal).getName();
}
} else {
language = voiceProvider.replace("-tts", "").replace("-formal", "");
language = voiceProvider.replace(IndexConstants.VOICE_PROVIDER_SUFFIX, "")
.replace("-formal", "");
}
}

View file

@ -28,8 +28,8 @@ public class JSTTSCommandPlayerImpl extends TTSCommandPlayerImpl {
jsScope = context.initSafeStandardObjects();
try {
BufferedReader br = new BufferedReader(new FileReader(new File(
app.getAppPath(IndexConstants.VOICE_INDEX_DIR).getAbsolutePath() +
"/" + voiceProvider + "/" + voiceProvider.replace("-tts", "_tts") + ".js")));
app.getAppPath(IndexConstants.VOICE_INDEX_DIR).getAbsolutePath() + "/" + voiceProvider + "/"
+ voiceProvider.replace(IndexConstants.VOICE_PROVIDER_SUFFIX, "_tts") + ".js")));
context.evaluateReader(jsScope, br, "JS", 1, null);
br.close();
} catch (Exception e) {