Save custom plugins first part
This commit is contained in:
parent
74f75f9ed7
commit
9c08188485
3 changed files with 84 additions and 2 deletions
|
@ -1,5 +1,8 @@
|
||||||
package net.osmand.plus;
|
package net.osmand.plus;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class CustomOsmandPlugin extends OsmandPlugin {
|
public class CustomOsmandPlugin extends OsmandPlugin {
|
||||||
|
|
||||||
public String pluginId;
|
public String pluginId;
|
||||||
|
@ -48,4 +51,15 @@ public class CustomOsmandPlugin extends OsmandPlugin {
|
||||||
public int getLogoResourceId() {
|
public int getLogoResourceId() {
|
||||||
return R.drawable.ic_action_skiing;
|
return R.drawable.ic_action_skiing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toJson() throws JSONException {
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
|
||||||
|
json.put("type", SettingsHelper.SettingsItemType.PLUGIN.name());
|
||||||
|
json.put("pluginId", getId());
|
||||||
|
json.put("name", getName());
|
||||||
|
json.put("Description", getDescription());
|
||||||
|
|
||||||
|
return json.toString();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -22,6 +22,7 @@ import net.osmand.PlatformUtil;
|
||||||
import net.osmand.access.AccessibilityPlugin;
|
import net.osmand.access.AccessibilityPlugin;
|
||||||
import net.osmand.plus.activities.MapActivity;
|
import net.osmand.plus.activities.MapActivity;
|
||||||
import net.osmand.plus.activities.TabActivity.TabItem;
|
import net.osmand.plus.activities.TabActivity.TabItem;
|
||||||
|
import net.osmand.plus.api.SettingsAPI;
|
||||||
import net.osmand.plus.audionotes.AudioVideoNotesPlugin;
|
import net.osmand.plus.audionotes.AudioVideoNotesPlugin;
|
||||||
import net.osmand.plus.dashboard.tools.DashFragmentData;
|
import net.osmand.plus.dashboard.tools.DashFragmentData;
|
||||||
import net.osmand.plus.development.OsmandDevelopmentPlugin;
|
import net.osmand.plus.development.OsmandDevelopmentPlugin;
|
||||||
|
@ -41,8 +42,12 @@ import net.osmand.plus.settings.BaseSettingsFragment;
|
||||||
import net.osmand.plus.skimapsplugin.SkiMapsPlugin;
|
import net.osmand.plus.skimapsplugin.SkiMapsPlugin;
|
||||||
import net.osmand.plus.srtmplugin.SRTMPlugin;
|
import net.osmand.plus.srtmplugin.SRTMPlugin;
|
||||||
import net.osmand.plus.views.OsmandMapTileView;
|
import net.osmand.plus.views.OsmandMapTileView;
|
||||||
|
import net.osmand.util.Algorithms;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -55,6 +60,9 @@ public abstract class OsmandPlugin {
|
||||||
|
|
||||||
public static final String PLUGIN_ID_KEY = "plugin_id";
|
public static final String PLUGIN_ID_KEY = "plugin_id";
|
||||||
|
|
||||||
|
private static final String PLUGINS_PREFERENCES_NAME = "net.osmand.plugins";
|
||||||
|
private static final String CUSTOM_PLUGINS_KEY = "custom_plugins";
|
||||||
|
|
||||||
private static final Log LOG = PlatformUtil.getLog(OsmandPlugin.class);
|
private static final Log LOG = PlatformUtil.getLog(OsmandPlugin.class);
|
||||||
|
|
||||||
private static List<OsmandPlugin> allPlugins = new ArrayList<OsmandPlugin>();
|
private static List<OsmandPlugin> allPlugins = new ArrayList<OsmandPlugin>();
|
||||||
|
@ -243,6 +251,7 @@ public abstract class OsmandPlugin {
|
||||||
allPlugins.add(new OsmEditingPlugin(app));
|
allPlugins.add(new OsmEditingPlugin(app));
|
||||||
allPlugins.add(new OsmandDevelopmentPlugin(app));
|
allPlugins.add(new OsmandDevelopmentPlugin(app));
|
||||||
|
|
||||||
|
loadCustomPlugins(app);
|
||||||
activatePlugins(app, enabledPlugins);
|
activatePlugins(app, enabledPlugins);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,6 +265,51 @@ public abstract class OsmandPlugin {
|
||||||
plugin.onInstall(app, activity);
|
plugin.onInstall(app, activity);
|
||||||
}
|
}
|
||||||
initPlugin(app, plugin);
|
initPlugin(app, plugin);
|
||||||
|
saveCustomPlugins(app);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void loadCustomPlugins(@NonNull OsmandApplication app) {
|
||||||
|
SettingsAPI settingsAPI = app.getSettings().getSettingsAPI();
|
||||||
|
Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME);
|
||||||
|
String customPluginsJson = settingsAPI.getString(pluginPrefs, CUSTOM_PLUGINS_KEY, "");
|
||||||
|
if (!Algorithms.isEmpty(customPluginsJson)) {
|
||||||
|
try {
|
||||||
|
JSONArray jArray = new JSONArray(customPluginsJson);
|
||||||
|
for (int i = 0; i < jArray.length(); i++) {
|
||||||
|
JSONObject json = jArray.getJSONObject(i);
|
||||||
|
|
||||||
|
String pluginId = json.getString("pluginId");
|
||||||
|
String name = json.getString("name");
|
||||||
|
String description = json.getString("Description");
|
||||||
|
|
||||||
|
CustomOsmandPlugin plugin = new CustomOsmandPlugin(app);
|
||||||
|
plugin.pluginId = pluginId;
|
||||||
|
plugin.name = name;
|
||||||
|
plugin.description = description;
|
||||||
|
allPlugins.add(plugin);
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void saveCustomPlugins(OsmandApplication app) {
|
||||||
|
List<CustomOsmandPlugin> customOsmandPlugins = getCustomPlugins();
|
||||||
|
if (!customOsmandPlugins.isEmpty()) {
|
||||||
|
SettingsAPI settingsAPI = app.getSettings().getSettingsAPI();
|
||||||
|
Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME);
|
||||||
|
JSONArray itemsJson = new JSONArray();
|
||||||
|
for (CustomOsmandPlugin plugin : customOsmandPlugins) {
|
||||||
|
try {
|
||||||
|
String json = plugin.toJson();
|
||||||
|
itemsJson.put(new JSONObject(json));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
settingsAPI.edit(pluginPrefs).putString(CUSTOM_PLUGINS_KEY, itemsJson.toString()).commit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void activatePlugins(OsmandApplication app, Set<String> enabledPlugins) {
|
private static void activatePlugins(OsmandApplication app, Set<String> enabledPlugins) {
|
||||||
|
@ -530,6 +584,16 @@ public abstract class OsmandPlugin {
|
||||||
return lst;
|
return lst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<CustomOsmandPlugin> getCustomPlugins() {
|
||||||
|
ArrayList<CustomOsmandPlugin> lst = new ArrayList<CustomOsmandPlugin>(allPlugins.size());
|
||||||
|
for (OsmandPlugin plugin : allPlugins) {
|
||||||
|
if (plugin instanceof CustomOsmandPlugin) {
|
||||||
|
lst.add((CustomOsmandPlugin) plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lst;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T extends OsmandPlugin> T getEnabledPlugin(Class<T> clz) {
|
public static <T extends OsmandPlugin> T getEnabledPlugin(Class<T> clz) {
|
||||||
for (OsmandPlugin lr : getEnabledPlugins()) {
|
for (OsmandPlugin lr : getEnabledPlugins()) {
|
||||||
|
|
|
@ -805,8 +805,12 @@ public class ImportHelper {
|
||||||
final CustomOsmandPlugin plugin = pluginItem.getPlugin();
|
final CustomOsmandPlugin plugin = pluginItem.getPlugin();
|
||||||
OsmandPlugin.addCustomPlugin(app, activity, plugin);
|
OsmandPlugin.addCustomPlugin(app, activity, plugin);
|
||||||
|
|
||||||
if (!Algorithms.isEmpty(pluginItem.getPluginItems())) {
|
List<SettingsHelper.SettingsItem> pluginItems = pluginItem.getPluginItems();
|
||||||
app.getSettingsHelper().importSettings(file, pluginItem.getPluginItems(), "", 1, new SettingsHelper.SettingsImportListener() {
|
if (!Algorithms.isEmpty(pluginItems)) {
|
||||||
|
for (SettingsHelper.SettingsItem item : pluginItems) {
|
||||||
|
item.setShouldReplace(true);
|
||||||
|
}
|
||||||
|
app.getSettingsHelper().importSettings(file, pluginItems, "", 1, new SettingsHelper.SettingsImportListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsHelper.SettingsItem> items) {
|
public void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsHelper.SettingsItem> items) {
|
||||||
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, plugin.getName()));
|
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, plugin.getName()));
|
||||||
|
|
Loading…
Reference in a new issue