29
29
import com .google .android .gms .plus .Plus ;
30
30
import com .google .example .games .basegameutils .BaseGameUtils ;
31
31
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
+ */
33
39
public class MainActivity extends Activity implements View .OnClickListener ,
34
40
GoogleApiClient .ConnectionCallbacks , GoogleApiClient .OnConnectionFailedListener {
35
41
@@ -137,7 +143,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
137
143
SnapshotMetadata selected = Games .Snapshots .getSnapshotFromBundle (bundle );
138
144
if (selected == null ) {
139
145
// 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 );
141
147
setData (null );
142
148
displaySnapshotMetadata (null );
143
149
} else {
@@ -148,7 +154,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
148
154
}
149
155
} else {
150
156
// 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 );
152
158
setData (null );
153
159
displaySnapshotMetadata (null );
154
160
}
@@ -217,8 +223,19 @@ public void onClick(View v) {
217
223
}
218
224
}
219
225
226
+ /**
227
+ * Start the sign-in process after the user clicks the sign-in button.
228
+ */
220
229
private void beginUserInitiatedSignIn () {
221
230
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." );
222
239
mSignInClicked = true ;
223
240
mGoogleApiClient .connect ();
224
241
}
@@ -237,13 +254,13 @@ private void cloudSaveLoad() {
237
254
public void onResult (StateResult stateResult ) {
238
255
if (stateResult .getStatus ().isSuccess ()) {
239
256
// Successfully loaded data from App State
240
- displayMessage ("Loaded from Cloud Save" , false );
257
+ displayMessage (getString ( R . string . cloud_save_load_success ) , false );
241
258
byte [] data = stateResult .getLoadedResult ().getLocalData ();
242
259
setData (new String (data ));
243
260
displayAppStateMetadata (stateResult .getLoadedResult ().getStateKey ());
244
261
} else {
245
262
// 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 );
247
264
clearDataUI ();
248
265
}
249
266
@@ -272,9 +289,9 @@ private void cloudSaveUpdate() {
272
289
@ Override
273
290
public void onResult (StateResult stateResult ) {
274
291
if (stateResult .getStatus ().isSuccess ()) {
275
- displayMessage ("Saved to Cloud Save" , false );
292
+ displayMessage (getString ( R . string . cloud_save_update_success ) , false );
276
293
} else {
277
- displayMessage ("Failed to save to Cloud Save" , true );
294
+ displayMessage (getString ( R . string . cloud_save_update_failure ) , true );
278
295
}
279
296
280
297
dismissProgressDialog ();
@@ -301,7 +318,6 @@ private void cloudSaveMigrate() {
301
318
final String snapshotName = makeSnapshotName (APP_STATE_KEY );
302
319
final String description = "Saved game #" + APP_STATE_KEY ;
303
320
final long playedTimeMillis = 60 * 60 * 1000 ;
304
- final byte [] data = getData ().getBytes ();
305
321
final Bitmap bitmap = BitmapFactory .decodeResource (getResources (), R .drawable .ic_launcher );
306
322
307
323
AsyncTask <Void , Void , Boolean > migrateTask = new AsyncTask <Void , Void , Boolean >() {
@@ -312,6 +328,17 @@ protected void onPreExecute() {
312
328
313
329
@ Override
314
330
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
+
315
342
// Open the snapshot, creating if necessary
316
343
OpenSnapshotResult open = Games .Snapshots .open (
317
344
mGoogleApiClient , snapshotName , createIfMissing ).await ();
@@ -341,18 +368,16 @@ protected Boolean doInBackground(Void... params) {
341
368
return false ;
342
369
}
343
370
344
- // TODO(samstern): Delete data from appstate?
345
-
346
371
// No failures
347
372
return true ;
348
373
}
349
374
350
375
@ Override
351
376
protected void onPostExecute (Boolean result ) {
352
377
if (result ) {
353
- displayMessage ("Migrated to Saved Games." , false );
378
+ displayMessage (getString ( R . string . cloud_save_migrate_success ) , false );
354
379
} else {
355
- displayMessage ("Failed to migrate to Saved Games." , true );
380
+ displayMessage (getString ( R . string . cloud_save_migrate_failure ) , true );
356
381
}
357
382
358
383
dismissProgressDialog ();
@@ -384,12 +409,12 @@ private void savedGamesLoad(String snapshotName) {
384
409
@ Override
385
410
public void onResult (OpenSnapshotResult openSnapshotResult ) {
386
411
if (openSnapshotResult .getStatus ().isSuccess ()) {
387
- displayMessage ("Loaded from Saved Games" , false );
412
+ displayMessage (getString ( R . string . saved_games_load_success ) , false );
388
413
byte [] data = openSnapshotResult .getSnapshot ().readFully ();
389
414
setData (new String (data ));
390
415
displaySnapshotMetadata (openSnapshotResult .getSnapshot ().getMetadata ());
391
416
} else {
392
- displayMessage ("Failed to load from Saved Games" , true );
417
+ displayMessage (getString ( R . string . saved_games_load_failure ) , true );
393
418
clearDataUI ();
394
419
}
395
420
@@ -462,9 +487,9 @@ protected Boolean doInBackground(Void... params) {
462
487
@ Override
463
488
protected void onPostExecute (Boolean result ) {
464
489
if (result ) {
465
- displayMessage ("Saved to Saved Games" , false );
490
+ displayMessage (getString ( R . string . saved_games_update_success ) , false );
466
491
} else {
467
- displayMessage ("Failed to save to Saved Games" , true );
492
+ displayMessage (getString ( R . string . saved_games_update_failure ) , true );
468
493
}
469
494
470
495
dismissProgressDialog ();
@@ -483,19 +508,26 @@ private String makeSnapshotName(int appStateKey) {
483
508
return "Snapshot-" + String .valueOf (appStateKey );
484
509
}
485
510
511
+ /**
512
+ * Display either the signed-in or signed-out view, depending on the user's state.
513
+ */
486
514
private void updateUI () {
487
515
// Show signed in or signed out view
488
516
if (isSignedIn ()) {
489
517
findViewById (R .id .layout_signed_in ).setVisibility (View .VISIBLE );
490
518
findViewById (R .id .layout_signed_out ).setVisibility (View .GONE );
491
- displayMessage ("Signed in." , false );
519
+ displayMessage (getString ( R . string . message_signed_in ) , false );
492
520
} else {
493
521
findViewById (R .id .layout_signed_in ).setVisibility (View .GONE );
494
522
findViewById (R .id .layout_signed_out ).setVisibility (View .VISIBLE );
495
523
displayMessage (getString (R .string .message_sign_in ), false );
496
524
}
497
525
}
498
526
527
+ /**
528
+ * Replace the data displaying in the EditText.
529
+ * @param data the String to display.
530
+ */
499
531
private void setData (String data ) {
500
532
EditText dataEditText = (EditText ) findViewById (R .id .edit_game_data );
501
533
@@ -506,11 +538,20 @@ private void setData(String data) {
506
538
}
507
539
}
508
540
541
+ /**
542
+ * Get the data from the EditText.
543
+ * @return the String in the EditText, or "" if empty.
544
+ */
509
545
private String getData () {
510
546
EditText dataEditText = (EditText ) findViewById (R .id .edit_game_data );
511
547
return dataEditText .getText ().toString ();
512
548
}
513
549
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
+ */
514
555
private void displayMessage (String msg , boolean error ) {
515
556
// Set text
516
557
TextView messageView = (TextView ) findViewById (R .id .text_message );
@@ -524,6 +565,10 @@ private void displayMessage(String msg, boolean error) {
524
565
}
525
566
}
526
567
568
+ /**
569
+ * Display metadata about AppState save data,
570
+ * @param stateKey the slot stateKey of the AppState.
571
+ */
527
572
private void displayAppStateMetadata (int stateKey ) {
528
573
TextView metaDataView = (TextView ) findViewById (R .id .text_metadata );
529
574
metaDataView .setText ("" );
@@ -533,6 +578,10 @@ private void displayAppStateMetadata(int stateKey) {
533
578
metaDataView .setText (metadataStr );
534
579
}
535
580
581
+ /**
582
+ * Display metadata about Snapshot save data.
583
+ * @param metadata the SnapshotMetadata associated with the saved game.
584
+ */
536
585
private void displaySnapshotMetadata (SnapshotMetadata metadata ) {
537
586
TextView metaDataView = (TextView ) findViewById (R .id .text_metadata );
538
587
metaDataView .setText ("" );
@@ -550,6 +599,9 @@ private void displaySnapshotMetadata(SnapshotMetadata metadata) {
550
599
metaDataView .setText (metadataStr );
551
600
}
552
601
602
+ /**
603
+ * Clear the data and metadata displays.
604
+ */
553
605
private void clearDataUI () {
554
606
// Clear the Game Data field and the Metadata field
555
607
EditText dataEditText = (EditText ) findViewById (R .id .edit_game_data );
@@ -559,10 +611,18 @@ private void clearDataUI() {
559
611
metaDataView .setText ("" );
560
612
}
561
613
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
+ */
562
618
private boolean isSignedIn () {
563
619
return (mGoogleApiClient != null && mGoogleApiClient .isConnected ());
564
620
}
565
621
622
+ /**
623
+ * Show a progress dialog for asynchronous operations.
624
+ * @param msg the message to display.
625
+ */
566
626
private void showProgressDialog (String msg ) {
567
627
if (mProgressDialog == null ) {
568
628
mProgressDialog = new ProgressDialog (this );
@@ -573,6 +633,9 @@ private void showProgressDialog(String msg) {
573
633
mProgressDialog .show ();
574
634
}
575
635
636
+ /**
637
+ * Hide the progress dialog, if it was showing.
638
+ */
576
639
private void dismissProgressDialog () {
577
640
if (mProgressDialog != null && mProgressDialog .isShowing ()) {
578
641
mProgressDialog .dismiss ();
0 commit comments