Code example for GamesClient

Methods: disconnect, isConnected, signOut

0
        mSignInError = false;
 
        if (mPlusClient != null && mPlusClient.isConnected()) {
            mPlusClient.clearDefaultAccount();
        } 
        if (mGamesClient != null && mGamesClient.isConnected()) {
            showProgressDialog(false); 
            mGamesClient.signOut(this);
        } 
 
        // kill connects to all clients but games, which must remain 
        // connected til we get onSignOutComplete() 
        killConnections(CLIENT_ALL & ~CLIENT_GAMES);
    } 
 
    /** 
     * Handle activity result. Call this method from your Activity's 
     * onActivityResult callback. If the activity result pertains to the sign-in 
     * process, processes it appropriately. 
     */ 
    public void onActivityResult(int requestCode, int responseCode, Intent intent) {
        if (requestCode == RC_RESOLVE) {
            // We're coming back from an activity that was launched to resolve a 
            // connection 
            // problem. For example, the sign-in UI. 
            mExpectingActivityResult = false;
            debugLog("onActivityResult, req " + requestCode + " response " + responseCode);
            if (responseCode == Activity.RESULT_OK) {
                // Ready to try to connect again. 
                debugLog("responseCode == RESULT_OK. So connecting."); 
                connectCurrentClient(); 
            } else if (responseCode == Activity.RESULT_CANCELED) {
                // User cancelled. 
                mAutoSignIn = false;
                mConnectionResult = null;
                mUserInitiatedSignIn = false;
                dismissDialog(); 
            } else { 
                // Whatever the problem we were trying to solve, it was not 
                // solved. 
                // So give up and show an error message. 
                debugLog("responseCode != RESULT_OK, so not reconnecting."); 
                giveUp(); 
            } 
        } 
    } 
 
    /** 
     * Starts a user-initiated sign-in flow. This should be called when the user 
     * clicks on a "Sign In" button. As a result, authentication/consent dialogs 
     * may show up. At the end of the process, the GameHelperListener's 
     * onSignInSucceeded() or onSignInFailed() methods will be called. 
     */ 
    public void beginUserInitiatedSignIn() { 
        if (mSignedIn)
            return; // nothing to do 
 
        // reset the flag to sign in automatically on onStart() -- now a 
        // wanted behavior 
        mAutoSignIn = true;
 
        // Is Google Play services available? 
        int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getContext());
        debugLog("isGooglePlayServicesAvailable returned " + result);
        if (result != ConnectionResult.SUCCESS) {
            // Nope. 
            debugLog("Google Play services not available. Show error dialog."); 
            Dialog errorDialog = getErrorDialog(result);
            errorDialog.show();
            if (mListener != null)
                mListener.onSignInFailed();
            return; 
        } 
 
        mUserInitiatedSignIn = true;
        if (mConnectionResult != null) {
            // We have a pending connection result from a previous failure, so 
            // start with that. 
            debugLog("beginUserInitiatedSignIn: continuing pending sign-in flow."); 
            showProgressDialog(true); 
            resolveConnectionResult(); 
        } else { 
            // We don't have a pending connection result, so start anew. 
            debugLog("beginUserInitiatedSignIn: starting new sign-in flow."); 
            startConnections(); 
        } 
    } 
 
    Context getContext() {
        return mActivity;
    } 
 
    void addToScope(StringBuilder scopeStringBuilder, String scope) {
        if (scopeStringBuilder.length() == 0) {
            scopeStringBuilder.append("oauth2:");
        } else { 
            scopeStringBuilder.append(" ");
        } 
        scopeStringBuilder.append(scope);
    } 
 
    void startConnections() { 
        mConnectedClients = CLIENT_NONE;
        mInvitationId = null;
        connectNextClient(); 
    } 
 
    void showProgressDialog(boolean signIn) {
        String message = signIn ? mSigningInMessage : mSigningOutMessage;
 
        if (mProgressDialog == null) {
            if (getContext() == null) 
                return; 
            mProgressDialog = new ProgressDialog(getContext());
        } 
 
        mProgressDialog.setMessage(message == null ? "" : message);
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.show();
    } 
 
    void dismissDialog() { 
        if (mProgressDialog != null)
            mProgressDialog.dismiss();
        mProgressDialog = null;
    } 
 
    void connectNextClient() { 
        // do we already have all the clients we need? 
        int pendingClients = mRequestedClients & ~mConnectedClients;
        if (pendingClients == 0) {
            debugLog("All clients now connected. Sign-in successful."); 
            succeedSignIn(); 
            return; 
        } 
 
        showProgressDialog(true); 
 
        // which client should be the next one to connect? 
        if (mGamesClient != null && (0 != (pendingClients & CLIENT_GAMES))) {
            debugLog("Connecting GamesClient."); 
            mClientCurrentlyConnecting = CLIENT_GAMES;
        } else if (mPlusClient != null && (0 != (pendingClients & CLIENT_PLUS))) {
            debugLog("Connecting PlusClient."); 
            mClientCurrentlyConnecting = CLIENT_PLUS;
        } else if (mAppStateClient != null && (0 != (pendingClients & CLIENT_APPSTATE))) {
            debugLog("Connecting AppStateClient."); 
            mClientCurrentlyConnecting = CLIENT_APPSTATE;
        } else { 
            throw new AssertionError("Not all clients connected, yet no one is next. R="
                    + mRequestedClients + ", C=" + mConnectedClients);
        } 
 
        connectCurrentClient(); 
    } 
 
    void connectCurrentClient() { 
        switch (mClientCurrentlyConnecting) {
            case CLIENT_GAMES:
                mGamesClient.connect();
                break; 
            case CLIENT_APPSTATE:
                mAppStateClient.connect();
                break; 
            case CLIENT_PLUS:
                mPlusClient.connect();
                break; 
        } 
    } 
 
    void killConnections(int whatClients) {
        if ((whatClients & CLIENT_GAMES) != 0 && mGamesClient != null
                && mGamesClient.isConnected()) {
            mConnectedClients &= ~CLIENT_GAMES;
            mGamesClient.disconnect();
        } 
        if ((whatClients & CLIENT_PLUS) != 0 && mPlusClient != null
                && mPlusClient.isConnected()) {
            mConnectedClients &= ~CLIENT_PLUS;
            mPlusClient.disconnect();