com.google.android.gms.games.multiplayer.Multiplayer Java Examples

The following examples show how to use com.google.android.gms.games.multiplayer.Multiplayer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SkeletonActivity.java    From android-basic-samples with Apache License 2.0 7 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

  if (requestCode == RC_SIGN_IN) {

    Task<GoogleSignInAccount> task =
        GoogleSignIn.getSignedInAccountFromIntent(intent);

    try {
      GoogleSignInAccount account = task.getResult(ApiException.class);
      onConnected(account);
    } catch (ApiException apiException) {
      String message = apiException.getMessage();
      if (message == null || message.isEmpty()) {
        message = getString(R.string.signin_other_error);
      }

      onDisconnected();

      new AlertDialog.Builder(this)
          .setMessage(message)
          .setNeutralButton(android.R.string.ok, null)
          .show();
    }
  } else if (requestCode == RC_LOOK_AT_MATCHES) {
    // Returning from the 'Select Match' dialog

    if (resultCode != Activity.RESULT_OK) {
      logBadActivityResult(requestCode, resultCode,
          "User cancelled returning from the 'Select Match' dialog.");
      return;
    }

    TurnBasedMatch match = intent
        .getParcelableExtra(Multiplayer.EXTRA_TURN_BASED_MATCH);

    if (match != null) {
      updateMatch(match);
    }

    Log.d(TAG, "Match = " + match);
  } else if (requestCode == RC_SELECT_PLAYERS) {
    // Returning from 'Select players to Invite' dialog

    if (resultCode != Activity.RESULT_OK) {
      // user canceled
      logBadActivityResult(requestCode, resultCode,
          "User cancelled returning from 'Select players to Invite' dialog");
      return;
    }

    // get the invitee list
    ArrayList<String> invitees = intent
        .getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);

    // get automatch criteria
    Bundle autoMatchCriteria;

    int minAutoMatchPlayers = intent.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
    int maxAutoMatchPlayers = intent.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);

    if (minAutoMatchPlayers > 0) {
      autoMatchCriteria = RoomConfig.createAutoMatchCriteria(minAutoMatchPlayers,
          maxAutoMatchPlayers, 0);
    } else {
      autoMatchCriteria = null;
    }

    TurnBasedMatchConfig tbmc = TurnBasedMatchConfig.builder()
        .addInvitedPlayers(invitees)
        .setAutoMatchCriteria(autoMatchCriteria).build();

    // Start the match
    mTurnBasedMultiplayerClient.createMatch(tbmc)
        .addOnSuccessListener(new OnSuccessListener<TurnBasedMatch>() {
          @Override
          public void onSuccess(TurnBasedMatch turnBasedMatch) {
            onInitiateMatch(turnBasedMatch);
          }
        })
        .addOnFailureListener(createFailureListener("There was a problem creating a match!"));
    showSpinner();
  }
}
 
Example #2
Source File: GameHelper.java    From ANE-Google-Play-Game-Services with Apache License 2.0 6 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #3
Source File: DrawingActivity.java    From 8bitartist with Apache License 2.0 6 votes vote down vote up
@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "onConnected: sign-in successful.");

    // This is *NOT* required; if you do not register a handler for
    // invitation events, you will get standard notifications instead.
    // Standard notifications may be preferable behavior in many cases.
    Games.Invitations.registerInvitationListener(mGoogleApiClient, this);

    // Get invitation from Bundle
    if (bundle != null) {
        Invitation invitation = bundle.getParcelable(Multiplayer.EXTRA_INVITATION);
        if (invitation != null) {
            onInvitationReceived(invitation);
        }
    }

    updateViewVisibility();
}
 
Example #4
Source File: GameHelper.java    From google-play-game-services-ane with MIT License 6 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint.getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #5
Source File: Main.java    From chess with Apache License 2.0 6 votes vote down vote up
@Override
public void onConnected(final Bundle bundle) {
    if (BuildConfig.DEBUG) Logger.log("Main.onConnected");
    TurnBasedMatch match;
    if (bundle != null &&
            (match = bundle.getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH)) != null) {
        if (gameFragment == null || !gameFragment.isVisible()) {
            if (Game.load(match.getData(), new Match(match), mGoogleApiClient)) {
                startGame(match.getMatchId());
            } else {
                updateApp();
            }
        }
    } else {
        if (startFragment != null && startFragment.isVisible()) startFragment.loadMatches();
    }
    Games.TurnBasedMultiplayer.registerMatchUpdateListener(mGoogleApiClient, this);
}
 
