hunkyhari

FireBase Auth UI

Mar 8th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. package com.shreesoftsolutions.apps.thescribbledstories;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.design.widget.FloatingActionButton;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.util.Log;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.Toast;
  14.  
  15. import com.firebase.ui.auth.AuthUI;
  16. import com.firebase.ui.auth.ErrorCodes;
  17. import com.firebase.ui.auth.IdpResponse;
  18. import com.firebase.ui.auth.ResultCodes;
  19. import com.google.firebase.FirebaseApp;
  20. import com.google.firebase.auth.FirebaseAuth;
  21. import com.shreesoftsolutions.apps.thescribbledstories.Utils.MenuNavigation;
  22.  
  23. import java.util.Arrays;
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.     private View mRootView;
  27.     private final String TAG = "MainActivity";
  28.     private FirebaseAuth auth;
  29.     private FirebaseApp firebaseApp;
  30.     private static final int RC_SIGN_IN = 123;
  31.     private MenuNavigation menuNavigation;
  32.  
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_main);
  37.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  38.         setSupportActionBar(toolbar);
  39.  
  40.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  41.         fab.setOnClickListener(new View.OnClickListener() {
  42.             @Override
  43.             public void onClick(View view) {
  44.                 Log.d(TAG,"Starting Capture Text");
  45.                 startActivity(new Intent(view.getContext(), CaptureText.class));
  46.             }
  47.         });
  48.  
  49.         firebaseApp = FirebaseApp.initializeApp(getApplicationContext());
  50.         auth = FirebaseAuth.getInstance();
  51.         checkLoginStatus();
  52.         menuNavigation = new MenuNavigation(this);
  53.     }
  54.  
  55.     @Override
  56.     protected void onResume() {
  57.         super.onResume();
  58.         checkLoginStatus();
  59.     }
  60.  
  61.     private void checkLoginStatus(){
  62.         if(auth.getCurrentUser() != null){
  63.             //already signed in
  64.             // Keep the user on this page
  65.         }else{
  66.             //Log the user in
  67.             startActivityForResult(
  68.                     AuthUI.getInstance(firebaseApp)
  69.                             .createSignInIntentBuilder()
  70.                             .setProviders(Arrays.asList(
  71.                                     new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()
  72.                             ))
  73.                             .build(),
  74.                     RC_SIGN_IN);
  75.         }
  76.     }
  77.  
  78.     private void callNextActivity(){   
  79.         startActivity(new Intent(this, ProfileActivity.class));
  80.     }
  81.  
  82.     @Override
  83.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  84.         super.onActivityResult(requestCode, resultCode, data);
  85.         // RC_SIGN_IN is the request code you passed into startActivityForResult(...) when starting the sign in flow.
  86.         if (requestCode == RC_SIGN_IN) {
  87.             IdpResponse response = IdpResponse.fromResultIntent(data);
  88.  
  89.             // Successfully signed in
  90.             if (resultCode == ResultCodes.OK) {
  91.                 //Signed in for the first Time.
  92.                 //Now show the profile page.
  93.                 callNextActivity();
  94.                 finish();
  95.                 return;
  96.             } else {
  97.                 // Sign in failed
  98.                 if (response == null) {
  99.                     // User pressed back button
  100.                     showSnackbar(R.string.sign_in_cancelled);
  101.                     return;
  102.                 }
  103.  
  104.                 if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
  105.                     showSnackbar(R.string.no_internet_connection);
  106.                     return;
  107.                 }
  108.  
  109.                 if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
  110.                     showSnackbar(R.string.unknown_error);
  111.                     return;
  112.                 }
  113.             }
  114.  
  115.             showSnackbar(R.string.unknown_sign_in_response);
  116.         }
  117.     }
  118.  
  119.  
  120.     private void showSnackbar(int text){
  121.         Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
  122.     }
  123.  
  124.     @Override
  125.     public boolean onCreateOptionsMenu(Menu menu) {
  126.         // Inflate the menu; this adds items to the action bar if it is present.
  127.         getMenuInflater().inflate(R.menu.menu_main, menu);
  128.         return true;
  129.     }
  130.  
  131.     @Override
  132.     public boolean onOptionsItemSelected(MenuItem item){
  133.         return menuNavigation.handleMenuOption(item);
  134.     }
  135. }
Add Comment
Please, Sign In to add comment