Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bud.ohad.noticeboardeylon;
- import android.Manifest;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.graphics.drawable.Drawable;
- import android.graphics.drawable.ScaleDrawable;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.text.InputType;
- import android.text.method.PasswordTransformationMethod;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.List;
- public class Login extends Activity {
- private FirebaseAuth mAuth;
- Button btnLoggedIn;
- EditText etEmail, etPassword;
- Intent toEventList;
- CheckBox cbShowPassword;
- ProgressDialog dialog;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //starts alerts
- Job.scheduleJob(getApplicationContext());
- //dialog begins
- dialog=new ProgressDialog(this);
- dialog.setMessage("Authenticating please wait... \n\nIf this is taking too long check your internet connection");
- dialog.setCancelable(false);
- dialog.setInverseBackgroundForced(false);
- dialog.show();
- mAuth = FirebaseAuth.getInstance();
- btnLoggedIn = new Button(getApplicationContext());
- btnLoggedIn = findViewById(R.id.btnLoggedIn);
- cbShowPassword = findViewById(R.id.cbShowPassword);
- etEmail = new EditText(getApplicationContext());
- etEmail = (EditText) findViewById(R.id.etEmail);
- etPassword = new EditText(getApplicationContext());
- etPassword = (EditText) findViewById(R.id.etPassword);
- // register();
- btnLoggedIn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(dialog!=null){
- dialog.show();
- }
- etEmail.setError(null);
- etPassword.setError(null);
- boolean error =false;
- //retrieves user entered details
- final String retreivedEmail = etEmail.getText().toString().trim();
- final String retreivedPassword = etPassword.getText().toString();
- //Checks mail validity offline
- if(!BroardFunctions.isEmailValid(retreivedEmail)){
- etEmail.setError("Mail isn't valid");
- error=true;
- }
- //checks for password
- if(retreivedPassword.equals("")){
- etPassword.setError("Password cannot be empty");
- error=true;
- }
- if(!error) {
- /*
- * no error has been found
- */
- final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
- rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot snapshot) {
- mAuth = FirebaseAuth.getInstance();
- signIn(mAuth, retreivedEmail, retreivedPassword);// commence sign in
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Toast.makeText(getApplicationContext(), "Access to firebase canceled!",
- Toast.LENGTH_LONG).show();
- if (dialog != null) {
- dialog.dismiss();
- }
- }
- });
- }else{
- if(dialog!=null) {
- dialog.dismiss();
- }
- Toast.makeText(getApplicationContext(),"Invalid inputs",Toast.LENGTH_SHORT).show();
- }
- }
- });
- /*
- shows and hides password on toggle
- */
- cbShowPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked){
- etPassword.setTransformationMethod(null);
- }else{
- etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
- }
- }
- });
- }
- @Override
- public void onStart() {
- super.onStart();
- // Check if user is signed in (non-null) and update UI accordingly.
- FirebaseUser currentUser = mAuth.getCurrentUser();
- updateUI(currentUser);
- }
- /**
- *
- * @param mAuth Firebase auth
- * @param email the user entered email
- * @param password the user entered password
- */
- public void signIn(final FirebaseAuth mAuth, String email, String password) {
- mAuth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- // Sign in success, update UI with the signed-in user's information
- Log.d("5", "signInWithEmail:success");
- FirebaseUser user = mAuth.getCurrentUser();
- Toast.makeText(getApplicationContext(), "Authentication Successful!",
- Toast.LENGTH_SHORT).show();
- updateUI(user);//update ui
- } else {
- // If sign in fails, display a message to the user.
- Log.w("5", "signInWithEmail:failure", task.getException());
- Toast.makeText(getApplicationContext(), "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- updateUI(null);
- }
- }
- });
- }
- private void updateUI(final FirebaseUser user) {
- if (user == null) {
- if(dialog!=null){
- dialog.dismiss();
- }
- } else {
- final FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference ref = database.getReference();
- ref.child("Users").child(user.getUid()).addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- ServerUser currUser = dataSnapshot.getValue(ServerUser.class);
- Log.d("5", "Current user details: " + currUser.toString());
- if(dialog!=null){
- dialog.dismiss();
- }
- //move to all event activity
- toEventList = new Intent(getApplicationContext(), AllEvents.class);
- startActivity(toEventList);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- System.out.println("The read failed: " + databaseError.getCode());
- if(dialog!=null){
- dialog.dismiss();
- }
- }
- });
- }
- }
- /* private void register(){
- mAuth.createUserWithEmailAndPassword("arel601@gmail.com", "arel1234")
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- // Sign in success, update UI with the signed-in user's information
- // Log.d(TAG, "createUserWithEmail:success");
- final FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference ref1 = database.getReference();
- FirebaseUser user = mAuth.getCurrentUser();
- ref1.child("Users").child(user.getUid()).setValue(new ServerUser(user.getUid(),"Moshe","Smith",false,
- 1));
- updateUI(user);
- } else {
- // If sign in fails, display a message to the user.
- // Log.w(TAG, "createUserWithEmail:failure", task.getException());
- // Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
- // Toast.LENGTH_SHORT).show();
- updateUI(null);
- }
- // ...
- }
- });
- }
- */
- /**
- * overrides the back press fucntion
- */
- @Override
- public void onBackPressed() {
- Log.d("2","Back was pressed all events");
- //presents a dialog that asks if you would like to quit the application
- final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this);
- alertDialogBuilder.setTitle("Are you sure you want to exit?");
- alertDialogBuilder
- .setCancelable(true)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- //user want to quit
- dialog.cancel();
- Toast.makeText(getApplicationContext(),"Goodbye", Toast.LENGTH_LONG);
- exit();//quit
- }
- })
- .setNegativeButton("No, im not sure", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- //user doesn't want to quit
- }
- });
- AlertDialog alertDialog = alertDialogBuilder.create();
- alertDialog.show();
- }
- /**
- * This function simply exits the application
- */
- private void exit(){
- Intent homeIntent = new Intent(Intent.ACTION_MAIN);
- homeIntent.addCategory( Intent.CATEGORY_HOME );
- homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(homeIntent);
- }
- }
Add Comment
Please, Sign In to add comment