Java Code Examples for com.google.android.gms.games.Games#getSnapshotsClient()

The following examples show how to use com.google.android.gms.games.Games#getSnapshotsClient() . 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: GameSyncService.java    From Passbook with Apache License 2.0 7 votes vote down vote up
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    if(requestCode == CA.AUTH) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            mSignInAccount = task.getResult(ApiException.class);
            mSnapshotClient = Games.getSnapshotsClient(activity, mSignInAccount);
            mListener.onSyncProgress(CA.AUTH);
            read();
        } catch (ApiException e) {
            mListener.onSyncFailed(CA.CONNECTION);
        }
    }
}
 
Example 2
Source File: Request.java    From play_games with MIT License 6 votes vote down vote up
public void resolveSnapshotConflict(String snapshotName, String conflictId, String content, Map<String, String> metadata) {
    if (!hasOnlyAllowedKeys(metadata, "description")) {
        return;
    }
    SnapshotsClient snapshotsClient = Games.getSnapshotsClient(this.registrar.activity(), currentAccount);
    Snapshot snapshot = pluginRef.getLoadedSnapshot().get(snapshotName);
    if (snapshot == null) {
        error("SNAPSHOT_NOT_OPENED", "The snapshot with name " + snapshotName + " was not opened before.");
        return;
    }
    snapshot.getSnapshotContents().writeBytes(content.getBytes());
    SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
            .setDescription(metadata.get("description"))
            .build();

    snapshotsClient.resolveConflict(conflictId, snapshot.getMetadata().getSnapshotId(), metadataChange, snapshot.getSnapshotContents()).addOnSuccessListener(generateCallback(snapshotName)).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            error("RESOLVE_SNAPSHOT_CONFLICT_ERROR", e);
        }
    });
}
 
Example 3
Source File: PlayGames.java    From remixed-dungeon with GNU General Public License v3.0 6 votes vote down vote up
private Task<SnapshotMetadata> writeSnapshot(Snapshot snapshot,
											 byte[] data) {

	// Set the data payload for the snapshot
	snapshot.getSnapshotContents().writeBytes(data);

	// Create the change operation
	SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
			.build();

	SnapshotsClient snapshotsClient =
			Games.getSnapshotsClient(Game.instance(), signedInAccount);

	// Commit the operation
	return snapshotsClient.commitAndClose(snapshot, metadataChange);
}
 
Example 4
Source File: PlayGames.java    From remixed-dungeon with GNU General Public License v3.0 6 votes vote down vote up
private void writeToSnapshot(String snapshotId, byte[] content) {

		SnapshotsClient snapshotsClient =
				Games.getSnapshotsClient(Game.instance(), signedInAccount);

		snapshotsClient.open(snapshotId, true, SnapshotsClient.RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED)
				.addOnFailureListener( e -> EventCollector.logException(e))
				.addOnSuccessListener(snapshotDataOrConflict -> {
					if(snapshotDataOrConflict.isConflict()) {
						EventCollector.logException(snapshotDataOrConflict.getConflict().getConflictId());
						//Just remove conflicting snapshot and try again
						snapshotsClient.delete(snapshotDataOrConflict.getConflict().getConflictingSnapshot().getMetadata())
								.addOnCompleteListener(
										task -> PlayGames.this.writeToSnapshot(snapshotId, content)
								);
						return;
					}
					writeSnapshot(snapshotDataOrConflict.getData(),content);
				});
	}
 
Example 5
Source File: SavedGamesController.java    From PGSGP with MIT License 5 votes vote down vote up
protected void showSavedGamesUI(String title, boolean allowAddBtn, boolean allowDeleteBtn, int maxNumberOfSavedGamesToShow) {
    GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(activity);
    if (connectionController.isConnected().first && googleSignInAccount != null) {
        SnapshotsClient snapshotsClient = Games.getSnapshotsClient(activity, googleSignInAccount);
        Task<Intent> intentTask = snapshotsClient.getSelectSnapshotIntent(title, allowAddBtn, allowDeleteBtn, maxNumberOfSavedGamesToShow);

        intentTask.addOnSuccessListener(new OnSuccessListener<Intent>() {
            @Override
            public void onSuccess(Intent intent) {
                activity.startActivityForResult(intent, RC_SAVED_GAMES);
            }
        });
    }
}
 
Example 6
Source File: Request.java    From play_games with MIT License 5 votes vote down vote up
public void openSnapshot(String snapshotName) {
    SnapshotsClient snapshotsClient = Games.getSnapshotsClient(this.registrar.activity(), currentAccount);
    snapshotsClient.open(snapshotName, true).addOnSuccessListener(generateCallback(snapshotName)).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "Could not open snapshot", e);
            error("SNAPSHOT_FAILURE", e);
        }
    });
}
 
Example 7
Source File: Request.java    From play_games with MIT License 5 votes vote down vote up
public void saveSnapshot(String snapshotName, String content, Map<String, String> metadata) {
    if (!hasOnlyAllowedKeys(metadata, "description")) {
        return;
    }
    SnapshotsClient snapshotsClient = Games.getSnapshotsClient(this.registrar.activity(), currentAccount);
    Snapshot snapshot = pluginRef.getLoadedSnapshot().get(snapshotName);
    if (snapshot == null) {
        error("SNAPSHOT_NOT_OPENED", "The snapshot with name " + snapshotName + " was not opened before.");
        return;
    }
    snapshot.getSnapshotContents().writeBytes(content.getBytes());
    SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
            .setDescription(metadata.get("description"))
            .build();
    snapshotsClient.commitAndClose(snapshot, metadataChange).addOnSuccessListener(new OnSuccessListener<SnapshotMetadata>() {
        @Override
        public void onSuccess(SnapshotMetadata snapshotMetadata) {
            Map<String, Object> result = new HashMap<>();
            result.put("status", true);
            result(result);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            error("SAVE_SNAPSHOT_ERROR", e);
        }
    });
}
 
Example 8
Source File: PlayGames.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public void unpackSnapshotTo(String snapshotId, File readTo, IResult result) {
	// Get the SnapshotsClient from the signed in account.
	SnapshotsClient snapshotsClient =
			Games.getSnapshotsClient(Game.instance(), signedInAccount);

	// Open the saved game using its name.
	snapshotsClient.open(snapshotId, true, SnapshotsClient.RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED)
			.addOnFailureListener(e1 -> EventCollector.logException(e1))
			.continueWith(task -> {
				Snapshot snapshot = task.getResult().getData();

				// Opening the snapshot was a success and any conflicts have been resolved.
				try {
					// Extract the raw data from the snapshot.
					return snapshot.getSnapshotContents().readFully();
				} catch (IOException e) {
					EventCollector.logException(e);
				}
				result.status(false);
				return null;
			})
			.addOnCompleteListener(task -> {
				result.status(
						Unzip.unzipStream(
								new ByteArrayInputStream(
										task.getResult()),
										readTo.getAbsolutePath(),
										null));
			});
}
 
Example 9
Source File: MainActivity.java    From android-basic-samples with Apache License 2.0 3 votes vote down vote up
private void onAccountChanged(GoogleSignInAccount googleSignInAccount) {
  mSnapshotsClient = Games.getSnapshotsClient(this, googleSignInAccount);

  // Sign-in worked!
  log("Sign-in successful! Loading game state from cloud.");

  showSignOutBar();

  showSnapshots(getString(R.string.title_load_game), false, false);
}