From 75e5ee8394faa1ebca1044517a3e690eaaf0720b Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Tue, 31 Jul 2018 15:16:59 +0300 Subject: [PATCH] Change JSON structure --- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 124 +++++++++--------- .../src/net/osmand/plus/OsmandSettings.java | 2 +- 2 files changed, 66 insertions(+), 60 deletions(-) diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 31705bd657..5274728e4b 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -75,6 +75,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -1202,51 +1203,53 @@ public class OsmandAidlApi { boolean addNavDrawerItems(String appPackage, List items) { if (!TextUtils.isEmpty(appPackage) && items != null && !items.isEmpty()) { - List existing = getNavDrawerItems(); - for (Iterator it = existing.iterator(); it.hasNext(); ) { - if (appPackage.equals(it.next().appPackage)) { - it.remove(); - } - } - int counter = 0; - for (net.osmand.aidl.navdrawer.NavDrawerItem item : items) { - existing.add(new NavDrawerItem(item.getName(), appPackage, item.getUri(), item.getIconName(), item.getFlags())); - if (++counter >= MAX_NAV_DRAWER_ITEMS_PER_APP) { + List newItems = new ArrayList<>(MAX_NAV_DRAWER_ITEMS_PER_APP); + boolean success = true; + for (int i = 0; i < items.size() && i <= MAX_NAV_DRAWER_ITEMS_PER_APP; i++) { + net.osmand.aidl.navdrawer.NavDrawerItem item = items.get(i); + String name = item.getName(); + String uri = item.getUri(); + if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(uri)) { + newItems.add(new NavDrawerItem(name, uri, item.getIconName(), item.getFlags())); + } else { + success = false; break; } } - saveNavDrawerItems(existing); - return true; + if (success) { + saveNavDrawerItems(appPackage, newItems); + } + return success; } return false; } public void registerNavDrawerItems(final Activity activity, ContextMenuAdapter adapter) { PackageManager pm = activity.getPackageManager(); - for (NavDrawerItem item : getNavDrawerItems()) { - if (TextUtils.isEmpty(item.name) || TextUtils.isEmpty(item.appPackage) || TextUtils.isEmpty(item.uri)) { - continue; - } - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.uri)); - if (intent.resolveActivity(pm) == null) { - intent = pm.getLaunchIntentForPackage(item.appPackage); - } - if (intent != null) { - if (item.flags != -1) { - intent.addFlags(item.flags); + for (Map.Entry> entry : getNavDrawerItems().entrySet()) { + String appPackage = entry.getKey(); + for (NavDrawerItem item : entry.getValue()) { + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.uri)); + if (intent.resolveActivity(pm) == null) { + intent = pm.getLaunchIntentForPackage(appPackage); + } + if (intent != null) { + if (item.flags != -1) { + intent.addFlags(item.flags); + } + final Intent finalIntent = intent; + adapter.addItem(new ContextMenuItem.ItemBuilder() + .setTitle(item.name) + .setIcon(getIconId(item.iconName)) + .setListener(new ContextMenuAdapter.ItemClickListener() { + @Override + public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked, int[] viewCoordinates) { + activity.startActivity(finalIntent); + return true; + } + }) + .createItem()); } - final Intent finalIntent = intent; - adapter.addItem(new ContextMenuItem.ItemBuilder() - .setTitle(item.name) - .setIcon(getIconId(item.iconName)) - .setListener(new ContextMenuAdapter.ItemClickListener() { - @Override - public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked, int[] viewCoordinates) { - activity.startActivity(finalIntent); - return true; - } - }) - .createItem()); } } } @@ -1259,37 +1262,43 @@ public class OsmandAidlApi { return -1; } - private void saveNavDrawerItems(List items) { - JSONArray jArray = new JSONArray(); - for (NavDrawerItem item : items) { - try { + private void saveNavDrawerItems(String appPackage, List items) { + try { + JSONArray jArray = new JSONArray(); + for (NavDrawerItem item : items) { JSONObject obj = new JSONObject(); obj.put(NavDrawerItem.NAME_KEY, item.name); - obj.put(NavDrawerItem.APP_PACKAGE_KEY, item.appPackage); obj.put(NavDrawerItem.URI_KEY, item.uri); obj.put(NavDrawerItem.ICON_NAME_KEY, item.iconName); obj.put(NavDrawerItem.FLAGS_KEY, item.flags); jArray.put(obj); - } catch (JSONException e) { - e.printStackTrace(); } + JSONObject allItems = new JSONObject(app.getSettings().API_NAV_DRAWER_ITEMS_JSON.get()); + allItems.put(appPackage, jArray); + app.getSettings().API_NAV_DRAWER_ITEMS_JSON.set(allItems.toString()); + } catch (JSONException e) { + e.printStackTrace(); } - app.getSettings().API_NAV_DRAWER_ITEMS_JSON.set(jArray.toString()); } - private List getNavDrawerItems() { - List res = new ArrayList<>(); + private Map> getNavDrawerItems() { + Map> res = new LinkedHashMap<>(); try { - JSONArray jArray = new JSONArray(app.getSettings().API_NAV_DRAWER_ITEMS_JSON.get()); - for (int i = 0; i < jArray.length(); i++) { - JSONObject obj = jArray.getJSONObject(i); - res.add(new NavDrawerItem( - obj.optString(NavDrawerItem.NAME_KEY), - obj.optString(NavDrawerItem.APP_PACKAGE_KEY), - obj.optString(NavDrawerItem.URI_KEY), - obj.optString(NavDrawerItem.ICON_NAME_KEY), - obj.optInt(NavDrawerItem.FLAGS_KEY, -1) - )); + 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 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); } } catch (JSONException e) { e.printStackTrace(); @@ -1300,20 +1309,17 @@ public class OsmandAidlApi { private static class NavDrawerItem { static final String NAME_KEY = "name"; - static final String APP_PACKAGE_KEY = "pack"; static final String URI_KEY = "uri"; static final String ICON_NAME_KEY = "icon_name"; static final String FLAGS_KEY = "flags"; private String name; - private String appPackage; private String uri; private String iconName; private int flags; - NavDrawerItem(String name, String appPackage, String uri, String iconName, int flags) { + NavDrawerItem(String name, String uri, String iconName, int flags) { this.name = name; - this.appPackage = appPackage; this.uri = uri; this.iconName = iconName; this.flags = flags; diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 64a32562f9..c1cb454bc2 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -742,7 +742,7 @@ public class OsmandSettings { public final CommonPreference SHOW_DASHBOARD_ON_START = new BooleanPreference("should_show_dashboard_on_start", false).makeGlobal(); public final CommonPreference SHOW_DASHBOARD_ON_MAP_SCREEN = new BooleanPreference("show_dashboard_on_map_screen", false).makeGlobal(); - public final CommonPreference API_NAV_DRAWER_ITEMS_JSON = new StringPreference("api_nav_drawer_items_json", "").makeGlobal(); + public final CommonPreference API_NAV_DRAWER_ITEMS_JSON = new StringPreference("api_nav_drawer_items_json", "{}").makeGlobal(); public final CommonPreference NUMBER_OF_STARTS_FIRST_XMAS_SHOWN = new IntPreference("number_of_starts_first_xmas_shown", 0).makeGlobal();