Example #6
Source File: GameHelper.java    From ColorPhun with Apache License 2.0 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #7
Source File: MainActivity.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
private void handleInvitationInboxResult(int response, Intent data) {
  if (response != Activity.RESULT_OK) {
    Log.w(TAG, "*** invitation inbox UI cancelled, " + response);
    switchToMainScreen();
    return;
  }

  Log.d(TAG, "Invitation inbox UI succeeded.");
  Invitation invitation = data.getExtras().getParcelable(Multiplayer.EXTRA_INVITATION);

  // accept invitation
  if (invitation != null) {
    acceptInviteToRoom(invitation.getInvitationId());
  }
}
 
Example #8
Source File: MainActivity.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
private void handleSelectPlayersResult(int response, Intent data) {
  if (response != Activity.RESULT_OK) {
    Log.w(TAG, "*** select players UI cancelled, " + response);
    switchToMainScreen();
    return;
  }

  Log.d(TAG, "Select players UI succeeded.");

  // get the invitee list
  final ArrayList<String> invitees = data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
  Log.d(TAG, "Invitee count: " + invitees.size());

  // get the automatch criteria
  Bundle autoMatchCriteria = null;
  int minAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
  int maxAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
  if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0) {
    autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
        minAutoMatchPlayers, maxAutoMatchPlayers, 0);
    Log.d(TAG, "Automatch criteria: " + autoMatchCriteria);
  }

  // create the room
  Log.d(TAG, "Creating room...");
  switchToScreen(R.id.screen_wait);
  keepScreenOn();
  resetGameVars();

  mRoomConfig = RoomConfig.builder(mRoomUpdateCallback)
      .addPlayersToInvite(invitees)
      .setOnMessageReceivedListener(mOnRealTimeMessageReceivedListener)
      .setRoomStatusUpdateCallback(mRoomStatusUpdateCallback)
      .setAutoMatchCriteria(autoMatchCriteria).build();
  mRealTimeMultiplayerClient.create(mRoomConfig);
  Log.d(TAG, "Room created, waiting for it to be ready...");
}
 
Example #9
Source File: GameHelper.java    From cordova-google-play-games-services with MIT License 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #10
Source File: GameHelper.java    From FlappyCow with MIT License 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #11
Source File: GameHelper.java    From tedroid with Apache License 2.0 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #12
Source File: GameHelper.java    From Onesearch with MIT License 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #13
Source File: GameHelper.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #14
Source File: GameHelper.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #15
Source File: GameHelper.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
/**
 * Called when we successfully obtain a connection to a client.
 */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #16
Source File: GameHelper.java    From martianrun with Apache License 2.0 5 votes vote down vote up
/**
 * Called when we successfully obtain a connection to a client.
 */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #17
Source File: GameHelper.java    From FixMath with Apache License 2.0 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #18
Source File: GameHelper.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #19
Source File: RealTimeMultiplayer.java    From godot-gpgs with MIT License 5 votes vote down vote up
private void handleInvitationInboxResult(int response, Intent data) {
    if (response != Activity.RESULT_OK) {
        Log.w(TAG, "*** invitation inbox UI cancelled, " + response);
        GodotLib.calldeferred(instanceId, "_on_gpgs_rtm_invitation_cancelled", new Object[] { });
        return;
    }

    Log.d(TAG, "GPGS: Invitation inbox UI succeeded.");
    Invitation inv = data.getExtras().getParcelable(Multiplayer.EXTRA_INVITATION);

    // accept invitation
    acceptInviteToRoom(inv.getInvitationId());
}
 
