Skip to content

Commit 65c9f13

Browse files
authored
Merge pull request #4 from calumma-robert/master
Fixes & Improvements
2 parents aea1435 + f7c807e commit 65c9f13

File tree

8 files changed

+73
-18
lines changed

8 files changed

+73
-18
lines changed

GoogleSignIn/Future.cs

+18-3
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ internal Future(FutureAPIImpl<T> impl) {
5555
/// Gets the status.
5656
/// </summary>
5757
/// <value>The status is set when Pending == false.</value>
58-
GoogleSignInStatusCode Status { get { return apiImpl.Status; } }
58+
public GoogleSignInStatusCode Status { get { return apiImpl.Status; } }
5959

6060
/// <summary>
6161
/// Gets the result.
6262
/// </summary>
6363
/// <value>The result is set when Pending == false and there is no error.
6464
/// </value>
65-
T Result { get { return apiImpl.Result; } }
65+
public T Result { get { return apiImpl.Result; } }
6666

6767
/// <summary>
6868
/// Waits for result then completes the TaskCompleationSource.
@@ -71,6 +71,21 @@ internal Future(FutureAPIImpl<T> impl) {
7171
/// <param name="tcs">Tcs.</param>
7272
internal IEnumerator WaitForResult(TaskCompletionSource<T> tcs) {
7373
yield return new WaitUntil(() => !Pending);
74+
yield return null;
75+
if (Status == GoogleSignInStatusCode.CANCELED) {
76+
tcs.SetCanceled();
77+
} else if (Status == GoogleSignInStatusCode.SUCCESS ||
78+
Status == GoogleSignInStatusCode.SUCCESS_CACHE) {
79+
tcs.SetResult(Result);
80+
} else {
81+
tcs.SetException(new GoogleSignIn.SignInException(Status));
82+
}
83+
}
84+
85+
internal async Task WaitForResultAsync(TaskCompletionSource<T> tcs)
86+
{
87+
while (Pending) await Task.Yield();
88+
await Task.Yield();
7489
if (Status == GoogleSignInStatusCode.CANCELED) {
7590
tcs.SetCanceled();
7691
} else if (Status == GoogleSignInStatusCode.SUCCESS ||
@@ -81,4 +96,4 @@ internal IEnumerator WaitForResult(TaskCompletionSource<T> tcs) {
8196
}
8297
}
8398
}
84-
}
99+
}

GoogleSignIn/GoogleSignIn.cs

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ public Task<GoogleSignInUser> SignIn() {
123123
return tcs.Task;
124124
}
125125

126+
public Task<GoogleSignInUser> SignInAsync() {
127+
var tcs = new TaskCompletionSource<GoogleSignInUser>();
128+
impl.SignIn().WaitForResultAsync(tcs);
129+
return tcs.Task;
130+
}
131+
132+
public Future<GoogleSignInUser> SignInFuture() => impl.SignIn();
133+
126134
/// <summary>Starts the silent authentication process.</summary>
127135
/// <remarks>
128136
/// The authenication process is started and will attempt to sign in without
@@ -136,6 +144,14 @@ public Task<GoogleSignInUser> SignInSilently() {
136144
return tcs.Task;
137145
}
138146

147+
public Task<GoogleSignInUser> SignInSilentlyAsync() {
148+
var tcs = new TaskCompletionSource<GoogleSignInUser>();
149+
impl.SignInSilently().WaitForResultAsync(tcs);
150+
return tcs.Task;
151+
}
152+
153+
public Future<GoogleSignInUser> SignInSilentlyFuture() => impl.SignInSilently();
154+
139155
/// <summary>
140156
/// Signs out the User.
141157
/// </summary>

GoogleSignIn/Impl/GoogleSignInImpl.cs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// <copyright file="GoogleSignInImpl.cs" company="Google Inc.">
1+
#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
2+
// <copyright file="GoogleSignInImpl.cs" company="Google Inc.">
23
// Copyright (C) 2017 Google Inc. All Rights Reserved.
34
//
45
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -141,7 +142,7 @@ public void OnResult(int result, AndroidJavaObject acct)
141142
Debug.Log("googlesignin.IListener : " + acct.Call<string>("toString"));
142143
Debug.Log("ID : " + acct.Call<string>("getId"));
143144
}
144-
else Debug.LogError("Should not get null account");
145+
else Debug.LogWarning("Should not get null account");
145146
}
146147
}
147148

@@ -334,11 +335,10 @@ public static class Ext
334335
public static AndroidJavaObject ToAndroidJavaObject(in this HandleRef self) => self.Handle.ToAndroidJavaObject();
335336
public static AndroidJavaObject ToAndroidJavaObject(in this IntPtr intPtr)
336337
{
337-
if(intPtr == IntPtr.Zero)
338+
if (intPtr == IntPtr.Zero)
338339
return null;
339340

340-
try
341-
{
341+
try {
342342
#if UNITY_2022_2_OR_NEWER
343343
return new AndroidJavaObject(intPtr);
344344
#else
@@ -348,13 +348,11 @@ public static AndroidJavaObject ToAndroidJavaObject(in this IntPtr intPtr)
348348
Debug.LogFormat("constructorInfo : {0}",constructorInfo);
349349
return constructorInfo.Invoke(new object[] { intPtr }) as AndroidJavaObject;
350350
#endif
351+
} catch (Exception e) {
352+
Debug.LogError("Exception creating AndroidJavaObject: " + e);
353+
return null;
351354
}
352-
catch(Exception e)
353-
{
354-
Debug.LogException(e);
355-
}
356-
357-
return null;
358355
}
359356
}
360357
}
358+
#endif

