com.google.android.gms.games.snapshot.SnapshotMetadataChange Java Examples

The following examples show how to use com.google.android.gms.games.snapshot.SnapshotMetadataChange. 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: 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 #2
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 6 votes vote down vote up
@Override
public PendingResult<CommitSnapshotResult> commitAndClose(GoogleApiClient googleApiClient,
                                                          final Snapshot snapshot,
                                                          SnapshotMetadataChange
                                                                  snapshotMetadataChange) {
    if (isAlreadyOpen(snapshot.getMetadata().getUniqueName()) &&
            !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) {
        setIsClosing(snapshot.getMetadata().getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.commitAndClose(googleApiClient, snapshot,
                            snapshotMetadataChange), result -> {
                // even if commit and close fails, the file is closed.
                Log.d(TAG, "CommitAndClose complete, closing " +
                        snapshot.getMetadata().getUniqueName());
                setClosed(snapshot.getMetadata().getUniqueName());
            });
        } catch (RuntimeException e) {
            setClosed(snapshot.getMetadata().getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshot.getMetadata().getUniqueName() +
                " is either closed or is closing");
    }
}
 
Example #3
Source File: GameSyncService.java    From Passbook with Apache License 2.0 6 votes vote down vote up
@Override
public void send(final byte[] data) {
    if(data == null || mSignInAccount == null) {
        return;
    }
    mData = data;
    mSnapshotClient.open(SAVED_DATA, true)
            .continueWithTask(task -> {
                Snapshot snapshot = task.getResult().getData();
                if (snapshot != null) {
                    snapshot.getSnapshotContents().writeBytes(mData);
                    SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder().build();
                    return mSnapshotClient.commitAndClose(snapshot, metadataChange);
                }
                return null;
            })
            .addOnFailureListener(t -> mListener.onSyncFailed(CA.DATA_SENT))
            .addOnCompleteListener(t -> mListener.onSyncProgress(CA.DATA_SENT));
}
 
Example #4
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 #5
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 #6
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> resolveConflict(GoogleApiClient googleApiClient,
                                                         String conflictId, String snapshotId,
                                                         SnapshotMetadataChange snapshotMetadataChange,
                                                         SnapshotContents snapshotContents) {

    // Since the unique name of the snapshot is unknown, this resolution method cannot be safely
    // used.  Please use another method of resolution.
    throw new IllegalStateException("resolving conflicts with ids is not supported.");
}
 
Example #7
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> resolveConflict(GoogleApiClient googleApiClient,
                                                         String conflictId, String snapshotId,
                                                         SnapshotMetadataChange snapshotMetadataChange,
                                                         SnapshotContents snapshotContents) {

    // Since the unique name of the snapshot is unknown, this resolution method cannot be safely
    // used.  Please use another method of resolution.
    throw new IllegalStateException("resolving conflicts with ids is not supported.");
}
 
Example #8
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<CommitSnapshotResult> commitAndClose(GoogleApiClient googleApiClient,
                                                          final Snapshot snapshot,
                                                          SnapshotMetadataChange
                                                                  snapshotMetadataChange) {
    if (isAlreadyOpen(snapshot.getMetadata().getUniqueName()) &&
            !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) {
        setIsClosing(snapshot.getMetadata().getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.commitAndClose(googleApiClient, snapshot,
                            snapshotMetadataChange),
                    new ResultListener() {
                        @Override
                        public void onResult(Result result) {
                            // even if commit and close fails, the file is closed.
                            Log.d(TAG, "CommitAndClose complete, closing " +
                                    snapshot.getMetadata().getUniqueName());
                            setClosed(snapshot.getMetadata().getUniqueName());
                        }
                    });
        } catch (RuntimeException e) {
            setClosed(snapshot.getMetadata().getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshot.getMetadata().getUniqueName() +
                " is either closed or is closing");
    }
}
 
Example #9
Source File: MainActivity.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Generates metadata, takes a screenshot, and performs the write operation for saving a
 * snapshot.
 */
