PageRenderTime 232ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/j2ptechnologies/itwtravelgo/Activity/ResetPassword.java

https://bitbucket.org/itwgoandroid/itw-go-android
Java | 261 lines | 212 code | 49 blank | 0 comment | 10 complexity | d0c6d034ea55db310126bbbd6b609793 MD5 | raw file
  1. package com.j2ptechnologies.itwtravelgo.Activity;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.content.Intent;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.support.annotation.RequiresApi;
  8. import android.support.design.widget.FloatingActionButton;
  9. import android.support.design.widget.Snackbar;
  10. import android.support.v7.widget.CardView;
  11. import android.transition.Transition;
  12. import android.transition.TransitionInflater;
  13. import android.view.View;
  14. import android.view.ViewAnimationUtils;
  15. import android.view.animation.AccelerateInterpolator;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import com.android.volley.Request;
  19. import com.android.volley.Response;
  20. import com.android.volley.VolleyError;
  21. import com.j2ptechnologies.itwtravelgo.R;
  22. import com.j2ptechnologies.itwtravelgo.Utils.CatLoadingView;
  23. import com.j2ptechnologies.itwtravelgo.Utils.CommonMethods;
  24. import com.j2ptechnologies.itwtravelgo.Utils.CommonMethodsFragments;
  25. import com.j2ptechnologies.itwtravelgo.Utils.Constants;
  26. import com.j2ptechnologies.itwtravelgo.api.PostApi;
  27. import com.j2ptechnologies.itwtravelgo.app.App;
  28. import com.j2ptechnologies.itwtravelgo.db.DatabaseHandler;
  29. import com.j2ptechnologies.itwtravelgo.util.Util;
  30. import org.json.JSONException;
  31. import org.json.JSONObject;
  32. import java.util.regex.Pattern;
  33. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  34. public class ResetPassword extends CommonMethods {
  35. FloatingActionButton fab;
  36. CardView cvAdd;
  37. String currEmail = "";
  38. private final Pattern VALID_EMAIL_ADDRESS_REGEX =
  39. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  40. private EditText email, password;
  41. Button bt_go;
  42. public static String STR_EXTRA_ACTION_REGISTER = "register";
  43. CatLoadingView mView;
  44. @Override
  45. protected void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47. getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
  48. setContentView(R.layout.activity_reset_password);
  49. Util.getSoftButtonsBarSizePort(ResetPassword.this);
  50. mView = new CatLoadingView();
  51. Bundle bundle = getIntent().getExtras();
  52. try {
  53. currEmail = bundle.getString("currentEmail");
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. fab = (FloatingActionButton) findViewById(R.id.fab);
  58. cvAdd = (CardView) findViewById(R.id.cv_add);
  59. email = (EditText) findViewById(R.id.email);
  60. password = (EditText) findViewById(R.id.password);
  61. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  62. ShowEnterAnimation();
  63. }
  64. fab.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. animateRevealClose();
  68. }
  69. });
  70. email.setText(currEmail);
  71. bt_go = (Button) findViewById(R.id.bt_go);
  72. bt_go.setOnClickListener(new View.OnClickListener() {
  73. @Override
  74. public void onClick(View view) {
  75. if (!validateEmail((email.getText().toString().trim()))) {
  76. email.setError("Please enter valid Email");
  77. return;
  78. }
  79. if (password.getText().toString().trim().length() < 6) {
  80. password.setError("Password lenght must be min 6");
  81. return;
  82. }
  83. JSONObject json = new JSONObject();
  84. try {
  85. DatabaseHandler db = new DatabaseHandler(getBaseContext());
  86. json.put("email", email.getText().toString().trim());
  87. if (CommonMethodsFragments.getDetails("currentPassword", db) != null && CommonMethodsFragments.getDetails("currentPassword", db).length() >= 6 && CommonMethodsFragments.getDetails("currentEmail", db).equalsIgnoreCase(currEmail)) {
  88. json.put("password", CommonMethodsFragments.getDetails("currentPassword", db));
  89. } else {
  90. json.put("password", Constants.defaultPassword);
  91. }
  92. json.put("newPassword", password.getText().toString().trim());
  93. } catch (JSONException e) {
  94. e.printStackTrace();
  95. }
  96. resetPassword(json, email.getText().toString().trim(), password.getText().toString().trim());
  97. }
  98. });
  99. }
  100. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  101. private void ShowEnterAnimation() {
  102. Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
  103. getWindow().setSharedElementEnterTransition(transition);
  104. transition.addListener(new Transition.TransitionListener() {
  105. @Override
  106. public void onTransitionStart(Transition transition) {
  107. cvAdd.setVisibility(View.GONE);
  108. }
  109. @Override
  110. public void onTransitionEnd(Transition transition) {
  111. transition.removeListener(this);
  112. animateRevealShow();
  113. }
  114. @Override
  115. public void onTransitionCancel(Transition transition) {
  116. }
  117. @Override
  118. public void onTransitionPause(Transition transition) {
  119. }
  120. @Override
  121. public void onTransitionResume(Transition transition) {
  122. }
  123. });
  124. }
  125. public void animateRevealShow() {
  126. Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth() / 2, 0, fab.getWidth() / 2, cvAdd.getHeight());
  127. mAnimator.setDuration(500);
  128. mAnimator.setInterpolator(new AccelerateInterpolator());
  129. mAnimator.addListener(new AnimatorListenerAdapter() {
  130. @Override
  131. public void onAnimationEnd(Animator animation) {
  132. super.onAnimationEnd(animation);
  133. }
  134. @Override
  135. public void onAnimationStart(Animator animation) {
  136. cvAdd.setVisibility(View.VISIBLE);
  137. super.onAnimationStart(animation);
  138. }
  139. });
  140. mAnimator.start();
  141. }
  142. public void animateRevealClose() {
  143. Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth() / 2, 0, cvAdd.getHeight(), fab.getWidth() / 2);
  144. mAnimator.setDuration(500);
  145. mAnimator.setInterpolator(new AccelerateInterpolator());
  146. mAnimator.addListener(new AnimatorListenerAdapter() {
  147. @Override
  148. public void onAnimationEnd(Animator animation) {
  149. cvAdd.setVisibility(View.INVISIBLE);
  150. super.onAnimationEnd(animation);
  151. fab.setImageResource(R.drawable.ic_signup);
  152. ResetPassword.super.onBackPressed();
  153. }
  154. @Override
  155. public void onAnimationStart(Animator animation) {
  156. super.onAnimationStart(animation);
  157. }
  158. });
  159. mAnimator.start();
  160. }
  161. @Override
  162. public void onBackPressed() {
  163. animateRevealClose();
  164. }
  165. public void resetPassword(JSONObject json, final String email, final String password) {
  166. String URL = Constants.setPassword;
  167. showDialog();
  168. PostApi api = new PostApi(Request.Method.POST, URL, json, new Response.Listener<JSONObject>() {
  169. @Override
  170. public void onResponse(JSONObject jsonObject) {
  171. dismissDialog();
  172. try {
  173. int code = jsonObject.getInt("code");
  174. if (code == 200) {
  175. DatabaseHandler db = new DatabaseHandler(getBaseContext());
  176. CommonMethodsFragments.addDetails("currentEmail", email, db);
  177. CommonMethodsFragments.addDetails("isRegistered", "true", db);
  178. CommonMethodsFragments.addDetails("myCurrentPassword", password, db);
  179. Intent i = new Intent(getBaseContext(), Dashboard.class);
  180. i.putExtra("from", "resetpassword");
  181. startActivity(i);
  182. }
  183. } catch (JSONException e) {
  184. e.printStackTrace();
  185. }
  186. }
  187. }, new Response.ErrorListener() {
  188. @Override
  189. public void onErrorResponse(VolleyError volleyError) {
  190. Snackbar snackbar = null;
  191. dismissDialog();
  192. final Snackbar finalSnackbar = snackbar;
  193. snackbar = Snackbar
  194. .make(getWindow().getDecorView(), "Something went wrong", Snackbar.LENGTH_LONG)
  195. .setAction("", new View.OnClickListener() {
  196. @Override
  197. public void onClick(View view) {
  198. }
  199. });
  200. snackbar.show();
  201. }
  202. });
  203. App.getVolleyQueue().add(api);
  204. }
  205. public void showDialog() {
  206. mView.setClickCancelAble(false);
  207. mView.show(getSupportFragmentManager(), "");
  208. }
  209. public void dismissDialog() {
  210. mView.dismiss();
  211. }
  212. }