3535import com .google .android .gms .common .api .PendingResult ;
3636import com .google .android .gms .common .api .ResultCallback ;
3737import com .google .android .gms .common .api .Scope ;
38+ import com .google .android .gms .common .api .Status ;
3839import java .lang .reflect .Field ;
3940import java .lang .reflect .InvocationTargetException ;
4041import java .lang .reflect .Method ;
4142import java .util .Locale ;
43+ import java .util .concurrent .TimeUnit ;
4244
4345/**
4446 * 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) {
6264 Auth .GoogleSignInApi .silentSignIn (mGoogleApiClient ).setResultCallback (new ResultCallback <GoogleSignInResult >() {
6365 @ Override
6466 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 ;
6871 }
6972
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 );
7480 setState (State .READY );
7581 }
7682 });
@@ -153,15 +159,22 @@ public TokenRequest getRequest() {
153159 private PendingResult <TokenResult > tokenPendingResult = null ;
154160
155161 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 ();
157166 }
158167
159168 public int getStatus () {
160169 if (tokenPendingResult == null ) {
161170 return CommonStatusCodes .DEVELOPER_ERROR ;
162171 }
163172
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 ();
165178 }
166179
167180 private GoogleApiClient mGoogleApiClient ;
@@ -287,34 +300,29 @@ private void processRequest(final boolean silent) {
287300 }
288301
289302 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+ });
310320
311321 // Build the GoogleAPIClient
312322 buildClient (request );
313323
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 ));
316325 if (!mGoogleApiClient .hasConnectedApi (Auth .GOOGLE_SIGN_IN_API )) {
317-
318326 DoSignIn (silent );
319327 }
320328 } catch (Throwable throwable ) {
0 commit comments