Aidl fixes

This commit is contained in:
Chumva 2019-10-02 17:10:42 +03:00
parent 86077b2d65
commit 937b1b5120
3 changed files with 50 additions and 20 deletions

View file

@ -111,6 +111,7 @@ 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;
@ -243,6 +244,7 @@ public class OsmandAidlApi {
public void onDestroyMapActivity(MapActivity mapActivity) {
app.getAppCustomization().removeListener(mapActivity);
aMapPointUpdateListener = null;
mapActivityActive = false;
for (BroadcastReceiver b : receivers.values()) {
if(b == null) {
@ -1819,9 +1821,10 @@ public class OsmandAidlApi {
return res;
}
public boolean switchEnabled(@NonNull ConnectedApp app) {
app.enabled = !app.enabled;
return saveConnectedApps();
public boolean switchEnabled(@NonNull ConnectedApp connectedApp) {
connectedApp.enabled = !connectedApp.enabled;
ApplicationMode selectedAppMode = app.getSettings().APPLICATION_MODE.get();
return saveConnectedApps(selectedAppMode, connectedApps);
}
boolean isAppEnabled(@NonNull String pack) {
@ -1829,12 +1832,22 @@ public class OsmandAidlApi {
if (app == null) {
app = new ConnectedApp(pack, true);
connectedApps.put(pack, app);
saveConnectedApps();
saveNewConnectedApp(app);
}
return app.enabled;
}
private boolean saveConnectedApps() {
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) {
try {
JSONArray array = new JSONArray();
for (ConnectedApp app : connectedApps.values()) {
@ -1843,7 +1856,7 @@ public class OsmandAidlApi {
obj.put(ConnectedApp.PACK_KEY, app.pack);
array.put(obj);
}
return app.getSettings().API_CONNECTED_APPS_JSON.set(array.toString());
return app.getSettings().API_CONNECTED_APPS_JSON.setModeValue(mode, array.toString());
} catch (JSONException e) {
e.printStackTrace();
}
@ -1851,9 +1864,16 @@ 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 {
connectedApps.clear();
JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.get());
JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.getModeValue(mode));
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
String pack = obj.optString(ConnectedApp.PACK_KEY, "");
@ -1863,6 +1883,7 @@ public class OsmandAidlApi {
} catch (JSONException e) {
e.printStackTrace();
}
return connectedApps;
}
boolean setNavDrawerLogo(@Nullable String uri) {

View file

@ -15,6 +15,7 @@ import android.widget.ArrayAdapter;
import net.osmand.IProgress;
import net.osmand.IndexConstants;
import net.osmand.aidl.OsmandAidlApi;
import net.osmand.aidl.customization.CustomizationInfoParams;
import net.osmand.aidl.customization.OsmandSettingsParams;
import net.osmand.aidl.customization.SetWidgetsParams;
@ -524,18 +525,21 @@ public class OsmAndAppCustomization {
JSONObject allItems = new JSONObject(app.getSettings().API_NAV_DRAWER_ITEMS_JSON.get());
for (Iterator<?> it = allItems.keys(); it.hasNext(); ) {
String appPackage = (String) it.next();
JSONArray jArray = allItems.getJSONArray(appPackage);
List<NavDrawerItem> list = new ArrayList<>();
for (int i = 0; i < jArray.length(); i++) {
JSONObject obj = jArray.getJSONObject(i);
list.add(new NavDrawerItem(
obj.optString(NavDrawerItem.NAME_KEY),
obj.optString(NavDrawerItem.URI_KEY),
obj.optString(NavDrawerItem.ICON_NAME_KEY),
obj.optInt(NavDrawerItem.FLAGS_KEY, -1)
));
OsmandAidlApi.ConnectedApp connectedApp = app.getAidlApi().getConnectedApp(appPackage);
if (connectedApp != null && connectedApp.isEnabled()) {
JSONArray jArray = allItems.getJSONArray(appPackage);
List<NavDrawerItem> list = new ArrayList<>();
for (int i = 0; i < jArray.length(); i++) {
JSONObject obj = jArray.getJSONObject(i);
list.add(new NavDrawerItem(
obj.optString(NavDrawerItem.NAME_KEY),
obj.optString(NavDrawerItem.URI_KEY),
obj.optString(NavDrawerItem.ICON_NAME_KEY),
obj.optInt(NavDrawerItem.FLAGS_KEY, -1)
));
}
res.put(appPackage, list);
}
res.put(appPackage, list);
}
} catch (JSONException e) {
e.printStackTrace();

View file

@ -922,7 +922,12 @@ public class OsmandSettings {
public final CommonPreference<Boolean> SHOW_OSMAND_WELCOME_SCREEN = new BooleanPreference("show_osmand_welcome_screen", true).makeGlobal();
public final CommonPreference<String> API_NAV_DRAWER_ITEMS_JSON = new StringPreference("api_nav_drawer_items_json", "{}").makeGlobal();
public final CommonPreference<String> API_CONNECTED_APPS_JSON = new StringPreference("api_connected_apps_json", "[]").makeProfile();
public final CommonPreference<String> API_CONNECTED_APPS_JSON = new StringPreference("api_connected_apps_json", "[]") {
@Override
public String getModeValue(ApplicationMode mode) {
return getValue(getProfilePreferences(mode), "[]");
}
}.makeProfile();
public final CommonPreference<Integer> NUMBER_OF_STARTS_FIRST_XMAS_SHOWN = new IntPreference("number_of_starts_first_xmas_shown", 0).makeGlobal();