Example #20
Source File: GameHelper.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
/** Called when we successfully obtain a connection to a client. */
@Override
public void onConnected(Bundle connectionHint) {
    debugLog("onConnected: connected!");

    if (connectionHint != null) {
        debugLog("onConnected: connection hint provided. Checking for invite.");
        Invitation inv = connectionHint
                .getParcelable(Multiplayer.EXTRA_INVITATION);
        if (inv != null && inv.getInvitationId() != null) {
            // retrieve and cache the invitation ID
            debugLog("onConnected: connection hint has a room invite!");
            mInvitation = inv;
            debugLog("Invitation ID: " + mInvitation.getInvitationId());
        }

        // Do we have any requests pending?
        mRequests = Games.Requests
                .getGameRequestsFromBundle(connectionHint);
        if (!mRequests.isEmpty()) {
            // We have requests in onConnected's connectionHint.
            debugLog("onConnected: connection hint has " + mRequests.size()
                    + " request(s)");
        }

        debugLog("onConnected: connection hint provided. Checking for TBMP game.");
        mTurnBasedMatch = connectionHint
                .getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);
    }

    // we're good to go
    succeedSignIn();
}
 
Example #21
Source File: Main.java    From chess with Apache License 2.0 4 votes vote down vote up
@Override
public void onActivityResult(int request, int response, final Intent data) {
    super.onActivityResult(request, response, data);
    if (BuildConfig.DEBUG) Logger.log("Main onActivityResult");
    if (response == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED &&
            !mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
        mGoogleApiClient.connect();
    } else if (response == Activity.RESULT_CANCELED) {
        // User cancelled.
        mGoogleApiClient.disconnect();
    } else if (response == Activity.RESULT_OK) {
        if (request == RC_SELECT_PLAYERS) {
            // get the invitee list
            final ArrayList<String> invitees =
                    data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);

            // get auto-match criteria
            Bundle autoMatchCriteria;
            int minAutoMatchPlayers =
                    data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
            int maxAutoMatchPlayers =
                    data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
            if (minAutoMatchPlayers > 0) {
                autoMatchCriteria = RoomConfig
                        .createAutoMatchCriteria(minAutoMatchPlayers, maxAutoMatchPlayers, 0);
            } else {
                autoMatchCriteria = null;
            }

            TurnBasedMatchConfig tbmc =
                    TurnBasedMatchConfig.builder().addInvitedPlayers(invitees)
                            .setAutoMatchCriteria(autoMatchCriteria)
                            .setVariant(StartFragment.LAST_SELECTED_MATCH_MODE).build();

            // kick the match off
            Games.TurnBasedMultiplayer.createMatch(mGoogleApiClient, tbmc).setResultCallback(
                    new ResultCallback<TurnBasedMultiplayer.InitiateMatchResult>() {
                        @Override
                        public void onResult(final TurnBasedMultiplayer.InitiateMatchResult result) {
                            if (BuildConfig.DEBUG) Logger.log(
                                    "InitiateMatchResult onResult " + result.getStatus());
                            // Check if the status code is not success;
                            if (result.getStatus().getStatusCode() !=
                                    GamesStatusCodes.STATUS_OK) {
                                return;
                            }
                            TurnBasedMatch match = result.getMatch();
                            if (match.getData() == null) {
                                Game.newGame(new Match(match), mGoogleApiClient);
                            } else {
                                if (!Game.load(match.getData(), new Match(match),
                                        mGoogleApiClient)) {
                                    updateApp();
                                    return;
                                }
                            }
                            startGame(match.getMatchId());
                        }
                    });
        } else if (request == RC_RESOLVE) {
            // We're coming back from an activity that was launched to resolve a
            // connection problem. For example, the sign-in UI.
            if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
                // Ready to try to connect again.
                mGoogleApiClient.connect();
            }
        }
    }
}
 
