From 5bcb0a55fd4cd522ed6a9c079a52ece041d15b94 Mon Sep 17 00:00:00 2001 From: PaulStets Date: Thu, 9 Aug 2018 16:51:06 +0300 Subject: [PATCH] Download missing js files --- .../src/net/osmand/plus/AppInitializer.java | 3 +- .../SettingsDevelopmentActivity.java | 4 +- .../plus/download/DownloadIndexesThread.java | 37 ------------------- .../download/DownloadOsmandIndexesHelper.java | 22 ++++++++++- 4 files changed, 26 insertions(+), 40 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 0236dc4c9f..d1f901f31b 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -29,6 +29,7 @@ import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadOsmandIndexesHelper; import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.WaypointHelper; @@ -180,8 +181,8 @@ public class AppInitializer implements IProgress { if(prevAppVersion < VERSION_2_3) { startPrefs.edit().putInt(VERSION_INSTALLED_NUMBER, VERSION_2_3).commit(); } else if (prevAppVersion < VERSION_3_2) { + DownloadOsmandIndexesHelper.copyMissingJSAssets(app); startPrefs.edit().putInt(VERSION_INSTALLED_NUMBER, VERSION_3_2).commit(); - app.getDownloadThread().copyJSVoiceGuidanceFiles(); } startPrefs.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit(); appVersionChanged = true; diff --git a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java index 7f437cd040..a0c46124e2 100644 --- a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java @@ -14,6 +14,7 @@ import android.preference.PreferenceScreen; import android.support.v7.app.AlertDialog; import android.view.View; +import net.osmand.IProgress; import net.osmand.StateChangedListener; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmAndLocationSimulation; @@ -22,6 +23,7 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.activities.actions.AppModeDialog; +import net.osmand.plus.download.DownloadOsmandIndexesHelper; import net.osmand.util.SunriseSunset; import java.text.SimpleDateFormat; @@ -68,7 +70,7 @@ public class SettingsDevelopmentActivity extends SettingsBaseActivity { @Override public void stateChanged(Boolean change) { if (change) { - getMyApplication().getDownloadThread().copyJSVoiceGuidanceFiles(); + DownloadOsmandIndexesHelper.copyMissingJSAssets(getMyApplication()); } getMyApplication().getDownloadThread().runReloadIndexFilesSilent(); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index f109a897c4..d9edf2aa30 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -100,43 +100,6 @@ public class DownloadIndexesThread { } } - public void copyJSVoiceGuidanceFiles() { - File extStorage = app.getAppPath(IndexConstants.VOICE_INDEX_DIR); - Map jsFiles = getJSTTSIndexes(); - if (extStorage.exists()) { - for (File f : extStorage.listFiles()) { - if (f.isDirectory()) { - if (jsFiles.containsKey(f.getName()) && !shouldSkip(f)) { - runDownloadFiles(jsFiles.get(f.getName())); - } - } - } - } - } - - private boolean shouldSkip(File voiceDir) { - for (File f : voiceDir.listFiles()) { - if (f.getName().endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) { - return true; - } - } - return false; - } - - private Map getJSTTSIndexes() { - Map items = new HashMap<>(); - DownloadOsmandIndexesHelper.IndexFileList indexFileList = new DownloadOsmandIndexesHelper.IndexFileList(); - DownloadOsmandIndexesHelper.listVoiceAssets(indexFileList, app.getAssets(), app.getPackageManager(), app.getSettings()); - for (IndexItem item : indexFileList.getIndexFiles()) { - if (item.getType() == DownloadActivityType.VOICE_FILE && !item.isDownloaded()) { - if (item.getFileName().endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) { - items.put(item.getBasename(), item); - } - } - } - return items; - } - @UiThread protected void downloadInProgress() { if (uiActivity != null) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index 167d366397..bbf02ded76 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -19,6 +19,7 @@ import net.osmand.PlatformUtil; import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; +import net.osmand.plus.resources.ResourceManager; import org.apache.commons.logging.Log; import org.xmlpull.v1.XmlPullParser; @@ -123,6 +124,25 @@ public class DownloadOsmandIndexesHelper { listVoiceAssets(result, amanager, pm, app.getSettings()); return result; } + + public static void copyMissingJSAssets(OsmandApplication app) { + try { + Map mapping = assetMapping(app.getAssets()); + File appPath = app.getAppPath(null); + if (appPath.canWrite()) { + for (Map.Entry entry : mapping.entrySet()) { + File jsFile = new File(appPath, entry.getValue()); + if (jsFile.getParentFile().exists() && !jsFile.exists()) { + ResourceManager.copyAssets(app.getAssets(), entry.getKey(), jsFile); + } + } + } + } catch (XmlPullParserException e) { + log.error("Error while loading tts files from assets", e); + } catch (IOException e) { + log.error("Error while loading tts files from assets", e); + } + } private static Map assetMapping(AssetManager assetManager) throws XmlPullParserException, IOException { XmlPullParser xmlParser = XmlPullParserFactory.newInstance().newPullParser(); @@ -141,7 +161,7 @@ public class DownloadOsmandIndexesHelper { return assets; } - public static void listVoiceAssets(IndexFileList result, AssetManager amanager, PackageManager pm, + private static void listVoiceAssets(IndexFileList result, AssetManager amanager, PackageManager pm, OsmandSettings settings) { try { String ext = DownloadActivityType.addVersionToExt(IndexConstants.TTSVOICE_INDEX_EXT_ZIP, IndexConstants.TTSVOICE_VERSION);