refactor way of reading bundled_assets.xml

This commit is contained in:
veliymolfar 2020-06-26 13:49:03 +03:00
parent e85866bb22
commit a7ae126281
2 changed files with 64 additions and 74 deletions

View file

@ -124,17 +124,18 @@ public class DownloadOsmandIndexesHelper {
return result; return result;
} }
public static Map<String, String> assetMapping(AssetManager assetManager) throws XmlPullParserException, IOException { public static List<AssetEntry> getBundledAssets(AssetManager assetManager) throws XmlPullParserException, IOException {
XmlPullParser xmlParser = XmlPullParserFactory.newInstance().newPullParser(); XmlPullParser xmlParser = XmlPullParserFactory.newInstance().newPullParser();
InputStream isBundledAssetsXml = assetManager.open("bundled_assets.xml"); InputStream isBundledAssetsXml = assetManager.open("bundled_assets.xml");
xmlParser.setInput(isBundledAssetsXml, "UTF-8"); xmlParser.setInput(isBundledAssetsXml, "UTF-8");
Map<String, String> assets = new HashMap<String, String>(); List<AssetEntry> assets = new ArrayList<>();
int next = 0; int next = 0;
while ((next = xmlParser.next()) != XmlPullParser.END_DOCUMENT) { while ((next = xmlParser.next()) != XmlPullParser.END_DOCUMENT) {
if (next == XmlPullParser.START_TAG && xmlParser.getName().equals("asset")) { if (next == XmlPullParser.START_TAG && xmlParser.getName().equals("asset")) {
final String source = xmlParser.getAttributeValue(null, "source"); final String source = xmlParser.getAttributeValue(null, "source");
final String destination = xmlParser.getAttributeValue(null, "destination"); final String destination = xmlParser.getAttributeValue(null, "destination");
assets.put(source, destination); final String combinedMode = xmlParser.getAttributeValue(null, "mode");
assets.add(new AssetEntry(source, destination, combinedMode));
} }
} }
isBundledAssetsXml.close(); isBundledAssetsXml.close();
@ -155,15 +156,15 @@ public class DownloadOsmandIndexesHelper {
} catch (NameNotFoundException e) { } catch (NameNotFoundException e) {
//do nothing... //do nothing...
} }
Map<String, String> mapping = assetMapping(amanager); List<AssetEntry> mapping = getBundledAssets(amanager);
for (String key : mapping.keySet()) { for (AssetEntry asset : mapping) {
String target = mapping.get(key); String target = asset.destination;
if (target.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS) && target.startsWith("voice/")) { if (target.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS) && target.startsWith("voice/") && target.contains("-tts")) {
String lang = target.substring("voice/".length(), target.indexOf("-tts")); String lang = target.substring("voice/".length(), target.indexOf("-tts"));
File destFile = new File(voicePath, target.substring("voice/".length(), File destFile = new File(voicePath, target.substring("voice/".length(),
target.indexOf("/", "voice/".length())) + "/" + lang + "_tts.js"); target.indexOf("/", "voice/".length())) + "/" + lang + "_tts.js");
result.add(new AssetIndexItem(lang + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS, result.add(new AssetIndexItem(lang + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS,
"voice", date, dateModified, "0.1", destFile.length(), key, "voice", date, dateModified, "0.1", destFile.length(), asset.source,
destFile.getPath(), DownloadActivityType.VOICE_FILE)); destFile.getPath(), DownloadActivityType.VOICE_FILE));
} }
} }
@ -263,5 +264,15 @@ public class DownloadOsmandIndexesHelper {
} }
} }
public static class AssetEntry {
public final String source;
public final String destination;
public final String combinedMode;
public AssetEntry(String source, String destination, String combinedMode) {
this.source = source;
this.destination = destination;
this.combinedMode = combinedMode;
}
}
} }

View file

