Fix check for market plugins

This commit is contained in:
Chumva 2019-11-18 10:20:32 +02:00
parent 8b038fc5ed
commit 687607698d
7 changed files with 134 additions and 70 deletions

View file

@ -56,8 +56,6 @@ public abstract class OsmandPlugin {
private static List<OsmandPlugin> allPlugins = new ArrayList<OsmandPlugin>(); private static List<OsmandPlugin> allPlugins = new ArrayList<OsmandPlugin>();
private static final Log LOG = PlatformUtil.getLog(OsmandPlugin.class); private static final Log LOG = PlatformUtil.getLog(OsmandPlugin.class);
private static final String SRTM_PLUGIN_COMPONENT_PAID = "net.osmand.srtmPlugin.paid"; //$NON-NLS-1$
private static final String SRTM_PLUGIN_COMPONENT = "net.osmand.srtmPlugin";
private boolean active; private boolean active;
private String installURL = null; private String installURL = null;
@ -99,6 +97,14 @@ public abstract class OsmandPlugin {
return true; return true;
} }
public boolean isMarketPlugin() {
return false;
}
public boolean isPaid() {
return false;
}
public boolean needsInstallation() { public boolean needsInstallation() {
return installURL != null; return installURL != null;
} }
@ -111,6 +117,14 @@ public abstract class OsmandPlugin {
return installURL; return installURL;
} }
public String getComponentId1() {
return null;
}
public String getComponentId2() {
return null;
}
public List<ApplicationMode> getAddedAppModes() { public List<ApplicationMode> getAddedAppModes() {
return Collections.emptyList(); return Collections.emptyList();
} }
@ -167,15 +181,15 @@ public abstract class OsmandPlugin {
allPlugins.add(new OsmandRasterMapsPlugin(app)); allPlugins.add(new OsmandRasterMapsPlugin(app));
allPlugins.add(new OsmandMonitoringPlugin(app)); allPlugins.add(new OsmandMonitoringPlugin(app));
checkMarketPlugin(app, new SRTMPlugin(app), true, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT); checkMarketPlugin(app, new SRTMPlugin(app));
// ? questionable - definitely not market plugin // ? questionable - definitely not market plugin
// checkMarketPlugin(app, enabledPlugins, new TouringViewPlugin(app), false, TouringViewPlugin.COMPONENT, null); // checkMarketPlugin(app, enabledPlugins, new TouringViewPlugin(app), false, TouringViewPlugin.COMPONENT, null);
checkMarketPlugin(app, new NauticalMapsPlugin(app), false, NauticalMapsPlugin.COMPONENT, null); checkMarketPlugin(app, new NauticalMapsPlugin(app));
checkMarketPlugin(app, new SkiMapsPlugin(app), false, SkiMapsPlugin.COMPONENT, null); checkMarketPlugin(app, new SkiMapsPlugin(app));
allPlugins.add(new AudioVideoNotesPlugin(app)); allPlugins.add(new AudioVideoNotesPlugin(app));
checkMarketPlugin(app, new ParkingPositionPlugin(app), false, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null); checkMarketPlugin(app, new ParkingPositionPlugin(app));
allPlugins.add(new AccessibilityPlugin(app)); allPlugins.add(new AccessibilityPlugin(app));
allPlugins.add(new OsmEditingPlugin(app)); allPlugins.add(new OsmEditingPlugin(app));
allPlugins.add(new OsmandDevelopmentPlugin(app)); allPlugins.add(new OsmandDevelopmentPlugin(app));
@ -193,7 +207,9 @@ public abstract class OsmandPlugin {
} }
public static void updateActivatedPlugins(OsmandApplication app) { public static void updateActivatedPlugins(OsmandApplication app) {
checkMarketPlugins(app); for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) {
updateMarketPlugin(app, plugin);
}
Set<String> enabledPlugins = app.getSettings().getEnabledPlugins(); Set<String> enabledPlugins = app.getSettings().getEnabledPlugins();
for (OsmandPlugin plugin : allPlugins) { for (OsmandPlugin plugin : allPlugins) {
if (enabledPlugins.contains(plugin.getId())) { if (enabledPlugins.contains(plugin.getId())) {
@ -215,17 +231,16 @@ public abstract class OsmandPlugin {
} }
} }
private static void checkMarketPlugin(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin, private static void checkMarketPlugin(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin) {
boolean paid, String id, String id2) { if (updateMarketPlugin(app, plugin)) {
if (updateMarketPlugin(app, plugin, paid, id, id2)) {
allPlugins.add(plugin); allPlugins.add(plugin);
} }
} }
private static boolean updateMarketPlugin(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin, private static boolean updateMarketPlugin(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin) {
boolean paid, String id, String id2) {
boolean marketEnabled = Version.isMarketEnabled(app); boolean marketEnabled = Version.isMarketEnabled(app);
boolean pckg = checkPackage(app, id, id2); boolean pckg = checkPluginPackage(app, plugin);
boolean paid = plugin.isPaid();
if ((Version.isDeveloperVersion(app) || !Version.isProductionVersion(app)) && !paid) { if ((Version.isDeveloperVersion(app) || !Version.isProductionVersion(app)) && !paid) {
// for test reasons // for test reasons
marketEnabled = false; marketEnabled = false;
@ -243,66 +258,41 @@ public abstract class OsmandPlugin {
if (!app.getSettings().getPlugins().contains("-" + plugin.getId())) { if (!app.getSettings().getPlugins().contains("-" + plugin.getId())) {
app.getSettings().enablePlugin(plugin.getId(), false); app.getSettings().enablePlugin(plugin.getId(), false);
} }
plugin.setInstallURL(Version.getUrlWithUtmRef(app, id)); plugin.setInstallURL(Version.getUrlWithUtmRef(app, plugin.getComponentId1()));
return true; return true;
} }
} }
return false; return false;
} }
private static void checkMarketPlugins(@NonNull OsmandApplication app) { public static boolean isPluginEnabledForMode(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin, @NonNull ApplicationMode mode) {
SRTMPlugin srtmPlugin = OsmandPlugin.getPlugin(SRTMPlugin.class); if (plugin.isMarketPlugin()) {
if (srtmPlugin != null) { boolean marketEnabled = Version.isMarketEnabled(app);
updateMarketPlugin(app, srtmPlugin, true, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT); boolean pckg = checkPluginPackage(app, plugin);
if ((Version.isDeveloperVersion(app) || !Version.isProductionVersion(app)) && !plugin.isPaid()) {
// for test reasons
marketEnabled = false;
} }
NauticalMapsPlugin nauticalMapsPlugin = OsmandPlugin.getPlugin(NauticalMapsPlugin.class); if (pckg || (!marketEnabled && !plugin.isPaid())) {
if (nauticalMapsPlugin != null) { return pckg && !app.getSettings().getPluginsForMode(mode).contains("-" + plugin.getId());
updateMarketPlugin(app, nauticalMapsPlugin, false, NauticalMapsPlugin.COMPONENT, null);
} }
SkiMapsPlugin skiMapsPlugin = OsmandPlugin.getPlugin(SkiMapsPlugin.class); return false;
if (skiMapsPlugin != null) { } else {
updateMarketPlugin(app, skiMapsPlugin, false, SkiMapsPlugin.COMPONENT, null); return app.getSettings().getEnabledPluginsForMode(mode).contains(plugin.getId());
}
ParkingPositionPlugin parkingPlugin = OsmandPlugin.getPlugin(ParkingPositionPlugin.class);
if (parkingPlugin != null) {
updateMarketPlugin(app, parkingPlugin, false, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null);
} }
} }
public static void checkInstalledMarketPlugins(@NonNull OsmandApplication app, @Nullable Activity activity) { public static void checkInstalledMarketPlugins(@NonNull OsmandApplication app, @Nullable Activity activity) {
for (OsmandPlugin plugin : OsmandPlugin.getAvailablePlugins()) { for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) {
if (plugin.getInstallURL() != null) { if (plugin.getInstallURL() != null && checkPluginPackage(app, plugin) && updateMarketPlugin(app, plugin)) {
if (plugin instanceof SRTMPlugin) {
if (checkPackage(app, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT)
&& updateMarketPlugin(app, plugin, true, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT)) {
plugin.onInstall(app, activity); plugin.onInstall(app, activity);
initPlugin(app, plugin); initPlugin(app, plugin);
} }
} else if (plugin instanceof NauticalMapsPlugin) {
if (checkPackage(app, NauticalMapsPlugin.COMPONENT, null)
&& updateMarketPlugin(app, plugin, false, NauticalMapsPlugin.COMPONENT, null)) {
plugin.onInstall(app, activity);
initPlugin(app, plugin);
}
} else if (plugin instanceof SkiMapsPlugin) {
if (checkPackage(app, SkiMapsPlugin.COMPONENT, null)
&& updateMarketPlugin(app, plugin, false, SkiMapsPlugin.COMPONENT, null)) {
plugin.onInstall(app, activity);
initPlugin(app, plugin);
}
} else if (plugin instanceof ParkingPositionPlugin) {
if (checkPackage(app, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null)
&& updateMarketPlugin(app, plugin, false, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null)) {
plugin.onInstall(app, activity);
initPlugin(app, plugin);
}
}
}
} }
} }
private static boolean checkPackage(OsmandApplication app, String id, String id2) { private static boolean checkPluginPackage(OsmandApplication app, OsmandPlugin plugin) {
return isPackageInstalled(id, app) || isPackageInstalled(id2, app) return isPackageInstalled(plugin.getComponentId1(), app) || isPackageInstalled(plugin.getComponentId2(), app)
|| InAppPurchaseHelper.isSubscribedToLiveUpdates(app); || InAppPurchaseHelper.isSubscribedToLiveUpdates(app);
} }
@ -477,6 +467,16 @@ public abstract class OsmandPlugin {
return lst; return lst;
} }
public static List<OsmandPlugin> getMarketPlugins() {
ArrayList<OsmandPlugin> lst = new ArrayList<OsmandPlugin>(allPlugins.size());
for (OsmandPlugin p : allPlugins) {
if (p.isMarketPlugin()) {
lst.add(p);
}
}
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()) {

View file

@ -1013,7 +1013,11 @@ public class OsmandSettings {
}.makeProfile(); }.makeProfile();
public Set<String> getEnabledPlugins() { public Set<String> getEnabledPlugins() {
String plugs = PLUGINS.get(); return getEnabledPluginsForMode(APPLICATION_MODE.get());
}
public Set<String> getEnabledPluginsForMode(ApplicationMode mode) {
String plugs = PLUGINS.getModeValue(mode);
StringTokenizer toks = new StringTokenizer(plugs, ","); StringTokenizer toks = new StringTokenizer(plugs, ",");
Set<String> res = new LinkedHashSet<String>(); Set<String> res = new LinkedHashSet<String>();
while (toks.hasMoreTokens()) { while (toks.hasMoreTokens()) {
@ -1039,11 +1043,11 @@ public class OsmandSettings {
return res; return res;
} }
public void enablePlugin(String pluginId, boolean enable) { public boolean enablePlugin(String pluginId, boolean enable) {
enablePluginForMode(pluginId, enable, APPLICATION_MODE.get()); return enablePluginForMode(pluginId, enable, APPLICATION_MODE.get());
} }
public void enablePluginForMode(String pluginId, boolean enable, ApplicationMode mode) { public boolean enablePluginForMode(String pluginId, boolean enable, ApplicationMode mode) {
Set<String> set = getPluginsForMode(mode); Set<String> set = getPluginsForMode(mode);
if (enable) { if (enable) {
set.remove("-" + pluginId); set.remove("-" + pluginId);
@ -1061,8 +1065,9 @@ public class OsmandSettings {
} }
} }
if (!serialization.toString().equals(PLUGINS.getModeValue(mode))) { if (!serialization.toString().equals(PLUGINS.getModeValue(mode))) {
PLUGINS.setModeValue(mode, serialization.toString()); return PLUGINS.setModeValue(mode, serialization.toString());
} }
return false;
} }

View file

@ -43,6 +43,16 @@ public class NauticalMapsPlugin extends OsmandPlugin {
return R.drawable.nautical_map; return R.drawable.nautical_map;
} }
@Override
public boolean isMarketPlugin() {
return true;
}
@Override
public String getComponentId1() {
return COMPONENT;
}
@Override @Override
public String getDescription() { public String getDescription() {
return app.getString(net.osmand.plus.R.string.plugin_nautical_descr); return app.getString(net.osmand.plus.R.string.plugin_nautical_descr);

View file

@ -174,6 +174,15 @@ public class ParkingPositionPlugin extends OsmandPlugin {
return "feature_articles/parking-plugin.html"; return "feature_articles/parking-plugin.html";
} }
@Override
public boolean isMarketPlugin() {
return true;
}
@Override
public String getComponentId1() {
return PARKING_PLUGIN_COMPONENT;
}
@Override @Override
public void registerLayers(MapActivity activity) { public void registerLayers(MapActivity activity) {

View file

@ -262,12 +262,13 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
if (plugin instanceof SkiMapsPlugin || plugin instanceof NauticalMapsPlugin) { if (plugin instanceof SkiMapsPlugin || plugin instanceof NauticalMapsPlugin) {
continue; continue;
} }
boolean pluginEnabled = OsmandPlugin.isPluginEnabledForMode(app, plugin, getSelectedAppMode());
SwitchPreferenceEx preference = new SwitchPreferenceEx(ctx); SwitchPreferenceEx preference = new SwitchPreferenceEx(ctx);
preference.setPersistent(false); preference.setPersistent(false);
preference.setKey(plugin.getId()); preference.setKey(plugin.getId());
preference.setTitle(plugin.getName()); preference.setTitle(plugin.getName());
preference.setChecked(plugin.isActive()); preference.setChecked(pluginEnabled);
preference.setIcon(getPluginIcon(plugin)); preference.setIcon(getPluginIcon(plugin, pluginEnabled));
preference.setIntent(getPluginIntent(plugin)); preference.setIntent(getPluginIntent(plugin));
preference.setLayoutResource(R.layout.preference_dialog_and_switch); preference.setLayoutResource(R.layout.preference_dialog_and_switch);
@ -275,9 +276,9 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
} }
} }
private Drawable getPluginIcon(OsmandPlugin plugin) { private Drawable getPluginIcon(OsmandPlugin plugin, boolean pluginEnabled) {
int iconResId = plugin.getLogoResourceId(); int iconResId = plugin.getLogoResourceId();
return plugin.isActive() ? getActiveIcon(iconResId) : getIcon(iconResId, isNightMode() ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light); return pluginEnabled ? getActiveIcon(iconResId) : getIcon(iconResId, isNightMode() ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light);
} }
private Intent getPluginIntent(OsmandPlugin plugin) { private Intent getPluginIntent(OsmandPlugin plugin) {
@ -343,8 +344,15 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
if (plugin != null) { if (plugin != null) {
if (newValue instanceof Boolean) { if (newValue instanceof Boolean) {
if ((plugin.isActive() || !plugin.needsInstallation())) { if ((plugin.isActive() || !plugin.needsInstallation())) {
if (OsmandPlugin.enablePlugin(getActivity(), app, plugin, (Boolean) newValue)) { ApplicationMode selectedMode = getSelectedAppMode();
preference.setIcon(getPluginIcon(plugin)); boolean pluginChanged;
if (selectedMode.equals(settings.APPLICATION_MODE.get())) {
pluginChanged = OsmandPlugin.enablePlugin(getActivity(), app, plugin, (Boolean) newValue);
} else {
pluginChanged = settings.enablePluginForMode(plugin.getId(), (Boolean) newValue, selectedMode);
}
if (pluginChanged) {
preference.setIcon(getPluginIcon(plugin, (Boolean) newValue));
return true; return true;
} }
} else if (plugin.needsInstallation() && preference.getIntent() != null) { } else if (plugin.needsInstallation() && preference.getIntent() != null) {

View file

@ -43,6 +43,15 @@ public class SkiMapsPlugin extends OsmandPlugin {
return R.drawable.ski_map; return R.drawable.ski_map;
} }
@Override
public boolean isMarketPlugin() {
return true;
}
@Override
public String getComponentId1() {
return COMPONENT;
}
@Override @Override
public String getHelpFileName() { public String getHelpFileName() {

View file

@ -44,6 +44,9 @@ public class SRTMPlugin extends OsmandPlugin {
public static final String ID = "osmand.srtm"; public static final String ID = "osmand.srtm";
public static final String FREE_ID = "osmand.srtm.paid"; public static final String FREE_ID = "osmand.srtm.paid";
private static final String SRTM_PLUGIN_COMPONENT_PAID = "net.osmand.srtmPlugin.paid";
private static final String SRTM_PLUGIN_COMPONENT = "net.osmand.srtmPlugin";
public static final String CONTOUR_LINES_ATTR = "contourLines"; public static final String CONTOUR_LINES_ATTR = "contourLines";
public static final String CONTOUR_LINES_SCHEME_ATTR = "contourColorScheme"; public static final String CONTOUR_LINES_SCHEME_ATTR = "contourColorScheme";
public static final String CONTOUR_LINES_DISABLED_VALUE = "disabled"; public static final String CONTOUR_LINES_DISABLED_VALUE = "disabled";
@ -81,6 +84,26 @@ public class SRTMPlugin extends OsmandPlugin {
return super.needsInstallation() && !InAppPurchaseHelper.isSubscribedToLiveUpdates(app); return super.needsInstallation() && !InAppPurchaseHelper.isSubscribedToLiveUpdates(app);
} }
@Override
public boolean isMarketPlugin() {
return true;
}
@Override
public boolean isPaid() {
return true;
}
@Override
public String getComponentId1() {
return SRTM_PLUGIN_COMPONENT_PAID;
}
@Override
public String getComponentId2() {
return SRTM_PLUGIN_COMPONENT;
}
@Override @Override
public String getDescription() { public String getDescription() {
return app.getString(R.string.srtm_plugin_description); return app.getString(R.string.srtm_plugin_description);