Search in sources :

Example 41 with BaseMessage

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

the class ThreadAdapter method getItemViewTypes.

/**
 * This method is used to maintain different viewType based on message category and type and
 * returns the different view types to adapter based on it.
 *
 * Ex :- For message with category <b>CometChatConstants.CATEGORY_MESSAGE</b> and type
 * <b>CometChatConstants.MESSAGE_TYPE_TEXT</b> and message is sent by a <b>Logged-in user</b>,
 * It will return <b>RIGHT_TEXT_MESSAGE</b>
 *
 * @param position is a position of item in recyclerView.
 * @return It returns int which is value of view type of item.
 *
 * @see ThreadAdapter#onCreateViewHolder(ViewGroup, int)
 * @see BaseMessage
 */
private int getItemViewTypes(int position) {
    BaseMessage baseMessage = messageList.get(position);
    HashMap<String, JSONObject> extensionList = Extensions.extensionCheck(baseMessage);
    if (baseMessage.getDeletedAt() == 0) {
        if (baseMessage.getCategory().equals(CometChatConstants.CATEGORY_MESSAGE)) {
            switch(baseMessage.getType()) {
                case CometChatConstants.MESSAGE_TYPE_TEXT:
                    if (extensionList != null && extensionList.containsKey("linkPreview") && extensionList.get("linkPreview") != null)
                        return LINK_MESSAGE;
                    else if (baseMessage.getMetadata() != null && baseMessage.getMetadata().has("reply"))
                        return REPLY_TEXT_MESSAGE;
                    else
                        return TEXT_MESSAGE;
                case CometChatConstants.MESSAGE_TYPE_AUDIO:
                    return AUDIO_MESSAGE;
                case CometChatConstants.MESSAGE_TYPE_IMAGE:
                    return IMAGE_MESSAGE;
                case CometChatConstants.MESSAGE_TYPE_VIDEO:
                    return VIDEO_MESSAGE;
                case CometChatConstants.MESSAGE_TYPE_FILE:
                    return FILE_MESSAGE;
                default:
                    return -1;
            }
        } else {
            if (baseMessage.getCategory().equals(CometChatConstants.CATEGORY_CUSTOM)) {
                if (baseMessage.getType().equalsIgnoreCase("LOCATION"))
                    return LOCATION_CUSTOM_MESSAGE;
                else
                    return CUSTOM_MESSAGE;
            }
        }
    } else {
        return DELETE_MESSAGE;
    }
    return -1;
}
Also used : BaseMessage(com.cometchat.pro.models.BaseMessage) JSONObject(org.json.JSONObject)

Example 42 with BaseMessage

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

the class ThreadAdapter method setImageData.

/**
 * This method is called whenever viewType of item is media. It is used to bind ImageMessageViewHolder
 * contents with MediaMessage at a given position.
 * It loads image of MediaMessage using its url.
 *
 * @param viewHolder is a object of ImageMessageViewHolder.
 * @param i is a position of item in recyclerView.
 * @see MediaMessage
 * @see BaseMessage
 */
