Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Email and Profile Scope #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions gpgs/src/gpgs_extension.cpp
Expand Up @@ -25,6 +25,8 @@ struct GPGS
jmethodID m_getId;
jmethodID m_getIdToken;
jmethodID m_getServerAuthCode;
jmethodID m_getEmail;
jmethodID m_getProfile;
jmethodID m_isLoggedIn;
jmethodID m_setGravityForPopups;
jmethodID m_isSupported;
Expand Down Expand Up @@ -302,6 +304,16 @@ static int GpgsAuth_getId(lua_State* L)
return CallStringMethod(L, g_gpgs.m_GpgsJNI, g_gpgs.m_getId);
}

static int GpgsAuth_getEmail(lua_State* L)
{
return CallStringMethod(L, g_gpgs.m_GpgsJNI,g_gpgs.m_getEmail);
}

static int GpgsAuth_getProfile(lua_State* L)
{
return CallStringMethod(L, g_gpgs.m_GpgsJNI,g_gpgs.m_getProfile);
}

static int GpgsAuth_getIdToken(lua_State* L)
{
return CallStringMethod(L, g_gpgs.m_GpgsJNI, g_gpgs.m_getIdToken);
Expand Down Expand Up @@ -757,6 +769,8 @@ static const luaL_reg Gpgs_methods[] =
{"get_id", GpgsAuth_getId},
{"get_id_token", GpgsAuth_getIdToken},
{"get_server_auth_code", GpgsAuth_getServerAuthCode},
{"get_email", GpgsAuth_getEmail},
{"get_profile", GpgsAuth_getProfile},
{"is_logged_in", GpgsAuth_isLoggedIn},
{"set_popup_position", GpgsAuth_setGravityForPopups},
{"set_callback", Gpg_set_callback},
Expand Down Expand Up @@ -877,6 +891,8 @@ static void InitJNIMethods(JNIEnv* env, jclass cls)
g_gpgs.m_getId = env->GetMethodID(cls, "getId", "()Ljava/lang/String;");
g_gpgs.m_getIdToken = env->GetMethodID(cls, "getIdToken", "()Ljava/lang/String;");
g_gpgs.m_getServerAuthCode = env->GetMethodID(cls, "getServerAuthCode", "()Ljava/lang/String;");
g_gpgs.m_getEmail = env->GetMethodID(cls,"getEmail","()Ljava/lang/String;");
g_gpgs.m_getProfile = env->GetMethodID(cls,"getProfile","()Ljava/lang/String;");
g_gpgs.m_setGravityForPopups = env->GetMethodID(cls, "setGravityForPopups", "(I)V");

//disk
Expand Down Expand Up @@ -934,7 +950,7 @@ static void CheckInitializationParams(const char* client_id, bool request_server
}


static void InitializeJNI(const char* client_id, bool request_server_auth_code, bool request_id_token)
static void InitializeJNI(const char* client_id, bool request_server_auth_code, bool request_id_token, bool request_email, bool request_profile)
{
CheckInitializationParams(client_id, request_server_auth_code > 0, request_id_token > 0);

Expand All @@ -944,11 +960,11 @@ static void InitializeJNI(const char* client_id, bool request_server_auth_code,

InitJNIMethods(env, cls);

jmethodID jni_constructor = env->GetMethodID(cls, "<init>", "(Landroid/app/Activity;ZZZLjava/lang/String;)V");
jmethodID jni_constructor = env->GetMethodID(cls, "<init>", "(Landroid/app/Activity;ZZZZZLjava/lang/String;)V");
jstring java_client_id = env->NewStringUTF(client_id);

g_gpgs.m_GpgsJNI = env->NewGlobalRef(env->NewObject(cls, jni_constructor, threadAttacher.GetActivity()->clazz,
g_gpgs_disk.is_using, request_server_auth_code, request_id_token, java_client_id));
g_gpgs_disk.is_using, request_server_auth_code, request_id_token, request_email, request_profile, java_client_id));

env->DeleteLocalRef(java_client_id);
}
Expand All @@ -962,10 +978,12 @@ static dmExtension::Result InitializeGpgs(dmExtension::Params* params)

int request_server_auth_code = dmConfigFile::GetInt(params->m_ConfigFile, "gpgs.request_server_auth_code", 0);
int request_id_token = dmConfigFile::GetInt(params->m_ConfigFile, "gpgs.request_id_token", 0);
int request_email = dmConfigFile::GetInt(params->m_ConfigFile, "gpgs.request_email", 0);
int request_profile = dmConfigFile::GetInt(params->m_ConfigFile, "gpgs.request_profile", 0);

const char* client_id = dmConfigFile::GetString(params->m_ConfigFile, "gpgs.client_id", 0);

InitializeJNI(client_id, request_server_auth_code > 0, request_id_token > 0);
InitializeJNI(client_id, request_server_auth_code > 0, request_id_token > 0, request_email > 0, request_profile > 0);
dmAndroid::RegisterOnActivityResultListener(OnActivityResult);
gpgs_callback_initialize();
return dmExtension::RESULT_OK;
Expand Down
38 changes: 37 additions & 1 deletion gpgs/src/java/com/defold/gpgs/GpgsJNI.java
Expand Up @@ -101,6 +101,8 @@ public class GpgsJNI {
private String client_id;
private boolean is_request_id_token;
private boolean is_request_auth_code;
private boolean is_request_email;
private boolean is_request_profile;
private boolean is_supported;

//--------------------------------------------------
Expand Down Expand Up @@ -197,12 +199,14 @@ private void sendFailedMessage(int msg, String error_text, Exception e) {
}
}

public GpgsJNI(Activity activity, boolean is_disk_active, boolean is_request_auth_code, boolean is_request_id_token, String client_id) {
public GpgsJNI(Activity activity, boolean is_disk_active, boolean is_request_auth_code, boolean is_request_id_token, boolean is_request_email, boolean is_request_profile, String client_id) {
this.activity = activity;
this.is_disk_active = is_disk_active;
this.client_id = client_id;
this.is_request_auth_code = is_request_auth_code;
this.is_request_id_token = is_request_id_token;
this.is_request_email = is_request_email;
this.is_request_profile = is_request_profile;

this.is_supported = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity) == ConnectionResult.SUCCESS;

Expand Down Expand Up @@ -252,12 +256,36 @@ private GoogleSignInOptions getSignInOptions() {
builder.requestServerAuthCode(client_id);
}

if (is_request_email) {
builder.requestEmail();
}

if (is_request_profile)
{
builder.requestProfile();
}

mSignInOptions = builder.build();
}

return mSignInOptions;
}

private String profileToJsonString() throws JSONException {
JSONObject profile = new JSONObject();

profile.put("name",mSignedInAccount.getDisplayName());
profile.put("given_name",mSignedInAccount.getGivenName());
profile.put("family_name",mSignedInAccount.getFamilyName());
profile.put("photo_url",mSignedInAccount.getPhotoUrl());
profile.put("id",mSignedInAccount.getId());

String profileText = profile.toString();

return profileText;

}

public void activityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (intent != null) {
Expand Down Expand Up @@ -337,6 +365,14 @@ public String getId() {
return isLoggedIn() ? mPlayer.getPlayerId() : null;
}

public String getEmail() {
return isLoggedIn() ? mSignedInAccount.getEmail() : null;
}

public String getProfile() throws JSONException{
return isLoggedIn() ? profileToJsonString() : null;
}

public String getIdToken() {
return isLoggedIn() ? mSignedInAccount.getIdToken() : null;
}
Expand Down