17
17
import com .google .android .gms .games .Games ;
18
18
import com .google .android .gms .games .Player ;
19
19
import com .google .example .games .basegameutils .BaseGameUtils ;
20
+ import com .google .firebase .iid .FirebaseInstanceId ;
20
21
21
22
import butterknife .BindView ;
22
23
import butterknife .ButterKnife ;
23
24
import io .reactivex .android .schedulers .AndroidSchedulers ;
24
25
import io .reactivex .schedulers .Schedulers ;
25
26
import website .bloop .app .BloopApplication ;
27
+ import website .bloop .app .BloopFirebaseInstanceIDService ;
26
28
import website .bloop .app .R ;
27
29
import website .bloop .app .api .BloopAPIService ;
28
30
import website .bloop .app .views .SonarView ;
29
31
32
+ import static website .bloop .app .BloopApplication .BLOOP_PREFERENCE_FILE ;
33
+
30
34
/**
31
35
* Login authentication through Google Play Games
32
36
* Reference: https://developers.google.com/games/services/training/signin
@@ -46,7 +50,7 @@ public class PlayLoginActivity extends AppCompatActivity
46
50
private boolean mSignInClicked = false ;
47
51
48
52
private GoogleApiClient mGoogleApiClient ;
49
- private BloopAPIService mService ;
53
+ private BloopApplication mApplication ;
50
54
51
55
@ BindView (R .id .signInName )
52
56
TextView loginText ;
@@ -80,7 +84,7 @@ protected void onCreate(Bundle savedInstanceState) {
80
84
// set to application (like singleton) so we can re-call it
81
85
BloopApplication .getInstance ().setGoogleApiClient (mGoogleApiClient );
82
86
83
- mService = BloopApplication .getInstance (). getService ();
87
+ mApplication = BloopApplication .getInstance ();
84
88
SharedPreferences loginPref = getSharedPreferences (PREF_LOGIN , Context .MODE_PRIVATE );
85
89
boolean loggedIn = loginPref .getBoolean (PREF_LOGIN_VAL , false );
86
90
@@ -107,19 +111,11 @@ public void onConnected(@Nullable Bundle bundle) {
107
111
108
112
playerId = Games .Players .getCurrentPlayerId (mGoogleApiClient );
109
113
110
- BloopApplication . getInstance () .setPlayerName (displayName );
111
- BloopApplication . getInstance () .setPlayerId (playerId );
114
+ mApplication .setPlayerName (displayName );
115
+ mApplication .setPlayerId (playerId );
112
116
113
117
website .bloop .app .api .Player player = new website .bloop .app .api .Player (displayName , playerId , null );
114
-
115
- // TODO: move this to another class?
116
- mService .addPlayer (player )
117
- .subscribeOn (Schedulers .newThread ())
118
- .observeOn (AndroidSchedulers .mainThread ())
119
- .subscribe (
120
- responseBody -> {},
121
- throwable -> Log .e (TAG , throwable .getMessage ())
122
- );
118
+ addUser (player );
123
119
}
124
120
125
121
// hide button on login
@@ -139,6 +135,23 @@ public void onConnected(@Nullable Bundle bundle) {
139
135
finish ();
140
136
}
141
137
138
+ private void addUser (website .bloop .app .api .Player player ) {
139
+ SharedPreferences prefs = getSharedPreferences (BLOOP_PREFERENCE_FILE , Context .MODE_PRIVATE );
140
+ mApplication .getService ().addPlayer (player )
141
+ .subscribeOn (Schedulers .newThread ())
142
+ .observeOn (AndroidSchedulers .mainThread ())
143
+ .subscribe (
144
+ responseBody -> {
145
+ String token = prefs .getString (BloopFirebaseInstanceIDService .PREF_FIREBASE_TOKEN , "" );
146
+ if (token .equals ("" )) {
147
+ token = FirebaseInstanceId .getInstance ().getToken ();
148
+ }
149
+ mApplication .sendFirebaseRegistrationToServer (token );
150
+ },
151
+ throwable -> Log .e (TAG , throwable .getMessage ())
152
+ );
153
+ }
154
+
142
155
@ Override
143
156
public void onConnectionSuspended (int i ) {
144
157
// Attempt to reconnect
0 commit comments