Use dev url

This commit is contained in:
Dima-1 2020-11-17 23:15:14 +02:00
parent f8012eb8d2
commit 89d43ece79
12 changed files with 123 additions and 73 deletions

View file

@ -25,16 +25,16 @@ public class OsmOAuthAuthorizationClient {
private final OsmAndJDKHttpClient httpClient; private final OsmAndJDKHttpClient httpClient;
public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationClient.class); 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()); httpClient = new OsmAndJDKHttpClient(JDKHttpClientConfig.defaultConfig());
service = new ServiceBuilder(key) service = new ServiceBuilder(key)
.apiSecret(secret) .apiSecret(secret)
.httpClient(httpClient) .httpClient(httpClient)
.callback("osmand-oauth://example.com/oauth") .callback("osmand-oauth://example.com/oauth")
.build(new OsmApi()); .build(api);
} }
static class OsmApi extends DefaultApi10a { public static class OsmApi extends DefaultApi10a {
@Override @Override
public OAuth1SignatureType getSignatureType() { public OAuth1SignatureType getSignatureType() {
return OAuth1SignatureType.QUERY_STRING; 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() { public OsmAndJDKHttpClient getHttpClient() {
return httpClient; return httpClient;
} }

View file

@ -207,14 +207,10 @@ android {
buildTypes { buildTypes {
debug { debug {
buildConfigField "String", "OPR_BASE_URL", "\"https://test.openplacereviews.org/\"" 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 signingConfig signingConfigs.development
} }
release { release {
buildConfigField "String", "OPR_BASE_URL", "\"https://test.openplacereviews.org/\"" 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")) { if (gradle.startParameter.taskNames.toString().contains("huawei")) {
signingConfig signingConfigs.publishingHuawei signingConfig signingConfigs.publishingHuawei
} else { } else {

View file

@ -11,6 +11,8 @@
Thx - Hardy Thx - Hardy
--> -->
<string name="use_dev_url">Use dev.openstreetmap.org</string>
<string name="use_dev_url_descr">Switch to use "dev.openstreetmap.org" instead of "openstreetmap.org" to testing uploading OSM Note / POI / GPX.</string>
<string name="app_mode_motorboat">Motorboat</string> <string name="app_mode_motorboat">Motorboat</string>
<string name="app_mode_kayak">Kayak</string> <string name="app_mode_kayak">Kayak</string>
<string name="shared_string_search_history">Search history</string> <string name="shared_string_search_history">Search history</string>

View file

@ -32,6 +32,14 @@
android:title="@string/offline_edition" android:title="@string/offline_edition"
tools:icon="@drawable/ic_action_offline" /> tools:icon="@drawable/ic_action_offline" />
<net.osmand.plus.settings.preferences.SwitchPreferenceEx
android:key="use_dev_url"
android:layout="@layout/preference_with_descr_dialog_and_switch"
android:summaryOff="@string/shared_string_disabled"
android:summaryOn="@string/shared_string_enabled"
android:title="@string/use_dev_url"
tools:icon="@drawable/ic_action_laptop" />
<Preference <Preference
android:layout="@layout/simple_divider_item" android:layout="@layout/simple_divider_item"
android:selectable="false" /> android:selectable="false" />

View file

@ -13,4 +13,9 @@ public interface OsmAndConstants {
public int UI_HANDLER_SEARCH = 7000; 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";
} }

View file

@ -66,33 +66,19 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
@Override @Override
public EntityInfo getEntityInfo(long id) { public EntityInfo getEntityInfo(long id) {
if(entityInfoId != null && entityInfoId.getId().longValue() == id) { if (entityInfoId != null && entityInfoId.getId().longValue() == id) {
return entityInfo; return entityInfo;
} }
return null; return null;
} }
private static String getSiteApi() { private String getSiteApi() {
final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; return settings.getOsmUrl();
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 final static String URL_TO_UPLOAD_GPX = getSiteApi() + "api/0.6/gpx/create";
public String uploadGPXFile(String tagstring, String description, String visibility, File f) { public String uploadGPXFile(String tagstring, String description, String visibility, File f) {
OsmOAuthAuthorizationAdapter adapter = new OsmOAuthAuthorizationAdapter(ctx); OsmOAuthAuthorizationAdapter adapter = new OsmOAuthAuthorizationAdapter(ctx);
String url = URL_TO_UPLOAD_GPX; String url = getSiteApi() + "api/0.6/gpx/create";
Map<String, String> additionalData = new LinkedHashMap<String, String>(); Map<String, String> additionalData = new LinkedHashMap<String, String>();
additionalData.put("description", description); additionalData.put("description", description);
additionalData.put("tags", tagstring); additionalData.put("tags", tagstring);

View file

@ -248,15 +248,8 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
} }
protected List<OpenStreetNote> loadingBugs(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude) { protected List<OpenStreetNote> loadingBugs(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude) {
final int deviceApiVersion = android.os.Build.VERSION.SDK_INT;
String SITE_API; String SITE_API = activity.getMyApplication().getSettings().getOsmUrl();
if (deviceApiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) {
SITE_API = "https://api.openstreetmap.org/";
} else {
SITE_API = "http://api.openstreetmap.org/";
}
List<OpenStreetNote> bugs = new ArrayList<>(); List<OpenStreetNote> bugs = new ArrayList<>();
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();

View file

@ -26,26 +26,12 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil {
private static final String GET = "GET"; private static final String GET = "GET";
private static final String POST = "POST"; private static final String POST = "POST";
static String getNotesApi() { String getNotesApi() {
final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; return settings.getOsmUrl() + "api/0.6/notes";
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;
} }
static String getUserDetailsApi() { String getUserDetailsApi() {
final int deviceApiVersion = android.os.Build.VERSION.SDK_INT; return settings.getOsmUrl() + "api/0.6/user/details";
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;
} }
private OsmandApplication app; private OsmandApplication app;

View file

@ -43,6 +43,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
private static final String OPEN_OSM_EDITS = "open_osm_edits"; private static final String OPEN_OSM_EDITS = "open_osm_edits";
public static final String OSM_LOGIN_DATA = "osm_login_data"; public static final String OSM_LOGIN_DATA = "osm_login_data";
private static final String OSM_EDITING_INFO = "osm_editing_info"; private static final String OSM_EDITING_INFO = "osm_editing_info";
private static final String USE_DEV_URL = "use_dev_url";
private OsmOAuthAuthorizationAdapter authorizationAdapter; private OsmOAuthAuthorizationAdapter authorizationAdapter;
@ -61,6 +62,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
setupLogoutPref(); setupLogoutPref();
setupOfflineEditingPref(); setupOfflineEditingPref();
setupUseDevUrlPref();
setupOsmEditsDescrPref(); setupOsmEditsDescrPref();
setupOsmEditsPref(); setupOsmEditsPref();
} }
@ -129,6 +131,13 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
offlineEditingPref.setIcon(icon); 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() { private void setupOsmEditsDescrPref() {
String menu = getString(R.string.shared_string_menu); String menu = getString(R.string.shared_string_menu);
String myPlaces = getString(R.string.shared_string_my_places); 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)); 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 @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
String prefId = preference.getKey(); String prefId = preference.getKey();
@ -172,10 +193,16 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
return true; return true;
} }
} else if (OSM_LOGOUT.equals(prefId)) { } else if (OSM_LOGOUT.equals(prefId)) {
osmLogout();
return true;
}
return super.onPreferenceClick(preference);
}
private void osmLogout() {
if (isValidToken()) { if (isValidToken()) {
settings.USER_ACCESS_TOKEN.resetToDefault(); settings.USER_ACCESS_TOKEN.resetToDefault();
settings.USER_ACCESS_TOKEN_SECRET.resetToDefault(); settings.USER_ACCESS_TOKEN_SECRET.resetToDefault();
authorizationAdapter.resetToken(); authorizationAdapter.resetToken();
} else { } else {
settings.USER_NAME.resetToDefault(); settings.USER_NAME.resetToDefault();
@ -183,9 +210,6 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
} }
app.showShortToastMessage(R.string.osm_edit_logout_success); app.showShortToastMessage(R.string.osm_edit_logout_success);
updateAllSettings(); updateAllSettings();
return true;
}
return super.onPreferenceClick(preference);
} }
@Override @Override

View file

@ -5,6 +5,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.WebView; 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.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken; import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.model.OAuthAsyncRequestCallback; 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.PlatformUtil;
import net.osmand.osm.oauth.OsmOAuthAuthorizationClient; import net.osmand.osm.oauth.OsmOAuthAuthorizationClient;
import net.osmand.plus.BuildConfig; import net.osmand.plus.OsmAndConstants;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -27,15 +28,25 @@ public class OsmOAuthAuthorizationAdapter {
private static final int THREAD_ID = 10101; private static final int THREAD_ID = 10101;
private static final String OSM_USER = "user"; private static final String OSM_USER = "user";
private static final String DISPLAY_NAME = "display_name"; 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 OsmandApplication app;
private OsmOAuthAuthorizationClient client = private final OsmOAuthAuthorizationClient client;
new OsmOAuthAuthorizationClient(BuildConfig.OSM_OAUTH_CONSUMER_KEY, BuildConfig.OSM_OAUTH_CONSUMER_SECRET);
public OsmOAuthAuthorizationAdapter(OsmandApplication app) { public OsmOAuthAuthorizationAdapter(OsmandApplication app) {
TrafficStats.setThreadStatsTag(THREAD_ID); TrafficStats.setThreadStatsTag(THREAD_ID);
this.app = app; 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(); restoreToken();
} }
@ -104,7 +115,8 @@ public class OsmOAuthAuthorizationAdapter {
} }
public Response getOsmUserDetails() throws InterruptedException, ExecutionException, IOException { 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 { public String parseUserName(Response response) throws XmlPullParserException, IOException {

View file

@ -19,13 +19,18 @@ public class OsmOAuthHelper {
private final OsmandApplication app; private final OsmandApplication app;
private final OsmOAuthAuthorizationAdapter authorizationAdapter; private OsmOAuthAuthorizationAdapter authorizationAdapter;
public OsmOAuthHelper(@NonNull OsmandApplication app) { public OsmOAuthHelper(@NonNull OsmandApplication app) {
this.app = app; this.app = app;
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
} }
public OsmOAuthAuthorizationAdapter updateAdapter(){
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
return authorizationAdapter;
}
public void startOAuth(ViewGroup view) { public void startOAuth(ViewGroup view) {
authorizationAdapter.startOAuth(view); authorizationAdapter.startOAuth(view);
} }

View file

@ -1133,6 +1133,17 @@ public class OsmandSettings {
// this value boolean is synchronized with settings_pref.xml preference offline POI/Bugs edition // this value boolean is synchronized with settings_pref.xml preference offline POI/Bugs edition
public final OsmandPreference<Boolean> OFFLINE_EDITION = new BooleanPreference(this, "offline_osm_editing", true).makeGlobal().makeShared(); public final OsmandPreference<Boolean> OFFLINE_EDITION = new BooleanPreference(this, "offline_osm_editing", true).makeGlobal().makeShared();
public final OsmandPreference<Boolean> 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 // this value string is synchronized with settings_pref.xml preference name
public final CommonPreference<DayNightMode> DAYNIGHT_MODE = public final CommonPreference<DayNightMode> DAYNIGHT_MODE =