PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/OnlineScheduler/app/src/main/java/com/example/ranjith/jpmconlinescheduler/Bio.java

https://bitbucket.org/vnrjpmc/onlinescheduler
Java | 235 lines | 142 code | 35 blank | 58 comment | 6 complexity | fa682cf546133f299d1e32b10503a3fa MD5 | raw file
  1. package com.example.ranjith.jpmconlinescheduler;
  2. import android.app.ProgressDialog;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.support.annotation.NonNull;
  9. import android.support.annotation.Nullable;
  10. import android.support.v4.app.Fragment;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.Toast;
  18. import com.example.ranjith.jpmconlinescheduler.Admin.Update;
  19. import com.example.ranjith.jpmconlinescheduler.Util.Utils;
  20. import com.example.ranjith.jpmconlinescheduler.WorkFlow.Registration;
  21. import com.example.ranjith.jpmconlinescheduler.WorkFlow.Servicess;
  22. import com.example.ranjith.jpmconlinescheduler.WorkFlow.SignIn;
  23. import com.google.android.gms.tasks.OnCompleteListener;
  24. import com.google.android.gms.tasks.Task;
  25. import com.google.firebase.auth.FirebaseAuth;
  26. import com.google.firebase.auth.FirebaseUser;
  27. import com.google.firebase.database.DataSnapshot;
  28. import com.google.firebase.database.DatabaseError;
  29. import com.google.firebase.database.DatabaseReference;
  30. import com.google.firebase.database.FirebaseDatabase;
  31. import com.google.firebase.database.Query;
  32. import com.google.firebase.database.ValueEventListener;
  33. import java.util.regex.Matcher;
  34. import java.util.regex.Pattern;
  35. public class Bio extends Fragment{
  36. public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  37. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  38. // TODO: Rename parameter arguments, choose names that match
  39. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  40. private static final String ARG_PARAM1 = "param1";
  41. private static final String ARG_PARAM2 = "param2";
  42. // TODO: Rename and change types of parameters
  43. private String mParam1;
  44. private String mParam2;
  45. private View view;
  46. private ProgressDialog progressDialog;
  47. private Query query;
  48. private String path;
  49. private String mail;
  50. public Bio() {
  51. // Required empty public constructor
  52. }
  53. /**
  54. * Use this factory method to create a new instance of
  55. * this fragment using the provided parameters.
  56. *
  57. * @param param1 Parameter 1.
  58. * @param param2 Parameter 2.
  59. * @return A new instance of fragment Bio.
  60. */
  61. // TODO: Rename and change types and number of parameters
  62. public static Bio newInstance(String param1, String param2) {
  63. Bio fragment = new Bio();
  64. Bundle args = new Bundle();
  65. args.putString(ARG_PARAM1, param1);
  66. args.putString(ARG_PARAM2, param2);
  67. fragment.setArguments(args);
  68. return fragment;
  69. }
  70. @Override
  71. public void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. if (getArguments() != null) {
  74. mParam1 = getArguments().getString(ARG_PARAM1);
  75. mParam2 = getArguments().getString(ARG_PARAM2);
  76. }
  77. }
  78. @Override
  79. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  80. Bundle savedInstanceState) {
  81. // Inflate the layout for this fragment
  82. view = inflater.inflate(R.layout.fragment_bio, container, false);
  83. Button update=(Button)view.findViewById(R.id.update);
  84. update.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View view) {
  87. update();
  88. }
  89. });
  90. return view;
  91. }
  92. private void update() {
  93. progressDialog = Utils.showLoadingDialog(getActivity(), false);
  94. EditText email = (EditText) view.findViewById(R.id.email);
  95. mail=email.getText().toString();
  96. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email.getText().toString());
  97. if (matcher.find()) {
  98. emailVerification();
  99. updateDetails();
  100. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  101. user.updateEmail(email.getText().toString())
  102. .addOnCompleteListener(new OnCompleteListener<Void>() {
  103. @Override
  104. public void onComplete(@NonNull Task<Void> task) {
  105. if (task.isSuccessful()) {
  106. progressDialog.cancel();
  107. // Log.d(TAG, "User email address updated.");
  108. SharedPreferences shared = getActivity().getSharedPreferences("loginData", Context.MODE_PRIVATE);
  109. SharedPreferences.Editor editor = shared.edit();
  110. editor.putString("user","");editor.putString("visited","none");
  111. editor.putString("mvisited","no");
  112. editor.putString("activity","no");
  113. editor.putString("image","null");
  114. editor.commit();
  115. Toast.makeText(getActivity(), "Updated succesfully.. Please Login again", Toast.LENGTH_LONG).show();
  116. startActivity(new Intent(getActivity(), SignIn.class));
  117. }
  118. }
  119. });
  120. } else {
  121. progressDialog.cancel();
  122. Toast.makeText(getActivity(), "Invalid email id", Toast.LENGTH_SHORT).show();
  123. }
  124. }
  125. private void updateDetails() {
  126. DatabaseReference reference= FirebaseDatabase.getInstance().getReference().child("Customer").push();
  127. reference.child("Email").setValue(mail);
  128. reference.child("Verified").setValue("True");
  129. // final SharedPreferences shared = getActivity().getSharedPreferences("loginData", Context.MODE_PRIVATE);
  130. // DatabaseReference reference= FirebaseDatabase.getInstance().getReference().child("Customer");
  131. // query = reference.orderByChild("Email").equalTo(shared.getString("user", ""));
  132. // query.addListenerForSingleValueEvent(new ValueEventListener() {
  133. // @Override
  134. // public void onDataChange(DataSnapshot dataSnapshot) {
  135. // if (dataSnapshot.exists()) {
  136. // DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
  137. // path = nodeDataSnapshot.getKey();
  138. // Log.d("pass", path);
  139. // if (path != null) {
  140. // Notify();
  141. // }
  142. //
  143. // }
  144. // else {
  145. // progressDialog.cancel();
  146. // Toast.makeText(getActivity(), "You haven;t generated a token..", Toast.LENGTH_SHORT).show();
  147. // }
  148. // }
  149. //
  150. // @Override
  151. // public void onCancelled(DatabaseError databaseError) {
  152. // progressDialog.cancel();
  153. //
  154. // }
  155. // });
  156. }
  157. private void Notify() {
  158. final DatabaseReference keyRef=FirebaseDatabase.getInstance().getReference().child("Customer").child(path).child("Email");
  159. keyRef.setValue(mail);
  160. }
  161. private void emailVerification() {
  162. FirebaseAuth auth = FirebaseAuth.getInstance();
  163. FirebaseUser user = auth.getCurrentUser();
  164. user.sendEmailVerification()
  165. .addOnCompleteListener(new OnCompleteListener<Void>() {
  166. @Override
  167. public void onComplete(@NonNull Task<Void> task) {
  168. if (task.isSuccessful()) {
  169. progressDialog.cancel();
  170. Toast.makeText(getActivity(),"Email verification link has been sent",Toast.LENGTH_LONG).show();
  171. }
  172. else
  173. {
  174. Toast.makeText(getActivity(),"Email verification link sending failed",Toast.LENGTH_LONG).show();
  175. }
  176. }
  177. });
  178. }
  179. // // TODO: Rename method, update argument and hook method into UI event
  180. // public void onButtonPressed(Uri uri) {
  181. // if (mListener != null) {
  182. // mListener.onFragmentInteraction(uri);
  183. // }
  184. // }
  185. /**
  186. * This interface must be implemented by activities that contain this
  187. * fragment to allow an interaction in this fragment to be communicated
  188. * to the activity and potentially other fragments contained in that
  189. * activity.
  190. * <p>
  191. * See the Android Training lesson <a href=
  192. * "http://developer.android.com/training/basics/fragments/communicating.html"
  193. * >Communicating with Other Fragments</a> for more information.
  194. */
  195. @Override
  196. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  197. super.onViewCreated(view, savedInstanceState);
  198. getActivity().setTitle("Update Info");
  199. }
  200. }