Search in sources :

Example 11 with CometChatException

use of com.cometchat.pro.exceptions.CometChatException in project android-java-chat-push-notification-app by cometchat-pro.

the class MessageAdapter method setReactionSupport.

private void setReactionSupport(BaseMessage baseMessage, ChipGroup reactionLayout) {
    HashMap<String, String> reactionOnMessage = Extensions.getReactionsOnMessage(baseMessage);
    if (reactionOnMessage != null && reactionOnMessage.size() > 0) {
        reactionLayout.setVisibility(View.VISIBLE);
        reactionLayout.removeAllViews();
        for (String str : reactionOnMessage.keySet()) {
            // if (reactionLayout.getChildCount()<reactionOnMessage.size()) {
            Chip chip = new Chip(context);
            chip.setChipStrokeWidth(2f);
            chip.setChipBackgroundColor(ColorStateList.valueOf(context.getResources().getColor(android.R.color.transparent)));
            chip.setChipStrokeColor(ColorStateList.valueOf(Color.parseColor(UIKitSettings.getColor())));
            chip.setText(str + " " + reactionOnMessage.get(str));
            reactionLayout.addView(chip);
            chip.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View view) {
                    Intent intent = new Intent(context, CometChatReactionInfoActivity.class);
                    intent.putExtra(UIKitConstants.IntentStrings.REACTION_INFO, baseMessage.getMetadata().toString());
                    context.startActivity(intent);
                    return true;
                }
            });
            chip.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    JSONObject body = new JSONObject();
                    try {
                        body.put("msgId", baseMessage.getId());
                        body.put("emoji", str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    CometChat.callExtension("reactions", "POST", "/v1/react", body, new CometChat.CallbackListener<JSONObject>() {

                        @Override
                        public void onSuccess(JSONObject responseObject) {
                        // ReactionModel added successfully.
                        }

                        @Override
                        public void onError(CometChatException e) {
                        // Some error occured.
                        }
                    });
                }
            });
        // }
        }
    }
}
Also used : CometChatException(com.cometchat.pro.exceptions.CometChatException) Intent(android.content.Intent) Chip(com.google.android.material.chip.Chip) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CardView(androidx.cardview.widget.CardView) TextView(android.widget.TextView) MaterialCardView(com.google.android.material.card.MaterialCardView) CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) CometChatReactionInfoActivity(com.cometchat.pro.uikit.ui_components.messages.extensions.Reactions.CometChatReactionInfoActivity)

Example 12 with CometChatException

use of com.cometchat.pro.exceptions.CometChatException in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatThreadMessageList method showBottomSheet.