private void setImageData(ImageMessageViewHolder viewHolder, int i) {
    BaseMessage baseMessage = messageList.get(i);
    setAvatar(viewHolder.ivUser, baseMessage.getSender().getAvatar(), baseMessage.getSender().getName());
    viewHolder.tvUser.setText(baseMessage.getSender().getName());
    boolean isImageNotSafe = Extensions.getImageModeration(context, baseMessage);
    String smallUrl = Extensions.getThumbnailGeneration(context, baseMessage);
    viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_defaulf_image));
    Attachment attachment = ((MediaMessage) baseMessage).getAttachment();
    if (attachment != null) {
        if (smallUrl != null) {
            if (((MediaMessage) baseMessage).getAttachment().getFileExtension().equalsIgnoreCase(".gif")) {
                setImageDrawable(viewHolder, smallUrl, true, false);
            } else {
                setImageDrawable(viewHolder, smallUrl, false, isImageNotSafe);
            }
        } else {
            if (((MediaMessage) baseMessage).getAttachment().getFileExtension().equalsIgnoreCase(".gif"))
                setImageDrawable(viewHolder, ((MediaMessage) baseMessage).getAttachment().getFileUrl(), true, false);
            else
                setImageDrawable(viewHolder, ((MediaMessage) baseMessage).getAttachment().getFileUrl(), false, isImageNotSafe);
        }
    }
    if (baseMessage.getMetadata() != null) {
        try {
            String filePath = baseMessage.getMetadata().getString("path");
            Glide.with(context).load(filePath).diskCacheStrategy(DiskCacheStrategy.NONE).into(viewHolder.imageView);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    if (isImageNotSafe) {
        viewHolder.sensitiveLayout.setVisibility(View.VISIBLE);
    } else {
        viewHolder.sensitiveLayout.setVisibility(View.GONE);
    }
    showMessageTime(viewHolder, baseMessage);
    // if (selectedItemList.contains(baseMessage.getId()))
    // viewHolder.txtTime.setVisibility(View.VISIBLE);
    // else
    // viewHolder.txtTime.setVisibility(View.GONE);
    viewHolder.rlMessageBubble.setOnClickListener(view -> {
        if (isImageNotSafe) {
            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setTitle("Unsafe Content");
            alert.setIcon(R.drawable.ic_hand);
            alert.setMessage("Are you surely want to see this unsafe content");
            alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MediaUtils.openFile(((MediaMessage) baseMessage).getAttachment().getFileUrl(), context);
                }
            });
            alert.setNegativeButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            alert.create().show();
        } else {
            setSelectedMessage(baseMessage.getId());
            notifyDataSetChanged();
            openMediaViewActivity(baseMessage);
        }
    });
    viewHolder.rlMessageBubble.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            if (!isLongClickEnabled && !isTextMessageClick) {
                isImageMessageClick = true;
                setLongClickSelectedItem(baseMessage);
                messageLongClick.setLongMessageClick(longselectedItemList);
                notifyDataSetChanged();
            }
            return true;
        }
    });
    viewHolder.reactionLayout.setVisibility(View.GONE);
    setReactionSupport(baseMessage, viewHolder.reactionLayout);
}
Also used : AlertDialog(android.app.AlertDialog) MediaMessage(com.cometchat.pro.models.MediaMessage) DialogInterface(android.content.DialogInterface) JSONException(org.json.JSONException) Attachment(com.cometchat.pro.models.Attachment) 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) BaseMessage(com.cometchat.pro.models.BaseMessage)

Example 43 with BaseMessage

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

the class ThreadAdapter method setAudioData.

private void setAudioData(AudioMessageViewHolder viewHolder, int i) {
    BaseMessage baseMessage = messageList.get(i);
    if (baseMessage != null && baseMessage.getDeletedAt() == 0) {
        viewHolder.playBtn.setImageTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.textColorWhite)));
        setAvatar(viewHolder.ivUser, baseMessage.getSender().getAvatar(), baseMessage.getSender().getName());
        viewHolder.tvUser.setText(baseMessage.getSender().getName());
        showMessageTime(viewHolder, baseMessage);
        // if (selectedItemList.contains(baseMessage.getId()))
        // viewHolder.txtTime.setVisibility(View.VISIBLE);
        // else
        // viewHolder.txtTime.setVisibility(View.GONE);
        Attachment attachment = ((MediaMessage) baseMessage).getAttachment();
        if (attachment != null) {
            viewHolder.playBtn.setVisibility(View.VISIBLE);
            viewHolder.length.setText(Utils.getFileSize(((MediaMessage) baseMessage).getAttachment().getFileSize()));
        } else {
            viewHolder.length.setText("-");
            viewHolder.playBtn.setVisibility(View.GONE);
        }
        viewHolder.playBtn.setImageResource(R.drawable.ic_play_arrow_black_24dp);
        viewHolder.playBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // MediaUtils.openFile(((MediaMessage) baseMessage).getAttachment().getFileUrl(),context);
                mediaPlayer.reset();
                if (messagePosition != i) {
                    notifyItemChanged(messagePosition);
                    messagePosition = i;
                }
                try {
                    mediaPlayer.setDataSource(((MediaMessage) baseMessage).getAttachment().getFileUrl());
                    mediaPlayer.prepare();
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            viewHolder.playBtn.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                        }
                    });
                } catch (Exception e) {
                    Log.e(TAG, "MediaPlayerError: " + e.getMessage());
                }
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start();
                    viewHolder.playBtn.setImageResource(R.drawable.ic_pause_24dp);
                } else {
                    mediaPlayer.pause();
                    viewHolder.playBtn.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                }
            }
        });
        viewHolder.rlMessageBubble.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                if (!isLongClickEnabled && !isTextMessageClick) {
                    isImageMessageClick = true;
                    setLongClickSelectedItem(baseMessage);
                    messageLongClick.setLongMessageClick(longselectedItemList);
                    notifyDataSetChanged();
                }
                return true;
            }
        });
        viewHolder.reactionLayout.setVisibility(View.GONE);
        setReactionSupport(baseMessage, viewHolder.reactionLayout);
    }
}
Also used : MediaMessage(com.cometchat.pro.models.MediaMessage) BaseMessage(com.cometchat.pro.models.BaseMessage) Attachment(com.cometchat.pro.models.Attachment) 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) MediaPlayer(android.media.MediaPlayer)

