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

The following examples show how to use com.google.android.gms.games.snapshot.Snapshots. 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: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 6 votes vote down vote up
@Override
public PendingResult<DeleteSnapshotResult> delete(GoogleApiClient googleApiClient,
                                                  final SnapshotMetadata snapshotMetadata) {
    if (!isAlreadyOpen(snapshotMetadata.getUniqueName()) &&
            !isAlreadyClosing(snapshotMetadata.getUniqueName())) {
        setIsClosing(snapshotMetadata.getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.delete(googleApiClient, snapshotMetadata),
                    new ResultListener() {
                        @Override
                        public void onResult(Result result) {
                            // deleted files are closed.
                            setClosed(snapshotMetadata.getUniqueName());
                        }
                    });
        } catch (RuntimeException e) {
            setClosed(snapshotMetadata.getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshotMetadata.getUniqueName() +
                " is either open or is busy");
    }
}
 
Example #2
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 6 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> resolveConflict(GoogleApiClient googleApiClient,
                                                         String conflictId,
                                                         final Snapshot snapshot) {
    if (!isAlreadyOpen(snapshot.getMetadata().getUniqueName()) &&
            !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) {
        setIsOpening(snapshot.getMetadata().getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.resolveConflict(googleApiClient, conflictId, snapshot),
                    result -> {
                        if (!result.getStatus().isSuccess()) {
                            setClosed(snapshot.getMetadata().getUniqueName());
                        }
                    });
        } catch (RuntimeException e) {
            setClosed(snapshot.getMetadata().getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshot.getMetadata().getUniqueName() +
                " is already open or is busy");
    }
}
 
Example #3
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 6 votes vote down vote up
@Override
public PendingResult<DeleteSnapshotResult> delete(GoogleApiClient googleApiClient,
                                                  final SnapshotMetadata snapshotMetadata) {
    if (!isAlreadyOpen(snapshotMetadata.getUniqueName()) &&
            !isAlreadyClosing(snapshotMetadata.getUniqueName())) {
        setIsClosing(snapshotMetadata.getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.delete(googleApiClient, snapshotMetadata),
                    result -> {
                        // deleted files are closed.
                        setClosed(snapshotMetadata.getUniqueName());
                    });
        } catch (RuntimeException e) {
            setClosed(snapshotMetadata.getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshotMetadata.getUniqueName() +
                " is either open or is busy");
    }
}
 
Example #4
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 #5
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,
                                                         final Snapshot snapshot) {
    if (!isAlreadyOpen(snapshot.getMetadata().getUniqueName()) &&
            !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) {
        setIsOpening(snapshot.getMetadata().getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.resolveConflict(googleApiClient, conflictId, snapshot),
                    new ResultListener() {
                        @Override
                        public void onResult(Result result) {
                            if (!result.getStatus().isSuccess()) {
                                setClosed(snapshot.getMetadata().getUniqueName());
                            }
                        }
                    });
        } catch (RuntimeException e) {
            setClosed(snapshot.getMetadata().getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshot.getMetadata().getUniqueName() +
                " is already open or is busy");
    }
}
 