private void showBottomSheet(CometChatMessageActions messageActionFragment) {
    messageActionFragment.show(getFragmentManager(), messageActionFragment.getTag());
    messageActionFragment.setMessageActionListener(new CometChatMessageActions.MessageActionListener() {

        @Override
        public void onPrivateReplyToUser() {
            if (baseMessage != null) {
                User user = baseMessage.getSender();
                Intent intent = new Intent(context, CometChatMessageListActivity.class);
                intent.putExtra(UIKitConstants.IntentStrings.UID, user.getUid());
                intent.putExtra(UIKitConstants.IntentStrings.AVATAR, user.getAvatar());
                intent.putExtra(UIKitConstants.IntentStrings.STATUS, user.getStatus());
                intent.putExtra(UIKitConstants.IntentStrings.NAME, user.getName());
                intent.putExtra(UIKitConstants.IntentStrings.LINK, user.getLink());
                intent.putExtra(UIKitConstants.IntentStrings.TYPE, CometChatConstants.RECEIVER_TYPE_USER);
                startActivity(intent);
                if (getActivity() != null)
                    getActivity().finish();
            }
        }

        @Override
        public void onThreadMessageClick() {
        }

        @Override
        public void onEditMessageClick() {
            if (isParent)
                editParentMessage();
            else
                editThreadMessage();
        }

        @Override
        public void onReplyMessageClick() {
        }

        @Override
        public void onReplyMessagePrivately() {
        }

        @Override
        public void onForwardMessageClick() {
            if (isParent)
                startForwardThreadActivity();
            else
                startForwardMessageActivity();
        }

        @Override
        public void onDeleteMessageClick() {
            deleteMessage(baseMessage);
            if (messageAdapter != null) {
                messageAdapter.clearLongClickSelectedItem();
                messageAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onCopyMessageClick() {
            String copyMessage = "";
            if (isParent) {
                copyMessage = message;
                isParent = true;
            } else {
                for (BaseMessage bMessage : baseMessages) {
                    if (bMessage.getDeletedAt() == 0 && bMessage instanceof TextMessage) {
                        copyMessage = copyMessage + "[" + Utils.getLastMessageDate(context, bMessage.getSentAt()) + "] " + bMessage.getSender().getName() + ": " + ((TextMessage) bMessage).getText();
                    }
                }
                isParent = false;
            }
            Log.e(TAG, "onCopy: " + message);
            ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("ThreadMessageAdapter", copyMessage);
            clipboardManager.setPrimaryClip(clipData);
            Toast.makeText(context, getResources().getString(R.string.text_copied), Toast.LENGTH_LONG).show();
            if (messageAdapter != null) {
                messageAdapter.clearLongClickSelectedItem();
                messageAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onShareMessageClick() {
            shareMessage();
        }

        @Override
        public void onMessageInfoClick() {
            Intent intent = new Intent(context, CometChatMessageInfoScreenActivity.class);
            if (isParent) {
            } else {
                intent.putExtra(UIKitConstants.IntentStrings.ID, baseMessage.getId());
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, baseMessage.getType());
                intent.putExtra(UIKitConstants.IntentStrings.SENTAT, baseMessage.getSentAt());
                if (baseMessage.getType().equals(CometChatConstants.MESSAGE_TYPE_TEXT)) {
                    intent.putExtra(UIKitConstants.IntentStrings.TEXTMESSAGE, Extensions.checkProfanityMessage(context, baseMessage));
                } else if (baseMessage.getCategory().equals(CometChatConstants.CATEGORY_CUSTOM)) {
                    if (((CustomMessage) baseMessage).getCustomData() != null)
                        intent.putExtra(UIKitConstants.IntentStrings.CUSTOM_MESSAGE, ((CustomMessage) baseMessage).getCustomData().toString());
                    if (baseMessage.getType().equals(UIKitConstants.IntentStrings.LOCATION)) {
                        intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.LOCATION);
                    }
                } else {
                    boolean isImageNotSafe = Extensions.getImageModeration(context, baseMessage);
                    intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_URL, ((MediaMessage) baseMessage).getAttachment().getFileUrl());
                    intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_NAME, ((MediaMessage) baseMessage).getAttachment().getFileName());
                    intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_SIZE, ((MediaMessage) baseMessage).getAttachment().getFileSize());
                    intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_EXTENSION, ((MediaMessage) baseMessage).getAttachment().getFileExtension());
                    intent.putExtra(UIKitConstants.IntentStrings.IMAGE_MODERATION, isImageNotSafe);
                }
            }
            context.startActivity(intent);
        }

        @Override
        public void onReactionClick(Reaction reaction) {
            if (reaction.getName().equals("add_emoji")) {
                CometChatReactionDialog reactionDialog = new CometChatReactionDialog();
                reactionDialog.setOnEmojiClick(new OnReactionClickListener() {

                    @Override
                    public void onEmojiClicked(Reaction emojicon) {
                        sendReaction(emojicon);
                        reactionDialog.dismiss();
                    }
                });
                reactionDialog.show(getFragmentManager(), "ReactionDialog");
            } else {
                sendReaction(reaction);
            }
        }

        @Override
        public void onTranslateMessageClick() {
            try {
                String localeLanguage = Locale.getDefault().getLanguage();
                JSONObject body = new JSONObject();
                JSONArray languages = new JSONArray();
                languages.put(localeLanguage);
                if (isParent) {
                    body.put("msgId", parentId);
                    body.put("text", textMessage.getText().toString());
                } else {
                    body.put("msgId", baseMessage.getId());
                    body.put("text", ((TextMessage) baseMessage).getText());
                }
                body.put("languages", languages);
                CometChat.callExtension("message-translation", "POST", "/v2/translate", body, new CometChat.CallbackListener<JSONObject>() {

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        if (isParent) {
                            if (Extensions.isMessageTranslated(jsonObject, textMessage.getText().toString())) {
                                String translatedMessage = Extensions.getTextFromTranslatedMessage(jsonObject, textMessage.getText().toString());
                                textMessage.setText(translatedMessage);
                            } else {
                                CometChatSnackBar.show(context, rvChatListView, getString(R.string.no_translation_available), CometChatSnackBar.INFO);
                            }
                        } else {
                            if (Extensions.isMessageTranslated(jsonObject, ((TextMessage) baseMessage).getText())) {
                                if (baseMessage.getMetadata() != null) {
                                    JSONObject meta = baseMessage.getMetadata();
                                    try {
                                        meta.accumulate("values", jsonObject);
                                        baseMessage.setMetadata(meta);
                                        updateMessage(baseMessage);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    baseMessage.setMetadata(jsonObject);
                                    updateMessage(baseMessage);
                                }
                            } else {
                                CometChatSnackBar.show(context, rvChatListView, getString(R.string.no_translation_available), CometChatSnackBar.INFO);
                            }
                        }
                    }

                    @Override
                    public void onError(CometChatException e) {
                        CometChatSnackBar.show(context, rvChatListView, CometChatError.Extension.localized(e, "message-translation"), CometChatSnackBar.ERROR);
                    }
                });
            } catch (Exception e) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onRetryClick() {
        }
    });
}
Also used : ClipboardManager(android.content.ClipboardManager) OnReactionClickListener(com.cometchat.pro.uikit.ui_components.shared.cometchatReaction.listener.OnReactionClickListener) CometChatException(com.cometchat.pro.exceptions.CometChatException) User(com.cometchat.pro.models.User) MediaMessage(com.cometchat.pro.models.MediaMessage) CometChatMessageListActivity(com.cometchat.pro.uikit.ui_components.messages.message_list.CometChatMessageListActivity) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Intent(android.content.Intent) Reaction(com.cometchat.pro.uikit.ui_components.shared.cometchatReaction.model.Reaction) CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONException(org.json.JSONException) CometChatReactionDialog(com.cometchat.pro.uikit.ui_components.shared.cometchatReaction.CometChatReactionDialog) BaseMessage(com.cometchat.pro.models.BaseMessage) JSONObject(org.json.JSONObject) CometChatMessageInfoScreenActivity(com.cometchat.pro.uikit.ui_components.messages.message_information.CometChatMessageInfoScreenActivity) CometChatMessageActions(com.cometchat.pro.uikit.ui_components.messages.message_actions.CometChatMessageActions) ClipData(android.content.ClipData) TextMessage(com.cometchat.pro.models.TextMessage)

