Firebase_login_registration_fargotpwd
//LOGIN _REGISTER_FARGOTPASSWORD IN FIREBASE
//login,regis,forgor aa 3 ni only layout j bnave and bdha no code MainActivity java ma karvo
//Manifest ma internet ni permission leve
//firebase connect ni procee
//user and common ni only java j file bnave use ie pojo class
//1.Tool ma jvu firebase pr click karvu
//2.analytics pr coonect karvu
//3.cloud messaging and kd add fem to your app aeni pr click karvu
//4.sav thi uper <- aavo aero hse click karvo and Authenticatiion and realtime database
conect karva
//firebase open karvu email/password enable karvu
//pche database ma crete database ma jvu pache 2nd number no radio btn click
karvu(text data vadu)
Step 0:dependacy //last ma add karvi
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtEmail"
android:hint="Email"
android:inputType="textEmailAddress"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtPassword"
android:hint="Password"
android:inputType="textPassword"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
step 2:registration_xml.layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtEmail"
android:hint="Email"
android:inputType="textEmailAddress"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtPassword"
android:hint="Password"
android:inputType="textPassword"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtName"
android:hint="Name"
android:inputType="textPersonName"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtPhone"
android:hint="Phone"
android:inputType="phone"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
step 3:fargotpassword_xml_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtEmail"
android:hint="Email"
android:inputType="textEmailAddress"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Step 4:login regis forgot.xml
Step 5:MainActivity_java
public class activitymainloginandregis extends AppCompatActivity {
Button btnSignIn,btnRegister;
EditText edtEmail,edtPassword,edtName,edtPhone;
String strEmail,strPassword,strName,strPhone;
RelativeLayout rootLayout;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
TextView txt_forgot_pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymainloginandregis);
//Init PaperDb
//Paper.init(this);
//Init Firebase
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference(Common.user_driver_tbl);
//Init View
btnSignIn = findViewById(R.id.btnSignIn);
btnRegister = findViewById(R.id.btnRegister);
rootLayout = findViewById(R.id.rootLayout);
txt_forgot_pwd =findViewById(R.id.txt_forgot_password);
//Event
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRegisterDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showLoginDialog();
}
});
txt_forgot_pwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialogForgotPwd();
}
});
}
private void autoLogin(String user, String pwd) {
final android.app.AlertDialog waitingDialog = new ProgressDialog(activitymainloginandregis.this);
waitingDialog.show();
//Login
auth.signInWithEmailAndPassword(user,pwd).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
waitingDialog.dismiss();
FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//after assigned value
Common.currentUser=dataSnapshot.getValue(User.class);
//start new activity
startActivity(new Intent(activitymainloginandregis.this,drawer.class));
waitingDialog.dismiss();
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
waitingDialog.dismiss();
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
//Active Button
btnSignIn.setEnabled(true);
}
});
}
private void showDialogForgotPwd() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("FORGOT PASSWORD");
alertDialog.setMessage("Please enter your email address");
LayoutInflater inflater =LayoutInflater.from(this);
View forgot_pwd_layout = inflater.inflate(R.layout.fargot_pwd,null);
edtEmail = (EditText)forgot_pwd_layout.findViewById(R.id.edtEmail);
alertDialog.setView(forgot_pwd_layout);
alertDialog.setPositiveButton("RESET", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialogInterface, int i) {
final android.app.AlertDialog waitingDialog = new ProgressDialog(activitymainloginandregis.this);
waitingDialog.show();
auth.sendPasswordResetEmail(edtEmail.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
dialogInterface.dismiss();
waitingDialog.dismiss();
Snackbar.make(rootLayout,"Reset password link has been sent.",Snackbar.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialogInterface.dismiss();
waitingDialog.dismiss();
Snackbar.make(rootLayout,""+e.getMessage(),Snackbar.LENGTH_SHORT).show();
}
});
}
});
alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.show();
}
private void showLoginDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("SIGN IN");
dialog.setMessage("Please use email to sign in");
LayoutInflater inflater =LayoutInflater.from(this);
View login_layout = inflater.inflate(R.layout.login,null);
edtEmail = (EditText)login_layout.findViewById(R.id.edtEmail);
edtPassword = (EditText)login_layout.findViewById(R.id.edtPassword);
dialog.setView(login_layout);
//Set Button
dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
strEmail = edtEmail.getText().toString();
strPassword = edtPassword.getText().toString();
final android.app.AlertDialog waitingDialog = new ProgressDialog(activitymainloginandregis.this);
waitingDialog.show();
//Login
auth.signInWithEmailAndPassword(strEmail,strPassword).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
waitingDialog.dismiss();
//Fetch data and save to variables
FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Save value
// Paper.book().write(Common.user_field,edtEmail.getText().toString());
//Paper.book().write(Common.pwd_field,edtPassword.getText().toString());
//after assigned value
Common.currentUser=dataSnapshot.getValue(User.class);
//start new activity
startActivity(new Intent(activitymainloginandregis.this, drawer.class));
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
waitingDialog.dismiss();
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
//Active Button
btnSignIn.setEnabled(true);
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}
private void showRegisterDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("REGISTER");
dialog.setMessage("Please use email to register");
LayoutInflater inflater =LayoutInflater.from(this);
View register_layout = inflater.inflate(R.layout.registration,null);
edtEmail = (EditText) register_layout.findViewById(R.id.edtEmail);
edtPassword =(EditText) register_layout.findViewById(R.id.edtPassword);
edtName = (EditText)register_layout.findViewById(R.id.edtName);
edtPhone = (EditText)register_layout.findViewById(R.id.edtPhone);
dialog.setView(register_layout);
//Set Button
dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
strEmail = edtEmail.getText().toString();
strPhone = edtPhone.getText().toString();
strPassword = edtPassword.getText().toString();
strName = edtName.getText().toString();
//Check Validation
if (TextUtils.isEmpty(strEmail))
{
Snackbar.make(rootLayout,"Please enter email address",Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(strPhone))
{
Snackbar.make(rootLayout,"Please enter phone number",Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(strPassword))
{
Snackbar.make(rootLayout,"Please enter password",Snackbar.LENGTH_SHORT).show();
return;
}
if (strPassword.length() < 6)
{
Snackbar.make(rootLayout,"Password to short !!!",Snackbar.LENGTH_SHORT).show();
return;
}
//Register New User
auth.createUserWithEmailAndPassword(strEmail, strPassword).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
//Save user to db
User user = new User();
user.setEmail(strEmail);
user.setName(strName);
user.setPhone(strPhone);
user.setPassword(strPassword);
//Use email to primary key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout,"Register successfully !!!",Snackbar.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}
}
Step 5:Common
package demoproject.aalap.com.ideal;
import android.location.Location;
/**
* Created by ADMIN on 04-07-2018.
*/
public class Common {
public static final String driver_tbl="Drivers";
public static final String user_driver_tbl="DriversInformation";
public static final String user_rider_tbl="RidersInformation";
public static final String pickup_request_tbl="PickupRequest";
public static final String token_tbl="Tokens";
public static final int PICK_IMAGE_REQUEST=9999;
public static User currentUser;
public static Location mLastLocation=null;
public static final String baseURL = "https://maps.googleapis.com";
public static final String fcmURL = "https://fcm.googleapis.com/";
public static final String user_field = "usr";
public static final String pwd_field = "pwd";
public static double base_fare = 2.55; //Base on uber fee on newyork
private static double time_rate = 0.35;
private static double distance_rate = 1.75;
public static double formulaPrice(double km, int min)
{
return (base_fare+(time_rate*min)+(distance_rate*km));
}
// public static IGoogleAPI getGoogleAPI()
//{
// return RetrofitClient.getClient(baseURL).create(IGoogleAPI.class);
//}
//public static IFCMService getFCMService()
//{
// return FCMClient.getClient(fcmURL).create(IFCMService.class);
//}
}
Step 6:User
package demoproject.aalap.com.ideal;
/**
* Created by ADMIN on 04-07-2018.
*/
public class User {
private String email;
private String password;
private String name;
private String phone;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
//login,regis,forgor aa 3 ni only layout j bnave and bdha no code MainActivity java ma karvo
//Manifest ma internet ni permission leve
//firebase connect ni procee
//user and common ni only java j file bnave use ie pojo class
//1.Tool ma jvu firebase pr click karvu
//2.analytics pr coonect karvu
//3.cloud messaging and kd add fem to your app aeni pr click karvu
//4.sav thi uper <- aavo aero hse click karvo and Authenticatiion and realtime database
conect karva
//firebase open karvu email/password enable karvu
//pche database ma crete database ma jvu pache 2nd number no radio btn click
karvu(text data vadu)
Step 0:dependacy //last ma add karvi
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleCompatible implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.android.support:design:26.0.0' implementation 'com.android.support:support-v4:26.0.0' implementation 'com.google.firebase:firebase-auth:11.0.4' implementation 'com.google.firebase:firebase-database:11.0.4' implementation 'com.google.firebase:firebase-storage:11.0.4' implementation 'com.google.firebase:firebase-messaging:11.0.4' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:0.5' androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' implementation 'de.hdodenhof:circleimageview:2.2.0' implementation 'com.android.support:cardview-v7:26.0.0'} apply plugin: 'com.google.gms.google-services'Step 1:login_xml.layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtEmail"
android:hint="Email"
android:inputType="textEmailAddress"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtPassword"
android:hint="Password"
android:inputType="textPassword"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
step 2:registration_xml.layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtEmail"
android:hint="Email"
android:inputType="textEmailAddress"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtPassword"
android:hint="Password"
android:inputType="textPassword"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtName"
android:hint="Name"
android:inputType="textPersonName"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtPhone"
android:hint="Phone"
android:inputType="phone"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
step 3:fargotpassword_xml_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtEmail"
android:hint="Email"
android:inputType="textEmailAddress"
android:text=""
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Step 4:login regis forgot.xml
<RelativeLayout 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" android:id="@+id/rootLayout" android:background="#000000" tools:context="demoproject.aalap.com.ppp.loginRF"> <LinearLayout android:orientation="vertical" android:layout_marginTop="30dp" android:layout_centerHorizontal="true" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="U B E R" android:textSize="36sp" android:textAlignment="center" android:textColor="#ddfce515" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="PARTNER" android:textSize="16sp" android:layout_marginTop="20dp" android:textAlignment="center" android:textColor="#ddfce515" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/txt_forgot_password" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:text="FORGOT PASSWORD?" android:textSize="12sp" android:textColor="#ddfce515" android:textAlignment="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:weightSum="2" android:layout_above="@id/txt_forgot_password" android:layout_margin="16dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btnSignIn" android:background="#1bcccc" android:textColor="#ffffff" android:text="SIGN IN" android:layout_marginRight="6dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:id="@+id/btnRegister" android:background="#ffffff" android:textColor="#000000" android:text="REGISTER" android:layout_marginLeft="6dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
Step 5:MainActivity_java
public class activitymainloginandregis extends AppCompatActivity {
Button btnSignIn,btnRegister;
EditText edtEmail,edtPassword,edtName,edtPhone;
String strEmail,strPassword,strName,strPhone;
RelativeLayout rootLayout;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
TextView txt_forgot_pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymainloginandregis);
//Init PaperDb
//Paper.init(this);
//Init Firebase
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference(Common.user_driver_tbl);
//Init View
btnSignIn = findViewById(R.id.btnSignIn);
btnRegister = findViewById(R.id.btnRegister);
rootLayout = findViewById(R.id.rootLayout);
txt_forgot_pwd =findViewById(R.id.txt_forgot_password);
//Event
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRegisterDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showLoginDialog();
}
});
txt_forgot_pwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialogForgotPwd();
}
});
}
private void autoLogin(String user, String pwd) {
final android.app.AlertDialog waitingDialog = new ProgressDialog(activitymainloginandregis.this);
waitingDialog.show();
//Login
auth.signInWithEmailAndPassword(user,pwd).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
waitingDialog.dismiss();
FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//after assigned value
Common.currentUser=dataSnapshot.getValue(User.class);
//start new activity
startActivity(new Intent(activitymainloginandregis.this,drawer.class));
waitingDialog.dismiss();
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
waitingDialog.dismiss();
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
//Active Button
btnSignIn.setEnabled(true);
}
});
}
private void showDialogForgotPwd() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("FORGOT PASSWORD");
alertDialog.setMessage("Please enter your email address");
LayoutInflater inflater =LayoutInflater.from(this);
View forgot_pwd_layout = inflater.inflate(R.layout.fargot_pwd,null);
edtEmail = (EditText)forgot_pwd_layout.findViewById(R.id.edtEmail);
alertDialog.setView(forgot_pwd_layout);
alertDialog.setPositiveButton("RESET", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialogInterface, int i) {
final android.app.AlertDialog waitingDialog = new ProgressDialog(activitymainloginandregis.this);
waitingDialog.show();
auth.sendPasswordResetEmail(edtEmail.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
dialogInterface.dismiss();
waitingDialog.dismiss();
Snackbar.make(rootLayout,"Reset password link has been sent.",Snackbar.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialogInterface.dismiss();
waitingDialog.dismiss();
Snackbar.make(rootLayout,""+e.getMessage(),Snackbar.LENGTH_SHORT).show();
}
});
}
});
alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.show();
}
private void showLoginDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("SIGN IN");
dialog.setMessage("Please use email to sign in");
LayoutInflater inflater =LayoutInflater.from(this);
View login_layout = inflater.inflate(R.layout.login,null);
edtEmail = (EditText)login_layout.findViewById(R.id.edtEmail);
edtPassword = (EditText)login_layout.findViewById(R.id.edtPassword);
dialog.setView(login_layout);
//Set Button
dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
strEmail = edtEmail.getText().toString();
strPassword = edtPassword.getText().toString();
final android.app.AlertDialog waitingDialog = new ProgressDialog(activitymainloginandregis.this);
waitingDialog.show();
//Login
auth.signInWithEmailAndPassword(strEmail,strPassword).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
waitingDialog.dismiss();
//Fetch data and save to variables
FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Save value
// Paper.book().write(Common.user_field,edtEmail.getText().toString());
//Paper.book().write(Common.pwd_field,edtPassword.getText().toString());
//after assigned value
Common.currentUser=dataSnapshot.getValue(User.class);
//start new activity
startActivity(new Intent(activitymainloginandregis.this, drawer.class));
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
waitingDialog.dismiss();
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
//Active Button
btnSignIn.setEnabled(true);
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}
private void showRegisterDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("REGISTER");
dialog.setMessage("Please use email to register");
LayoutInflater inflater =LayoutInflater.from(this);
View register_layout = inflater.inflate(R.layout.registration,null);
edtEmail = (EditText) register_layout.findViewById(R.id.edtEmail);
edtPassword =(EditText) register_layout.findViewById(R.id.edtPassword);
edtName = (EditText)register_layout.findViewById(R.id.edtName);
edtPhone = (EditText)register_layout.findViewById(R.id.edtPhone);
dialog.setView(register_layout);
//Set Button
dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
strEmail = edtEmail.getText().toString();
strPhone = edtPhone.getText().toString();
strPassword = edtPassword.getText().toString();
strName = edtName.getText().toString();
//Check Validation
if (TextUtils.isEmpty(strEmail))
{
Snackbar.make(rootLayout,"Please enter email address",Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(strPhone))
{
Snackbar.make(rootLayout,"Please enter phone number",Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(strPassword))
{
Snackbar.make(rootLayout,"Please enter password",Snackbar.LENGTH_SHORT).show();
return;
}
if (strPassword.length() < 6)
{
Snackbar.make(rootLayout,"Password to short !!!",Snackbar.LENGTH_SHORT).show();
return;
}
//Register New User
auth.createUserWithEmailAndPassword(strEmail, strPassword).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
//Save user to db
User user = new User();
user.setEmail(strEmail);
user.setName(strName);
user.setPhone(strPhone);
user.setPassword(strPassword);
//Use email to primary key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout,"Register successfully !!!",Snackbar.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT).show();
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}
}
Step 5:Common
package demoproject.aalap.com.ideal;
import android.location.Location;
/**
* Created by ADMIN on 04-07-2018.
*/
public class Common {
public static final String driver_tbl="Drivers";
public static final String user_driver_tbl="DriversInformation";
public static final String user_rider_tbl="RidersInformation";
public static final String pickup_request_tbl="PickupRequest";
public static final String token_tbl="Tokens";
public static final int PICK_IMAGE_REQUEST=9999;
public static User currentUser;
public static Location mLastLocation=null;
public static final String baseURL = "https://maps.googleapis.com";
public static final String fcmURL = "https://fcm.googleapis.com/";
public static final String user_field = "usr";
public static final String pwd_field = "pwd";
public static double base_fare = 2.55; //Base on uber fee on newyork
private static double time_rate = 0.35;
private static double distance_rate = 1.75;
public static double formulaPrice(double km, int min)
{
return (base_fare+(time_rate*min)+(distance_rate*km));
}
// public static IGoogleAPI getGoogleAPI()
//{
// return RetrofitClient.getClient(baseURL).create(IGoogleAPI.class);
//}
//public static IFCMService getFCMService()
//{
// return FCMClient.getClient(fcmURL).create(IFCMService.class);
//}
}
Step 6:User
package demoproject.aalap.com.ideal;
/**
* Created by ADMIN on 04-07-2018.
*/
public class User {
private String email;
private String password;
private String name;
private String phone;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Comments
Post a Comment