@Override
  public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info =
        (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    Contact contact = ((ContactAdapter) getListAdapter()).getItem(info.position);
    switch (item.getItemId()) {
      case R.id.profile:
        Intent profileIntent = new Intent(getBaseContext(), ContactViewActivity.class);
        profileIntent.putExtra("ContactID", contact.getId());
        startActivity(profileIntent);
        return true;
      case R.id.edit:
        Intent editIntent = new Intent(getBaseContext(), ContactEditActivity.class);
        editIntent.putExtra("ContactID", contact.getId());
        startActivity(editIntent);
        return true;
      case R.id.delete:
        // TODO Maybe a confirmation???
        ContactRepositoryInterface datasource =
            ContactRepositoryFactory.getInstance().getContactRepository(this, this);
        this.contact_adapter.remove(contact);
        datasource.open();
        datasource.delete(contact);
        datasource.close();

        refreshList();
        return true;
      default:
        return super.onContextItemSelected(item);
    }
  }
  private void refreshList() {
    this.contact_adapter.clear();
    ContactRepositoryInterface datasource =
        ContactRepositoryFactory.getInstance().getContactRepository(this, this);
    datasource.open();
    for (Contact c : datasource.all()) {
      this.contact_adapter.add(c);
    }
    datasource.close();
    this.contact_adapter.notifyDataSetChanged();

    EditText search_box = (EditText) findViewById(R.id.search_box);
    contact_adapter.getFilter().filter(search_box.getText().toString());
  }
  @SuppressWarnings("unused")
  private void createNewContacts() {
    ContactRepositoryInterface datasource =
        ContactRepositoryFactory.getInstance().getContactRepository(this, this);
    datasource.open();

    Contact ray =
        new Contact("Ray Tiley")
            .setTitle("Tightrope Media Systems")
            .addEmail("*****@*****.**")
            .setDefaultEmail("*****@*****.**")
            .addPhoneNumber("207-518-8612")
            .addPhoneNumber("866-866-4118")
            .setDefaultTextPhone("207-518-8612")
            .setDefaultContactPhone("866-866-4118");

    Contact tyler =
        new Contact("Tyler Smith")
            .setTitle("General Dynamics")
            .addEmail("*****@*****.**")
            .setDefaultEmail("*****@*****.**")
            .addPhoneNumber("555-555-1000")
            .setDefaultContactPhone("555-555-1000")
            .setDefaultTextPhone("555-555-1000");

    Contact steveA =
        new Contact("Steve Atterbury")
            .setTitle("Lockheed")
            .addEmail("*****@*****.**")
            .setDefaultEmail("*****@*****.**")
            .addPhoneNumber("555-555-2000")
            .setDefaultTextPhone("555-555-2000")
            .setDefaultContactPhone("555-555-2000");

    Contact steveM =
        new Contact("Steve McAdams")
            .setTitle("Lockheed")
            .addEmail("*****@*****.**")
            .addPhoneNumber("555-555-3000");

    ray = datasource.add(ray);
    steveA = datasource.add(steveA);
    tyler = datasource.add(tyler);
    steveM = datasource.add(steveM);

    ray.downloadGravatar(this);
    steveA.downloadGravatar(this);
    tyler.downloadGravatar(this);
    steveM.downloadGravatar(this);

    datasource.close();

    refreshList();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Resources res = getResources();
    setContentView(R.layout.contact_list);
    ToolbarConfig toolbar = new ToolbarConfig(this, res.getString(R.string.contacts));

    // setup the about button
    Button button = toolbar.getToolbarRightButton();
    button.setText(res.getString(R.string.new_contact));
    button.setOnClickListener(this);

    toolbar.hideLeftButton();

    // initialize the list view
    ContactRepositoryInterface datasource =
        ContactRepositoryFactory.getInstance().getContactRepository(this, this);
    datasource.open();

    // Check if the user wants to prepopulate some awesomeness
    // This is awkward since we update the list asynchronously
    /*
    if(datasource.count() == 0) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage("You have no contacts, want us to make some for you?")
                .setTitle("No Contacts")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // Make some contacts.
                        createNewContacts();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int id) {
                        //Do nothing
                    }
                })
                .create()
                .show();
    }
    */

    contact_adapter = new ContactAdapter(this, R.layout.contact_list_item, datasource.all());
    setListAdapter(contact_adapter);
    datasource.close();

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    // setup context menu
    registerForContextMenu(lv);

    // Setup Search
    EditText search_box = (EditText) findViewById(R.id.search_box);
    search_box.addTextChangedListener(
        new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // To change body of implemented methods use File | Settings |
            // File Templates.
          }

          @Override
          public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            ContactListActivity.this.contact_adapter.getFilter().filter(charSequence);
            filtered = true;
          }

          @Override
          public void afterTextChanged(Editable editable) {
            // To change body of implemented methods use File | Settings |
            // File Templates.
          }
        });
  }