From a5bc7bea298af8d59c9375d632b7653c072f2929 Mon Sep 17 00:00:00 2001 From: Mian Khalid Date: Thu, 20 Apr 2017 17:04:04 +0500 Subject: [PATCH] Fix google plus signup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed legacy commented code and migrated the logic to OkHttp alternative for fetching a user’s google profile. --- .../mobile/social/google/GoogleProvider.java | 88 +++++++------------ 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/OpenEdXMobile/src/main/java/org/edx/mobile/social/google/GoogleProvider.java b/OpenEdXMobile/src/main/java/org/edx/mobile/social/google/GoogleProvider.java index e4334baa48..189ab74043 100644 --- a/OpenEdXMobile/src/main/java/org/edx/mobile/social/google/GoogleProvider.java +++ b/OpenEdXMobile/src/main/java/org/edx/mobile/social/google/GoogleProvider.java @@ -1,21 +1,22 @@ package org.edx.mobile.social.google; import android.content.Context; -import android.os.Bundle; +import android.support.annotation.NonNull; import android.text.TextUtils; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -//import org.edx.mobile.http.HttpManager; +import org.edx.mobile.http.callback.CallTrigger; +import org.edx.mobile.http.callback.ErrorHandlingOkCallback; +import org.edx.mobile.http.provider.OkHttpClientProvider; import org.edx.mobile.social.SocialFactory; import org.edx.mobile.social.SocialLoginDelegate; import org.edx.mobile.social.SocialMember; import org.edx.mobile.social.SocialProvider; -import org.edx.mobile.task.Task; +import okhttp3.Request; +import roboguice.RoboGuice; public class GoogleProvider implements SocialProvider { + private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s"; private GoogleOauth2 google; @@ -42,55 +43,30 @@ public void getUser(Callback callback) { public void getUserInfo(Context context, SocialFactory.SOCIAL_SOURCE_TYPE socialType, String accessToken, final SocialLoginDelegate.SocialUserInfoCallback userInfoCallback) { - // TODO: See if this should be implemented in Retrofit - //new GoogleUserInfoTask(context, userInfoCallback, accessToken).execute(); - + final OkHttpClientProvider okHttpClientProvider = RoboGuice.getInjector(context).getInstance(OkHttpClientProvider.class); + okHttpClientProvider.get().newCall(new Request.Builder() + .url(String.format(USER_INFO_URL, accessToken)) + .get() + .build()) + .enqueue(new ErrorHandlingOkCallback(context, + GoogleUserProfile.class, CallTrigger.LOADING_UNCACHED) { + @Override + protected void onResponse(@NonNull GoogleUserProfile userProfile) { + String name = userProfile.name; + if (TextUtils.isEmpty(name)) { + if (!TextUtils.isEmpty(userProfile.given_name)) { + name = userProfile.given_name + " "; + } + if (!TextUtils.isEmpty(userProfile.family_name)) { + if (TextUtils.isEmpty(name)) { + name = userProfile.family_name; + } else { + name += userProfile.family_name; + } + } + } + userInfoCallback.setSocialUserInfo(google.getEmail(), name); + } + }); } - - /*private class GoogleUserInfoTask extends Task { - private SocialLoginDelegate.SocialUserInfoCallback userInfoCallback; - private String accessToken; - - public GoogleUserInfoTask(Context activity, SocialLoginDelegate.SocialUserInfoCallback userInfoCallback, String accessToken) { - super(activity); - this.userInfoCallback = userInfoCallback; - this.accessToken = accessToken; - } - - @Override - public void onSuccess(String result) { - userInfoCallback.setSocialUserInfo(((GoogleOauth2) google).getEmail(), result); - } - - @Override - public String call() throws Exception { - //try to wait a while . - try { - Thread.sleep(300); - } catch (InterruptedException e) { - e.printStackTrace(); - } - //TODO - current code use GoogleAuthUtil for google login, - //It is hard to synchronize with GoogleApiClient.Builder - //also the way to handle the session for both google and facebook need - //the code refactoring. - Bundle p = new Bundle(); - String url = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessToken; - String json = new HttpManager(getContext()).get(url, p).body; - logger.debug(json); - Gson gson = new GsonBuilder().create(); - GoogleUserProfile userProfile = gson.fromJson(json, GoogleUserProfile.class); - String name = userProfile.name; - if (TextUtils.isEmpty(name)) { - if (!TextUtils.isEmpty(userProfile.given_name)) { - name = userProfile.given_name + " "; - } - if (!TextUtils.isEmpty(userProfile.family_name)) { - name += userProfile.given_name; - } - } - return name; - } - }*/ - }