Search in sources :

Example 6 with CustomMessage

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

the class ThreadAdapter method setLocationData.

private void setLocationData(ThreadAdapter.LocationMessageViewHolder viewHolder, int i) {
    BaseMessage baseMessage = messageList.get(i);
    viewHolder.tvUser.setVisibility(View.VISIBLE);
    viewHolder.ivUser.setVisibility(View.VISIBLE);
    setAvatar(viewHolder.ivUser, baseMessage.getSender().getAvatar(), baseMessage.getSender().getName());
    viewHolder.tvUser.setText(baseMessage.getSender().getName());
    setLocationData(baseMessage, viewHolder.tvAddress, viewHolder.ivMap);
    viewHolder.senderTxt.setText(String.format(context.getString(R.string.shared_location), baseMessage.getSender().getName()));
    viewHolder.navigateBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                double latitude = ((CustomMessage) baseMessage).getCustomData().getDouble("latitude");
                double longitude = ((CustomMessage) baseMessage).getCustomData().getDouble("longitude");
                ;
                String label = Utils.getAddress(context, latitude, longitude);
                String uriBegin = "geo:" + latitude + "," + longitude;
                String query = label;
                String encodedQuery = Uri.encode(query);
                String uriString = uriBegin + "?q=" + encodedQuery + "&z=16";
                Uri uri = Uri.parse(uriString);
                Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);
                // mapIntent.setPackage("com.google.android.apps.maps");
                context.startActivity(mapIntent);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    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 -> {
        if (isLongClickEnabled && !isImageMessageClick) {
            setLongClickSelectedItem(baseMessage);
            messageLongClick.setLongMessageClick(longselectedItemList);
        } else {
            setSelectedMessage(baseMessage.getId());
        }
        notifyDataSetChanged();
    });
    viewHolder.rlMessageBubble.setOnLongClickListener(new View.OnLongClickListener() {

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

Example 7 with CustomMessage

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

the class MessageAdapter method setPollsData.

private void setPollsData(PollMessageViewHolder viewHolder, int i) {
    BaseMessage baseMessage = messageList.get(i);
    if (!baseMessage.getSender().getUid().equals(loggedInUser.getUid())) {
        if (baseMessage.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_USER)) {
            viewHolder.tvUser.setVisibility(View.GONE);
            viewHolder.ivUser.setVisibility(View.GONE);
        } else if (baseMessage.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_GROUP)) {
            if (isUserDetailVisible) {
                viewHolder.tvUser.setVisibility(View.VISIBLE);
                viewHolder.ivUser.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tvUser.setVisibility(View.GONE);
                viewHolder.ivUser.setVisibility(View.INVISIBLE);
            }
            setAvatar(viewHolder.ivUser, baseMessage.getSender().getAvatar(), baseMessage.getSender().getName());
            viewHolder.tvUser.setText(baseMessage.getSender().getName());
        }
    }
    FeatureRestriction.isThreadedMessagesEnabled(new FeatureRestriction.OnSuccessListener() {

        @Override
        public void onSuccess(Boolean booleanVal) {
            if (baseMessage.getReplyCount() != 0 && booleanVal) {
                viewHolder.tvThreadReplyCount.setVisibility(View.VISIBLE);
                viewHolder.tvThreadReplyCount.setText(baseMessage.getReplyCount() + " " + context.getResources().getString(R.string.replies));
            } else {
                viewHolder.tvThreadReplyCount.setVisibility(View.GONE);
            }
        }
    });
    viewHolder.tvThreadReplyCount.setOnClickListener(view -> {
        Intent intent = new Intent(context, CometChatThreadMessageListActivity.class);
        // intent.putExtra(StringContract.IntentStrings.PARENT_BASEMESSAGE,baseMessage.toString());
        intent.putExtra(UIKitConstants.IntentStrings.NAME, baseMessage.getSender().getName());
        intent.putExtra(UIKitConstants.IntentStrings.AVATAR, baseMessage.getSender().getAvatar());
        intent.putExtra(UIKitConstants.IntentStrings.REPLY_COUNT, baseMessage.getReplyCount());
        intent.putExtra(UIKitConstants.IntentStrings.UID, baseMessage.getSender().getName());
        intent.putExtra(UIKitConstants.IntentStrings.PARENT_ID, baseMessage.getId());
        intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, baseMessage.getType());
        intent.putExtra(UIKitConstants.IntentStrings.SENTAT, baseMessage.getSentAt());
        intent.putExtra(UIKitConstants.IntentStrings.REACTION_INFO, Extensions.getReactionsOnMessage(baseMessage));
        try {
            JSONObject option = ((CustomMessage) baseMessage).getCustomData().getJSONObject("options");
            intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.POLLS);
            intent.putExtra(UIKitConstants.IntentStrings.POLL_QUESTION, ((CustomMessage) baseMessage).getCustomData().getString("question"));
            intent.putExtra(UIKitConstants.IntentStrings.POLL_OPTION, option.toString());
            intent.putExtra(UIKitConstants.IntentStrings.POLL_VOTE_COUNT, Extensions.getVoteCount(baseMessage));
            intent.putExtra(UIKitConstants.IntentStrings.POLL_RESULT, Extensions.getVoterInfo(baseMessage, option.length()));
        } catch (Exception e) {
            Log.e(TAG, "startThreadActivityError: " + e.getMessage());
        }
        intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_CATEGORY, baseMessage.getCategory());
        intent.putExtra(UIKitConstants.IntentStrings.TYPE, baseMessage.getReceiverType());
        if (baseMessage.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_GROUP)) {
            intent.putExtra(UIKitConstants.IntentStrings.GUID, baseMessage.getReceiverUid());
        } else {
            if (baseMessage.getReceiverUid().equals(loggedInUser.getUid()))
                intent.putExtra(UIKitConstants.IntentStrings.UID, baseMessage.getSender().getUid());
            else
                intent.putExtra(UIKitConstants.IntentStrings.UID, baseMessage.getReceiverUid());
        }
        context.startActivity(intent);
    });
    viewHolder.optionGroup.removeAllViews();
    viewHolder.totalCount.setText("0" + context.getString(R.string.votes));
    ArrayList<String> optionList = new ArrayList<>();
    try {
        JSONObject jsonObject = ((CustomMessage) baseMessage).getCustomData();
        JSONObject options = jsonObject.getJSONObject("options");
        ArrayList<String> voterInfo = Extensions.getVoterInfo(baseMessage, options.length());
        viewHolder.tvQuestion.setText(jsonObject.getString("question"));
        for (int k = 0; k < options.length(); k++) {
            int voteCount = Extensions.getVoteCount(baseMessage);
            if (voteCount == 1) {
                viewHolder.totalCount.setText(voteCount + context.getString(R.string.vote));
            } else {
                viewHolder.totalCount.setText(voteCount + context.getString(R.string.votes));
            }
            LinearLayout linearLayout = new LinearLayout(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayout.setPadding(8, 8, 8, 8);
            linearLayout.setBackgroundColor(Color.parseColor(UIKitSettings.getColor()));
            linearLayout.setBackgroundTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.textColorWhite)));
            layoutParams.bottomMargin = (int) Utils.dpToPx(context, 8);
            linearLayout.setLayoutParams(layoutParams);
            TextView textViewPercentage = new TextView(context);
            TextView textViewOption = new TextView(context);
            textViewPercentage.setPadding(16, 4, 0, 4);
            textViewOption.setPadding(16, 4, 0, 4);
            textViewOption.setTextAppearance(context, R.style.TextAppearance_AppCompat_Medium);
            textViewPercentage.setTextAppearance(context, R.style.TextAppearance_AppCompat_Medium);
            textViewPercentage.setTextColor(context.getResources().getColor(R.color.primaryTextColor));
            textViewOption.setTextColor(context.getResources().getColor(R.color.primaryTextColor));
            String optionStr = options.getString(String.valueOf(k + 1));
            textViewOption.setText(optionStr);
            if (voteCount > 0) {
                int percentage = Math.round((Integer.parseInt(voterInfo.get(k)) * 100) / voteCount);
                if (percentage > 0)
                    textViewPercentage.setText(percentage + "% ");
            }
            if (k + 1 == Extensions.userVotedOn(baseMessage, optionList.size() + 1, loggedInUser.getUid())) {
                textViewPercentage.setCompoundDrawablePadding(8);
                textViewPercentage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_baseline_check_circle_24, 0, 0, 0);
            }
            int finalK = k;
            if (viewHolder.optionGroup.getChildCount() != options.length()) {
                viewHolder.loadingProgress.setVisibility(View.GONE);
                linearLayout.addView(textViewPercentage);
                linearLayout.addView(textViewOption);
                viewHolder.optionGroup.addView(linearLayout);
            }
            linearLayout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    try {
                        String pollsId = null;
                        if (jsonObject.has("id"))
                            pollsId = jsonObject.getString("id");
                        else
                            pollsId = baseMessage.getId() + "";
                        JSONObject pollsJsonObject = new JSONObject();
                        pollsJsonObject.put("vote", finalK + 1);
                        pollsJsonObject.put("id", pollsId);
                        CometChat.callExtension("polls", "POST", "/v2/vote", pollsJsonObject, new CometChat.CallbackListener<JSONObject>() {

                            @Override
                            public void onSuccess(JSONObject jsonObject) {
                                // Voted successfully
                                viewHolder.loadingProgress.setVisibility(View.VISIBLE);
                                viewHolder.totalCount.setText("0" + context.getString(R.string.votes));
                                Log.e(TAG, "onSuccess: " + jsonObject.toString());
                                Toast.makeText(context, context.getString(R.string.voted_success), Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onError(CometChatException e) {
                                // Some error occured
                                Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                                Log.e(TAG, "onErrorExtension: " + e.getMessage() + "\n" + e.getCode());
                            }
                        });
                    } catch (Exception e) {
                        Log.e(TAG, "onError: " + e.getMessage());
                    }
                }
            });
            optionList.add(options.getString(String.valueOf(k + 1)));
        }
    } catch (Exception e) {
        Log.e(TAG, "setPollsData: " + e.getMessage() + "\n" + viewHolder.totalCount.getText());
    }
    PatternUtils.setHyperLinkSupport(context, viewHolder.tvQuestion);
    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);
    setColorFilter(baseMessage, viewHolder.cvMessageView);
    viewHolder.rlMessageBubble.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {
            if (!isImageMessageClick && !isTextMessageClick) {
                isLongClickEnabled = true;
                isLocationMessageClick = true;
                setLongClickSelectedItem(baseMessage);
                messageLongClick.setLongMessageClick(longselectedItemList);
            }
            return true;
        }
    });
    viewHolder.reactionLayout.setVisibility(View.GONE);
    setReactionSupport(baseMessage, viewHolder.reactionLayout);
}
Also used : CometChatException(com.cometchat.pro.exceptions.CometChatException) ArrayList(java.util.ArrayList) Intent(android.content.Intent) 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) BaseMessage(com.cometchat.pro.models.BaseMessage) JSONObject(org.json.JSONObject) FeatureRestriction(com.cometchat.pro.uikit.ui_settings.FeatureRestriction) CustomMessage(com.cometchat.pro.models.CustomMessage) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout)

