forked from organicmaps/organicmaps
Added draft of "Login with OSM website" button
Signed-off-by: Sergiy Kozyr <s.trump@gmail.com>
This commit is contained in:
parent
4e8280f923
commit
cc32c18732
5 changed files with 63 additions and 0 deletions
|
@ -34,6 +34,20 @@ bool LoadOsmUserPreferences(std::string const & oauthToken, UserPreferences & ou
|
|||
extern "C"
|
||||
{
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
Java_app_organicmaps_editor_OsmOAuth_nativeOAuthParams(JNIEnv * env, jclass clazz)
|
||||
{
|
||||
OsmOAuth auth = OsmOAuth::ServerAuth();
|
||||
jobjectArray oauthParams = env->NewObjectArray(5, env->FindClass("java/lang/String"), nullptr);
|
||||
env->SetObjectArrayElement(oauthParams, 0, ToJavaString(env, auth.GetBaseUrl()));
|
||||
env->SetObjectArrayElement(oauthParams, 1, ToJavaString(env, auth.GetClientId()));
|
||||
env->SetObjectArrayElement(oauthParams, 2, ToJavaString(env, auth.GetClientSecret()));
|
||||
env->SetObjectArrayElement(oauthParams, 3, ToJavaString(env, auth.GetScope()));
|
||||
env->SetObjectArrayElement(oauthParams, 4, ToJavaString(env, auth.GetRedirectUri()));
|
||||
|
||||
return oauthParams;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_app_organicmaps_editor_OsmOAuth_nativeAuthWithPassword(JNIEnv * env, jclass clazz,
|
||||
jstring login, jstring password)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package app.organicmaps.editor;
|
||||
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
@ -29,6 +31,7 @@ public class OsmLoginFragment extends BaseMwmToolbarFragment
|
|||
{
|
||||
private ProgressBar mProgress;
|
||||
private Button mLoginButton;
|
||||
private Button mLoginWebsiteButton;
|
||||
private Button mLostPasswordButton;
|
||||
private TextInputEditText mLoginInput;
|
||||
private TextInputEditText mPasswordInput;
|
||||
|
@ -47,6 +50,8 @@ public class OsmLoginFragment extends BaseMwmToolbarFragment
|
|||
getToolbarController().setTitle(R.string.login);
|
||||
mLoginInput = view.findViewById(R.id.osm_username);
|
||||
mPasswordInput = view.findViewById(R.id.osm_password);
|
||||
mLoginWebsiteButton = view.findViewById(R.id.login_website);
|
||||
mLoginWebsiteButton.setOnClickListener((v) -> loginWithBrowser());
|
||||
mLoginButton = view.findViewById(R.id.login);
|
||||
mLoginButton.setOnClickListener((v) -> login());
|
||||
mLostPasswordButton = view.findViewById(R.id.lost_password);
|
||||
|
@ -75,6 +80,28 @@ public class OsmLoginFragment extends BaseMwmToolbarFragment
|
|||
});
|
||||
}
|
||||
|
||||
private void loginWithBrowser()
|
||||
{
|
||||
mLoginWebsiteButton.setEnabled(false);
|
||||
|
||||
String[] oauthParams = OsmOAuth.nativeOAuthParams();
|
||||
Uri oauth2url = Uri.parse(oauthParams[0] + "/oauth2/authorize")
|
||||
.buildUpon()
|
||||
.appendQueryParameter("client_id", oauthParams[1])
|
||||
.appendQueryParameter("scope", oauthParams[3])
|
||||
.appendQueryParameter("redirect_uri", oauthParams[4])
|
||||
.appendQueryParameter("response_type", "code")
|
||||
.build();
|
||||
try {
|
||||
Intent myIntent = new Intent(Intent.ACTION_VIEW, oauth2url);
|
||||
startActivity(myIntent);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
//Toast.makeText(this, "No application can handle this request."
|
||||
// + " Please install a webbrowser", Toast.LENGTH_LONG).show();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void enableInput(boolean enable)
|
||||
{
|
||||
mPasswordInput.setEnabled(enable);
|
||||
|
|
|
@ -10,6 +10,8 @@ import androidx.annotation.Size;
|
|||
import androidx.annotation.WorkerThread;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import app.organicmaps.MwmApplication;
|
||||
import app.organicmaps.util.NetworkPolicy;
|
||||
|
||||
|
@ -98,6 +100,12 @@ public final class OsmOAuth
|
|||
return nativeGetHistoryUrl(getUsername(context));
|
||||
}
|
||||
|
||||
/*
|
||||
Returns 5 strings: ServerURL, ClientId, ClientSecret, Scope, RedirectUri
|
||||
*/
|
||||
@NonNull
|
||||
public static native String[] nativeOAuthParams();
|
||||
|
||||
/**
|
||||
* @return string with OAuth2 token
|
||||
*/
|
||||
|
|
|
@ -60,6 +60,13 @@
|
|||
android:text="@string/login_to_make_edits_visible"
|
||||
android:textAppearance="@style/MwmTextAppearance.Body2"
|
||||
android:textColor="?android:textColorPrimary" />
|
||||
<Button
|
||||
android:id="@+id/login_website"
|
||||
style="@style/MwmWidget.Button.Accent"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Login with OSM website"
|
||||
android:textAppearance="@style/MwmTextAppearance.Body2.Light" />
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
style="@style/MwmWidget.Editor.CustomTextInput"
|
||||
android:layout_marginTop="@dimen/margin_half"
|
||||
|
|
|
@ -93,6 +93,13 @@ public:
|
|||
/// @param[api] If false, request is made to m_baseUrl.
|
||||
Response DirectRequest(std::string const & method, bool api = true) const;
|
||||
|
||||
// Getters
|
||||
std::string GetBaseUrl() const { return m_baseUrl; }
|
||||
std::string GetClientId() const { return m_oauth2params.m_clientId; }
|
||||
std::string GetClientSecret() const { return m_oauth2params.m_clientSecret; }
|
||||
std::string GetScope() const { return m_oauth2params.m_scope; }
|
||||
std::string GetRedirectUri() const { return m_oauth2params.m_redirectUri; }
|
||||
|
||||
/// @name Methods for WebView-based authentication.
|
||||
//@{
|
||||
std::string FinishAuthorization(std::string const & oauth2code) const;
|
||||
|
|
Loading…
Add table
Reference in a new issue