From 89d43ece792da108ae81c740d35722c68d725162 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Tue, 17 Nov 2020 23:15:14 +0200 Subject: [PATCH] Use dev url --- .../oauth/OsmOAuthAuthorizationClient.java | 28 +++++++++-- OsmAnd/build.gradle | 4 -- OsmAnd/res/values/strings.xml | 2 + OsmAnd/res/xml/osm_editing.xml | 8 ++++ .../src/net/osmand/plus/OsmAndConstants.java | 13 ++++-- .../plus/osmedit/OpenstreetmapRemoteUtil.java | 22 ++------- .../net/osmand/plus/osmedit/OsmBugsLayer.java | 9 +--- .../plus/osmedit/OsmBugsRemoteUtil.java | 22 ++------- .../plus/osmedit/OsmEditingFragment.java | 46 ++++++++++++++----- .../oauth/OsmOAuthAuthorizationAdapter.java | 24 +++++++--- .../plus/osmedit/oauth/OsmOAuthHelper.java | 7 ++- .../plus/settings/backend/OsmandSettings.java | 11 +++++ 12 files changed, 123 insertions(+), 73 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java index f1c718465e..cf2f5a5acc 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java @@ -25,16 +25,16 @@ public class OsmOAuthAuthorizationClient { private final OsmAndJDKHttpClient httpClient; public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationClient.class); - public OsmOAuthAuthorizationClient(String key, String secret) { + public OsmOAuthAuthorizationClient(String key, String secret, DefaultApi10a api) { httpClient = new OsmAndJDKHttpClient(JDKHttpClientConfig.defaultConfig()); service = new ServiceBuilder(key) .apiSecret(secret) .httpClient(httpClient) .callback("osmand-oauth://example.com/oauth") - .build(new OsmApi()); + .build(api); } - static class OsmApi extends DefaultApi10a { + public static class OsmApi extends DefaultApi10a { @Override public OAuth1SignatureType getSignatureType() { return OAuth1SignatureType.QUERY_STRING; @@ -56,6 +56,28 @@ public class OsmOAuthAuthorizationClient { } } + public static class OsmDevApi extends DefaultApi10a { + @Override + public OAuth1SignatureType getSignatureType() { + return OAuth1SignatureType.QUERY_STRING; + } + + @Override + public String getRequestTokenEndpoint() { + return "https://master.apis.dev.openstreetmap.org/oauth/request_token"; + } + + @Override + public String getAccessTokenEndpoint() { + return "https://master.apis.dev.openstreetmap.org/oauth/access_token"; + } + + @Override + protected String getAuthorizationBaseUrl() { + return "https://master.apis.dev.openstreetmap.org/oauth/authorize"; + } + } + public OsmAndJDKHttpClient getHttpClient() { return httpClient; } diff --git a/OsmAnd/build.gradle b/OsmAnd/build.gradle index c52dac72ad..bab0785bb8 100644 --- a/OsmAnd/build.gradle +++ b/OsmAnd/build.gradle @@ -207,14 +207,10 @@ android { buildTypes { debug { buildConfigField "String", "OPR_BASE_URL", "\"https://test.openplacereviews.org/\"" - buildConfigField "String", "OSM_OAUTH_CONSUMER_KEY", "\"Ti2qq3fo4i4Wmuox3SiWRIGq3obZisBHnxmcM05y\"" - buildConfigField "String", "OSM_OAUTH_CONSUMER_SECRET", "\"lxulb3HYoMmd2cC4xxNe1dyfRMAY8dS0eNihJ0DM\"" signingConfig signingConfigs.development } release { buildConfigField "String", "OPR_BASE_URL", "\"https://test.openplacereviews.org/\"" - buildConfigField "String", "OSM_OAUTH_CONSUMER_KEY", "\"Ti2qq3fo4i4Wmuox3SiWRIGq3obZisBHnxmcM05y\"" - buildConfigField "String", "OSM_OAUTH_CONSUMER_SECRET", "\"lxulb3HYoMmd2cC4xxNe1dyfRMAY8dS0eNihJ0DM\"" if (gradle.startParameter.taskNames.toString().contains("huawei")) { signingConfig signingConfigs.publishingHuawei } else { diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 6dc1b914d5..426f769d3a 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,8 @@ Thx - Hardy --> + Use dev.openstreetmap.org + Switch to use "dev.openstreetmap.org" instead of "openstreetmap.org" to testing uploading OSM Note / POI / GPX. Motorboat Kayak Search history diff --git a/OsmAnd/res/xml/osm_editing.xml b/OsmAnd/res/xml/osm_editing.xml index 469e841c36..8fd0883fc8 100644 --- a/OsmAnd/res/xml/osm_editing.xml +++ b/OsmAnd/res/xml/osm_editing.xml @@ -32,6 +32,14 @@ android:title="@string/offline_edition" tools:icon="@drawable/ic_action_offline" /> + + diff --git a/OsmAnd/src/net/osmand/plus/OsmAndConstants.java b/OsmAnd/src/net/osmand/plus/OsmAndConstants.java index 4e808ac947..16a5312405 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndConstants.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndConstants.java @@ -1,16 +1,21 @@ package net.osmand.plus; public interface OsmAndConstants { - - + + public int UI_HANDLER_MAP_VIEW = 3000; - + public int UI_HANDLER_MAP_CONTROLS = 4000; - + public int UI_HANDLER_LOCATION_SERVICE = 5000; public int UI_HANDLER_PROGRESS = 6000; public int UI_HANDLER_SEARCH = 7000; + String OSM_OAUTH_DEVELOPER_KEY = "v8G8r9NLJZGMV4he5lwbQlz620FNVARKjI9Bm5UJ"; + String OSM_OAUTH_DEVELOPER_SECRET = "jDvM95Ne1Bq2BDTmIfB6b3ZMxvdK87WGfp6DC07J"; + String OSM_OAUTH_CONSUMER_KEY = "Ti2qq3fo4i4Wmuox3SiWRIGq3obZisBHnxmcM05y"; + String OSM_OAUTH_CONSUMER_SECRET = "lxulb3HYoMmd2cC4xxNe1dyfRMAY8dS0eNihJ0DM"; + } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java index 8940093c19..98256d0635 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java @@ -66,33 +66,19 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { @Override public EntityInfo getEntityInfo(long id) { - if(entityInfoId != null && entityInfoId.getId().longValue() == id) { + if (entityInfoId != null && entityInfoId.getId().longValue() == id) { return entityInfo; } return null; } - private static String getSiteApi() { - final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; - - String RETURN_API; - - if (deviceApiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) { - RETURN_API = "https://api.openstreetmap.org/"; - } else { - RETURN_API = "http://api.openstreetmap.org/"; - } - - // RETURN_API = "http://api06.dev.openstreetmap.org/"; - - return RETURN_API; + private String getSiteApi() { + return settings.getOsmUrl(); } - private final static String URL_TO_UPLOAD_GPX = getSiteApi() + "api/0.6/gpx/create"; - public String uploadGPXFile(String tagstring, String description, String visibility, File f) { OsmOAuthAuthorizationAdapter adapter = new OsmOAuthAuthorizationAdapter(ctx); - String url = URL_TO_UPLOAD_GPX; + String url = getSiteApi() + "api/0.6/gpx/create"; Map additionalData = new LinkedHashMap(); additionalData.put("description", description); additionalData.put("tags", tagstring); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java index 78b356fd03..71f7f112ec 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java @@ -248,15 +248,8 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider } protected List loadingBugs(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude) { - final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; - String SITE_API; - - if (deviceApiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) { - SITE_API = "https://api.openstreetmap.org/"; - } else { - SITE_API = "http://api.openstreetmap.org/"; - } + String SITE_API = activity.getMyApplication().getSettings().getOsmUrl(); List bugs = new ArrayList<>(); StringBuilder b = new StringBuilder(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java index a6e2dd5ca8..f2fba4c52d 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java @@ -26,26 +26,12 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { private static final String GET = "GET"; private static final String POST = "POST"; - static String getNotesApi() { - final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; - String RETURN_API; - if (deviceApiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) { - RETURN_API = "https://api.openstreetmap.org/api/0.6/notes"; - } else { - RETURN_API = "http://api.openstreetmap.org/api/0.6/notes"; - } - return RETURN_API; + String getNotesApi() { + return settings.getOsmUrl() + "api/0.6/notes"; } - static String getUserDetailsApi() { - final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; - String RETURN_API; - if (deviceApiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) { - RETURN_API = "https://api.openstreetmap.org/api/0.6/user/details"; - } else { - RETURN_API = "http://api.openstreetmap.org/api/0.6/user/details"; - } - return RETURN_API; + String getUserDetailsApi() { + return settings.getOsmUrl() + "api/0.6/user/details"; } private OsmandApplication app; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java index d6156bfcfb..d7000e5811 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java @@ -43,6 +43,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer private static final String OPEN_OSM_EDITS = "open_osm_edits"; public static final String OSM_LOGIN_DATA = "osm_login_data"; private static final String OSM_EDITING_INFO = "osm_editing_info"; + private static final String USE_DEV_URL = "use_dev_url"; private OsmOAuthAuthorizationAdapter authorizationAdapter; @@ -61,6 +62,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer setupLogoutPref(); setupOfflineEditingPref(); + setupUseDevUrlPref(); setupOsmEditsDescrPref(); setupOsmEditsPref(); } @@ -129,6 +131,13 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer offlineEditingPref.setIcon(icon); } + private void setupUseDevUrlPref() { + Drawable icon = getPersistentPrefIcon(R.drawable.ic_action_laptop); + SwitchPreferenceEx useDevUrlPref = findPreference(settings.USE_DEV_URL.getId()); + useDevUrlPref.setDescription(getString(R.string.use_dev_url_descr)); + useDevUrlPref.setIcon(icon); + } + private void setupOsmEditsDescrPref() { String menu = getString(R.string.shared_string_menu); String myPlaces = getString(R.string.shared_string_my_places); @@ -152,6 +161,18 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer createProfile.setIcon(getActiveIcon(R.drawable.ic_action_folder)); } + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + String prefId = preference.getKey(); + if (USE_DEV_URL.equals(prefId) && newValue instanceof Boolean) { + settings.USE_DEV_URL.set((Boolean) newValue); + osmLogout(); + authorizationAdapter = app.getOsmOAuthHelper().updateAdapter(); + return true; + } + return super.onPreferenceChange(preference, newValue); + } + @Override public boolean onPreferenceClick(Preference preference) { String prefId = preference.getKey(); @@ -172,22 +193,25 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer return true; } } else if (OSM_LOGOUT.equals(prefId)) { - if (isValidToken()) { - settings.USER_ACCESS_TOKEN.resetToDefault(); - settings.USER_ACCESS_TOKEN_SECRET.resetToDefault(); - - authorizationAdapter.resetToken(); - } else { - settings.USER_NAME.resetToDefault(); - settings.USER_PASSWORD.resetToDefault(); - } - app.showShortToastMessage(R.string.osm_edit_logout_success); - updateAllSettings(); + osmLogout(); return true; } return super.onPreferenceClick(preference); } + private void osmLogout() { + if (isValidToken()) { + settings.USER_ACCESS_TOKEN.resetToDefault(); + settings.USER_ACCESS_TOKEN_SECRET.resetToDefault(); + authorizationAdapter.resetToken(); + } else { + settings.USER_NAME.resetToDefault(); + settings.USER_PASSWORD.resetToDefault(); + } + app.showShortToastMessage(R.string.osm_edit_logout_success); + updateAllSettings(); + } + @Override public void onPreferenceChanged(String prefId) { updateAllSettings(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java index 7c2562b580..0dc7733b16 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java @@ -5,6 +5,7 @@ import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; +import com.github.scribejava.core.builder.api.DefaultApi10a; import com.github.scribejava.core.model.OAuth1AccessToken; import com.github.scribejava.core.model.OAuth1RequestToken; import com.github.scribejava.core.model.OAuthAsyncRequestCallback; @@ -13,7 +14,7 @@ import com.github.scribejava.core.model.Verb; import net.osmand.PlatformUtil; import net.osmand.osm.oauth.OsmOAuthAuthorizationClient; -import net.osmand.plus.BuildConfig; +import net.osmand.plus.OsmAndConstants; import net.osmand.plus.OsmandApplication; import org.xmlpull.v1.XmlPullParser; @@ -27,15 +28,25 @@ public class OsmOAuthAuthorizationAdapter { private static final int THREAD_ID = 10101; private static final String OSM_USER = "user"; private static final String DISPLAY_NAME = "display_name"; - private static final String OSM_USER_DETAILS_URL = "https://api.openstreetmap.org/api/0.6/user/details"; - private OsmandApplication app; - private OsmOAuthAuthorizationClient client = - new OsmOAuthAuthorizationClient(BuildConfig.OSM_OAUTH_CONSUMER_KEY, BuildConfig.OSM_OAUTH_CONSUMER_SECRET); + private final OsmOAuthAuthorizationClient client; public OsmOAuthAuthorizationAdapter(OsmandApplication app) { TrafficStats.setThreadStatsTag(THREAD_ID); this.app = app; + DefaultApi10a api10a; + String key; + String secret; + if (app.getSettings().USE_DEV_URL.get()) { + api10a = new OsmOAuthAuthorizationClient.OsmDevApi(); + key = OsmAndConstants.OSM_OAUTH_DEVELOPER_KEY; + secret = OsmAndConstants.OSM_OAUTH_DEVELOPER_SECRET; + } else { + api10a = new OsmOAuthAuthorizationClient.OsmApi(); + key = OsmAndConstants.OSM_OAUTH_CONSUMER_KEY; + secret = OsmAndConstants.OSM_OAUTH_CONSUMER_SECRET; + } + client = new OsmOAuthAuthorizationClient(key, secret, api10a); restoreToken(); } @@ -104,7 +115,8 @@ public class OsmOAuthAuthorizationAdapter { } public Response getOsmUserDetails() throws InterruptedException, ExecutionException, IOException { - return performRequest(OSM_USER_DETAILS_URL, Verb.GET.name(), null); + String osmUserDetailsUrl = app.getSettings().getOsmUrl() + "api/0.6/user/details"; + return performRequest(osmUserDetailsUrl, Verb.GET.name(), null); } public String parseUserName(Response response) throws XmlPullParserException, IOException { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java index d13bf624f2..d58f62fa01 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java @@ -19,13 +19,18 @@ public class OsmOAuthHelper { private final OsmandApplication app; - private final OsmOAuthAuthorizationAdapter authorizationAdapter; + private OsmOAuthAuthorizationAdapter authorizationAdapter; public OsmOAuthHelper(@NonNull OsmandApplication app) { this.app = app; authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); } + public OsmOAuthAuthorizationAdapter updateAdapter(){ + authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); + return authorizationAdapter; + } + public void startOAuth(ViewGroup view) { authorizationAdapter.startOAuth(view); } diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index a574e0d0fd..065fd2113a 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -1133,6 +1133,17 @@ public class OsmandSettings { // this value boolean is synchronized with settings_pref.xml preference offline POI/Bugs edition public final OsmandPreference OFFLINE_EDITION = new BooleanPreference(this, "offline_osm_editing", true).makeGlobal().makeShared(); + public final OsmandPreference USE_DEV_URL = new BooleanPreference(this, "use_dev_url", false).makeGlobal().makeShared(); + + public String getOsmUrl() { + String osmUrl; + if (USE_DEV_URL.get()) { + osmUrl = "https://master.apis.dev.openstreetmap.org/"; + } else { + osmUrl = "https://api.openstreetmap.org/"; + } + return osmUrl; + } // this value string is synchronized with settings_pref.xml preference name public final CommonPreference DAYNIGHT_MODE =