Example #6
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 #7
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final SnapshotMetadata snapshotMetadata,
                                              int conflictPolicy) {
    // check if the file is already open
    if (!isAlreadyOpen(snapshotMetadata.getUniqueName())) {
        setIsOpening(snapshotMetadata.getUniqueName());
        try {
            return new CoordinatedPendingResult<>(Games.Snapshots.open(
                    googleApiClient, snapshotMetadata, conflictPolicy),
                    new ResultListener() {
                        @Override
                        public void onResult(Result result) {
                            // if open failed, set the file to closed, otherwise, keep it open.
                            if (!result.getStatus().isSuccess()) {
                                Log.d(TAG, "Open was not a success: " +
                                        result.getStatus() + " for filename " +
                                        snapshotMetadata.getUniqueName());
                                setClosed(snapshotMetadata.getUniqueName());
                            } else {
                                Log.d(TAG, "Open was successful: " +
                                        snapshotMetadata.getUniqueName());
                            }
                        }
                    });
        } catch (RuntimeException e) {
            setClosed(snapshotMetadata.getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshotMetadata.getUniqueName() + " is already open");
    }
}
 
Example #8
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final SnapshotMetadata snapshotMetadata) {
    // check if the file is already open
    if (!isAlreadyOpen(snapshotMetadata.getUniqueName())) {
        setIsOpening(snapshotMetadata.getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.open(googleApiClient, snapshotMetadata),
                    new ResultListener() {
                        @Override
                        public void onResult(Result result) {
                            // if open failed, set the file to closed, otherwise, keep it open.
                            if (!result.getStatus().isSuccess()) {
                                Log.d(TAG, "Open was not a success: " +
                                        result.getStatus() + " for filename " +
                                        snapshotMetadata.getUniqueName());
                                setClosed(snapshotMetadata.getUniqueName());
                            } else {
                                Log.d(TAG, "Open was successful: " +
                                        snapshotMetadata.getUniqueName());
                            }
                        }
                    });
        } catch (RuntimeException e) {
            setClosed(snapshotMetadata.getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshotMetadata.getUniqueName() + " is already open");
    }
}
 
Example #9
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final String filename, boolean createIfNotFound,
                                              int conflictPolicy) {
    // check if the file is already open
    if (!isAlreadyOpen(filename)) {
        setIsOpening(filename);
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.open(googleApiClient, filename, createIfNotFound,
                            conflictPolicy),
                    new ResultListener() {
                        @Override
                        public void onResult(Result result) {
                            // if open failed, set the file to closed, otherwise, keep it open.
                            if (!result.getStatus().isSuccess()) {
                                Log.d(TAG, "Open was not a success: " +
                                        result.getStatus() + " for filename " + filename);
                                setClosed(filename);
                            } else {
                                Log.d(TAG, "Open successful: " + filename);
                            }
                        }
                    });
        } catch (RuntimeException e) {
            setClosed(filename);
            throw e;
        }
    } else {
        throw new IllegalStateException(filename + " is already open");
    }
}
 
Example #10
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final String filename, boolean createIfNotFound) {
    // check if the file is already open
    if (!isAlreadyOpen(filename)) {
        setIsOpening(filename);
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.open(googleApiClient, filename, createIfNotFound),
                    new ResultListener() {
                        @Override
                        public void onResult(Result result) {
                            // if open failed, set the file to closed, otherwise, keep it open.
                            if (!result.getStatus().isSuccess()) {
                                Log.d(TAG, "Open was not a success: " +
                                        result.getStatus() + " for filename " + filename);
                                setClosed(filename);
                            } else {
                                Log.d(TAG, "Open successful: " + filename);
                            }
                        }
                    });
        } catch (RuntimeException e) {
            // catch runtime exceptions here - they should not happen, but they do.
            // mark the file as closed so it can be attempted to be opened again.
            setClosed(filename);
            throw e;
        }
    } else {
        // a more sophisticated solution could attach this operation to a future
        // that would be triggered by closing the file, but this will at least avoid
        // corrupting the data with non-resolvable conflicts.
        throw new IllegalStateException(filename + " is already open");
    }
}
 
Example #11
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public void discardAndClose(GoogleApiClient googleApiClient, Snapshot snapshot) {
    if (isAlreadyOpen(snapshot.getMetadata().getUniqueName()) &&
            !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) {
        Games.Snapshots.discardAndClose(googleApiClient, snapshot);
        Log.d(TAG, "Closed " + snapshot.getMetadata().getUniqueName());
        setClosed(snapshot.getMetadata().getUniqueName());
    } else {
        throw new IllegalStateException(snapshot.getMetadata().getUniqueName() +
                " is not open or is busy");
    }
}
 
