-
Notifications
You must be signed in to change notification settings - Fork 954
Expand file tree
/
Copy pathMainActivity.java
More file actions
1128 lines (964 loc) · 38.2 KB
/
MainActivity.java
File metadata and controls
1128 lines (964 loc) · 38.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.example.games.bc;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.GamesActivityResultCodes;
import com.google.android.gms.games.GamesCallbackStatusCodes;
import com.google.android.gms.games.GamesClient;
import com.google.android.gms.games.GamesClientStatusCodes;
import com.google.android.gms.games.InvitationsClient;
import com.google.android.gms.games.Player;
import com.google.android.gms.games.PlayersClient;
import com.google.android.gms.games.RealTimeMultiplayerClient;
import com.google.android.gms.games.multiplayer.Invitation;
import com.google.android.gms.games.multiplayer.InvitationCallback;
import com.google.android.gms.games.multiplayer.Multiplayer;
import com.google.android.gms.games.multiplayer.Participant;
import com.google.android.gms.games.multiplayer.realtime.OnRealTimeMessageReceivedListener;
import com.google.android.gms.games.multiplayer.realtime.RealTimeMessage;
import com.google.android.gms.games.multiplayer.realtime.Room;
import com.google.android.gms.games.multiplayer.realtime.RoomConfig;
import com.google.android.gms.games.multiplayer.realtime.RoomStatusUpdateCallback;
import com.google.android.gms.games.multiplayer.realtime.RoomUpdateCallback;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Button Clicker 2000. A minimalistic game showing the multiplayer features of
* the Google Play game services API. The objective of this game is clicking a
* button. Whoever clicks the button the most times within a 20 second interval
* wins. It's that simple. This game can be played with 2, 3 or 4 players. The
* code is organized in sections in order to make understanding as clear as
* possible. We start with the integration section where we show how the game
* is integrated with the Google Play game services API, then move on to
* game-specific UI and logic.
* <p>
* INSTRUCTIONS: To run this sample, please set up
* a project in the Developer Console. Then, place your app ID on
* res/values/ids.xml. Also, change the package name to the package name you
* used to create the client ID in Developer Console. Make sure you sign the
* APK with the certificate whose fingerprint you entered in Developer Console
* when creating your Client Id.
*
* @author Bruno Oliveira (btco), 2013-04-26
*/
public class MainActivity extends Activity implements
View.OnClickListener {
/*
* API INTEGRATION SECTION. This section contains the code that integrates
* the game with the Google Play game services API.
*/
final static String TAG = "ButtonClicker2000";
// Request codes for the UIs that we show with startActivityForResult:
final static int RC_SELECT_PLAYERS = 10000;
final static int RC_INVITATION_INBOX = 10001;
final static int RC_WAITING_ROOM = 10002;
// Request code used to invoke sign in user interactions.
private static final int RC_SIGN_IN = 9001;
// Client used to sign in with Google APIs
private GoogleSignInClient mGoogleSignInClient = null;
// Client used to interact with the real time multiplayer system.
private RealTimeMultiplayerClient mRealTimeMultiplayerClient = null;
// Client used to interact with the Invitation system.
private InvitationsClient mInvitationsClient = null;
// Room ID where the currently active game is taking place; null if we're
// not playing.
String mRoomId = null;
// Holds the configuration of the current room.
RoomConfig mRoomConfig;
// Are we playing in multiplayer mode?
boolean mMultiplayer = false;
// The participants in the currently active game
ArrayList<Participant> mParticipants = null;
// My participant ID in the currently active game
String mMyId = null;
// If non-null, this is the id of the invitation we received via the
// invitation listener
String mIncomingInvitationId = null;
// Message buffer for sending messages
byte[] mMsgBuf = new byte[2];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the client used to sign in.
mGoogleSignInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
// set up a click listener for everything we care about
for (int id : CLICKABLES) {
findViewById(id).setOnClickListener(this);
}
switchToMainScreen();
checkPlaceholderIds();
}
// Check the sample to ensure all placeholder ids are are updated with real-world values.
// This is strictly for the purpose of the samples; you don't need this in a production
// application.
private void checkPlaceholderIds() {
StringBuilder problems = new StringBuilder();
if (getPackageName().startsWith("com.google.")) {
problems.append("- Package name start with com.google.*\n");
}
for (Integer id : new Integer[]{R.string.app_id}) {
String value = getString(id);
if (value.startsWith("YOUR_")) {
// needs replacing
problems.append("- Placeholders(YOUR_*) in ids.xml need updating\n");
break;
}
}
if (problems.length() > 0) {
problems.insert(0, "The following problems were found:\n\n");
problems.append("\nThese problems may prevent the app from working properly.");
problems.append("\n\nSee the TODO window in Android Studio for more information");
(new AlertDialog.Builder(this)).setMessage(problems.toString())
.setNeutralButton(android.R.string.ok, null).create().show();
}
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
// Since the state of the signed in user can change when the activity is not active
// it is recommended to try and sign in silently from when the app resumes.
signInSilently();
}
@Override
protected void onPause() {
super.onPause();
// unregister our listeners. They will be re-registered via onResume->signInSilently->onConnected.
if (mInvitationsClient != null) {
mInvitationsClient.unregisterInvitationCallback(mInvitationCallback);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_single_player:
case R.id.button_single_player_2:
// play a single-player game
resetGameVars();
startGame(false);
break;
case R.id.button_sign_in:
// start the sign-in flow
Log.d(TAG, "Sign-in button clicked");
startSignInIntent();
break;
case R.id.button_sign_out:
// user wants to sign out
// sign out.
Log.d(TAG, "Sign-out button clicked");
signOut();
switchToScreen(R.id.screen_sign_in);
break;
case R.id.button_invite_players:
switchToScreen(R.id.screen_wait);
// show list of invitable players
mRealTimeMultiplayerClient.getSelectOpponentsIntent(1, 3).addOnSuccessListener(
new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityForResult(intent, RC_SELECT_PLAYERS);
}
}
).addOnFailureListener(createFailureListener("There was a problem selecting opponents."));
break;
case R.id.button_see_invitations:
switchToScreen(R.id.screen_wait);
// show list of pending invitations
mInvitationsClient.getInvitationInboxIntent().addOnSuccessListener(
new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityForResult(intent, RC_INVITATION_INBOX);
}
}
).addOnFailureListener(createFailureListener("There was a problem getting the inbox."));
break;
case R.id.button_accept_popup_invitation:
// user wants to accept the invitation shown on the invitation popup
// (the one we got through the OnInvitationReceivedListener).
acceptInviteToRoom(mIncomingInvitationId);
mIncomingInvitationId = null;
break;
case R.id.button_quick_game:
// user wants to play against a random opponent right now
startQuickGame();
break;
case R.id.button_click_me:
// (gameplay) user clicked the "click me" button
scoreOnePoint();
break;
}
}
void startQuickGame() {
// quick-start a game with 1 randomly selected opponent
final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
MAX_OPPONENTS, 0);
switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
mRoomConfig = RoomConfig.builder(mRoomUpdateCallback)
.setOnMessageReceivedListener(mOnRealTimeMessageReceivedListener)
.setRoomStatusUpdateCallback(mRoomStatusUpdateCallback)
.setAutoMatchCriteria(autoMatchCriteria)
.build();
mRealTimeMultiplayerClient.create(mRoomConfig);
}
/**
* Start a sign in activity. To properly handle the result, call tryHandleSignInResult from
* your Activity's onActivityResult function
*/
public void startSignInIntent() {
startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
}
/**
* Try to sign in without displaying dialogs to the user.
* <p>
* If the user has already signed in previously, it will not show dialog.
*/
public void signInSilently() {
Log.d(TAG, "signInSilently()");
mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInSilently(): success");
onConnected(task.getResult());
} else {
Log.d(TAG, "signInSilently(): failure", task.getException());
onDisconnected();
}
}
});
}
public void signOut() {
Log.d(TAG, "signOut()");
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signOut(): success");
} else {
handleException(task.getException(), "signOut() failed!");
}
onDisconnected();
}
});
}
/**
* Since a lot of the operations use tasks, we can use a common handler for whenever one fails.
*
* @param exception The exception to evaluate. Will try to display a more descriptive reason for the exception.
* @param details Will display alongside the exception if you wish to provide more details for why the exception
* happened
*/
private void handleException(Exception exception, String details) {
int status = 0;
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
status = apiException.getStatusCode();
}
String errorString = null;
switch (status) {
case GamesCallbackStatusCodes.OK:
break;
case GamesClientStatusCodes.MULTIPLAYER_ERROR_NOT_TRUSTED_TESTER:
errorString = getString(R.string.status_multiplayer_error_not_trusted_tester);
break;
case GamesClientStatusCodes.MATCH_ERROR_ALREADY_REMATCHED:
errorString = getString(R.string.match_error_already_rematched);
break;
case GamesClientStatusCodes.NETWORK_ERROR_OPERATION_FAILED:
errorString = getString(R.string.network_error_operation_failed);
break;
case GamesClientStatusCodes.INTERNAL_ERROR:
errorString = getString(R.string.internal_error);
break;
case GamesClientStatusCodes.MATCH_ERROR_INACTIVE_MATCH:
errorString = getString(R.string.match_error_inactive_match);
break;
case GamesClientStatusCodes.MATCH_ERROR_LOCALLY_MODIFIED:
errorString = getString(R.string.match_error_locally_modified);
break;
default:
errorString = getString(R.string.unexpected_status, GamesClientStatusCodes.getStatusCodeString(status));
break;
}
if (errorString == null) {
return;
}
String message = getString(R.string.status_exception_error, details, status, exception);
new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage(message + "\n" + errorString)
.setNeutralButton(android.R.string.ok, null)
.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task =
GoogleSignIn.getSignedInAccountFromIntent(intent);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
onConnected(account);
} catch (ApiException apiException) {
String message = apiException.getMessage();
if (message == null || message.isEmpty()) {
message = getString(R.string.signin_other_error);
}
onDisconnected();
new AlertDialog.Builder(this)
.setMessage(message)
.setNeutralButton(android.R.string.ok, null)
.show();
}
} else if (requestCode == RC_SELECT_PLAYERS) {
// we got the result from the "select players" UI -- ready to create the room
handleSelectPlayersResult(resultCode, intent);
} else if (requestCode == RC_INVITATION_INBOX) {
// we got the result from the "select invitation" UI (invitation inbox). We're
// ready to accept the selected invitation:
handleInvitationInboxResult(resultCode, intent);
} else if (requestCode == RC_WAITING_ROOM) {
// we got the result from the "waiting room" UI.
if (resultCode == Activity.RESULT_OK) {
// ready to start playing
Log.d(TAG, "Starting game (waiting room returned OK).");
startGame(true);
} else if (resultCode == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
// player indicated that they want to leave the room
leaveRoom();
} else if (resultCode == Activity.RESULT_CANCELED) {
// Dialog was cancelled (user pressed back key, for instance). In our game,
// this means leaving the room too. In more elaborate games, this could mean
// something else (like minimizing the waiting room UI).
leaveRoom();
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
// Handle the result of the "Select players UI" we launched when the user clicked the
// "Invite friends" button. We react by creating a room with those players.
private void handleSelectPlayersResult(int response, Intent data) {
if (response != Activity.RESULT_OK) {
Log.w(TAG, "*** select players UI cancelled, " + response);
switchToMainScreen();
return;
}
Log.d(TAG, "Select players UI succeeded.");
// get the invitee list
final ArrayList<String> invitees = data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
Log.d(TAG, "Invitee count: " + invitees.size());
// get the automatch criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
Log.d(TAG, "Automatch criteria: " + autoMatchCriteria);
}
// create the room
Log.d(TAG, "Creating room...");
switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
mRoomConfig = RoomConfig.builder(mRoomUpdateCallback)
.addPlayersToInvite(invitees)
.setOnMessageReceivedListener(mOnRealTimeMessageReceivedListener)
.setRoomStatusUpdateCallback(mRoomStatusUpdateCallback)
.setAutoMatchCriteria(autoMatchCriteria).build();
mRealTimeMultiplayerClient.create(mRoomConfig);
Log.d(TAG, "Room created, waiting for it to be ready...");
}
// Handle the result of the invitation inbox UI, where the player can pick an invitation
// to accept. We react by accepting the selected invitation, if any.
private void handleInvitationInboxResult(int response, Intent data) {
if (response != Activity.RESULT_OK) {
Log.w(TAG, "*** invitation inbox UI cancelled, " + response);
switchToMainScreen();
return;
}
Log.d(TAG, "Invitation inbox UI succeeded.");
Invitation invitation = data.getExtras().getParcelable(Multiplayer.EXTRA_INVITATION);
// accept invitation
if (invitation != null) {
acceptInviteToRoom(invitation.getInvitationId());
}
}
// Accept the given invitation.
void acceptInviteToRoom(String invitationId) {
// accept the invitation
Log.d(TAG, "Accepting invitation: " + invitationId);
mRoomConfig = RoomConfig.builder(mRoomUpdateCallback)
.setInvitationIdToAccept(invitationId)
.setOnMessageReceivedListener(mOnRealTimeMessageReceivedListener)
.setRoomStatusUpdateCallback(mRoomStatusUpdateCallback)
.build();
switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
mRealTimeMultiplayerClient.join(mRoomConfig)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Room Joined Successfully!");
}
});
}
// Activity is going to the background. We have to leave the current room.
@Override
public void onStop() {
Log.d(TAG, "**** got onStop");
// if we're in a room, leave it.
leaveRoom();
// stop trying to keep the screen on
stopKeepingScreenOn();
switchToMainScreen();
super.onStop();
}
// Handle back key to make sure we cleanly leave a game if we are in the middle of one
@Override
public boolean onKeyDown(int keyCode, KeyEvent e) {
if (keyCode == KeyEvent.KEYCODE_BACK && mCurScreen == R.id.screen_game) {
leaveRoom();
return true;
}
return super.onKeyDown(keyCode, e);
}
// Leave the room.
void leaveRoom() {
Log.d(TAG, "Leaving room.");
mSecondsLeft = 0;
stopKeepingScreenOn();
if (mRoomId != null) {
mRealTimeMultiplayerClient.leave(mRoomConfig, mRoomId)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mRoomId = null;
mRoomConfig = null;
}
});
switchToScreen(R.id.screen_wait);
} else {
switchToMainScreen();
}
}
// Show the waiting room UI to track the progress of other players as they enter the
// room and get connected.
void showWaitingRoom(Room room) {
// minimum number of players required for our game
// For simplicity, we require everyone to join the game before we start it
// (this is signaled by Integer.MAX_VALUE).
final int MIN_PLAYERS = Integer.MAX_VALUE;
mRealTimeMultiplayerClient.getWaitingRoomIntent(room, MIN_PLAYERS)
.addOnSuccessListener(new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
// show waiting room UI
startActivityForResult(intent, RC_WAITING_ROOM);
}
})
.addOnFailureListener(createFailureListener("There was a problem getting the waiting room!"));
}
private InvitationCallback mInvitationCallback = new InvitationCallback() {
// Called when we get an invitation to play a game. We react by showing that to the user.
@Override
public void onInvitationReceived(@NonNull Invitation invitation) {
// We got an invitation to play a game! So, store it in
// mIncomingInvitationId
// and show the popup on the screen.
mIncomingInvitationId = invitation.getInvitationId();
((TextView) findViewById(R.id.incoming_invitation_text)).setText(
invitation.getInviter().getDisplayName() + " " +
getString(R.string.is_inviting_you));
switchToScreen(mCurScreen); // This will show the invitation popup
}
@Override
public void onInvitationRemoved(@NonNull String invitationId) {
if (mIncomingInvitationId.equals(invitationId) && mIncomingInvitationId != null) {
mIncomingInvitationId = null;
switchToScreen(mCurScreen); // This will hide the invitation popup
}
}
};
/*
* CALLBACKS SECTION. This section shows how we implement the several games
* API callbacks.
*/
private String mPlayerId;
// The currently signed in account, used to check the account has changed outside of this activity when resuming.
GoogleSignInAccount mSignedInAccount = null;
private void onConnected(GoogleSignInAccount googleSignInAccount) {
Log.d(TAG, "onConnected(): connected to Google APIs");
if (mSignedInAccount != googleSignInAccount) {
mSignedInAccount = googleSignInAccount;
// update the clients
mRealTimeMultiplayerClient = Games.getRealTimeMultiplayerClient(this, googleSignInAccount);
mInvitationsClient = Games.getInvitationsClient(MainActivity.this, googleSignInAccount);
// get the playerId from the PlayersClient
PlayersClient playersClient = Games.getPlayersClient(this, googleSignInAccount);
playersClient.getCurrentPlayer()
.addOnSuccessListener(new OnSuccessListener<Player>() {
@Override
public void onSuccess(Player player) {
mPlayerId = player.getPlayerId();
switchToMainScreen();
}
})
.addOnFailureListener(createFailureListener("There was a problem getting the player id!"));
}
// register listener so we are notified if we receive an invitation to play
// while we are in the game
mInvitationsClient.registerInvitationCallback(mInvitationCallback);
// get the invitation from the connection hint
// Retrieve the TurnBasedMatch from the connectionHint
GamesClient gamesClient = Games.getGamesClient(MainActivity.this, googleSignInAccount);
gamesClient.getActivationHint()
.addOnSuccessListener(new OnSuccessListener<Bundle>() {
@Override
public void onSuccess(Bundle hint) {
if (hint != null) {
Invitation invitation =
hint.getParcelable(Multiplayer.EXTRA_INVITATION);
if (invitation != null && invitation.getInvitationId() != null) {
// retrieve and cache the invitation ID
Log.d(TAG, "onConnected: connection hint has a room invite!");
acceptInviteToRoom(invitation.getInvitationId());
}
}
}
})
.addOnFailureListener(createFailureListener("There was a problem getting the activation hint!"));
}
private OnFailureListener createFailureListener(final String string) {
return new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
handleException(e, string);
}
};
}
public void onDisconnected() {
Log.d(TAG, "onDisconnected()");
mRealTimeMultiplayerClient = null;
mInvitationsClient = null;
switchToMainScreen();
}
private RoomStatusUpdateCallback mRoomStatusUpdateCallback = new RoomStatusUpdateCallback() {
// Called when we are connected to the room. We're not ready to play yet! (maybe not everybody
// is connected yet).
@Override
public void onConnectedToRoom(Room room) {
Log.d(TAG, "onConnectedToRoom.");
//get participants and my ID:
mParticipants = room.getParticipants();
mMyId = room.getParticipantId(mPlayerId);
// save room ID if its not initialized in onRoomCreated() so we can leave cleanly before the game starts.
if (mRoomId == null) {
mRoomId = room.getRoomId();
}
// print out the list of participants (for debug purposes)
Log.d(TAG, "Room ID: " + mRoomId);
Log.d(TAG, "My ID " + mMyId);
Log.d(TAG, "<< CONNECTED TO ROOM>>");
}
// Called when we get disconnected from the room. We return to the main screen.
@Override
public void onDisconnectedFromRoom(Room room) {
mRoomId = null;
mRoomConfig = null;
showGameError();
}
// We treat most of the room update callbacks in the same way: we update our list of
// participants and update the display. In a real game we would also have to check if that
// change requires some action like removing the corresponding player avatar from the screen,
// etc.
@Override
public void onPeerDeclined(Room room, @NonNull List<String> arg1) {
updateRoom(room);
}
@Override
public void onPeerInvitedToRoom(Room room, @NonNull List<String> arg1) {
updateRoom(room);
}
@Override
public void onP2PDisconnected(@NonNull String participant) {
}
@Override
public void onP2PConnected(@NonNull String participant) {
}
@Override
public void onPeerJoined(Room room, @NonNull List<String> arg1) {
updateRoom(room);
}
@Override
public void onPeerLeft(Room room, @NonNull List<String> peersWhoLeft) {
updateRoom(room);
}
@Override
public void onRoomAutoMatching(Room room) {
updateRoom(room);
}
@Override
public void onRoomConnecting(Room room) {
updateRoom(room);
}
@Override
public void onPeersConnected(Room room, @NonNull List<String> peers) {
updateRoom(room);
}
@Override
public void onPeersDisconnected(Room room, @NonNull List<String> peers) {
updateRoom(room);
}
};
// Show error message about game being cancelled and return to main screen.
void showGameError() {
new AlertDialog.Builder(this)
.setMessage(getString(R.string.game_problem))
.setNeutralButton(android.R.string.ok, null).create();
switchToMainScreen();
}
private RoomUpdateCallback mRoomUpdateCallback = new RoomUpdateCallback() {
// Called when room has been created
@Override
public void onRoomCreated(int statusCode, Room room) {
Log.d(TAG, "onRoomCreated(" + statusCode + ", " + room + ")");
if (statusCode != GamesCallbackStatusCodes.OK) {
Log.e(TAG, "*** Error: onRoomCreated, status " + statusCode);
showGameError();
return;
}
// save room ID so we can leave cleanly before the game starts.
mRoomId = room.getRoomId();
// show the waiting room UI
showWaitingRoom(room);
}
// Called when room is fully connected.
@Override
public void onRoomConnected(int statusCode, Room room) {
Log.d(TAG, "onRoomConnected(" + statusCode + ", " + room + ")");
if (statusCode != GamesCallbackStatusCodes.OK) {
Log.e(TAG, "*** Error: onRoomConnected, status " + statusCode);
showGameError();
return;
}
updateRoom(room);
}
@Override
public void onJoinedRoom(int statusCode, Room room) {
Log.d(TAG, "onJoinedRoom(" + statusCode + ", " + room + ")");
if (statusCode != GamesCallbackStatusCodes.OK) {
Log.e(TAG, "*** Error: onRoomConnected, status " + statusCode);
showGameError();
return;
}
// show the waiting room UI
showWaitingRoom(room);
}
// Called when we've successfully left the room (this happens a result of voluntarily leaving
// via a call to leaveRoom(). If we get disconnected, we get onDisconnectedFromRoom()).
@Override
public void onLeftRoom(int statusCode, @NonNull String roomId) {
// we have left the room; return to main screen.
Log.d(TAG, "onLeftRoom, code " + statusCode);
switchToMainScreen();
}
};
void updateRoom(Room room) {
if (room != null) {
mParticipants = room.getParticipants();
}
if (mParticipants != null) {
updatePeerScoresDisplay();
}
}
/*
* GAME LOGIC SECTION. Methods that implement the game's rules.
*/
// Current state of the game:
int mSecondsLeft = -1; // how long until the game ends (seconds)
final static int GAME_DURATION = 20; // game duration, seconds.
int mScore = 0; // user's current score
// Reset game variables in preparation for a new game.
void resetGameVars() {
mSecondsLeft = GAME_DURATION;
mScore = 0;
mParticipantScore.clear();
mFinishedParticipants.clear();
}
// Start the gameplay phase of the game.
void startGame(boolean multiplayer) {
mMultiplayer = multiplayer;
updateScoreDisplay();
broadcastScore(false);
switchToScreen(R.id.screen_game);
findViewById(R.id.button_click_me).setVisibility(View.VISIBLE);
// run the gameTick() method every second to update the game.
final Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
if (mSecondsLeft <= 0) {
return;
}
gameTick();
h.postDelayed(this, 1000);
}
}, 1000);
}
// Game tick -- update countdown, check if game ended.
void gameTick() {
if (mSecondsLeft > 0) {
--mSecondsLeft;
}
// update countdown
((TextView) findViewById(R.id.countdown)).setText("0:" +
(mSecondsLeft < 10 ? "0" : "") + String.valueOf(mSecondsLeft));
if (mSecondsLeft <= 0) {
// finish game
findViewById(R.id.button_click_me).setVisibility(View.GONE);
broadcastScore(true);
}
}
// indicates the player scored one point
void scoreOnePoint() {
if (mSecondsLeft <= 0) {
return; // too late!
}
++mScore;
updateScoreDisplay();
updatePeerScoresDisplay();
// broadcast our new score to our peers
broadcastScore(false);
}
/*
* COMMUNICATIONS SECTION. Methods that implement the game's network
* protocol.
*/
// Score of other participants. We update this as we receive their scores
// from the network.
Map<String, Integer> mParticipantScore = new HashMap<>();
// Participants who sent us their final score.
Set<String> mFinishedParticipants = new HashSet<>();
// Called when we receive a real-time message from the network.
// Messages in our game are made up of 2 bytes: the first one is 'F' or 'U'
// indicating
// whether it's a final or interim score. The second byte is the score.
// There is also the
// 'S' message, which indicates that the game should start.
OnRealTimeMessageReceivedListener mOnRealTimeMessageReceivedListener = new OnRealTimeMessageReceivedListener() {
@Override
public void onRealTimeMessageReceived(@NonNull RealTimeMessage realTimeMessage) {
byte[] buf = realTimeMessage.getMessageData();
String sender = realTimeMessage.getSenderParticipantId();
Log.d(TAG, "Message received: " + (char) buf[0] + "/" + (int) buf[1]);
if (buf[0] == 'F' || buf[0] == 'U') {
// score update.
int existingScore = mParticipantScore.containsKey(sender) ?
mParticipantScore.get(sender) : 0;
int thisScore = (int) buf[1];
if (thisScore > existingScore) {
// this check is necessary because packets may arrive out of
// order, so we
// should only ever consider the highest score we received, as
// we know in our
// game there is no way to lose points. If there was a way to
// lose points,
// we'd have to add a "serial number" to the packet.
mParticipantScore.put(sender, thisScore);
}
// update the scores on the screen
updatePeerScoresDisplay();
// if it's a final score, mark this participant as having finished
// the game
if ((char) buf[0] == 'F') {
mFinishedParticipants.add(realTimeMessage.getSenderParticipantId());
}
}
}
};
// Broadcast my score to everybody else.
void broadcastScore(boolean finalScore) {
if (!mMultiplayer) {
// playing single-player mode
return;
}
// First byte in message indicates whether it's a final score or not
mMsgBuf[0] = (byte) (finalScore ? 'F' : 'U');
// Second byte is the score.
mMsgBuf[1] = (byte) mScore;
// Send to every other participant.
for (Participant p : mParticipants) {
if (p.getParticipantId().equals(mMyId)) {
continue;
}
if (p.getStatus() != Participant.STATUS_JOINED) {
continue;
}
if (finalScore) {
// final score notification must be sent via reliable message
mRealTimeMultiplayerClient.sendReliableMessage(mMsgBuf,
mRoomId, p.getParticipantId(), new RealTimeMultiplayerClient.ReliableMessageSentCallback() {
@Override
public void onRealTimeMessageSent(int statusCode, int tokenId, String recipientParticipantId) {
Log.d(TAG, "RealTime message sent");
Log.d(TAG, " statusCode: " + statusCode);
Log.d(TAG, " tokenId: " + tokenId);
Log.d(TAG, " recipientParticipantId: " + recipientParticipantId);
}