I accidentally closed the previous chat window and lost the history.

Basically I'm trying to put functions into an utility called FirebaseAuthUtil:


These were two of the older functions in MainActivity:
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //From googleSignIn();
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);


                AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);

                Objects.requireNonNull(mAuth.getCurrentUser()).linkWithCredential(credential)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> linkTask) {
                                if (linkTask.isSuccessful()) {
                                    FirebaseUser firebaseUser = linkTask.getResult().getUser();
                                    Log.d("TagDB", "Linking the anonymous account with the Google account was successful");
                                    // TODO: Update the UI with the user’s information or proceed to the main part of your app
                                } else {
                                    if (linkTask.getException() instanceof FirebaseAuthUserCollisionException) {
                                        //The Google account is already in use. Sync data.
                                        Log.d("TagDB", "The Google account is already in use. Syncing data.");
                                        signInWithCredential(credential);
                                    }
                                }
                            }
                        });
            } catch (ApiException e) {
                // Google Sign In failed
                Toast.makeText(this, "Google Sign In failed, try again!", Toast.LENGTH_SHORT).show();
                Log.d("TagDB", Objects.requireNonNull(e.getMessage()));
            }
        }
    }
    private void signInWithCredential(AuthCredential credential) {
        final FirebaseUser anonymousUser = mAuth.getCurrentUser();
        final String anonymousUserId = mAuth.getUid();

        mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> signInTask) {
                if (signInTask.isSuccessful()) {
                    //Sync is managed in readFromBD
                    FirebaseDatabaseUtil.readFromDB(MainActivity.this, true);

                    //Delete the anonymous user’s authentication record
                    if (anonymousUser != null && !anonymousUser.getUid().equals(mAuth.getUid())) {
                        anonymousUser.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Log.d("TagDB", "Anonymous user auth record deleted successfully.");
                                } else {
                                    Log.w("TagDB", "Failed to delete anonymous user auth record.", task.getException());
                                }
                            }
                        });

                        //Delete the anonymous user’s database document
                        usersRef.document(Objects.requireNonNull(anonymousUserId)).delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Log.d("TagDB", "Deleted anonymous user data from Firestore.");
                                } else {
                                    Log.w("TagDB", "Error deleting anonymous user data from Firestore.", task.getException());
                                }
                            }
                        });
                    }
                } else {
                    Toast.makeText(MainActivity.this,"Google Sign In failed, try again!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


And this way your new recommendation that I've pasted into the FirebaseAuthUtil:

    public static void initializeGoogleSignIn(Context context, String defaultWebClientId) {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(defaultWebClientId)
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(context, gso);
    }

    public static Intent getGoogleSignInIntent() {
        if (mGoogleSignInClient == null) {
            return null;
        }
        return mGoogleSignInClient.getSignInIntent();
    }

    public static void handleGoogleSignInResult(Intent data, final OnGoogleSignInCompleteListener listener) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);

            AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
            FirebaseUser currentUser = mAuth.getCurrentUser();

            if (currentUser != null) {
                currentUser.linkWithCredential(credential)
                        .addOnCompleteListener(task1 -> {
                            if (task1.isSuccessful()) {
                                // … Callback success with FirebaseUser …
                                listener.onGoogleSignInComplete(task1.getResult().getUser(), false);
                            } else {
                                Exception e = task1.getException();
                                if (e instanceof FirebaseAuthUserCollisionException) {
                                    // … Callback for collision …
                                    listener.onGoogleSignInCollision(credential);
                                } else {
                                    listener.onGoogleSignInFailed(e);
                                }
                            }
                        });
            }
        } catch (ApiException e) {
            listener.onGoogleSignInFailed(e);
        }
    }

    // signInWithCredential logic ported as-is with slight modifications for static context
    public static void signInWithCredential(AuthCredential credential, final OnGoogleSignInCompleteListener listener) {
        mAuth.signInWithCredential(credential).addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                // … Callback success …
                listener.onGoogleSignInComplete(task.getResult().getUser(), true);
            } else {
                Exception e = task.getException();
                listener.onGoogleSignInFailed(e);
            }
        });
    }

    // … Other methods, such as saveToDB …

    public interface OnGoogleSignInCompleteListener {
        void onGoogleSignInComplete(FirebaseUser user, boolean isNewUser);
        void onGoogleSignInCollision(AuthCredential credential);
        void onGoogleSignInFailed(Exception e);
    }




Now there's only one problem, I don't know hot to use this in MainActivity. I've already added //Sign In with Google
        FirebaseAuthUtil.initializeGoogleSignIn(this, getString(R.string.default_web_client_id)); to onCreate in MainActivity

Please help me