Example 8 with CustomMessage

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

the class MessageAdapter method setLocationData.

/**
 * This method is called whenever viewType of item is customMessage and customType is Location.
 * It is used to bind LocationMessageViewHolder
 * contents with CustomMessage at a given position.
 *
 * @param viewHolder is a object of LocationMessageViewHolder.
 * @param i is a position of item in recyclerView.
 * @see CustomMessage
 * @see BaseMessage
 */
private void setLocationData(LocationMessageViewHolder viewHolder, int i) {
    BaseMessage baseMessage = messageList.get(i);
    if (!baseMessage.getSender().getUid().equals(loggedInUser.getUid())) {
        if (baseMessage.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_USER)) {
            viewHolder.tvUser.setVisibility(View.GONE);
            viewHolder.ivUser.setVisibility(View.GONE);
        } else if (baseMessage.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_GROUP)) {
            if (isUserDetailVisible) {
                viewHolder.tvUser.setVisibility(View.VISIBLE);
                viewHolder.ivUser.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tvUser.setVisibility(View.GONE);
                viewHolder.ivUser.setVisibility(View.INVISIBLE);
            }
            setAvatar(viewHolder.ivUser, baseMessage.getSender().getAvatar(), baseMessage.getSender().getName());
            viewHolder.tvUser.setText(baseMessage.getSender().getName());
        }
    }
    FeatureRestriction.isThreadedMessagesEnabled(new FeatureRestriction.OnSuccessListener() {

        @Override
        public void onSuccess(Boolean booleanVal) {
            if (baseMessage.getReplyCount() != 0 && booleanVal) {
                viewHolder.tvThreadReplyCount.setVisibility(View.VISIBLE);
                viewHolder.tvThreadReplyCount.setText(baseMessage.getReplyCount() + " " + context.getResources().getString(R.string.replies));
            } else {
                viewHolder.tvThreadReplyCount.setVisibility(View.GONE);
            }
        }
    });
    viewHolder.tvThreadReplyCount.setOnClickListener(view -> {
        Intent intent = new Intent(context, CometChatThreadMessageListActivity.class);
        // intent.putExtra(StringContract.IntentStrings.PARENT_BASEMESSAGE,baseMessage.toString());
        intent.putExtra(UIKitConstants.IntentStrings.NAME, baseMessage.getSender().getName());
        intent.putExtra(UIKitConstants.IntentStrings.AVATAR, baseMessage.getSender().getAvatar());
        intent.putExtra(UIKitConstants.IntentStrings.REPLY_COUNT, baseMessage.getReplyCount());
        intent.putExtra(UIKitConstants.IntentStrings.UID, baseMessage.getSender().getName());
        intent.putExtra(UIKitConstants.IntentStrings.PARENT_ID, baseMessage.getId());
        intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, baseMessage.getType());
        intent.putExtra(UIKitConstants.IntentStrings.REACTION_INFO, Extensions.getReactionsOnMessage(baseMessage));
        intent.putExtra(UIKitConstants.IntentStrings.SENTAT, baseMessage.getSentAt());
        try {
            intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.LOCATION);
            intent.putExtra(UIKitConstants.IntentStrings.LOCATION_LATITUDE, ((CustomMessage) baseMessage).getCustomData().getDouble("latitude"));
            intent.putExtra(UIKitConstants.IntentStrings.LOCATION_LONGITUDE, ((CustomMessage) baseMessage).getCustomData().getDouble("longitude"));
        } catch (Exception e) {
            Log.e(TAG, "startThreadActivityError: " + e.getMessage());
        }
        intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_CATEGORY, baseMessage.getCategory());
        intent.putExtra(UIKitConstants.IntentStrings.TYPE, baseMessage.getReceiverType());
        if (baseMessage.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_GROUP)) {
            intent.putExtra(UIKitConstants.IntentStrings.GUID, baseMessage.getReceiverUid());
        } else {
            if (baseMessage.getReceiverUid().equals(loggedInUser.getUid()))
                intent.putExtra(UIKitConstants.IntentStrings.UID, baseMessage.getSender().getUid());
            else
                intent.putExtra(UIKitConstants.IntentStrings.UID, baseMessage.getReceiverUid());
        }
        context.startActivity(intent);
    });
    try {
        LATITUDE = ((CustomMessage) baseMessage).getCustomData().getDouble("latitude");
        LONGITUDE = ((CustomMessage) baseMessage).getCustomData().getDouble("longitude");
        viewHolder.tvAddress.setText(Utils.getAddress(context, LATITUDE, LONGITUDE));
        String mapUrl = UIKitConstants.MapUrl.MAPS_URL + LATITUDE + "," + LONGITUDE + "&key=" + UIKitConstants.MapUrl.MAP_ACCESS_KEY;
        Glide.with(context).load(mapUrl).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.default_map).into(viewHolder.ivMap);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    viewHolder.senderTxt.setText(String.format(context.getString(R.string.shared_location), baseMessage.getSender().getName()));
    viewHolder.ivMap.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                double latitude = ((CustomMessage) baseMessage).getCustomData().getDouble("latitude");
                double longitude = ((CustomMessage) baseMessage).getCustomData().getDouble("longitude");
                ;
                String label = Utils.getAddress(context, latitude, longitude);
                String uriBegin = "geo:" + latitude + "," + longitude;
                String query = label;
                String encodedQuery = Uri.encode(query);
                String uriString = uriBegin + "?q=" + encodedQuery + "&z=16";
                Uri uri = Uri.parse(uriString);
                Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);
                // mapIntent.setPackage("com.google.android.apps.maps");
                context.startActivity(mapIntent);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    showMessageTime(viewHolder, baseMessage);
    setColorFilter(baseMessage, viewHolder.cvMessageView);
    // 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 -> {
        if (isLongClickEnabled && !isImageMessageClick) {
            setLongClickSelectedItem(baseMessage);
            messageLongClick.setLongMessageClick(longselectedItemList);
        } else {
            setSelectedMessage(baseMessage.getId());
        }
        notifyItemChanged(i);
    });
    viewHolder.rlMessageBubble.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {
            if (!isImageMessageClick && !isTextMessageClick) {
                isLongClickEnabled = true;
                isLocationMessageClick = true;
                setLongClickSelectedItem(baseMessage);
                messageLongClick.setLongMessageClick(longselectedItemList);
                notifyItemChanged(i);
            }
            return true;
        }
    });
    viewHolder.reactionLayout.setVisibility(View.GONE);
    setReactionSupport(baseMessage, viewHolder.reactionLayout);
}
Also used : JSONException(org.json.JSONException) Intent(android.content.Intent) 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) Uri(android.net.Uri) CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONException(org.json.JSONException) BaseMessage(com.cometchat.pro.models.BaseMessage) FeatureRestriction(com.cometchat.pro.uikit.ui_settings.FeatureRestriction) CustomMessage(com.cometchat.pro.models.CustomMessage)

