use of com.applozic.mobicomkit.api.account.user.MobiComUserPreference in project Applozic-Android-SDK by AppLozic.
the class RegisterUserClientService method updateAccount.
private RegistrationResponse updateAccount(String email, String userId, String phoneNumber, String displayName, String imageLink, String pushNotificationId) throws Exception {
MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
User user = new User();
user.setUserId(userId);
user.setEmail(email);
user.setImageLink(imageLink);
user.setRegistrationId(pushNotificationId);
user.setDisplayName(displayName);
// user.setCountryCode(mobiComUserPreference.getCountryCode());
/*if (!TextUtils.isEmpty(phoneNumber)) {
try {
user.setCountryCode(PhoneNumberUtil.getInstance().getRegionCodeForNumber(PhoneNumberUtil.getInstance().parse(phoneNumber, "")));
mobiComUserPreference.setCountryCode(user.getCountryCode());
} catch (NumberParseException e) {
e.printStackTrace();
}
}*/
user.setContactNumber(phoneNumber);
final RegistrationResponse registrationResponse = createAccount(user);
Intent intent = new Intent(context, ApplozicMqttIntentService.class);
intent.putExtra(ApplozicMqttIntentService.CONNECTED_PUBLISH, true);
ApplozicMqttIntentService.enqueueWork(context, intent);
return registrationResponse;
}
use of com.applozic.mobicomkit.api.account.user.MobiComUserPreference in project Applozic-Android-SDK by AppLozic.
the class RegisterUserClientService method createAccount.
public RegistrationResponse createAccount(String email, String userId, String phoneNumber, String displayName, String imageLink, String pushNotificationId) throws Exception {
MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
String url = mobiComUserPreference.getUrl();
mobiComUserPreference.clearAll();
mobiComUserPreference.setUrl(url);
return updateAccount(email, userId, phoneNumber, displayName, imageLink, pushNotificationId);
}
use of com.applozic.mobicomkit.api.account.user.MobiComUserPreference in project Applozic-Android-SDK by AppLozic.
the class MessageClientService method sendPendingMessageToServer.
public void sendPendingMessageToServer(Message message, boolean broadcast) {
try {
if (message.isContactMessage()) {
try {
this.processMessage(message, 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);
if (broadcast) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.MESSAGE_SYNC_ACK_FROM_SERVER.toString(), message);
}
messageDatabaseService.updateMessageSyncStatus(message, keyString);
} catch (Exception e) {
Utils.printLog(context, TAG, "Error while sending pending messages.");
}
}
use of com.applozic.mobicomkit.api.account.user.MobiComUserPreference 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.api.account.user.MobiComUserPreference in project Applozic-Android-SDK by AppLozic.
the class MobiComConversationFragment method sendMessage.
public void sendMessage(String message, Map<String, String> messageMetaData, FileMeta fileMetas, String fileMetaKeyStrings, short messageContentType) {
MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(getActivity());
Message messageToSend = new Message();
if (channel != null) {
messageToSend.setGroupId(channel.getKey());
if (!TextUtils.isEmpty(channel.getClientGroupId())) {
messageToSend.setClientGroupId(channel.getClientGroupId());
}
/* List<String> contactIds = new ArrayList<String>();
List<String> toList = new ArrayList<String>();
for (Contact contact : channel.getContacts()) {
if (!TextUtils.isEmpty(contact.getContactNumber())) {
toList.add(contact.getContactNumber());
contactIds.add(contact.getFormattedContactNumber());
}
}
messageToSend.setTo(TextUtils.join(",", toList));
messageToSend.setContactIds(TextUtils.join(",", contactIds));*/
} else {
messageToSend.setTo(contact.getContactIds());
messageToSend.setContactIds(contact.getContactIds());
}
messageToSend.setRead(Boolean.TRUE);
messageToSend.setStoreOnDevice(Boolean.TRUE);
if (messageToSend.getCreatedAtTime() == null) {
messageToSend.setCreatedAtTime(System.currentTimeMillis() + userPreferences.getDeviceTimeOffset());
}
if (currentConversationId != null && currentConversationId != 0) {
messageToSend.setConversationId(currentConversationId);
}
messageToSend.setSendToDevice(Boolean.FALSE);
messageToSend.setType(sendType.getSelectedItemId() == 1 ? Message.MessageType.MT_OUTBOX.getValue() : Message.MessageType.OUTBOX.getValue());
messageToSend.setTimeToLive(getTimeToLive());
messageToSend.setMessage(message);
messageToSend.setDeviceKeyString(userPreferences.getDeviceKeyString());
messageToSend.setScheduledAt(scheduledTimeHolder.getTimestamp());
messageToSend.setSource(Message.Source.MT_MOBILE_APP.getValue());
if (!TextUtils.isEmpty(filePath)) {
List<String> filePaths = new ArrayList<String>();
filePaths.add(filePath);
messageToSend.setFilePaths(filePaths);
if (messageContentType == Message.ContentType.AUDIO_MSG.getValue() || messageContentType == Message.ContentType.CONTACT_MSG.getValue() || messageContentType == Message.ContentType.VIDEO_MSG.getValue()) {
messageToSend.setContentType(messageContentType);
} else {
messageToSend.setContentType(Message.ContentType.ATTACHMENT.getValue());
}
} else {
messageToSend.setContentType(messageContentType);
}
messageToSend.setFileMetaKeyStrings(fileMetaKeyStrings);
messageToSend.setFileMetas(fileMetas);
if (!TextUtils.isEmpty(ApplozicClient.getInstance(getActivity()).getMessageMetaData())) {
Type mapType = new TypeToken<Map<String, String>>() {
}.getType();
Map<String, String> messageMetaDataMap = null;
try {
messageMetaDataMap = new Gson().fromJson(ApplozicClient.getInstance(getActivity()).getMessageMetaData(), mapType);
messageToSend.setMetadata(messageMetaDataMap);
} catch (Exception e) {
e.printStackTrace();
}
} else {
messageToSend.setMetadata(this.messageMetaData);
}
conversationService.sendMessage(messageToSend, messageIntentClass);
if (replayRelativeLayout != null) {
replayRelativeLayout.setVisibility(View.GONE);
}
if (selfDestructMessageSpinner != null) {
selfDestructMessageSpinner.setSelection(0);
}
attachmentLayout.setVisibility(View.GONE);
if (channel != null && channel.getType() != null && Channel.GroupType.BROADCAST_ONE_BY_ONE.getValue().equals(channel.getType())) {
sendBroadcastMessage(message, filePath);
}
this.messageMetaData = null;
filePath = null;
}
Aggregations