Guest User

Untitled

a guest
Nov 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. public class ForgotPasswordActivity extends AppCompatActivity {
  2.  
  3. private static final String TAG = ForgotPasswordActivity.class.getSimpleName();
  4. private CircleButton mButtonReturn;
  5. private Button mButtonForgotPassword;
  6. private EditText mEditEmail;
  7. private EditText mEditNewPassword;
  8. private EditText mEditNewPasswordConfirm;
  9.  
  10.  
  11.  
  12. private ProgressDialog mpDialog;
  13.  
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_forgot_password);
  19.  
  20.  
  21. //-----------------Find View--------------
  22. mButtonReturn = (CircleButton) findViewById(R.id.Forgot_password_button_return);
  23. mEditEmail = (EditText) findViewById(R.id.Forgot_password_editText_email);
  24. mEditNewPassword = (EditText) findViewById(R.id.Forgot_password_editText_password_new);
  25. mEditNewPasswordConfirm = (EditText) findViewById(R.id.Forgot_password_editText_password_new_confirm);
  26. mButtonForgotPassword = (Button) findViewById(R.id.Forgot_password_button_Forgot_password);
  27.  
  28. //-----------------ProgressDialog--------------
  29. mpDialog = new ProgressDialog(this);
  30. mpDialog.setCancelable(false);
  31.  
  32. //-----------------test_of_confirm_password--------------
  33. mEditNewPassword.addTextChangedListener(new TextWatcher() {
  34. public void afterTextChanged(Editable s) {
  35. if(!(mEditNewPassword.getText().toString().equals(mEditNewPasswordConfirm.getText().toString()))){
  36. mEditNewPasswordConfirm.setError(getResources().getString(R.string.Forgot_password_text_no_same_password));
  37. mEditNewPassword.setError(getResources().getString(R.string.Forgot_password_text_no_same_password));
  38. }
  39. else{
  40. mEditNewPassword.setError(null);
  41. mEditNewPasswordConfirm.setError(null);
  42. }
  43. }
  44. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  45. public void onTextChanged(CharSequence query, int start, int before, int count) {
  46. }
  47. });
  48. mEditNewPasswordConfirm.addTextChangedListener(new TextWatcher() {
  49. public void afterTextChanged(Editable s) {
  50. if(!(mEditNewPassword.getText().toString().equals(mEditNewPasswordConfirm.getText().toString()))){
  51. mEditNewPasswordConfirm.setError(getResources().getString(R.string.Forgot_password_text_no_same_password));
  52. mEditNewPassword.setError(getResources().getString(R.string.Forgot_password_text_no_same_password));
  53.  
  54. }
  55. else{
  56. mEditNewPassword.setError(null);
  57. mEditNewPasswordConfirm.setError(null);
  58. }
  59. }
  60. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  61. public void onTextChanged(CharSequence query, int start, int before, int count) {
  62. }
  63. });
  64.  
  65. //-----------------return button--------------
  66. mButtonReturn.setOnClickListener(new View.OnClickListener() {
  67. public void onClick(View v) {
  68. finish();
  69. Toast.makeText(getApplicationContext(),
  70. R.string.Forgot_password_Toast_Return, Toast.LENGTH_LONG).show();
  71. }
  72. });
  73.  
  74. //-----------------change password with the test if the password is strong--------------
  75.  
  76. mButtonForgotPassword.setOnClickListener(new View.OnClickListener() {
  77. public void onClick(View v) {
  78. if(!(mEditNewPassword.getText().toString().length()>0)){
  79. mEditNewPasswordConfirm.setError(getResources().getString(R.string.Change_password_text_no_same_password));
  80. mEditNewPassword.setError(getResources().getString(R.string.Change_password_text_no_same_password));
  81. Toast.makeText(getApplicationContext(),R.string.Change_password_text_no_same_password, Toast.LENGTH_LONG).show();
  82. return ;
  83. }
  84. else{
  85. if(!(mEditNewPassword.getText().toString().equals(mEditNewPasswordConfirm.getText().toString()))){
  86. mEditNewPasswordConfirm.setError(getResources().getString(R.string.Change_password_text_no_same_password));
  87. mEditNewPassword.setError(getResources().getString(R.string.Change_password_text_no_same_password));
  88. Toast.makeText(getApplicationContext(),R.string.Change_password_text_no_same_password, Toast.LENGTH_LONG).show();
  89. return ;
  90. }
  91. else{
  92. if(test_password_strength((mEditNewPassword.getText().toString()))){
  93. ChangeUset(mEditEmail.getText().toString(), mEditNewPassword.getText().toString());
  94. }
  95. else{
  96. mEditNewPassword.setError(getResources().getString(R.string.Change_password_password_no_strong));
  97. Toast.makeText(getApplicationContext(),R.string.Change_password_Toast_password_no_strong, Toast.LENGTH_LONG).show();
  98. }
  99. }
  100. }
  101.  
  102. }
  103. });
  104.  
  105.  
  106.  
  107. }
  108.  
  109.  
  110. /**
  111. * test if the password is length more than 6, have a letter, and a digit or symbol
  112. * */
  113. private boolean test_password_strength(String password){
  114.  
  115. if(password.length()<6){
  116. return false;
  117. }
  118. boolean haveletter=false; //does the string have letters
  119. boolean havenonletter=false; //does the string have digit or symbole
  120.  
  121. for (char letter : password.toCharArray() ) {
  122. if((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
  123. haveletter=true;
  124. }
  125. else{
  126. havenonletter=true;
  127. }
  128. }
  129. return haveletter && havenonletter;
  130. }
  131.  
  132.  
  133.  
  134. /**
  135. * Send a request to the server to change the password
  136. * */
  137. private void ChangeUset(final String email, final String new_password) {
  138. // Tag used to cancel the request
  139. String tag_string_req = "forgot_password";
  140. // set the message of the dialog and shwo it in waiting the response
  141. mpDialog.setMessage(getResources().getString(R.string.Forgot_password_Dialog));
  142. showDialog();
  143. //send the message
  144. StringRequest strReq = new StringRequest(Request.Method.POST,
  145. AppConfig.URL_FORGOT_PASSWORD, new Response.Listener<String>() {
  146. @Override
  147. public void onResponse(String response) {
  148. Log.d(TAG, "Register Response: " + response);
  149. //read and process the response
  150. try {
  151. JSONObject jObj = new JSONObject(response);
  152. boolean error = jObj.getBoolean("error");
  153. if (!error) {
  154. JSONObject jsonuser = jObj.getJSONObject("user");
  155. Gson gson = new Gson();
  156. User user = gson.fromJson(jsonuser.toString(),User.class);
  157. // Inserting row in users table
  158. LoginActivity.sDb.addUser(user);
  159. Toast.makeText(getApplicationContext(), R.string.Forgot_password_Toast_password_updated, Toast.LENGTH_LONG).show();
  160. finish();
  161. } else {
  162. // Error occurred in registration. Get the error
  163. // message
  164. String errorMsg = jObj.getString("error_msg");
  165. Toast.makeText(getApplicationContext(),
  166. errorMsg, Toast.LENGTH_LONG).show();
  167. }
  168. } catch (JSONException e) {
  169. e.printStackTrace();
  170. Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
  171. }
  172. hideDialog();
  173.  
  174. }
  175. }, new Response.ErrorListener() {
  176.  
  177. @Override
  178. public void onErrorResponse(VolleyError error) {
  179. Log.e(TAG, "Registration Error: " + error.getMessage());
  180. Toast.makeText(getApplicationContext(),
  181. error.getMessage(), Toast.LENGTH_LONG).show();
  182. hideDialog();
  183. }
  184. }) {
  185.  
  186. @Override
  187. protected Map<String, String> getParams() {
  188. // Posting params to register url
  189. Map<String, String> params = new HashMap<>();
  190. params.put("email", email);
  191. params.put("new_password", new_password);
  192. return params;
  193. }
  194.  
  195. };
  196.  
  197. // Adding request to request queue
  198. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  199. }
  200.  
  201. /**
  202. * Show the dialog box for the waiting
  203. * */
  204. private void showDialog() {
  205. if (!mpDialog.isShowing())
  206. mpDialog.show();
  207. }
  208.  
  209. /**
  210. * Hide the dialog box for the waiting
  211. * */
  212. private void hideDialog() {
  213. if (mpDialog.isShowing())
  214. mpDialog.dismiss();
  215. }
  216.  
  217. }
Add Comment
Please, Sign In to add comment