Example 9 with CustomMessage

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

the class CometChatMessageList method startThreadActivity.

private void startThreadActivity() {
    Intent intent = new Intent(getContext(), CometChatThreadMessageListActivity.class);
    intent.putExtra(UIKitConstants.IntentStrings.CONVERSATION_NAME, name);
    intent.putExtra(UIKitConstants.IntentStrings.NAME, baseMessage.getSender().getName());
    intent.putExtra(UIKitConstants.IntentStrings.UID, baseMessage.getSender().getName());
    intent.putExtra(UIKitConstants.IntentStrings.AVATAR, baseMessage.getSender().getAvatar());
    intent.putExtra(UIKitConstants.IntentStrings.PARENT_ID, baseMessage.getId());
    intent.putExtra(UIKitConstants.IntentStrings.REPLY_COUNT, baseMessage.getReplyCount());
    intent.putExtra(UIKitConstants.IntentStrings.SENTAT, baseMessage.getSentAt());
    intent.putExtra(UIKitConstants.IntentStrings.REACTION_INFO, Extensions.getReactionsOnMessage(baseMessage));
    if (baseMessage.getCategory().equalsIgnoreCase(CometChatConstants.CATEGORY_MESSAGE)) {
        intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, baseMessage.getType());
        if (baseMessage.getType().equals(CometChatConstants.MESSAGE_TYPE_TEXT))
            intent.putExtra(UIKitConstants.IntentStrings.TEXTMESSAGE, ((TextMessage) baseMessage).getText());
        else {
            intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_NAME, ((MediaMessage) baseMessage).getAttachment().getFileName());
            intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_EXTENSION, ((MediaMessage) baseMessage).getAttachment().getFileExtension());
            intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_URL, ((MediaMessage) baseMessage).getAttachment().getFileUrl());
            intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_SIZE, ((MediaMessage) baseMessage).getAttachment().getFileSize());
            intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_MIME_TYPE, ((MediaMessage) baseMessage).getAttachment().getFileMimeType());
        }
    } else {
        try {
            if (baseMessage.getType().equals(UIKitConstants.IntentStrings.LOCATION)) {
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.LOCATION);
                intent.putExtra(UIKitConstants.IntentStrings.LOCATION_LATITUDE, ((CustomMessage) baseMessage).getCustomData().getDouble("latitude"));
                intent.putExtra(UIKitConstants.IntentStrings.LOCATION_LONGITUDE, ((CustomMessage) baseMessage).getCustomData().getDouble("longitude"));
            } else if (baseMessage.getType().equals(UIKitConstants.IntentStrings.STICKERS)) {
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_NAME, ((CustomMessage) baseMessage).getCustomData().getString("name"));
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE_IMAGE_URL, ((CustomMessage) baseMessage).getCustomData().getString("url"));
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.STICKERS);
            } else if (baseMessage.getType().equals(UIKitConstants.IntentStrings.WHITEBOARD)) {
                intent.putExtra(UIKitConstants.IntentStrings.TEXTMESSAGE, Extensions.getWhiteBoardUrl(baseMessage));
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.WHITEBOARD);
            } else if (baseMessage.getType().equals(UIKitConstants.IntentStrings.WRITEBOARD)) {
                intent.putExtra(UIKitConstants.IntentStrings.TEXTMESSAGE, Extensions.getWriteBoardUrl(baseMessage));
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.WRITEBOARD);
            } else if (baseMessage.getType().equals(UIKitConstants.IntentStrings.POLLS)) {
                JSONObject options = ((CustomMessage) baseMessage).getCustomData().getJSONObject("options");
                intent.putExtra(UIKitConstants.IntentStrings.POLL_QUESTION, ((CustomMessage) baseMessage).getCustomData().getString("question"));
                intent.putExtra(UIKitConstants.IntentStrings.POLL_OPTION, options.toString());
                intent.putExtra(UIKitConstants.IntentStrings.POLL_VOTE_COUNT, Extensions.getVoteCount(baseMessage));
                intent.putExtra(UIKitConstants.IntentStrings.POLL_RESULT, Extensions.getVoterInfo(baseMessage, options.length()));
                intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_TYPE, UIKitConstants.IntentStrings.POLLS);
            }
        } catch (Exception e) {
            Log.e(TAG, "startThreadActivityError: " + e.getMessage());
        }
    }
    intent.putExtra(UIKitConstants.IntentStrings.MESSAGE_CATEGORY, baseMessage.getCategory());
    intent.putExtra(UIKitConstants.IntentStrings.TYPE, type);
    if (type.equals(CometChatConstants.CONVERSATION_TYPE_GROUP)) {
        intent.putExtra(UIKitConstants.IntentStrings.GUID, Id);
    } else {
        intent.putExtra(UIKitConstants.IntentStrings.UID, Id);
    }
    startActivity(intent);
}
Also used : MediaMessage(com.cometchat.pro.models.MediaMessage) JSONObject(org.json.JSONObject) CustomMessage(com.cometchat.pro.models.CustomMessage) Intent(android.content.Intent) TextMessage(com.cometchat.pro.models.TextMessage) CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONException(org.json.JSONException)

