1818import android .support .v7 .app .AppCompatActivity ;
1919import android .support .v7 .widget .Toolbar ;
2020import android .transition .TransitionManager ;
21+ import android .util .Log ;
2122import android .util .TypedValue ;
2223import android .view .Menu ;
2324import android .view .MenuItem ;
3536import com .google .android .gms .games .AchievementsClient ;
3637import com .google .android .gms .games .Games ;
3738import com .google .android .gms .games .LeaderboardsClient ;
39+ import com .google .android .gms .tasks .OnCompleteListener ;
40+ import com .google .android .gms .tasks .Task ;
3841import com .google .firebase .analytics .FirebaseAnalytics ;
3942import com .google .firebase .auth .FirebaseAuth ;
4043import com .google .firebase .auth .FirebaseUser ;
44+ import com .google .firebase .auth .UserInfo ;
45+ import com .google .firebase .auth .UserProfileChangeRequest ;
4146import com .google .firebase .database .DataSnapshot ;
4247import com .google .firebase .database .DatabaseError ;
4348import com .google .firebase .database .DatabaseReference ;
@@ -68,6 +73,7 @@ public class DrawingActivity extends AppCompatActivity
6873 private static final int PIXEL_HEIGHT = 280 ;
6974 private static final int RC_ACHIEVEMENT_UI = 9003 ;
7075 private static final int RC_LEADERBOARD_UI = 9004 ;
76+ private static final int RC_SETTING_UI = 9005 ;
7177
7278 /* Variables */
7379 String currBatchName ;
@@ -101,6 +107,7 @@ public class DrawingActivity extends AppCompatActivity
101107 ProgressBar barSend ;
102108 FloatingActionButton fabSend ;
103109 TextView navUserEmail ;
110+ TextView navUserName ;
104111 TextView tvImageName ;
105112 TextView tvUserScore ;
106113
@@ -114,6 +121,8 @@ public class DrawingActivity extends AppCompatActivity
114121 NavigationView navView ;
115122 View navHeaderLayout ;
116123
124+ boolean isGoogleSignIn = false ;
125+
117126
118127 @ Override
119128 protected void onCreate (Bundle savedInstanceState ) {
@@ -138,6 +147,12 @@ protected void onCreate(Bundle savedInstanceState) {
138147 userUID = mUser .getUid ();
139148 userEmail = mUser .getEmail ();
140149
150+ for (UserInfo u : mUser .getProviderData ()) {
151+ if (u .getProviderId ().equals ("google.com" )) {
152+ isGoogleSignIn = true ;
153+ }
154+ }
155+
141156 // get drawing view from XML (where the finger writes the number)
142157 drawView = findViewById (R .id .draw );
143158
@@ -160,10 +175,23 @@ protected void onCreate(Bundle savedInstanceState) {
160175 navUserEmail .setText (userEmail );
161176 tvImageName = findViewById (R .id .tv_img_name );
162177
163- TextView navUserName = navHeaderLayout .findViewById (R .id .nav_drawer_name );
178+ navUserName = navHeaderLayout .findViewById (R .id .nav_drawer_name );
164179 String userName = mUser .getDisplayName ();
165- if (!(userName == null || userName .equals ("" ))) {
166- navUserName .setText (userName );
180+ if (userName == null || userName .equals ("" )) {
181+ /* New users gets the default name */
182+ userName = getString (R .string .default_user_name );
183+ UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest .Builder ()
184+ .setDisplayName (userName )
185+ .build ();
186+ mUser .updateProfile (profileUpdates );
187+ }
188+ navUserName .setText (userName );
189+
190+ if (!isGoogleSignIn ) {
191+ Menu menu = navView .getMenu ();
192+ menu .findItem (R .id .nav_achievements ).setEnabled (false );
193+ menu .findItem (R .id .nav_leaderboard ).setEnabled (false );
194+ menu .findItem (R .id .nav_gplay_setting ).setEnabled (false );
167195 }
168196
169197// FIXME get user profile photo
@@ -203,7 +231,9 @@ protected void onCreate(Bundle savedInstanceState) {
203231 /* Google Play Game */
204232 gAcct = GoogleSignIn .getLastSignedInAccount (this );
205233 if (gAcct != null ) {
206- // Games.getGamesClient(this, gAcct).setViewForPopups(clDrawMain);
234+ Games .getGamesClient (this , gAcct ).setViewForPopups (clDrawMain );
235+ }
236+ if (isGoogleSignIn ) {
207237 mAchClient = Games .getAchievementsClient (this , gAcct );
208238 mLeadClient = Games .getLeaderboardsClient (this , gAcct );
209239 }
@@ -212,10 +242,12 @@ protected void onCreate(Bundle savedInstanceState) {
212242 @ Override
213243 public boolean onNavigationItemSelected (@ NonNull MenuItem item ) {
214244 int id = item .getItemId ();
215- if (id == R .id .nav_achievements ) {
245+ if (id == R .id .nav_achievements && isGoogleSignIn ) {
216246 showAchievements ();
217- } else if (id == R .id .nav_leaderboard ) {
247+ } else if (id == R .id .nav_leaderboard && isGoogleSignIn ) {
218248 showLeaderBoard ();
249+ } else if (id == R .id .nav_gplay_setting && isGoogleSignIn ) {
250+ showGPlaySettings ();
219251 } else if (id == R .id .nav_logout ) {
220252 logOut ();
221253 } else if (id == R .id .nav_settings ) {
@@ -229,18 +261,20 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
229261 return true ;
230262 }
231263
264+
232265 private void showAchievements () {
233- if (gAcct != null ) {
234- mAchClient .getAchievementsIntent ()
235- .addOnSuccessListener ((intent -> startActivityForResult (intent , RC_ACHIEVEMENT_UI )));
236- }
266+ mAchClient .getAchievementsIntent ()
267+ .addOnSuccessListener ((intent -> startActivityForResult (intent , RC_ACHIEVEMENT_UI )));
237268 }
238269
239270 private void showLeaderBoard () {
240- if (gAcct != null ) {
241- mLeadClient .getLeaderboardIntent (getString (R .string .leaderboard_wurm_scores_id ))
242- .addOnSuccessListener ((intent -> startActivityForResult (intent , RC_LEADERBOARD_UI )));
243- }
271+ mLeadClient .getLeaderboardIntent (getString (R .string .leaderboard_wurm_scores_id ))
272+ .addOnSuccessListener ((intent -> startActivityForResult (intent , RC_LEADERBOARD_UI )));
273+ }
274+
275+ private void showGPlaySettings () {
276+ Games .getGamesClient (this , gAcct ).getSettingsIntent ()
277+ .addOnSuccessListener ((intent -> startActivityForResult (intent , RC_SETTING_UI )));
244278 }
245279
246280 private void showSettings () {
@@ -277,6 +311,17 @@ private void feedbackScreen() {
277311 // say he presses home button and then comes back to app, onResume() is called.
278312 protected void onResume () {
279313 drawView .onResume ();
314+ if (GoogleSignIn .getLastSignedInAccount (this ) == null ) {
315+ FirebaseAuth .getInstance ().signOut ();
316+ Intent i = new Intent (this , LoginActivity .class );
317+ startActivity (i );
318+ }
319+ mUser .reload ().addOnCompleteListener (task -> {
320+ if (task .isSuccessful ()) {
321+ navUserName .setText (mUser .getDisplayName ());
322+ navUserEmail .setText (mUser .getEmail ());
323+ }
324+ });
280325 super .onResume ();
281326 }
282327
0 commit comments