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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* Copyright (C) 2015 Samsung System LSI
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.bluetooth.map;
import android.util.Log;
import com.android.bluetooth.SignedLongLong;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BluetoothMapConvoContactElement
implements Comparable<BluetoothMapConvoContactElement> {
public static final long CONTACT_ID_TYPE_SMS_MMS = 1;
public static final long CONTACT_ID_TYPE_EMAIL = 2;
public static final long CONTACT_ID_TYPE_IM = 3;
private static final String XML_ATT_PRIORITY = "priority";
private static final String XML_ATT_PRESENCE_STATUS = "presence_status";
private static final String XML_ATT_PRESENCE_AVAILABILITY = "presence_availability";
private static final String XML_ATT_X_BT_UID = "x_bt_uid";
private static final String XML_ATT_LAST_ACTIVITY = "last_activity";
private static final String XML_ATT_CHAT_STATE = "chat_state";
private static final String XML_ATT_NAME = "name";
private static final String XML_ATT_DISPLAY_NAME = "display_name";
private static final String XML_ATT_UCI = "x_bt_uci";
protected static final String XML_TAG_CONVOCONTACT = "convocontact";
private static final String TAG = "BluetoothMapConvoContactElement";
private static final boolean D = false;
private static final boolean V = false;
private String mUci = null;
private String mName = null;
private String mDisplayName = null;
private String mPresenceStatus = null;
private int mPresenceAvailability = -1;
private int mPriority = -1;
private long mLastActivity = -1;
private SignedLongLong mBtUid = null;
private int mChatState = -1;
public static BluetoothMapConvoContactElement createFromMapContact(MapContact contact,
String address) {
BluetoothMapConvoContactElement newElement = new BluetoothMapConvoContactElement();
newElement.mUci = address;
// TODO: For now we use the ID as BT-UID
newElement.mBtUid = new SignedLongLong(contact.getId(), 0);
newElement.mDisplayName = contact.getName();
return newElement;
}
public BluetoothMapConvoContactElement(String uci, String name, String displayName,
String presenceStatus, int presenceAvailability, long lastActivity, int chatState,
int priority, String btUid) {
this.mUci = uci;
this.mName = name;
this.mDisplayName = displayName;
this.mPresenceStatus = presenceStatus;
this.mPresenceAvailability = presenceAvailability;
this.mLastActivity = lastActivity;
this.mChatState = chatState;
this.mPresenceStatus = presenceStatus;
this.mPriority = priority;
if (btUid != null) {
try {
this.mBtUid = SignedLongLong.fromString(btUid);
} catch (UnsupportedEncodingException e) {
Log.w(TAG, e);
}
}
}
public BluetoothMapConvoContactElement() {
// TODO Auto-generated constructor stub
}
public String getPresenceStatus() {
return mPresenceStatus;
}
public String getDisplayName() {
return mDisplayName;
}
public void setDisplayName(String displayName) {
this.mDisplayName = displayName;
}
public void setPresenceStatus(String presenceStatus) {
this.mPresenceStatus = presenceStatus;
}
public int getPresenceAvailability() {
return mPresenceAvailability;
}
public void setPresenceAvailability(int presenceAvailability) {
this.mPresenceAvailability = presenceAvailability;
}
public int getPriority() {
return mPriority;
}
public void setPriority(int priority) {
this.mPriority = priority;
}
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public String getBtUid() {
return mBtUid.toHexString();
}
public void setBtUid(SignedLongLong btUid) {
this.mBtUid = btUid;
}
public int getChatState() {
return mChatState;
}
public void setChatState(int chatState) {
this.mChatState = chatState;
}
public void setChatState(String chatState) {
this.mChatState = Integer.valueOf(chatState);
}
public String getLastActivityString() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Date date = new Date(mLastActivity);
return format.format(date); // Format to YYYYMMDDTHHMMSS local time
}
public void setLastActivity(long dateTime) {
this.mLastActivity = dateTime;
}
public void setLastActivity(String lastActivity) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Date date = format.parse(lastActivity);
this.mLastActivity = date.getTime();
}
public void setContactId(String uci) {
this.mUci = uci;
}
public String getContactId() {
return mUci;
}
@Override
public int compareTo(BluetoothMapConvoContactElement e) {
if (this.mLastActivity < e.mLastActivity) {
return 1;
} else if (this.mLastActivity > e.mLastActivity) {
return -1;
} else {
return 0;
}
}
/* Encode the MapConvoContactElement into the StringBuilder reference.
* Here we have taken the choice not to report empty attributes, to reduce the
* amount of data to be transfered over BT. */
public void encode(XmlSerializer xmlConvoElement)
throws IllegalArgumentException, IllegalStateException, IOException {
// construct the XML tag for a single contact in the convolisting element.
xmlConvoElement.startTag(null, XML_TAG_CONVOCONTACT);
if (mUci != null) {
xmlConvoElement.attribute(null, XML_ATT_UCI, mUci);
}
if (mDisplayName != null) {
xmlConvoElement.attribute(null, XML_ATT_DISPLAY_NAME,
BluetoothMapUtils.stripInvalidChars(mDisplayName));
}
if (mName != null) {
xmlConvoElement.attribute(null, XML_ATT_NAME,
BluetoothMapUtils.stripInvalidChars(mName));
}
if (mChatState != -1) {
xmlConvoElement.attribute(null, XML_ATT_CHAT_STATE, String.valueOf(mChatState));
}
if (mLastActivity != -1) {
xmlConvoElement.attribute(null, XML_ATT_LAST_ACTIVITY, this.getLastActivityString());
}
if (mBtUid != null) {
xmlConvoElement.attribute(null, XML_ATT_X_BT_UID, mBtUid.toHexString());
}
if (mPresenceAvailability != -1) {
xmlConvoElement.attribute(null, XML_ATT_PRESENCE_AVAILABILITY,
String.valueOf(mPresenceAvailability));
}
if (mPresenceStatus != null) {
xmlConvoElement.attribute(null, XML_ATT_PRESENCE_STATUS, mPresenceStatus);
}
if (mPriority != -1) {
xmlConvoElement.attribute(null, XML_ATT_PRIORITY, String.valueOf(mPriority));
}
xmlConvoElement.endTag(null, XML_TAG_CONVOCONTACT);
}
/**
* Call this function to create a BluetoothMapConvoContactElement. Will consume the end-tag.
* @param parser must point into XML_TAG_CONVERSATION tag, hence attributes can be read.
* @return
* @throws IOException
* @throws XmlPullParserException
*/
public static BluetoothMapConvoContactElement createFromXml(XmlPullParser parser)
throws ParseException, XmlPullParserException, IOException {
int count = parser.getAttributeCount();
BluetoothMapConvoContactElement newElement;
if (count < 1) {
throw new IllegalArgumentException(
XML_TAG_CONVOCONTACT + " is not decorated with attributes");
}
newElement = new BluetoothMapConvoContactElement();
for (int i = 0; i < count; i++) {
String attributeName = parser.getAttributeName(i).trim();
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase(XML_ATT_UCI)) {
newElement.mUci = attributeValue;
} else if (attributeName.equalsIgnoreCase(XML_ATT_NAME)) {
newElement.mName = attributeValue;
} else if (attributeName.equalsIgnoreCase(XML_ATT_DISPLAY_NAME)) {
newElement.mDisplayName = attributeValue;
} else if (attributeName.equalsIgnoreCase(XML_ATT_CHAT_STATE)) {
newElement.setChatState(attributeValue);
} else if (attributeName.equalsIgnoreCase(XML_ATT_LAST_ACTIVITY)) {
newElement.setLastActivity(attributeValue);
} else if (attributeName.equalsIgnoreCase(XML_ATT_X_BT_UID)) {
newElement.setBtUid(SignedLongLong.fromString(attributeValue));
} else if (attributeName.equalsIgnoreCase(XML_ATT_PRESENCE_AVAILABILITY)) {
newElement.mPresenceAvailability = Integer.parseInt(attributeValue);
} else if (attributeName.equalsIgnoreCase(XML_ATT_PRESENCE_STATUS)) {
newElement.setPresenceStatus(attributeValue);
} else if (attributeName.equalsIgnoreCase(XML_ATT_PRIORITY)) {
newElement.setPriority(Integer.parseInt(attributeValue));
} else {
if (D) {
Log.i(TAG, "Unknown XML attribute: " + parser.getAttributeName(i));
}
}
}
parser.nextTag(); // Consume the end-tag
return newElement;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BluetoothMapConvoContactElement other = (BluetoothMapConvoContactElement) obj;
/* As we use equals only for test, we don't compare auto assigned values
* if (mBtUid == null) {
if (other.mBtUid != null) {
return false;
}
} else if (!mBtUid.equals(other.mBtUid)) {
return false;
}*/
if (mChatState != other.mChatState) {
return false;
}
if (mDisplayName == null) {
if (other.mDisplayName != null) {
return false;
}
} else if (!mDisplayName.equals(other.mDisplayName)) {
return false;
}
/* As we use equals only for test, we don't compare auto assigned values
* if (mId == null) {
if (other.mId != null) {
return false;
}
} else if (!mId.equals(other.mId)) {
return false;
}*/
if (mLastActivity != other.mLastActivity) {
return false;
}
if (mName == null) {
if (other.mName != null) {
return false;
}
} else if (!mName.equals(other.mName)) {
return false;
}
if (mPresenceAvailability != other.mPresenceAvailability) {
return false;
}
if (mPresenceStatus == null) {
if (other.mPresenceStatus != null) {
return false;
}
} else if (!mPresenceStatus.equals(other.mPresenceStatus)) {
return false;
}
if (mPriority != other.mPriority) {
return false;
}
return true;
}
}
|