protected String getEnrolmentBodyXml(SIP5060ProvisioningRequest req) {

    SharedPreferences settings =
        getSharedPreferences(RegisterAccount.PREFS_FILE, Context.MODE_PRIVATE);

    String enrolmentNum = settings.getString(RegisterAccount.PREF_PHONE_NUMBER, "");

    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    try {
      serializer.setOutput(writer);
      serializer.startDocument("UTF-8", true);
      String ns = RegistrationUtil.NS;
      serializer.startTag(ns, "enrolment");

      RegistrationUtil.serializeProperty(serializer, ns, "phoneNumber", enrolmentNum);
      RegistrationUtil.serializeProperty(serializer, ns, "secret", req.getAuthPassword());
      RegistrationUtil.serializeProperty(serializer, ns, "firstName", "");
      RegistrationUtil.serializeProperty(serializer, ns, "lastName", "");
      RegistrationUtil.serializeProperty(serializer, ns, "emailAddress", "");
      RegistrationUtil.serializeProperty(serializer, ns, "language", getLanguage());

      serializer.endTag(ns, "enrolment");
      serializer.endDocument();
      return writer.toString();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  private SIPIdentity createSIPIdentity(
      AppProperties props, SharedPreferences settings, SIP5060ProvisioningRequest req) {

    SIPIdentity sipIdentity = new SIPIdentity();
    String uri = req.getPhoneNumber() + "@" + props.getSipDomain();
    sipIdentity.setUri(uri);
    sipIdentity.setAuthUser(uri);
    sipIdentity.setAuthPassword(req.getAuthPassword());
    sipIdentity.setReg(true);
    // sipIdentity.setRegServerName(props.getSipServer());
    sipIdentity.setRegServerName("");
    sipIdentity.setRegServerPort(props.getSipPort());
    sipIdentity.setRegServerProtocol(props.getSipProtocol());
    // sipIdentity.setOutboundServerName(props.getSipServer());
    sipIdentity.setOutboundServerName("");
    sipIdentity.setOutboundServerPort(props.getSipPort());
    sipIdentity.setOutboundServerProtocol(props.getSipProtocol());
    sipIdentity.setCarrierRoute(false);
    // sipIdentity.setStunServerName(props.getStunServer());
    sipIdentity.setStunServerName("");
    sipIdentity.setStunServerPort(props.getStunPort());
    sipIdentity.setStunServerProtocol("udp");
    return sipIdentity;
  }
  protected void setupSIP(Context context, SIP5060ProvisioningRequest req) throws IOException {

    AppProperties props = new AppProperties(context);

    // Setup the SIP preferences
    SharedPreferences settings =
        context.getSharedPreferences(RegisterAccount.PREFS_FILE, Context.MODE_PRIVATE);

    SharedPreferences sipSettings =
        context.getSharedPreferences(Settings.sharedPrefsFile, Context.MODE_PRIVATE);
    Editor edSIP = sipSettings.edit();

    String num = req.getPhoneNumber();

    LumicallDataSource ds = new LumicallDataSource(context);
    ds.open();
    SIPIdentity sipIdentity = createSIPIdentity(props, settings, req);
    for (SIPIdentity s : ds.getSIPIdentities()) {
      if (s.getUri().equals(sipIdentity.getUri())) sipIdentity.setId(s.getId());
    }
    ds.persistSIPIdentity(sipIdentity);
    ds.deleteSIP5060ProvisioningRequest(req);
    ds.close();
    edSIP.putString(Settings.PREF_SIP, Long.toString(sipIdentity.getId()));
    if (!sipSettings.contains(Settings.PREF_TEL)) edSIP.putString(Settings.PREF_TEL, "-1");

    /* edSIP.putString(Settings.PREF_USERNAME, settings.getString(RegisterAccount.PREF_PHONE_NUMBER, null));
    edSIP.putString(Settings.PREF_PASSWORD, settings.getString(RegisterAccount.PREF_SECRET, null));
    edSIP.putString(Settings.PREF_SERVER, DEFAULT_SIP_SERVER);
    edSIP.putString(Settings.PREF_DOMAIN, DEFAULT_SIP_DOMAIN);
    edSIP.putString(Settings.PREF_PROTOCOL, "tcp");  // FIXME - change to TLS
    edSIP.putBoolean(Settings.PREF_STUN, true);
    edSIP.putString(Settings.PREF_STUN_SERVER, DEFAULT_STUN_SERVER);
    edSIP.putString(Settings.PREF_STUN_SERVER_PORT, "" + DEFAULT_STUN_SERVER_PORT); */
    edSIP.putBoolean(Settings.PREF_WLAN, true);
    edSIP.putBoolean(Settings.PREF_EDGE, true);
    edSIP.putBoolean(Settings.PREF_3G, true);
    edSIP.putBoolean(Settings.PREF_ON, true);

    if (edSIP.commit()) Log.v(TAG, "Configured prefs for number " + num);
    else {
      Log.e(TAG, "error while committing preferences");
    }

    // Receiver.engine(context).updateDNS();
    Receiver.engine(context).halt();
    Receiver.engine(context).StartEngine();
  }
  protected void handleValidationResponseSMS(String smsText) {

    try {
      Pattern p = Pattern.compile(PHASE2_PATTERN);
      Matcher m = p.matcher(smsText);
      if (!m.matches()) {
        // some other SMS, not for us
        logger.info("ignoring SMS (not for us), body = " + smsText);
        return;
      }
      String validationCode2 = m.group(1);
      String validatedNumber = m.group(2);

      LumicallDataSource ds = new LumicallDataSource(this);
      ds.open();
      List<SIP5060ProvisioningRequest> reqs = ds.getSIP5060ProvisioningRequests();
      if (reqs.size() < 1) {
        logger.severe("no SIP5060ProvisioningRequest");
        throw new RegistrationFailedException("no SIP5060ProvisioningRequest");
      }
      SIP5060ProvisioningRequest req = reqs.get(0);

      req.setPhoneNumber(validatedNumber);
      req.setValidationCode2(validationCode2);

      ds.persistSIP5060ProvisioningRequest(req);
      logger.info(
          "validation reply SMS, code2 = " + validationCode2 + ", my number = " + validatedNumber);

      AppProperties props;
      try {
        props = new AppProperties(this);
      } catch (IOException e) {
        throw new RegistrationFailedException(
            e.getClass().getCanonicalName() + ": " + e.getMessage());
      }

      notification =
          new Notification(
              R.drawable.icon22, getText(R.string.enrolment_submitting_code), new Date().getTime());
      Intent notificationIntent = new Intent(this, RegisterAccount.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notification.setLatestEventInfo(
          this,
          getText(R.string.enrolment_request_title),
          getText(R.string.enrolment_submitting_code),
          contentIntent);
      nm.notify(10, notification);

      String s = getValidationBodyXml(validatedNumber, validationCode2);
      String responseText =
          RegistrationUtil.submitMessage(
              props, "validate", getValidationEncryptedXml(this, s), null);

      if (!responseText.startsWith("DONE")) {
        logger.severe(
            "Bad response from validation server when sending phase2 code, text = " + responseText);
        return;
      }
      notification =
          new Notification(
              R.drawable.icon22, getText(R.string.enrolment_submitted_code), new Date().getTime());
      notificationIntent = new Intent(this, ActivateAccount.class);
      contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notification.setLatestEventInfo(
          this,
          getText(R.string.enrolment_request_title),
          getText(R.string.enrolment_submitted_code),
          contentIntent);
      nm.notify(10, notification);

      // This only gets updated if no exception occurred
      validationDone(this, req);

      ds.close();

    } catch (RegistrationFailedException e) {
      // TODO: display error to user
      Log.e(TAG, e.toString());

      notification =
          new Notification(
              R.drawable.icon22,
              getText(R.string.enrolment_submission_failed),
              new Date().getTime());
      Intent notificationIntent = new Intent(this, RegisterAccount.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notification.setLatestEventInfo(
          this,
          getText(R.string.enrolment_request_title),
          getText(R.string.enrolment_submission_failed),
          contentIntent);
      nm.notify(10, notification);
    }
  }
  protected void doEnrolmentBySMS(Context context) {
    try {
      AppProperties props;
      try {
        props = new AppProperties(context);
      } catch (IOException e) {
        throw new RegistrationFailedException(
            e.getClass().getCanonicalName() + ": " + e.getMessage());
      }

      notification =
          new Notification(
              R.drawable.icon22, getText(R.string.enrolment_request_detail), new Date().getTime());
      Intent notificationIntent = new Intent(this, RegisterAccount.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notification.setLatestEventInfo(
          this,
          getText(R.string.enrolment_request_title),
          getText(R.string.enrolment_request_detail),
          contentIntent);
      nm.notify(10, notification);

      LumicallDataSource ds = new LumicallDataSource(this);
      ds.open();
      List<SIP5060ProvisioningRequest> reqs = ds.getSIP5060ProvisioningRequests();
      if (reqs.size() < 1) {
        logger.severe("no SIP5060ProvisioningRequest");
        throw new RegistrationFailedException("no SIP5060ProvisioningRequest");
      }
      SIP5060ProvisioningRequest req = reqs.get(0);
      String enrolmentMessage = getEnrolmentEncryptedXml(req);
      String numberToDial = RegistrationUtil.submitMessage(props, "enrol", enrolmentMessage, req);
      ds.persistSIP5060ProvisioningRequest(req);

      notification =
          new Notification(
              R.drawable.icon22,
              getText(R.string.enrolment_requested_detail),
              new Date().getTime());
      notificationIntent = new Intent(this, ActivateAccount.class);
      contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notification.setLatestEventInfo(
          this,
          getText(R.string.enrolment_request_title),
          getText(R.string.enrolment_requested_detail),
          contentIntent);
      nm.notify(10, notification);

      logger.info("HTTP response: " + numberToDial);
      if (numberToDial.charAt(0) == '+') {
        sendValidationSMS(context, numberToDial, req.getValidationCode1(), DEFAULT_RETRY_COUNT);
      }
      ds.close();

    } catch (RegistrationFailedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

      notification =
          new Notification(
              R.drawable.icon22, getText(R.string.enrolment_request_failed), new Date().getTime());
      Intent notificationIntent = new Intent(this, RegisterAccount.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notification.setLatestEventInfo(
          this,
          getText(R.string.enrolment_request_title),
          getText(R.string.enrolment_request_failed),
          contentIntent);
      nm.notify(10, notification);
    }
  }