use of com.applozic.mobicomkit.feed.MessageResponse in project Applozic-Android-SDK by AppLozic.
the class MessageClientService method processMessage.
public void processMessage(Message message, Handler handler) throws Exception {
boolean isBroadcast = (message.getMessageId() == null);
MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(context);
message.setSent(Boolean.TRUE);
message.setSendToDevice(Boolean.FALSE);
message.setSuUserKeyString(userPreferences.getSuUserKeyString());
message.processContactIds(context);
Contact contact = null;
Channel channel = null;
boolean isBroadcastOneByOneGroupType = false;
boolean isOpenGroup = false;
boolean skipMessage = false;
if (message.getGroupId() == null) {
contact = baseContactService.getContactById(message.getContactIds());
} else {
channel = ChannelService.getInstance(context).getChannel(message.getGroupId());
isOpenGroup = Channel.GroupType.OPEN.getValue().equals(channel.getType());
isBroadcastOneByOneGroupType = Channel.GroupType.BROADCAST_ONE_BY_ONE.getValue().equals(channel.getType());
}
long messageId = -1;
List<String> fileKeys = new ArrayList<String>();
String keyString = null;
if (!isBroadcastOneByOneGroupType) {
keyString = UUID.randomUUID().toString();
message.setKeyString(keyString);
message.setSentToServer(false);
} else {
message.setSentToServer(true);
}
if (Message.MetaDataType.HIDDEN.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue())) || Message.MetaDataType.PUSHNOTIFICATION.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
skipMessage = true;
}
if (!skipMessage && !isOpenGroup) {
messageId = messageDatabaseService.createMessage(message);
}
if (isBroadcast && !skipMessage) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
}
if (!isBroadcastOneByOneGroupType && message.isUploadRequired() && !isOpenGroup) {
for (String filePath : message.getFilePaths()) {
try {
String fileMetaResponse = new FileClientService(context).uploadBlobImage(filePath, handler);
if (fileMetaResponse == null) {
if (skipMessage) {
return;
}
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", "Error while uploading");
msg.sendToTarget();
}
if (!message.isContactMessage()) {
messageDatabaseService.updateCanceledFlag(messageId, 1);
}
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
return;
}
if (ApplozicClient.getInstance(context).isCustomStorageServiceEnabled()) {
if (!TextUtils.isEmpty(fileMetaResponse)) {
message.setFileMetas((FileMeta) GsonUtils.getObjectFromJson(fileMetaResponse, FileMeta.class));
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", null);
msg.sendToTarget();
}
}
} else {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(fileMetaResponse).getAsJsonObject();
if (jsonObject.has(FILE_META)) {
Gson gson = new Gson();
message.setFileMetas(gson.fromJson(jsonObject.get(FILE_META), FileMeta.class));
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", null);
msg.sendToTarget();
}
}
}
} catch (Exception ex) {
Utils.printLog(context, TAG, "Error uploading file to server: " + filePath);
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", "Error uploading file to server: " + filePath);
msg.sendToTarget();
}
/* recentMessageSentToServer.remove(message);*/
if (!message.isContactMessage() && !skipMessage) {
messageDatabaseService.updateCanceledFlag(messageId, 1);
}
if (!skipMessage) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
}
return;
}
}
if (messageId != -1 && !skipMessage) {
messageDatabaseService.updateMessageFileMetas(messageId, message);
}
}
Message newMessage = new Message();
newMessage.setTo(message.getTo());
newMessage.setKeyString(message.getKeyString());
newMessage.setMessage(message.getMessage());
newMessage.setFileMetas(message.getFileMetas());
newMessage.setCreatedAtTime(message.getCreatedAtTime());
newMessage.setRead(Boolean.TRUE);
newMessage.setDeviceKeyString(message.getDeviceKeyString());
newMessage.setSuUserKeyString(message.getSuUserKeyString());
newMessage.setSent(message.isSent());
newMessage.setType(message.getType());
newMessage.setTimeToLive(message.getTimeToLive());
newMessage.setSource(message.getSource());
newMessage.setScheduledAt(message.getScheduledAt());
newMessage.setStoreOnDevice(message.isStoreOnDevice());
newMessage.setDelivered(message.getDelivered());
newMessage.setStatus(message.getStatus());
newMessage.setMetadata(message.getMetadata());
newMessage.setSendToDevice(message.isSendToDevice());
newMessage.setContentType(message.getContentType());
newMessage.setConversationId(message.getConversationId());
if (message.getGroupId() != null) {
newMessage.setGroupId(message.getGroupId());
}
if (!TextUtils.isEmpty(message.getClientGroupId())) {
newMessage.setClientGroupId(message.getClientGroupId());
}
if (contact != null && !TextUtils.isEmpty(contact.getApplicationId())) {
newMessage.setApplicationId(contact.getApplicationId());
} else {
newMessage.setApplicationId(getApplicationKey(context));
}
try {
if (!isBroadcastOneByOneGroupType) {
String response = sendMessage(newMessage);
if (message.hasAttachment() && TextUtils.isEmpty(response) && !message.isContactMessage() && !skipMessage && !isOpenGroup) {
messageDatabaseService.updateCanceledFlag(messageId, 1);
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", "Error uploading file to server");
msg.sendToTarget();
}
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
}
MessageResponse messageResponse = (MessageResponse) GsonUtils.getObjectFromJson(response, MessageResponse.class);
keyString = messageResponse.getMessageKey();
if (!TextUtils.isEmpty(keyString)) {
message.setSentMessageTimeAtServer(Long.parseLong(messageResponse.getCreatedAtTime()));
message.setConversationId(messageResponse.getConversationId());
message.setSentToServer(true);
message.setKeyString(keyString);
}
if (!skipMessage && !isOpenGroup) {
messageDatabaseService.updateMessage(messageId, message.getSentMessageTimeAtServer(), keyString, message.isSentToServer());
}
} else {
message.setSentMessageTimeAtServer(message.getCreatedAtTime());
messageDatabaseService.updateMessage(messageId, message.getSentMessageTimeAtServer(), keyString, message.isSentToServer());
}
if (message.isSentToServer()) {
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.MESSAGE_SENT;
msg.getData().putString("message", message.getKeyString());
msg.sendToTarget();
}
}
if (!TextUtils.isEmpty(keyString)) {
// Todo: Handle server message add failure due to internet disconnect.
} else {
// Todo: If message type is mtext, tell user that internet is not working, else send update with db id.
}
if (!skipMessage || isOpenGroup) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.MESSAGE_SYNC_ACK_FROM_SERVER.toString(), message);
}
} catch (Exception e) {
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", "Error uploading file");
msg.sendToTarget();
// handler.onCompleted(new ApplozicException("Error uploading file"));
}
}
/* if (recentMessageSentToServer.size() > 20) {
recentMessageSentToServer.subList(0, 10).clear();
}*/
}
use of com.applozic.mobicomkit.feed.MessageResponse in project Applozic-Android-SDK by AppLozic.
the class MessageClientService method processMessage.
// Cleanup: private
public void processMessage(Message message, Handler handler, String userDisplayName) throws Exception {
boolean isBroadcast = (message.getMessageId() == null);
MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(context);
message.setSent(Boolean.TRUE);
message.setSendToDevice(Boolean.FALSE);
message.setSuUserKeyString(userPreferences.getSuUserKeyString());
message.processContactIds(context);
Contact contact = null;
Channel channel = null;
boolean isBroadcastOneByOneGroupType = false;
boolean isOpenGroup = false;
boolean skipMessage = false;
if (message.getGroupId() == null) {
contact = baseContactService.getContactById(message.getContactIds());
} else {
channel = ChannelService.getInstance(context).getChannel(message.getGroupId());
isOpenGroup = (Channel.GroupType.OPEN.getValue().equals(channel.getType()) && !message.hasAttachment());
isBroadcastOneByOneGroupType = Channel.GroupType.BROADCAST_ONE_BY_ONE.getValue().equals(channel.getType());
}
long messageId = -1;
List<String> fileKeys = new ArrayList<String>();
String keyString = null;
String oldMessageKey = null;
if (!isBroadcastOneByOneGroupType) {
keyString = UUID.randomUUID().toString();
oldMessageKey = keyString;
message.setKeyString(keyString);
message.setSentToServer(false);
} else {
message.setSentToServer(true);
}
if (Message.MetaDataType.HIDDEN.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue())) || Message.MetaDataType.PUSHNOTIFICATION.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
skipMessage = true;
}
if (!skipMessage && !isOpenGroup) {
messageId = messageDatabaseService.createMessage(message);
}
if (isBroadcast && !skipMessage) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
}
if (!isBroadcastOneByOneGroupType && message.isUploadRequired()) {
for (String filePath : message.getFilePaths()) {
FileMeta thumbnailFileMeta = null;
String mimeType = FileUtils.getMimeType(filePath);
if (mimeType != null && mimeType.startsWith("video")) {
thumbnailFileMeta = uploadVideoThumbnail(filePath, message.getCreatedAtTime(), oldMessageKey);
}
try {
String fileMetaResponse = new FileClientService(context).uploadBlobImage(filePath, handler, oldMessageKey);
if (fileMetaResponse == null) {
if (skipMessage) {
return;
}
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", "Error while uploading");
msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
msg.sendToTarget();
}
if (!message.isContactMessage()) {
messageDatabaseService.updateCanceledFlag(messageId, 1);
}
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
return;
}
if (ApplozicClient.getInstance(context).isS3StorageServiceEnabled()) {
if (!TextUtils.isEmpty(fileMetaResponse)) {
message.setFileMetas((FileMeta) GsonUtils.getObjectFromJson(fileMetaResponse, FileMeta.class));
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
msg.getData().putString("error", null);
msg.sendToTarget();
}
}
} else {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(fileMetaResponse).getAsJsonObject();
if (jsonObject.has(FILE_META)) {
Gson gson = new Gson();
message.setFileMetas(gson.fromJson(jsonObject.get(FILE_META), FileMeta.class));
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
msg.getData().putString("error", null);
msg.sendToTarget();
}
}
}
// update message file-meta with thumbnail data
if (thumbnailFileMeta != null) {
FileMeta messageFileMeta = message.getFileMetas();
if (!TextUtils.isEmpty(thumbnailFileMeta.getBlobKeyString())) {
messageFileMeta.setThumbnailBlobKey(thumbnailFileMeta.getBlobKeyString());
}
if (!TextUtils.isEmpty(thumbnailFileMeta.getThumbnailUrl())) {
messageFileMeta.setThumbnailUrl(thumbnailFileMeta.getThumbnailUrl());
}
message.setFileMetas(messageFileMeta);
}
} catch (Exception ex) {
Utils.printLog(context, TAG, "Error uploading file to server: " + filePath);
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
msg.getData().putString("error", "Error uploading file to server: " + filePath);
msg.sendToTarget();
}
/* recentMessageSentToServer.remove(message);*/
if (!message.isContactMessage() && !skipMessage) {
messageDatabaseService.updateCanceledFlag(messageId, 1);
}
if (!skipMessage) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
}
return;
}
}
if (messageId != -1 && !skipMessage) {
messageDatabaseService.updateMessageFileMetas(messageId, message);
}
}
Message newMessage = new Message();
newMessage.setTo(message.getTo());
newMessage.setKeyString(message.getKeyString());
newMessage.setMessage(message.getMessage());
newMessage.setFileMetas(message.getFileMetas());
newMessage.setCreatedAtTime(message.getCreatedAtTime());
newMessage.setRead(Boolean.TRUE);
newMessage.setDeviceKeyString(message.getDeviceKeyString());
newMessage.setSuUserKeyString(message.getSuUserKeyString());
newMessage.setSent(message.isSent());
newMessage.setType(message.getType());
newMessage.setTimeToLive(message.getTimeToLive());
newMessage.setSource(message.getSource());
newMessage.setScheduledAt(message.getScheduledAt());
newMessage.setStoreOnDevice(message.isStoreOnDevice());
newMessage.setDelivered(message.getDelivered());
newMessage.setStatus(message.getStatus());
newMessage.setMetadata(message.getMetadata());
newMessage.setSendToDevice(message.isSendToDevice());
newMessage.setContentType(message.getContentType());
newMessage.setConversationId(message.getConversationId());
if (message.getGroupId() != null) {
newMessage.setGroupId(message.getGroupId());
}
if (!TextUtils.isEmpty(message.getClientGroupId())) {
newMessage.setClientGroupId(message.getClientGroupId());
}
if (contact != null && !TextUtils.isEmpty(contact.getApplicationId())) {
newMessage.setApplicationId(contact.getApplicationId());
} else {
newMessage.setApplicationId(getApplicationKey(context));
}
try {
if (!isBroadcastOneByOneGroupType) {
String response = sendMessage(newMessage);
if (message.hasAttachment() && TextUtils.isEmpty(response) && !message.isContactMessage() && !skipMessage && !isOpenGroup) {
messageDatabaseService.updateCanceledFlag(messageId, 1);
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString("error", "Error uploading file to server");
msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
msg.sendToTarget();
}
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
}
MessageResponse messageResponse = (MessageResponse) GsonUtils.getObjectFromJson(response, MessageResponse.class);
keyString = messageResponse.getMessageKey();
if (!TextUtils.isEmpty(keyString)) {
message.setSentMessageTimeAtServer(Long.parseLong(messageResponse.getCreatedAtTime()));
message.setConversationId(messageResponse.getConversationId());
message.setSentToServer(true);
message.setKeyString(keyString);
if (contact != null && !TextUtils.isEmpty(userDisplayName) && contact.isUserDisplayUpdateRequired()) {
UserService.getInstance(context).updateUserDisplayName(message.getTo(), userDisplayName);
}
}
if (!skipMessage && !isOpenGroup) {
messageDatabaseService.updateMessage(messageId, message.getSentMessageTimeAtServer(), keyString, message.isSentToServer());
}
} else {
message.setSentMessageTimeAtServer(message.getCreatedAtTime());
messageDatabaseService.updateMessage(messageId, message.getSentMessageTimeAtServer(), keyString, message.isSentToServer());
}
if (message.isSentToServer()) {
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.MESSAGE_SENT;
msg.getData().putString(MobiComKitConstants.MESSAGE_INTENT_EXTRA, message.getKeyString());
String messageJson = GsonUtils.getJsonFromObject(message, Message.class);
msg.getData().putString(MobiComKitConstants.MESSAGE_JSON_INTENT_EXTRA, messageJson);
msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
msg.sendToTarget();
}
}
if (!TextUtils.isEmpty(keyString)) {
// Todo: Handle server message add failure due to internet disconnect.
} else {
// Todo: If message type is mtext, tell user that internet is not working, else send update with db id.
}
if (!skipMessage || isOpenGroup) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.MESSAGE_SYNC_ACK_FROM_SERVER.toString(), message);
}
} catch (Exception e) {
if (handler != null) {
android.os.Message msg = handler.obtainMessage();
msg.what = MobiComConversationService.UPLOAD_COMPLETED;
msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
msg.getData().putString("error", "Error uploading file");
msg.sendToTarget();
// handler.onCompleted(new ApplozicException("Error uploading file"));
}
}
/* if (recentMessageSentToServer.size() > 20) {
recentMessageSentToServer.subList(0, 10).clear();
}*/
}
use of com.applozic.mobicomkit.feed.MessageResponse in project Applozic-Android-SDK by AppLozic.
the class MessageClientService method sendPendingMessageToServer.
// Cleanup: private
public void sendPendingMessageToServer(Message message, boolean broadcast) {
try {
if (message.isContactMessage()) {
try {
this.processMessage(message, null, null);
} catch (Exception e) {
Utils.printLog(context, TAG, "Exception while sending contact message.");
}
return;
}
if (message.hasAttachment()) {
return;
}
MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
message.setDeviceKeyString(mobiComUserPreference.getDeviceKeyString());
message.setSuUserKeyString(mobiComUserPreference.getSuUserKeyString());
String response = sendMessage(message);
if (TextUtils.isEmpty(response) || response.contains("<html>") || response.equals("error")) {
Utils.printLog(context, TAG, "Error while sending pending messages.");
return;
}
MessageResponse messageResponse = (MessageResponse) GsonUtils.getObjectFromJson(response, MessageResponse.class);
String keyString = messageResponse.getMessageKey();
String createdAt = messageResponse.getCreatedAtTime();
message.setSentMessageTimeAtServer(Long.parseLong(createdAt));
message.setKeyString(keyString);
message.setSentToServer(true);
if (broadcast) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.MESSAGE_SYNC_ACK_FROM_SERVER.toString(), message);
}
messageDatabaseService.updateMessageSyncStatus(message, keyString);
if (message.getGroupId() == null && TextUtils.isEmpty(message.getContactIds())) {
Contact contact = contactDatabase.getContactById(message.getContactIds());
if (contact != null && contact.isUserDisplayUpdateRequired()) {
UserService.getInstance(context).updateUserDisplayName(contact.getUserId(), contact.getDisplayName());
}
}
} catch (Exception e) {
Utils.printLog(context, TAG, "Error while sending pending messages.");
}
}
Aggregations