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.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@ -1835,8 +1834,7 @@ public class OsmandAidlApi {
public boolean switchEnabled(@NonNull ConnectedApp connectedApp) {
connectedApp.enabled = !connectedApp.enabled;
ApplicationMode selectedAppMode = app.getSettings().APPLICATION_MODE.get();
return saveConnectedApps(selectedAppMode, connectedApps);
return saveConnectedApps();
}
public boolean isAppEnabled(@NonNull String pack) {
@ -1844,22 +1842,12 @@ public class OsmandAidlApi {
if (app == null) {
app = new ConnectedApp(pack, true);
connectedApps.put(pack, app);
saveNewConnectedApp(app);
saveConnectedApps();
}
return app.enabled;
}
private void saveNewConnectedApp(ConnectedApp connectedApp) {
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) {
private boolean saveConnectedApps() {
try {
JSONArray array = new JSONArray();
for (ConnectedApp app : connectedApps.values()) {
@ -1868,7 +1856,7 @@ public class OsmandAidlApi {
obj.put(ConnectedApp.PACK_KEY, app.pack);
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) {
e.printStackTrace();
}
@ -1876,16 +1864,9 @@ public class OsmandAidlApi {
}
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 {
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++) {
JSONObject obj = array.getJSONObject(i);
String pack = obj.optString(ConnectedApp.PACK_KEY, "");
@ -1895,7 +1876,6 @@ public class OsmandAidlApi {
} catch (JSONException e) {
e.printStackTrace();
}
return connectedApps;
}
boolean setNavDrawerLogo(@Nullable String uri) {

View file

@ -267,23 +267,6 @@ public abstract class OsmandPlugin {
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) {
Set<String> enabledPlugins = app.getSettings().getEnabledPlugins();
for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) {

View file

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

View file

@ -262,13 +262,12 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
if (plugin instanceof SkiMapsPlugin || plugin instanceof NauticalMapsPlugin) {
continue;
}
boolean pluginEnabled = OsmandPlugin.isPluginEnabledForMode(app, plugin, getSelectedAppMode());
SwitchPreferenceEx preference = new SwitchPreferenceEx(ctx);
preference.setPersistent(false);
preference.setKey(plugin.getId());
preference.setTitle(plugin.getName());
preference.setChecked(pluginEnabled);
preference.setIcon(getPluginIcon(plugin, pluginEnabled));
preference.setChecked(plugin.isActive());
preference.setIcon(getPluginIcon(plugin));
preference.setIntent(getPluginIntent(plugin));
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();
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) {
@ -349,15 +348,8 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
if (plugin != null) {
if (newValue instanceof Boolean) {
if ((plugin.isActive() || !plugin.needsInstallation())) {
ApplicationMode selectedMode = getSelectedAppMode();
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));
if (OsmandPlugin.enablePlugin(getActivity(), app, plugin, (Boolean) newValue)) {
preference.setIcon(getPluginIcon(plugin));
return true;
}
} else if (plugin.needsInstallation() && preference.getIntent() != null) {