35
35
import com .google .android .gms .common .api .PendingResult ;
36
36
import com .google .android .gms .common .api .ResultCallback ;
37
37
import com .google .android .gms .common .api .Scope ;
38
+ import com .google .android .gms .common .api .Status ;
38
39
import java .lang .reflect .Field ;
39
40
import java .lang .reflect .InvocationTargetException ;
40
41
import java .lang .reflect .Method ;
41
42
import java .util .Locale ;
43
+ import java .util .concurrent .TimeUnit ;
42
44
43
45
/**
44
46
* Activity fragment with no UI added to the parent activity in order to manage the accessing of the
@@ -62,15 +64,19 @@ void DoSignIn(boolean silent) {
62
64
Auth .GoogleSignInApi .silentSignIn (mGoogleApiClient ).setResultCallback (new ResultCallback <GoogleSignInResult >() {
63
65
@ Override
64
66
public void onResult (@ NonNull GoogleSignInResult googleSignInResult ) {
65
- if (!googleSignInResult .isSuccess ()) {
66
- GoogleSignInHelper .logError (
67
- "Error with " + "silentSignIn: " + googleSignInResult .getStatus ());
67
+ if (googleSignInResult == null ) {
68
+ GoogleSignInHelper .logError ("Error with silentSignIn: googleSignInResult is null" );
69
+ GoogleSignInHelper .nativeOnResult (request .getHandle (),CommonStatusCodes .INTERNAL_ERROR ,null );
70
+ return ;
68
71
}
69
72
70
- GoogleSignInHelper .nativeOnResult (
71
- request .getHandle (),
72
- googleSignInResult .getStatus ().getStatusCode (),
73
- googleSignInResult .getSignInAccount ());
73
+ Status status = googleSignInResult .getStatus ();
74
+ GoogleSignInAccount acct = googleSignInResult .isSuccess () ? googleSignInResult .getSignInAccount () : null ;
75
+ if (acct == null ) {
76
+ GoogleSignInHelper .logError ("Error with silentSignIn: " + status );
77
+ }
78
+
79
+ GoogleSignInHelper .nativeOnResult (request .getHandle (),status != null ? status .getStatusCode () : CommonStatusCodes .INTERNAL_ERROR ,acct );
74
80
setState (State .READY );
75
81
}
76
82
});
@@ -153,15 +159,22 @@ public TokenRequest getRequest() {
153
159
private PendingResult <TokenResult > tokenPendingResult = null ;
154
160
155
161
public GoogleSignInAccount getAccount () {
156
- return tokenPendingResult .await ().getAccount ();
162
+ if (tokenPendingResult == null || tokenPendingResult .isCanceled ())
163
+ return null ;
164
+
165
+ return tokenPendingResult .await (3 ,TimeUnit .SECONDS ).getAccount ();
157
166
}
158
167
159
168
public int getStatus () {
160
169
if (tokenPendingResult == null ) {
161
170
return CommonStatusCodes .DEVELOPER_ERROR ;
162
171
}
163
172
164
- return tokenPendingResult .await ().getStatus ().getStatusCode ();
173
+ if (tokenPendingResult .isCanceled ()) {
174
+ return CommonStatusCodes .CANCELED ;
175
+ }
176
+
177
+ return tokenPendingResult .await (3 ,TimeUnit .SECONDS ).getStatus ().getStatusCode ();
165
178
}
166
179
167
180
private GoogleApiClient mGoogleApiClient ;
@@ -287,34 +300,29 @@ private void processRequest(final boolean silent) {
287
300
}
288
301
289
302
setState (State .BUSY );
290
- request
291
- .getPendingResponse ()
292
- .setResultCallback (
293
- new ResultCallback <TokenResult >() {
294
- @ Override
295
- public void onResult (@ NonNull TokenResult tokenResult ) {
296
- GoogleSignInHelper .logDebug (
297
- String .format (
298
- Locale .getDefault (),
299
- "Calling nativeOnResult: handle: %s, status: %d acct: %s" ,
300
- tokenResult .getHandle (),
301
- tokenResult .getStatus ().getStatusCode (),
302
- tokenResult .getAccount ()));
303
- GoogleSignInHelper .nativeOnResult (
304
- tokenResult .getHandle (),
305
- tokenResult .getStatus ().getStatusCode (),
306
- tokenResult .getAccount ());
307
- clearRequest (false );
308
- }
309
- });
303
+ request .getPendingResponse ().setResultCallback (new ResultCallback <TokenResult >() {
304
+ @ Override
305
+ public void onResult (@ NonNull TokenResult tokenResult ) {
306
+ GoogleSignInHelper .logDebug (
307
+ String .format (
308
+ Locale .getDefault (),
309
+ "Calling nativeOnResult: handle: %s, status: %d acct: %s" ,
310
+ tokenResult .getHandle (),
311
+ tokenResult .getStatus ().getStatusCode (),
312
+ tokenResult .getAccount ()));
313
+ GoogleSignInHelper .nativeOnResult (
314
+ tokenResult .getHandle (),
315
+ tokenResult .getStatus ().getStatusCode (),
316
+ tokenResult .getAccount ());
317
+ clearRequest (false );
318
+ }
319
+ });
310
320
311
321
// Build the GoogleAPIClient
312
322
buildClient (request );
313
323
314
- GoogleSignInHelper .logDebug (
315
- " Has connected == " + mGoogleApiClient .hasConnectedApi (Auth .GOOGLE_SIGN_IN_API ));
324
+ GoogleSignInHelper .logDebug (" Has connected == " + mGoogleApiClient .hasConnectedApi (Auth .GOOGLE_SIGN_IN_API ));
316
325
if (!mGoogleApiClient .hasConnectedApi (Auth .GOOGLE_SIGN_IN_API )) {
317
-
318
326
DoSignIn (silent );
319
327
}
320
328
} catch (Throwable throwable ) {
0 commit comments