Skip to content

Commit

Permalink
Added Profile Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumanta Paudel committed Feb 20, 2024
1 parent ae1ba18 commit fead73a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
20 changes: 14 additions & 6 deletions gpgs/src/gpgs_extension.cpp
Expand Up @@ -25,10 +25,11 @@ 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;
jmethodID m_getEmail;
};

struct GPGS_Disk
Expand Down Expand Up @@ -308,6 +309,11 @@ 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 @@ -764,6 +770,7 @@ static const luaL_reg Gpgs_methods[] =
{"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 @@ -885,6 +892,7 @@ static void InitJNIMethods(JNIEnv* env, jclass cls)
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 @@ -942,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, bool request_email)
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 @@ -952,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;ZZZZLjava/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, request_email, 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 @@ -971,11 +979,11 @@ 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);
dmLogInfo("Request Email: ", request_email)
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, request_email > 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
30 changes: 27 additions & 3 deletions gpgs/src/java/com/defold/gpgs/GpgsJNI.java
Expand Up @@ -102,6 +102,7 @@ public class GpgsJNI {
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 @@ -198,13 +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, boolean is_request_email, 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 @@ -257,15 +259,33 @@ private GoogleSignInOptions getSignInOptions() {
if (is_request_email) {
builder.requestEmail();
}

builder.requestProfile();

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 @@ -349,6 +369,10 @@ 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

0 comments on commit fead73a

Please sign in to comment.