Example #12
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 5 votes vote down vote up
@Override
public Intent getSelectSnapshotIntent(GoogleApiClient googleApiClient, String title,
                                      boolean allowAddButton, boolean allowDelete,
                                      int maxSnapshots) {
    return Games.Snapshots.getSelectSnapshotIntent(googleApiClient, title,
            allowAddButton, allowDelete, maxSnapshots);
}
 
Example #13
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final SnapshotMetadata snapshotMetadata,
                                              int conflictPolicy) {
    // check if the file is already open
    if (!isAlreadyOpen(snapshotMetadata.getUniqueName())) {
        setIsOpening(snapshotMetadata.getUniqueName());
        try {
            return new CoordinatedPendingResult<>(Games.Snapshots.open(
                    googleApiClient, snapshotMetadata, conflictPolicy), result -> {
                // if open failed, set the file to closed, otherwise, keep it open.
                if (!result.getStatus().isSuccess()) {
                    Log.d(TAG, "Open was not a success: " +
                            result.getStatus() + " for filename " +
                            snapshotMetadata.getUniqueName());
                    setClosed(snapshotMetadata.getUniqueName());
                } else {
                    Log.d(TAG, "Open was successful: " +
                            snapshotMetadata.getUniqueName());
                }
            });
        } catch (RuntimeException e) {
            setClosed(snapshotMetadata.getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshotMetadata.getUniqueName() + " is already open");
    }
}
 
Example #14
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final String filename, boolean createIfNotFound,
                                              int conflictPolicy) {
    // check if the file is already open
    if (!isAlreadyOpen(filename)) {
        setIsOpening(filename);
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.open(googleApiClient, filename, createIfNotFound,
                            conflictPolicy), result -> {
                // if open failed, set the file to closed, otherwise, keep it open.
                if (!result.getStatus().isSuccess()) {
                    Log.d(TAG, "Open was not a success: " +
                            result.getStatus() + " for filename " + filename);
                    setClosed(filename);
                } else {
                    Log.d(TAG, "Open successful: " + filename);
                }
            });
        } catch (RuntimeException e) {
            setClosed(filename);
            throw e;
        }
    } else {
        throw new IllegalStateException(filename + " is already open");
    }
}
 
Example #15
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final String filename, boolean createIfNotFound) {
    // check if the file is already open
    if (!isAlreadyOpen(filename)) {
        setIsOpening(filename);
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.open(googleApiClient, filename, createIfNotFound), result -> {
                // if open failed, set the file to closed, otherwise, keep it open.
                if (!result.getStatus().isSuccess()) {
                    Log.d(TAG, "Open was not a success: " +
                            result.getStatus() + " for filename " + filename);
                    setClosed(filename);
                } else {
                    Log.d(TAG, "Open successful: " + filename);
                }
            });
        } catch (RuntimeException e) {
            // catch runtime exceptions here - they should not happen, but they do.
            // mark the file as closed so it can be attempted to be opened again.
            setClosed(filename);
            throw e;
        }
    } else {
        // a more sophisticated solution could attach this operation to a future
        // that would be triggered by closing the file, but this will at least avoid
        // corrupting the data with non-resolvable conflicts.
        throw new IllegalStateException(filename + " is already open");
    }
}
 
Example #16
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@Override
public void discardAndClose(GoogleApiClient googleApiClient, Snapshot snapshot) {
    if (isAlreadyOpen(snapshot.getMetadata().getUniqueName()) &&
            !isAlreadyClosing(snapshot.getMetadata().getUniqueName())) {
        Games.Snapshots.discardAndClose(googleApiClient, snapshot);
        Log.d(TAG, "Closed " + snapshot.getMetadata().getUniqueName());
        setClosed(snapshot.getMetadata().getUniqueName());
    } else {
        throw new IllegalStateException(snapshot.getMetadata().getUniqueName() +
                " is not open or is busy");
    }
}
 
