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

View file

@ -283,6 +283,21 @@ public abstract class OsmandPlugin {
saveCustomPlugins(app); 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) { private static void loadCustomPlugins(@NonNull OsmandApplication app) {
SettingsAPI settingsAPI = app.getSettings().getSettingsAPI(); SettingsAPI settingsAPI = app.getSettings().getSettingsAPI();
Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME); Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME);
@ -303,7 +318,6 @@ public abstract class OsmandPlugin {
private static void saveCustomPlugins(OsmandApplication app) { private static void saveCustomPlugins(OsmandApplication app) {
List<CustomOsmandPlugin> customOsmandPlugins = getCustomPlugins(); List<CustomOsmandPlugin> customOsmandPlugins = getCustomPlugins();
if (!customOsmandPlugins.isEmpty()) {
SettingsAPI settingsAPI = app.getSettings().getSettingsAPI(); SettingsAPI settingsAPI = app.getSettings().getSettingsAPI();
Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME); Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME);
JSONArray itemsJson = new JSONArray(); JSONArray itemsJson = new JSONArray();
@ -323,7 +337,6 @@ public abstract class OsmandPlugin {
settingsAPI.edit(pluginPrefs).putString(CUSTOM_PLUGINS_KEY, jsonStr).commit(); settingsAPI.edit(pluginPrefs).putString(CUSTOM_PLUGINS_KEY, jsonStr).commit();
} }
} }
}
private static void activatePlugins(OsmandApplication app, Set<String> enabledPlugins) { private static void activatePlugins(OsmandApplication app, Set<String> enabledPlugins) {
for (OsmandPlugin plugin : allPlugins) { for (OsmandPlugin plugin : allPlugins) {

View file

@ -1,6 +1,7 @@
package net.osmand.plus.activities; package net.osmand.plus.activities;
import android.app.Activity; import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.os.Bundle; import android.os.Bundle;
@ -14,11 +15,13 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.aidl.ConnectedApp; import net.osmand.aidl.ConnectedApp;
import net.osmand.plus.CustomOsmandPlugin;
import net.osmand.plus.OsmandApplication; 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;
@ -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(); 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();
}
} }