Search in sources :

Example 11 with ChatMessage

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);
    }
}
Also used : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) MessageReceivedEvent(com.yellowmessenger.sdk.events.MessageReceivedEvent)

Example 12 with ChatMessage

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);
    }
}
Also used : TypingEvent(com.yellowmessenger.sdk.events.TypingEvent) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) SmackException(org.jivesoftware.smack.SmackException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageReceivedEvent(com.yellowmessenger.sdk.events.MessageReceivedEvent)

Example 13 with ChatMessage

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();
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) CustomSCRAMSHA1Mechanism(com.yellowmessenger.sdk.xmpp.CustomSCRAMSHA1Mechanism) MessageAcknowledgementEvent(com.yellowmessenger.sdk.events.MessageAcknowledgementEvent) SmackException(org.jivesoftware.smack.SmackException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AndroidSmackInitializer(org.jivesoftware.smack.android.AndroidSmackInitializer) ExtensionsInitializer(org.jivesoftware.smack.extensions.ExtensionsInitializer) XMPPUser(com.yellowmessenger.sdk.models.XMPPUser)

Example 14 with ChatMessage

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;
}
Also used : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Intent(android.content.Intent) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) ParseException(java.text.ParseException) Date(java.util.Date) ImageActivity(com.yellowmessenger.sdk.ImageActivity) ParseException(java.text.ParseException) MessageObject(com.yellowmessenger.sdk.models.MessageObject)

Example 15 with ChatMessage

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;
}
Also used : ChatActivity(com.yellowmessenger.sdk.ChatActivity) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) Bundle(android.os.Bundle) Intent(android.content.Intent) ParseException(java.text.ParseException) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) Date(java.util.Date)

Aggregations

ChatMessage (com.yellowmessenger.sdk.models.db.ChatMessage)29 SendMessageEvent (com.yellowmessenger.sdk.events.SendMessageEvent)13 View (android.view.View)7 HorizontalScrollView (android.widget.HorizontalScrollView)7 ImageView (android.widget.ImageView)7 TextView (android.widget.TextView)7 ParseException (java.text.ParseException)7 Date (java.util.Date)7 Subscribe (org.greenrobot.eventbus.Subscribe)5 Intent (android.content.Intent)4 ChatResponse (com.yellowmessenger.sdk.models.ChatResponse)3 Question (com.yellowmessenger.sdk.models.Question)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SmackException (org.jivesoftware.smack.SmackException)3 JSONObject (org.json.JSONObject)3 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)3 Paint (android.graphics.Paint)2 Bundle (android.os.Bundle)2 ViewGroup (android.view.ViewGroup)2 MessageReceivedEvent (com.yellowmessenger.sdk.events.MessageReceivedEvent)2