private void getPliResponse(ByteArrayOutputStream buf) {

    // Locale Language Setting
    String lang = SystemProperties.get("persist.sys.language");

    if (lang != null) {
      // tag
      int tag = ComprehensionTlvTag.LANGUAGE.value();
      buf.write(tag);
      ResponseData.writeLength(buf, lang.length());
      buf.write(lang.getBytes(), 0, lang.length());
    }
  }
  @Override
  public void format(ByteArrayOutputStream buf) {
    if (buf == null) {
      return;
    }

    // Text string object
    int tag = 0x80 | ComprehensionTlvTag.LANGUAGE.value();
    buf.write(tag); // tag

    byte[] data;

    if (mLang != null && mLang.length() > 0) {
      data = GsmAlphabet.stringToGsm8BitPacked(mLang);
    } else {
      data = new byte[0];
    }

    buf.write(data.length);

    for (byte b : data) {
      buf.write(b);
    }
  }
  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);
  }