Gmail Integration using Android and Firebase


Gmail Integration using Android and Firebase


Output Screen









add dependency in Gradle
    
implementation 'com.google.firebase:firebase-auth:19.3.2'
implementation 'com.google.android.gms:play-services-auth:18.1.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.gms.common.SignInButton
android:id="@+id/google_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
/>
</androidx.constraintlayout.widget.ConstraintLayout>


activity_home_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:scaleType="centerCrop"
android:layout_gravity="center"
android:id="@+id/myimage"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myid"
android:layout_marginTop="20dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:onClick="logout"/>

</LinearLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firebasedemo">

<uses-permission-sdk-23 android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".SingleTask"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".HomePage"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

SingleTask.java

package com.example.firebasedemo;

import android.app.Application;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;

public class SingleTask extends Application {
private GoogleSignInClient mGoogleSignInClient;

@Override
public void onCreate() {
super.onCreate();
GoogleSignInOptions gso = new GoogleSignInOptions.
        Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}

public GoogleSignInClient getmGoogleSignInClient() {
return mGoogleSignInClient;
}
}


MainActivity.java

package com.example.firebasedemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.android.gms.common.SignInButton;

public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private SingleTask singleTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();

singleTask = (SingleTask) getApplication();

SignInButton signbutton = findViewById(R.id.google_button);
signbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}

@Override
protected void onStart() {
super.onStart();
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
goToHomePage();
}
}

private void signIn() {
Intent signInIntent = singleTask.getmGoogleSignInClient().
        getSignInIntent();
startActivityForResult(signInIntent, 1001);
}

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


if (requestCode == 1001) {
Task<GoogleSignInAccount> task = GoogleSignIn.
            getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w("error", "Google sign in failed", e);
}
}
}

private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.
        getCredential(idToken, null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
goToHomePage();
} else {
Log.w("error", "signInWithCredential:failure",
                        task.getException());

}
}
});
}

private void goToHomePage() {
Intent in = new Intent(MainActivity.this, HomePage.class);
startActivity(in);
finish();
}

}


HomePage.java

package com.example.firebasedemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomePage extends AppCompatActivity {
private FirebaseAuth mAuth;
private SingleTask singleTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);

ImageView image = findViewById(R.id.imageprofile);
TextView detail = findViewById(R.id.detail);

mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();

if (user != null) {
Glide.with(this).load(user.getPhotoUrl()).into(image);
detail.setText(user.getDisplayName() + "\n" +
    user.getEmail() + "\n" + user.getUid() + "\n" + user.getProviderId());
}

singleTask = (SingleTask) getApplication();

}

public void logout(View view) {
mAuth.signOut();
singleTask.getmGoogleSignInClient().signOut().
    addOnCompleteListener(new OnCompleteListener<Void>() {

@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Intent in = new Intent(HomePage.this, MainActivity.class);
startActivity(in);
finish();
}
}
});

}
}


3 comments:

CEEcrets said...

Valuable post. If you have a secrets to share then CEEcrets is highly secured Anonymous Social Network Apps for sharing secrets.

CEEcrets said...

Amazing post, CEEcrets is an interesting medium of sharing secrets. It is an Anonymous Social Media Apps for enjoying secrets.

NewzKast said...

It is an informative post.
Now get Trending news celebrity on the go with the best news app NewzKast.