content: package org.snowcorp.login;
  
  import android.annotation.SuppressLint;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.CountDownTimer;
  import android.util.Log;
  import android.view.View;
  import android.view.WindowManager;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import com.android.volley.Request;
  import com.android.volley.toolbox.StringRequest;
  import com.google.android.material.button.MaterialButton;
  import com.google.android.material.textfield.TextInputLayout;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  import org.snowcorp.login.helper.DatabaseHandler;
  import org.snowcorp.login.helper.Functions;
  import org.snowcorp.login.helper.SessionManager;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Objects;
  import java.util.concurrent.TimeUnit;
  
  /**
   * Created by Akshay Raj on 06-02-2017.
   * akshay@snowcorp.org
   * www.snowcorp.org
   */
  public class EmailVerify extends AppCompatActivity {
      private static final String TAG = EmailVerify.class.getSimpleName();
  
      private TextInputLayout textVerifyCode;
      private MaterialButton btnVerify, btnResend;
      private TextView otpCountDown;
  
      private SessionManager session;
      private DatabaseHandler db;
  
      private static final String FORMAT = "%02d:%02d";
  
      Bundle bundle;
  
      private static String KEY_UID = "uid";
      private static String KEY_NAME = "name";
      private static String KEY_EMAIL = "email";
      private static String KEY_CREATED_AT = "created_at";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_email_verify);
  
          textVerifyCode = findViewById(R.id.verify_code);
          btnVerify = findViewById(R.id.btnVerify);
          btnResend = findViewById(R.id.btnResendCode);
          otpCountDown = findViewById(R.id.otpCountDown);
  
          bundle = getIntent().getExtras();
  
          db = new DatabaseHandler(getApplicationContext());
          session = new SessionManager(getApplicationContext());
  
          // Hide Keyboard
          getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  
          init();
      }
  
      private void init() {
          btnVerify.setOnClickListener(v -> {
              // Hide Keyboard
              Functions.hideSoftKeyboard(EmailVerify.this);
  
              String email = bundle.getString("email");
              String otp = Objects.requireNonNull(textVerifyCode.getEditText()).getText().toString();
  
              if (!otp.isEmpty()) {
                  verifyCode(email, otp);
                  textVerifyCode.setErrorEnabled(false);
              } else {
                  textVerifyCode.setError("Please enter verification code");
              }
          });
  
          btnResend.setEnabled(false);
          btnResend.setOnClickListener(v -> {
              String email = bundle.getString("email");
              resendCode(email);
          });
  
          countDown();
      }
  
      private void countDown() {
          new CountDownTimer(70000, 1000) { // adjust the milli seconds here
  
              @SuppressLint({"SetTextI18n", "DefaultLocale"})
              public void onTick(long millisUntilFinished) {
                  otpCountDown.setVisibility(View.VISIBLE);
                  otpCountDown.setText(""+String.format(FORMAT,
                          TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                          TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
                                  TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)) ));
              }
  
              public void onFinish() {
                  otpCountDown.setVisibility(View.GONE);
                  btnResend.setEnabled(true);
              }
          }.start();
      }
  
      private void verifyCode(final String email, final String otp) {
          // Tag used to cancel the request
          String tag_string_req = "req_verify_code";
  
          showDialog("Checking in ...");
  
          StringRequest strReq = new StringRequest(Request.Method.POST,
                  Functions.OTP_VERIFY_URL, response -> {
                      Log.d(TAG, "Verification Response: " + response);
                      hideDialog();
  
                      try {
                          JSONObject jObj = new JSONObject(response);
                          boolean error = jObj.getBoolean("error");
  
                          // Check for error node in json
                          if (!error) {
                              JSONObject json_user = jObj.getJSONObject("user");
  
                              Functions logout = new Functions();
                              logout.logoutUser(getApplicationContext());
                              db.addUser(json_user.getString(KEY_UID), json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json_user.getString(KEY_CREATED_AT));
                              session.setLogin(true);
                              Intent upanel = new Intent(EmailVerify.this, HomeActivity.class);
                              upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                              startActivity(upanel);
                              finish();
  
                          } else {
                              Toast.makeText(getApplicationContext(), "Invalid Verification Code", Toast.LENGTH_LONG).show();
                              textVerifyCode.setError("Invalid Verification Code");
                          }
                      } catch (JSONException e) {
                          // JSON error
                          e.printStackTrace();
                          Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                      }
  
                  }, error -> {
              Log.e(TAG, "Verify Code Error: " + error.getMessage());
              Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
              hideDialog();
          }) {
  
              @Override
              protected Map<String, String> getParams() {
                  // Posting parameters to login url
                  Map<String, String> params = new HashMap<>();
  
                  params.put("tag", "verify_code");
                  params.put("email", email);
                  params.put("otp", otp);
  
                  return params;
              }
  
              @Override
              public Map<String, String> getHeaders() {
                  Map<String,String> params = new HashMap<>();
                  params.put("Content-Type","application/x-www-form-urlencoded");
                  return params;
              }
  
          };
          // Adding request to request queue
          MyApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
      }
  
      private void resendCode(final String email) {
          // Tag used to cancel the request
          String tag_string_req = "req_resend_code";
  
          showDialog("Resending code ...");
  
          StringRequest strReq = new StringRequest(Request.Method.POST,
                  Functions.OTP_VERIFY_URL, response -> {
                      Log.d(TAG, "Resend Code Response: " + response);
                      hideDialog();
  
                      try {
                          JSONObject jObj = new JSONObject(response);
                          boolean error = jObj.getBoolean("error");
  
                          // Check for error node in json
                          if (!error) {
                              Toast.makeText(getApplicationContext(), "Code successfully sent to your email!", Toast.LENGTH_LONG).show();
                              btnResend.setEnabled(false);
                              countDown();
                          } else {
                              Toast.makeText(getApplicationContext(), "Code sending failed!", Toast.LENGTH_LONG).show();
                          }
                      } catch (JSONException e) {
                          // JSON error
                          e.printStackTrace();
                          Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                      }
  
                  }, error -> {
              Log.e(TAG, "Resend Code Error: " + error.getMessage());
              Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
              hideDialog();
          }) {
              @Override
              protected Map<String, String> getParams() {
                  // Posting parameters to login url
                  Map<String, String> params = new HashMap<>();
  
                  params.put("tag", "resend_code");
                  params.put("email", email);
  
                  return params;
              }
  
              @Override
              public Map<String, String> getHeaders() {
                  Map<String,String> params = new HashMap<>();
                  params.put("Content-Type","application/x-www-form-urlencoded");
                  return params;
              }
  
          };
          // Adding request to request queue
          MyApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
      }
  
      private void showDialog(String title) {
          Functions.showProgressDialog(EmailVerify.this, title);
      }
  
      private void hideDialog() {
          Functions.hideProgressDialog(EmailVerify.this);
      }
  
      @Override
      public void onResume(){
          super.onResume();
          countDown();
      }
  }
  