commit
62629478b6
12 changed files with 203 additions and 121 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -198,14 +198,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\""
|
|
||||||
signingConfig signingConfigs.publishing
|
signingConfig signingConfigs.publishing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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="select_items_for_import">Select items that will be imported.</string>
|
<string name="select_items_for_import">Select items that will be imported.</string>
|
||||||
<string name="select_groups_for_import">Select groups that will be imported.</string>
|
<string name="select_groups_for_import">Select groups that will be imported.</string>
|
||||||
<string name="export_not_enough_space">There is not enough space</string>
|
<string name="export_not_enough_space">There is not enough space</string>
|
||||||
|
|
|
@ -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" />
|
||||||
|
|
|
@ -1,16 +1,21 @@
|
||||||
package net.osmand.plus;
|
package net.osmand.plus;
|
||||||
|
|
||||||
public interface OsmAndConstants {
|
public interface OsmAndConstants {
|
||||||
|
|
||||||
|
|
||||||
public int UI_HANDLER_MAP_VIEW = 3000;
|
public int UI_HANDLER_MAP_VIEW = 3000;
|
||||||
|
|
||||||
public int UI_HANDLER_MAP_CONTROLS = 4000;
|
public int UI_HANDLER_MAP_CONTROLS = 4000;
|
||||||
|
|
||||||
public int UI_HANDLER_LOCATION_SERVICE = 5000;
|
public int UI_HANDLER_LOCATION_SERVICE = 5000;
|
||||||
|
|
||||||
public int UI_HANDLER_PROGRESS = 6000;
|
public int UI_HANDLER_PROGRESS = 6000;
|
||||||
|
|
||||||
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";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package net.osmand.plus.osmedit;
|
package net.osmand.plus.osmedit;
|
||||||
|
|
||||||
|
|
||||||
|
import com.github.scribejava.core.model.Response;
|
||||||
|
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
import net.osmand.osm.io.Base64;
|
import net.osmand.osm.io.Base64;
|
||||||
import net.osmand.osm.io.NetworkUtils;
|
import net.osmand.osm.io.NetworkUtils;
|
||||||
|
import net.osmand.osm.oauth.OsmOAuthAuthorizationClient;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
import net.osmand.plus.Version;
|
import net.osmand.plus.Version;
|
||||||
|
@ -19,6 +22,8 @@ import java.io.UnsupportedEncodingException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public class OsmBugsRemoteUtil implements OsmBugsUtil {
|
public class OsmBugsRemoteUtil implements OsmBugsUtil {
|
||||||
|
|
||||||
|
@ -26,26 +31,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;
|
||||||
|
@ -107,57 +98,82 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil {
|
||||||
return editingPOI(getUserDetailsApi(), GET, "validate_login", false);
|
return editingPOI(getUserDetailsApi(), GET, "validate_login", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private OsmBugResult editingPOI(String url, String requestMethod, String userOperation,
|
private OsmBugResult editingPOI(String url, String requestMethod, String userOperation, boolean anonymous) {
|
||||||
boolean anonymous) {
|
OsmOAuthAuthorizationAdapter authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
|
||||||
OsmOAuthAuthorizationAdapter client = new OsmOAuthAuthorizationAdapter(app);
|
OsmBugResult result = new OsmBugResult();
|
||||||
OsmBugResult r = new OsmBugResult();
|
if (authorizationAdapter.isValidToken() && !anonymous) {
|
||||||
try {
|
try {
|
||||||
HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url);
|
result = performOAuthRequest(url, requestMethod, userOperation, authorizationAdapter);
|
||||||
log.info("Editing poi " + url);
|
} catch (InterruptedException e) {
|
||||||
connection.setConnectTimeout(15000);
|
log.error(e);
|
||||||
connection.setRequestMethod(requestMethod);
|
result.warning = e.getMessage();
|
||||||
connection.setRequestProperty("User-Agent", Version.getFullVersion(app)); //$NON-NLS-1$
|
} catch (ExecutionException e) {
|
||||||
|
log.error(e);
|
||||||
if (!anonymous) {
|
result.warning = e.getMessage();
|
||||||
if (client.isValidToken()) {
|
} catch (IOException e) {
|
||||||
connection.addRequestProperty("Authorization", "OAuth " + client.getClient().getAccessToken().getToken());
|
log.error(e);
|
||||||
} else {
|
result.warning = e.getMessage();
|
||||||
String token = settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get(); //$NON-NLS-1$
|
|
||||||
connection.addRequestProperty("Authorization", "Basic " + Base64.encode(token.getBytes("UTF-8"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
connection.setDoInput(true);
|
try {
|
||||||
connection.connect();
|
result = performBasicRequest(url, requestMethod, anonymous);
|
||||||
String msg = connection.getResponseMessage();
|
} catch (FileNotFoundException | NullPointerException e) {
|
||||||
boolean ok = connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
// that's tricky case why NPE is thrown to fix that problem httpClient could be used
|
||||||
log.info(msg); //$NON-NLS-1$
|
String msg = app.getString(R.string.auth_failed);
|
||||||
// populate return fields.
|
log.error(msg, e);
|
||||||
|
result.warning = app.getString(R.string.auth_failed) + "";
|
||||||
StringBuilder responseBody;
|
} catch (MalformedURLException e) {
|
||||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
|
log.error(userOperation + " " + app.getString(R.string.failed_op), e);
|
||||||
responseBody = Algorithms.readFromInputStream(connection.getErrorStream());
|
result.warning = e.getMessage() + "";
|
||||||
} else {
|
} catch (IOException e) {
|
||||||
responseBody = Algorithms.readFromInputStream(connection.getInputStream());
|
log.error(userOperation + " " + app.getString(R.string.failed_op), e);
|
||||||
|
result.warning = e.getMessage() + " link unavailable";
|
||||||
}
|
}
|
||||||
log.info("Response : " + responseBody); //$NON-NLS-1$
|
|
||||||
connection.disconnect();
|
|
||||||
if (!ok) {
|
|
||||||
r.warning = msg + "\n" + responseBody;
|
|
||||||
}
|
|
||||||
} catch (FileNotFoundException | NullPointerException e) {
|
|
||||||
// that's tricky case why NPE is thrown to fix that problem httpClient could be used
|
|
||||||
String msg = app.getString(R.string.auth_failed);
|
|
||||||
log.error(msg, e);
|
|
||||||
r.warning = app.getString(R.string.auth_failed) + "";
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
log.error(userOperation + " " + app.getString(R.string.failed_op), e); //$NON-NLS-1$
|
|
||||||
r.warning = e.getMessage() + "";
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(userOperation + " " + app.getString(R.string.failed_op), e); //$NON-NLS-1$
|
|
||||||
r.warning = e.getMessage() + " link unavailable";
|
|
||||||
}
|
}
|
||||||
return r;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private OsmBugResult performBasicRequest(String url, String requestMethod, boolean anonymous) throws IOException {
|
||||||
|
OsmBugResult result = new OsmBugResult();
|
||||||
|
HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url);
|
||||||
|
log.info("Editing poi " + url);
|
||||||
|
connection.setConnectTimeout(15000);
|
||||||
|
connection.setRequestMethod(requestMethod);
|
||||||
|
connection.setRequestProperty("User-Agent", Version.getFullVersion(app));
|
||||||
|
if (!anonymous) {
|
||||||
|
String token = settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get();
|
||||||
|
connection.addRequestProperty("Authorization", "Basic " + Base64.encode(token.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
}
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.connect();
|
||||||
|
String msg = connection.getResponseMessage();
|
||||||
|
boolean ok = connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||||
|
log.info(msg);
|
||||||
|
// populate return fields.
|
||||||
|
|
||||||
|
StringBuilder responseBody;
|
||||||
|
if (connection.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
|
||||||
|
responseBody = Algorithms.readFromInputStream(connection.getErrorStream());
|
||||||
|
} else {
|
||||||
|
responseBody = Algorithms.readFromInputStream(connection.getInputStream());
|
||||||
|
}
|
||||||
|
log.info("Response : " + responseBody);
|
||||||
|
connection.disconnect();
|
||||||
|
if (!ok) {
|
||||||
|
result.warning = msg + "\n" + responseBody;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private OsmBugResult performOAuthRequest(String url, String requestMethod, String userOperation,
|
||||||
|
OsmOAuthAuthorizationAdapter authorizationAdapter)
|
||||||
|
throws InterruptedException, ExecutionException, IOException {
|
||||||
|
OsmBugResult result = new OsmBugResult();
|
||||||
|
OsmOAuthAuthorizationClient client = authorizationAdapter.getClient();
|
||||||
|
Response response = client.performRequest(url, requestMethod, userOperation);
|
||||||
|
if (response.getCode() != HttpURLConnection.HTTP_OK) {
|
||||||
|
result.warning = response.getMessage() + "\n" + response.getBody();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,6 @@ import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
import net.osmand.PlatformUtil;
|
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
import net.osmand.plus.activities.MapActivity;
|
import net.osmand.plus.activities.MapActivity;
|
||||||
import net.osmand.plus.helpers.AndroidUiHelper;
|
import net.osmand.plus.helpers.AndroidUiHelper;
|
||||||
|
@ -31,16 +30,12 @@ import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
|
||||||
import net.osmand.plus.widgets.style.CustomTypefaceSpan;
|
import net.osmand.plus.widgets.style.CustomTypefaceSpan;
|
||||||
import net.osmand.util.Algorithms;
|
import net.osmand.util.Algorithms;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
|
|
||||||
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
|
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
|
||||||
import static net.osmand.plus.osmedit.OsmEditingPlugin.OSM_EDIT_TAB;
|
import static net.osmand.plus.osmedit.OsmEditingPlugin.OSM_EDIT_TAB;
|
||||||
|
|
||||||
public class OsmEditingFragment extends BaseSettingsFragment implements OnPreferenceChanged, ValidateOsmLoginListener,
|
public class OsmEditingFragment extends BaseSettingsFragment implements OnPreferenceChanged, ValidateOsmLoginListener,
|
||||||
OsmAuthorizationListener {
|
OsmAuthorizationListener {
|
||||||
|
|
||||||
private static final Log log = PlatformUtil.getLog(OsmEditingFragment.class);
|
|
||||||
|
|
||||||
private static final String OSM_LOGOUT = "osm_logout";
|
private static final String OSM_LOGOUT = "osm_logout";
|
||||||
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";
|
||||||
|
@ -74,6 +69,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
||||||
setupLogoutPref();
|
setupLogoutPref();
|
||||||
|
|
||||||
setupOfflineEditingPref();
|
setupOfflineEditingPref();
|
||||||
|
setupUseDevUrlPref();
|
||||||
setupOsmEditsDescrPref();
|
setupOsmEditsDescrPref();
|
||||||
setupOsmEditsPref();
|
setupOsmEditsPref();
|
||||||
}
|
}
|
||||||
|
@ -142,6 +138,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);
|
||||||
|
@ -165,6 +168,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 (settings.USE_DEV_URL.getId().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();
|
||||||
|
@ -185,10 +200,18 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (OSM_LOGOUT.equals(prefId)) {
|
} else if (OSM_LOGOUT.equals(prefId)) {
|
||||||
if (isValidToken()) {
|
osmLogout();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return super.onPreferenceClick(preference);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void osmLogout() {
|
||||||
|
boolean validToken = isValidToken();
|
||||||
|
if (validToken || isLoginExists()) {
|
||||||
|
if (validToken) {
|
||||||
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();
|
||||||
|
@ -196,13 +219,15 @@ 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
|
||||||
public void onPreferenceChanged(String prefId) {
|
public void onPreferenceChanged(String prefId) {
|
||||||
|
if (settings.USE_DEV_URL.getId().equals(prefId)) {
|
||||||
|
osmLogout();
|
||||||
|
authorizationAdapter = app.getOsmOAuthHelper().updateAdapter();
|
||||||
|
}
|
||||||
updateAllSettings();
|
updateAllSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import android.webkit.WebView;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
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;
|
||||||
|
@ -16,7 +17,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.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
@ -31,16 +32,27 @@ 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";
|
|
||||||
public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationAdapter.class);
|
public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationAdapter.class);
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,9 +174,10 @@ public class OsmOAuthAuthorizationAdapter {
|
||||||
return parseUserName(response);
|
return parseUserName(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
String userName = null;
|
String userName = null;
|
||||||
|
|
|
@ -12,7 +12,7 @@ import java.util.Set;
|
||||||
public class OsmOAuthHelper {
|
public class OsmOAuthHelper {
|
||||||
|
|
||||||
private final OsmandApplication app;
|
private final OsmandApplication app;
|
||||||
private final OsmOAuthAuthorizationAdapter authorizationAdapter;
|
private OsmOAuthAuthorizationAdapter authorizationAdapter;
|
||||||
private final Set<OsmAuthorizationListener> listeners = new HashSet<>();
|
private final Set<OsmAuthorizationListener> listeners = new HashSet<>();
|
||||||
|
|
||||||
public OsmOAuthHelper(@NonNull OsmandApplication app) {
|
public OsmOAuthHelper(@NonNull OsmandApplication app) {
|
||||||
|
@ -24,6 +24,11 @@ public class OsmOAuthHelper {
|
||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public OsmOAuthAuthorizationAdapter updateAdapter(){
|
||||||
|
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
|
||||||
|
return authorizationAdapter;
|
||||||
|
}
|
||||||
|
|
||||||
public void removeListener(OsmAuthorizationListener listener) {
|
public void removeListener(OsmAuthorizationListener listener) {
|
||||||
listeners.remove(listener);
|
listeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 =
|
||||||
|
|
Loading…
Reference in a new issue