Code example for ConnectionResult

Methods: getErrorCode, hasResolution, startResolutionForResult

0
    /** Handles a connection failure reported by a client. */ 
    @Override 
    public void onConnectionFailed(ConnectionResult result) {
        // save connection result for later reference 
        mConnectionResult = result;
        debugLog("onConnectionFailed: result " + result.getErrorCode());
        dismissDialog(); 
 
        if (!mUserInitiatedSignIn) {
            // If the user didn't initiate the sign-in, we don't try to resolve 
            // the connection problem automatically -- instead, we fail and wait 
            // for the user to want to sign in. That way, they won't get an 
            // authentication (or other) popup unless they are actively trying 
            // to 
            // sign in. 
            debugLog("onConnectionFailed: since user didn't initiate sign-in, failing now."); 
            mConnectionResult = result;
            if (mListener != null) {
                mListener.onSignInFailed();
            } 
            return; 
        } 
 
        debugLog("onConnectionFailed: since user initiated sign-in, trying to resolve problem."); 
 
        // Resolve the connection result. This usually means showing a dialog or 
        // starting an Activity that will allow the user to give the appropriate 
        // consents so that sign-in can be successful. 
        resolveConnectionResult(); 
    } 
 
    /** 
     * Attempts to resolve a connection failure. This will usually involve 
     * starting a UI flow that lets the user give the appropriate consents 
     * necessary for sign-in to work. 
     */ 
    void resolveConnectionResult() { 
        // Try to resolve the problem 
        debugLog("resolveConnectionResult: trying to resolve result: " + mConnectionResult);
        if (mConnectionResult.hasResolution()) {
            // This problem can be fixed. So let's try to fix it. 
            debugLog("result has resolution. Starting it."); 
            try { 
                // launch appropriate UI flow (which might, for example, be the 
                // sign-in flow) 
                mExpectingActivityResult = true;
                mConnectionResult.startResolutionForResult(mActivity, RC_RESOLVE);
            } catch (SendIntentException e) {
                // Try connecting again 
                debugLog("SendIntentException."); 
                connectCurrentClient(); 
            } 
        } else { 
            // It's not a problem what we can solve, so give up and show an 
            // error. 
            debugLog("resolveConnectionResult: result has no resolution. Giving up."); 
            giveUp(); 
        } 
    } 
 
    /** 
     * Give up on signing in due to an error. Shows the appropriate error 
     * message to the user, using a standard error dialog as appropriate to the 
     * cause of the error. That dialog will indicate to the user how the problem 
     * can be solved (for example, re-enable Google Play Services, upgrade to a 
     * new version, etc). 
     */ 
    void giveUp() { 
        mSignInError = true;
        mAutoSignIn = false;
        dismissDialog(); 
        debugLog("giveUp: giving up on connection. " + 
                ((mConnectionResult == null) ? "(no connection result)" :
                        ("Status code: " + mConnectionResult.getErrorCode())));
 
        Dialog errorDialog = null;
        if (mConnectionResult != null) {
            // get error dialog for that specific problem 
            errorDialog = getErrorDialog(mConnectionResult.getErrorCode());
            errorDialog.show();
            if (mListener != null) {
                mListener.onSignInFailed();
            } 
        } else {