Example 44 with BaseMessage

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

the class ThreadAdapter method setCustomData.

private void setCustomData(CustomMessageViewHolder viewHolder, int i) {
    BaseMessage baseMessage = messageList.get(i);
    if (baseMessage != null) {
        setAvatar(viewHolder.ivUser, baseMessage.getSender().getAvatar(), baseMessage.getSender().getName());
        viewHolder.tvUser.setText(baseMessage.getSender().getName());
        viewHolder.txtMessage.setText(context.getResources().getString(R.string.custom_message));
        viewHolder.txtMessage.setTypeface(fontUtils.getTypeFace(FontUtils.robotoLight));
        viewHolder.txtMessage.setTextColor(context.getResources().getColor(R.color.primaryTextColor));
        showMessageTime(viewHolder, baseMessage);
        if (messageList.get(messageList.size() - 1).equals(baseMessage)) {
            selectedItemList.add(baseMessage.getId());
        }
        if (selectedItemList.contains(baseMessage.getId()))
            viewHolder.txtTime.setVisibility(View.VISIBLE);
        else
            viewHolder.txtTime.setVisibility(View.GONE);
        viewHolder.rlMessageBubble.setOnClickListener(view -> {
            setSelectedMessage(baseMessage.getId());
            notifyDataSetChanged();
        });
        viewHolder.itemView.setTag(R.string.message, baseMessage);
    }
}
Also used : BaseMessage(com.cometchat.pro.models.BaseMessage)

Example 45 with BaseMessage

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

the class ThreadAdapter method setDeliveryReceipts.

/**
 * This method is used to set real time delivery receipt of particular message in messageList
 * of adapter by updating message.
 *
 * @param messageReceipt is a object of MessageReceipt.
 * @see MessageReceipt
 */
public void setDeliveryReceipts(MessageReceipt messageReceipt) {
    for (int i = messageList.size() - 1; i >= 0; i--) {
        BaseMessage baseMessage = messageList.get(i);
        if (baseMessage.getDeliveredAt() == 0) {
            int index = messageList.indexOf(baseMessage);
            messageList.get(index).setDeliveredAt(messageReceipt.getDeliveredAt());
        }
    }
    notifyDataSetChanged();
}
Also used : BaseMessage(com.cometchat.pro.models.BaseMessage)

Aggregations

BaseMessage (com.cometchat.pro.models.BaseMessage)53 View (android.view.View)19 ImageView (android.widget.ImageView)19 TextView (android.widget.TextView)19 CardView (androidx.cardview.widget.CardView)19 RecyclerView (androidx.recyclerview.widget.RecyclerView)19 MaterialCardView (com.google.android.material.card.MaterialCardView)19 Intent (android.content.Intent)18 MediaMessage (com.cometchat.pro.models.MediaMessage)14 JSONException (org.json.JSONException)14 CometChatException (com.cometchat.pro.exceptions.CometChatException)11 FeatureRestriction (com.cometchat.pro.uikit.ui_settings.FeatureRestriction)11 JSONObject (org.json.JSONObject)10 TextMessage (com.cometchat.pro.models.TextMessage)8 User (com.cometchat.pro.models.User)7 AlertDialog (android.app.AlertDialog)6 DialogInterface (android.content.DialogInterface)6 CustomMessage (com.cometchat.pro.models.CustomMessage)5 Call (com.cometchat.pro.core.Call)4 Attachment (com.cometchat.pro.models.Attachment)4