Add opr blockchain name

This commit is contained in:
Vitaliy 2021-01-28 18:54:45 +02:00
parent 8098f5d92c
commit 44bcf48375
4 changed files with 35 additions and 8 deletions

View file

@ -421,7 +421,7 @@ public class MenuBuilder {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
if (openDBAPI.checkPrivateKeyValid(baseUrl, name, privateKey)) { if (openDBAPI.checkPrivateKeyValid(app, baseUrl, name, privateKey)) {
app.runInUIThread(new Runnable() { app.runInUIThread(new Runnable() {
@Override @Override
public void run() { public void run() {

View file

@ -131,12 +131,12 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
try { try {
StringBuilder error = new StringBuilder(); StringBuilder error = new StringBuilder();
String privateKey = app.getSettings().OPR_ACCESS_TOKEN.get(); String privateKey = app.getSettings().OPR_ACCESS_TOKEN.get();
String username = app.getSettings().OPR_USERNAME.get(); String name = app.getSettings().OPR_BLOCKCHAIN_NAME.get();
res = openDBAPI.uploadImage( res = openDBAPI.uploadImage(
placeId, placeId,
baseUrl, baseUrl,
privateKey, privateKey,
username, name,
response, error); response, error);
if (res != 200) { if (res != 200) {
app.showToastMessage(error.toString()); app.showToastMessage(error.toString());
@ -170,7 +170,7 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
String baseUrl = OPRConstants.getBaseUrl(app); String baseUrl = OPRConstants.getBaseUrl(app);
String name = app.getSettings().OPR_USERNAME.get(); String name = app.getSettings().OPR_USERNAME.get();
String privateKey = app.getSettings().OPR_ACCESS_TOKEN.get(); String privateKey = app.getSettings().OPR_ACCESS_TOKEN.get();
if (openDBAPI.checkPrivateKeyValid(baseUrl, name, privateKey)) { if (openDBAPI.checkPrivateKeyValid(app, baseUrl, name, privateKey)) {
app.showToastMessage(R.string.cannot_upload_image); app.showToastMessage(R.string.cannot_upload_image);
} else { } else {
app.runInUIThread(new Runnable() { app.runInUIThread(new Runnable() {

View file

@ -3,10 +3,15 @@ package net.osmand.plus.osmedit.opr;
import android.net.TrafficStats; import android.net.TrafficStats;
import android.os.Build; import android.os.Build;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.osm.io.NetworkUtils; import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.OsmandApplication;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
@ -27,6 +32,7 @@ import java.security.KeyPair;
import java.security.Security; import java.security.Security;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -40,7 +46,7 @@ public class OpenDBAPI {
public static final String PURPOSE = "osmand-android"; public static final String PURPOSE = "osmand-android";
private static final Log log = PlatformUtil.getLog(SecUtils.class); private static final Log log = PlatformUtil.getLog(SecUtils.class);
private static final String checkLoginEndpoint = "api/auth/user-check-loginkey?"; private static final String checkLoginEndpoint = "api/auth/user-check-loginkey?";
private static final String LOGIN_SUCCESS_MESSAGE = "{\"result\":\"OK\"}"; private static final String LOGIN_SUCCESS_MESSAGE = "\"result\":\"OK\"";
private static final int THREAD_ID = 11200; private static final int THREAD_ID = 11200;
/* /*
@ -51,7 +57,7 @@ public class OpenDBAPI {
* Need to encode key * Need to encode key
* Do not call on mainThread * Do not call on mainThread
*/ */
public boolean checkPrivateKeyValid(String baseUrl, String username, String privateKey) { public boolean checkPrivateKeyValid(OsmandApplication app, String baseUrl, String username, String privateKey) {
String url = null; String url = null;
try { try {
String purposeParam = "purpose=" + PURPOSE; String purposeParam = "purpose=" + PURPOSE;
@ -65,9 +71,27 @@ public class OpenDBAPI {
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
return false; return false;
} }
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
return (NetworkUtils.sendGetRequest(url,null,response) == null) && String error = NetworkUtils.sendGetRequest(url, null, response);
response.toString().contains(LOGIN_SUCCESS_MESSAGE); if (error == null) {
String responseStr = response.toString();
try {
Map<String, String> tagMap = new Gson().fromJson(
responseStr, new TypeToken<HashMap<String, String>>() {
}.getType()
);
if (Algorithms.isEmpty(tagMap) && tagMap.containsKey("blockchain-name")) {
String blockchainName = tagMap.get("blockchain-name");
app.getSettings().OPR_BLOCKCHAIN_NAME.set(blockchainName);
}
} catch (JsonSyntaxException e) {
return false;
}
return responseStr.contains(LOGIN_SUCCESS_MESSAGE);
} else {
return false;
}
} }
public int uploadImage(String[] placeId, String baseUrl, String privateKey, String username, String image, StringBuilder sb) throws FailedVerificationException { public int uploadImage(String[] placeId, String baseUrl, String privateKey, String username, String image, StringBuilder sb) throws FailedVerificationException {

View file

@ -1171,6 +1171,9 @@ public class OsmandSettings {
public final OsmandPreference<String> OPR_USERNAME = public final OsmandPreference<String> OPR_USERNAME =
new StringPreference(this, "opr_username_secret", "").makeGlobal(); new StringPreference(this, "opr_username_secret", "").makeGlobal();
public final OsmandPreference<String> OPR_BLOCKCHAIN_NAME =
new StringPreference(this, "opr_blockchain_name", "").makeGlobal();
// 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 final OsmandPreference<Boolean> USE_DEV_URL = new BooleanPreference(this, "use_dev_url", false).makeGlobal().makeShared();