3131import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
3232import com .google .android .gms .common .api .ApiException ;
3333import com .google .android .gms .games .AchievementsClient ;
34+ import com .google .android .gms .games .AnnotatedData ;
35+ import com .google .android .gms .games .EventsClient ;
3436import com .google .android .gms .games .Games ;
3537import com .google .android .gms .games .LeaderboardsClient ;
3638import com .google .android .gms .games .Player ;
3739import com .google .android .gms .games .PlayersClient ;
40+ import com .google .android .gms .games .event .Event ;
41+ import com .google .android .gms .games .event .EventBuffer ;
3842import com .google .android .gms .tasks .OnCompleteListener ;
3943import com .google .android .gms .tasks .OnFailureListener ;
4044import com .google .android .gms .tasks .OnSuccessListener ;
@@ -72,6 +76,7 @@ public class MainActivity extends FragmentActivity implements
7276 // Client variables
7377 private AchievementsClient mAchievementsClient ;
7478 private LeaderboardsClient mLeaderboardsClient ;
79+ private EventsClient mEventsClient ;
7580 private PlayersClient mPlayersClient ;
7681
7782 // request codes we use when invoking an external activity
@@ -118,6 +123,40 @@ public void onCreate(Bundle savedInstanceState) {
118123 mMainMenuFragment ).commit ();
119124 }
120125
126+ private void loadAndPrintEvents () {
127+
128+ final MainActivity mainActivity = this ;
129+
130+ mEventsClient .load (true )
131+ .addOnSuccessListener (new OnSuccessListener <AnnotatedData <EventBuffer >>() {
132+ @ Override
133+ public void onSuccess (AnnotatedData <EventBuffer > eventBufferAnnotatedData ) {
134+ EventBuffer eventBuffer = eventBufferAnnotatedData .get ();
135+
136+ int count = 0 ;
137+ if (eventBuffer != null ) {
138+ count = eventBuffer .getCount ();
139+ }
140+
141+ Log .i (TAG , "number of events: " + count );
142+
143+ for (int i = 0 ; i < count ; i ++) {
144+ Event event = eventBuffer .get (i );
145+ Log .i (TAG , "event: "
146+ + event .getName ()
147+ + " -> "
148+ + event .getValue ());
149+ }
150+ }
151+ })
152+ .addOnFailureListener (new OnFailureListener () {
153+ @ Override
154+ public void onFailure (@ NonNull Exception e ) {
155+ handleException (e , getString (R .string .achievements_exception ));
156+ }
157+ });
158+ }
159+
121160 // Switch UI to the given fragment
122161 private void switchToFragment (Fragment newFrag ) {
123162 getSupportFragmentManager ().beginTransaction ().replace (R .id .fragment_container , newFrag )
@@ -259,6 +298,7 @@ private void handleException(Exception e, String details) {
259298 private void startGame (boolean hardMode ) {
260299 mHardMode = hardMode ;
261300 switchToFragment (mGameplayFragment );
301+ mEventsClient .increment (getString (R .string .event_start ), 1 );
262302 }
263303
264304 @ Override
@@ -281,6 +321,8 @@ public void onEnteredScore(int requestedScore) {
281321
282322 // switch to the exciting "you won" screen
283323 switchToFragment (mWinFragment );
324+
325+ mEventsClient .increment (getString (R .string .event_number_chosen ), 1 );
284326 }
285327
286328 // Checks if n is prime. We don't consider 0 and 1 to be prime.
@@ -423,6 +465,7 @@ private void onConnected(GoogleSignInAccount googleSignInAccount) {
423465
424466 mAchievementsClient = Games .getAchievementsClient (this , googleSignInAccount );
425467 mLeaderboardsClient = Games .getLeaderboardsClient (this , googleSignInAccount );
468+ mEventsClient = Games .getEventsClient (this , googleSignInAccount );
426469 mPlayersClient = Games .getPlayersClient (this , googleSignInAccount );
427470
428471 // Show sign-out button on main menu
@@ -455,6 +498,8 @@ public void onComplete(@NonNull Task<Player> task) {
455498 Toast .makeText (this , getString (R .string .your_progress_will_be_uploaded ),
456499 Toast .LENGTH_LONG ).show ();
457500 }
501+
502+ loadAndPrintEvents ();
458503 }
459504
460505 private void onDisconnected () {
0 commit comments