@ -39,6 +39,8 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.download.DownloadOsmandIndexesHelper;
import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetEntry;
import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.render.MapRenderRepositories; import net.osmand.plus.render.MapRenderRepositories;
import net.osmand.plus.render.NativeOsmandLibrary; import net.osmand.plus.render.NativeOsmandLibrary;
@ -52,9 +54,7 @@ import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
@ -77,7 +77,6 @@ import java.util.concurrent.ConcurrentHashMap;
import static net.osmand.IndexConstants.VOICE_INDEX_DIR; import static net.osmand.IndexConstants.VOICE_INDEX_DIR;
import static net.osmand.plus.download.DownloadOsmandIndexesHelper.assetMapping;
/** /**
* Resource manager is responsible to work with all resources * Resource manager is responsible to work with all resources
@ -450,20 +449,20 @@ public class ResourceManager {
public void copyMissingJSAssets() { public void copyMissingJSAssets() {
try { try {
Map<String, String> mapping = assetMapping(context.getAssets()); List<AssetEntry> assets = DownloadOsmandIndexesHelper.getBundledAssets(context.getAssets());
File appPath = context.getAppPath(null); File appPath = context.getAppPath(null);
if (appPath.canWrite()) { if (appPath.canWrite()) {
for (Entry<String,String> entry : mapping.entrySet()) { for (AssetEntry asset : assets) {
File jsFile = new File(appPath, entry.getValue()); File jsFile = new File(appPath, asset.destination);
if (entry.getValue().contains("-tts") && entry.getValue() if (asset.destination.contains("-tts") && asset.destination
.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) { .endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) {
File oggFile = new File(appPath, entry.getValue().replace("-tts", "")); File oggFile = new File(appPath, asset.destination.replace("-tts", ""));
if (oggFile.getParentFile().exists() && !oggFile.exists()) { if (oggFile.getParentFile().exists() && !oggFile.exists()) {
copyAssets(context.getAssets(), entry.getKey(), oggFile); copyAssets(context.getAssets(), asset.source, oggFile);
} }
} }
if (jsFile.getParentFile().exists() && !jsFile.exists()) { if (jsFile.getParentFile().exists() && !jsFile.exists()) {
copyAssets(context.getAssets(), entry.getKey(), jsFile); copyAssets(context.getAssets(), asset.source, jsFile);
} }
} }
} }
@ -541,20 +540,11 @@ public class ResourceManager {
private final static String ASSET_COPY_MODE__alwaysOverwriteOrCopy = "alwaysOverwriteOrCopy"; private final static String ASSET_COPY_MODE__alwaysOverwriteOrCopy = "alwaysOverwriteOrCopy";
private final static String ASSET_COPY_MODE__copyOnlyIfDoesNotExist = "copyOnlyIfDoesNotExist"; private final static String ASSET_COPY_MODE__copyOnlyIfDoesNotExist = "copyOnlyIfDoesNotExist";
private void unpackBundledAssets(AssetManager assetManager, File appDataDir, IProgress progress, boolean isFirstInstall) throws IOException, XmlPullParserException { private void unpackBundledAssets(AssetManager assetManager, File appDataDir, IProgress progress, boolean isFirstInstall) throws IOException, XmlPullParserException {
XmlPullParser xmlParser = XmlPullParserFactory.newInstance().newPullParser(); List<AssetEntry> assetEntries = DownloadOsmandIndexesHelper.getBundledAssets(assetManager);
InputStream isBundledAssetsXml = assetManager.open("bundled_assets.xml"); for (AssetEntry asset : assetEntries) {
xmlParser.setInput(isBundledAssetsXml, "UTF-8"); final String[] modes = asset.combinedMode.split("\\|");
int next = 0;
while ((next = xmlParser.next()) != XmlPullParser.END_DOCUMENT) {
if (next == XmlPullParser.START_TAG && xmlParser.getName().equals("asset")) {
final String source = xmlParser.getAttributeValue(null, "source");
final String destination = xmlParser.getAttributeValue(null, "destination");
final String combinedMode = xmlParser.getAttributeValue(null, "mode");
final String[] modes = combinedMode.split("\\|");
if (modes.length == 0) { if (modes.length == 0) {
log.error("Mode '" + combinedMode + "' is not valid"); log.error("Mode '" + asset.combinedMode + "' is not valid");
continue; continue;
} }
String installMode = null; String installMode = null;
@ -570,13 +560,13 @@ public class ResourceManager {
log.error("Mode '" + mode + "' is unknown"); log.error("Mode '" + mode + "' is unknown");
} }
final File destinationFile = new File(appDataDir, destination); final File destinationFile = new File(appDataDir, asset.destination);
boolean unconditional = false; boolean unconditional = false;
if (installMode != null) if (installMode != null)
unconditional = unconditional || (ASSET_INSTALL_MODE__alwaysCopyOnFirstInstall.equals(installMode) && isFirstInstall); unconditional = unconditional || (ASSET_INSTALL_MODE__alwaysCopyOnFirstInstall.equals(installMode) && isFirstInstall);
if (copyMode == null) if (copyMode == null)
log.error("No copy mode was defined for " + source); log.error("No copy mode was defined for " + asset.source);
unconditional = unconditional || ASSET_COPY_MODE__alwaysOverwriteOrCopy.equals(copyMode); unconditional = unconditional || ASSET_COPY_MODE__alwaysOverwriteOrCopy.equals(copyMode);
boolean shouldCopy = unconditional; boolean shouldCopy = unconditional;
@ -584,21 +574,10 @@ public class ResourceManager {
shouldCopy = shouldCopy || (ASSET_COPY_MODE__copyOnlyIfDoesNotExist.equals(copyMode) && !destinationFile.exists()); shouldCopy = shouldCopy || (ASSET_COPY_MODE__copyOnlyIfDoesNotExist.equals(copyMode) && !destinationFile.exists());
if (shouldCopy) { if (shouldCopy) {
copyAssets(assetManager, source, destinationFile); copyAssets(assetManager, asset.source, destinationFile);
}
if (source.contains("-tts") && source.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) {
File oggFile = new File(appDataDir + VOICE_INDEX_DIR, source.replace("-tts", ""));
if (oggFile.getParentFile().exists()
&& oggFile.exists()
&& ASSET_COPY_MODE__overwriteOnlyIfExists.equals(copyMode)) {
copyAssets(assetManager, source, oggFile);
} }
} }
} }
}
isBundledAssetsXml.close();
}
public static void copyAssets(AssetManager assetManager, String assetName, File file) throws IOException { public static void copyAssets(AssetManager assetManager, String assetName, File file) throws IOException {
if(file.exists()){ if(file.exists()){