Example 10 with CustomMessage

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

the class CometChatMessageList method sendCustomMessage.

/**
 * This method sends custom message based on parameter received
 * @param customType
 * @param customData
 */
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);
    String pushNotificationMessage = "";
    if (customType.equalsIgnoreCase(UIKitConstants.IntentStrings.LOCATION))
        pushNotificationMessage = getString(R.string.shared_location);
    else if (customType.equalsIgnoreCase(UIKitConstants.IntentStrings.STICKERS))
        pushNotificationMessage = getString(R.string.shared_a_sticker);
    try {
        JSONObject jsonObject = customMessage.getMetadata();
        if (jsonObject == null) {
            jsonObject = new JSONObject();
            jsonObject.put("incrementUnreadCount", true);
            jsonObject.put("pushNotification", pushNotificationMessage);
        } else {
            jsonObject.accumulate("incrementUnreadCount", true);
        }
        customMessage.setMetadata(jsonObject);
    } catch (Exception e) {
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() {

        @Override
        public void onSuccess(CustomMessage customMessage) {
            Log.e(TAG, "onSuccessCustomMesage: " + customMessage.toString());
            if (customType.equalsIgnoreCase(UIKitConstants.IntentStrings.GROUP_CALL)) {
                rvSmartReply.setVisibility(GONE);
                if (CometChat.getActiveCall() == null)
                    CallUtils.startDirectCall(context, Utils.getDirectCallData(customMessage));
            }
            if (messageAdapter != null) {
                messageAdapter.addMessage(customMessage);
                scrollToBottom();
            }
        }

        @Override
        public void onError(CometChatException e) {
            if (getActivity() != null) {
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}
Also used : CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONObject(org.json.JSONObject) CometChat(com.cometchat.pro.core.CometChat) CustomMessage(com.cometchat.pro.models.CustomMessage) CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONException(org.json.JSONException)

Aggregations

CustomMessage (com.cometchat.pro.models.CustomMessage)12 Intent (android.content.Intent)8 CometChatException (com.cometchat.pro.exceptions.CometChatException)8 JSONException (org.json.JSONException)8 View (android.view.View)7 ImageView (android.widget.ImageView)7 TextView (android.widget.TextView)6 RecyclerView (androidx.recyclerview.widget.RecyclerView)6 JSONObject (org.json.JSONObject)6 CardView (androidx.cardview.widget.CardView)5 BaseMessage (com.cometchat.pro.models.BaseMessage)5 MaterialCardView (com.google.android.material.card.MaterialCardView)5 TextMessage (com.cometchat.pro.models.TextMessage)3 FeatureRestriction (com.cometchat.pro.uikit.ui_settings.FeatureRestriction)3 Uri (android.net.Uri)2 Call (com.cometchat.pro.core.Call)2 CometChat (com.cometchat.pro.core.CometChat)2 MediaMessage (com.cometchat.pro.models.MediaMessage)2 ArrayList (java.util.ArrayList)2 AlertDialog (android.app.AlertDialog)1