/**
   * on button click
   *
   * @param view view
   */
  public void allowPassword(View view) {

    sendPassword(
        password.getValue(Password.KEY_PASSWORD),
        password.getValue(Password.KEY_USERNAME),
        sessionID);

    finish();
  }
  public void savePassword(View view) {
    EditText passwordField = (EditText) findViewById(R.id.password);
    String passwordText = passwordField.getText().toString();

    EditText usernameField = (EditText) findViewById(R.id.username);
    username = usernameField.getText().toString();

    JSONObject extra = new JSONObject();
    try {
      extra.put("elementId", elementId);
      extra.put("elementName", elementName);
    } catch (JSONException e) {
      e.printStackTrace();
    }

    if (password == null) {
      password = new Password();
    }

    password.setValue(Password.KEY_LOCATION, location);
    password.setValue(Password.KEY_DOMAIN, domain);
    password.setValue(Password.KEY_TITLE, title);
    password.setValue(Password.KEY_USERNAME, username);
    password.setValue(Password.KEY_ICON, Password.findAutoIcon(domain));
    password.setValue(Password.KEY_PASSWORD, passwordText);

    sendPassword(
        password.getValue(Password.KEY_PASSWORD),
        password.getValue(Password.KEY_USERNAME),
        sessionID);

    databaseHelper.savePassword(password);

    Toast.makeText(this, "Password send and saved", Toast.LENGTH_SHORT).show();

    finish();
  }
  public void ignorePassword(View view) {
    password.putExtra("ignore", true);

    databaseHelper.savePassword(password);

    try {
      JSONObject jsonObject = new JSONObject();

      jsonObject.put("inResponseto", sessionID);

      JSONObject message = new JSONObject();
      message.put("action", "ignore");

      jsonObject.put("message", encrypt(message));

      Logger.log("sendPassword", jsonObject.toString());

      JsonObjectRequest request =
          new JsonObjectRequest(
              Request.Method.POST,
              getResources().getString(R.string.rest_generic),
              jsonObject.toString(),
              new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                  Log.w("response", response.toString());
                }
              },
              null);

      requestQueue.add(request);

    } catch (JSONException e) {
      e.printStackTrace();
    }

    finish();
  }
  private void handleResponse(JSONObject response) throws Exception {

    if ("{}".equals(response.toString())) { // no session is open
      throw new Exception("aasdgasg");
    }

    browserId = Utils.jsonObjectGetString(response, "browser_id");
    sessionID = Utils.jsonObjectGetString(response, "id");

    Browser browser = databaseHelper.getBrowser(browserId);

    // preload public key
    preparePublicKey = new PreparePublicKey();
    preparePublicKey.execute(browser);

    JSONObject encryptedMessage = Utils.jsonObjectGetJSONObject(response, "message");

    if ("{}".equals(encryptedMessage.toString())) {
      throw new Exception("aasdgasg");
    }

    JSONObject message =
        Utils.getJSONObject(Utils.decrypt(encryptedMessage, browser.getMy_private_key()));

    Logger.log("Received", message.toString());

    location = Utils.jsonObjectGetString(message, "location");
    domain = Utils.jsonObjectGetString(message, "domain");
    title = Utils.jsonObjectGetString(message, "title");
    username = Utils.jsonObjectGetString(message, "username");
    passwordString = Utils.jsonObjectGetString(message, "password");
    elementId = Utils.jsonObjectGetString(message, "element_id");
    elementName = Utils.jsonObjectGetString(message, "element_name");

    Logger.log("search by domain", domain);

    password = databaseHelper.getPasswordByDomain(domain);

    setContentView(R.layout.activity_login);

    if (password == null) { // no password is known for the domain

      Logger.log("register", "register");

      TextView loginTitle = (TextView) findViewById(R.id.login_title);
      loginTitle.setText(domain);
      ((Button) findViewById(R.id.button_allow)).setText(R.string.allow_and_save);
      findViewById(R.id.edit).setVisibility(View.VISIBLE);

      updateUsernamePassword(username, passwordString);

      EditText passwordField = (EditText) findViewById(R.id.password);
      passwordField.setOnFocusChangeListener(
          new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
              if (!hasFocus) {
                hideKeyboard(v);
              }
            }
          });

    } else {
      Logger.log("login", "login");

      TextView loginTitle = (TextView) findViewById(R.id.login_title);
      loginTitle.setText(domain);

      updateUsernamePassword(
          password.getValue(Password.KEY_USERNAME), password.getValue(Password.KEY_PASSWORD));
    }
  }