Change JSON structure

This commit is contained in:
Alex Sytnyk 2018-07-31 15:16:59 +03:00
parent 5139c9fe5a
commit 75e5ee8394
2 changed files with 66 additions and 60 deletions

View file

@ -75,6 +75,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -1202,34 +1203,35 @@ public class OsmandAidlApi {
boolean addNavDrawerItems(String appPackage, List<net.osmand.aidl.navdrawer.NavDrawerItem> items) { boolean addNavDrawerItems(String appPackage, List<net.osmand.aidl.navdrawer.NavDrawerItem> items) {
if (!TextUtils.isEmpty(appPackage) && items != null && !items.isEmpty()) { if (!TextUtils.isEmpty(appPackage) && items != null && !items.isEmpty()) {
List<NavDrawerItem> existing = getNavDrawerItems(); List<NavDrawerItem> newItems = new ArrayList<>(MAX_NAV_DRAWER_ITEMS_PER_APP);
for (Iterator<NavDrawerItem> it = existing.iterator(); it.hasNext(); ) { boolean success = true;
if (appPackage.equals(it.next().appPackage)) { for (int i = 0; i < items.size() && i <= MAX_NAV_DRAWER_ITEMS_PER_APP; i++) {
it.remove(); net.osmand.aidl.navdrawer.NavDrawerItem item = items.get(i);
} String name = item.getName();
} String uri = item.getUri();
int counter = 0; if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(uri)) {
for (net.osmand.aidl.navdrawer.NavDrawerItem item : items) { newItems.add(new NavDrawerItem(name, uri, item.getIconName(), item.getFlags()));
existing.add(new NavDrawerItem(item.getName(), appPackage, item.getUri(), item.getIconName(), item.getFlags())); } else {
if (++counter >= MAX_NAV_DRAWER_ITEMS_PER_APP) { success = false;
break; break;
} }
} }
saveNavDrawerItems(existing); if (success) {
return true; saveNavDrawerItems(appPackage, newItems);
}
return success;
} }
return false; return false;
} }
public void registerNavDrawerItems(final Activity activity, ContextMenuAdapter adapter) { public void registerNavDrawerItems(final Activity activity, ContextMenuAdapter adapter) {
PackageManager pm = activity.getPackageManager(); PackageManager pm = activity.getPackageManager();
for (NavDrawerItem item : getNavDrawerItems()) { for (Map.Entry<String, List<NavDrawerItem>> entry : getNavDrawerItems().entrySet()) {
if (TextUtils.isEmpty(item.name) || TextUtils.isEmpty(item.appPackage) || TextUtils.isEmpty(item.uri)) { String appPackage = entry.getKey();
continue; for (NavDrawerItem item : entry.getValue()) {
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.uri)); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.uri));
if (intent.resolveActivity(pm) == null) { if (intent.resolveActivity(pm) == null) {
intent = pm.getLaunchIntentForPackage(item.appPackage); intent = pm.getLaunchIntentForPackage(appPackage);
} }
if (intent != null) { if (intent != null) {
if (item.flags != -1) { if (item.flags != -1) {
@ -1250,6 +1252,7 @@ public class OsmandAidlApi {
} }
} }
} }
}
private int getIconId(@Nullable String iconName) { private int getIconId(@Nullable String iconName) {
if (!TextUtils.isEmpty(iconName)) { if (!TextUtils.isEmpty(iconName)) {
@ -1259,38 +1262,44 @@ public class OsmandAidlApi {
return -1; return -1;
} }
private void saveNavDrawerItems(List<NavDrawerItem> items) { private void saveNavDrawerItems(String appPackage, List<NavDrawerItem> items) {
try {
JSONArray jArray = new JSONArray(); JSONArray jArray = new JSONArray();
for (NavDrawerItem item : items) { for (NavDrawerItem item : items) {
try {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put(NavDrawerItem.NAME_KEY, item.name); 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.URI_KEY, item.uri);
obj.put(NavDrawerItem.ICON_NAME_KEY, item.iconName); obj.put(NavDrawerItem.ICON_NAME_KEY, item.iconName);
obj.put(NavDrawerItem.FLAGS_KEY, item.flags); obj.put(NavDrawerItem.FLAGS_KEY, item.flags);
jArray.put(obj); jArray.put(obj);
}
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) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
app.getSettings().API_NAV_DRAWER_ITEMS_JSON.set(jArray.toString());
}
private List<NavDrawerItem> getNavDrawerItems() { private Map<String, List<NavDrawerItem>> getNavDrawerItems() {
List<NavDrawerItem> res = new ArrayList<>(); Map<String, List<NavDrawerItem>> res = new LinkedHashMap<>();
try { try {
JSONArray jArray = new JSONArray(app.getSettings().API_NAV_DRAWER_ITEMS_JSON.get()); 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++) { for (int i = 0; i < jArray.length(); i++) {
JSONObject obj = jArray.getJSONObject(i); JSONObject obj = jArray.getJSONObject(i);
res.add(new NavDrawerItem( list.add(new NavDrawerItem(
obj.optString(NavDrawerItem.NAME_KEY), obj.optString(NavDrawerItem.NAME_KEY),
obj.optString(NavDrawerItem.APP_PACKAGE_KEY),
obj.optString(NavDrawerItem.URI_KEY), obj.optString(NavDrawerItem.URI_KEY),
obj.optString(NavDrawerItem.ICON_NAME_KEY), obj.optString(NavDrawerItem.ICON_NAME_KEY),
obj.optInt(NavDrawerItem.FLAGS_KEY, -1) obj.optInt(NavDrawerItem.FLAGS_KEY, -1)
)); ));
} }
res.put(appPackage, list);
}
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1300,20 +1309,17 @@ public class OsmandAidlApi {
private static class NavDrawerItem { private static class NavDrawerItem {
static final String NAME_KEY = "name"; static final String NAME_KEY = "name";
static final String APP_PACKAGE_KEY = "pack";
static final String URI_KEY = "uri"; static final String URI_KEY = "uri";
static final String ICON_NAME_KEY = "icon_name"; static final String ICON_NAME_KEY = "icon_name";
static final String FLAGS_KEY = "flags"; static final String FLAGS_KEY = "flags";
private String name; private String name;
private String appPackage;
private String uri; private String uri;
private String iconName; private String iconName;
private int flags; 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.name = name;
this.appPackage = appPackage;
this.uri = uri; this.uri = uri;
this.iconName = iconName; this.iconName = iconName;
this.flags = flags; this.flags = flags;

View file

@ -742,7 +742,7 @@ public class OsmandSettings {
public final CommonPreference<Boolean> SHOW_DASHBOARD_ON_START = new BooleanPreference("should_show_dashboard_on_start", false).makeGlobal(); public final CommonPreference<Boolean> SHOW_DASHBOARD_ON_START = new BooleanPreference("should_show_dashboard_on_start", false).makeGlobal();
public final CommonPreference<Boolean> SHOW_DASHBOARD_ON_MAP_SCREEN = new BooleanPreference("show_dashboard_on_map_screen", false).makeGlobal(); public final CommonPreference<Boolean> SHOW_DASHBOARD_ON_MAP_SCREEN = new BooleanPreference("show_dashboard_on_map_screen", false).makeGlobal();
public final CommonPreference<String> API_NAV_DRAWER_ITEMS_JSON = new StringPreference("api_nav_drawer_items_json", "").makeGlobal(); public final CommonPreference<String> API_NAV_DRAWER_ITEMS_JSON = new StringPreference("api_nav_drawer_items_json", "{}").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();