Example #17
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@Override
public PendingResult<OpenSnapshotResult> open(GoogleApiClient googleApiClient,
                                              final SnapshotMetadata snapshotMetadata) {
    // check if the file is already open
    if (!isAlreadyOpen(snapshotMetadata.getUniqueName())) {
        setIsOpening(snapshotMetadata.getUniqueName());
        try {
            return new CoordinatedPendingResult<>(
                    Games.Snapshots.open(googleApiClient, snapshotMetadata), result -> {
                // if open failed, set the file to closed, otherwise, keep it open.
                if (!result.getStatus().isSuccess()) {
                    Log.d(TAG, "Open was not a success: " +
                            result.getStatus() + " for filename " +
                            snapshotMetadata.getUniqueName());
                    setClosed(snapshotMetadata.getUniqueName());
                } else {
                    Log.d(TAG, "Open was successful: " +
                            snapshotMetadata.getUniqueName());
                }
            });
        } catch (RuntimeException e) {
            setClosed(snapshotMetadata.getUniqueName());
            throw e;
        }
    } else {
        throw new IllegalStateException(snapshotMetadata.getUniqueName() + " is already open");
    }
}
 
Example #18
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
public boolean deleteGameStateSync(String fileId, ISaveGameStateResponseListener success) {
    if (!isSessionActive()) {
        if (success != null)
            success.onGameStateSaved(false, "NO_CONNECTION");
        return false;
    }

    // Open the snapshot, creating if necessary
    Snapshots.OpenSnapshotResult open = Games.Snapshots.open(
            mGoogleApiClient, fileId, false).await();

    Snapshot snapshot = processSnapshotOpenResult(open, 0);

    if (snapshot == null) {
        Gdx.app.log(GAMESERVICE_ID, "Could not delete game state " + fileId + ": " +
                open.getStatus().getStatusMessage());
        if (success != null)
            success.onGameStateSaved(false, open.getStatus().getStatusMessage());
        return false;
    }

    Snapshots.DeleteSnapshotResult deleteResult = Games.Snapshots.delete(mGoogleApiClient,
            snapshot.getMetadata()).await();

    boolean deletionDone = deleteResult.getStatus().isSuccess();

    Gdx.app.log(GAMESERVICE_ID, "Delete game state " + fileId + ": " + deletionDone +
            " - " + open.getStatus().getStatusMessage());

    if (success != null) {

        success.onGameStateSaved(deletionDone,
                deleteResult.getStatus().getStatusMessage());
    }

    return deletionDone;
}
 
Example #19
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
private boolean fetchGameStatesSync(IFetchGameStatesListResponseListener callback) {
    if (!isSessionActive())
        return false;

    if (!driveApiEnabled)
        throw new UnsupportedOperationException();

    Snapshots.LoadSnapshotsResult loadResult = Games.Snapshots.load(mGoogleApiClient, forceRefresh).await();

    if (!loadResult.getStatus().isSuccess()) {
        Gdx.app.log(GAMESERVICE_ID, "Failed to fetch game states:" +
                loadResult.getStatus().getStatusMessage());
        callback.onFetchGameStatesListResponse(null);
        return false;
    }

    SnapshotMetadataBuffer snapshots = loadResult.getSnapshots();

    Array<String> gameStates = new Array<String>(snapshots.getCount());

    for (SnapshotMetadata snapshot : snapshots) {
        gameStates.add(snapshot.getTitle());
    }

    snapshots.release();

    callback.onFetchGameStatesListResponse(gameStates);

    return true;
}
 
Example #20
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
public boolean loadGameStateSync(String id, ILoadGameStateResponseListener listener) {
    if (!isSessionActive()) {
        listener.gsGameStateLoaded(null);
        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.");
            listener.gsGameStateLoaded(null);
            return false;
        }

        // Read
        byte[] mSaveGameData = snapshot.getSnapshotContents().readFully();
        listener.gsGameStateLoaded(mSaveGameData);
        return true;
    } catch (Throwable t) {
        Gdx.app.error(GAMESERVICE_ID, "Error while reading Snapshot.", t);
        listener.gsGameStateLoaded(null);
        return false;
    }

}
 
