// Saves the repository to the database
  private boolean saveRepository() {
    String encrypted_username = new String(encrypt_helper.encrypt(username.getText().toString()));
    String encrypted_password = new String(encrypt_helper.encrypt(password.getText().toString()));

    Beans.RepositoryBean cur_repo =
        Beans.RepositoryBean.getInstance(
            edit_repo,
            repo_name.getText().toString(),
            url.getText().toString(),
            repo_types.getSelectedItemPosition(),
            repo_auth.getSelectedItemPosition(),
            encrypted_username,
            encrypted_password,
            new String(encrypt_helper.encrypt(key.getText().toString())),
            encrypt_helper);

    // Make sure edit mode is not on
    if (!edit_mode) {
      // Attempt to add the repository to the application
      if (!db_helper.insert(cur_repo)) {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(R.string.add_repo_adding_error_title);
        alert.setMessage("Could not add repository to the list");
        alert.setPositiveButton(android.R.string.ok, null);
        alert.show();
        return false;
      }

      Toast.makeText(this, R.string.add_repo_added_repo, 1000).show();
    } else {
      // Attempt to update the repository in the database
      if (!db_helper.update(cur_repo)) {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(R.string.add_repo_adding_error_title);
        alert.setMessage("Could not update repository");
        alert.setPositiveButton(android.R.string.ok, null);
        alert.show();
        return false;
      }

      Toast.makeText(this, R.string.add_repo_updated_repo, 1000).show();
    }

    return true;
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_repo);

    // Check to see if extras were passed to the intent. In that case, edit the repository
    if (getIntent().getExtras() != null) {
      Bundle rec_bundle = getIntent().getExtras();

      // Get the repository to edit
      edit_repo = rec_bundle.getLong("editRepo", -1);

      // If the repository id is valid, set the activity to edit mode
      if (edit_repo >= 0) {
        edit_mode = true;
        this.setTitle(R.string.add_repo_edit_title);
      }
    }

    encrypt_helper =
        EncryptionHelper.getInstance("DEADBEEF".toCharArray(), new byte[] {'L', 'O', 'L'});
    db_helper = DatabaseHelper.getInstance(this);

    repo_name = (EditText) findViewById(R.id.repo_name);

    username = (EditText) findViewById(R.id.add_repo_username);
    password = (EditText) findViewById(R.id.add_repo_password);
    password.setRawInputType(
        InputType.TYPE_TEXT_VARIATION_PASSWORD); // Make this into a password box
    password.setTransformationMethod(new PasswordTransformationMethod());

    key = (EditText) findViewById(R.id.add_repo_key);

    url = (EditText) findViewById(R.id.add_repo_url);

    repo_types = (Spinner) findViewById(R.id.repo_type_spinner);
    repo_auth = (Spinner) findViewById(R.id.repo_authentication_spinner);

    // Create a new list for storing the services the program can handle
    ArrayList<String> services = new ArrayList<String>();

    // Add the services supported by the program
    services.add("HGWeb Served");
    services.add("Google Code");
    services.add("Bitbucket");
    services.add("CodePlex");

    // Create a new adapter with the list of services
    AddRepoAdapter type_adapter = new AddRepoAdapter(this, services);

    // Add the adapter to the spinner that stores the list of services
    repo_types.setAdapter(type_adapter);

    // Create a new list to store authentication types
    ArrayList<String> authentication = new ArrayList<String>();

    // Add the authentication types
    authentication.add(0, "None");
    authentication.add(Mercury.AuthenticationTypes.HTTP, "HTTP");
    authentication.add(Mercury.AuthenticationTypes.TOKEN, "Token");
    // authentication.add(SSH, "SSH");

    // Create a new adapter based on the authentication types
    AddRepoAdapter auth_adapter = new AddRepoAdapter(this, authentication);

    // Add the adapter to the authentication spinner
    repo_auth.setAdapter(auth_adapter);

    // Create a listener that triggers whenever the authentication type is changed
    repo_auth.setOnItemSelectedListener(
        new OnItemSelectedListener() {

          @Override
          public void onItemSelected(AdapterView<?> adapterview, View view, int position, long id) {
            // If anything other than "None" is selected, enable the username and password or ssh
            // fields
            if (position > Mercury.AuthenticationTypes.NONE) {
              if (position == Mercury.AuthenticationTypes.TOKEN) {
                if (username.getVisibility() != View.GONE
                    || password.getVisibility() != View.GONE) {
                  // Hide both the username and password if they're not already gone
                  username.setVisibility(View.GONE);
                  password.setVisibility(View.GONE);
                }

                // Make the key text box visible
                key.setVisibility(View.VISIBLE);

                // Enable the controls on the key text box
                key.setEnabled(true);
                key.setFocusable(true);
                key.setClickable(true);
                key.setFocusableInTouchMode(true);

              } else {
                // Make the username and password visible
                username.setVisibility(View.VISIBLE);
                password.setVisibility(View.VISIBLE);

                key.setVisibility(View.GONE);
                key.setText("");

                // Re-enable the username and password text boxes
                username.setEnabled(true);
                username.setCursorVisible(true);
                username.setClickable(true);
                username.setFocusable(true);
                username.setFocusableInTouchMode(true);

                password.setEnabled(true);
                password.setCursorVisible(true);
                password.setClickable(true);
                password.setFocusable(true);
                password.setFocusableInTouchMode(true);
              }
              // Otherwise, disable all fields from user interaction
            } else {
              key.setEnabled(false);
              key.setFocusable(false);
              key.setClickable(false);

              username.setEnabled(false);
              username.setFocusable(false);
              username.setClickable(false);

              password.setEnabled(false);
              password.setClickable(false);
              password.setFocusable(false);
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> arg0) {
            // Will never be called on a spinner
          }
        });

    Button save_button = (Button) findViewById(R.id.add_repo_save);
    Button discard = (Button) findViewById(R.id.add_repo_discard);

    discard.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            killActivity(Activity.RESULT_CANCELED);
          }
        });

    save_button.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            // Check the user input
            if (checkInput()) {
              // Attempt to save the repository
              if (saveRepository())
                // If successful go back to the list
                killActivity(Activity.RESULT_OK);
            }
          }
        });

    if (edit_mode) {
      // Get the information for this repository
      Beans.RepositoryBean repo_info = db_helper.getRepository(edit_repo, null);

      // Set the information in the activity
      repo_name.setText(repo_info.getTitle());
      url.setText(repo_info.getUrl());

      username.setText(repo_info.getUsername());
      password.setText(repo_info.getPassword());
      key.setText(repo_info.getSSHKey());

      repo_types.setSelection(repo_info.getType());
      repo_auth.setSelection(repo_info.getAuthentication());
    }
  }