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

Fixes & Improvements #4

Merged
merged 7 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions GoogleSignIn/Future.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ internal Future(FutureAPIImpl<T> impl) {
/// Gets the status.
/// </summary>
/// <value>The status is set when Pending == false.</value>
GoogleSignInStatusCode Status { get { return apiImpl.Status; } }
public GoogleSignInStatusCode Status { get { return apiImpl.Status; } }

/// <summary>
/// Gets the result.
/// </summary>
/// <value>The result is set when Pending == false and there is no error.
/// </value>
T Result { get { return apiImpl.Result; } }
public T Result { get { return apiImpl.Result; } }

/// <summary>
/// Waits for result then completes the TaskCompleationSource.
Expand All @@ -71,6 +71,21 @@ internal Future(FutureAPIImpl<T> impl) {
/// <param name="tcs">Tcs.</param>
internal IEnumerator WaitForResult(TaskCompletionSource<T> tcs) {
yield return new WaitUntil(() => !Pending);
yield return null;
if (Status == GoogleSignInStatusCode.CANCELED) {
tcs.SetCanceled();
} else if (Status == GoogleSignInStatusCode.SUCCESS ||
Status == GoogleSignInStatusCode.SUCCESS_CACHE) {
tcs.SetResult(Result);
} else {
tcs.SetException(new GoogleSignIn.SignInException(Status));
}
}

internal async Task WaitForResultAsync(TaskCompletionSource<T> tcs)
{
while (Pending) await Task.Yield();
await Task.Yield();
if (Status == GoogleSignInStatusCode.CANCELED) {
tcs.SetCanceled();
} else if (Status == GoogleSignInStatusCode.SUCCESS ||
Expand All @@ -81,4 +96,4 @@ internal IEnumerator WaitForResult(TaskCompletionSource<T> tcs) {
}
}
}
}
}
16 changes: 16 additions & 0 deletions GoogleSignIn/GoogleSignIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public Task<GoogleSignInUser> SignIn() {
return tcs.Task;
}

public Task<GoogleSignInUser> SignInAsync() {
var tcs = new TaskCompletionSource<GoogleSignInUser>();
impl.SignIn().WaitForResultAsync(tcs);
return tcs.Task;
}

public Future<GoogleSignInUser> SignInFuture() => impl.SignIn();

/// <summary>Starts the silent authentication process.</summary>
/// <remarks>
/// The authenication process is started and will attempt to sign in without
Expand All @@ -136,6 +144,14 @@ public Task<GoogleSignInUser> SignInSilently() {
return tcs.Task;
}

public Task<GoogleSignInUser> SignInSilentlyAsync() {
var tcs = new TaskCompletionSource<GoogleSignInUser>();
impl.SignInSilently().WaitForResultAsync(tcs);
return tcs.Task;
}

public Future<GoogleSignInUser> SignInSilentlyFuture() => impl.SignInSilently();

/// <summary>
/// Signs out the User.
/// </summary>
Expand Down
20 changes: 9 additions & 11 deletions GoogleSignIn/Impl/GoogleSignInImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// <copyright file="GoogleSignInImpl.cs" company="Google Inc.">
#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
// <copyright file="GoogleSignInImpl.cs" company="Google Inc.">
// Copyright (C) 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -141,7 +142,7 @@ public void OnResult(int result, AndroidJavaObject acct)
Debug.Log("googlesignin.IListener : " + acct.Call<string>("toString"));
Debug.Log("ID : " + acct.Call<string>("getId"));
}
else Debug.LogError("Should not get null account");
else Debug.LogWarning("Should not get null account");
}
}

