kalehv
10/9/2016 - 10:20 PM

Firebase UI Google Login LaunchActivity

Firebase UI Google Login LaunchActivity

// http://bit.ly/firebaseauthlogin

package me.kalehv.whatever;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.firebase.ui.auth.AuthUI;
import com.google.firebase.auth.FirebaseAuth;

public class LaunchActivity extends AppCompatActivity {

    private static final int RC_SIGN_IN = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // No setContentView(). This activity does not need a view.
        super.onCreate(savedInstanceState);
        if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            // signed in
            startHomeActivityAndFinishThis();
        } else {
            // not signed in
            startActivityForResult(
                    AuthUI.getInstance()
                            .createSignInIntentBuilder()
                            .setIsSmartLockEnabled(!BuildConfig.DEBUG)
                            .setTheme(R.style.AppTheme_FirebaseSignIn)
                            .setProviders(AuthUI.GOOGLE_PROVIDER)
                            .build(),
                    RC_SIGN_IN);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            if (resultCode == RESULT_OK) {
                startHomeActivityAndFinishThis();
                finish();
            } else {
                Toast.makeText(this, "Error! Try signing in after some time", Toast.LENGTH_LONG).show();
            }
        }
    }

    private void startHomeActivityAndFinishThis() {
        Intent nextIntent = new Intent(this, MainActivity.class);
        startActivity(nextIntent);

        finish();
    }
}