From 44bcf483759a72aa40a36761b46e1b166acf53ba Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 28 Jan 2021 18:54:45 +0200 Subject: [PATCH] Add opr blockchain name --- .../plus/mapcontextmenu/MenuBuilder.java | 2 +- .../mapcontextmenu/UploadPhotosAsyncTask.java | 6 ++-- .../osmand/plus/osmedit/opr/OpenDBAPI.java | 32 ++++++++++++++++--- .../plus/settings/backend/OsmandSettings.java | 3 ++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java index 183e41a84f..93f2bdf6b5 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java @@ -421,7 +421,7 @@ public class MenuBuilder { new Thread(new Runnable() { @Override public void run() { - if (openDBAPI.checkPrivateKeyValid(baseUrl, name, privateKey)) { + if (openDBAPI.checkPrivateKeyValid(app, baseUrl, name, privateKey)) { app.runInUIThread(new Runnable() { @Override public void run() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java index 30932e8b42..1bb48ff1f0 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java @@ -131,12 +131,12 @@ public class UploadPhotosAsyncTask extends AsyncTask { try { StringBuilder error = new StringBuilder(); 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( placeId, baseUrl, privateKey, - username, + name, response, error); if (res != 200) { app.showToastMessage(error.toString()); @@ -170,7 +170,7 @@ public class UploadPhotosAsyncTask extends AsyncTask { String baseUrl = OPRConstants.getBaseUrl(app); String name = app.getSettings().OPR_USERNAME.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); } else { app.runInUIThread(new Runnable() { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java b/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java index a16361c8bd..14c0e97672 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java @@ -3,10 +3,15 @@ package net.osmand.plus.osmedit.opr; import android.net.TrafficStats; import android.os.Build; +import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; +import com.google.gson.reflect.TypeToken; import net.osmand.PlatformUtil; import net.osmand.osm.io.NetworkUtils; +import net.osmand.plus.OsmandApplication; +import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import org.bouncycastle.jce.provider.BouncyCastleProvider; @@ -27,6 +32,7 @@ import java.security.KeyPair; import java.security.Security; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -40,7 +46,7 @@ public class OpenDBAPI { public static final String PURPOSE = "osmand-android"; private static final Log log = PlatformUtil.getLog(SecUtils.class); 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; /* @@ -51,7 +57,7 @@ public class OpenDBAPI { * Need to encode key * 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; try { String purposeParam = "purpose=" + PURPOSE; @@ -65,9 +71,27 @@ public class OpenDBAPI { } catch (UnsupportedEncodingException e) { return false; } + StringBuilder response = new StringBuilder(); - return (NetworkUtils.sendGetRequest(url,null,response) == null) && - response.toString().contains(LOGIN_SUCCESS_MESSAGE); + String error = NetworkUtils.sendGetRequest(url, null, response); + if (error == null) { + String responseStr = response.toString(); + try { + Map tagMap = new Gson().fromJson( + responseStr, new TypeToken>() { + }.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 { diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index dfaaea9468..0d3cddb136 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -1171,6 +1171,9 @@ public class OsmandSettings { public final OsmandPreference OPR_USERNAME = new StringPreference(this, "opr_username_secret", "").makeGlobal(); + public final OsmandPreference OPR_BLOCKCHAIN_NAME = + new StringPreference(this, "opr_blockchain_name", "").makeGlobal(); + // 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();