Add ability to remove custom plugins

This commit is contained in:
Vitaliy 2020-03-31 23:32:03 +03:00
parent ddf505025e
commit 709e5d8993
3 changed files with 86 additions and 26 deletions

View file

@ -105,7 +105,14 @@ public class CustomOsmandPlugin extends OsmandPlugin {
});
}
private void removePluginItemsFromFile(final File file) {
public void removePluginItems(PluginItemsListener itemsListener) {
File pluginItemsFile = getPluginItemsFile();
if (pluginItemsFile.exists()) {
removePluginItemsFromFile(pluginItemsFile, itemsListener);
}
}
private void removePluginItemsFromFile(final File file, final PluginItemsListener itemsListener) {
app.getSettingsHelper().collectSettings(file, "", 1, new SettingsCollectListener() {
@Override
public void onSettingsCollectFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items) {
@ -165,6 +172,9 @@ public class CustomOsmandPlugin extends OsmandPlugin {
}
}
}
if (itemsListener != null) {
itemsListener.onItemsRemoved();
}
}
});
}
@ -172,15 +182,15 @@ public class CustomOsmandPlugin extends OsmandPlugin {
@Override
public void disable(OsmandApplication app) {
super.disable(app);
File pluginItemsFile = getPluginItemsFile();
if (pluginItemsFile.exists()) {
removePluginItemsFromFile(pluginItemsFile);
}
removePluginItems(null);
}
private File getPluginItemsFile() {
File pluginDir = new File(app.getAppPath(null), IndexConstants.PLUGINS_DIR + pluginId);
return new File(pluginDir, "items" + IndexConstants.OSMAND_SETTINGS_FILE_EXT);
public File getPluginDir() {
return new File(app.getAppPath(null), IndexConstants.PLUGINS_DIR + pluginId);
}
public File getPluginItemsFile() {
return new File(getPluginDir(), "items" + IndexConstants.OSMAND_SETTINGS_FILE_EXT);
}
@Override
@ -406,4 +416,10 @@ public class CustomOsmandPlugin extends OsmandPlugin {
public Drawable getAssetResourceImage() {
return image;
}
public interface PluginItemsListener {
void onItemsRemoved();
}
}

View file

@ -283,6 +283,21 @@ public abstract class OsmandPlugin {
saveCustomPlugins(app);
}
public static void removeCustomPlugin(@NonNull OsmandApplication app, @NonNull final CustomOsmandPlugin plugin) {
allPlugins.remove(plugin);
if (plugin.isActive()) {
plugin.removePluginItems(new CustomOsmandPlugin.PluginItemsListener() {
@Override
public void onItemsRemoved() {
Algorithms.removeAllFiles(plugin.getPluginDir());
}
});
} else {
Algorithms.removeAllFiles(plugin.getPluginDir());
}
saveCustomPlugins(app);
}
private static void loadCustomPlugins(@NonNull OsmandApplication app) {
SettingsAPI settingsAPI = app.getSettings().getSettingsAPI();
Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME);
@ -303,26 +318,24 @@ public abstract class OsmandPlugin {
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 {
JSONObject json = new JSONObject();
json.put("pluginId", plugin.getId());
plugin.writeAdditionalDataToJson(json);
plugin.writeDependentFilesJson(json);
itemsJson.put(json);
} catch (JSONException e) {
e.printStackTrace();
}
}
String jsonStr = itemsJson.toString();
if (!jsonStr.equals(settingsAPI.getString(pluginPrefs, CUSTOM_PLUGINS_KEY, ""))) {
settingsAPI.edit(pluginPrefs).putString(CUSTOM_PLUGINS_KEY, jsonStr).commit();
SettingsAPI settingsAPI = app.getSettings().getSettingsAPI();
Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME);
JSONArray itemsJson = new JSONArray();
for (CustomOsmandPlugin plugin : customOsmandPlugins) {
try {
JSONObject json = new JSONObject();
json.put("pluginId", plugin.getId());
plugin.writeAdditionalDataToJson(json);
plugin.writeDependentFilesJson(json);
itemsJson.put(json);
} catch (JSONException e) {
e.printStackTrace();
}
}
String jsonStr = itemsJson.toString();
if (!jsonStr.equals(settingsAPI.getString(pluginPrefs, CUSTOM_PLUGINS_KEY, ""))) {
settingsAPI.edit(pluginPrefs).putString(CUSTOM_PLUGINS_KEY, jsonStr).commit();
}
}
private static void activatePlugins(OsmandApplication app, Set<String> enabledPlugins) {

View file

@ -1,6 +1,7 @@
package net.osmand.plus.activities;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
@ -14,11 +15,13 @@ import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment;
import net.osmand.AndroidUtils;
import net.osmand.aidl.ConnectedApp;
import net.osmand.plus.CustomOsmandPlugin;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
@ -251,6 +254,34 @@ public class PluginsActivity extends OsmandListActivity implements DownloadIndex
});
}
if (plugin instanceof CustomOsmandPlugin) {
MenuItem settingsItem = optionsMenu.getMenu().add(R.string.shared_string_delete);
settingsItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
showDeletePluginDialog((CustomOsmandPlugin) plugin);
optionsMenu.dismiss();
return true;
}
});
}
optionsMenu.show();
}
private void showDeletePluginDialog(final CustomOsmandPlugin plugin) {
AlertDialog.Builder builder = new AlertDialog.Builder(PluginsActivity.this);
builder.setTitle(getString(R.string.delete_confirmation_msg, plugin.getName()));
builder.setMessage(R.string.are_you_sure);
builder.setNegativeButton(R.string.shared_string_cancel, null);
builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
OsmandApplication app = getMyApplication();
OsmandPlugin.removeCustomPlugin(app, plugin);
getListAdapter().remove(plugin);
}
});
builder.show();
}
}