public class LoginActivity extends AppCompatActivity {
private static final int RC_SIGN_IN = 1;
@BindView(R.id.activity_login_google_button)
SignInButton googleButton;
@BindView(R.id.activity_login_progress)
ProgressBar progress;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseAuth mAuth;
@State
boolean isLoading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
Icepick.restoreInstanceState(this, savedInstanceState);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(getString(R.string.default_web_client_id))
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("@@@@", "onAuthStateChanged:signed_in:" + user.getUid());
Intent home = new Intent(LoginActivity.this, MainActivity.class);
startActivity(home);
finish();
} else {
// User is signed out
Log.d("@@@@", "onAuthStateChanged:signed_out");
}
}
};
displayLoadingState();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Icepick.saveInstanceState(this, outState);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
@Override
public void onStart() {
super.onStart();
if (mAuthListener != null) {
mAuth.addAuthStateListener(mAuthListener);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
hideProgress();
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleGoogleSignInResult(result);
}
}
private void hideProgress() {
isLoading = false;
displayLoadingState();
}
private void displayLoadingState() {
progress.setVisibility(isLoading ? VISIBLE : GONE);
googleButton.setVisibility(!isLoading ? VISIBLE : GONE);
}
private void showProgress() {
isLoading = true;
displayLoadingState();
}
private void handleGoogleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
Toast.makeText(this, R.string.error_google_sign_in, Toast.LENGTH_SHORT).show();
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("@@@@", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("@@@@", "firebaseAuthWithGoogleComplete:" + task.isSuccessful());
Needle.onBackgroundThread().execute(new Runnable() {
@Override
public void run() {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
} catch (IOException e) {
Log.d("@@@@", "deleteInstanceIdCatch: " + e.getMessage());
}
}
});
if (!task.isSuccessful()) {
Log.w("@@@@", "firebaseAuthWithGoogleFailed", task.getException());
Toast.makeText(LoginActivity.this, R.string.error_google_sign_in, Toast.LENGTH_SHORT).show();
}
}
});
}
@OnClick(R.id.activity_login_google_button)
public void attemptGoogleSignIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
showProgress();
}
}
this is the login activity code in which I'm getting an error that cannot find symbol class FirebaseInstanceIdService I have attached an image for reference
this is for my final year project which is a chatting based application because of this process, I cannot able to generate APK but Gradle sync is successful.