/** Creates an activity record from a text line created by toString() */
    public static ActivityRecord parseActivityRecord(String textLine) {

      ActivityRecord result = null;
      String[] fields = null;

      if (textLine != null) {
        try {
          // Splits the string
          fields = textLine.split(FIELDS_REGEXP);

          // Parses fixed lenght part
          if (fields.length > 3) {
            result = new ActivityRecord();

            // Parses the date
            result.date =
                !fields[0].equals(NULL_STRING) ? new Date(Long.parseLong(fields[0])) : null;

            // Parses the state
            result.state = !fields[1].equals(NULL_STRING) ? State.valueOf(fields[1]) : null;

            // Parses the action
            result.action = !fields[2].equals(NULL_STRING) ? StateAction.valueOf(fields[2]) : null;

            // Parses the requested action
            result.requestedAction =
                !fields[3].equals(NULL_STRING) ? RequestedAction.valueOf(fields[3]) : null;
          }

          // Parses variable length part
          if (fields.length > 4) {
            result.payload = new Object[fields.length - 4];
            System.arraycopy(fields, 4, result.payload, 0, fields.length - 4);
            parsePayload(result.payload);
          }

        } catch (Exception e) {
          Log.e(LOGTAG, "Error parsing activity record: " + textLine);
          Log.e(LOGTAG, Log.getStackTraceString(e));
          result = null;
        }
      }

      return result;
    }
    @Override
    public String toString() {

      // Fixed length part: fields 0 to 3.
      String result =
          (date != null ? date.getTime() : NULL_STRING)
              + FIELD_SEPARATOR
              + (state != null ? state.name() : NULL_STRING)
              + FIELD_SEPARATOR
              + (action != null ? action.name() : NULL_STRING)
              + FIELD_SEPARATOR
              + (requestedAction != null ? requestedAction.name() : NULL_STRING)
              + FIELD_SEPARATOR;

      // Variable length part: fields 4 and following.
      if (payload != null) {
        for (int i = 0; i < payload.length; i++) {
          result +=
              (payload[i] != null ? manageFieldSeparator(payload[i].toString(), true) : NULL_STRING)
                  + FIELD_SEPARATOR;
        }
      }
      return result;
    }