diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index 2c85514de8..67709531f9 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -65,7 +65,6 @@ - > 1; - String baseUrl = OPRWebviewActivity.getBaseUrl(app); + String baseUrl = OPRConstants.getBaseUrl(app); String url = baseUrl + "api/objects-by-index?type=opr.place&index=osmid&key=" + amenityId; String response = AndroidNetworkUtils.sendRequest(app, url, Collections.emptyMap(), "Requesting location images...", false, false); diff --git a/OsmAnd/src/net/osmand/plus/openplacereviews/OPRConstants.java b/OsmAnd/src/net/osmand/plus/openplacereviews/OPRConstants.java new file mode 100644 index 0000000000..a329e48235 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/openplacereviews/OPRConstants.java @@ -0,0 +1,38 @@ +package net.osmand.plus.openplacereviews; + + +import android.content.Context; + +import net.osmand.plus.R; +import net.osmand.plus.osmedit.opr.OpenDBAPI; + +public class OPRConstants { + public static final String OPR_OAUTH_PREFIX = "opr-oauth"; + private static final String PURPOSE = OpenDBAPI.PURPOSE; + private static final String CALLBACK_URL = OPR_OAUTH_PREFIX + "://osmand_opr_auth"; + + public static String getBaseUrl(Context ctx) { + return ctx.getString(R.string.opr_base_url); + } + + + public static String getLoginUrl(Context ctx) { + return getBaseUrl(ctx) + "login" + getQueryString(ctx); + } + + public static String getRegisterUrl(Context ctx) { + return getBaseUrl(ctx) + "signup" + getQueryString(ctx); + } + + public static String getQueryString(Context ctx) { + return "?" + getPurposeParam(ctx) + "&" + getCallbackParam(ctx); + } + + public static String getPurposeParam(Context ctx) { + return "purpose=" + PURPOSE; + } + + public static String getCallbackParam(Context ctx) { + return "callback=" + CALLBACK_URL; + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/openplacereviews/OPRWebviewActivity.java b/OsmAnd/src/net/osmand/plus/openplacereviews/OPRWebviewActivity.java deleted file mode 100644 index 266aabe78e..0000000000 --- a/OsmAnd/src/net/osmand/plus/openplacereviews/OPRWebviewActivity.java +++ /dev/null @@ -1,160 +0,0 @@ -package net.osmand.plus.openplacereviews; - - -import android.content.Context; -import android.graphics.PorterDuff; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.view.View; -import android.webkit.CookieManager; -import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.widget.TextView; - -import androidx.appcompat.widget.Toolbar; -import androidx.core.content.ContextCompat; - -import net.osmand.AndroidUtils; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; -import net.osmand.plus.activities.OsmandActionBarActivity; -import net.osmand.plus.settings.backend.OsmandSettings; - -import java.util.ArrayList; -import java.util.List; - -public class OPRWebviewActivity extends OsmandActionBarActivity { - public static final String KEY_LOGIN = "LOGIN_KEY"; - public static final String KEY_TITLE = "TITLE_KEY"; - public static final String OPR_OAUTH_PREFIX = "opr-oauth"; - private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)"; - private static final String PURPOSE = "opr-android"; - private static final String CALLBACK_URL = OPR_OAUTH_PREFIX + "://osmand_opr_auth"; - private WebView webView; - private boolean isLogin = false; - - public static String getBaseUrl(Context ctx) { - return ctx.getString(R.string.opr_base_url); - } - - public static String getCookieUrl(Context ctx) { - return getBaseUrl(ctx) + "profile"; - } - - public static String getLoginUrl(Context ctx) { - return getBaseUrl(ctx) + "login" + getQueryString(ctx); - } - - public static String getRegisterUrl(Context ctx) { - return getBaseUrl(ctx) + "signup" + getQueryString(ctx); - } - - public static String getQueryString(Context ctx) { - return "?" + getPurposeParam(ctx) + "&" + getCallbackParam(ctx); - } - - public static String getPurposeParam(Context ctx) { - return "purpose=" + PURPOSE; - } - - public static String getCallbackParam(Context ctx) { - return "callback=" + CALLBACK_URL; - } - - public static List getFinishUrls(Context ctx) { - String googleOAuthFinishUrl = getBaseUrl(ctx) + "auth?code=4"; - String profileUrl = getCookieUrl(ctx); - List urls = new ArrayList<>(); - urls.add(googleOAuthFinishUrl); - urls.add(profileUrl); - return urls; - } - - public void onCreate(Bundle savedInstanceState) { - OsmandApplication app = getMyApplication(); - OsmandSettings settings = app.getSettings(); - boolean nightMode = !settings.isLightContent(); - int themeId = nightMode ? R.style.OsmandDarkTheme_NoActionbar : R.style.OsmandLightTheme_NoActionbar_LightStatusBar; - setTheme(themeId); - getWindow().setStatusBarColor(ContextCompat.getColor(this, nightMode - ? R.color.list_background_color_dark : R.color.list_background_color_light)); - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_opr_webview); - Bundle bundle = getIntent().getExtras(); - Toolbar toolbar = findViewById(R.id.toolbar); - if (bundle != null) { - TextView titleView = findViewById(R.id.toolbar_text); - String title = bundle.getString(KEY_TITLE, ""); - titleView.setText(title); - } - toolbar.setBackgroundDrawable(new ColorDrawable(AndroidUtils.getColorFromAttr(this, R.attr.bg_color))); - final Drawable upArrow = app.getUIUtilities().getIcon(AndroidUtils.getNavigationIconResId(this)); - upArrow.setColorFilter(ContextCompat.getColor(this, R.color.color_favorite_gray), PorterDuff.Mode.SRC_ATOP); - toolbar.setNavigationIcon(upArrow); - toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up); - toolbar.setNavigationOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - onBackPressed(); - } - }); - webView = findViewById(R.id.printDialogWebview); - webView.getSettings().setUserAgentString(USER_AGENT); - webView.setWebViewClient(new CloseOnSuccessWebViewClient()); - webView.getSettings().setJavaScriptEnabled(true); - WebView.setWebContentsDebuggingEnabled(true); - if (bundle != null) { - isLogin = bundle.getBoolean(KEY_LOGIN); - if (isLogin) { - webView.loadUrl(getLoginUrl(this)); - } else { - webView.loadUrl(getRegisterUrl(this)); - } - } - } - - @Override - public boolean onSupportNavigateUp() { - onBackPressed(); - return true; - } - - public static String getPrivateKeyFromCookie(Context ctx) { - return returnCookieByKey(ctx, "opr-token"); - } - - public static String getUsernameFromCookie(Context ctx) { - return returnCookieByKey(ctx, "opr-nickname"); - } - - private static String returnCookieByKey(Context ctx, String key) { - String CookieValue = null; - CookieManager cookieManager = CookieManager.getInstance(); - String cookies = cookieManager.getCookie(getCookieUrl(ctx)); - if (cookies == null || cookies.isEmpty()) { - return ""; - } - String[] temp = cookies.split(";"); - for (String ar1 : temp) { - if (ar1.contains(key)) { - String[] temp1 = ar1.split("="); - CookieValue = temp1[1]; - break; - } - } - return CookieValue; - } - - public class CloseOnSuccessWebViewClient extends WebViewClient { - @Override - public void onPageFinished(WebView view, String url) { - for (String furl : getFinishUrls(OPRWebviewActivity.this)) { - if (url.contains(furl) && isLogin) { - finish(); - } - } - super.onPageFinished(view, url); - } - } -} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/openplacereviews/OprStartFragment.java b/OsmAnd/src/net/osmand/plus/openplacereviews/OprStartFragment.java index da2959efc4..1b435654ef 100644 --- a/OsmAnd/src/net/osmand/plus/openplacereviews/OprStartFragment.java +++ b/OsmAnd/src/net/osmand/plus/openplacereviews/OprStartFragment.java @@ -70,14 +70,14 @@ public class OprStartFragment extends BaseOsmAndFragment { } private void handleHaveAccount() { - String url = OPRWebviewActivity.getLoginUrl(requireContext()); + String url = OPRConstants.getLoginUrl(requireContext()); CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); customTabsIntent.launchUrl(requireContext(), Uri.parse(url)); } private void handleCreateAccount() { - String url = OPRWebviewActivity.getRegisterUrl(requireContext()); + String url = OPRConstants.getRegisterUrl(requireContext()); CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); customTabsIntent.launchUrl(requireContext(), Uri.parse(url)); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java b/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java index edce712a1a..faaeaa61e8 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/opr/OpenDBAPI.java @@ -37,16 +37,16 @@ import static org.openplacereviews.opendb.SecUtils.signMessageWithKeyBase64; public class OpenDBAPI { + public static final String PURPOSE = "opr-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 PURPOSE = "opr-android"; private static final int THREAD_ID = 11200; /* * method for check if user is loggined in blockchain * params - * - username: blockchain username in format "openplacereviews:test_1" + * - username: blockchain username in format "openplacereviews" * - privatekey: "base64:PKCS#8:actualKey" * Need to encode key * Do not call on mainThread @@ -54,8 +54,8 @@ public class OpenDBAPI { public boolean checkPrivateKeyValid(String baseUrl, String username, String privateKey) { String url = null; try { - url = baseUrl + checkLoginEndpoint + - "purpose=" + PURPOSE + "&" + + String purposeParam = "purpose=" + PURPOSE; + url = baseUrl + checkLoginEndpoint + purposeParam + "&" + "name=" + username + "&" +