SetNewPasswordActivity.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.cygnusa.hyperm;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import androidx.appcompat.app.AlertDialog;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import android.text.TextUtils;
  9. import android.view.Gravity;
  10. import android.view.LayoutInflater;
  11. import android.view.MenuItem;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.view.ViewGroup;
  16. import android.view.inputmethod.InputMethodManager;
  17. import android.widget.EditText;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20. import com.google.gson.Gson;
  21. import com.cygnusa.hyperm.util.ApiService;
  22. import com.cygnusa.hyperm.util.GlobalData;
  23. import com.cygnusa.hyperm.util.MainApplication;
  24. import com.cygnusa.hyperm.util.TransparentProgressDialog;
  25. import com.cygnusa.hyperm.util.UtilService;
  26. import com.cygnusa.hyperm.wrappers.ErrorResponse;
  27. import java.util.HashMap;
  28. import java.util.regex.Pattern;
  29. import retrofit2.Call;
  30. import retrofit2.Callback;
  31. import retrofit2.Response;
  32. public class SetNewPasswordActivity extends AppCompatActivity {
  33. EditText newPassword, confirmPassword;
  34. ApiService restService;
  35. TransparentProgressDialog progressDialog;
  36. // String PASSWORD_PATTERN = "((?=.*[a-zA-Z0-9]).{8,32})";
  37. String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z,A-Z]).{8,30})";
  38. AlertDialog.Builder alertDialogBuilder;
  39. AlertDialog alertDialog;
  40. String userName;
  41. public static String accessToken;
  42. public static void hideSoftKeyboard(Activity activity) {
  43. try {
  44. InputMethodManager inputMethodManager =
  45. (InputMethodManager) activity.getSystemService(
  46. Activity.INPUT_METHOD_SERVICE);
  47. inputMethodManager.hideSoftInputFromWindow(
  48. activity.getCurrentFocus().getWindowToken(), 0);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. setContentView(R.layout.activity_set_password);
  57. GlobalData.openDatabase(SetNewPasswordActivity.this);
  58. setupWindowAnimations();
  59. setupUI(findViewById(R.id.bgLayout));
  60. Bundle extras = getIntent().getExtras();
  61. if (extras != null) {
  62. userName = extras.getString("username");
  63. }
  64. restService = ((MainApplication) getApplication()).getClient();
  65. newPassword = findViewById(R.id.newPassword);
  66. confirmPassword = findViewById(R.id.confirmPassword);
  67. findViewById(R.id.continueButton).setOnClickListener(new OnClickListener() {
  68. @Override
  69. public void onClick(View view) {
  70. attemptLogin();
  71. }
  72. });
  73. // Toolbar toolbar = findViewById(R.id.toolbar);
  74. // setSupportActionBar(toolbar);
  75. // toolbar.setTitle("");
  76. // getSupportActionBar().setTitle("");
  77. // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  78. // getSupportActionBar().setDisplayShowHomeEnabled(true);
  79. }
  80. private void setupWindowAnimations() {
  81. this.overridePendingTransition(R.anim.animation_enter,
  82. R.anim.animation_leave);
  83. }
  84. @Override
  85. public boolean onOptionsItemSelected(MenuItem item) {
  86. if (item.getItemId() == android.R.id.home) {
  87. startActivity(new Intent(getBaseContext(), LoginActivity.class));
  88. finish();
  89. }
  90. return super.onOptionsItemSelected(item);
  91. }
  92. @Override
  93. public void onBackPressed() {
  94. startActivity(new Intent(getBaseContext(), SignUpActivity.class));
  95. finish();
  96. }
  97. private void attemptLogin() {
  98. String nPassword = newPassword.getText().toString().trim();
  99. String cPassword = confirmPassword.getText().toString().trim();
  100. if (TextUtils.isEmpty(nPassword) && TextUtils.isEmpty(cPassword)) {
  101. GlobalData.showToast(getBaseContext(), "Please fill in all necessary fields.", Toast.LENGTH_SHORT).show();
  102. } else if (TextUtils.isEmpty(nPassword)) {
  103. GlobalData.showToast(getBaseContext(), "Please enter the new password", Toast.LENGTH_SHORT).show();
  104. } else if (TextUtils.isEmpty(cPassword)) {
  105. GlobalData.showToast(getBaseContext(), "Please enter the confirm password", Toast.LENGTH_SHORT).show();
  106. } else if (!nPassword.equals(cPassword)) {
  107. GlobalData.showToast(getBaseContext(), "The entered passwords does not match", Toast.LENGTH_SHORT).show();
  108. } else if (!Pattern.compile(PASSWORD_PATTERN).matcher(cPassword).matches()) {
  109. GlobalData.showToast(getBaseContext(), "The password must have at least 8 characters, " +
  110. " and its alphanumeric." +
  111. "", Toast.LENGTH_LONG).show();
  112. } else {
  113. if (new UtilService().isNetworkAvailable(SetNewPasswordActivity.this)) {
  114. progressDialog = new TransparentProgressDialog(SetNewPasswordActivity.this);
  115. progressDialog.show();
  116. try {
  117. HashMap<String, String> map = new HashMap<>();
  118. map.put("new_password", nPassword);
  119. map.put("type", "register");
  120. map.put("username", userName);
  121. restService.changePassword(map).enqueue(new Callback<ErrorResponse>() {
  122. @Override
  123. public void onResponse(Call<ErrorResponse> call, Response<ErrorResponse> response) {
  124. if (progressDialog != null && progressDialog.isShowing()) {
  125. progressDialog.dismiss();
  126. }
  127. if (response.body() != null && response.body().getStatus() == 200) {
  128. ((MainApplication) getApplication()).addAccessToken(String.format("Bearer %s", response.body().getAccess_token()));
  129. accessToken=response.body().getAccess_token();
  130. restService = ((MainApplication) getApplication()).getClient();
  131. // GlobalData.showToast(getBaseContext(), "Your password is created successfully", Toast.LENGTH_SHORT).show();
  132. showInstructions();
  133. } else if (response.body() != null) {
  134. GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
  135. } else if (response.errorBody() != null) {
  136. Gson gson = new Gson();
  137. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  138. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  139. }
  140. }
  141. @Override
  142. public void onFailure(Call<ErrorResponse> call, Throwable t) {
  143. if (progressDialog != null && progressDialog.isShowing()) {
  144. progressDialog.dismiss();
  145. }
  146. GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  147. }
  148. });
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152. } else {
  153. GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
  154. }
  155. }
  156. }
  157. private void showInstructions() {
  158. View view = LayoutInflater.from(SetNewPasswordActivity.this).inflate(
  159. R.layout.signup_instruction_popup, null);
  160. TextView textHead = (TextView) view.findViewById(R.id.textHead);
  161. TextView text1 = (TextView) view.findViewById(R.id.text1);
  162. TextView text2 = (TextView) view.findViewById(R.id.text2);
  163. TextView text3 = (TextView) view.findViewById(R.id.text3);
  164. TextView text4 = (TextView) view.findViewById(R.id.text4);
  165. text2.setVisibility(View.INVISIBLE);
  166. text3.setVisibility(View.INVISIBLE);
  167. text4.setVisibility(View.INVISIBLE);
  168. textHead.setText("Fill in the mandatory fields");
  169. text1.setText("You are one step closer to complete the registration");
  170. text1.setGravity(Gravity.CENTER);
  171. view.findViewById(R.id.btn_ok).setOnClickListener(
  172. new View.OnClickListener() {
  173. @Override
  174. public void onClick(View v) {
  175. alertDialog.dismiss();
  176. startActivity(new Intent(getBaseContext(), MainProfileActivity.class));
  177. finish();
  178. }
  179. });
  180. alertDialogBuilder = new AlertDialog.Builder(SetNewPasswordActivity.this);
  181. alertDialogBuilder.setCancelable(false);
  182. alertDialogBuilder.setView(view);
  183. alertDialog = alertDialogBuilder.create();
  184. alertDialog.show();
  185. }
  186. public void setupUI(View view) {
  187. try {
  188. if (!(view instanceof EditText)) {
  189. view.setOnTouchListener(new View.OnTouchListener() {
  190. @SuppressLint("ClickableViewAccessibility")
  191. public boolean onTouch(View v, MotionEvent event) {
  192. hideSoftKeyboard(SetNewPasswordActivity.this);
  193. return false;
  194. }
  195. });
  196. }
  197. if (view instanceof ViewGroup) {
  198. for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
  199. View innerView = ((ViewGroup) view).getChildAt(i);
  200. setupUI(innerView);
  201. }
  202. }
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. }
  206. }
  207. }