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

The following examples show how to use com.google.android.gms.games.snapshot.SnapshotMetadata. 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: SelectSnapshotActivity.java    From android-basic-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long listId) {

  SnapshotMetadata selected = (SnapshotMetadata) adapterView.getItemAtPosition(position);
  Intent intent = new Intent(Intent.ACTION_DEFAULT);

  intent.putExtra(SNAPSHOT_METADATA, selected.freeze());

  if (mConflictId != null) {
    intent.putExtra(CONFLICT_ID, mConflictId);
    intent.putExtra(RETRY_COUNT, mRetryCount);
  }

  Log.d(TAG, "Finishing item at position " + position + " clicked");
  setResult(RESULT_OK, intent);
  finish();
}
 
Example #3
Source File: SelectSnapshotActivity.java    From android-basic-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_select_snapshot);

  //expect the intent to include the list of snapshots or snapshot metadatas to display
  Intent intent = getIntent();
  if (intent != null) {
    ArrayList<SnapshotMetadata> snapshotMetadataList;
    ListView vw = (ListView) findViewById(R.id.snapshot_list);

    snapshotMetadataList = intent.getParcelableArrayListExtra(SNAPSHOT_METADATA_LIST);
    // set a custom list adapter that can display the image and other
    // information about a snapshot.
    vw.setAdapter(
        new SnapshotListAdapter<SnapshotMetadata>(this, snapshotMetadataList));

    mConflictId = intent.getStringExtra(CONFLICT_ID);
    mRetryCount = intent.getIntExtra(RETRY_COUNT, 0);

    // register this class as the listener for when an item is selected
    vw.setOnItemClickListener(this);
  }
}
 
Example #4
Source File: MainActivity.java    From android-basic-samples with Apache License 2.0 6 votes vote down vote up
private void selectSnapshotItem(int requestCode,
                                ArrayList<Snapshot> items,
                                String conflictId,
                                int retryCount) {

  ArrayList<SnapshotMetadata> snapshotList = new ArrayList<SnapshotMetadata>(items.size());
  for (Snapshot m : items) {
    snapshotList.add(m.getMetadata().freeze());
  }
  Intent intent = new Intent(this, SelectSnapshotActivity.class);
  intent.putParcelableArrayListExtra(SelectSnapshotActivity.SNAPSHOT_METADATA_LIST,
      snapshotList);

  intent.putExtra(SelectSnapshotActivity.CONFLICT_ID, conflictId);
  intent.putExtra(SelectSnapshotActivity.RETRY_COUNT, retryCount);

  Log.d(TAG, "Starting activity to select snapshot");
  startActivityForResult(intent, requestCode);
}
 
Example #5
Source File: PlayGames.java    From remixed-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public void loadSnapshots(@Nullable final Runnable doneCallback) {
	if (isConnected()) {
		Games.getSnapshotsClient(Game.instance(), signedInAccount)
				.load(false)
                   .addOnSuccessListener(
				snapshotMetadataBufferAnnotatedData -> {

					mSavedGamesNames = new ArrayList<>();
					for (SnapshotMetadata m : snapshotMetadataBufferAnnotatedData.get()) {
						mSavedGamesNames.add(m.getUniqueName());
					}
					if (doneCallback!=null) {
						doneCallback.run();
					}
				}
		)
		.addOnFailureListener(e -> {
               EventCollector.logException(e, "Play Games load");
           });
	}
}
 
Example #6
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 #7
Source File: PlayGameServices.java    From PGSGP with MIT License 6 votes vote down vote up
protected void onMainActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SignInController.RC_SIGN_IN) {
        GoogleSignInResult googleSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        signInController.onSignInActivityResult(googleSignInResult);
    } else if (requestCode == AchievementsController.RC_ACHIEVEMENT_UI || requestCode == LeaderboardsController.RC_LEADERBOARD_UI) {
        Pair<Boolean, String> isConnected = connectionController.isConnected();
        godotCallbacksUtils.invokeGodotCallback(GodotCallbacksUtils.PLAYER_CONNECTED, new Object[]{isConnected.first, isConnected.second});
    } else if (requestCode == SavedGamesController.RC_SAVED_GAMES) {
        if (data != null) {
            if (data.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA)) {
                SnapshotMetadata snapshotMetadata = data.getParcelableExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA);
                if (snapshotMetadata != null) {
                    savedGamesController.loadSnapshot(snapshotMetadata.getUniqueName());
                }
            } else if (data.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_NEW)) {
                String unique = new BigInteger(281, new Random()).toString(13);
                String currentSaveName = appActivity.getString(R.string.default_game_name) + unique;

                savedGamesController.createNewSnapshot(currentSaveName);
            }
        }
    }
}
 
Example #8
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 #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 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 #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 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 #11
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 #12
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 #13
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 #14
Source File: MainActivity.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
private void selectSnapshotItem(int requestCode, ArrayList<SnapshotMetadata> items) {

    ArrayList<SnapshotMetadata> metadataArrayList =
        new ArrayList<SnapshotMetadata>(items.size());
    for (SnapshotMetadata m : items) {
      metadataArrayList.add(m.freeze());
    }
    Intent intent = new Intent(this, SelectSnapshotActivity.class);
    intent.putParcelableArrayListExtra(SelectSnapshotActivity.SNAPSHOT_METADATA_LIST,
        metadataArrayList);

    startActivityForResult(intent, requestCode);
  }
 
Example #15
Source File: SnapshotCoordinator.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
public Task<SnapshotsClient.DataOrConflict<Snapshot>> open(final SnapshotsClient snapshotsClient,
                                                           final SnapshotMetadata snapshotMetadata) {
  final String filename = snapshotMetadata.getUniqueName();

  return setIsOpeningTask(filename).continueWithTask(new Continuation<Void, Task<SnapshotsClient.DataOrConflict<Snapshot>>>() {
    @Override
    public Task<SnapshotsClient.DataOrConflict<Snapshot>> then(@NonNull Task<Void> task) throws Exception {
      return snapshotsClient.open(snapshotMetadata)
          .addOnCompleteListener(createOpenListener(filename));
    }
  });
}
 
Example #16
Source File: SnapshotCoordinator.java    From android-basic-samples with Apache License 2.0 5 votes vote down vote up
public Task<SnapshotsClient.DataOrConflict<Snapshot>> open(final SnapshotsClient snapshotsClient,
                                                           final SnapshotMetadata snapshotMetadata,
                                                           final int conflictPolicy) {
  final String filename = snapshotMetadata.getUniqueName();

  return setIsOpeningTask(filename).continueWithTask(new Continuation<Void, Task<SnapshotsClient.DataOrConflict<Snapshot>>>() {
    @Override
    public Task<SnapshotsClient.DataOrConflict<Snapshot>> then(@NonNull Task<Void> task) throws Exception {
      return snapshotsClient.open(snapshotMetadata, conflictPolicy)
          .addOnCompleteListener(createOpenListener(filename));
    }
  });
}
 
Example #17
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 #18
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 #19
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 #20
Source File: SnapshotCoordinator.java    From Asteroid with Apache License 2.0 4 votes vote down vote up
@Override
public SnapshotMetadata getSnapshotFromBundle(Bundle bundle) {
    return Games.Snapshots.getSnapshotFromBundle(bundle);
}
 
Example #21
Source File: SnapshotCoordinator.java    From android-basic-samples with Apache License 2.0 4 votes vote down vote up
public SnapshotMetadata getSnapshotFromBundle(Bundle bundle) {
  return SnapshotsClient.getSnapshotFromBundle(bundle);
}