[android] Google auth

This commit is contained in:
Александр Зацепин 2018-04-13 13:54:41 +03:00 committed by Arsentiy Milchakov
parent 7a5df99da9
commit adaeb6e5f7
5 changed files with 91 additions and 23 deletions

View file

@ -51,11 +51,12 @@ dependencies {
implementation 'com.android.support:customtabs:25.0.0'
// google play services
implementation 'com.google.android.gms:play-services-location:10.0.1'
implementation 'com.google.android.gms:play-services-analytics:10.0.1'
implementation 'com.google.android.gms:play-services-plus:10.0.1'
implementation 'com.google.android.gms:play-services-gcm:10.0.1'
implementation 'com.google.android.gms:play-services-ads:10.0.1'
implementation 'com.google.android.gms:play-services-location:11.8.0'
implementation 'com.google.android.gms:play-services-analytics:11.8.0'
implementation 'com.google.android.gms:play-services-plus:11.8.0'
implementation 'com.google.android.gms:play-services-gcm:11.8.0'
implementation 'com.google.android.gms:play-services-ads:11.8.0'
implementation 'com.google.android.gms:play-services-auth:11.8.0'
// statistics
implementation 'com.flurry.android:analytics:6.7.0'
// crash reporting

View file

@ -51,4 +51,10 @@ extern "C"
{
return static_cast<jlong>(AD_PERMISION_CHECK_DURATION);
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_PrivateVariables_googleWebClientId(JNIEnv * env, jclass clazz)
{
return env->NewStringUTF(GOOGLE_WEB_CLIENT_ID);
}
}

View file

@ -22,6 +22,12 @@
android:textAppearance="@style/MwmTextAppearance.Body3"
android:text="@string/profile_authorization_message"
/>
<com.google.android.gms.common.SignInButton
android:id="@+id/google_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/height_primary_button"
android:layout_marginTop="@dimen/margin_base"/>
<com.facebook.login.widget.LoginButton
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:id="@+id/facebook_button"

View file

@ -12,6 +12,7 @@ public class PrivateVariables
public static native int myTargetSlot();
public static native int myTargetRbSlot();
public static native String myTargetCheckUrl();
public static native String googleWebClientId();
/**
* @return interval in seconds
*/

View file

@ -20,7 +20,14 @@ 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;
import com.mapswithme.util.log.Logger;
@ -34,9 +41,11 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
private static final String TAG = SocialAuthDialogFragment.class.getSimpleName();
@SuppressWarnings("NullableProblems")
@NonNull
private GoogleSignInClient mGoogleSignInClient;
@NonNull
private final CallbackManager mFacebookCallbackManager = CallbackManager.Factory.create();
@Nullable
private String mPhoneAuthToken;
@ -46,6 +55,17 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
PhoneAuthActivity.startForResult(this);
};
@NonNull
private final View.OnClickListener mGoogleClickListener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = mGoogleSignInClient.getSignInIntent();
startActivity(intent);
}
};
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
@ -55,12 +75,27 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
return res;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(PrivateVariables.googleWebClientId())
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getActivity(), gso);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_auth_passport_dialog, container, false);
SignInButton googleButton = view.findViewById(R.id.google_button);
googleButton.setOnClickListener(mGoogleClickListener);
LoginButton facebookButton = view.findViewById(R.id.facebook_button);
facebookButton.setReadPermissions(Constants.FACEBOOK_PERMISSIONS);
facebookButton.setFragment(this);
@ -75,12 +110,18 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
public void onResume()
{
super.onResume();
AccessToken facebookToken = AccessToken.getCurrentAccessToken();
String tokenValue = null;
if (facebookToken != null)
tokenValue = facebookToken.getToken();
if (TextUtils.isEmpty(tokenValue))
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))
{
Statistics.INSTANCE.trackEvent(Statistics.EventName.UGC_AUTH_SHOWN);
return;
@ -125,10 +166,11 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
{
String token = mPhoneAuthToken;
@Framework.AuthTokenType
int tokenType = !TextUtils.isEmpty(token) ?
Framework.SOCIAL_TOKEN_PHONE :
Framework.SOCIAL_TOKEN_FACEBOOK;
if (TextUtils.isEmpty(token))
{
GoogleSignInAccount googleAccount = GoogleSignIn.getLastSignedInAccount(MwmApplication.get());
token = googleAccount != null ? googleAccount.getIdToken() : null;
}
if (TextUtils.isEmpty(token))
{
@ -137,10 +179,23 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
}
int resultCode = TextUtils.isEmpty(token) ? Activity.RESULT_CANCELED : Activity.RESULT_OK;
sendResult(resultCode, token, tokenType, null, true);
sendResult(resultCode, token, getAuthTokenType(), 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
@ -162,27 +217,26 @@ public class SocialAuthDialogFragment extends BaseMwmDialogFragment
public void onCancel()
{
LOGGER.w(TAG, "onCancel");
sendResult(Activity.RESULT_CANCELED, null, Framework.SOCIAL_TOKEN_FACEBOOK,
null, true);
sendErrorResult(Activity.RESULT_CANCELED, Framework.SOCIAL_TOKEN_FACEBOOK,
null, true);
}
@Override
public void onError(FacebookException error)
{
LOGGER.e(TAG, "onError", error);
sendResult(Activity.RESULT_CANCELED, null, Framework.SOCIAL_TOKEN_FACEBOOK,
sendErrorResult(Activity.RESULT_CANCELED, Framework.SOCIAL_TOKEN_FACEBOOK,
error != null ? error.getMessage() : null, false);
}
private void sendResult(int resultCode, @Nullable String socialToken,
@Framework.AuthTokenType int type, @Nullable String error,
boolean isCancel)
private void sendErrorResult(int resultCode, @Framework.AuthTokenType int type,
@Nullable String error, boolean isCancel)
{
SocialAuthDialogFragment fragment = mFragmentRef.get();
if (fragment == null)
return;
fragment.sendResult(resultCode, socialToken, type, error, isCancel);
fragment.sendResult(resultCode, null, type, error, isCancel);
}
}
}