Example 13 with CometChatException

use of com.cometchat.pro.exceptions.CometChatException in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatThreadMessageList method sendMessage.

/**
 * This method is used to send Text Message to other users and groups.
 *
 * @param message is a String which is been sent as message.
 * @see TextMessage
 * @see CometChat#sendMessage(TextMessage, CometChat.CallbackListener)
 */
private void sendMessage(String message) {
    TextMessage textMessage;
    if (type.equalsIgnoreCase(CometChatConstants.RECEIVER_TYPE_USER))
        textMessage = new TextMessage(Id, message, CometChatConstants.RECEIVER_TYPE_USER);
    else
        textMessage = new TextMessage(Id, message, CometChatConstants.RECEIVER_TYPE_GROUP);
    textMessage.setParentMessageId(parentId);
    sendTypingIndicator(true);
    textMessage.setCategory(CometChatConstants.CATEGORY_MESSAGE);
    textMessage.setSender(loggedInUser);
    textMessage.setMuid(System.currentTimeMillis() + "");
    if (messageAdapter != null) {
        MediaUtils.playSendSound(context, R.raw.outgoing_message);
        messageAdapter.addMessage(textMessage);
        scrollToBottom();
    }
    noReplyMessages.setVisibility(GONE);
    isSmartReplyClicked = false;
    CometChat.sendMessage(textMessage, new CometChat.CallbackListener<TextMessage>() {

        @Override
        public void onSuccess(TextMessage textMessage) {
            if (messageAdapter != null) {
                setReply();
                messageAdapter.updateChangedMessage(textMessage);
            }
        }

        @Override
        public void onError(CometChatException e) {
            if (messageAdapter != null) {
                textMessage.setSentAt(-1);
                messageAdapter.updateChangedMessage(textMessage);
            }
            if (getActivity() != null) {
                CometChatSnackBar.show(context, rvChatListView, CometChatError.localized(e), CometChatSnackBar.ERROR);
            }
            Log.d(TAG, "onError: " + e.getMessage());
        }
    });
}
Also used : CometChatException(com.cometchat.pro.exceptions.CometChatException) CometChat(com.cometchat.pro.core.CometChat) TextMessage(com.cometchat.pro.models.TextMessage)

