Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorАлександр Зацепин <az@mapswithme.com>2018-04-15 11:41:50 +0300
committerArsentiy Milchakov <milcars@mapswithme.com>2018-04-16 11:55:04 +0300
commitd2b81dc0fc162123c26ab73071b4448fef4c59ac (patch)
tree8f1056470808f6526215e65e6c756b7d7a20a2ba /android/src
parentadaeb6e5f7cc936d072ceac770dae4f4696d9b0b (diff)
[android] Refactored social token processing
Diffstat (limited to 'android/src')
-rw-r--r--android/src/com/mapswithme/maps/Framework.java5
-rw-r--r--android/src/com/mapswithme/maps/auth/Authorizer.java4
-rw-r--r--android/src/com/mapswithme/maps/auth/FacebookTokenHandler.java31
-rw-r--r--android/src/com/mapswithme/maps/auth/GoogleTokenHandler.java33
-rw-r--r--android/src/com/mapswithme/maps/auth/SocialAuthDialogFragment.java99
-rw-r--r--android/src/com/mapswithme/maps/auth/TokenHandler.java16
6 files changed, 140 insertions, 48 deletions
diff --git a/android/src/com/mapswithme/maps/Framework.java b/android/src/com/mapswithme/maps/Framework.java
index 5d8769a008..2f3474287f 100644
--- a/android/src/com/mapswithme/maps/Framework.java
+++ b/android/src/com/mapswithme/maps/Framework.java
@@ -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;
diff --git a/android/src/com/mapswithme/maps/auth/Authorizer.java b/android/src/com/mapswithme/maps/auth/Authorizer.java
index 026794def7..967b04a857 100644
--- a/android/src/com/mapswithme/maps/auth/Authorizer.java
+++ b/android/src/com/mapswithme/maps/auth/Authorizer.java
@@ -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();
diff --git a/android/src/com/mapswithme/maps/auth/FacebookTokenHandler.java b/android/src/com/mapswithme/maps/auth/FacebookTokenHandler.java
new file mode 100644
index 0000000000..aa9dce2467
--- /dev/null
+++ b/android/src/com/mapswithme/maps/auth/FacebookTokenHandler.java
@@ -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;
+ }
+}
diff --git a/android/src/com/mapswithme/maps/auth/GoogleTokenHandler.java b/android/src/com/mapswithme/maps/auth/GoogleTokenHandler.java
new file mode 100644
index 0000000000..80a75318c0
--- /dev/null
+++ b/android/src/com/mapswithme/maps/auth/GoogleTokenHandler.java
@@ -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;
+ }
+}
diff --git a/android/src/com/mapswithme/maps/auth/SocialAuthDialogFragment.java b/android/src/com/mapswithme/maps/auth/SocialAuthDialogFragment.java
index 17a0627353..9d6bb70a0f 100644
--- a/android/src/com/mapswithme/maps/auth/SocialAuthDialogFragment.java
+++ b/android/src/com/mapswithme/maps/auth/SocialAuthDialogFragment.java
@@ -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;
}
-
- if (TextUtils.isEmpty(token))
+ else
{
- AccessToken facebookToken = AccessToken.getCurrentAccessToken();
- token = facebookToken != null ? facebookToken.getToken() : null;
+ 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!");
}
- 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
diff --git a/android/src/com/mapswithme/maps/auth/TokenHandler.java b/android/src/com/mapswithme/maps/auth/TokenHandler.java
new file mode 100644
index 0000000000..d1649c071d
--- /dev/null
+++ b/android/src/com/mapswithme/maps/auth/TokenHandler.java
@@ -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();
+}