Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #946 from edx/khalid/LEARNER-587
Browse files Browse the repository at this point in the history
Fix google plus signup
  • Loading branch information
miankhalid committed Apr 21, 2017
2 parents d08df5c + a5bc7be commit 8d8aead
Showing 1 changed file with 32 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -42,55 +43,30 @@ public void getUser(Callback<SocialMember> 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<GoogleUserProfile>(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<String> {
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;
}
}*/

}

0 comments on commit 8d8aead

Please sign in to comment.