GoogleSignIn/Impl/GoogleSignInImplEditor.cs

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ static HttpListener BindLocalHostFirstAvailablePort()
6262
#if UNITY_EDITOR_WIN
6363
var listeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
6464
return Enumerable.Range(minPort, ushort.MaxValue - minPort).Where((i) => !listeners.Any((x) => x.Port == i)).Select((port) => {
65+
#elif UNITY_EDITOR_OSX
66+
return Enumerable.Range(minPort, ushort.MaxValue - minPort).Select((port) => {
6567
#else
6668
return Enumerable.Range(0,10).Select((i) => UnityEngine.Random.Range(minPort,ushort.MaxValue)).Select((port) => {
6769
#endif

GoogleSignIn/Impl/NativeFuture.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// <copyright file="NativeFuture.cs" company="Google Inc.">
1+
#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
2+
// <copyright file="NativeFuture.cs" company="Google Inc.">
23
// Copyright (C) 2017 Google Inc. All Rights Reserved.
34
//
45
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -87,3 +88,4 @@ public GoogleSignInStatusCode Status {
8788
}
8889
}
8990
}
91+
#endif

Plugins/Android/src/main/java/com/google/googlesignin/GoogleSignInFragment.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
7070
return;
7171
}
7272

73+
GoogleSignInFragment.this.tokenResult = new TokenResult(googleSignInResult.getSignInAccount(), googleSignInResult.getStatus().getStatusCode());
74+
7375
Status status = googleSignInResult.getStatus();
7476
GoogleSignInAccount acct = googleSignInResult.isSuccess() ? googleSignInResult.getSignInAccount() : null;
7577
if (acct == null) {
7678
GoogleSignInHelper.logError("Error with silentSignIn: " + status);
7779
}
7880

7981
GoogleSignInHelper.nativeOnResult(request.getHandle(),status != null ? status.getStatusCode() : CommonStatusCodes.INTERNAL_ERROR,acct);
82+
request.cancel();
8083
setState(State.READY);
8184
}
8285
});
@@ -122,6 +125,7 @@ public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
122125
}
123126

124127
public void disconnect() {
128+
this.tokenResult = null;
125129
this.tokenPendingResult = null;
126130
if (mGoogleApiClient != null) {
127131
mGoogleApiClient.disconnect();
@@ -156,16 +160,23 @@ public TokenRequest getRequest() {
156160
return request;
157161
}
158162

163+
private TokenResult tokenResult = null;
159164
private PendingResult<TokenResult> tokenPendingResult = null;
160165

161166
public GoogleSignInAccount getAccount() {
167+
if (tokenResult != null)
168+
return tokenResult.getAccount();
169+
162170
if(tokenPendingResult == null || tokenPendingResult.isCanceled())
163171
return null;
164172

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

168176
public int getStatus() {
177+
if (tokenResult != null)
178+
return tokenResult.getStatus().getStatusCode();
179+
169180
if(tokenPendingResult == null) {
170181
return CommonStatusCodes.DEVELOPER_ERROR;
171182
}
@@ -207,6 +218,7 @@ public static GoogleSignInFragment getInstance(Activity parentActivity) {
207218
public synchronized boolean submitRequest(TokenRequest request) {
208219
if (this.request == null || this.state == State.READY) {
209220
this.request = request;
221+
this.tokenResult = null;
210222
this.tokenPendingResult = request.getPendingResponse();
211223
return true;
212224
}
@@ -230,6 +242,7 @@ private synchronized void setState(State state) {
230242
*/
231243
public void signOut() {
232244
clearRequest(true);
245+
this.tokenResult = null;
233246
this.tokenPendingResult = null;
234247
if (mGoogleApiClient != null) {
235248
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
@@ -303,6 +316,7 @@ private void processRequest(final boolean silent) {
303316
request.getPendingResponse().setResultCallback(new ResultCallback<TokenResult>() {
304317
@Override
305318
public void onResult(@NonNull TokenResult tokenResult) {
319+
GoogleSignInFragment.this.tokenResult = tokenResult;
306320
GoogleSignInHelper.logDebug(
307321
String.format(
308322
Locale.getDefault(),
@@ -314,7 +328,8 @@ public void onResult(@NonNull TokenResult tokenResult) {
314328
tokenResult.getHandle(),
315329
tokenResult.getStatus().getStatusCode(),
316330
tokenResult.getAccount());
317-
clearRequest(false);
331+
request.cancel();
332+
setState(State.READY);
318333
}
319334
});
320335

Plugins/Android/src/main/java/com/google/googlesignin/TokenPendingResult.java

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.android.gms.common.api.ResultCallback;
2323
import java.util.concurrent.CountDownLatch;
2424
import java.util.concurrent.TimeUnit;
25+
import java.util.Locale;
2526

2627
/**
2728
* Pending result class for TokenResult. This allows the pending result to be returned to the
@@ -41,6 +42,12 @@ public TokenPendingResult(IListener requestHandle) {
4142
result = new TokenResult();
4243
result.setHandle(requestHandle);
4344
}
45+
46+
@Override
47+
public String toString() {
48+
return String.format(
49+
Locale.getDefault(), "Pending Result: %s", (result == null) ? "<null>" : result);
50+
}
4451

4552
@Override
4653
public TokenResult await() {

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.google.signin",
33
"displayName": "Google Signin",
4-
"version": "0.8.0",
4+
"version": "0.8.3",
55
"unity": "2021.3",
66
"description": "Google Signin for android, ios ,desktop",
77
"keywords": [
@@ -19,4 +19,4 @@
1919
"path": "Samples~/SignInSample"
2020
}
2121
]
22-
}
22+
}

0 commit comments

Comments
 (0)