Example #22
Source File: DrawingActivity.java    From 8bitartist with Apache License 2.0 4 votes vote down vote up
@Override
public void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);
    Log.i(TAG, "onActivityResult: code = " + request + ", response = " + response);

    // Coming back from resolving a sign-in request
    if (request == RC_SIGN_IN) {
        mSignInClicked = false;
        mResolvingConnectionFailure = false;
        if (response == RESULT_OK) {
            mGoogleApiClient.connect();
        } else {
            BaseGameUtils.showActivityResultError(this, request, response,
                    R.string.sign_in_failed);
        }
    }

    // Coming back from a RealTime Multiplayer waiting room
    if (request == RC_WAITING_ROOM) {
        dismissSpinner();

        Room room = data.getParcelableExtra(Multiplayer.EXTRA_ROOM);
        if (response == RESULT_OK) {
            Log.d(TAG, "Waiting Room: Success");
            mRoom = room;
            startMatch();
        } else if (response == RESULT_CANCELED) {
            Log.d(TAG, "Waiting Room: Canceled");
            leaveRoom();
        } else if (response == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
            Log.d(TAG, "Waiting Room: Left Room");
            leaveRoom();
        } else if (response == GamesActivityResultCodes.RESULT_INVALID_ROOM) {
            Log.d(TAG, "Waiting Room: Invalid Room");
            leaveRoom();
        }
    }

    // We are coming back from the player selection UI, in preparation to start a match.
    if (request == RC_SELECT_PLAYERS) {
        if (response != Activity.RESULT_OK) {
            // user canceled
            Log.d(TAG, "onActivityResult: user canceled player selection.");
            return;
        }

        // Create a basic room configuration
        RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(this)
                .setMessageReceivedListener(this)
                .setRoomStatusUpdateListener(this);

        // Set the auto match criteria
        int minAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
        int maxAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
        if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0) {
            Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
                    minAutoMatchPlayers, maxAutoMatchPlayers, 0);
            roomConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
        }

        // Set the invitees
        final ArrayList<String> invitees = data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
        if (invitees != null && invitees.size() > 0) {
            roomConfigBuilder.addPlayersToInvite(invitees);
        }

        // Build the room and start the match
        showSpinner();
        Games.RealTimeMultiplayer.create(mGoogleApiClient, roomConfigBuilder.build());
    }
}
 
Example #23
Source File: MainActivity.java    From android-basic-samples with Apache License 2.0 4 votes vote down vote up
private void onConnected(GoogleSignInAccount googleSignInAccount) {
  Log.d(TAG, "onConnected(): connected to Google APIs");
  if (mSignedInAccount != googleSignInAccount) {

    mSignedInAccount = googleSignInAccount;

    // update the clients
    mRealTimeMultiplayerClient = Games.getRealTimeMultiplayerClient(this, googleSignInAccount);
    mInvitationsClient = Games.getInvitationsClient(MainActivity.this, googleSignInAccount);

    // get the playerId from the PlayersClient
    PlayersClient playersClient = Games.getPlayersClient(this, googleSignInAccount);
    playersClient.getCurrentPlayer()
        .addOnSuccessListener(new OnSuccessListener<Player>() {
          @Override
          public void onSuccess(Player player) {
            mPlayerId = player.getPlayerId();

            switchToMainScreen();
          }
        })
        .addOnFailureListener(createFailureListener("There was a problem getting the player id!"));
  }

  // register listener so we are notified if we receive an invitation to play
  // while we are in the game
  mInvitationsClient.registerInvitationCallback(mInvitationCallback);

  // get the invitation from the connection hint
  // Retrieve the TurnBasedMatch from the connectionHint
  GamesClient gamesClient = Games.getGamesClient(MainActivity.this, googleSignInAccount);
  gamesClient.getActivationHint()
      .addOnSuccessListener(new OnSuccessListener<Bundle>() {
        @Override
        public void onSuccess(Bundle hint) {
          if (hint != null) {
            Invitation invitation =
                hint.getParcelable(Multiplayer.EXTRA_INVITATION);

            if (invitation != null && invitation.getInvitationId() != null) {
              // retrieve and cache the invitation ID
              Log.d(TAG, "onConnected: connection hint has a room invite!");
              acceptInviteToRoom(invitation.getInvitationId());
            }
          }
        }
      })
      .addOnFailureListener(createFailureListener("There was a problem getting the activation hint!"));
}
 
