Skip to content

Commit b0aa429

Browse files
committed
SavedGames: eclipse scripts, comments, strings, and some fixes
1 parent bad1fc5 commit b0aa429

File tree

4 files changed

+97
-23
lines changed

4 files changed

+97
-23
lines changed

BasicSamples/SavedGames/src/main/java/com/google/example/games/savedgames/MainActivity.java

+80-17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@
2929
import com.google.android.gms.plus.Plus;
3030
import com.google.example.games.basegameutils.BaseGameUtils;
3131

32-
32+
/**
33+
* SavedGames. A sample that demonstrates how to migrate from the Cloud Save (AppState) API to the
34+
* newer Saved Games (Snapshots) API. The app allows load/update to both services as well as an
35+
* example of migrating data from AppState to Snapshots.
36+
*
37+
* @author Sam Stern ([email protected])
38+
*/
3339
public class MainActivity extends Activity implements View.OnClickListener,
3440
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
3541

@@ -137,7 +143,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
137143
SnapshotMetadata selected = Games.Snapshots.getSnapshotFromBundle(bundle);
138144
if (selected == null) {
139145
// No snapshot in the Intent bundle, display error message
140-
displayMessage("Failed to select Saved Game data.", true);
146+
displayMessage(getString(R.string.saved_games_select_failure), true);
141147
setData(null);
142148
displaySnapshotMetadata(null);
143149
} else {
@@ -148,7 +154,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
148154
}
149155
} else {
150156
// User canceled the select intent or it failed for some other reason
151-
displayMessage("No Saved Game selected.", true);
157+
displayMessage(getString(R.string.saved_games_select_cancel), true);
152158
setData(null);
153159
displaySnapshotMetadata(null);
154160
}
@@ -217,8 +223,19 @@ public void onClick(View v) {
217223
}
218224
}
219225

