From 14205b2b0ad1be619a6c876c93f5bf46a94ad40e Mon Sep 17 00:00:00 2001 From: cmclark00 Date: Wed, 2 Apr 2025 19:52:02 -0400 Subject: [PATCH] Enhance: Update Google Play Games integration and versioning - Incremented version code to 5 and version name to 0.4 in build.gradle. - Removed Google Play sign-in buttons from both portrait and landscape layouts. - Updated HighScoresActivity to hide the leaderboard button. - Improved score submission process in GooglePlayGamesManager with forced sign-in and user feedback. - Enhanced MainActivity to submit scores to Google Play Games upon game over. --- app/build.gradle | 4 +- .../com/pixelmintdrop/HighScoresActivity.kt | 7 +- .../java/com/pixelmintdrop/MainActivity.kt | 18 +++- .../model/GooglePlayGamesManager.kt | 96 +++++++++++++++---- .../main/res/layout-land/activity_main.xml | 14 --- app/src/main/res/layout/activity_main.xml | 13 --- 6 files changed, 97 insertions(+), 55 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 54b1a06..872d5a3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,8 +11,8 @@ android { applicationId "com.pixelmintdrop" minSdk 30 targetSdk 35 - versionCode 3 - versionName "0.2" + versionCode 5 + versionName "0.4" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/pixelmintdrop/HighScoresActivity.kt b/app/src/main/java/com/pixelmintdrop/HighScoresActivity.kt index 4dc6639..e38c248 100644 --- a/app/src/main/java/com/pixelmintdrop/HighScoresActivity.kt +++ b/app/src/main/java/com/pixelmintdrop/HighScoresActivity.kt @@ -76,10 +76,8 @@ class HighScoresActivity : AppCompatActivity() { finish() } - // Set up leaderboard button - binding.leaderboardButton?.setOnClickListener { - showGlobalLeaderboard() - } + // Hide the leaderboard button + binding.leaderboardButton?.visibility = View.GONE } catch (e: Exception) { Log.e("HighScoresActivity", "Error in onCreate", e) // Show an error message if necessary, or finish gracefully @@ -118,7 +116,6 @@ class HighScoresActivity : AppCompatActivity() { // Apply theme to buttons binding.backButton.setTextColor(textColor) - binding.leaderboardButton?.setTextColor(textColor) // Update adapter theme highScoreAdapter.applyTheme(themeId) diff --git a/app/src/main/java/com/pixelmintdrop/MainActivity.kt b/app/src/main/java/com/pixelmintdrop/MainActivity.kt index f84ed8f..1bf5875 100644 --- a/app/src/main/java/com/pixelmintdrop/MainActivity.kt +++ b/app/src/main/java/com/pixelmintdrop/MainActivity.kt @@ -473,6 +473,10 @@ class MainActivity : AppCompatActivity(), Log.d(TAG, "[GameOverDebug] Triples: $lastSessionTriples (from manager: ${statsManager.getSessionTriples()})") Log.d(TAG, "[GameOverDebug] Quads: $lastSessionQuads (from manager: ${statsManager.getSessionQuads()})") + // *** Submit score to Google Play Games *** + Log.d(TAG, "Attempting to submit end-game score to Play Games: $lastSessionScore") + highScoreManager.getGooglePlayGamesManager().submitScore(lastSessionScore.toLong(), this@MainActivity) + // End the session (updates lifetime stats) statsManager.endSession() @@ -1077,6 +1081,10 @@ class MainActivity : AppCompatActivity(), Log.d(TAG, "[GameOverDebug] Triples: $lastSessionTriples (from manager: ${statsManager.getSessionTriples()})") Log.d(TAG, "[GameOverDebug] Quads: $lastSessionQuads (from manager: ${statsManager.getSessionQuads()})") + // *** Submit score to Google Play Games *** + Log.d(TAG, "Attempting to submit end-game score to Play Games: $lastSessionScore") + highScoreManager.getGooglePlayGamesManager().submitScore(lastSessionScore.toLong(), this@MainActivity) + // End the session (updates lifetime stats) statsManager.endSession() @@ -2243,20 +2251,24 @@ class MainActivity : AppCompatActivity(), * Handles sign-in to Google Play Games Services */ private fun signInToPlayGames() { + Log.d(TAG, "Attempting to sign into Google Play Games") gamesSignInClient.isAuthenticated.addOnCompleteListener { task -> val isAuthenticated = task.isSuccessful && task.result.isAuthenticated if (!isAuthenticated) { - // Silent sign-in failed, offer to sign in explicitly - Log.d(TAG, "Not authenticated with Play Games, will prompt user later") + // Silent sign-in failed, but don't prompt right away + Log.d(TAG, "Not authenticated with Play Games, will prompt for sign-in when needed") - // We don't show UI for this immediately, we'll add a button to the pause menu + // Optional: Add a sign-in button to your menu or UI for manual sign-in + // We'll attempt sign-in when accessing leaderboards or submitting scores } else { Log.d(TAG, "Already authenticated with Play Games") // If we were already authenticated, make sure scores are synced val lastHighScore = highScoreManager.getHighScores().maxByOrNull { it.score } lastHighScore?.let { + // Submit the high score if we're already authenticated + Log.d(TAG, "User is signed in, submitting last high score: ${it.score}") highScoreManager.getGooglePlayGamesManager().submitScore(it.score.toLong(), this) } } diff --git a/app/src/main/java/com/pixelmintdrop/model/GooglePlayGamesManager.kt b/app/src/main/java/com/pixelmintdrop/model/GooglePlayGamesManager.kt index aff48d1..d88229a 100644 --- a/app/src/main/java/com/pixelmintdrop/model/GooglePlayGamesManager.kt +++ b/app/src/main/java/com/pixelmintdrop/model/GooglePlayGamesManager.kt @@ -62,18 +62,48 @@ class GooglePlayGamesManager(private val context: Context) { // Submit score to leaderboard (requires an activity for potential sign-in) fun submitScore(score: Long, activity: Activity? = null) { - val account = GoogleSignIn.getLastSignedInAccount(context) - if (account == null) { - Log.w(TAG, "Not signed in, score will not be submitted to Google Play Games") + if (activity == null) { + Log.w(TAG, "Activity context is required for score submission.") return } + + Log.d(TAG, "Attempting to submit score: $score") - try { - val leaderboardsClient = PlayGames.getLeaderboardsClient(activity ?: return) - leaderboardsClient.submitScore(LEADERBOARD_ID, score) - Log.d(TAG, "Score $score submitted to leaderboard $LEADERBOARD_ID") - } catch (e: Exception) { - Log.e(TAG, "Error submitting score to leaderboard", e) + // Always force a sign-in to ensure we have a valid account + val signInClient = PlayGames.getGamesSignInClient(activity) + signInClient.signIn().addOnCompleteListener { signInTask -> + if (signInTask.isSuccessful) { + Log.d(TAG, "Sign-in successful, retrieving account for score submission") + + // After sign-in, get the account and submit score + try { + // Use PlayGames API directly to get client and submit score + val leaderboardsClient = PlayGames.getLeaderboardsClient(activity) + leaderboardsClient.submitScore(LEADERBOARD_ID, score) + Log.d(TAG, "Successfully submitted score $score to leaderboard $LEADERBOARD_ID") + + // Show confirmation to user + android.widget.Toast.makeText( + activity, + "Score submitted to leaderboard", + android.widget.Toast.LENGTH_SHORT + ).show() + } catch (e: Exception) { + Log.e(TAG, "Error submitting score after sign-in", e) + android.widget.Toast.makeText( + activity, + "Failed to submit score to leaderboard", + android.widget.Toast.LENGTH_SHORT + ).show() + } + } else { + Log.e(TAG, "Sign-in failed, unable to submit score", signInTask.exception) + android.widget.Toast.makeText( + activity, + "Sign-in required to submit score to leaderboard", + android.widget.Toast.LENGTH_SHORT + ).show() + } } } @@ -81,20 +111,50 @@ class GooglePlayGamesManager(private val context: Context) { fun showLeaderboard(activity: Activity, listener: LeaderboardIntentListener) { Log.d(TAG, "showLeaderboard called in GooglePlayGamesManager") - Log.d(TAG, "Attempting to get LeaderboardsClient (assuming user is signed in)") + // First check if already authenticated + PlayGames.getGamesSignInClient(activity).isAuthenticated.addOnCompleteListener { authTask -> + val isAuthenticated = authTask.isSuccessful && authTask.result.isAuthenticated + + if (isAuthenticated) { + // Already authenticated, get leaderboard + getLeaderboardIntent(activity, listener) + } else { + // Need to authenticate first + Log.d(TAG, "User not authenticated for leaderboard, attempting sign-in") + PlayGames.getGamesSignInClient(activity).signIn().addOnCompleteListener { signInTask -> + if (signInTask.isSuccessful) { + // Successfully signed in, now get leaderboard + Log.d(TAG, "Sign-in successful, getting leaderboard") + getLeaderboardIntent(activity, listener) + } else { + // Failed to sign in + Log.e(TAG, "Failed to sign in for leaderboard access", signInTask.exception) + listener.onLeaderboardIntentFailure( + Exception("Sign-in required to view leaderboard", signInTask.exception) + ) + + // Inform user + android.widget.Toast.makeText( + activity, + "Sign-in required to view leaderboard", + android.widget.Toast.LENGTH_SHORT + ).show() + } + } + } + } + } + + // Private helper to get leaderboard intent + private fun getLeaderboardIntent(activity: Activity, listener: LeaderboardIntentListener) { + Log.d(TAG, "Attempting to get LeaderboardsClient") try { val leaderboardsClient = PlayGames.getLeaderboardsClient(activity) Log.d(TAG, "LeaderboardsClient obtained, attempting to get leaderboard intent for ID: $LEADERBOARD_ID") leaderboardsClient .getLeaderboardIntent(LEADERBOARD_ID) .addOnSuccessListener { intent -> - Log.d(TAG, "Successfully obtained leaderboard intent. Calling listener.") - // try { // Removed - Activity start is now handled by MainActivity - // activity.startActivity(intent) - // Log.d(TAG, "Leaderboard activity started successfully.") - // } catch (e: Exception) { - // Log.e(TAG, "Error starting leaderboard activity", e) - // } + Log.d(TAG, "Successfully obtained leaderboard intent. Calling listener.") listener.onLeaderboardIntentSuccess(intent) } .addOnFailureListener { e -> @@ -102,7 +162,7 @@ class GooglePlayGamesManager(private val context: Context) { listener.onLeaderboardIntentFailure(e) } } catch (e: Exception) { - Log.e(TAG, "Error obtaining LeaderboardsClient or getting leaderboard intent", e) // Updated log message + Log.e(TAG, "Error obtaining LeaderboardsClient or getting leaderboard intent", e) listener.onLeaderboardIntentFailure(e) } } diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml index bd54ce7..13f79f2 100644 --- a/app/src/main/res/layout-land/activity_main.xml +++ b/app/src/main/res/layout-land/activity_main.xml @@ -452,20 +452,6 @@ android:textStyle="bold" android:fontFamily="sans-serif" android:textAllCaps="false" /> - - -