Example 14 with CometChatException

use of com.cometchat.pro.exceptions.CometChatException in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatThreadMessageList method sendReaction.

private void sendReaction(Reaction reaction) {
    JSONObject body = new JSONObject();
    try {
        body.put("msgId", baseMessage.getId());
        body.put("emoji", reaction.getName());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    CometChat.callExtension("reactions", "POST", "/v1/react", body, new CometChat.CallbackListener<JSONObject>() {

        @Override
        public void onSuccess(JSONObject responseObject) {
            Log.e(TAG, "onSuccess: " + responseObject.toString());
        // ReactionModel added successfully.
        }

        @Override
        public void onError(CometChatException e) {
            CometChatSnackBar.show(context, rvChatListView, CometChatError.Extension.localized(e, "reactions"), CometChatSnackBar.ERROR);
            Log.e(TAG, "onError: " + e.getCode() + e.getMessage() + e.getDetails());
        }
    });
}
Also used : CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONObject(org.json.JSONObject) CometChat(com.cometchat.pro.core.CometChat) JSONException(org.json.JSONException)

Example 15 with CometChatException

use of com.cometchat.pro.exceptions.CometChatException in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatThreadMessageList method sendCustomMessage.

private void sendCustomMessage(String customType, JSONObject customData) {
    CustomMessage customMessage;
    if (type.equalsIgnoreCase(CometChatConstants.RECEIVER_TYPE_USER))
        customMessage = new CustomMessage(Id, CometChatConstants.RECEIVER_TYPE_USER, customType, customData);
    else
        customMessage = new CustomMessage(Id, CometChatConstants.RECEIVER_TYPE_GROUP, customType, customData);
    customMessage.setParentMessageId(parentId);
    CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() {

        @Override
        public void onSuccess(CustomMessage customMessage) {
            noReplyMessages.setVisibility(GONE);
            if (messageAdapter != null) {
                messageAdapter.addMessage(customMessage);
                setReply();
                scrollToBottom();
            }
        }

        @Override
        public void onError(CometChatException e) {
            if (getActivity() != null) {
                CometChatSnackBar.show(context, rvChatListView, CometChatError.localized(e), CometChatSnackBar.ERROR);
            }
        }
    });
}
Also used : CometChatException(com.cometchat.pro.exceptions.CometChatException) CometChat(com.cometchat.pro.core.CometChat) CustomMessage(com.cometchat.pro.models.CustomMessage)

Aggregations

CometChatException (com.cometchat.pro.exceptions.CometChatException)54 CometChat (com.cometchat.pro.core.CometChat)45 JSONObject (org.json.JSONObject)20 JSONException (org.json.JSONException)19 Intent (android.content.Intent)14 ArrayList (java.util.ArrayList)13 ProgressDialog (android.app.ProgressDialog)12 User (com.cometchat.pro.models.User)10 View (android.view.View)9 TextView (android.widget.TextView)9 TextMessage (com.cometchat.pro.models.TextMessage)9 List (java.util.List)8 ImageView (android.widget.ImageView)7 RecyclerView (androidx.recyclerview.widget.RecyclerView)7 BaseMessage (com.cometchat.pro.models.BaseMessage)6 Call (com.cometchat.pro.core.Call)5 MediaMessage (com.cometchat.pro.models.MediaMessage)5 UsersRequest (com.cometchat.pro.core.UsersRequest)4 CometChatReactionDialog (com.cometchat.pro.uikit.ui_components.shared.cometchatReaction.CometChatReactionDialog)4 MaterialCardView (com.google.android.material.card.MaterialCardView)4