Make plugins and connected apps global again

This commit is contained in:
Chumva 2019-12-02 12:22:53 +02:00
parent 5b0999cc37
commit 4b702b6ea0
4 changed files with 20 additions and 79 deletions

View file

@ -96,7 +96,6 @@ import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -1835,8 +1834,7 @@ public class OsmandAidlApi {
public boolean switchEnabled(@NonNull ConnectedApp connectedApp) { public boolean switchEnabled(@NonNull ConnectedApp connectedApp) {
connectedApp.enabled = !connectedApp.enabled; connectedApp.enabled = !connectedApp.enabled;
ApplicationMode selectedAppMode = app.getSettings().APPLICATION_MODE.get(); return saveConnectedApps();
return saveConnectedApps(selectedAppMode, connectedApps);
} }
public boolean isAppEnabled(@NonNull String pack) { public boolean isAppEnabled(@NonNull String pack) {
@ -1844,22 +1842,12 @@ public class OsmandAidlApi {
if (app == null) { if (app == null) {
app = new ConnectedApp(pack, true); app = new ConnectedApp(pack, true);
connectedApps.put(pack, app); connectedApps.put(pack, app);
saveNewConnectedApp(app); saveConnectedApps();
} }
return app.enabled; return app.enabled;
} }
private void saveNewConnectedApp(ConnectedApp connectedApp) { private boolean saveConnectedApps() {
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
Map<String, ConnectedApp> connectedApps = loadConnectedAppsForMode(mode);
if (!connectedApps.containsKey(connectedApp.pack)) {
connectedApps.put(connectedApp.pack, connectedApp);
saveConnectedApps(mode, connectedApps);
}
}
}
private boolean saveConnectedApps(ApplicationMode mode, Map<String, ConnectedApp> connectedApps) {
try { try {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
for (ConnectedApp app : connectedApps.values()) { for (ConnectedApp app : connectedApps.values()) {
@ -1868,7 +1856,7 @@ public class OsmandAidlApi {
obj.put(ConnectedApp.PACK_KEY, app.pack); obj.put(ConnectedApp.PACK_KEY, app.pack);
array.put(obj); array.put(obj);
} }
return app.getSettings().API_CONNECTED_APPS_JSON.setModeValue(mode, array.toString()); return app.getSettings().API_CONNECTED_APPS_JSON.set(array.toString());
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1876,16 +1864,9 @@ public class OsmandAidlApi {
} }
public void loadConnectedApps() { public void loadConnectedApps() {
ApplicationMode selectedAppMode = app.getSettings().APPLICATION_MODE.get();
Map<String, ConnectedApp> appsForMode = loadConnectedAppsForMode(selectedAppMode);
connectedApps.clear();
connectedApps.putAll(appsForMode);
}
private Map<String, ConnectedApp> loadConnectedAppsForMode(ApplicationMode mode) {
Map<String, ConnectedApp> connectedApps = new HashMap<>();
try { try {
JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.getModeValue(mode)); connectedApps.clear();
JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.get());
for (int i = 0; i < array.length(); i++) { for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i); JSONObject obj = array.getJSONObject(i);
String pack = obj.optString(ConnectedApp.PACK_KEY, ""); String pack = obj.optString(ConnectedApp.PACK_KEY, "");
@ -1895,7 +1876,6 @@ public class OsmandAidlApi {
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
return connectedApps;
} }
boolean setNavDrawerLogo(@Nullable String uri) { boolean setNavDrawerLogo(@Nullable String uri) {

View file

@ -267,23 +267,6 @@ public abstract class OsmandPlugin {
return false; return false;
} }
public static boolean isPluginEnabledForMode(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin, @NonNull ApplicationMode mode) {
if (plugin.isMarketPlugin()) {
boolean marketEnabled = Version.isMarketEnabled(app);
boolean pckg = checkPluginPackage(app, plugin);
if ((Version.isDeveloperVersion(app) || !Version.isProductionVersion(app)) && !plugin.isPaid()) {
// for test reasons
marketEnabled = false;
}
if (pckg || (!marketEnabled && !plugin.isPaid())) {
return pckg && !app.getSettings().getPluginsForMode(mode).contains("-" + plugin.getId());
}
return false;
} else {
return app.getSettings().getEnabledPluginsForMode(mode).contains(plugin.getId());
}
}
public static void checkInstalledMarketPlugins(@NonNull OsmandApplication app, @Nullable Activity activity) { public static void checkInstalledMarketPlugins(@NonNull OsmandApplication app, @Nullable Activity activity) {
Set<String> enabledPlugins = app.getSettings().getEnabledPlugins(); Set<String> enabledPlugins = app.getSettings().getEnabledPlugins();
for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) { for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) {

View file

@ -437,8 +437,6 @@ public class OsmandSettings {
OsmandAidlApi aidlApi = ctx.getAidlApi(); OsmandAidlApi aidlApi = ctx.getAidlApi();
if (aidlApi != null) { if (aidlApi != null) {
aidlApi.loadConnectedApps();
OsmandPlugin.updateActivatedPlugins(ctx);
ctx.poiFilters.loadSelectedPoiFilters(); ctx.poiFilters.loadSelectedPoiFilters();
} }
@ -1017,14 +1015,10 @@ public class OsmandSettings {
return super.getProfileDefaultValue(mode); return super.getProfileDefaultValue(mode);
} }
} }
}.makeProfile(); }.makeGlobal();
public Set<String> getEnabledPlugins() { public Set<String> getEnabledPlugins() {
return getEnabledPluginsForMode(APPLICATION_MODE.get()); String plugs = PLUGINS.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()) {
@ -1037,11 +1031,7 @@ public class OsmandSettings {
} }
public Set<String> getPlugins() { public Set<String> getPlugins() {
return getPluginsForMode(APPLICATION_MODE.get()); String plugs = PLUGINS.get();
}
public Set<String> getPluginsForMode(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()) {
@ -1051,11 +1041,7 @@ public class OsmandSettings {
} }
public boolean enablePlugin(String pluginId, boolean enable) { public boolean enablePlugin(String pluginId, boolean enable) {
return enablePluginForMode(pluginId, enable, APPLICATION_MODE.get()); Set<String> set = getPlugins();
}
public boolean enablePluginForMode(String pluginId, boolean enable, ApplicationMode mode) {
Set<String> set = getPluginsForMode(mode);
if (enable) { if (enable) {
set.remove("-" + pluginId); set.remove("-" + pluginId);
set.add(pluginId); set.add(pluginId);
@ -1071,8 +1057,8 @@ public class OsmandSettings {
serialization.append(","); serialization.append(",");
} }
} }
if (!serialization.toString().equals(PLUGINS.getModeValue(mode))) { if (!serialization.toString().equals(PLUGINS.get())) {
return PLUGINS.setModeValue(mode, serialization.toString()); return PLUGINS.set(serialization.toString());
} }
return false; return false;
} }
@ -1172,7 +1158,7 @@ public class OsmandSettings {
return super.getProfileDefaultValue(mode); return super.getProfileDefaultValue(mode);
} }
} }
}.makeProfile(); }.makeGlobal();
public final CommonPreference<Integer> NUMBER_OF_STARTS_FIRST_XMAS_SHOWN = new IntPreference("number_of_starts_first_xmas_shown", 0).makeGlobal(); public final CommonPreference<Integer> NUMBER_OF_STARTS_FIRST_XMAS_SHOWN = new IntPreference("number_of_starts_first_xmas_shown", 0).makeGlobal();

View file

@ -262,13 +262,12 @@ 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(pluginEnabled); preference.setChecked(plugin.isActive());
preference.setIcon(getPluginIcon(plugin, pluginEnabled)); preference.setIcon(getPluginIcon(plugin));
preference.setIntent(getPluginIntent(plugin)); preference.setIntent(getPluginIntent(plugin));
preference.setLayoutResource(R.layout.preference_dialog_and_switch); preference.setLayoutResource(R.layout.preference_dialog_and_switch);
@ -276,9 +275,9 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
} }
} }
private Drawable getPluginIcon(OsmandPlugin plugin, boolean pluginEnabled) { private Drawable getPluginIcon(OsmandPlugin plugin) {
int iconResId = plugin.getLogoResourceId(); int iconResId = plugin.getLogoResourceId();
return pluginEnabled ? getActiveIcon(iconResId) : getIcon(iconResId, isNightMode() ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light); return plugin.isActive() ? 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) {
@ -349,15 +348,8 @@ 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())) {
ApplicationMode selectedMode = getSelectedAppMode(); if (OsmandPlugin.enablePlugin(getActivity(), app, plugin, (Boolean) newValue)) {
boolean pluginChanged; preference.setIcon(getPluginIcon(plugin));
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) {