Example #24
Source File: SkeletonActivity.java    From android-basic-samples with Apache License 2.0 4 votes vote down vote up
private void onConnected(GoogleSignInAccount googleSignInAccount) {
  Log.d(TAG, "onConnected(): connected to Google APIs");

  mTurnBasedMultiplayerClient = Games.getTurnBasedMultiplayerClient(this, googleSignInAccount);
  mInvitationsClient = Games.getInvitationsClient(this, googleSignInAccount);

  Games.getPlayersClient(this, googleSignInAccount)
      .getCurrentPlayer()
      .addOnSuccessListener(
          new OnSuccessListener<Player>() {
            @Override
            public void onSuccess(Player player) {
              mDisplayName = player.getDisplayName();
              mPlayerId = player.getPlayerId();

              setViewVisibility();
            }
          }
      )
      .addOnFailureListener(createFailureListener("There was a problem getting the player!"));

  Log.d(TAG, "onConnected(): Connection successful");

  // Retrieve the TurnBasedMatch from the connectionHint
  GamesClient gamesClient = Games.getGamesClient(this, googleSignInAccount);
  gamesClient.getActivationHint()
      .addOnSuccessListener(new OnSuccessListener<Bundle>() {
        @Override
        public void onSuccess(Bundle hint) {
          if (hint != null) {
            TurnBasedMatch match = hint.getParcelable(Multiplayer.EXTRA_TURN_BASED_MATCH);

            if (match != null) {
              updateMatch(match);
            }
          }
        }
      })
      .addOnFailureListener(createFailureListener(
          "There was a problem getting the activation hint!"));

  setViewVisibility();

  // As a demonstration, we are registering this activity as a handler for
  // invitation and match events.

  // This is *NOT* required; if you do not register a handler for
  // invitation events, you will get standard notifications instead.
  // Standard notifications may be preferable behavior in many cases.
  mInvitationsClient.registerInvitationCallback(mInvitationCallback);

  // Likewise, we are registering the optional MatchUpdateListener, which
  // will replace notifications you would get otherwise. You do *NOT* have
  // to register a MatchUpdateListener.
  mTurnBasedMultiplayerClient.registerTurnBasedMatchUpdateCallback(mMatchUpdateCallback);
}
 
Example #25
Source File: RealTimeMultiplayer.java    From godot-gpgs with MIT License 4 votes vote down vote up
/**
 * Intent Activity result parser
 * @param request The request of the Intent
 * @param response The response of the Intent
 * @param intent The Intent
 */
public void onActivityResult(int request, int response, Intent intent) {
    switch(request) {
        case RealTimeMultiplayer.RC_SELECT_PLAYERS:
            // Return if the user cancel
            if (response != Activity.RESULT_OK) return;

            // get the invitee list
            Bundle extras = intent.getExtras();
            final ArrayList<String> invitees = intent.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);

            // Get auto-match criteria
            Bundle autoMatchCriteria = null;
            int minAutoMatchPlayers = intent.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
            int maxAutoMatchPlayers = intent.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);

            if (minAutoMatchPlayers > 0) autoMatchCriteria = RoomConfig.createAutoMatchCriteria(minAutoMatchPlayers, maxAutoMatchPlayers, 0);
            else autoMatchCriteria = null;

            // Create the room and specify a variant if appropriate
            RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
            roomConfigBuilder.addPlayersToInvite(invitees);
            if (autoMatchCriteria != null) roomConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
            RoomConfig roomConfig = roomConfigBuilder.build();
            Games.RealTimeMultiplayer.create(googleApiClient, roomConfig);

            // Prevent screen from sleeping during handshake
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            break;

        case RealTimeMultiplayer.RC_WAITING_ROOM:
            // we got the result from the "waiting room" UI.
            if (response == Activity.RESULT_OK) {
                // ready to start playing
                Log.d(TAG, "GPGS: Starting game (waiting room returned OK).");
                GodotLib.calldeferred(instanceId, "_on_gpgs_rtm_start_game", new Object[] { });
            } else if (response == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
                // player indicated that they want to leave the room
                leaveRoom();
            } else if (response == Activity.RESULT_CANCELED) {
                // Dialog was cancelled (user pressed back key, for instance). In our game,
                // this means leaving the room too. In more elaborate games, this could mean
                // something else (like minimizing the waiting room UI).
                leaveRoom();
            }
            break;

        case RealTimeMultiplayer.RC_INVITATION_INBOX:
            // we got the result from the "select invitation" UI (invitation inbox). We're
            // ready to accept the selected invitation:
            handleInvitationInboxResult(response, intent);
            break;
    }
}