Creating a WhatsApp Clone - Part II
* need additional information or have any questions.
*/
package com.codename1.whatsapp.model;
import com.codename1.contacts.Contact;
import com.codename1.io.JSONParser;
import com.codename1.io.Log;
import com.codename1.io.Preferences;
import com.codename1.io.Util;
import com.codename1.io.rest.RequestBuilder;
import com.codename1.io.rest.Response;
import com.codename1.io.rest.Rest;
import com.codename1.io.websocket.WebSocket;
import com.codename1.properties.PropertyIndex;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.EncodedImage;
import com.codename1.util.EasyThread;
import com.codename1.util.OnComplete;
import com.codename1.util.regex.StringReader;
import java.io.IOException;
import java.io.InputStream;
Server
import com.codename1.ui.Display;
import com.codename1.ui.EncodedImage;
import com.codename1.util.EasyThread;
import com.codename1.util.OnComplete;
import com.codename1.util.regex.StringReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Server {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String WEBSOCKER_URL = "ws://localhost:8080/socket";
private static final String USER_FILE_NAME = "user.json";
private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json";
private static ChatContact currentUser;
Server
import com.codename1.ui.Display;
import com.codename1.ui.EncodedImage;
import com.codename1.util.EasyThread;
import com.codename1.util.OnComplete;
import com.codename1.util.regex.StringReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Server {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String WEBSOCKER_URL = "ws://localhost:8080/socket";
private static final String USER_FILE_NAME = "user.json";
private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json";
private static ChatContact currentUser;
Server
import java.util.List;
import java.util.Map;
public class Server {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String WEBSOCKER_URL = "ws://localhost:8080/socket";
private static final String USER_FILE_NAME = "user.json";
private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json";
private static ChatContact currentUser;
private static WebSocket connection;
private static boolean connected;
private static List<ChatMessage> messageQueue;
public static ChatContact user() {
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
Server
import java.util.List;
import java.util.Map;
public class Server {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String WEBSOCKER_URL = "ws://localhost:8080/socket";
private static final String USER_FILE_NAME = "user.json";
private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json";
private static ChatContact currentUser;
private static WebSocket connection;
private static boolean connected;
private static List<ChatMessage> messageQueue;
public static ChatContact user() {
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
Server
import java.util.List;
import java.util.Map;
public class Server {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String WEBSOCKER_URL = "ws://localhost:8080/socket";
private static final String USER_FILE_NAME = "user.json";
private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json";
private static ChatContact currentUser;
private static WebSocket connection;
private static boolean connected;
private static List<ChatMessage> messageQueue;
public static ChatContact user() {
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
Server
import java.util.List;
import java.util.Map;
public class Server {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String WEBSOCKER_URL = "ws://localhost:8080/socket";
private static final String USER_FILE_NAME = "user.json";
private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json";
private static ChatContact currentUser;
private static WebSocket connection;
private static boolean connected;
private static List<ChatMessage> messageQueue;
public static ChatContact user() {
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
Server
import java.util.List;
import java.util.Map;
public class Server {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String WEBSOCKER_URL = "ws://localhost:8080/socket";
private static final String USER_FILE_NAME = "user.json";
private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json";
private static ChatContact currentUser;
private static WebSocket connection;
private static boolean connected;
private static List<ChatMessage> messageQueue;
public static ChatContact user() {
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
Server
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME);
if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) {
messageQueue = new ChatMessage().getPropertyIndex().
loadJSONList(MESSAGE_QUEUE_FILE_NAME);
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().getPropertyIndex().
loadJSONList("contacts.json");
} else {
contactCache = new ArrayList<>();
}
} else {
contactCache = new ArrayList<>();
}
}
Server
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME);
if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) {
messageQueue = new ChatMessage().getPropertyIndex().
loadJSONList(MESSAGE_QUEUE_FILE_NAME);
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().getPropertyIndex().
loadJSONList("contacts.json");
} else {
contactCache = new ArrayList<>();
}
} else {
contactCache = new ArrayList<>();
}
}
Server
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME);
if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) {
messageQueue = new ChatMessage().getPropertyIndex().
loadJSONList(MESSAGE_QUEUE_FILE_NAME);
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().getPropertyIndex().
loadJSONList("contacts.json");
} else {
contactCache = new ArrayList<>();
}
} else {
contactCache = new ArrayList<>();
}
}
Server
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME);
if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) {
messageQueue = new ChatMessage().getPropertyIndex().
loadJSONList(MESSAGE_QUEUE_FILE_NAME);
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().getPropertyIndex().
loadJSONList("contacts.json");
} else {
contactCache = new ArrayList<>();
}
} else {
contactCache = new ArrayList<>();
}
}
Server
return currentUser;
}
public static void init() {
if(existsInStorage(USER_FILE_NAME)) {
currentUser = new ChatContact();
currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME);
if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) {
messageQueue = new ChatMessage().getPropertyIndex().
loadJSONList(MESSAGE_QUEUE_FILE_NAME);
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().getPropertyIndex().
loadJSONList("contacts.json");
} else {
contactCache = new ArrayList<>();
}
} else {
contactCache = new ArrayList<>();
}
}
Server
} else {
contactCache = new ArrayList<>();
}
}
public static void flushMessageQueue() {
if(connected && messageQueue != null && messageQueue.size() > 0) {
for(ChatMessage m : messageQueue) {
connection.send(m.getPropertyIndex().toJSON());
}
messageQueue.clear();
}
}
private static RequestBuilder post(String u) {
RequestBuilder r = Rest.post(SERVER_URL + u).jsonContent();
if(currentUser != null && currentUser.token.get() != null) {
r.header("auth", currentUser.token.get());
}
return r;
}
private static RequestBuilder get(String u) {
Server
}
messageQueue.clear();
}
}
private static RequestBuilder post(String u) {
RequestBuilder r = Rest.post(SERVER_URL + u).jsonContent();
if(currentUser != null && currentUser.token.get() != null) {
r.header("auth", currentUser.token.get());
}
return r;
}
private static RequestBuilder get(String u) {
RequestBuilder r = Rest.get(SERVER_URL + u).jsonContent();
if(currentUser != null && currentUser.token.get() != null) {
r.header("auth", currentUser.token.get());
}
return r;
}
public static void login(OnComplete<ChatContact> c) {
post("user/login").
Server
r.header("auth", currentUser.token.get());
}
return r;
}
public static void login(OnComplete<ChatContact> c) {
post("user/login").
body(currentUser).fetchAsProperties(
res -> {
currentUser = (ChatContact)res.getResponseData();
currentUser.
getPropertyIndex().
storeJSON(USER_FILE_NAME);
c.completed(currentUser);
},
ChatContact.class);
}
public static void signup(ChatContact user,
OnComplete<ChatContact> c) {
post("user/signup").
body(user).fetchAsProperties(
res -> {
Server
r.header("auth", currentUser.token.get());
}
return r;
}
public static void login(OnComplete<ChatContact> c) {
post("user/login").
body(currentUser).fetchAsProperties(
res -> {
currentUser = (ChatContact)res.getResponseData();
currentUser.
getPropertyIndex().
storeJSON(USER_FILE_NAME);
c.completed(currentUser);
},
ChatContact.class);
}
public static void signup(ChatContact user,
OnComplete<ChatContact> c) {
post("user/signup").
body(user).fetchAsProperties(
res -> {
Server
r.header("auth", currentUser.token.get());
}
return r;
}
public static void login(OnComplete<ChatContact> c) {
post("user/login").
body(currentUser).fetchAsProperties(
res -> {
currentUser = (ChatContact)res.getResponseData();
currentUser.
getPropertyIndex().
storeJSON(USER_FILE_NAME);
c.completed(currentUser);
},
ChatContact.class);
}
public static void signup(ChatContact user,
OnComplete<ChatContact> c) {
post("user/signup").
body(user).fetchAsProperties(
res -> {
Server
c.completed(currentUser);
},
ChatContact.class);
}
public static void signup(ChatContact user,
OnComplete<ChatContact> c) {
post("user/signup").
body(user).fetchAsProperties(
res -> {
currentUser = (ChatContact)res.getResponseData();
currentUser.
getPropertyIndex().
storeJSON(USER_FILE_NAME);
c.completed(currentUser);
},
ChatContact.class);
}
public static void update(OnComplete<ChatContact> c) {
post("user/update").
body(currentUser).fetchAsProperties(
Server
c.completed(currentUser);
},
ChatContact.class);
}
public static boolean verify(String code) {
Response<String> result = get("user/verify").
queryParam("userId", currentUser.id.get()).
queryParam("code", code).
getAsString();
return "OK".equals(result.getResponseData());
}
public static void sendMessage(ChatMessage m, ChatContact cont) {
cont.lastActivityTime.set(new Date());
cont.chats.add(m);
saveContacts();
if(connected) {
post("user/sendMessage").
body(m).
fetchAsProperties(e -> {
cont.chats.remove(m);
Server
c.completed(currentUser);
},
ChatContact.class);
}
public static void update(OnComplete<ChatContact> c) {
post("user/update").
body(currentUser).fetchAsProperties(
res -> {
currentUser = (ChatContact)res.getResponseData();
currentUser.
getPropertyIndex().
storeJSON(USER_FILE_NAME);
c.completed(currentUser);
},
ChatContact.class);
}
public static boolean verify(String code) {
Response<String> result = get("user/verify").
queryParam("userId", currentUser.id.get()).
queryParam("code", code).
Server
return "OK".equals(result.getResponseData());
}
public static void sendMessage(ChatMessage m, ChatContact cont) {
cont.lastActivityTime.set(new Date());
cont.chats.add(m);
saveContacts();
if(connected) {
post("user/sendMessage").
body(m).
fetchAsProperties(e -> {
cont.chats.remove(m);
cont.chats.add((ChatMessage)e.getResponseData());
saveContacts();
}, ChatMessage.class);
//connection.send(m.getPropertyIndex().toJSON());
} else {
if(messageQueue == null) {
messageQueue = new ArrayList<>();
}
messageQueue.add(m);
PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME,
Server
return "OK".equals(result.getResponseData());
}
public static void sendMessage(ChatMessage m, ChatContact cont) {
cont.lastActivityTime.set(new Date());
cont.chats.add(m);
saveContacts();
if(connected) {
post("user/sendMessage").
body(m).
fetchAsProperties(e -> {
cont.chats.remove(m);
cont.chats.add((ChatMessage)e.getResponseData());
saveContacts();
}, ChatMessage.class);
//connection.send(m.getPropertyIndex().toJSON());
} else {
if(messageQueue == null) {
messageQueue = new ArrayList<>();
}
messageQueue.add(m);
PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME,
Server
return "OK".equals(result.getResponseData());
}
public static void sendMessage(ChatMessage m, ChatContact cont) {
cont.lastActivityTime.set(new Date());
cont.chats.add(m);
saveContacts();
if(connected) {
post("user/sendMessage").
body(m).
fetchAsProperties(e -> {
cont.chats.remove(m);
cont.chats.add((ChatMessage)e.getResponseData());
saveContacts();
}, ChatMessage.class);
//connection.send(m.getPropertyIndex().toJSON());
} else {
if(messageQueue == null) {
messageQueue = new ArrayList<>();
}
messageQueue.add(m);
PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME,
Server
return "OK".equals(result.getResponseData());
}
public static void sendMessage(ChatMessage m, ChatContact cont) {
cont.lastActivityTime.set(new Date());
cont.chats.add(m);
saveContacts();
if(connected) {
post("user/sendMessage").
body(m).
fetchAsProperties(e -> {
cont.chats.remove(m);
cont.chats.add((ChatMessage)e.getResponseData());
saveContacts();
}, ChatMessage.class);
//connection.send(m.getPropertyIndex().toJSON());
} else {
if(messageQueue == null) {
messageQueue = new ArrayList<>();
}
messageQueue.add(m);
PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME,
Server
public static void sendMessage(ChatMessage m, ChatContact cont) {
cont.lastActivityTime.set(new Date());
cont.chats.add(m);
saveContacts();
if(connected) {
post("user/sendMessage").
body(m).
fetchAsProperties(e -> {
cont.chats.remove(m);
cont.chats.add((ChatMessage)e.getResponseData());
saveContacts();
}, ChatMessage.class);
//connection.send(m.getPropertyIndex().toJSON());
} else {
if(messageQueue == null) {
messageQueue = new ArrayList<>();
}
messageQueue.add(m);
PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME,
messageQueue);
}
}
Server
messageQueue);
}
}
public static void bindMessageListener(final ServerMessages
callback) {
connection = new WebSocket(WEBSOCKER_URL) {
@Override
protected void onOpen() {
connected = true;
long lastMessageTime =
Preferences.get("LastReceivedMessage", (long)0);
send("{"t":"init","tok":"" +
currentUser.token.get() +
"","time":" +
lastMessageTime + "}");
callSerially(() -> callback.connected());
final WebSocket w = this;
new Thread() {
public void run() {
Util.sleep(80000);
while(connection == w) {
// keep-alive message every 80 seconds to avoid
Server
messageQueue);
}
}
public static void bindMessageListener(final ServerMessages
callback) {
connection = new WebSocket(WEBSOCKER_URL) {
@Override
protected void onOpen() {
connected = true;
long lastMessageTime =
Preferences.get("LastReceivedMessage", (long)0);
send("{"t":"init","tok":"" +
currentUser.token.get() +
"","time":" +
lastMessageTime + "}");
callSerially(() -> callback.connected());
final WebSocket w = this;
new Thread() {
public void run() {
Util.sleep(80000);
while(connection == w) {
// keep-alive message every 80 seconds to avoid
Server
callback.messageReceived(c);
});
} catch(IOException err) {
Log.e(err);
throw new RuntimeException(err.toString());
}
}
@Override
protected void onMessage(byte[] message) {
}
@Override
protected void onError(Exception ex) {
Log.e(ex);
}
};
connection.autoReconnect(5000);
connection.connect();
}
private static void updateMessage(ChatMessage m) {
Server
public static void bindMessageListener(final ServerMessages
callback) {
connection = new WebSocket(WEBSOCKER_URL) {
@Override
protected void onOpen() {
connected = true;
long lastMessageTime =
Preferences.get("LastReceivedMessage", (long)0);
send("{"t":"init","tok":"" +
currentUser.token.get() +
"","time":" +
lastMessageTime + "}");
callSerially(() -> callback.connected());
final WebSocket w = this;
new Thread() {
public void run() {
Util.sleep(80000);
while(connection == w) {
// keep-alive message every 80 seconds to avoid
// cloudflare killing of the connection
// https://community.cloudflare.com/t/cloudflare-
websocket-timeout/5865/3
send("{"t":"ping"}");
Server
public static void bindMessageListener(final ServerMessages
callback) {
connection = new WebSocket(WEBSOCKER_URL) {
@Override
protected void onOpen() {
connected = true;
long lastMessageTime =
Preferences.get("LastReceivedMessage", (long)0);
send("{"t":"init","tok":"" +
currentUser.token.get() +
"","time":" +
lastMessageTime + "}");
callSerially(() -> callback.connected());
final WebSocket w = this;
new Thread() {
public void run() {
Util.sleep(80000);
while(connection == w) {
// keep-alive message every 80 seconds to avoid
// cloudflare killing of the connection
// https://community.cloudflare.com/t/cloudflare-
websocket-timeout/5865/3
send("{"t":"ping"}");
Server
callback) {
connection = new WebSocket(WEBSOCKER_URL) {
@Override
protected void onOpen() {
connected = true;
long lastMessageTime =
Preferences.get("LastReceivedMessage", (long)0);
send("{"t":"init","tok":"" +
currentUser.token.get() +
"","time":" +
lastMessageTime + "}");
callSerially(() -> callback.connected());
final WebSocket w = this;
new Thread() {
public void run() {
Util.sleep(80000);
while(connection == w) {
// keep-alive message every 80 seconds to avoid
// cloudflare killing of the connection
// https://community.cloudflare.com/t/cloudflare-
websocket-timeout/5865/3
send("{"t":"ping"}");
Server
Preferences.get("LastReceivedMessage", (long)0);
send("{"t":"init","tok":"" +
currentUser.token.get() +
"","time":" +
lastMessageTime + "}");
callSerially(() -> callback.connected());
final WebSocket w = this;
new Thread() {
public void run() {
Util.sleep(80000);
while(connection == w) {
// keep-alive message every 80 seconds to avoid
// cloudflare killing of the connection
// https://community.cloudflare.com/t/cloudflare-
websocket-timeout/5865/3
send("{"t":"ping"}");
Util.sleep(80000);
}
}
}.start();
}
Server
}
}.start();
}
@Override
protected void onClose(int statusCode, String reason) {
connected = false;
callSerially(() -> callback.disconnected());
}
@Override
protected void onMessage(String message) {
try {
StringReader r = new StringReader(message);
JSONParser jp = new JSONParser();
JSONParser.setUseBoolean(true);
JSONParser.setUseLongs(true);
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
Server
connected = false;
callSerially(() -> callback.disconnected());
}
@Override
protected void onMessage(String message) {
try {
StringReader r = new StringReader(message);
JSONParser jp = new JSONParser();
JSONParser.setUseBoolean(true);
JSONParser.setUseLongs(true);
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
Server
connected = false;
callSerially(() -> callback.disconnected());
}
@Override
protected void onMessage(String message) {
try {
StringReader r = new StringReader(message);
JSONParser jp = new JSONParser();
JSONParser.setUseBoolean(true);
JSONParser.setUseLongs(true);
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
Server
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
c.viewedBy.asList());
return;
}
Preferences.set("LastReceivedMessage",
c.time.get().getTime());
updateMessage(c);
ackMessage(c.id.get());
callback.messageReceived(c);
});
Server
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
c.viewedBy.asList());
return;
}
Preferences.set("LastReceivedMessage",
c.time.get().getTime());
updateMessage(c);
ackMessage(c.id.get());
callback.messageReceived(c);
});
Server
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
c.viewedBy.asList());
return;
}
Preferences.set("LastReceivedMessage",
c.time.get().getTime());
updateMessage(c);
ackMessage(c.id.get());
callback.messageReceived(c);
});
Server
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
c.viewedBy.asList());
return;
}
Preferences.set("LastReceivedMessage",
c.time.get().getTime());
updateMessage(c);
ackMessage(c.id.get());
callback.messageReceived(c);
});
Server
Map m = jp.parseJSON(r);
ChatMessage c = new ChatMessage();
c.getPropertyIndex().
populateFromMap(m, ChatMessage.class);
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
c.viewedBy.asList());
return;
}
Preferences.set("LastReceivedMessage",
c.time.get().getTime());
updateMessage(c);
ackMessage(c.id.get());
callback.messageReceived(c);
});
Server
callSerially(() -> {
if(c.typing.get() != null &&
c.typing.getBoolean()) {
callback.userTyping(c.authorId.get());
return;
}
if(c.viewedBy.size() > 0) {
callback.messageViewed(message,
c.viewedBy.asList());
return;
}
Preferences.set("LastReceivedMessage",
c.time.get().getTime());
updateMessage(c);
ackMessage(c.id.get());
callback.messageReceived(c);
});
} catch(IOException err) {
Log.e(err);
throw new RuntimeException(err.toString());
}
Server
ackMessage(c.id.get());
callback.messageReceived(c);
});
} catch(IOException err) {
Log.e(err);
throw new RuntimeException(err.toString());
}
}
@Override
protected void onMessage(byte[] message) {
}
@Override
protected void onError(Exception ex) {
Log.e(ex);
}
};
connection.autoReconnect(5000);
connection.connect();
}
Server
};
connection.autoReconnect(5000);
connection.connect();
}
private static void updateMessage(ChatMessage m) {
for(ChatContact c : contactCache) {
if(c.id.get() != null &&
c.id.get().equals(m.authorId.get())) {
c.lastActivityTime.set(new Date());
c.chats.add(m);
saveContacts();
return;
}
}
findRegisteredUserById(m.authorId.get(), cc -> {
contactCache.add(cc);
cc.chats.add(m);
saveContacts();
});
}
Server
};
connection.autoReconnect(5000);
connection.connect();
}
private static void updateMessage(ChatMessage m) {
for(ChatContact c : contactCache) {
if(c.id.get() != null &&
c.id.get().equals(m.authorId.get())) {
c.lastActivityTime.set(new Date());
c.chats.add(m);
saveContacts();
return;
}
}
findRegisteredUserById(m.authorId.get(), cc -> {
contactCache.add(cc);
cc.chats.add(m);
saveContacts();
});
}
Server
};
connection.autoReconnect(5000);
connection.connect();
}
private static void updateMessage(ChatMessage m) {
for(ChatContact c : contactCache) {
if(c.id.get() != null &&
c.id.get().equals(m.authorId.get())) {
c.lastActivityTime.set(new Date());
c.chats.add(m);
saveContacts();
return;
}
}
findRegisteredUserById(m.authorId.get(), cc -> {
contactCache.add(cc);
cc.chats.add(m);
saveContacts();
});
}
Server
cc.chats.add(m);
saveContacts();
});
}
public static void closeWebsocketConnection() {
if(connection != null) {
connection.close();
connection = null;
}
}
public static void saveContacts() {
if(contactCache != null && contactsThread != null) {
contactsThread.run(() -> {
PropertyIndex.storeJSONList("contacts.json",
contactCache);
});
}
}
private static List<ChatContact> contactCache;
Server
}
public static void closeWebsocketConnection() {
if(connection != null) {
connection.close();
connection = null;
}
}
public static void saveContacts() {
if(contactCache != null && contactsThread != null) {
contactsThread.run(() -> {
PropertyIndex.storeJSONList("contacts.json",
contactCache);
});
}
}
private static List<ChatContact> contactCache;
private static EasyThread contactsThread;
public static void fetchContacts(
OnComplete<List<ChatContact>> contactsCallback) {
Server
});
}
}
private static List<ChatContact> contactCache;
private static EasyThread contactsThread;
public static void fetchContacts(
OnComplete<List<ChatContact>> contactsCallback) {
if(contactsThread == null) {
contactsThread = EasyThread.start("Contacts Thread");
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(!contactsThread.isThisIt()) {
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(contactCache != null) {
callSeriallyOnIdle(() ->
contactsCallback.completed(contactCache));
return;
}
if(existsInStorage("contacts.json")) {
Server
});
}
}
private static List<ChatContact> contactCache;
private static EasyThread contactsThread;
public static void fetchContacts(
OnComplete<List<ChatContact>> contactsCallback) {
if(contactsThread == null) {
contactsThread = EasyThread.start("Contacts Thread");
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(!contactsThread.isThisIt()) {
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(contactCache != null) {
callSeriallyOnIdle(() ->
contactsCallback.completed(contactCache));
return;
}
if(existsInStorage("contacts.json")) {
Server
});
}
}
private static List<ChatContact> contactCache;
private static EasyThread contactsThread;
public static void fetchContacts(
OnComplete<List<ChatContact>> contactsCallback) {
if(contactsThread == null) {
contactsThread = EasyThread.start("Contacts Thread");
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(!contactsThread.isThisIt()) {
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(contactCache != null) {
callSeriallyOnIdle(() ->
contactsCallback.completed(contactCache));
return;
}
if(existsInStorage("contacts.json")) {
Server
});
}
}
private static List<ChatContact> contactCache;
private static EasyThread contactsThread;
public static void fetchContacts(
OnComplete<List<ChatContact>> contactsCallback) {
if(contactsThread == null) {
contactsThread = EasyThread.start("Contacts Thread");
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(!contactsThread.isThisIt()) {
contactsThread.run(() -> fetchContacts(contactsCallback));
return;
}
if(contactCache != null) {
callSeriallyOnIdle(() ->
contactsCallback.completed(contactCache));
return;
}
if(existsInStorage("contacts.json")) {
Server
callSeriallyOnIdle(() ->
contactsCallback.completed(contactCache));
return;
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().
getPropertyIndex().
loadJSONList("contacts.json");
callSerially(() -> contactsCallback.completed(contactCache));
for(ChatContact c : contactCache) {
if(existsInStorage(c.name.get() + ".jpg")) {
String f = c.name.get() + ".jpg";
try (InputStream is = createStorageInputStream(f)) {
c.photo.set(EncodedImage.create(is));
} catch(IOException err) {
Log.e(err);
}
}
}
return;
}
Server
callSeriallyOnIdle(() ->
contactsCallback.completed(contactCache));
return;
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().
getPropertyIndex().
loadJSONList("contacts.json");
callSerially(() -> contactsCallback.completed(contactCache));
for(ChatContact c : contactCache) {
if(existsInStorage(c.name.get() + ".jpg")) {
String f = c.name.get() + ".jpg";
try (InputStream is = createStorageInputStream(f)) {
c.photo.set(EncodedImage.create(is));
} catch(IOException err) {
Log.e(err);
}
}
}
return;
}
Server
callSeriallyOnIdle(() ->
contactsCallback.completed(contactCache));
return;
}
if(existsInStorage("contacts.json")) {
contactCache = new ChatContact().
getPropertyIndex().
loadJSONList("contacts.json");
callSerially(() -> contactsCallback.completed(contactCache));
for(ChatContact c : contactCache) {
if(existsInStorage(c.name.get() + ".jpg")) {
String f = c.name.get() + ".jpg";
try (InputStream is = createStorageInputStream(f)) {
c.photo.set(EncodedImage.create(is));
} catch(IOException err) {
Log.e(err);
}
}
}
return;
}
Server
}
}
return;
}
Contact[] contacts = Display.getInstance().getAllContacts(true,
true, false, true, false, false);
ArrayList<ChatContact> l = new ArrayList<>();
for(Contact c : contacts) {
ChatContact cc = new ChatContact().
phone.set(c.getPrimaryPhoneNumber()).
name.set(c.getDisplayName());
l.add(cc);
callSeriallyOnIdle(() -> {
cc.photo.set(c.getPhoto());
if(cc.photo.get() != null) {
contactsThread.run(() -> {
String f = cc.name.get() + ".jpg";
try(OutputStream os =
createStorageOutputStream(f)) {
EncodedImage img = EncodedImage.
createFromImage(cc.photo.get(), true);
Server
return;
}
Contact[] contacts = Display.getInstance().getAllContacts(true,
true, false, true, false, false);
ArrayList<ChatContact> l = new ArrayList<>();
for(Contact c : contacts) {
ChatContact cc = new ChatContact().
phone.set(c.getPrimaryPhoneNumber()).
name.set(c.getDisplayName());
l.add(cc);
callSeriallyOnIdle(() -> {
cc.photo.set(c.getPhoto());
if(cc.photo.get() != null) {
contactsThread.run(() -> {
String f = cc.name.get() + ".jpg";
try(OutputStream os =
createStorageOutputStream(f)) {
EncodedImage img = EncodedImage.
createFromImage(cc.photo.get(), true);
os.write(img.getImageData());
} catch(IOException err) {
Log.e(err);
Server
ArrayList<ChatContact> l = new ArrayList<>();
for(Contact c : contacts) {
ChatContact cc = new ChatContact().
phone.set(c.getPrimaryPhoneNumber()).
name.set(c.getDisplayName());
l.add(cc);
callSeriallyOnIdle(() -> {
cc.photo.set(c.getPhoto());
if(cc.photo.get() != null) {
contactsThread.run(() -> {
String f = cc.name.get() + ".jpg";
try(OutputStream os =
createStorageOutputStream(f)) {
EncodedImage img = EncodedImage.
createFromImage(cc.photo.get(), true);
os.write(img.getImageData());
} catch(IOException err) {
Log.e(err);
}
});
}
});
Server
ArrayList<ChatContact> l = new ArrayList<>();
for(Contact c : contacts) {
ChatContact cc = new ChatContact().
phone.set(c.getPrimaryPhoneNumber()).
name.set(c.getDisplayName());
l.add(cc);
callSeriallyOnIdle(() -> {
cc.photo.set(c.getPhoto());
if(cc.photo.get() != null) {
contactsThread.run(() -> {
String f = cc.name.get() + ".jpg";
try(OutputStream os =
createStorageOutputStream(f)) {
EncodedImage img = EncodedImage.
createFromImage(cc.photo.get(), true);
os.write(img.getImageData());
} catch(IOException err) {
Log.e(err);
}
});
}
});
Server
cc.photo.set(c.getPhoto());
if(cc.photo.get() != null) {
contactsThread.run(() -> {
String f = cc.name.get() + ".jpg";
try(OutputStream os =
createStorageOutputStream(f)) {
EncodedImage img = EncodedImage.
createFromImage(cc.photo.get(), true);
os.write(img.getImageData());
} catch(IOException err) {
Log.e(err);
}
});
}
});
}
PropertyIndex.storeJSONList("contacts.json", l);
callSerially(() -> contactsCallback.completed(l));
}
public static void findRegisteredUser(String phone,
Server
});
}
PropertyIndex.storeJSONList("contacts.json", l);
callSerially(() -> contactsCallback.completed(l));
}
public static void findRegisteredUser(String phone,
OnComplete<ChatContact> resultCallback) {
get("user/findRegisteredUser").
queryParam("phone", phone).
fetchAsPropertyList(res -> {
List l = res.getResponseData();
if(l.size() == 0) {
resultCallback.completed(null);
return;
}
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void findRegisteredUserById(String id,
OnComplete<ChatContact> resultCallback) {
Server
resultCallback.completed(null);
return;
}
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void findRegisteredUserById(String id,
OnComplete<ChatContact> resultCallback) {
get("user/findRegisteredUserById").
queryParam("id", id).
fetchAsPropertyList(res -> {
List l = res.getResponseData();
if(l.size() == 0) {
resultCallback.completed(null);
return;
}
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void fetchChatList(
Server
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void fetchChatList(
OnComplete<List<ChatContact>> contactsCallback) {
fetchContacts(cl -> {
List<ChatContact> response = new ArrayList<>();
for(ChatContact c : cl) {
if(c.lastActivityTime.get() != null) {
response.add(c);
}
}
Collections.sort(response,
(ChatContact o1, ChatContact o2) ->
(int)(o1.lastActivityTime.get().getTime() -
o2.lastActivityTime.get().getTime()));
contactsCallback.completed(response);
});
}
public static void ackMessage(String messageId) {
Server
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void fetchChatList(
OnComplete<List<ChatContact>> contactsCallback) {
fetchContacts(cl -> {
List<ChatContact> response = new ArrayList<>();
for(ChatContact c : cl) {
if(c.lastActivityTime.get() != null) {
response.add(c);
}
}
Collections.sort(response,
(ChatContact o1, ChatContact o2) ->
(int)(o1.lastActivityTime.get().getTime() -
o2.lastActivityTime.get().getTime()));
contactsCallback.completed(response);
});
}
public static void ackMessage(String messageId) {
Server
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void fetchChatList(
OnComplete<List<ChatContact>> contactsCallback) {
fetchContacts(cl -> {
List<ChatContact> response = new ArrayList<>();
for(ChatContact c : cl) {
if(c.lastActivityTime.get() != null) {
response.add(c);
}
}
Collections.sort(response,
(ChatContact o1, ChatContact o2) ->
(int)(o1.lastActivityTime.get().getTime() -
o2.lastActivityTime.get().getTime()));
contactsCallback.completed(response);
});
}
public static void ackMessage(String messageId) {
Server
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void fetchChatList(
OnComplete<List<ChatContact>> contactsCallback) {
fetchContacts(cl -> {
List<ChatContact> response = new ArrayList<>();
for(ChatContact c : cl) {
if(c.lastActivityTime.get() != null) {
response.add(c);
}
}
Collections.sort(response,
(ChatContact o1, ChatContact o2) ->
(int)(o1.lastActivityTime.get().getTime() -
o2.lastActivityTime.get().getTime()));
contactsCallback.completed(response);
});
}
public static void ackMessage(String messageId) {
Server
resultCallback.completed((ChatContact)l.get(0));
}, ChatContact.class);
}
public static void fetchChatList(
OnComplete<List<ChatContact>> contactsCallback) {
fetchContacts(cl -> {
List<ChatContact> response = new ArrayList<>();
for(ChatContact c : cl) {
if(c.lastActivityTime.get() != null) {
response.add(c);
}
}
Collections.sort(response,
(ChatContact o1, ChatContact o2) ->
(int)(o1.lastActivityTime.get().getTime() -
o2.lastActivityTime.get().getTime()));
contactsCallback.completed(response);
});
}
public static void ackMessage(String messageId) {
Server
}
Collections.sort(response,
(ChatContact o1, ChatContact o2) ->
(int)(o1.lastActivityTime.get().getTime() -
o2.lastActivityTime.get().getTime()));
contactsCallback.completed(response);
});
}
public static void ackMessage(String messageId) {
post("user/ackMessage").
body(messageId).fetchAsString(c -> {});
}
public static void updatePushKey(String key) {
if(user() != null) {
get("user/updatePushKey").
queryParam("id", user().id.get()).
queryParam("key", key).fetchAsString(c -> {});
}
}
}
Server
}
Collections.sort(response,
(ChatContact o1, ChatContact o2) ->
(int)(o1.lastActivityTime.get().getTime() -
o2.lastActivityTime.get().getTime()));
contactsCallback.completed(response);
});
}
public static void ackMessage(String messageId) {
post("user/ackMessage").
body(messageId).fetchAsString(c -> {});
}
public static void updatePushKey(String key) {
if(user() != null) {
get("user/updatePushKey").
queryParam("id", user().id.get()).
queryParam("key", key).fetchAsString(c -> {});
}
}
}
Server

Creating a Whatsapp Clone - Part II.pdf

  • 1.
    Creating a WhatsAppClone - Part II
  • 2.
    * need additionalinformation or have any questions. */ package com.codename1.whatsapp.model; import com.codename1.contacts.Contact; import com.codename1.io.JSONParser; import com.codename1.io.Log; import com.codename1.io.Preferences; import com.codename1.io.Util; import com.codename1.io.rest.RequestBuilder; import com.codename1.io.rest.Response; import com.codename1.io.rest.Rest; import com.codename1.io.websocket.WebSocket; import com.codename1.properties.PropertyIndex; import static com.codename1.ui.CN.*; import com.codename1.ui.Display; import com.codename1.ui.EncodedImage; import com.codename1.util.EasyThread; import com.codename1.util.OnComplete; import com.codename1.util.regex.StringReader; import java.io.IOException; import java.io.InputStream; Server
  • 3.
    import com.codename1.ui.Display; import com.codename1.ui.EncodedImage; importcom.codename1.util.EasyThread; import com.codename1.util.OnComplete; import com.codename1.util.regex.StringReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; public class Server { private static final String SERVER_URL = "http://localhost:8080/"; private static final String WEBSOCKER_URL = "ws://localhost:8080/socket"; private static final String USER_FILE_NAME = "user.json"; private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json"; private static ChatContact currentUser; Server
  • 4.
    import com.codename1.ui.Display; import com.codename1.ui.EncodedImage; importcom.codename1.util.EasyThread; import com.codename1.util.OnComplete; import com.codename1.util.regex.StringReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; public class Server { private static final String SERVER_URL = "http://localhost:8080/"; private static final String WEBSOCKER_URL = "ws://localhost:8080/socket"; private static final String USER_FILE_NAME = "user.json"; private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json"; private static ChatContact currentUser; Server
  • 5.
    import java.util.List; import java.util.Map; publicclass Server { private static final String SERVER_URL = "http://localhost:8080/"; private static final String WEBSOCKER_URL = "ws://localhost:8080/socket"; private static final String USER_FILE_NAME = "user.json"; private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json"; private static ChatContact currentUser; private static WebSocket connection; private static boolean connected; private static List<ChatMessage> messageQueue; public static ChatContact user() { return currentUser; } public static void init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); Server
  • 6.
    import java.util.List; import java.util.Map; publicclass Server { private static final String SERVER_URL = "http://localhost:8080/"; private static final String WEBSOCKER_URL = "ws://localhost:8080/socket"; private static final String USER_FILE_NAME = "user.json"; private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json"; private static ChatContact currentUser; private static WebSocket connection; private static boolean connected; private static List<ChatMessage> messageQueue; public static ChatContact user() { return currentUser; } public static void init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); Server
  • 7.
    import java.util.List; import java.util.Map; publicclass Server { private static final String SERVER_URL = "http://localhost:8080/"; private static final String WEBSOCKER_URL = "ws://localhost:8080/socket"; private static final String USER_FILE_NAME = "user.json"; private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json"; private static ChatContact currentUser; private static WebSocket connection; private static boolean connected; private static List<ChatMessage> messageQueue; public static ChatContact user() { return currentUser; } public static void init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); Server
  • 8.
    import java.util.List; import java.util.Map; publicclass Server { private static final String SERVER_URL = "http://localhost:8080/"; private static final String WEBSOCKER_URL = "ws://localhost:8080/socket"; private static final String USER_FILE_NAME = "user.json"; private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json"; private static ChatContact currentUser; private static WebSocket connection; private static boolean connected; private static List<ChatMessage> messageQueue; public static ChatContact user() { return currentUser; } public static void init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); Server
  • 9.
    import java.util.List; import java.util.Map; publicclass Server { private static final String SERVER_URL = "http://localhost:8080/"; private static final String WEBSOCKER_URL = "ws://localhost:8080/socket"; private static final String USER_FILE_NAME = "user.json"; private static final String MESSAGE_QUEUE_FILE_NAME = "message_queue.json"; private static ChatContact currentUser; private static WebSocket connection; private static boolean connected; private static List<ChatMessage> messageQueue; public static ChatContact user() { return currentUser; } public static void init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); Server
  • 10.
    return currentUser; } public staticvoid init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME); if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) { messageQueue = new ChatMessage().getPropertyIndex(). loadJSONList(MESSAGE_QUEUE_FILE_NAME); } if(existsInStorage("contacts.json")) { contactCache = new ChatContact().getPropertyIndex(). loadJSONList("contacts.json"); } else { contactCache = new ArrayList<>(); } } else { contactCache = new ArrayList<>(); } } Server
  • 11.
    return currentUser; } public staticvoid init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME); if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) { messageQueue = new ChatMessage().getPropertyIndex(). loadJSONList(MESSAGE_QUEUE_FILE_NAME); } if(existsInStorage("contacts.json")) { contactCache = new ChatContact().getPropertyIndex(). loadJSONList("contacts.json"); } else { contactCache = new ArrayList<>(); } } else { contactCache = new ArrayList<>(); } } Server
  • 12.
    return currentUser; } public staticvoid init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME); if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) { messageQueue = new ChatMessage().getPropertyIndex(). loadJSONList(MESSAGE_QUEUE_FILE_NAME); } if(existsInStorage("contacts.json")) { contactCache = new ChatContact().getPropertyIndex(). loadJSONList("contacts.json"); } else { contactCache = new ArrayList<>(); } } else { contactCache = new ArrayList<>(); } } Server
  • 13.
    return currentUser; } public staticvoid init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME); if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) { messageQueue = new ChatMessage().getPropertyIndex(). loadJSONList(MESSAGE_QUEUE_FILE_NAME); } if(existsInStorage("contacts.json")) { contactCache = new ChatContact().getPropertyIndex(). loadJSONList("contacts.json"); } else { contactCache = new ArrayList<>(); } } else { contactCache = new ArrayList<>(); } } Server
  • 14.
    return currentUser; } public staticvoid init() { if(existsInStorage(USER_FILE_NAME)) { currentUser = new ChatContact(); currentUser.getPropertyIndex().loadJSON(USER_FILE_NAME); if(existsInStorage(MESSAGE_QUEUE_FILE_NAME)) { messageQueue = new ChatMessage().getPropertyIndex(). loadJSONList(MESSAGE_QUEUE_FILE_NAME); } if(existsInStorage("contacts.json")) { contactCache = new ChatContact().getPropertyIndex(). loadJSONList("contacts.json"); } else { contactCache = new ArrayList<>(); } } else { contactCache = new ArrayList<>(); } } Server
  • 15.
    } else { contactCache= new ArrayList<>(); } } public static void flushMessageQueue() { if(connected && messageQueue != null && messageQueue.size() > 0) { for(ChatMessage m : messageQueue) { connection.send(m.getPropertyIndex().toJSON()); } messageQueue.clear(); } } private static RequestBuilder post(String u) { RequestBuilder r = Rest.post(SERVER_URL + u).jsonContent(); if(currentUser != null && currentUser.token.get() != null) { r.header("auth", currentUser.token.get()); } return r; } private static RequestBuilder get(String u) { Server
  • 16.
    } messageQueue.clear(); } } private static RequestBuilderpost(String u) { RequestBuilder r = Rest.post(SERVER_URL + u).jsonContent(); if(currentUser != null && currentUser.token.get() != null) { r.header("auth", currentUser.token.get()); } return r; } private static RequestBuilder get(String u) { RequestBuilder r = Rest.get(SERVER_URL + u).jsonContent(); if(currentUser != null && currentUser.token.get() != null) { r.header("auth", currentUser.token.get()); } return r; } public static void login(OnComplete<ChatContact> c) { post("user/login"). Server
  • 17.
    r.header("auth", currentUser.token.get()); } return r; } publicstatic void login(OnComplete<ChatContact> c) { post("user/login"). body(currentUser).fetchAsProperties( res -> { currentUser = (ChatContact)res.getResponseData(); currentUser. getPropertyIndex(). storeJSON(USER_FILE_NAME); c.completed(currentUser); }, ChatContact.class); } public static void signup(ChatContact user, OnComplete<ChatContact> c) { post("user/signup"). body(user).fetchAsProperties( res -> { Server
  • 18.
    r.header("auth", currentUser.token.get()); } return r; } publicstatic void login(OnComplete<ChatContact> c) { post("user/login"). body(currentUser).fetchAsProperties( res -> { currentUser = (ChatContact)res.getResponseData(); currentUser. getPropertyIndex(). storeJSON(USER_FILE_NAME); c.completed(currentUser); }, ChatContact.class); } public static void signup(ChatContact user, OnComplete<ChatContact> c) { post("user/signup"). body(user).fetchAsProperties( res -> { Server
  • 19.
    r.header("auth", currentUser.token.get()); } return r; } publicstatic void login(OnComplete<ChatContact> c) { post("user/login"). body(currentUser).fetchAsProperties( res -> { currentUser = (ChatContact)res.getResponseData(); currentUser. getPropertyIndex(). storeJSON(USER_FILE_NAME); c.completed(currentUser); }, ChatContact.class); } public static void signup(ChatContact user, OnComplete<ChatContact> c) { post("user/signup"). body(user).fetchAsProperties( res -> { Server
  • 20.
    c.completed(currentUser); }, ChatContact.class); } public static voidsignup(ChatContact user, OnComplete<ChatContact> c) { post("user/signup"). body(user).fetchAsProperties( res -> { currentUser = (ChatContact)res.getResponseData(); currentUser. getPropertyIndex(). storeJSON(USER_FILE_NAME); c.completed(currentUser); }, ChatContact.class); } public static void update(OnComplete<ChatContact> c) { post("user/update"). body(currentUser).fetchAsProperties( Server
  • 21.
    c.completed(currentUser); }, ChatContact.class); } public static booleanverify(String code) { Response<String> result = get("user/verify"). queryParam("userId", currentUser.id.get()). queryParam("code", code). getAsString(); return "OK".equals(result.getResponseData()); } public static void sendMessage(ChatMessage m, ChatContact cont) { cont.lastActivityTime.set(new Date()); cont.chats.add(m); saveContacts(); if(connected) { post("user/sendMessage"). body(m). fetchAsProperties(e -> { cont.chats.remove(m); Server
  • 22.
    c.completed(currentUser); }, ChatContact.class); } public static voidupdate(OnComplete<ChatContact> c) { post("user/update"). body(currentUser).fetchAsProperties( res -> { currentUser = (ChatContact)res.getResponseData(); currentUser. getPropertyIndex(). storeJSON(USER_FILE_NAME); c.completed(currentUser); }, ChatContact.class); } public static boolean verify(String code) { Response<String> result = get("user/verify"). queryParam("userId", currentUser.id.get()). queryParam("code", code). Server
  • 23.
    return "OK".equals(result.getResponseData()); } public staticvoid sendMessage(ChatMessage m, ChatContact cont) { cont.lastActivityTime.set(new Date()); cont.chats.add(m); saveContacts(); if(connected) { post("user/sendMessage"). body(m). fetchAsProperties(e -> { cont.chats.remove(m); cont.chats.add((ChatMessage)e.getResponseData()); saveContacts(); }, ChatMessage.class); //connection.send(m.getPropertyIndex().toJSON()); } else { if(messageQueue == null) { messageQueue = new ArrayList<>(); } messageQueue.add(m); PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME, Server
  • 24.
    return "OK".equals(result.getResponseData()); } public staticvoid sendMessage(ChatMessage m, ChatContact cont) { cont.lastActivityTime.set(new Date()); cont.chats.add(m); saveContacts(); if(connected) { post("user/sendMessage"). body(m). fetchAsProperties(e -> { cont.chats.remove(m); cont.chats.add((ChatMessage)e.getResponseData()); saveContacts(); }, ChatMessage.class); //connection.send(m.getPropertyIndex().toJSON()); } else { if(messageQueue == null) { messageQueue = new ArrayList<>(); } messageQueue.add(m); PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME, Server
  • 25.
    return "OK".equals(result.getResponseData()); } public staticvoid sendMessage(ChatMessage m, ChatContact cont) { cont.lastActivityTime.set(new Date()); cont.chats.add(m); saveContacts(); if(connected) { post("user/sendMessage"). body(m). fetchAsProperties(e -> { cont.chats.remove(m); cont.chats.add((ChatMessage)e.getResponseData()); saveContacts(); }, ChatMessage.class); //connection.send(m.getPropertyIndex().toJSON()); } else { if(messageQueue == null) { messageQueue = new ArrayList<>(); } messageQueue.add(m); PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME, Server
  • 26.
    return "OK".equals(result.getResponseData()); } public staticvoid sendMessage(ChatMessage m, ChatContact cont) { cont.lastActivityTime.set(new Date()); cont.chats.add(m); saveContacts(); if(connected) { post("user/sendMessage"). body(m). fetchAsProperties(e -> { cont.chats.remove(m); cont.chats.add((ChatMessage)e.getResponseData()); saveContacts(); }, ChatMessage.class); //connection.send(m.getPropertyIndex().toJSON()); } else { if(messageQueue == null) { messageQueue = new ArrayList<>(); } messageQueue.add(m); PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME, Server
  • 27.
    public static voidsendMessage(ChatMessage m, ChatContact cont) { cont.lastActivityTime.set(new Date()); cont.chats.add(m); saveContacts(); if(connected) { post("user/sendMessage"). body(m). fetchAsProperties(e -> { cont.chats.remove(m); cont.chats.add((ChatMessage)e.getResponseData()); saveContacts(); }, ChatMessage.class); //connection.send(m.getPropertyIndex().toJSON()); } else { if(messageQueue == null) { messageQueue = new ArrayList<>(); } messageQueue.add(m); PropertyIndex.storeJSONList(MESSAGE_QUEUE_FILE_NAME, messageQueue); } } Server
  • 28.
    messageQueue); } } public static voidbindMessageListener(final ServerMessages callback) { connection = new WebSocket(WEBSOCKER_URL) { @Override protected void onOpen() { connected = true; long lastMessageTime = Preferences.get("LastReceivedMessage", (long)0); send("{"t":"init","tok":"" + currentUser.token.get() + "","time":" + lastMessageTime + "}"); callSerially(() -> callback.connected()); final WebSocket w = this; new Thread() { public void run() { Util.sleep(80000); while(connection == w) { // keep-alive message every 80 seconds to avoid Server
  • 29.
    messageQueue); } } public static voidbindMessageListener(final ServerMessages callback) { connection = new WebSocket(WEBSOCKER_URL) { @Override protected void onOpen() { connected = true; long lastMessageTime = Preferences.get("LastReceivedMessage", (long)0); send("{"t":"init","tok":"" + currentUser.token.get() + "","time":" + lastMessageTime + "}"); callSerially(() -> callback.connected()); final WebSocket w = this; new Thread() { public void run() { Util.sleep(80000); while(connection == w) { // keep-alive message every 80 seconds to avoid Server
  • 30.
    callback.messageReceived(c); }); } catch(IOException err){ Log.e(err); throw new RuntimeException(err.toString()); } } @Override protected void onMessage(byte[] message) { } @Override protected void onError(Exception ex) { Log.e(ex); } }; connection.autoReconnect(5000); connection.connect(); } private static void updateMessage(ChatMessage m) { Server
  • 31.
    public static voidbindMessageListener(final ServerMessages callback) { connection = new WebSocket(WEBSOCKER_URL) { @Override protected void onOpen() { connected = true; long lastMessageTime = Preferences.get("LastReceivedMessage", (long)0); send("{"t":"init","tok":"" + currentUser.token.get() + "","time":" + lastMessageTime + "}"); callSerially(() -> callback.connected()); final WebSocket w = this; new Thread() { public void run() { Util.sleep(80000); while(connection == w) { // keep-alive message every 80 seconds to avoid // cloudflare killing of the connection // https://community.cloudflare.com/t/cloudflare- websocket-timeout/5865/3 send("{"t":"ping"}"); Server
  • 32.
    public static voidbindMessageListener(final ServerMessages callback) { connection = new WebSocket(WEBSOCKER_URL) { @Override protected void onOpen() { connected = true; long lastMessageTime = Preferences.get("LastReceivedMessage", (long)0); send("{"t":"init","tok":"" + currentUser.token.get() + "","time":" + lastMessageTime + "}"); callSerially(() -> callback.connected()); final WebSocket w = this; new Thread() { public void run() { Util.sleep(80000); while(connection == w) { // keep-alive message every 80 seconds to avoid // cloudflare killing of the connection // https://community.cloudflare.com/t/cloudflare- websocket-timeout/5865/3 send("{"t":"ping"}"); Server
  • 33.
    callback) { connection =new WebSocket(WEBSOCKER_URL) { @Override protected void onOpen() { connected = true; long lastMessageTime = Preferences.get("LastReceivedMessage", (long)0); send("{"t":"init","tok":"" + currentUser.token.get() + "","time":" + lastMessageTime + "}"); callSerially(() -> callback.connected()); final WebSocket w = this; new Thread() { public void run() { Util.sleep(80000); while(connection == w) { // keep-alive message every 80 seconds to avoid // cloudflare killing of the connection // https://community.cloudflare.com/t/cloudflare- websocket-timeout/5865/3 send("{"t":"ping"}"); Server
  • 34.
    Preferences.get("LastReceivedMessage", (long)0); send("{"t":"init","tok":"" + currentUser.token.get()+ "","time":" + lastMessageTime + "}"); callSerially(() -> callback.connected()); final WebSocket w = this; new Thread() { public void run() { Util.sleep(80000); while(connection == w) { // keep-alive message every 80 seconds to avoid // cloudflare killing of the connection // https://community.cloudflare.com/t/cloudflare- websocket-timeout/5865/3 send("{"t":"ping"}"); Util.sleep(80000); } } }.start(); } Server
  • 35.
    } }.start(); } @Override protected void onClose(intstatusCode, String reason) { connected = false; callSerially(() -> callback.disconnected()); } @Override protected void onMessage(String message) { try { StringReader r = new StringReader(message); JSONParser jp = new JSONParser(); JSONParser.setUseBoolean(true); JSONParser.setUseLongs(true); Map m = jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && Server
  • 36.
    connected = false; callSerially(()-> callback.disconnected()); } @Override protected void onMessage(String message) { try { StringReader r = new StringReader(message); JSONParser jp = new JSONParser(); JSONParser.setUseBoolean(true); JSONParser.setUseLongs(true); Map m = jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, Server
  • 37.
    connected = false; callSerially(()-> callback.disconnected()); } @Override protected void onMessage(String message) { try { StringReader r = new StringReader(message); JSONParser jp = new JSONParser(); JSONParser.setUseBoolean(true); JSONParser.setUseLongs(true); Map m = jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, Server
  • 38.
    Map m =jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, c.viewedBy.asList()); return; } Preferences.set("LastReceivedMessage", c.time.get().getTime()); updateMessage(c); ackMessage(c.id.get()); callback.messageReceived(c); }); Server
  • 39.
    Map m =jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, c.viewedBy.asList()); return; } Preferences.set("LastReceivedMessage", c.time.get().getTime()); updateMessage(c); ackMessage(c.id.get()); callback.messageReceived(c); }); Server
  • 40.
    Map m =jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, c.viewedBy.asList()); return; } Preferences.set("LastReceivedMessage", c.time.get().getTime()); updateMessage(c); ackMessage(c.id.get()); callback.messageReceived(c); }); Server
  • 41.
    Map m =jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, c.viewedBy.asList()); return; } Preferences.set("LastReceivedMessage", c.time.get().getTime()); updateMessage(c); ackMessage(c.id.get()); callback.messageReceived(c); }); Server
  • 42.
    Map m =jp.parseJSON(r); ChatMessage c = new ChatMessage(); c.getPropertyIndex(). populateFromMap(m, ChatMessage.class); callSerially(() -> { if(c.typing.get() != null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, c.viewedBy.asList()); return; } Preferences.set("LastReceivedMessage", c.time.get().getTime()); updateMessage(c); ackMessage(c.id.get()); callback.messageReceived(c); }); Server
  • 43.
    callSerially(() -> { if(c.typing.get()!= null && c.typing.getBoolean()) { callback.userTyping(c.authorId.get()); return; } if(c.viewedBy.size() > 0) { callback.messageViewed(message, c.viewedBy.asList()); return; } Preferences.set("LastReceivedMessage", c.time.get().getTime()); updateMessage(c); ackMessage(c.id.get()); callback.messageReceived(c); }); } catch(IOException err) { Log.e(err); throw new RuntimeException(err.toString()); } Server
  • 44.
    ackMessage(c.id.get()); callback.messageReceived(c); }); } catch(IOException err){ Log.e(err); throw new RuntimeException(err.toString()); } } @Override protected void onMessage(byte[] message) { } @Override protected void onError(Exception ex) { Log.e(ex); } }; connection.autoReconnect(5000); connection.connect(); } Server
  • 45.
    }; connection.autoReconnect(5000); connection.connect(); } private static voidupdateMessage(ChatMessage m) { for(ChatContact c : contactCache) { if(c.id.get() != null && c.id.get().equals(m.authorId.get())) { c.lastActivityTime.set(new Date()); c.chats.add(m); saveContacts(); return; } } findRegisteredUserById(m.authorId.get(), cc -> { contactCache.add(cc); cc.chats.add(m); saveContacts(); }); } Server
  • 46.
    }; connection.autoReconnect(5000); connection.connect(); } private static voidupdateMessage(ChatMessage m) { for(ChatContact c : contactCache) { if(c.id.get() != null && c.id.get().equals(m.authorId.get())) { c.lastActivityTime.set(new Date()); c.chats.add(m); saveContacts(); return; } } findRegisteredUserById(m.authorId.get(), cc -> { contactCache.add(cc); cc.chats.add(m); saveContacts(); }); } Server
  • 47.
    }; connection.autoReconnect(5000); connection.connect(); } private static voidupdateMessage(ChatMessage m) { for(ChatContact c : contactCache) { if(c.id.get() != null && c.id.get().equals(m.authorId.get())) { c.lastActivityTime.set(new Date()); c.chats.add(m); saveContacts(); return; } } findRegisteredUserById(m.authorId.get(), cc -> { contactCache.add(cc); cc.chats.add(m); saveContacts(); }); } Server
  • 48.
    cc.chats.add(m); saveContacts(); }); } public static voidcloseWebsocketConnection() { if(connection != null) { connection.close(); connection = null; } } public static void saveContacts() { if(contactCache != null && contactsThread != null) { contactsThread.run(() -> { PropertyIndex.storeJSONList("contacts.json", contactCache); }); } } private static List<ChatContact> contactCache; Server
  • 49.
    } public static voidcloseWebsocketConnection() { if(connection != null) { connection.close(); connection = null; } } public static void saveContacts() { if(contactCache != null && contactsThread != null) { contactsThread.run(() -> { PropertyIndex.storeJSONList("contacts.json", contactCache); }); } } private static List<ChatContact> contactCache; private static EasyThread contactsThread; public static void fetchContacts( OnComplete<List<ChatContact>> contactsCallback) { Server
  • 50.
    }); } } private static List<ChatContact>contactCache; private static EasyThread contactsThread; public static void fetchContacts( OnComplete<List<ChatContact>> contactsCallback) { if(contactsThread == null) { contactsThread = EasyThread.start("Contacts Thread"); contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(!contactsThread.isThisIt()) { contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(contactCache != null) { callSeriallyOnIdle(() -> contactsCallback.completed(contactCache)); return; } if(existsInStorage("contacts.json")) { Server
  • 51.
    }); } } private static List<ChatContact>contactCache; private static EasyThread contactsThread; public static void fetchContacts( OnComplete<List<ChatContact>> contactsCallback) { if(contactsThread == null) { contactsThread = EasyThread.start("Contacts Thread"); contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(!contactsThread.isThisIt()) { contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(contactCache != null) { callSeriallyOnIdle(() -> contactsCallback.completed(contactCache)); return; } if(existsInStorage("contacts.json")) { Server
  • 52.
    }); } } private static List<ChatContact>contactCache; private static EasyThread contactsThread; public static void fetchContacts( OnComplete<List<ChatContact>> contactsCallback) { if(contactsThread == null) { contactsThread = EasyThread.start("Contacts Thread"); contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(!contactsThread.isThisIt()) { contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(contactCache != null) { callSeriallyOnIdle(() -> contactsCallback.completed(contactCache)); return; } if(existsInStorage("contacts.json")) { Server
  • 53.
    }); } } private static List<ChatContact>contactCache; private static EasyThread contactsThread; public static void fetchContacts( OnComplete<List<ChatContact>> contactsCallback) { if(contactsThread == null) { contactsThread = EasyThread.start("Contacts Thread"); contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(!contactsThread.isThisIt()) { contactsThread.run(() -> fetchContacts(contactsCallback)); return; } if(contactCache != null) { callSeriallyOnIdle(() -> contactsCallback.completed(contactCache)); return; } if(existsInStorage("contacts.json")) { Server
  • 54.
    callSeriallyOnIdle(() -> contactsCallback.completed(contactCache)); return; } if(existsInStorage("contacts.json")) { contactCache= new ChatContact(). getPropertyIndex(). loadJSONList("contacts.json"); callSerially(() -> contactsCallback.completed(contactCache)); for(ChatContact c : contactCache) { if(existsInStorage(c.name.get() + ".jpg")) { String f = c.name.get() + ".jpg"; try (InputStream is = createStorageInputStream(f)) { c.photo.set(EncodedImage.create(is)); } catch(IOException err) { Log.e(err); } } } return; } Server
  • 55.
    callSeriallyOnIdle(() -> contactsCallback.completed(contactCache)); return; } if(existsInStorage("contacts.json")) { contactCache= new ChatContact(). getPropertyIndex(). loadJSONList("contacts.json"); callSerially(() -> contactsCallback.completed(contactCache)); for(ChatContact c : contactCache) { if(existsInStorage(c.name.get() + ".jpg")) { String f = c.name.get() + ".jpg"; try (InputStream is = createStorageInputStream(f)) { c.photo.set(EncodedImage.create(is)); } catch(IOException err) { Log.e(err); } } } return; } Server
  • 56.
    callSeriallyOnIdle(() -> contactsCallback.completed(contactCache)); return; } if(existsInStorage("contacts.json")) { contactCache= new ChatContact(). getPropertyIndex(). loadJSONList("contacts.json"); callSerially(() -> contactsCallback.completed(contactCache)); for(ChatContact c : contactCache) { if(existsInStorage(c.name.get() + ".jpg")) { String f = c.name.get() + ".jpg"; try (InputStream is = createStorageInputStream(f)) { c.photo.set(EncodedImage.create(is)); } catch(IOException err) { Log.e(err); } } } return; } Server
  • 57.
    } } return; } Contact[] contacts =Display.getInstance().getAllContacts(true, true, false, true, false, false); ArrayList<ChatContact> l = new ArrayList<>(); for(Contact c : contacts) { ChatContact cc = new ChatContact(). phone.set(c.getPrimaryPhoneNumber()). name.set(c.getDisplayName()); l.add(cc); callSeriallyOnIdle(() -> { cc.photo.set(c.getPhoto()); if(cc.photo.get() != null) { contactsThread.run(() -> { String f = cc.name.get() + ".jpg"; try(OutputStream os = createStorageOutputStream(f)) { EncodedImage img = EncodedImage. createFromImage(cc.photo.get(), true); Server
  • 58.
    return; } Contact[] contacts =Display.getInstance().getAllContacts(true, true, false, true, false, false); ArrayList<ChatContact> l = new ArrayList<>(); for(Contact c : contacts) { ChatContact cc = new ChatContact(). phone.set(c.getPrimaryPhoneNumber()). name.set(c.getDisplayName()); l.add(cc); callSeriallyOnIdle(() -> { cc.photo.set(c.getPhoto()); if(cc.photo.get() != null) { contactsThread.run(() -> { String f = cc.name.get() + ".jpg"; try(OutputStream os = createStorageOutputStream(f)) { EncodedImage img = EncodedImage. createFromImage(cc.photo.get(), true); os.write(img.getImageData()); } catch(IOException err) { Log.e(err); Server
  • 59.
    ArrayList<ChatContact> l =new ArrayList<>(); for(Contact c : contacts) { ChatContact cc = new ChatContact(). phone.set(c.getPrimaryPhoneNumber()). name.set(c.getDisplayName()); l.add(cc); callSeriallyOnIdle(() -> { cc.photo.set(c.getPhoto()); if(cc.photo.get() != null) { contactsThread.run(() -> { String f = cc.name.get() + ".jpg"; try(OutputStream os = createStorageOutputStream(f)) { EncodedImage img = EncodedImage. createFromImage(cc.photo.get(), true); os.write(img.getImageData()); } catch(IOException err) { Log.e(err); } }); } }); Server
  • 60.
    ArrayList<ChatContact> l =new ArrayList<>(); for(Contact c : contacts) { ChatContact cc = new ChatContact(). phone.set(c.getPrimaryPhoneNumber()). name.set(c.getDisplayName()); l.add(cc); callSeriallyOnIdle(() -> { cc.photo.set(c.getPhoto()); if(cc.photo.get() != null) { contactsThread.run(() -> { String f = cc.name.get() + ".jpg"; try(OutputStream os = createStorageOutputStream(f)) { EncodedImage img = EncodedImage. createFromImage(cc.photo.get(), true); os.write(img.getImageData()); } catch(IOException err) { Log.e(err); } }); } }); Server
  • 61.
    cc.photo.set(c.getPhoto()); if(cc.photo.get() != null){ contactsThread.run(() -> { String f = cc.name.get() + ".jpg"; try(OutputStream os = createStorageOutputStream(f)) { EncodedImage img = EncodedImage. createFromImage(cc.photo.get(), true); os.write(img.getImageData()); } catch(IOException err) { Log.e(err); } }); } }); } PropertyIndex.storeJSONList("contacts.json", l); callSerially(() -> contactsCallback.completed(l)); } public static void findRegisteredUser(String phone, Server
  • 62.
    }); } PropertyIndex.storeJSONList("contacts.json", l); callSerially(() ->contactsCallback.completed(l)); } public static void findRegisteredUser(String phone, OnComplete<ChatContact> resultCallback) { get("user/findRegisteredUser"). queryParam("phone", phone). fetchAsPropertyList(res -> { List l = res.getResponseData(); if(l.size() == 0) { resultCallback.completed(null); return; } resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public static void findRegisteredUserById(String id, OnComplete<ChatContact> resultCallback) { Server
  • 63.
    resultCallback.completed(null); return; } resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public staticvoid findRegisteredUserById(String id, OnComplete<ChatContact> resultCallback) { get("user/findRegisteredUserById"). queryParam("id", id). fetchAsPropertyList(res -> { List l = res.getResponseData(); if(l.size() == 0) { resultCallback.completed(null); return; } resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public static void fetchChatList( Server
  • 64.
    resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public staticvoid fetchChatList( OnComplete<List<ChatContact>> contactsCallback) { fetchContacts(cl -> { List<ChatContact> response = new ArrayList<>(); for(ChatContact c : cl) { if(c.lastActivityTime.get() != null) { response.add(c); } } Collections.sort(response, (ChatContact o1, ChatContact o2) -> (int)(o1.lastActivityTime.get().getTime() - o2.lastActivityTime.get().getTime())); contactsCallback.completed(response); }); } public static void ackMessage(String messageId) { Server
  • 65.
    resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public staticvoid fetchChatList( OnComplete<List<ChatContact>> contactsCallback) { fetchContacts(cl -> { List<ChatContact> response = new ArrayList<>(); for(ChatContact c : cl) { if(c.lastActivityTime.get() != null) { response.add(c); } } Collections.sort(response, (ChatContact o1, ChatContact o2) -> (int)(o1.lastActivityTime.get().getTime() - o2.lastActivityTime.get().getTime())); contactsCallback.completed(response); }); } public static void ackMessage(String messageId) { Server
  • 66.
    resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public staticvoid fetchChatList( OnComplete<List<ChatContact>> contactsCallback) { fetchContacts(cl -> { List<ChatContact> response = new ArrayList<>(); for(ChatContact c : cl) { if(c.lastActivityTime.get() != null) { response.add(c); } } Collections.sort(response, (ChatContact o1, ChatContact o2) -> (int)(o1.lastActivityTime.get().getTime() - o2.lastActivityTime.get().getTime())); contactsCallback.completed(response); }); } public static void ackMessage(String messageId) { Server
  • 67.
    resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public staticvoid fetchChatList( OnComplete<List<ChatContact>> contactsCallback) { fetchContacts(cl -> { List<ChatContact> response = new ArrayList<>(); for(ChatContact c : cl) { if(c.lastActivityTime.get() != null) { response.add(c); } } Collections.sort(response, (ChatContact o1, ChatContact o2) -> (int)(o1.lastActivityTime.get().getTime() - o2.lastActivityTime.get().getTime())); contactsCallback.completed(response); }); } public static void ackMessage(String messageId) { Server
  • 68.
    resultCallback.completed((ChatContact)l.get(0)); }, ChatContact.class); } public staticvoid fetchChatList( OnComplete<List<ChatContact>> contactsCallback) { fetchContacts(cl -> { List<ChatContact> response = new ArrayList<>(); for(ChatContact c : cl) { if(c.lastActivityTime.get() != null) { response.add(c); } } Collections.sort(response, (ChatContact o1, ChatContact o2) -> (int)(o1.lastActivityTime.get().getTime() - o2.lastActivityTime.get().getTime())); contactsCallback.completed(response); }); } public static void ackMessage(String messageId) { Server
  • 69.
    } Collections.sort(response, (ChatContact o1, ChatContacto2) -> (int)(o1.lastActivityTime.get().getTime() - o2.lastActivityTime.get().getTime())); contactsCallback.completed(response); }); } public static void ackMessage(String messageId) { post("user/ackMessage"). body(messageId).fetchAsString(c -> {}); } public static void updatePushKey(String key) { if(user() != null) { get("user/updatePushKey"). queryParam("id", user().id.get()). queryParam("key", key).fetchAsString(c -> {}); } } } Server
  • 70.
    } Collections.sort(response, (ChatContact o1, ChatContacto2) -> (int)(o1.lastActivityTime.get().getTime() - o2.lastActivityTime.get().getTime())); contactsCallback.completed(response); }); } public static void ackMessage(String messageId) { post("user/ackMessage"). body(messageId).fetchAsString(c -> {}); } public static void updatePushKey(String key) { if(user() != null) { get("user/updatePushKey"). queryParam("id", user().id.get()). queryParam("key", key).fetchAsString(c -> {}); } } } Server