private Task<SnapshotMetadata> writeSnapshot(Snapshot snapshot) {
  // Set the data payload for the snapshot.
  snapshot.getSnapshotContents().writeBytes(mSaveGame.toBytes());

  // Save the snapshot.
  SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
      .setCoverImage(getScreenShot())
      .setDescription("Modified data at: " + Calendar.getInstance().getTime())
      .build();
  return SnapshotCoordinator.getInstance().commitAndClose(mSnapshotsClient, snapshot, metadataChange);
}
 
Example #10
Source File: SnapshotCoordinator.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
public Task<SnapshotsClient.DataOrConflict<Snapshot>> resolveConflict(SnapshotsClient snapshotsClient,
                                                                      String conflictId, String snapshotId,
                                                                      SnapshotMetadataChange snapshotMetadataChange,
                                                                      SnapshotContents snapshotContents) {
  // Since the unique name of the snapshot is unknown, this resolution method cannot be safely
  // used.  Please use another method of resolution.
  throw new IllegalStateException("resolving conflicts with ids is not supported.");
}
 
Example #11
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 4 votes vote down vote up
@NonNull
public Boolean saveGameStateSync(String id, byte[] gameState, long progressValue,
                                 ISaveGameStateResponseListener listener) {
    if (!isSessionActive()) {
        if (listener != null)
            listener.onGameStateSaved(false, "NOT_CONNECTED");
        return false;
    }

    try {
        // Open the snapshot, creating if necessary
        Snapshots.OpenSnapshotResult open = Games.Snapshots.open(
                mGoogleApiClient, id, true).await();

        Snapshot snapshot = processSnapshotOpenResult(open, 0);

        if (snapshot == null) {
            Gdx.app.log(GAMESERVICE_ID, "Could not open Snapshot.");
            if (listener != null)
                listener.onGameStateSaved(false, "Could not open Snapshot.");
            return false;
        }

        if (progressValue < snapshot.getMetadata().getProgressValue()) {
            Gdx.app.error(GAMESERVICE_ID, "Progress of saved game state higher than current one. Did not save.");
            if (listener != null)
                listener.onGameStateSaved(true, null);
            return false;
        }

        // Write the new data to the snapshot
        snapshot.getSnapshotContents().writeBytes(gameState);

        // Change metadata
        SnapshotMetadataChange.Builder metaDataBuilder = new SnapshotMetadataChange.Builder()
                .fromMetadata(snapshot.getMetadata());
        metaDataBuilder = setSaveGameMetaData(metaDataBuilder, id, gameState, progressValue);
        SnapshotMetadataChange metadataChange = metaDataBuilder.build();

        Snapshots.CommitSnapshotResult commit = Games.Snapshots.commitAndClose(
                mGoogleApiClient, snapshot, metadataChange).await();

        if (!commit.getStatus().isSuccess())
            throw new RuntimeException(commit.getStatus().getStatusMessage());

        // No failures
        Gdx.app.log(GAMESERVICE_ID, "Successfully saved gamestate with " + gameState.length + "B");
        if (listener != null)
            listener.onGameStateSaved(true, null);
        return true;

    } catch (Throwable t) {
        Gdx.app.error(GAMESERVICE_ID, "Failed to commit snapshot:" + t.getMessage());
        if (listener != null)
            listener.onGameStateSaved(false, t.getMessage());
        return false;
    }
}
 
Example #12
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 2 votes vote down vote up
/**
 * override this method if you need to set some meta data, for example the description which is displayed
 * in the Play Games app
 *
 * @param metaDataBuilder builder for savegame metadata
 * @param id              snapshot id
 * @param gameState       gamestate data
 * @param progressValue   gamestate progress value
 * @return changed meta data builder
 */
protected SnapshotMetadataChange.Builder setSaveGameMetaData(SnapshotMetadataChange.Builder metaDataBuilder,
                                                             String id, byte[] gameState, long progressValue) {
    return metaDataBuilder.setProgressValue(progressValue);
}