|
| 1 | +package net.gotev.sipservice; |
| 2 | + |
| 3 | +import android.os.Parcel; |
| 4 | +import android.os.Parcelable; |
| 5 | + |
| 6 | +import androidx.annotation.NonNull; |
| 7 | + |
| 8 | +import java.util.regex.Matcher; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +/** |
| 12 | + * Voice-message state carried by an MWI NOTIFY (RFC 3842). Parcelable so it can ride a |
| 13 | + * single broadcast extra, mirroring {@link RtpStreamStats}. |
| 14 | + * <p> |
| 15 | + * Parsing follows RFC 3842's {@code application/simple-message-summary} body: |
| 16 | + * <ol> |
| 17 | + * <li>the body must be that content type (other event bodies are ignored);</li> |
| 18 | + * <li>{@code Messages-Waiting: yes|no} is the authoritative "are there new messages" |
| 19 | + * flag — it can be {@code yes} with no per-class line at all;</li> |
| 20 | + * <li>the optional {@code Voice-Message: new/old (urgentNew/urgentOld)} line refines it |
| 21 | + * with counts when present.</li> |
| 22 | + * </ol> |
| 23 | + */ |
| 24 | +public class VoicemailStatus implements Parcelable { |
| 25 | + |
| 26 | + private final boolean messagesWaiting; |
| 27 | + private final int newMessages; |
| 28 | + private final int oldMessages; |
| 29 | + private final int urgentNew; |
| 30 | + private final int urgentOld; |
| 31 | + |
| 32 | + VoicemailStatus(boolean messagesWaiting, int newMessages, int oldMessages, int urgentNew, |
| 33 | + int urgentOld) { |
| 34 | + this.messagesWaiting = messagesWaiting; |
| 35 | + this.newMessages = newMessages; |
| 36 | + this.oldMessages = oldMessages; |
| 37 | + this.urgentNew = urgentNew; |
| 38 | + this.urgentOld = urgentOld; |
| 39 | + } |
| 40 | + |
| 41 | + // The only body content type the message-summary event package defines. |
| 42 | + private static final Pattern CONTENT_TYPE = Pattern.compile("Content-Type:\\s*application" + |
| 43 | + "/simple-message-summary", Pattern.CASE_INSENSITIVE); |
| 44 | + // Authoritative waiting flag. |
| 45 | + private static final Pattern MESSAGES_WAITING = Pattern.compile("Messages-Waiting:\\s*" + |
| 46 | + "(yes|no)", Pattern.CASE_INSENSITIVE); |
| 47 | + // Optional per-class counts: <new>/<old>[ (<urgentNew>/<urgentOld>)]. |
| 48 | + private static final Pattern VOICE_MESSAGE = Pattern.compile("Voice-Message:\\s*(\\d+)" + |
| 49 | + "\\s*/\\s*(\\d+)(?:\\s*\\(\\s*(\\d+)\\s*/\\s*(\\d+)\\s*\\))?", |
| 50 | + Pattern.CASE_INSENSITIVE); |
| 51 | + |
| 52 | + /** |
| 53 | + * Parse the whole NOTIFY message. Returns an all-clear status when the body isn't a |
| 54 | + * simple-message-summary or is empty/unparseable. Never throws. |
| 55 | + */ |
| 56 | + public static VoicemailStatus parse(String wholeMessage) { |
| 57 | + if (wholeMessage == null || wholeMessage.isEmpty()) { |
| 58 | + return none(); |
| 59 | + } |
| 60 | + // 1) Only interpret simple-message-summary bodies. |
| 61 | + if (!CONTENT_TYPE.matcher(wholeMessage).find()) { |
| 62 | + return none(); |
| 63 | + } |
| 64 | + // 2) Messages-Waiting is the source of truth for "something is waiting". |
| 65 | + final Matcher waitingMatcher = MESSAGES_WAITING.matcher(wholeMessage); |
| 66 | + final boolean waiting = |
| 67 | + waitingMatcher.find() && "yes".equalsIgnoreCase(waitingMatcher.group(1)); |
| 68 | + // 3) Refine with per-class counts when the optional line is present. |
| 69 | + final Matcher voiceMatcher = VOICE_MESSAGE.matcher(wholeMessage); |
| 70 | + if (voiceMatcher.find()) { |
| 71 | + return new VoicemailStatus(waiting, safeParse(voiceMatcher.group(1)), |
| 72 | + safeParse(voiceMatcher.group(2)), safeParse(voiceMatcher.group(3)), |
| 73 | + safeParse(voiceMatcher.group(4))); |
| 74 | + } |
| 75 | + return new VoicemailStatus(waiting, 0, 0, 0, 0); |
| 76 | + } |
| 77 | + |
| 78 | + private static VoicemailStatus none() { |
| 79 | + return new VoicemailStatus(false, 0, 0, 0, 0); |
| 80 | + } |
| 81 | + |
| 82 | + private static int safeParse(String value) { |
| 83 | + if (value == null) return 0; |
| 84 | + try { |
| 85 | + return Integer.parseInt(value.trim()); |
| 86 | + } catch (NumberFormatException e) { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static final Parcelable.Creator<VoicemailStatus> CREATOR = |
| 92 | + new Parcelable.Creator<VoicemailStatus>() { |
| 93 | + @Override |
| 94 | + public VoicemailStatus createFromParcel(final Parcel in) { |
| 95 | + return new VoicemailStatus(in); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public VoicemailStatus[] newArray(final int size) { |
| 100 | + return new VoicemailStatus[size]; |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + private VoicemailStatus(Parcel in) { |
| 105 | + this.messagesWaiting = in.readByte() != 0; |
| 106 | + this.newMessages = in.readInt(); |
| 107 | + this.oldMessages = in.readInt(); |
| 108 | + this.urgentNew = in.readInt(); |
| 109 | + this.urgentOld = in.readInt(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void writeToParcel(Parcel parcel, int flags) { |
| 114 | + parcel.writeByte((byte) (messagesWaiting ? 1 : 0)); |
| 115 | + parcel.writeInt(newMessages); |
| 116 | + parcel.writeInt(oldMessages); |
| 117 | + parcel.writeInt(urgentNew); |
| 118 | + parcel.writeInt(urgentOld); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int describeContents() { |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + public boolean getMessagesWaiting() { |
| 127 | + return messagesWaiting; |
| 128 | + } |
| 129 | + |
| 130 | + public int getNewMessages() { |
| 131 | + return newMessages; |
| 132 | + } |
| 133 | + |
| 134 | + public int getOldMessages() { |
| 135 | + return oldMessages; |
| 136 | + } |
| 137 | + |
| 138 | + public int getUrgentNew() { |
| 139 | + return urgentNew; |
| 140 | + } |
| 141 | + |
| 142 | + public int getUrgentOld() { |
| 143 | + return urgentOld; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * True when the mailbox has messages waiting. Per RFC 3842 the {@code Messages-Waiting} |
| 148 | + * flag is authoritative and is always {@code yes} when new messages exist, so it alone |
| 149 | + * decides this. |
| 150 | + */ |
| 151 | + public boolean hasNewMessages() { |
| 152 | + return messagesWaiting; |
| 153 | + } |
| 154 | + |
| 155 | + @NonNull |
| 156 | + @Override |
| 157 | + public String toString() { |
| 158 | + return "VoicemailStatus{waiting=" + messagesWaiting + ", new=" + newMessages + ", old=" + oldMessages + ", urgentNew=" + urgentNew + ", urgentOld=" + urgentOld + "}"; |
| 159 | + } |
| 160 | +} |
0 commit comments