use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.
the class XMPPService method processXMPPMessage.
public void processXMPPMessage(String sender, String name, String message) {
ChatMessage chatMessage = new ChatMessage(sender, message, name, false);
chatMessage.save();
if (XMPPService.this.username != null && sender.toLowerCase().equals(XMPPService.this.username.toLowerCase())) {
Log.d("Event posting: ", message);
EventBus.getDefault().post(new MessageReceivedEvent(chatMessage));
} else {
notifyMessage(sender, name, message);
}
}
use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.
the class XMPPService method processMessage.
private void processMessage(String sender, String message) {
String name = PreferencesManager.getInstance(getApplicationContext()).getBusinessName(sender);
ChatResponse chatResponse = null;
try {
chatResponse = gson.fromJson(message, ChatResponse.class);
} catch (Exception e) {
// e.printStackTrace();
}
if (chatResponse != null) {
if (chatResponse.getTyping() != null) {
if (XMPPService.this.username != null && sender.toLowerCase().equals(XMPPService.this.username.toLowerCase())) {
Log.d("Event posting: ", message);
EventBus.getDefault().post(new TypingEvent(sender, chatResponse.getTyping()));
}
}
if (chatResponse.isValid()) {
ChatMessage chatMessage = new ChatMessage(sender, message, sender, false);
chatMessage.save();
if (XMPPService.this.username != null && sender.toLowerCase().equals(XMPPService.this.username.toLowerCase())) {
Log.d("Event posting: ", message);
EventBus.getDefault().post(new MessageReceivedEvent(chatMessage));
}
}
if (chatResponse.getTyping() == null && !(XMPPService.this.username != null && sender.toLowerCase().equals(XMPPService.this.username.toLowerCase()))) {
notifyMessage(sender, name, "...");
}
} else {
processXMPPMessage(sender, name, message);
}
}
use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.
the class XMPPService method createConnection.
private void createConnection() throws XmppStringprepException, NoSuchAlgorithmException {
if (mConnection == null) {
AndroidSmackInitializer androidSmackInitializer = new AndroidSmackInitializer();
androidSmackInitializer.initialize();
ExtensionsInitializer extensionsInitializer = new ExtensionsInitializer();
extensionsInitializer.initialize();
XMPPUser xmppUser = PreferencesManager.getInstance(XMPPService.this.getApplicationContext()).getXMPPUser();
XMPPTCPConnectionConfiguration connConfig = XMPPTCPConnectionConfiguration.builder().setXmppDomain(JidCreate.domainBareFrom(DOMAIN)).setHost(HOST).setPort(PORT).setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible).setCustomSSLContext(SSLContext.getInstance("TLS")).setSocketFactory(SSLSocketFactory.getDefault()).setUsernameAndPassword(xmppUser.getUsername(), xmppUser.getPassword()).build();
SmackConfiguration.setDefaultPacketReplyTimeout(5000);
XMPPTCPConnection.setUseStreamManagementDefault(true);
XMPPTCPConnection.setUseStreamManagementResumptionDefault(true);
mConnection = new XMPPTCPConnection(connConfig);
mConnection.setPacketReplyTimeout(5000);
mConnection.setPreferredResumptionTime(10);
mConnection.setUseStreamManagement(true);
mConnection.setUseStreamManagementResumption(true);
mConnection.addAsyncStanzaListener(packetListener, packetFilter);
mConnection.addAsyncStanzaListener(pingPacketListener, pingPacketFilter);
mConnection.addStanzaAcknowledgedListener(new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
// TODO Acknowledgement
ChatMessage chatMessage = ChatMessageDAO.getChatMessageByStanzaId(packet.getStanzaId());
if (chatMessage != null) {
chatMessage.setAcknowledged(true);
chatMessage.save();
EventBus.getDefault().post(new MessageAcknowledgementEvent(chatMessage, chatMessage.getAcknowledged()));
}
}
});
SASLAuthentication.unregisterSASLMechanism("org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism");
SASLAuthentication.registerSASLMechanism(new CustomSCRAMSHA1Mechanism());
mConnection.addConnectionListener(connectionListener);
ServerPingWithAlarmManager.getInstanceFor(mConnection).setEnabled(true);
// ReconnectionManager.getInstanceFor(mConnection).enableAutomaticReconnection();
}
try {
if (!mConnection.isConnected() && !mConnection.isAuthenticated()) {
mConnection.connect();
} else if (mConnection.isConnected() && !mConnection.isAuthenticated()) {
mConnection.login();
}
} catch (Exception e) {
// e.printStackTrace();
}
}
use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.
the class ChatListAdapter method getOwnView.
/*
RENDERING OWN VIEW I.E; right side of the chat
*/
private View getOwnView(int position, View convertView, ViewGroup parent) {
final ChatMessage chatMessage = values.get(position);
View view = convertView;
ViewHolderOwn ownViewHolder = null;
if (view == null) {
view = inflater.inflate(R.layout.chat_list_item_own, parent, false);
ownViewHolder = new ViewHolderOwn();
ownViewHolder.message = (TextView) view.findViewById(R.id.message);
ownViewHolder.timestamp = (TextView) view.findViewById(R.id.timestamp);
ownViewHolder.messageHolder = view.findViewById(R.id.message_holder);
ownViewHolder.productLayout = view.findViewById(R.id.product_image_layout);
ownViewHolder.productImage = (ImageView) view.findViewById(R.id.product_image);
ownViewHolder.productTitle = (TextView) view.findViewById(R.id.product_title);
ownViewHolder.productPrice = (TextView) view.findViewById(R.id.product_price);
view.setTag(ownViewHolder);
} else {
ownViewHolder = (ViewHolderOwn) view.getTag();
}
ownViewHolder.messageHolder.setOnClickListener(messageSendListener);
if (chatMessage.getBitmap() != null) {
ownViewHolder.productLayout.setVisibility(View.VISIBLE);
ownViewHolder.message.setVisibility(GONE);
ownViewHolder.productPrice.setVisibility(GONE);
ownViewHolder.productTitle.setVisibility(GONE);
ownViewHolder.productImage.setVisibility(View.VISIBLE);
ownViewHolder.productImage.setImageBitmap(chatMessage.getBitmap());
ownViewHolder.productImage.setOnClickListener(null);
} else {
ownViewHolder.message.setVisibility(View.VISIBLE);
ownViewHolder.productLayout.setVisibility(GONE);
MessageObject messageObject = null;
try {
messageObject = gson.fromJson(chatMessage.getMessage(), MessageObject.class);
} catch (Exception e) {
// e.printStackTrace();
}
if (messageObject != null && (messageObject.getMessage() != null || messageObject.getImage() != null)) {
ownViewHolder.message.setVisibility(GONE);
String title = messageObject.getMessage();
if (title != null) {
ownViewHolder.productTitle.setText(title);
ownViewHolder.productTitle.setVisibility(View.VISIBLE);
} else {
ownViewHolder.productTitle.setVisibility(GONE);
}
if (messageObject.getPriceString() != null) {
ownViewHolder.productPrice.setText(messageObject.getPriceString());
ownViewHolder.productPrice.setVisibility(View.VISIBLE);
} else {
ownViewHolder.productPrice.setVisibility(GONE);
}
if (messageObject.getImage() != null) {
ownViewHolder.productImage.setVisibility(View.VISIBLE);
DrawableManager.getInstance(context).fetchDrawableOnThread(messageObject.getImage(), ownViewHolder.productImage);
final String imageUrl = messageObject.getImage();
ownViewHolder.productImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ImageActivity.class);
Bundle bundle = new Bundle();
bundle.putStringArrayList("urls", new ArrayList<>(Collections.singletonList(imageUrl)));
intent.putExtras(bundle);
((Activity) context).startActivityForResult(intent, 0);
}
});
} else {
ownViewHolder.productImage.setVisibility(GONE);
}
ownViewHolder.productLayout.setVisibility(View.VISIBLE);
} else {
ownViewHolder.productLayout.setVisibility(GONE);
ownViewHolder.message.setVisibility(View.VISIBLE);
ownViewHolder.timestamp.setVisibility(View.VISIBLE);
ownViewHolder.message.setText(Html.fromHtml(chatMessage.getMessage()));
}
}
String timestampText = null;
try {
timestampText = DateUtils.getRelativeTimeSpanString(format.parse(values.get(position).getTimestamp()).getTime(), (new Date()).getTime(), DateUtils.FORMAT_ABBREV_RELATIVE).toString() + " - " + (chatMessage.getAcknowledged() ? "Sent" : "Sending..");
} catch (ParseException e) {
e.printStackTrace();
}
ownViewHolder.timestamp.setText(timestampText);
return view;
}
use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.
the class ChatListAdapter method getDeepLinkView.
private View getDeepLinkView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ChatMessage chatMessage = values.get(position);
DeepLinkViewHolder deepLinkViewHolder = null;
if (view == null) {
view = inflater.inflate(R.layout.chat_list_item_deeplink, parent, false);
deepLinkViewHolder = new DeepLinkViewHolder();
deepLinkViewHolder.message = (TextView) view.findViewById(R.id.message);
deepLinkViewHolder.timestamp = (TextView) view.findViewById(R.id.timestamp);
deepLinkViewHolder.deepLinkButton = view.findViewById(R.id.deep_link_button);
deepLinkViewHolder.buyButton = (TextView) view.findViewById(R.id.buy_button);
view.setTag(deepLinkViewHolder);
} else {
deepLinkViewHolder = (DeepLinkViewHolder) view.getTag();
}
deepLinkViewHolder.message.setText(chatMessage.getChatResponse().getDeepLink().getMessage());
deepLinkViewHolder.buyButton.setText(chatMessage.getChatResponse().getDeepLink().getButtonText().toUpperCase());
deepLinkViewHolder.deepLinkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ChatActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
bundle.putString("user", chatMessage.getChatResponse().getDeepLink().getUser());
bundle.putString("placeId", chatMessage.getChatResponse().getDeepLink().getPlaceId());
bundle.putString("name", chatMessage.getChatResponse().getDeepLink().getName());
if (chatMessage.getChatResponse().getDeepLink().getAction() != null) {
bundle.putString("action", chatMessage.getChatResponse().getDeepLink().getAction());
}
if (chatMessage.getChatResponse().getDeepLink().getCategory() != null) {
bundle.putString("category", chatMessage.getChatResponse().getDeepLink().getCategory());
}
intent.putExtras(bundle);
context.startActivity(intent);
}
});
try {
deepLinkViewHolder.timestamp.setText(DateUtils.getRelativeTimeSpanString(format.parse(values.get(position).getTimestamp()).getTime(), (new Date()).getTime(), DateUtils.FORMAT_ABBREV_RELATIVE).toString());
} catch (ParseException e) {
e.printStackTrace();
}
return view;
}
Aggregations