Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Last active August 7, 2025 21:49
Show Gist options
  • Select an option

  • Save alvareztech/8c38122832535b20f4afc42c5b0b9366 to your computer and use it in GitHub Desktop.

Select an option

Save alvareztech/8c38122832535b20f4afc42c5b0b9366 to your computer and use it in GitHub Desktop.

Revisions

  1. Daniel Alvarez revised this gist Jul 4, 2016. 2 changed files with 31 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions TodayApp.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    package tech.alvarez.today;

    import android.app.Application;

    import com.facebook.FacebookSdk;
    import com.facebook.appevents.AppEventsLogger;

    /**
    * Created by Daniel Alvarez on 29/6/16.
    * Copyright © 2016 Alvarez.tech. All rights reserved.
    */
    public class TodayApp extends Application {

    @Override
    public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
    }
    }
    11 changes: 11 additions & 0 deletions strings.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <resources>
    <string name="app_name">Today</string>

    <string name="facebook_app_id">TU_ID_APP</string>

    <string name="welcome">Bienvenido</string>
    <string name="cancel_login">Se canceló la operación</string>
    <string name="error_login">Ocurrió un error al ingresar</string>
    <string name="logout">Cerrar sesión</string>
    <string name="firebase_error_login">Error al iniciar sesión en Firebase</string>
    </resources>
  2. Daniel Alvarez revised this gist Jul 4, 2016. 4 changed files with 13 additions and 14 deletions.
    14 changes: 7 additions & 7 deletions LoginActivity.java
    Original file line number Diff line number Diff line change
    @@ -39,10 +39,14 @@ protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    progressBar = (ProgressBar) findViewById(R.id.progressBar);

    callbackManager = CallbackManager.Factory.create();

    loginButton = (LoginButton) findViewById(R.id.loginButton);

    loginButton.setReadPermissions(Arrays.asList("email"));

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
    @@ -70,8 +74,6 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    }
    }
    };

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    }

    private void handleFacebookAccessToken(AccessToken accessToken) {
    @@ -83,7 +85,7 @@ private void handleFacebookAccessToken(AccessToken accessToken) {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
    if (!task.isSuccessful()) {
    Toast.makeText(getApplicationContext(), R.string.error_firebase_login, Toast.LENGTH_LONG).show();
    Toast.makeText(getApplicationContext(), R.string.firebase_error_login, Toast.LENGTH_LONG).show();
    }
    progressBar.setVisibility(View.GONE);
    loginButton.setVisibility(View.VISIBLE);
    @@ -112,8 +114,6 @@ protected void onStart() {
    @Override
    protected void onStop() {
    super.onStop();
    if (firebaseAuthListener != null) {
    firebaseAuth.removeAuthStateListener(firebaseAuthListener);
    }
    firebaseAuth.removeAuthStateListener(firebaseAuthListener);
    }
    }
    }
    9 changes: 4 additions & 5 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@

    public class MainActivity extends AppCompatActivity {

    private TextView nombreTextView;
    private TextView nameTextView;
    private TextView emailTextView;
    private TextView uidTextView;

    @@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nombreTextView = (TextView) findViewById(R.id.nameTextView);
    nameTextView = (TextView) findViewById(R.id.nameTextView);
    emailTextView = (TextView) findViewById(R.id.emailTextView);
    uidTextView = (TextView) findViewById(R.id.uidTextView);

    @@ -32,10 +32,9 @@ protected void onCreate(Bundle savedInstanceState) {
    String name = user.getDisplayName();
    String email = user.getEmail();
    Uri photoUrl = user.getPhotoUrl();

    String uid = user.getUid();

    nombreTextView.setText(name);
    nameTextView.setText(name);
    emailTextView.setText(email);
    uidTextView.setText(uid);
    } else {
    @@ -54,4 +53,4 @@ public void logout(View view) {
    LoginManager.getInstance().logOut();
    goLoginScreen();
    }
    }
    }
    2 changes: 1 addition & 1 deletion activity_main.xml
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@
    android:id="@+id/emailTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/nameTextView"
    android:layout_above="@id/nameTextView"
    android:layout_centerHorizontal="true"
    android:text="Email"
    android:textSize="16sp" />
    2 changes: 1 addition & 1 deletion build.gradle
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,4 @@ dependencies {
    compile 'com.google.firebase:firebase-auth:9.0.2'
    }

    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'com.google.gms.google-services'
  3. Daniel Alvarez revised this gist Jul 4, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions colors.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <color name="colorPrimary">#4CAF50</color>
    <color name="colorPrimaryDark">#388E3C</color>
    <color name="colorAccent">#FFD740</color>
    </resources>
  4. Daniel Alvarez revised this gist Jul 4, 2016. 5 changed files with 192 additions and 29 deletions.
    54 changes: 25 additions & 29 deletions LoginActivity.java
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    import com.facebook.AccessToken;
    @@ -31,7 +32,7 @@ public class LoginActivity extends AppCompatActivity {
    private FirebaseAuth firebaseAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthListener;

    public static final String TAG = "TODAY";
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    @@ -41,16 +42,10 @@ protected void onCreate(Bundle savedInstanceState) {
    callbackManager = CallbackManager.Factory.create();

    loginButton = (LoginButton) findViewById(R.id.loginButton);

    firebaseAuth = FirebaseAuth.getInstance();

    loginButton.setReadPermissions("email", "public_profile");

    loginButton.setReadPermissions(Arrays.asList("email"));
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
    // goMainScreen();
    Log.d("TODAY", "onSuccess");
    handleFacebookAccessToken(loginResult.getAccessToken());
    }

    @@ -65,35 +60,48 @@ public void onError(FacebookException error) {
    }
    });

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    FirebaseUser user = firebaseAuth.getCurrentUser();
    if (user != null) {
    Log.d("TODAY", "OK FIREBASE" + user.getUid());
    } else {
    Log.d("TODAY", "NO LOGIN");
    goMainScreen();
    }
    }
    };

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    }

    private void handleFacebookAccessToken(AccessToken accessToken) {
    Log.d("TODAY", "handleFacebookAccessToken");
    progressBar.setVisibility(View.VISIBLE);
    loginButton.setVisibility(View.GONE);

    AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
    firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
    Log.d("TODAY", "signInWithCredential:onComplete:" + task.isSuccessful());

    if (!task.isSuccessful()) {
    Log.d("TODAY", "fallo:" + task.isSuccessful());
    Toast.makeText(getApplicationContext(), R.string.error_firebase_login, Toast.LENGTH_LONG).show();
    }
    progressBar.setVisibility(View.GONE);
    loginButton.setVisibility(View.VISIBLE);
    }
    });
    }

    private void goMainScreen() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }

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

    @Override
    protected void onStart() {
    @@ -108,16 +116,4 @@ protected void onStop() {
    firebaseAuth.removeAuthStateListener(firebaseAuthListener);
    }
    }

    private void goMainScreen() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    }
    }
    57 changes: 57 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    package tech.alvarez.today;

    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;

    import com.facebook.login.LoginManager;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.FirebaseUser;

    public class MainActivity extends AppCompatActivity {

    private TextView nombreTextView;
    private TextView emailTextView;
    private TextView uidTextView;

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

    nombreTextView = (TextView) findViewById(R.id.nameTextView);
    emailTextView = (TextView) findViewById(R.id.emailTextView);
    uidTextView = (TextView) findViewById(R.id.uidTextView);

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

    if (user != null) {
    String name = user.getDisplayName();
    String email = user.getEmail();
    Uri photoUrl = user.getPhotoUrl();

    String uid = user.getUid();

    nombreTextView.setText(name);
    emailTextView.setText(email);
    uidTextView.setText(uid);
    } else {
    goLoginScreen();
    }
    }

    private void goLoginScreen() {
    Intent intent = new Intent(this, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }

    public void logout(View view) {
    FirebaseAuth.getInstance().signOut();
    LoginManager.getInstance().logOut();
    goLoginScreen();
    }
    }
    31 changes: 31 additions & 0 deletions activity_login.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="tech.alvarez.today.LoginActivity">

    <com.facebook.login.widget.LoginButton
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" />

    <ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:indeterminate="true"
    android:visibility="gone" />

    </RelativeLayout>
    46 changes: 46 additions & 0 deletions activity_main.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="tech.alvarez.today.MainActivity">

    <TextView
    android:id="@+id/nameTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Nombre"
    android:textSize="20sp" />

    <TextView
    android:id="@+id/emailTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/nameTextView"
    android:layout_centerHorizontal="true"
    android:text="Email"
    android:textSize="16sp" />

    <TextView
    android:id="@+id/uidTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/nameTextView"
    android:layout_centerHorizontal="true"
    android:text="UID"
    android:textSize="14sp" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="logout"
    android:text="@string/logout" />
    </RelativeLayout>
    33 changes: 33 additions & 0 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
    applicationId "tech.alvarez.today"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    }

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'

    compile 'com.facebook.android:facebook-android-sdk:[4,5)'

    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-auth:9.0.2'
    }

    apply plugin: 'com.google.gms.google-services'
  5. Daniel Alvarez created this gist Jun 28, 2016.
    123 changes: 123 additions & 0 deletions LoginActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    package tech.alvarez.today;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.widget.Toast;

    import com.facebook.AccessToken;
    import com.facebook.CallbackManager;
    import com.facebook.FacebookCallback;
    import com.facebook.FacebookException;
    import com.facebook.login.LoginResult;
    import com.facebook.login.widget.LoginButton;
    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.FacebookAuthProvider;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.FirebaseUser;

    import java.util.Arrays;

    public class LoginActivity extends AppCompatActivity {

    private LoginButton loginButton;
    private CallbackManager callbackManager;

    private FirebaseAuth firebaseAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthListener;

    public static final String TAG = "TODAY";

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

    callbackManager = CallbackManager.Factory.create();

    loginButton = (LoginButton) findViewById(R.id.loginButton);

    firebaseAuth = FirebaseAuth.getInstance();

    loginButton.setReadPermissions("email", "public_profile");

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
    // goMainScreen();
    Log.d("TODAY", "onSuccess");
    handleFacebookAccessToken(loginResult.getAccessToken());
    }

    @Override
    public void onCancel() {
    Toast.makeText(getApplicationContext(), R.string.cancel_login, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(FacebookException error) {
    Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_SHORT).show();
    }
    });

    firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    FirebaseUser user = firebaseAuth.getCurrentUser();
    if (user != null) {
    Log.d("TODAY", "OK FIREBASE" + user.getUid());
    } else {
    Log.d("TODAY", "NO LOGIN");
    }
    }
    };
    }

    private void handleFacebookAccessToken(AccessToken accessToken) {
    Log.d("TODAY", "handleFacebookAccessToken");
    AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
    firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
    Log.d("TODAY", "signInWithCredential:onComplete:" + task.isSuccessful());

    if (!task.isSuccessful()) {
    Log.d("TODAY", "fallo:" + task.isSuccessful());
    }
    }
    });

    }


    @Override
    protected void onStart() {
    super.onStart();
    firebaseAuth.addAuthStateListener(firebaseAuthListener);
    }

    @Override
    protected void onStop() {
    super.onStop();
    if (firebaseAuthListener != null) {
    firebaseAuth.removeAuthStateListener(firebaseAuthListener);
    }
    }

    private void goMainScreen() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }

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