forked from organicmaps/organicmaps
[android] Refactored social token processing
This commit is contained in:
parent
adaeb6e5f7
commit
d2b81dc0fc
6 changed files with 141 additions and 49 deletions
|
@ -88,10 +88,11 @@ public class Framework
|
|||
public static final int ROUTE_REBUILD_AFTER_POINTS_LOADING = 0;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({ SOCIAL_TOKEN_FACEBOOK, SOCIAL_TOKEN_GOOGLE, SOCIAL_TOKEN_PHONE, TOKEN_MAPSME })
|
||||
@IntDef({ SOCIAL_TOKEN_INVALID, SOCIAL_TOKEN_FACEBOOK, SOCIAL_TOKEN_GOOGLE,
|
||||
SOCIAL_TOKEN_PHONE, TOKEN_MAPSME })
|
||||
public @interface AuthTokenType
|
||||
{}
|
||||
|
||||
public static final int SOCIAL_TOKEN_INVALID = -1;
|
||||
public static final int SOCIAL_TOKEN_FACEBOOK = 0;
|
||||
public static final int SOCIAL_TOKEN_GOOGLE = 1;
|
||||
public static final int SOCIAL_TOKEN_PHONE = 2;
|
||||
|
|
|
@ -82,7 +82,7 @@ public class Authorizer implements AuthorizationListener
|
|||
return;
|
||||
|
||||
@Framework.AuthTokenType
|
||||
int type = data.getIntExtra(Constants.EXTRA_TOKEN_TYPE, -1);
|
||||
int type = data.getIntExtra(Constants.EXTRA_TOKEN_TYPE, Framework.SOCIAL_TOKEN_INVALID);
|
||||
boolean isCancel = data.getBooleanExtra(Constants.EXTRA_IS_CANCEL, false);
|
||||
if (isCancel)
|
||||
{
|
||||
|
@ -101,7 +101,7 @@ public class Authorizer implements AuthorizationListener
|
|||
if (!TextUtils.isEmpty(socialToken))
|
||||
{
|
||||
@Framework.AuthTokenType
|
||||
int type = data.getIntExtra(Constants.EXTRA_TOKEN_TYPE, -1);
|
||||
int type = data.getIntExtra(Constants.EXTRA_TOKEN_TYPE, Framework.SOCIAL_TOKEN_INVALID);
|
||||
mIsAuthorizationInProgress = true;
|
||||
if (mCallback != null)
|
||||
mCallback.onAuthorizationStart();
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.mapswithme.maps.auth;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.facebook.AccessToken;
|
||||
import com.mapswithme.maps.Framework;
|
||||
|
||||
class FacebookTokenHandler implements TokenHandler
|
||||
{
|
||||
@Override
|
||||
public boolean checkToken()
|
||||
{
|
||||
AccessToken facebookToken = AccessToken.getCurrentAccessToken();
|
||||
return facebookToken != null && !TextUtils.isEmpty(facebookToken.getToken());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getToken()
|
||||
{
|
||||
AccessToken facebookToken = AccessToken.getCurrentAccessToken();
|
||||
return facebookToken != null ? facebookToken.getToken() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getType()
|
||||
{
|
||||
return Framework.SOCIAL_TOKEN_FACEBOOK;
|
||||
}
|
||||
}
|
33
android/src/com/mapswithme/maps/auth/GoogleTokenHandler.java
Normal file
33
android/src/com/mapswithme/maps/auth/GoogleTokenHandler.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package com.mapswithme.maps.auth;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
|
||||
import com.mapswithme.maps.Framework;
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
|
||||
class GoogleTokenHandler implements TokenHandler
|
||||
{
|
||||
@Override
|
||||
public boolean checkToken()
|
||||
{
|
||||
GoogleSignInAccount googleAccount = GoogleSignIn.getLastSignedInAccount(MwmApplication.get());
|
||||
return googleAccount != null && !TextUtils.isEmpty(googleAccount.getIdToken());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getToken()
|
||||
{
|
||||
GoogleSignInAccount googleAccount = GoogleSignIn.getLastSignedInAccount(MwmApplication.get());
|
||||
return googleAccount != null ? googleAccount.getIdToken() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getType()
|
||||
{
|
||||
return Framework.SOCIAL_TOKEN_GOOGLE;
|
||||
}
|
||||
}
|
|
@ -14,19 +14,16 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
|
||||
import com.facebook.AccessToken;
|
||||
import com.facebook.CallbackManager;
|
||||
import com.facebook.FacebookCallback;
|
||||
import com.facebook.FacebookException;
|
||||
import com.facebook.login.LoginResult;
|
||||
import com.facebook.login.widget.LoginButton;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
|
||||
import com.google.android.gms.common.SignInButton;
|
||||
import com.mapswithme.maps.Framework;
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.maps.PrivateVariables;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmDialogFragment;
|
||||
|
@ -35,6 +32,8 @@ import com.mapswithme.util.log.LoggerFactory;
|
|||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SocialAuthDialogFragment extends BaseMwmDialogFragment
|
||||
{
|
||||
|
@ -48,7 +47,32 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
|
|||
private final CallbackManager mFacebookCallbackManager = CallbackManager.Factory.create();
|
||||
@Nullable
|
||||
private String mPhoneAuthToken;
|
||||
@NonNull
|
||||
private final List<TokenHandler> mTokenHandlers = Arrays.asList(
|
||||
new FacebookTokenHandler(), new GoogleTokenHandler(),
|
||||
new TokenHandler()
|
||||
{
|
||||
@Override
|
||||
public boolean checkToken()
|
||||
{
|
||||
return !TextUtils.isEmpty(mPhoneAuthToken);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getToken()
|
||||
{
|
||||
return mPhoneAuthToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getType()
|
||||
{
|
||||
return Framework.SOCIAL_TOKEN_PHONE;
|
||||
}
|
||||
});
|
||||
@Nullable
|
||||
private TokenHandler mCurrentTokenHandler;
|
||||
@NonNull
|
||||
private final View.OnClickListener mPhoneClickListener = (View v) ->
|
||||
{
|
||||
|
@ -111,24 +135,18 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
|
|||
{
|
||||
super.onResume();
|
||||
|
||||
AccessToken facebookToken = AccessToken.getCurrentAccessToken();
|
||||
String fbTokenValue = null;
|
||||
if (facebookToken != null)
|
||||
fbTokenValue = facebookToken.getToken();
|
||||
|
||||
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(MwmApplication.get());
|
||||
String googleTokenValue = null;
|
||||
if (account != null)
|
||||
googleTokenValue = account.getIdToken();
|
||||
|
||||
if (TextUtils.isEmpty(fbTokenValue) && TextUtils.isEmpty(googleTokenValue))
|
||||
for (TokenHandler handler: mTokenHandlers)
|
||||
{
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.UGC_AUTH_SHOWN);
|
||||
return;
|
||||
if (handler.checkToken())
|
||||
{
|
||||
mCurrentTokenHandler = handler;
|
||||
LOGGER.i(TAG, "Social token is already obtained");
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.i(TAG, "Social token is already obtained");
|
||||
dismiss();
|
||||
Statistics.INSTANCE.trackEvent(Statistics.EventName.UGC_AUTH_SHOWN);
|
||||
}
|
||||
|
||||
private void sendResult(int resultCode, @Nullable String socialToken,
|
||||
|
@ -155,7 +173,7 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
|
|||
if (data != null && requestCode == Constants.REQ_CODE_PHONE_AUTH_RESULT)
|
||||
{
|
||||
mPhoneAuthToken = data.getStringExtra(Constants.EXTRA_PHONE_AUTH_TOKEN);
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
|
||||
mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
|
||||
|
@ -164,38 +182,31 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
|
|||
@Override
|
||||
public void onDismiss(DialogInterface dialog)
|
||||
{
|
||||
String token = mPhoneAuthToken;
|
||||
|
||||
if (TextUtils.isEmpty(token))
|
||||
int resultCode;
|
||||
String token;
|
||||
@Framework.AuthTokenType
|
||||
int type;
|
||||
if (mCurrentTokenHandler == null)
|
||||
{
|
||||
GoogleSignInAccount googleAccount = GoogleSignIn.getLastSignedInAccount(MwmApplication.get());
|
||||
token = googleAccount != null ? googleAccount.getIdToken() : null;
|
||||
resultCode = Activity.RESULT_CANCELED;
|
||||
token = null;
|
||||
type = Framework.SOCIAL_TOKEN_INVALID;
|
||||
}
|
||||
else
|
||||
{
|
||||
resultCode = Activity.RESULT_OK;
|
||||
token = mCurrentTokenHandler.getToken();
|
||||
type = mCurrentTokenHandler.getType();
|
||||
if (TextUtils.isEmpty(token))
|
||||
throw new AssertionError("Token must be non-null while token handler is non-null!");
|
||||
if (type == Framework.SOCIAL_TOKEN_INVALID)
|
||||
throw new AssertionError("Token type must be non-invalid while token handler is non-null!");
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(token))
|
||||
{
|
||||
AccessToken facebookToken = AccessToken.getCurrentAccessToken();
|
||||
token = facebookToken != null ? facebookToken.getToken() : null;
|
||||
}
|
||||
|
||||
int resultCode = TextUtils.isEmpty(token) ? Activity.RESULT_CANCELED : Activity.RESULT_OK;
|
||||
sendResult(resultCode, token, getAuthTokenType(), null, true);
|
||||
sendResult(resultCode, token, type, null, true);
|
||||
super.onDismiss(dialog);
|
||||
}
|
||||
|
||||
@Framework.AuthTokenType
|
||||
private int getAuthTokenType()
|
||||
{
|
||||
if (!TextUtils.isEmpty(mPhoneAuthToken))
|
||||
return Framework.SOCIAL_TOKEN_PHONE;
|
||||
|
||||
GoogleSignInAccount googleAccount = GoogleSignIn.getLastSignedInAccount(MwmApplication.get());
|
||||
if (googleAccount != null && !TextUtils.isEmpty(googleAccount.getIdToken()))
|
||||
return Framework.SOCIAL_TOKEN_GOOGLE;
|
||||
|
||||
return Framework.SOCIAL_TOKEN_FACEBOOK;
|
||||
}
|
||||
|
||||
private static class FBCallback implements FacebookCallback<LoginResult>
|
||||
{
|
||||
@NonNull
|
||||
|
|
16
android/src/com/mapswithme/maps/auth/TokenHandler.java
Normal file
16
android/src/com/mapswithme/maps/auth/TokenHandler.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
package com.mapswithme.maps.auth;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.mapswithme.maps.Framework;
|
||||
|
||||
interface TokenHandler
|
||||
{
|
||||
boolean checkToken();
|
||||
|
||||
@Nullable
|
||||
String getToken();
|
||||
|
||||
@Framework.AuthTokenType
|
||||
int getType();
|
||||
}
|
Loading…
Add table
Reference in a new issue