forked from organicmaps/organicmaps
[android] Common logic to delegate.
This commit is contained in:
parent
13a5359168
commit
9c2a684c7b
8 changed files with 246 additions and 362 deletions
|
@ -3,7 +3,9 @@
|
|||
#include "com/mapswithme/core/jni_helper.hpp"
|
||||
|
||||
#include "base/logging.hpp"
|
||||
|
||||
#include "editor/osm_auth.hpp"
|
||||
#include "editor/server_api.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -97,4 +99,20 @@ Java_com_mapswithme_maps_editor_OsmOAuth_nativeGetGoogleAuthUrl(JNIEnv * env, jc
|
|||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_com_mapswithme_maps_editor_OsmOAuth_nativeGetOsmUsername(JNIEnv * env, jclass clazz, jstring token, jstring secret)
|
||||
{
|
||||
try
|
||||
{
|
||||
TKeySecret keySecret(jni::ToNativeString(env, token), jni::ToNativeString(env, secret));
|
||||
ServerApi06 const api(OsmOAuth::ServerAuth(keySecret));
|
||||
return jni::ToJavaString(env, api.GetUserPreferences().m_displayName);
|
||||
}
|
||||
catch (exception const & ex)
|
||||
{
|
||||
LOG(LWARNING, ("Can't load user preferences from server: ", ex.what()));
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
|
|
@ -1,29 +1,18 @@
|
|||
package com.mapswithme.maps.editor;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.UrlQuerySanitizer;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.Size;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmDialogFragment;
|
||||
import com.mapswithme.maps.widget.InputWebView;
|
||||
import com.mapswithme.util.Constants;
|
||||
import com.mapswithme.util.concurrency.ThreadPool;
|
||||
import com.mapswithme.util.concurrency.UiThread;
|
||||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
||||
// TODO refactor together with with AuthFragment and BaseAuthFragment to avoid code duplication
|
||||
public class AuthDialogFragment extends BaseMwmDialogFragment implements View.OnClickListener
|
||||
public class AuthDialogFragment extends BaseMwmDialogFragment
|
||||
{
|
||||
private OsmAuthFragmentDelegate mOsmAuthDelegate;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
|
||||
|
@ -41,162 +30,7 @@ public class AuthDialogFragment extends BaseMwmDialogFragment implements View.On
|
|||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
|
||||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
view.findViewById(R.id.login_osm).setOnClickListener(this);
|
||||
view.findViewById(R.id.login_facebook).setOnClickListener(this);
|
||||
view.findViewById(R.id.login_google).setOnClickListener(this);
|
||||
view.findViewById(R.id.register).setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
// TODO show/hide spinners
|
||||
switch (v.getId())
|
||||
{
|
||||
case R.id.login_osm:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.OSM);
|
||||
loginOsm();
|
||||
break;
|
||||
case R.id.login_facebook:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.FACEBOOK);
|
||||
loginWebview(OsmOAuth.AuthType.FACEBOOK);
|
||||
break;
|
||||
case R.id.login_google:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.GOOGLE);
|
||||
loginWebview(OsmOAuth.AuthType.GOOGLE);
|
||||
break;
|
||||
case R.id.lost_password:
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_LOST_PASSWORD);
|
||||
recoverPassword();
|
||||
break;
|
||||
case R.id.register:
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_REG_REQUEST);
|
||||
register();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected void loginOsm()
|
||||
{
|
||||
startActivity(new Intent(getActivity(), OsmAuthActivity.class));
|
||||
dismiss();
|
||||
}
|
||||
|
||||
protected void loginWebview(final OsmOAuth.AuthType type)
|
||||
{
|
||||
final WebView webview = new InputWebView(getActivity());
|
||||
final AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(webview).create();
|
||||
|
||||
ThreadPool.getWorker().execute(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = (type == OsmOAuth.AuthType.FACEBOOK) ? OsmOAuth.nativeGetFacebookAuthUrl()
|
||||
: OsmOAuth.nativeGetGoogleAuthUrl();
|
||||
|
||||
UiThread.run(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if (isAdded())
|
||||
loadWebviewAuth(dialog, webview, auth, type);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
protected void loadWebviewAuth(final AlertDialog dialog, final WebView webview, @Size(3) final String[] auth, final OsmOAuth.AuthType type)
|
||||
{
|
||||
if (auth == null)
|
||||
{
|
||||
// TODO show some dialog
|
||||
return;
|
||||
}
|
||||
|
||||
final String authUrl = auth[0];
|
||||
webview.setWebViewClient(new WebViewClient()
|
||||
{
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url)
|
||||
{
|
||||
if (OsmOAuth.shouldReloadWebviewUrl(url))
|
||||
{
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
else if (url.contains(OsmOAuth.URL_PARAM_VERIFIER))
|
||||
{
|
||||
finishWebviewAuth(auth[1], auth[2], getVerifierFromUrl(url), type);
|
||||
dialog.cancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getVerifierFromUrl(String authUrl)
|
||||
{
|
||||
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
|
||||
sanitizer.setAllowUnregisteredParamaters(true);
|
||||
sanitizer.parseUrl(authUrl);
|
||||
return sanitizer.getValue(OsmOAuth.URL_PARAM_VERIFIER);
|
||||
}
|
||||
|
||||
});
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
|
||||
protected void finishWebviewAuth(final String key, final String secret, final String verifier, final OsmOAuth.AuthType type)
|
||||
{
|
||||
ThreadPool.getWorker().execute(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = OsmOAuth.nativeAuthWithWebviewToken(key, secret, verifier);
|
||||
UiThread.run(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
processAuth(auth, type);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void recoverPassword()
|
||||
{
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_RECOVER_PASSWORD)));
|
||||
}
|
||||
|
||||
protected void register()
|
||||
{
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_REGISTER)));
|
||||
dismiss();
|
||||
}
|
||||
|
||||
protected void processAuth(@Size(2) String[] auth, OsmOAuth.AuthType type)
|
||||
{
|
||||
if (auth == null)
|
||||
{
|
||||
if (isAdded())
|
||||
{
|
||||
new AlertDialog.Builder(getActivity()).setTitle(R.string.editor_login_error_dialog)
|
||||
.setPositiveButton(android.R.string.ok, null).show();
|
||||
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT,
|
||||
Statistics.params().add(Statistics.EventParam.IS_SUCCESS, false).add(Statistics.EventParam.TYPE, type.name));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
OsmOAuth.setAuthorization(auth[0], auth[1]);
|
||||
dismiss();
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT,
|
||||
Statistics.params().add(Statistics.EventParam.IS_SUCCESS, true).add(Statistics.EventParam.TYPE, type.name));
|
||||
mOsmAuthDelegate = new OsmAuthFragmentDelegate(this);
|
||||
mOsmAuthDelegate.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,21 @@
|
|||
package com.mapswithme.maps.editor;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.UrlQuerySanitizer;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.Size;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.widget.InputWebView;
|
||||
import com.mapswithme.maps.base.BaseMwmToolbarFragment;
|
||||
import com.mapswithme.maps.widget.ToolbarController;
|
||||
import com.mapswithme.util.Constants;
|
||||
import com.mapswithme.util.concurrency.ThreadPool;
|
||||
import com.mapswithme.util.concurrency.UiThread;
|
||||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
||||
public class AuthFragment extends BaseAuthFragment implements View.OnClickListener
|
||||
public class AuthFragment extends BaseMwmToolbarFragment
|
||||
{
|
||||
private OsmAuthFragmentDelegate mOsmAuthDelegate;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
|
||||
|
@ -37,10 +28,8 @@ public class AuthFragment extends BaseAuthFragment implements View.OnClickListen
|
|||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
mToolbarController.setTitle(R.string.thank_you);
|
||||
view.findViewById(R.id.login_osm).setOnClickListener(this);
|
||||
view.findViewById(R.id.login_facebook).setOnClickListener(this);
|
||||
view.findViewById(R.id.login_google).setOnClickListener(this);
|
||||
view.findViewById(R.id.register).setOnClickListener(this);
|
||||
mOsmAuthDelegate = new OsmAuthFragmentDelegate(this);
|
||||
mOsmAuthDelegate.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -56,136 +45,4 @@ public class AuthFragment extends BaseAuthFragment implements View.OnClickListen
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
// TODO show/hide spinners
|
||||
switch (v.getId())
|
||||
{
|
||||
case R.id.login_osm:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.OSM);
|
||||
loginOsm();
|
||||
break;
|
||||
case R.id.login_facebook:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.FACEBOOK);
|
||||
loginWebview(OsmOAuth.AuthType.FACEBOOK);
|
||||
break;
|
||||
case R.id.login_google:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.GOOGLE);
|
||||
loginWebview(OsmOAuth.AuthType.GOOGLE);
|
||||
break;
|
||||
case R.id.lost_password:
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_LOST_PASSWORD);
|
||||
recoverPassword();
|
||||
break;
|
||||
case R.id.register:
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_REG_REQUEST);
|
||||
register();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected void loginOsm()
|
||||
{
|
||||
getMwmActivity().replaceFragment(OsmAuthFragment.class, null, null);
|
||||
}
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
protected void loginWebview(final OsmOAuth.AuthType type)
|
||||
{
|
||||
final WebView webview = new InputWebView(getActivity());
|
||||
webview.getSettings().setJavaScriptEnabled(true);
|
||||
final AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(webview).create();
|
||||
|
||||
ThreadPool.getWorker().execute(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = (type == OsmOAuth.AuthType.FACEBOOK) ? OsmOAuth.nativeGetFacebookAuthUrl()
|
||||
: OsmOAuth.nativeGetGoogleAuthUrl();
|
||||
|
||||
UiThread.run(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if (isAdded())
|
||||
loadWebviewAuth(dialog, webview, auth, type);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
protected void loadWebviewAuth(final AlertDialog dialog, final WebView webview, @Size(3) final String[] auth, final OsmOAuth.AuthType type)
|
||||
{
|
||||
if (auth == null)
|
||||
{
|
||||
// TODO show some dialog
|
||||
return;
|
||||
}
|
||||
|
||||
final String authUrl = auth[0];
|
||||
webview.setWebViewClient(new WebViewClient()
|
||||
{
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url)
|
||||
{
|
||||
if (OsmOAuth.shouldReloadWebviewUrl(url))
|
||||
{
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
else if (url.contains(OsmOAuth.URL_PARAM_VERIFIER))
|
||||
{
|
||||
finishWebviewAuth(auth[1], auth[2], getVerifierFromUrl(url), type);
|
||||
dialog.cancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getVerifierFromUrl(String authUrl)
|
||||
{
|
||||
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
|
||||
sanitizer.setAllowUnregisteredParamaters(true);
|
||||
sanitizer.parseUrl(authUrl);
|
||||
return sanitizer.getValue(OsmOAuth.URL_PARAM_VERIFIER);
|
||||
}
|
||||
|
||||
});
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
|
||||
protected void finishWebviewAuth(final String key, final String secret, final String verifier, final OsmOAuth.AuthType type)
|
||||
{
|
||||
ThreadPool.getWorker().execute(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = OsmOAuth.nativeAuthWithWebviewToken(key, secret, verifier);
|
||||
UiThread.run(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
processAuth(auth, type);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void recoverPassword()
|
||||
{
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_RECOVER_PASSWORD)));
|
||||
}
|
||||
|
||||
protected void register()
|
||||
{
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_REGISTER)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
package com.mapswithme.maps.editor;
|
||||
|
||||
import android.support.annotation.Size;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmToolbarFragment;
|
||||
import com.mapswithme.util.Utils;
|
||||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
||||
public abstract class BaseAuthFragment extends BaseMwmToolbarFragment
|
||||
{
|
||||
protected void processAuth(@Size(2) String[] auth, OsmOAuth.AuthType type)
|
||||
{
|
||||
if (auth == null)
|
||||
{
|
||||
if (isAdded())
|
||||
{
|
||||
new AlertDialog.Builder(getActivity()).setTitle(R.string.editor_login_error_dialog)
|
||||
.setPositiveButton(android.R.string.ok, null).show();
|
||||
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT,
|
||||
Statistics.params().add(Statistics.EventParam.IS_SUCCESS, false).add(Statistics.EventParam.TYPE, type.name));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
OsmOAuth.setAuthorization(auth[0], auth[1]);
|
||||
Utils.navigateToParent(getActivity());
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT,
|
||||
Statistics.params().add(Statistics.EventParam.IS_SUCCESS, true).add(Statistics.EventParam.TYPE, type.name));
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@ import android.widget.TextView;
|
|||
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmToolbarFragment;
|
||||
import com.mapswithme.maps.widget.ToolbarController;
|
||||
import com.mapswithme.util.Constants;
|
||||
import com.mapswithme.util.Graphics;
|
||||
|
@ -24,8 +25,10 @@ import com.mapswithme.util.UiUtils;
|
|||
import com.mapswithme.util.concurrency.ThreadPool;
|
||||
import com.mapswithme.util.concurrency.UiThread;
|
||||
|
||||
public class OsmAuthFragment extends BaseAuthFragment implements View.OnClickListener
|
||||
public class OsmAuthFragment extends BaseMwmToolbarFragment implements View.OnClickListener
|
||||
{
|
||||
private OsmAuthFragmentDelegate mDelegate;
|
||||
|
||||
private ProgressBar mProgress;
|
||||
private TextView mTvLogin;
|
||||
private View mTvLostPassword;
|
||||
|
@ -61,6 +64,7 @@ public class OsmAuthFragment extends BaseAuthFragment implements View.OnClickLis
|
|||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
|
||||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
mDelegate = new OsmAuthFragmentDelegate(this);
|
||||
mToolbarController.setTitle(R.string.login);
|
||||
mEtLogin = (EditText) view.findViewById(R.id.osm_username);
|
||||
mEtPassword = (EditText) view.findViewById(R.id.osm_password);
|
||||
|
@ -108,7 +112,7 @@ public class OsmAuthFragment extends BaseAuthFragment implements View.OnClickLis
|
|||
public void run()
|
||||
{
|
||||
final String[] auth = OsmOAuth.nativeAuthWithPassword(username, password);
|
||||
|
||||
final String username = auth == null ? null : OsmOAuth.nativeGetOsmUsername(auth[0], auth[1]);
|
||||
UiThread.run(new Runnable()
|
||||
{
|
||||
@Override
|
||||
|
@ -120,7 +124,7 @@ public class OsmAuthFragment extends BaseAuthFragment implements View.OnClickLis
|
|||
enableInput(true);
|
||||
UiUtils.hide(mProgress);
|
||||
mTvLogin.setText(R.string.login);
|
||||
processAuth(auth, OsmOAuth.AuthType.OSM);
|
||||
mDelegate.processAuth(auth, OsmOAuth.AuthType.OSM, username);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,195 @@
|
|||
package com.mapswithme.maps.editor;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.UrlQuerySanitizer;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.Size;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.View;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmFragmentActivity;
|
||||
import com.mapswithme.maps.widget.InputWebView;
|
||||
import com.mapswithme.util.Constants;
|
||||
import com.mapswithme.util.Utils;
|
||||
import com.mapswithme.util.concurrency.ThreadPool;
|
||||
import com.mapswithme.util.concurrency.UiThread;
|
||||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
||||
public class OsmAuthFragmentDelegate implements View.OnClickListener
|
||||
{
|
||||
private final Fragment mFragment;
|
||||
|
||||
public OsmAuthFragmentDelegate(Fragment fragment)
|
||||
{
|
||||
mFragment = fragment;
|
||||
}
|
||||
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
|
||||
{
|
||||
view.findViewById(R.id.login_osm).setOnClickListener(this);
|
||||
view.findViewById(R.id.login_facebook).setOnClickListener(this);
|
||||
view.findViewById(R.id.login_google).setOnClickListener(this);
|
||||
view.findViewById(R.id.register).setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
// TODO show/hide spinners
|
||||
switch (v.getId())
|
||||
{
|
||||
case R.id.login_osm:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.OSM);
|
||||
loginOsm();
|
||||
break;
|
||||
case R.id.login_facebook:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.FACEBOOK);
|
||||
loginWebview(OsmOAuth.AuthType.FACEBOOK);
|
||||
break;
|
||||
case R.id.login_google:
|
||||
Statistics.INSTANCE.trackAuthRequest(OsmOAuth.AuthType.GOOGLE);
|
||||
loginWebview(OsmOAuth.AuthType.GOOGLE);
|
||||
break;
|
||||
case R.id.lost_password:
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_LOST_PASSWORD);
|
||||
recoverPassword();
|
||||
break;
|
||||
case R.id.register:
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_REG_REQUEST);
|
||||
register();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected void processAuth(@Size(2) String[] auth, OsmOAuth.AuthType type, String username)
|
||||
{
|
||||
if (auth == null)
|
||||
{
|
||||
if (mFragment.isAdded())
|
||||
{
|
||||
new AlertDialog.Builder(mFragment.getActivity()).setTitle(R.string.editor_login_error_dialog)
|
||||
.setPositiveButton(android.R.string.ok, null).show();
|
||||
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT,
|
||||
Statistics.params().add(Statistics.EventParam.IS_SUCCESS, false).add(Statistics.EventParam.TYPE, type.name));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
OsmOAuth.setAuthorization(auth[0], auth[1], username);
|
||||
Utils.navigateToParent(mFragment.getActivity());
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT,
|
||||
Statistics.params().add(Statistics.EventParam.IS_SUCCESS, true).add(Statistics.EventParam.TYPE, type.name));
|
||||
}
|
||||
|
||||
protected void loginOsm()
|
||||
{
|
||||
((BaseMwmFragmentActivity) mFragment.getActivity()).replaceFragment(OsmAuthFragment.class, null, null);
|
||||
}
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
protected void loginWebview(final OsmOAuth.AuthType type)
|
||||
{
|
||||
final WebView webview = new InputWebView(mFragment.getActivity());
|
||||
webview.getSettings().setJavaScriptEnabled(true);
|
||||
final AlertDialog dialog = new AlertDialog.Builder(mFragment.getActivity()).setView(webview).create();
|
||||
|
||||
ThreadPool.getWorker().execute(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = (type == OsmOAuth.AuthType.FACEBOOK) ? OsmOAuth.nativeGetFacebookAuthUrl()
|
||||
: OsmOAuth.nativeGetGoogleAuthUrl();
|
||||
|
||||
UiThread.run(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if (mFragment.isAdded())
|
||||
loadWebviewAuth(dialog, webview, auth, type);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
protected void loadWebviewAuth(final AlertDialog dialog, final WebView webview, @Size(3) final String[] auth, final OsmOAuth.AuthType type)
|
||||
{
|
||||
if (auth == null)
|
||||
{
|
||||
// TODO show some dialog
|
||||
return;
|
||||
}
|
||||
|
||||
final String authUrl = auth[0];
|
||||
webview.setWebViewClient(new WebViewClient()
|
||||
{
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url)
|
||||
{
|
||||
if (OsmOAuth.shouldReloadWebviewUrl(url))
|
||||
{
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
else if (url.contains(OsmOAuth.URL_PARAM_VERIFIER))
|
||||
{
|
||||
finishWebviewAuth(auth[1], auth[2], getVerifierFromUrl(url), type);
|
||||
dialog.cancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getVerifierFromUrl(String authUrl)
|
||||
{
|
||||
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
|
||||
sanitizer.setAllowUnregisteredParamaters(true);
|
||||
sanitizer.parseUrl(authUrl);
|
||||
return sanitizer.getValue(OsmOAuth.URL_PARAM_VERIFIER);
|
||||
}
|
||||
|
||||
});
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
|
||||
protected void finishWebviewAuth(final String key, final String secret, final String verifier, final OsmOAuth.AuthType type)
|
||||
{
|
||||
ThreadPool.getWorker().execute(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = OsmOAuth.nativeAuthWithWebviewToken(key, secret, verifier);
|
||||
final String username = auth == null ? null : OsmOAuth.nativeGetOsmUsername(auth[0], auth[1]);
|
||||
UiThread.run(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
processAuth(auth, type, username);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void recoverPassword()
|
||||
{
|
||||
mFragment.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_RECOVER_PASSWORD)));
|
||||
}
|
||||
|
||||
protected void register()
|
||||
{
|
||||
mFragment.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_REGISTER)));
|
||||
}
|
||||
}
|
|
@ -42,6 +42,7 @@ public final class OsmOAuth
|
|||
|
||||
private static final String PREF_OSM_TOKEN = "OsmToken";
|
||||
private static final String PREF_OSM_SECRET = "OsmSecret";
|
||||
private static final String PREF_OSM_USERNAME = "OsmUsername";
|
||||
|
||||
public static final String URL_PARAM_VERIFIER = "oauth_verifier";
|
||||
|
||||
|
@ -61,11 +62,17 @@ public final class OsmOAuth
|
|||
return MwmApplication.prefs().getString(PREF_OSM_SECRET, "");
|
||||
}
|
||||
|
||||
public static void setAuthorization(String token, String secret)
|
||||
public static String getUsername()
|
||||
{
|
||||
return MwmApplication.prefs().getString(PREF_OSM_USERNAME, "");
|
||||
}
|
||||
|
||||
public static void setAuthorization(String token, String secret, String username)
|
||||
{
|
||||
MwmApplication.prefs().edit()
|
||||
.putString(PREF_OSM_TOKEN, token)
|
||||
.putString(PREF_OSM_SECRET, secret)
|
||||
.putString(PREF_OSM_USERNAME, username)
|
||||
.apply();
|
||||
}
|
||||
|
||||
|
@ -74,6 +81,7 @@ public final class OsmOAuth
|
|||
MwmApplication.prefs().edit()
|
||||
.remove(PREF_OSM_TOKEN)
|
||||
.remove(PREF_OSM_SECRET)
|
||||
.remove(PREF_OSM_USERNAME)
|
||||
.apply();
|
||||
}
|
||||
|
||||
|
@ -100,7 +108,7 @@ public final class OsmOAuth
|
|||
@WorkerThread
|
||||
@Size(2)
|
||||
@Nullable
|
||||
public static native String[] nativeAuthWithWebviewToken(String secret, String token, String verifier);
|
||||
public static native String[] nativeAuthWithWebviewToken(String key, String secret, String verifier);
|
||||
|
||||
/**
|
||||
* @return url for web auth, and token with secret for finishing authorization later
|
||||
|
@ -115,4 +123,8 @@ public final class OsmOAuth
|
|||
@Size(3)
|
||||
@Nullable
|
||||
public static native String[] nativeGetGoogleAuthUrl();
|
||||
|
||||
@WorkerThread
|
||||
@Nullable
|
||||
public static native String nativeGetOsmUsername(String token, String secret);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ import com.mapswithme.util.UiUtils;
|
|||
|
||||
public class ProfileFragment extends AuthFragment implements View.OnClickListener
|
||||
{
|
||||
private View mEditsBlock;
|
||||
private TextView mEditsLocal;
|
||||
private View mEditsMore;
|
||||
private TextView mEditsSent;
|
||||
|
@ -38,7 +37,7 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene
|
|||
{
|
||||
mLogout = mToolbarController.findViewById(R.id.logout);
|
||||
mLogout.setOnClickListener(this);
|
||||
mEditsBlock = view.findViewById(R.id.block_edits);
|
||||
View mEditsBlock = view.findViewById(R.id.block_edits);
|
||||
UiUtils.show(mEditsBlock);
|
||||
final View localEdits = mEditsBlock.findViewById(R.id.local_edits);
|
||||
((ImageView) localEdits.findViewById(R.id.image)).setImageResource(R.drawable.ic_device);
|
||||
|
@ -101,8 +100,6 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene
|
|||
}
|
||||
}).tint().show();
|
||||
break;
|
||||
default:
|
||||
super.onClick(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue