From 2f98f7c475ce7b3389f71da6be0c96bda12a0c9f Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 25 Mar 2020 11:50:31 +0200 Subject: [PATCH] Remove plugins items after disabling plugin --- .../net/osmand/plus/CustomOsmandPlugin.java | 92 +++++++++++++- .../src/net/osmand/plus/SettingsHelper.java | 112 ++++++++---------- 2 files changed, 140 insertions(+), 64 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index 7c4f8fc23e..d3cc6d82f7 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -6,13 +6,26 @@ import android.content.res.Configuration; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import net.osmand.IndexConstants; import net.osmand.PlatformUtil; +import net.osmand.map.ITileSource; +import net.osmand.map.TileSourceManager; +import net.osmand.plus.SettingsHelper.AvoidRoadsSettingsItem; +import net.osmand.plus.SettingsHelper.MapSourcesSettingsItem; +import net.osmand.plus.SettingsHelper.PoiUiFilterSettingsItem; +import net.osmand.plus.SettingsHelper.QuickActionsSettingsItem; +import net.osmand.plus.SettingsHelper.SettingsCollectListener; +import net.osmand.plus.SettingsHelper.SettingsItem; +import net.osmand.plus.helpers.AvoidSpecificRoads; +import net.osmand.plus.poi.PoiUIFilter; +import net.osmand.plus.quickaction.QuickAction; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import org.json.JSONException; import org.json.JSONObject; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -52,12 +65,89 @@ public class CustomOsmandPlugin extends OsmandPlugin { @Override public boolean init(@NonNull OsmandApplication app, @Nullable Activity activity) { - return super.init(app, activity); + super.init(app, activity); + if (activity != null) { + // called from UI + File pluginItemsFile = getPluginItemsFile(); + if (pluginItemsFile.exists()) { + addPluginItemsFromFile(pluginItemsFile); + } + } + return true; + } + + private void addPluginItemsFromFile(final File file) { + app.getSettingsHelper().collectSettings(file, "", 1, new SettingsCollectListener() { + @Override + public void onSettingsCollectFinished(boolean succeed, boolean empty, @NonNull List items) { + if (succeed && !items.isEmpty()) { + + } + } + }); + } + + private void removePluginItemsFromFile(final File file) { + app.getSettingsHelper().collectSettings(file, "", 1, new SettingsCollectListener() { + @Override + public void onSettingsCollectFinished(boolean succeed, boolean empty, @NonNull List items) { + if (succeed && !items.isEmpty()) { + for (SettingsItem item : items) { + if (item instanceof QuickActionsSettingsItem) { + QuickActionsSettingsItem quickActionsSettingsItem = (QuickActionsSettingsItem) item; + List quickActions = quickActionsSettingsItem.getItems(); + for (QuickAction action : quickActions) { + QuickAction savedAction = app.getQuickActionRegistry().getQuickAction(app, action.getType(), action.getName(app), action.getParams()); + if (savedAction != null) { + app.getQuickActionRegistry().deleteQuickAction(savedAction); + } + } + } + if (item instanceof MapSourcesSettingsItem) { + MapSourcesSettingsItem mapSourcesSettingsItem = (MapSourcesSettingsItem) item; + List mapSources = mapSourcesSettingsItem.getItems(); + + for (ITileSource tileSource : mapSources) { + if (tileSource instanceof TileSourceManager.TileSourceTemplate) { +// app.getSettings().installTileSource((TileSourceManager.TileSourceTemplate) tileSource); + } else if (tileSource instanceof SQLiteTileSource) { +// ((SQLiteTileSource) tileSource).createDataBase(); + } + } + } + if (item instanceof PoiUiFilterSettingsItem) { + PoiUiFilterSettingsItem poiUiFilterSettingsItem = (PoiUiFilterSettingsItem) item; + List poiUIFilters = poiUiFilterSettingsItem.getItems(); + for (PoiUIFilter filter : poiUIFilters) { + app.getPoiFilters().removePoiFilter(filter); + } + app.getSearchUICore().refreshCustomPoiFilters(); + } + if (item instanceof AvoidRoadsSettingsItem) { + AvoidRoadsSettingsItem avoidRoadsSettingsItem = (AvoidRoadsSettingsItem) item; + List avoidRoadInfos = avoidRoadsSettingsItem.getItems(); + for (AvoidSpecificRoads.AvoidRoadInfo avoidRoad : avoidRoadInfos) { + app.getAvoidSpecificRoads().removeImpassableRoad(avoidRoad); + } + } + } + } + } + }); } @Override public void disable(OsmandApplication app) { super.disable(app); + File pluginItemsFile = getPluginItemsFile(); + if (pluginItemsFile.exists()) { + removePluginItemsFromFile(pluginItemsFile); + } + } + + private File getPluginItemsFile() { + File pluginDir = new File(app.getAppPath(null), IndexConstants.PLUGINS_DIR + pluginId); + return new File(pluginDir, "items" + IndexConstants.OSMAND_SETTINGS_FILE_EXT); } @Override diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 2775a54da5..2d64ac7d08 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -1241,28 +1241,33 @@ public class SettingsHelper { @Override public void apply() { if (!items.isEmpty() || !duplicateItems.isEmpty()) { - List newActions = new ArrayList<>(existingItems); - if (!duplicateItems.isEmpty()) { - if (shouldReplace) { - for (QuickAction duplicateItem : duplicateItems) { - for (QuickAction savedAction : existingItems) { - if (duplicateItem.getName(app).equals(savedAction.getName(app))) { - newActions.remove(savedAction); - } - } - } - } else { - for (QuickAction duplicateItem : duplicateItems) { - renameItem(duplicateItem); - } - } - newActions.addAll(duplicateItems); - } - newActions.addAll(items); + List newActions = getNewActions(); actionRegistry.updateQuickActions(newActions); } } + private List getNewActions() { + List newActions = new ArrayList<>(existingItems); + if (!duplicateItems.isEmpty()) { + if (shouldReplace) { + for (QuickAction duplicateItem : duplicateItems) { + for (QuickAction savedAction : existingItems) { + if (duplicateItem.getName(app).equals(savedAction.getName(app))) { + newActions.remove(savedAction); + } + } + } + } else { + for (QuickAction duplicateItem : duplicateItems) { + renameItem(duplicateItem); + } + } + newActions.addAll(duplicateItems); + } + newActions.addAll(items); + return newActions; + } + @Override public boolean shouldReadOnCollecting() { return true; @@ -1325,9 +1330,10 @@ public class SettingsHelper { Gson gson = new Gson(); Type type = new TypeToken>() { }.getType(); - if (!items.isEmpty()) { + if (!items.isEmpty() || !duplicateItems.isEmpty()) { + List newActions = getNewActions(); try { - for (QuickAction action : items) { + for (QuickAction action : newActions) { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", action.hasCustomName(app) ? action.getName(app) : ""); @@ -1966,10 +1972,10 @@ public class SettingsHelper { private Map items; private Map additionalParams; - private boolean onlyJson; + private boolean exportItemsFiles; - SettingsExporter(boolean onlyJson) { - this.onlyJson = onlyJson; + SettingsExporter(boolean exportItemsFiles) { + this.exportItemsFiles = exportItemsFiles; items = new LinkedHashMap<>(); additionalParams = new LinkedHashMap<>(); } @@ -1987,25 +1993,6 @@ public class SettingsHelper { void exportSettings(File file) throws JSONException, IOException { JSONObject json = createItemsJson(); - if (onlyJson) { - saveJsonItems(file, json); - } else { - saveZipItems(file, json); - } - } - - private void saveJsonItems(File file, JSONObject json) throws JSONException, IOException { - InputStream inputStream = new ByteArrayInputStream(json.toString(2).getBytes("UTF-8")); - OutputStream os = new BufferedOutputStream(new FileOutputStream(file), BUFFER); - try { - Algorithms.streamCopy(inputStream, os); - } finally { - Algorithms.closeStream(inputStream); - Algorithms.closeStream(os); - } - } - - private void saveZipItems(File file, JSONObject json) throws JSONException, IOException { OutputStream os = new BufferedOutputStream(new FileOutputStream(file), BUFFER); ZipOutputStream zos = new ZipOutputStream(os); try { @@ -2013,14 +2000,8 @@ public class SettingsHelper { zos.putNextEntry(entry); zos.write(json.toString(2).getBytes("UTF-8")); zos.closeEntry(); - for (SettingsItem item : items.values()) { - SettingsItemWriter writer = item.getWriter(); - if (writer != null) { - entry = new ZipEntry(item.getFileName()); - zos.putNextEntry(entry); - writer.writeToStream(zos); - zos.closeEntry(); - } + if (exportItemsFiles) { + writeItemFiles(zos); } zos.flush(); zos.finish(); @@ -2030,6 +2011,18 @@ public class SettingsHelper { } } + private void writeItemFiles(ZipOutputStream zos) throws IOException { + for (SettingsItem item : items.values()) { + ZipEntry entry = new ZipEntry(item.getFileName()); + zos.putNextEntry(entry); + SettingsItemWriter itemWriter = item.getWriter(); + if (itemWriter != null) { + itemWriter.writeToStream(zos); + } + zos.closeEntry(); + } + } + private JSONObject createItemsJson() throws JSONException { JSONObject json = new JSONObject(); json.put("version", VERSION); @@ -2387,10 +2380,10 @@ public class SettingsHelper { ExportAsyncTask(@NonNull File settingsFile, @Nullable SettingsExportListener listener, - @NonNull List items, boolean onlyJson) { + @NonNull List items, boolean exportItemsFiles) { this.file = settingsFile; this.listener = listener; - this.exporter = new SettingsExporter(onlyJson); + this.exporter = new SettingsExporter(exportItemsFiles); for (SettingsItem item : items) { exporter.addSettingsItem(item); } @@ -2431,23 +2424,16 @@ public class SettingsHelper { new ImportAsyncTask(settingsFile, items, latestChanges, version, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, @NonNull List items) { + public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, @NonNull List items, boolean exportItemsFiles) { File file = new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT); - ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items, false); - exportAsyncTasks.put(file, exportAsyncTask); - exportAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - - public void exportPluginItems(@NonNull File pluginDir, @Nullable SettingsExportListener listener, @NonNull List items) { - File file = new File(pluginDir, "items.json"); - ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items, true); + ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items, exportItemsFiles); exportAsyncTasks.put(file, exportAsyncTask); exportAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, - @NonNull SettingsItem... items) { - exportSettings(fileDir, fileName, listener, new ArrayList<>(Arrays.asList(items))); + boolean exportItemsFiles, @NonNull SettingsItem... items) { + exportSettings(fileDir, fileName, listener, new ArrayList<>(Arrays.asList(items)), exportItemsFiles); } public enum ImportType {