Expand Down Expand Up @@ -334,11 +335,10 @@ public static class Ext
public static AndroidJavaObject ToAndroidJavaObject(in this HandleRef self) => self.Handle.ToAndroidJavaObject();
public static AndroidJavaObject ToAndroidJavaObject(in this IntPtr intPtr)
{
if(intPtr == IntPtr.Zero)
if (intPtr == IntPtr.Zero)
return null;

try
{
try {
#if UNITY_2022_2_OR_NEWER
return new AndroidJavaObject(intPtr);
#else
Expand All @@ -348,13 +348,11 @@ public static AndroidJavaObject ToAndroidJavaObject(in this IntPtr intPtr)
Debug.LogFormat("constructorInfo : {0}",constructorInfo);
return constructorInfo.Invoke(new object[] { intPtr }) as AndroidJavaObject;
#endif
} catch (Exception e) {
Debug.LogError("Exception creating AndroidJavaObject: " + e);
return null;
}
catch(Exception e)
{
Debug.LogException(e);
}

return null;
}
}
}
#endif
2 changes: 2 additions & 0 deletions GoogleSignIn/Impl/GoogleSignInImplEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ static HttpListener BindLocalHostFirstAvailablePort()
#if UNITY_EDITOR_WIN
var listeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
return Enumerable.Range(minPort, ushort.MaxValue - minPort).Where((i) => !listeners.Any((x) => x.Port == i)).Select((port) => {
#elif UNITY_EDITOR_OSX
return Enumerable.Range(minPort, ushort.MaxValue - minPort).Select((port) => {
#else
return Enumerable.Range(0,10).Select((i) => UnityEngine.Random.Range(minPort,ushort.MaxValue)).Select((port) => {
#endif
Expand Down
4 changes: 3 additions & 1 deletion GoogleSignIn/Impl/NativeFuture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// <copyright file="NativeFuture.cs" company="Google Inc.">
#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
// <copyright file="NativeFuture.cs" company="Google Inc.">
// Copyright (C) 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -87,3 +88,4 @@ public GoogleSignInStatusCode Status {
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
return;
}

GoogleSignInFragment.this.tokenResult = new TokenResult(googleSignInResult.getSignInAccount(), googleSignInResult.getStatus().getStatusCode());

Status status = googleSignInResult.getStatus();
GoogleSignInAccount acct = googleSignInResult.isSuccess() ? googleSignInResult.getSignInAccount() : null;
if (acct == null) {
GoogleSignInHelper.logError("Error with silentSignIn: " + status);
}

GoogleSignInHelper.nativeOnResult(request.getHandle(),status != null ? status.getStatusCode() : CommonStatusCodes.INTERNAL_ERROR,acct);
request.cancel();
setState(State.READY);
}
});
Expand Down Expand Up @@ -122,6 +125,7 @@ public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}

public void disconnect() {
this.tokenResult = null;
this.tokenPendingResult = null;
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
Expand Down Expand Up @@ -156,16 +160,23 @@ public TokenRequest getRequest() {
return request;
}

private TokenResult tokenResult = null;
private PendingResult<TokenResult> tokenPendingResult = null;

public GoogleSignInAccount getAccount() {
if (tokenResult != null)
return tokenResult.getAccount();

if(tokenPendingResult == null || tokenPendingResult.isCanceled())
return null;

return tokenPendingResult.await(3,TimeUnit.SECONDS).getAccount();
}

public int getStatus() {
if (tokenResult != null)
return tokenResult.getStatus().getStatusCode();

if(tokenPendingResult == null) {
return CommonStatusCodes.DEVELOPER_ERROR;
}
Expand Down Expand Up @@ -207,6 +218,7 @@ public static GoogleSignInFragment getInstance(Activity parentActivity) {
public synchronized boolean submitRequest(TokenRequest request) {
if (this.request == null || this.state == State.READY) {
this.request = request;
this.tokenResult = null;
this.tokenPendingResult = request.getPendingResponse();
return true;
}
Expand All @@ -230,6 +242,7 @@ private synchronized void setState(State state) {
*/
public void signOut() {
clearRequest(true);
this.tokenResult = null;
this.tokenPendingResult = null;
if (mGoogleApiClient != null) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
Expand Down Expand Up @@ -303,6 +316,7 @@ private void processRequest(final boolean silent) {
request.getPendingResponse().setResultCallback(new ResultCallback<TokenResult>() {
@Override
public void onResult(@NonNull TokenResult tokenResult) {
GoogleSignInFragment.this.tokenResult = tokenResult;
GoogleSignInHelper.logDebug(
String.format(
Locale.getDefault(),
Expand All @@ -314,7 +328,8 @@ public void onResult(@NonNull TokenResult tokenResult) {
tokenResult.getHandle(),
tokenResult.getStatus().getStatusCode(),
tokenResult.getAccount());
clearRequest(false);
request.cancel();
setState(State.READY);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.android.gms.common.api.ResultCallback;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.Locale;

/**
* Pending result class for TokenResult. This allows the pending result to be returned to the
Expand All @@ -41,6 +42,12 @@ public TokenPendingResult(IListener requestHandle) {
result = new TokenResult();
result.setHandle(requestHandle);
}

@Override
public String toString() {
return String.format(
Locale.getDefault(), "Pending Result: %s", (result == null) ? "<null>" : result);
}

@Override
public TokenResult await() {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.google.signin",
"displayName": "Google Signin",
"version": "0.8.0",
"version": "0.8.3",
"unity": "2021.3",
"description": "Google Signin for android, ios ,desktop",
"keywords": [
Expand All @@ -19,4 +19,4 @@
"path": "Samples~/SignInSample"
}
]
}
}