private void eventDownload(
      int event, int sourceId, int destinationId, byte[] additionalInfo, boolean oneShot) {

    ByteArrayOutputStream buf = new ByteArrayOutputStream();

    // tag
    int tag = BerTlv.BER_EVENT_DOWNLOAD_TAG;
    buf.write(tag);

    // length
    buf.write(0x00); // place holder, assume length < 128.

    // event list
    tag = 0x80 | ComprehensionTlvTag.EVENT_LIST.value();
    buf.write(tag);
    buf.write(0x01); // length
    buf.write(event); // event value

    // device identities
    tag = 0x80 | ComprehensionTlvTag.DEVICE_IDENTITIES.value();
    buf.write(tag);
    buf.write(0x02); // length
    buf.write(sourceId); // source device id
    buf.write(destinationId); // destination device id

    // additional information
    if (additionalInfo != null) {
      for (byte b : additionalInfo) {
        buf.write(b);
      }
    }

    byte[] rawData = buf.toByteArray();

    // write real length
    int len = rawData.length - 2; // minus (tag + length)
    rawData[1] = (byte) len;

    String hexString = IccUtils.bytesToHexString(rawData);

    mCmdIf.sendEnvelope(hexString, null);
  }
  private void sendMenuSelection(int menuId, boolean helpRequired) {

    ByteArrayOutputStream buf = new ByteArrayOutputStream();

    // tag
    int tag = BerTlv.BER_MENU_SELECTION_TAG;
    buf.write(tag);

    // length
    buf.write(0x00); // place holder

    // device identities
    tag = 0x80 | ComprehensionTlvTag.DEVICE_IDENTITIES.value();
    buf.write(tag);
    buf.write(0x02); // length
    buf.write(DEV_ID_KEYPAD); // source device id
    buf.write(DEV_ID_UICC); // destination device id

    // item identifier
    tag = 0x80 | ComprehensionTlvTag.ITEM_ID.value();
    buf.write(tag);
    buf.write(0x01); // length
    buf.write(menuId); // menu identifier chosen

    // help request
    if (helpRequired) {
      tag = ComprehensionTlvTag.HELP_REQUEST.value();
      buf.write(tag);
      buf.write(0x00); // length
    }

    byte[] rawData = buf.toByteArray();

    // write real length
    int len = rawData.length - 2; // minus (tag + length)
    rawData[1] = (byte) len;

    String hexString = IccUtils.bytesToHexString(rawData);

    mCmdIf.sendEnvelope(hexString, null);
  }
  private void eventDownload(
      int event, int sourceId, int destinationId, byte[] additionalInfo, boolean oneShot) {

    ByteArrayOutputStream buf = new ByteArrayOutputStream();

    // tag
    int tag = BerTlv.BER_EVENT_DOWNLOAD_TAG;
    buf.write(tag);

    // length
    buf.write(0x00); // place holder, assume length < 128.

    // event list
    tag = 0x80 | ComprehensionTlvTag.EVENT_LIST.value();
    buf.write(tag);
    buf.write(0x01); // length
    buf.write(event); // event value

    // device identities
    tag = 0x80 | ComprehensionTlvTag.DEVICE_IDENTITIES.value();
    buf.write(tag);
    buf.write(0x02); // length
    buf.write(sourceId); // source device id
    buf.write(destinationId); // destination device id

    /*
     * Check for type of event download to be sent to UICC - Browser
     * termination,Idle screen available, User activity, Language selection
     * etc as mentioned under ETSI TS 102 223 section 7.5
     */

    /*
     * Currently the below events are supported:
     * Idle Screen Available,
     * Language Selection Event and
     * HCI Connectivity.
     * Other event download commands should be encoded similar way
     */
    /* TODO: eventDownload should be extended for other Envelope Commands */
    switch (event) {
      case IDLE_SCREEN_AVAILABLE_EVENT:
        CatLog.d(this, " Sending Idle Screen Available event download to ICC");
        break;
      case LANGUAGE_SELECTION_EVENT:
        CatLog.d(this, " Sending Language Selection event download to ICC");
        tag = 0x80 | ComprehensionTlvTag.LANGUAGE.value();
        buf.write(tag);
        // Language length should be 2 byte
        buf.write(0x02);
        break;
      case HCI_CONNECTIVITY_EVENT:
        CatLog.d(this, " Sending HCI Connectivity event download to ICC");
        break;
      default:
        break;
    }

    // additional information
    if (additionalInfo != null) {
      for (byte b : additionalInfo) {
        buf.write(b);
      }
    }

    byte[] rawData = buf.toByteArray();

    // write real length
    int len = rawData.length - 2; // minus (tag + length)
    rawData[1] = (byte) len;

    String hexString = IccUtils.bytesToHexString(rawData);

    CatLog.d(this, "ENVELOPE COMMAND: " + hexString);

    mCmdIf.sendEnvelope(hexString, null);
  }