226+
/**
227+
* Start the sign-in process after the user clicks the sign-in button.
228+
*/
220229
private void beginUserInitiatedSignIn() {
221230
Log.d(TAG, "beginUserInitiatedSignIn");
231+
// Check to see the developer who's running this sample code read the instructions :-)
232+
// NOTE: this check is here only because this is a sample! Don't include this
233+
// check in your actual production app.
234+
if (!BaseGameUtils.verifySampleSetup(this, R.string.app_id)) {
235+
Log.w(TAG, "*** Warning: setup problems detected. Sign in may not work!");
236+
}
237+
238+
showProgressDialog("Signing in.");
222239
mSignInClicked = true;
223240
mGoogleApiClient.connect();
224241
}
@@ -237,13 +254,13 @@ private void cloudSaveLoad() {
237254
public void onResult(StateResult stateResult) {
238255
if (stateResult.getStatus().isSuccess()) {
239256
// Successfully loaded data from App State
240-
displayMessage("Loaded from Cloud Save", false);
257+
displayMessage(getString(R.string.cloud_save_load_success), false);
241258
byte[] data = stateResult.getLoadedResult().getLocalData();
242259
setData(new String(data));
243260
displayAppStateMetadata(stateResult.getLoadedResult().getStateKey());
244261
} else {
245262
// Failed to load data from App State
246-
displayMessage("Failed to load from Cloud Save", true);
263+
displayMessage(getString(R.string.cloud_save_load_failure), true);
247264
clearDataUI();
248265
}
249266

@@ -272,9 +289,9 @@ private void cloudSaveUpdate() {
272289
@Override
273290
public void onResult(StateResult stateResult) {
274291
if (stateResult.getStatus().isSuccess()) {
275-
displayMessage("Saved to Cloud Save", false);
292+
displayMessage(getString(R.string.cloud_save_update_success), false);
276293
} else {
277-
displayMessage("Failed to save to Cloud Save", true);
294+
displayMessage(getString(R.string.cloud_save_update_failure), true);
278295
}
279296

280297
dismissProgressDialog();
@@ -301,7 +318,6 @@ private void cloudSaveMigrate() {
301318
final String snapshotName = makeSnapshotName(APP_STATE_KEY);
302319
final String description = "Saved game #" + APP_STATE_KEY;
303320
final long playedTimeMillis = 60 * 60 * 1000;
304-
final byte[] data = getData().getBytes();
305321
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
306322

307323
AsyncTask<Void, Void, Boolean> migrateTask = new AsyncTask<Void, Void, Boolean>() {
@@ -312,6 +328,17 @@ protected void onPreExecute() {
312328

313329
@Override
314330
protected Boolean doInBackground(Void... params) {
331+
// Get AppState Data
332+
StateResult load = AppStateManager.load(mGoogleApiClient, APP_STATE_KEY).await();
333+
334+
if (!load.getStatus().isSuccess()) {
335+
Log.w(TAG, "Could not load App State for migration.");
336+
return false;
337+
}
338+
339+
// Get Data from AppState
340+
byte[] data = load.getLoadedResult().getLocalData();
341+
315342
// Open the snapshot, creating if necessary
316343
OpenSnapshotResult open = Games.Snapshots.open(
317344
mGoogleApiClient, snapshotName, createIfMissing).await();
@@ -341,18 +368,16 @@ protected Boolean doInBackground(Void... params) {
341368
return false;
342369
}
343370

344-
// TODO(samstern): Delete data from appstate?
345-
346371
// No failures
347372
return true;
348373
}
349374

350375
@Override
351376
protected void onPostExecute(Boolean result) {
352377
if (result) {
353-
displayMessage("Migrated to Saved Games.", false);
378+
displayMessage(getString(R.string.cloud_save_migrate_success), false);
354379
} else {
355-
displayMessage("Failed to migrate to Saved Games.", true);
380+
displayMessage(getString(R.string.cloud_save_migrate_failure), true);
356381
}
357382

358383
dismissProgressDialog();
@@ -384,12 +409,12 @@ private void savedGamesLoad(String snapshotName) {
384409
@Override
385410
public void onResult(OpenSnapshotResult openSnapshotResult) {
386411
if (openSnapshotResult.getStatus().isSuccess()) {
387-
displayMessage("Loaded from Saved Games", false);
412+
displayMessage(getString(R.string.saved_games_load_success), false);
388413
byte[] data = openSnapshotResult.getSnapshot().readFully();
389414
setData(new String(data));
390415
displaySnapshotMetadata(openSnapshotResult.getSnapshot().getMetadata());
391416
} else {
392-
displayMessage("Failed to load from Saved Games", true);
417+
displayMessage(getString(R.string.saved_games_load_failure), true);
393418
clearDataUI();
394419
}
395420

@@ -462,9 +487,9 @@ protected Boolean doInBackground(Void... params) {
462487
@Override
463488
protected void onPostExecute(Boolean result) {
464489
if (result) {
465-
displayMessage("Saved to Saved Games", false);
490+
displayMessage(getString(R.string.saved_games_update_success), false);
466491
} else {
467-
displayMessage("Failed to save to Saved Games", true);
492+
displayMessage(getString(R.string.saved_games_update_failure), true);
468493
}
469494

470495
dismissProgressDialog();
@@ -483,19 +508,26 @@ private String makeSnapshotName(int appStateKey) {
483508
return "Snapshot-" + String.valueOf(appStateKey);
484509
}
485510

511+
/**
512+
* Display either the signed-in or signed-out view, depending on the user's state.
513+
*/
486514
private void updateUI() {
487515
// Show signed in or signed out view
488516
if (isSignedIn()) {
489517
findViewById(R.id.layout_signed_in).setVisibility(View.VISIBLE);
490518
findViewById(R.id.layout_signed_out).setVisibility(View.GONE);
491-
displayMessage("Signed in.", false);
519+
displayMessage(getString(R.string.message_signed_in), false);
492520
} else {
493521
findViewById(R.id.layout_signed_in).setVisibility(View.GONE);
494522
findViewById(R.id.layout_signed_out).setVisibility(View.VISIBLE);
495523
displayMessage(getString(R.string.message_sign_in), false);
496524
}
497525
}
498526

527+
/**
528+
* Replace the data displaying in the EditText.
529+
* @param data the String to display.
530+
*/
499531
private void setData(String data) {
500532
EditText dataEditText = (EditText) findViewById(R.id.edit_game_data);
501533

@@ -506,11 +538,20 @@ private void setData(String data) {
506538
}
507539
}
508540

541+
/**
542+
* Get the data from the EditText.
543+
* @return the String in the EditText, or "" if empty.
544+
*/
509545
private String getData() {
510546
EditText dataEditText = (EditText) findViewById(R.id.edit_game_data);
511547
return dataEditText.getText().toString();
512548
}
513549

550+
/**
551+
* Display a status message for the last operation at the bottom of the screen.
552+
* @param msg the message to display.
553+
* @param error true if an error occurred, false otherwise.
554+
*/
514555
private void displayMessage(String msg, boolean error) {
515556
// Set text
516557
TextView messageView = (TextView) findViewById(R.id.text_message);
@@ -524,6 +565,10 @@ private void displayMessage(String msg, boolean error) {
524565
}
525566
}
526567

568+
/**
569+
* Display metadata about AppState save data,
570+
* @param stateKey the slot stateKey of the AppState.
571+
*/
527572
private void displayAppStateMetadata(int stateKey) {
528573
TextView metaDataView = (TextView) findViewById(R.id.text_metadata);
529574
metaDataView.setText("");
@@ -533,6 +578,10 @@ private void displayAppStateMetadata(int stateKey) {
533578
metaDataView.setText(metadataStr);
534579
}
535580

581+
/**
582+
* Display metadata about Snapshot save data.
583+
* @param metadata the SnapshotMetadata associated with the saved game.
584+
*/
536585
private void displaySnapshotMetadata(SnapshotMetadata metadata) {
537586
TextView metaDataView = (TextView) findViewById(R.id.text_metadata);
538587
metaDataView.setText("");
@@ -550,6 +599,9 @@ private void displaySnapshotMetadata(SnapshotMetadata metadata) {
550599
metaDataView.setText(metadataStr);
551600
}
552601

602+
/**
603+
* Clear the data and metadata displays.
604+
*/
553605
private void clearDataUI() {
554606
// Clear the Game Data field and the Metadata field
555607
EditText dataEditText = (EditText) findViewById(R.id.edit_game_data);
@@ -559,10 +611,18 @@ private void clearDataUI() {
559611
metaDataView.setText("");
560612
}
561613

614+
/**
615+
* Determine if the Google API Client is signed in and ready to access Games APIs.
616+
* @return true if client exits and is signed in, false otherwise.
617+
*/
562618
private boolean isSignedIn() {
563619
return (mGoogleApiClient != null && mGoogleApiClient.isConnected());
564620
}
565621

622+
/**
623+
* Show a progress dialog for asynchronous operations.
624+
* @param msg the message to display.
625+
*/
566626
private void showProgressDialog(String msg) {
567627
if (mProgressDialog == null) {
568628
mProgressDialog = new ProgressDialog(this);
@@ -573,6 +633,9 @@ private void showProgressDialog(String msg) {
573633
mProgressDialog.show();
574634
}
575635

636+
/**
637+
* Hide the progress dialog, if it was showing.
638+
*/
576639
private void dismissProgressDialog() {
577640
if (mProgressDialog != null && mProgressDialog.isShowing()) {
578641
mProgressDialog.dismiss();

BasicSamples/SavedGames/src/main/res/values/strings.xml

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
<resources>
33

44
<string name="app_name">SavedGames</string>
5-
<string name="hello_world">Hello world!</string>
6-
<string name="action_settings">Settings</string>
7-
<string name="message_sign_in">Sign-in to begin.</string>
85
<string name="update">Update</string>
96
<string name="load">Load</string>
107
<string name="migrate">Migrate</string>
8+
<string name="select">Select</string>
119
<string name="signin_other_error">There was an error during sign in.</string>
1210
<string name="signin_failure">Unable to sign in. Please check your connection and try again,</string>
13-
<string name="select">Select</string>
11+
<string name="message_sign_in">Sign-in to begin.</string>
12+
<string name="message_signed_in">Signed in.</string>
13+
<string name="saved_games_load_success">Loaded from Saved Games.</string>
14+
<string name="saved_games_load_failure">Failed to load from Saved Games.</string>
15+
<string name="saved_games_update_success">Saved to Saved Games.</string>
16+
<string name="saved_games_update_failure">Failed to save to Saved Games</string>
17+
<string name="saved_games_select_failure">Failed to select from Saved Games.</string>
18+
<string name="saved_games_select_cancel">No Saved Game selected.</string>
19+
<string name="cloud_save_load_success">Loaded from Cloud Save.</string>
20+
<string name="cloud_save_load_failure">Failed to load from Cloud Save.</string>
21+
<string name="cloud_save_update_success">Saved to Cloud Save.</string>
22+
<string name="cloud_save_update_failure">Failed to save to Cloud Save.</string>
23+
<string name="cloud_save_migrate_success">Migrated to Saved Games.</string>
24+
<string name="cloud_save_migrate_failure">Failed to migrate to Saved Games.</string>
1425

1526
</resources>

Scripts/make_eclipse_compat

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fi
99
echo "Making eclipse-compatible projects..."
1010
mkdir -p eclipse_compat
1111

12-
for i in BeGenerous ButtonClicker TypeANumber CollectAllTheStars2 TrivialQuest TrivialQuest2 SkeletonTbmp libraries/BaseGameUtils; do
12+
for i in BeGenerous ButtonClicker TypeANumber CollectAllTheStars2 TrivialQuest TrivialQuest2 SkeletonTbmp SavedGames libraries/BaseGameUtils; do
1313
echo "Preparing $i..."
1414
mkdir -p eclipse_compat/$i
1515
cp BasicSamples/$i/src/main/AndroidManifest.xml eclipse_compat/$i

Scripts/make_eclipse_compat.cmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if NOT exist eclipse_compat mkdir eclipse_compat
1212

1313
echo "Converting each sample"
1414

15-
for %%i in (BeGenerous ButtonClicker TypeANumber CollectAllTheStars2 TrivialQuest TrivialQuest2 SkeletonTbmp libraries\BaseGameUtils) do (
15+
for %%i in (BeGenerous ButtonClicker TypeANumber CollectAllTheStars2 TrivialQuest TrivialQuest2 SkeletonTbmp SavedGames libraries\BaseGameUtils) do (
1616
echo "Preparing %%i..."
1717
if NOT exist eclipse_compat\%%i mkdir eclipse_compat\%%i
1818
copy BasicSamples\%%i\src\main\AndroidManifest.xml eclipse_compat\%%i

0 commit comments

Comments
 (0)