-
Notifications
You must be signed in to change notification settings - Fork 192
/
Server.java
763 lines (699 loc) · 27 KB
/
Server.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
package im.delight.faceless;
/*
* Copyright (c) delight.im <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
import im.delight.faceless.exceptions.SetupNotCompletedException;
import im.delight.android.location.SimpleLocation;
import im.delight.android.baselib.Collections;
import im.delight.android.baselib.Data;
import im.delight.android.languages.CustomLanguage;
import im.delight.android.webrequest.WebRequest;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.graphics.Color;
import android.preference.PreferenceManager;
public class Server {
public static final int MODE_FRIENDS = 1;
public static final int MODE_POPULAR = 2;
public static final int MODE_LATEST = 3;
public static final int MODE_FAVORITES = 4;
public static final int MODE_SUBSCRIPTIONS = 5;
public static final int MODE_NEARBY = 6;
public static final int STATUS_OK = 1;
public static final int STATUS_MAINTENANCE = 2;
public static final int STATUS_BAD_REQUEST = 3;
public static final int STATUS_OUTDATED_CLIENT = 4;
public static final int STATUS_NOT_AUTHORIZED = 5;
public static final int STATUS_TEMPORARILY_BANNED = 6;
public static final int STATUS_LOGIN_THROTTLED = 7;
public static final int STATUS_NO_CONNECTION = 8;
protected static final int MESSAGES_PER_REQUEST = 50;
public static class Callback {
// instances of this class are not allowed
private Callback() { }
public interface MessageEvent {
public void onReceivedMessages(int status, int mode, int page, boolean reachedEnd, final long latestMessageID, final int subscriptionUpdates, List<Message> messages);
public void onReceivedDetails(int status, boolean isFavorited, boolean isSubscribed);
public void onSentMessage(int status, String messageText, String messageTopic, String messageID, long messageTime, String messageColorHex, int messagePatternID, String messageCountryISO3);
}
public interface CommentEvent {
public void onReceivedComments(int status, List<Comment> comments, boolean hasPrivateComments);
public void onSentComment(int status, String commentText, String commentID, int ownerInThread, int privateRecipientInThread, long commentTime);
}
public interface FavoriteEvent {
public void onChangedFavorite(int status, boolean favorited);
}
public interface SubscriptionEvent {
public void onChangedSubscription(int status, boolean subscribed);
public void onClearedSubscriptions(int status);
}
public interface ConnectionEvent {
public void onAddedFriend(int status);
public void onAddedBlock(int status);
}
public interface ReportEvent {
public void onSentReport(int status);
}
public interface VerificationEvent {
public void onLoadVerification();
public void onPreparedVerification(int status, String apiPhoneNumber, String verificationCode, long validUntil);
}
}
public static class GetMessagesResponse {
public boolean reachedEnd;
public long latestMessageID;
public List<Message> messages;
public int subscriptionUpdates;
}
public static GetMessagesResponse getMessagesSync(final Context context, final int mode, final int page, final SimpleLocation.Point location, final Set<String> topicsList, final int friendsCount) {
WebRequest request = getMessagesRequest(context, mode, page, location, topicsList);
final String responseText = request.executeSync();
final int status = parseStatus(responseText);
if (status == STATUS_OK) {
GetMessagesResponse response = parseMessages(context, mode, page, responseText, friendsCount);
return response;
}
else {
return null;
}
}
public static void getMessagesAsync(final Context context, final int mode, final int page, final SimpleLocation.Point location, final Set<String> topicsList, final int friendsCount, final Callback.MessageEvent callback) {
WebRequest request = getMessagesRequest(context, mode, page, location, topicsList);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
final int status = parseStatus(responseText);
if (status == STATUS_OK) {
GetMessagesResponse response = parseMessages(context, mode, page, responseText, friendsCount);
if (callback != null) {
callback.onReceivedMessages(status, mode, page, response.reachedEnd, response.latestMessageID, response.subscriptionUpdates, response.messages);
}
}
else {
if (callback != null) {
callback.onReceivedMessages(status, mode, page, true, 0, -1, null);
}
}
}
@Override
public void onError() {
if (callback != null) {
callback.onReceivedMessages(STATUS_NO_CONNECTION, mode, page, true, 0, -1, null);
}
}
});
}
public static void getMessageDetails(final Context context, final String messageID, final Callback.MessageEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).get().to("/messages/details");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("messageID", messageID);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
final int status = parseStatus(responseText);
if (status == STATUS_OK) {
try {
final JSONObject responseData = new JSONObject(responseText);
final boolean isFavorited = responseData.getBoolean("isFavorited");
final boolean isSubscribed = responseData.getBoolean("isSubscribed");
if (callback != null) {
callback.onReceivedDetails(status, isFavorited, isSubscribed);
}
}
catch (Exception e) {
if (callback != null) {
callback.onReceivedDetails(status, false, false);
}
}
}
else {
if (callback != null) {
callback.onReceivedDetails(status, false, false);
}
}
}
@Override
public void onError() {
if (callback != null) {
callback.onReceivedDetails(STATUS_NO_CONNECTION, false, false);
}
}
});
}
public static void saveMessage(final Context context, final String colorHex, final int patternID, final String text, final String topic, final String visibility, final SimpleLocation.Point location, final Callback.MessageEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/messages/new");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("colorHex", colorHex);
request.addParam("patternID", patternID);
request.addParam("text", text);
request.addParam("topic", topic);
request.addParam("visibility", visibility);
try {
request.addParam("languageISO3", Locale.getDefault().getISO3Language().toUpperCase(Locale.US));
}
catch (Exception e) { }
String countryIso3 = null;
try {
// get the user's country from the preferences
countryIso3 = PreferenceManager.getDefaultSharedPreferences(context).getString(ActivitySettings.PREF_COUNTRY, null);
// if the country is not set in the preferences yet
if (countryIso3 == null || countryIso3.length() != 3) {
// use the Locale's country property as the default
countryIso3 = Locale.getDefault().getISO3Country().toUpperCase(Locale.US);
if (countryIso3 == null || countryIso3.length() != 3) {
Locale originalLocale = CustomLanguage.getOriginalLocale();
if (originalLocale != null) {
countryIso3 = originalLocale.getISO3Country().toUpperCase(Locale.US);
}
}
}
request.addParam("countryISO3", countryIso3);
}
catch (Exception e) { }
if (location != null) {
request.addParam("location[lat]", location.latitude);
request.addParam("location[long]", location.longitude);
}
final String messageCountryIso3 = countryIso3;
request.addParam("random", UUID.randomUUID().toString());
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
try {
final JSONObject responseData = new JSONObject(responseText);
final String messageID = responseData.getString("messageID");
final long messageTime = responseData.getInt("messageTime") * 1000L;
if (callback != null) {
callback.onSentMessage(parseStatus(responseText), text, topic, messageID, messageTime, colorHex, patternID, messageCountryIso3);
}
}
catch (Exception e) {
if (callback != null) {
callback.onSentMessage(parseStatus(responseText, true), null, null, null, 0, null, 0, null);
}
}
}
@Override
public void onError() {
if (callback != null) {
callback.onSentMessage(STATUS_NO_CONNECTION, null, null, null, 0, null, 0, null);
}
}
});
}
public static void getComments(final Context context, final String messageID, final Callback.CommentEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).get().to("/comments/list");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("messageID", messageID);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
final int status = parseStatus(responseText);
if (status == STATUS_OK) {
List<Comment> comments = new LinkedList<Comment>();
boolean hasPrivateComments = false;
try {
final JSONObject responseData = new JSONObject(responseText);
final JSONArray responseComments = responseData.getJSONArray("comments");
final int responseCommentCount = responseComments.length();
int privateRecipientInThread;
for (int i = 0; i < responseCommentCount; i++) {
JSONObject responseMessage = (JSONObject) responseComments.get(i);
privateRecipientInThread = responseMessage.isNull("privateRecipientInThread") ? 0 : responseMessage.getInt("privateRecipientInThread");
if (privateRecipientInThread > 0) {
hasPrivateComments = true;
}
comments.add(new Comment(responseMessage.getString("id"), responseMessage.getString("text"), privateRecipientInThread, responseMessage.getBoolean("isOwner"), responseMessage.getBoolean("isSelf"), (responseMessage.isNull("ownerInThread") ? 0 : responseMessage.getInt("ownerInThread")), responseMessage.getInt("time") * 1000L));
}
}
catch (Exception e) { }
if (callback != null) {
callback.onReceivedComments(status, comments, hasPrivateComments);
}
}
else {
if (callback != null) {
callback.onReceivedComments(status, null, false);
}
}
}
@Override
public void onError() {
if (callback != null) {
callback.onReceivedComments(STATUS_NO_CONNECTION, null, false);
}
}
});
}
public static void addComment(final Context context, final String messageID, final String privateReplyToCommentID, final String text, final Callback.CommentEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/comments/new");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("messageID", messageID);
if (privateReplyToCommentID != null) {
request.addParam("privateReplyToComment", privateReplyToCommentID);
}
request.addParam("text", text);
request.addParam("random", UUID.randomUUID().toString());
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
try {
final JSONObject responseData = new JSONObject(responseText);
if (callback != null) {
callback.onSentComment(parseStatus(responseText), text, responseData.getString("commentID"), (responseData.isNull("ownerInThread") ? 0 : responseData.getInt("ownerInThread")), (responseData.isNull("privateRecipientInThread") ? 0 : responseData.getInt("privateRecipientInThread")), (responseData.getInt("commentTime") * 1000L));
}
}
catch (Exception e) {
if (callback != null) {
callback.onSentComment(parseStatus(responseText, true), null, null, 0, 0, 0);
}
}
}
@Override
public void onError() {
if (callback != null) {
callback.onSentComment(STATUS_NO_CONNECTION, null, null, 0, 0, 0);
}
}
});
}
public static void setFavorited(final Context context, final String messageID, final boolean favorited, final Callback.FavoriteEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/favorites/set");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("messageID", messageID);
request.addParam("favorited", favorited ? 1 : 0);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
if (callback != null) {
callback.onChangedFavorite(parseStatus(responseText), favorited);
}
}
@Override
public void onError() {
if (callback != null) {
callback.onChangedFavorite(STATUS_NO_CONNECTION, false);
}
}
});
}
public static void setSubscribed(final Context context, final String messageID, final boolean subscribed, final Callback.SubscriptionEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/subscriptions/set");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("messageID", messageID);
request.addParam("subscribed", subscribed ? 1 : 0);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
if (callback != null) {
callback.onChangedSubscription(parseStatus(responseText), subscribed);
}
}
@Override
public void onError() {
if (callback != null) {
callback.onChangedSubscription(STATUS_NO_CONNECTION, false);
}
}
});
}
public static void clearSubscriptions(final Context context, final Callback.SubscriptionEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/subscriptions/clear");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
if (callback != null) {
callback.onClearedSubscriptions(parseStatus(responseText));
}
}
@Override
public void onError() {
if (callback != null) {
callback.onClearedSubscriptions(STATUS_NO_CONNECTION);
}
}
});
}
public static void setFriends(final Context context, final String usernamesCSV, final Callback.ConnectionEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/connections/friend");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("userList", usernamesCSV);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
if (callback != null) {
callback.onAddedFriend(parseStatus(responseText));
}
}
@Override
public void onError() {
if (callback != null) {
callback.onAddedFriend(STATUS_NO_CONNECTION);
}
}
});
}
public static int getFriendsCountSync(final Context context) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).get().to("/connections/friend/count");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
final String responseText = request.executeSync();
final int status = parseStatus(responseText);
if (status == STATUS_OK) {
try {
final JSONObject responseData = new JSONObject(responseText);
return responseData.getInt("friends");
}
catch (Exception e) {
return -1;
}
}
else {
return -1;
}
}
public static void setBlocked(final Context context, final String contentType, final String contentID, final Callback.ConnectionEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/connections/block");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("contentType", contentType);
request.addParam("contentID", contentID);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
if (callback != null) {
callback.onAddedBlock(parseStatus(responseText));
}
}
@Override
public void onError() {
if (callback != null) {
callback.onAddedBlock(STATUS_NO_CONNECTION);
}
}
});
}
public static void sendReport(final Context context, final String contentType, final String contentID, final int reason, final Callback.ReportEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/reports/new");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.addParam("contentType", contentType);
request.addParam("contentID", contentID);
request.addParam("reason", reason);
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
if (callback != null) {
callback.onSentReport(parseStatus(responseText));
}
}
@Override
public void onError() {
if (callback != null) {
callback.onSentReport(STATUS_NO_CONNECTION);
}
}
});
}
public static void prepareVerification(final Context context, final Callback.VerificationEvent callback) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
WebRequest request = new APIRequest(context).post().to("/verifications/prepare");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
request.executeAsync(new WebRequest.Callback() {
@Override
public void onSuccess(String responseText) {
final int status = parseStatus(responseText);
if (status == STATUS_OK) {
String apiPhoneNumber = null;
String verificationCode = null;
long validUntil = 0;
try {
final JSONObject responseData = new JSONObject(responseText);
apiPhoneNumber = responseData.getString("apiPhoneNumber");
verificationCode = responseData.getString("verificationCode");
validUntil = responseData.getLong("validUntil");
}
catch (Exception e) { }
if (callback != null) {
callback.onPreparedVerification(status, apiPhoneNumber, verificationCode, validUntil);
}
}
else {
if (callback != null) {
callback.onPreparedVerification(status, null, null, 0);
}
}
}
@Override
public void onError() {
if (callback != null) {
callback.onPreparedVerification(STATUS_NO_CONNECTION, null, null, 0);
}
}
});
}
protected static WebRequest getMessagesRequest(final Context context, final int mode, final int page, final SimpleLocation.Point location, final Set<String> topicsList) {
if (!Global.Setup.isComplete()) {
throw new SetupNotCompletedException();
}
// add meta category to list of topics as this should be visible to all users
topicsList.add("meta");
WebRequest request = new APIRequest(context).get().to("/messages/list");
request.auth(Global.Setup.getUsername(), Global.Setup.getPassword());
if (mode == MODE_FRIENDS) {
request.addParam("mode", "friends");
}
else if (mode == MODE_POPULAR) {
request.addParam("mode", "popular");
}
else if (mode == MODE_LATEST) {
request.addParam("mode", "latest");
}
else if (mode == MODE_FAVORITES) {
request.addParam("mode", "favorites");
}
else if (mode == MODE_SUBSCRIPTIONS) {
request.addParam("mode", "subscriptions");
}
else if (mode == MODE_NEARBY) {
if (location != null) {
request.addParam("location[lat]", location.latitude);
request.addParam("location[long]", location.longitude);
}
else {
throw new RuntimeException("Location required for nearby mode");
}
request.addParam("mode", "nearby");
}
else {
throw new RuntimeException("Unknown mode: "+mode);
}
request.addParam("page", page);
request.addParam("topicsList", Collections.implode(topicsList, ","));
try {
request.addParam("languageISO3", Locale.getDefault().getISO3Language().toUpperCase(Locale.US));
}
catch (Exception e) { }
return request;
}
protected static GetMessagesResponse parseMessages(final Context context, final int mode, final int page, final String responseText, final int friendsCount) {
List<Message> messages = new LinkedList<Message>();
long oldestMessageTime = System.currentTimeMillis();
long latestMessageID = 0;
int subscriptionUpdates = -1;
if (!Config.DEMO_ACTIVE) {
try {
final JSONObject responseData = new JSONObject(responseText);
final JSONArray responseMessages = responseData.getJSONArray("messages");
final int responseMessageCount = responseMessages.length();
long responseMessageTime;
int messageDegree;
int messageType;
SimpleLocation.Point messageLocation;
for (int i = 0; i < responseMessageCount; i++) {
JSONObject responseMessage = (JSONObject) responseMessages.get(i);
responseMessageTime = responseMessage.getInt("time") * 1000L;
if (responseMessageTime < oldestMessageTime) {
oldestMessageTime = responseMessageTime;
}
try {
latestMessageID = Math.max(latestMessageID, Message.getIDNumber(responseMessage.getString("id")));
}
catch (Exception e) { }
messageDegree = responseMessage.getInt("degree");
messageType = Message.Type.fromProperties(messageDegree, friendsCount);
messageLocation = parseLocation(responseMessage, "location");
messages.add(new Message(responseMessage.getString("id"), messageDegree, responseMessage.getString("colorHex"), responseMessage.getInt("patternID"), responseMessage.getString("text"), responseMessage.getString("topic"), responseMessageTime, responseMessage.getInt("favoritesCount"), responseMessage.getInt("commentsCount"), responseMessage.getString("countryISO3"), messageType, messageLocation, responseMessage.isNull("reasonForBan") ? false : responseMessage.getBoolean("reasonForBan")));
}
subscriptionUpdates = responseData.getInt("subscriptionUpdates");
}
catch (Exception e) {
GetMessagesResponse out = new GetMessagesResponse();
out.reachedEnd = true;
out.latestMessageID = latestMessageID;
out.messages = messages;
out.subscriptionUpdates = subscriptionUpdates;
return out;
}
}
else {
// pretend to have some subscription updates for demo purposes
subscriptionUpdates = 7;
}
// count messages that have been returned from the server
final int messageCount = messages.size();
// if users have not enough messages in their feed yet
if (messageCount < MESSAGES_PER_REQUEST && page == 0 && mode != MODE_SUBSCRIPTIONS && mode != MODE_FAVORITES) {
// get some example messages
List<Message> exampleMessages = getExampleMessages(context, (MESSAGES_PER_REQUEST - messageCount), oldestMessageTime);
// add the examples to the response
messages.addAll(exampleMessages);
}
GetMessagesResponse out = new GetMessagesResponse();
out.reachedEnd = messageCount < MESSAGES_PER_REQUEST;
out.latestMessageID = latestMessageID;
out.messages = messages;
out.subscriptionUpdates = subscriptionUpdates;
return out;
}
protected static SimpleLocation.Point parseLocation(final JSONObject jsonObj, final String locationFieldKey) {
try {
if (!jsonObj.isNull(locationFieldKey)) {
JSONObject locationObj = jsonObj.getJSONObject(locationFieldKey);
if (locationObj != null) {
if (!locationObj.isNull("lat") && !locationObj.isNull("long")) {
return new SimpleLocation.Point(locationObj.getDouble("lat"), locationObj.getDouble("long"));
}
}
}
}
catch (Exception e) { }
return null;
}
protected static List<Message> getExampleMessages(Context context, int number, long startTime) {
final List<Message> out = new LinkedList<Message>();
final String[] exampleMessages = context.getResources().getStringArray(R.array.example_messages);
number = Math.min(number, exampleMessages.length);
int color;
String colorHex;
long messageTime;
int comments;
int messageType;
for (int i = 0; i < number; i++) {
// create arbitrary but constant (on subsequent requests) colors
color = getExampleColor(i);
// turn color into hex value
colorHex = Data.colorToHex(color);
// create arbitrary but constant (on subsequent requests) message timestamps
messageTime = startTime - ((i+1) * 3600L * 12L * 1000L);
if (Config.DEMO_ACTIVE) {
messageType = Message.Type.NORMAL;
}
else {
messageType = Message.Type.EXAMPLE;
}
comments = 12 - (i * 3);
if (comments < 0) {
comments = 0;
}
out.add(new Message(null, ((i+1) % 4), colorHex, i, exampleMessages[i], "life", messageTime, 4, comments, null, messageType, null));
}
return out;
}
protected static int getExampleColor(int n) {
// don't start with a color that is too dark (value produces nice colors and has been retrieved experimentally)
n = n+9;
// use RGB values between 42 and 214 to prevent black and white
// use different slopes for R/G/B to prevent grayscale colors
return Color.rgb((42 + ((n * 45) % 172)), (42 + ((n * 75) % 172)), (42 + ((n * 105) % 172)));
}
protected static int parseStatus(final String responseText) {
return parseStatus(responseText, false);
}
protected static int parseStatus(final String responseText, final boolean requireError) {
try {
JSONObject json = new JSONObject(responseText);
final String status = json.getString("status");
if (status.equals("ok")) {
if (requireError) {
return STATUS_BAD_REQUEST;
}
else {
return STATUS_OK;
}
}
else if (status.equals("maintenance")) {
return STATUS_MAINTENANCE;
}
else if (status.equals("bad_request")) {
return STATUS_BAD_REQUEST;
}
else if (status.equals("outdated_client")) {
return STATUS_OUTDATED_CLIENT;
}
else if (status.equals("not_authorized")) {
return STATUS_NOT_AUTHORIZED;
}
else if (status.equals("temporarily_banned")) {
return STATUS_TEMPORARILY_BANNED;
}
else if (status.equals("login_throttled")) {
return STATUS_LOGIN_THROTTLED;
}
}
catch (Exception e) { }
return STATUS_NO_CONNECTION;
}
}