@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   switch (requestCode) {
     case REQUEST_ENABLE_BLUETOOTH:
       if (resultCode == BluetoothService.BLUETOOTH_DISCOVERABLE_DURATION) {
         bService.bluetoothDiscoverable();
       } else {
         // User did not enable Bluetooth or an error occured
       }
       break;
   }
 }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_view_tags:
        // little hack to refresh the data in our tag list view when we open it
        ListView lv = (ListView) viewTagList.findViewById(R.id.item_list);
        ((TagViewListAdaptor) lv.getAdapter()).refreshData();
        setContentView(viewTagList);
        return true;
      case R.id.menu_delete_note:
        Note n = ((NoteView) viewNote).getNote();
        db.deleteTag(n.getId());
        Toast.makeText(SBActivity.this, "Note " + n.getId() + " deleted.", Toast.LENGTH_SHORT)
            .show();
        goBack();
        return true;
      case R.id.menu_save_note:
        setContentView(viewCaptureNote);
        return true;
      case R.id.menu_tag_note:
        final Note curNote = ((NoteView) viewNote).getNote();
        final Cursor c = db.getTagListForImg(curNote.getId());
        CharSequence[] items = new CharSequence[c.getCount()];
        for (int i = 0; i < items.length; i++) {
          c.moveToNext();
          items[i] = c.getString(1);
        }

        final boolean[] checked = new boolean[c.getCount()];
        c.moveToFirst();
        for (int i = 0; i < items.length; i++) {
          checked[i] = c.getInt(2) == 1;
          c.moveToNext();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
            .setTitle("Set tags for note " + curNote.getId())
            .setIcon(android.R.drawable.ic_menu_edit)
            .setMultiChoiceItems(
                items,
                checked,
                new DialogInterface.OnMultiChoiceClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                    c.moveToPosition(i);
                    db.setTagForNote(curNote.getId(), c.getLong(0), b);
                  }
                });
        builder.show();
        return true;
      case R.id.menu_bluetooth_sync:
        Cursor c1 = getDbAdaptor().getUpdateList(null);
        int idxId = c1.getColumnIndex("_id");
        int idxTable = c1.getColumnIndex("table_name");
        int idxTarget = c1.getColumnIndex("target_id");
        int idxTarget2 = c1.getColumnIndex("target2_id");
        int idxType = c1.getColumnIndex("update_type");
        int idxTime = c1.getColumnIndex("update_time");
        Log.i(getClass().getName(), "Update list:");
        while (c1.moveToNext()) {
          Log.i(
              getClass().getName(),
              " [_id="
                  + c1.getInt(idxId)
                  + "] "
                  + (c1.getInt(idxType) == DbAdaptor.UPDATE_TYPE_ADD ? "Added" : "Deleted")
                  + " "
                  + "id "
                  + c1.getInt(idxTarget)
                  + (!c1.isNull(idxTarget2) ? " (id2 " + c1.getInt(idxTarget2) + ")" : "")
                  + " in table "
                  + c1.getString(idxTable)
                  + " (@ "
                  + new Date(c1.getInt(idxTime))
                  + ")");
        }

        if (bService.requestEnableBluetooth()) bService.bluetoothDiscoverable();
        return true;
    }
    return false;
  }