/
CallStartMessageContent.java
233 lines (190 loc) · 6.15 KB
/
CallStartMessageContent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (c) 2020 WildFireChat. All rights reserved.
*/
package cn.wildfirechat.message;
import static cn.wildfirechat.message.core.MessageContentType.ContentType_Call_Start;
import android.os.Parcel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import cn.wildfirechat.message.core.ContentTag;
import cn.wildfirechat.message.core.MessagePayload;
import cn.wildfirechat.message.core.PersistFlag;
/**
* Created by heavyrain lee on 2017/12/6.
*/
@ContentTag(type = ContentType_Call_Start, flag = PersistFlag.Persist_And_Count)
public class CallStartMessageContent extends MessageContent {
private String callId;
// 多人视音频是有效
private List<String> targetIds;
private long connectTime;
private long endTime;
private boolean audioOnly;
private String pin;
/**
* 0, UnKnown,
* 1, Busy,
* 2, SignalError,
* 3, Hangup,
* 4, MediaError,
* 5, RemoteHangup,
* 6, OpenCameraFailure,
* 7, Timeout,
* 8, AcceptByOtherClient
*/
private int status;
public CallStartMessageContent() {
}
public CallStartMessageContent(String callId, List<String> targetIds, boolean audioOnly) {
this.callId = callId;
this.audioOnly = audioOnly;
this.targetIds = targetIds;
}
public String getCallId() {
return callId;
}
public void setCallId(String callId) {
this.callId = callId;
}
public long getConnectTime() {
return connectTime;
}
public void setConnectTime(long connectTime) {
this.connectTime = connectTime;
}
public long getEndTime() {
return endTime;
}
public void setEndTime(long endTime) {
this.endTime = endTime;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public boolean isAudioOnly() {
return audioOnly;
}
public void setAudioOnly(boolean audioOnly) {
this.audioOnly = audioOnly;
}
public List<String> getTargetIds() {
return targetIds;
}
public void setTargetIds(List<String> targetIds) {
this.targetIds = targetIds;
}
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
@Override
public MessagePayload encode() {
MessagePayload payload = super.encode();
payload.content = callId;
payload.pushContent = "音视频通话邀请";
try {
JSONObject objWrite = new JSONObject();
if (connectTime > 0) {
objWrite.put("c", connectTime);
}
if (endTime > 0) {
objWrite.put("e", endTime);
}
if (status > 0) {
objWrite.put("s", status);
}
objWrite.put("t", targetIds.get(0));
JSONArray ts = new JSONArray(targetIds);
objWrite.put("ts", ts);
objWrite.put("a", audioOnly ? 1 : 0);
objWrite.put("p", pin);
payload.binaryContent = objWrite.toString().getBytes();
JSONObject pushDataWrite = new JSONObject();
pushDataWrite.put("callId", callId);
pushDataWrite.put("audioOnly", audioOnly);
if(targetIds != null && targetIds.size() > 0) {
pushDataWrite.put("participants", targetIds);
}
payload.pushData = pushDataWrite.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return payload;
}
@Override
public void decode(MessagePayload payload) {
callId = payload.content;
pushContent = payload.pushContent;
try {
if (payload.binaryContent != null) {
JSONObject jsonObject = new JSONObject(new String(payload.binaryContent));
connectTime = jsonObject.optLong("c", 0);
endTime = jsonObject.optLong("e", 0);
status = jsonObject.optInt("s", 0);
pin = jsonObject.optString("p");
JSONArray array = jsonObject.optJSONArray("ts");
targetIds = new ArrayList<>();
if (array == null) {
targetIds.add(jsonObject.getString("t"));
} else {
for (int i = 0; i < array.length(); i++) {
if (array.get(i) instanceof String) {
targetIds.add((String) array.get(i));
}
}
}
audioOnly = jsonObject.optInt("a") > 0;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public String digest(Message message) {
return "[网络电话]";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.callId);
dest.writeStringList(this.targetIds);
dest.writeLong(this.connectTime);
dest.writeLong(this.endTime);
dest.writeByte(this.audioOnly ? (byte) 1 : (byte) 0);
dest.writeInt(this.status);
dest.writeString(this.pin);
}
protected CallStartMessageContent(Parcel in) {
super(in);
this.callId = in.readString();
this.targetIds = new ArrayList<>();
in.readStringList(this.targetIds);
this.connectTime = in.readLong();
this.endTime = in.readLong();
this.audioOnly = in.readByte() != 0;
this.status = in.readInt();
this.pin = in.readString();
}
public static final Creator<CallStartMessageContent> CREATOR = new Creator<CallStartMessageContent>() {
@Override
public CallStartMessageContent createFromParcel(Parcel source) {
return new CallStartMessageContent(source);
}
@Override
public CallStartMessageContent[] newArray(int size) {
return new CallStartMessageContent[size];
}
};
}