Example #21
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
/**
 * Conflict resolution for when Snapshots are opened.  Must be run in an AsyncTask or in a
 * background thread,
 */
public Snapshot processSnapshotOpenResult(Snapshots.OpenSnapshotResult result, int retryCount) {
    Snapshot mResolvedSnapshot = null;
    retryCount++;

    int status = result.getStatus().getStatusCode();
    Gdx.app.log(GAMESERVICE_ID, "Open Snapshot Result status: " + result.getStatus().getStatusMessage());

    if (status == GamesStatusCodes.STATUS_OK) {
        return result.getSnapshot();
    } else if (status == GamesStatusCodes.STATUS_SNAPSHOT_CONFLICT) {
        Snapshot snapshot = result.getSnapshot();
        Snapshot conflictSnapshot = result.getConflictingSnapshot();

        // Resolve between conflicts by selecting the highest progress or, if equal, newest of the conflicting
        // snapshots.
        mResolvedSnapshot = snapshot;

        if (snapshot.getMetadata().getProgressValue() < conflictSnapshot.getMetadata().getProgressValue()
                || snapshot.getMetadata().getProgressValue() == conflictSnapshot.getMetadata().getProgressValue()
                && snapshot.getMetadata().getLastModifiedTimestamp() <
                conflictSnapshot.getMetadata().getLastModifiedTimestamp()) {
            mResolvedSnapshot = conflictSnapshot;
        }

        Snapshots.OpenSnapshotResult resolveResult = Games.Snapshots.resolveConflict(
                mGoogleApiClient, result.getConflictId(), mResolvedSnapshot).await();

        if (retryCount < MAX_SNAPSHOT_RESOLVE_RETRIES) {
            // Recursively attempt again
            return processSnapshotOpenResult(resolveResult, retryCount);
        }

    }

    // Fail, return null.
    return null;
}
 
Example #22
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
@Override
public Intent getSelectSnapshotIntent(GoogleApiClient googleApiClient, String title,
                                      boolean allowAddButton, boolean allowDelete,
                                      int maxSnapshots) {
    return Games.Snapshots.getSelectSnapshotIntent(googleApiClient, title,
            allowAddButton, allowDelete, maxSnapshots);
}
 
Example #23
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 #24
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 4 votes vote down vote up
@Override
public int getMaxDataSize(GoogleApiClient googleApiClient) {
    return Games.Snapshots.getMaxDataSize(googleApiClient);
}
 
Example #25
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 4 votes vote down vote up
@Override
public int getMaxCoverImageSize(GoogleApiClient googleApiClient) {
    return Games.Snapshots.getMaxCoverImageSize(googleApiClient);
}
 
Example #26
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 4 votes vote down vote up
@Override
public SnapshotMetadata getSnapshotFromBundle(Bundle bundle) {
    return Games.Snapshots.getSnapshotFromBundle(bundle);
}
 
Example #27
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 4 votes vote down vote up
@Override
public PendingResult<LoadSnapshotsResult> load(GoogleApiClient googleApiClient,
                                               boolean forceReload) {
    return Games.Snapshots.load(googleApiClient, forceReload);
}
 
Example #28
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 4 votes vote down vote up
@Override
public PendingResult<LoadSnapshotsResult> load(GoogleApiClient googleApiClient,
                                               boolean forceReload) {
    return Games.Snapshots.load(googleApiClient, forceReload);
}
 
Example #29
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 4 votes vote down vote up
@Override
public int getMaxCoverImageSize(GoogleApiClient googleApiClient) {
    return Games.Snapshots.getMaxCoverImageSize(googleApiClient);
}
 
Example #30
Source File: SnapshotCoordinator.java    From Trivia-Knowledge with Apache License 2.0 4 votes vote down vote up
@Override
public int getMaxDataSize(GoogleApiClient googleApiClient) {
    return Games.Snapshots.getMaxDataSize(googleApiClient);
}