Search in sources :

Example 1 with Message

use of com.applozic.mobicomkit.api.conversation.Message in project Applozic-Android-SDK by AppLozic.

the class MainActivity method onNavigationDrawerItemSelected.

@Override
public void onNavigationDrawerItemSelected(int position) {
    if (position == 1) {
        Intent intent = new Intent(this, ConversationActivity.class);
        if (ApplozicClient.getInstance(this).isContextBasedChat()) {
            intent.putExtra(ConversationUIService.CONTEXT_BASED_CHAT, true);
        }
        startActivity(intent);
        return;
    }
    /*
        if (position == 1) {
            ConversationFragment conversationFragment = new ConversationFragment();
            Contact contact = new Contact(this, "mobicomkit");
            mTitle = getString(R.string.user_id);
            addFragment(this, conversationFragment, "conversationFragment");
            conversationFragment.loadConversation(contact);
            return;

        }*/
    if (position == 0) {
        mTitle = getString(R.string.ecommerce);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, EcommerceFragment.newInstance("", "")).commit();
        return;
    }
    if (position == 2) {
        UserLogoutTask.TaskListener userLogoutTaskListener = new UserLogoutTask.TaskListener() {

            @Override
            public void onSuccess(Context context) {
                userLogoutTask = null;
                Toast.makeText(getBaseContext(), getBaseContext().getString(R.string.log_out_successful), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(Exception exception) {
                userLogoutTask = null;
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle(getString(R.string.text_alert));
                alertDialog.setMessage(exception.toString());
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.ok_alert), new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                if (!isFinishing()) {
                    alertDialog.show();
                }
            }
        };
        userLogoutTask = new UserLogoutTask(userLogoutTaskListener, this);
        userLogoutTask.execute((Void) null);
    }
    if (position == 3) {
        Map<String, String> messageMetaData = new HashMap<>();
        messageMetaData.put(Message.MetaDataType.KEY.getValue(), Message.MetaDataType.HIDDEN.getValue());
        Message message = new Message();
        MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(MainActivity.this);
        message.setContactIds("android");
        message.setTo("android");
        message.setContentType(Message.ContentType.CUSTOM.getValue());
        message.setMessage("this is meta data hidden");
        message.setMetadata(messageMetaData);
        message.setStoreOnDevice(Boolean.TRUE);
        message.setRead(Boolean.TRUE);
        message.setCreatedAtTime(System.currentTimeMillis() + userPreferences.getDeviceTimeOffset());
        message.setSendToDevice(Boolean.FALSE);
        message.setType(Message.MessageType.MT_OUTBOX.getValue());
        message.setDeviceKeyString(userPreferences.getDeviceKeyString());
        message.setSource(Message.Source.MT_MOBILE_APP.getValue());
        new MobiComConversationService(MainActivity.this).sendMessage(message, MessageIntentService.class);
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1)).commit();
}
Also used : Context(android.content.Context) AlertDialog(android.support.v7.app.AlertDialog) Message(com.applozic.mobicomkit.api.conversation.Message) DialogInterface(android.content.DialogInterface) HashMap(java.util.HashMap) MobiComUserPreference(com.applozic.mobicomkit.api.account.user.MobiComUserPreference) Intent(android.content.Intent) MobiComConversationService(com.applozic.mobicomkit.api.conversation.MobiComConversationService) FragmentManager(android.support.v4.app.FragmentManager) UserLogoutTask(com.applozic.mobicomkit.api.account.user.UserLogoutTask)

Example 2 with Message

use of com.applozic.mobicomkit.api.conversation.Message in project Applozic-Android-SDK by AppLozic.

the class ApplozicMqttService method messageArrived.

@Override
public void messageArrived(String s, final MqttMessage mqttMessage) throws Exception {
    Utils.printLog(context, TAG, "Received MQTT message: " + new String(mqttMessage.getPayload()));
    try {
        if (!TextUtils.isEmpty(s) && s.startsWith(TYPINGTOPIC)) {
            String[] typingResponse = mqttMessage.toString().split(",");
            String applicationId = typingResponse[0];
            String userId = typingResponse[1];
            String isTypingStatus = typingResponse[2];
            BroadcastService.sendUpdateTypingBroadcast(context, BroadcastService.INTENT_ACTIONS.UPDATE_TYPING_STATUS.toString(), applicationId, userId, isTypingStatus);
        } else {
            final MqttMessageResponse mqttMessageResponse = (MqttMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), MqttMessageResponse.class);
            if (mqttMessageResponse != null) {
                if (MobiComPushReceiver.processPushNotificationId(mqttMessageResponse.getId())) {
                    return;
                }
                final SyncCallService syncCallService = SyncCallService.getInstance(context);
                MobiComPushReceiver.addPushNotificationId(mqttMessageResponse.getId());
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        Utils.printLog(context, TAG, "MQTT message type: " + mqttMessageResponse.getType());
                        if (NOTIFICATION_TYPE.MESSAGE_RECEIVED.getValue().equals(mqttMessageResponse.getType()) || "MESSAGE_RECEIVED".equals(mqttMessageResponse.getType())) {
                            GcmMessageResponse messageResponse = (GcmMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), GcmMessageResponse.class);
                            if (messageResponse == null) {
                                return;
                            }
                            Message message = messageResponse.getMessage();
                            if (message.getGroupId() != null) {
                                Channel channel = ChannelService.getInstance(context).getChannelByChannelKey(message.getGroupId());
                                if (channel != null && Channel.GroupType.OPEN.getValue().equals(channel.getType())) {
                                    if (!MobiComUserPreference.getInstance(context).getDeviceKeyString().equals(message.getDeviceKeyString())) {
                                        syncCallService.syncMessages(message.getKeyString(), message);
                                    }
                                } else {
                                    syncCallService.syncMessages(null);
                                }
                            } else {
                                syncCallService.syncMessages(null);
                            }
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_DELIVERED.getValue().equals(mqttMessageResponse.getType()) || "MT_MESSAGE_DELIVERED".equals(mqttMessageResponse.getType())) {
                            String[] splitKeyString = (mqttMessageResponse.getMessage()).toString().split(",");
                            String keyString = splitKeyString[0];
                            // String userId = splitKeyString[1];
                            syncCallService.updateDeliveryStatus(keyString);
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_DELIVERED_AND_READ.getValue().equals(mqttMessageResponse.getType()) || "MT_MESSAGE_DELIVERED_READ".equals(mqttMessageResponse.getType())) {
                            String[] splitKeyString = (mqttMessageResponse.getMessage()).toString().split(",");
                            String keyString = splitKeyString[0];
                            syncCallService.updateReadStatus(keyString);
                        }
                        if (NOTIFICATION_TYPE.CONVERSATION_DELIVERED_AND_READ.getValue().equals(mqttMessageResponse.getType())) {
                            String contactId = mqttMessageResponse.getMessage().toString();
                            syncCallService.updateDeliveryStatusForContact(contactId, true);
                        }
                        if (NOTIFICATION_TYPE.CONVERSATION_READ.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.updateConversationReadStatus(mqttMessageResponse.getMessage().toString(), false);
                        }
                        if (NOTIFICATION_TYPE.GROUP_CONVERSATION_READ.getValue().equals(mqttMessageResponse.getType())) {
                            InstantMessageResponse instantMessageResponse = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                            syncCallService.updateConversationReadStatus(instantMessageResponse.getMessage(), true);
                        }
                        if (NOTIFICATION_TYPE.USER_CONNECTED.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.updateConnectedStatus(mqttMessageResponse.getMessage().toString(), new Date(), true);
                        }
                        if (NOTIFICATION_TYPE.USER_DISCONNECTED.getValue().equals(mqttMessageResponse.getType())) {
                            // disconnect comes with timestamp, ranjeet,1449866097000
                            String[] parts = mqttMessageResponse.getMessage().toString().split(",");
                            String userId = parts[0];
                            Date lastSeenAt = new Date();
                            if (parts.length >= 2 && !parts[1].equals("null")) {
                                lastSeenAt = new Date(Long.valueOf(parts[1]));
                            }
                            syncCallService.updateConnectedStatus(userId, lastSeenAt, false);
                        }
                        if (NOTIFICATION_TYPE.CONVERSATION_DELETED.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.deleteConversationThread(mqttMessageResponse.getMessage().toString());
                            BroadcastService.sendConversationDeleteBroadcast(context, BroadcastService.INTENT_ACTIONS.DELETE_CONVERSATION.toString(), mqttMessageResponse.getMessage().toString(), 0, "success");
                        }
                        if (NOTIFICATION_TYPE.GROUP_CONVERSATION_DELETED.getValue().equals(mqttMessageResponse.getType())) {
                            InstantMessageResponse instantMessageResponse = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                            syncCallService.deleteChannelConversationThread(instantMessageResponse.getMessage());
                            BroadcastService.sendConversationDeleteBroadcast(context, BroadcastService.INTENT_ACTIONS.DELETE_CONVERSATION.toString(), null, Integer.valueOf(instantMessageResponse.getMessage()), "success");
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_DELETED.getValue().equals(mqttMessageResponse.getType())) {
                            String messageKey = mqttMessageResponse.getMessage().toString().split(",")[0];
                            syncCallService.deleteMessage(messageKey);
                            BroadcastService.sendMessageDeleteBroadcast(context, BroadcastService.INTENT_ACTIONS.DELETE_MESSAGE.toString(), messageKey, null);
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_SENT.getValue().equals(mqttMessageResponse.getType())) {
                            GcmMessageResponse messageResponse = (GcmMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), GcmMessageResponse.class);
                            Message sentMessageSync = messageResponse.getMessage();
                            syncCallService.syncMessages(sentMessageSync.getKeyString());
                        }
                        if (NOTIFICATION_TYPE.USER_BLOCKED.getValue().equals(mqttMessageResponse.getType()) || NOTIFICATION_TYPE.USER_UN_BLOCKED.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.syncBlockUsers();
                        }
                        if (NOTIFICATION_TYPE.USER_DETAIL_CHANGED.getValue().equals(mqttMessageResponse.getType()) || NOTIFICATION_TYPE.USER_DELETE_NOTIFICATION.getValue().equals(mqttMessageResponse.getType())) {
                            String userId = mqttMessageResponse.getMessage().toString();
                            syncCallService.syncUserDetail(userId);
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_METADATA_UPDATE.getValue().equals(mqttMessageResponse.getType())) {
                            String keyString = null;
                            String deviceKey = null;
                            try {
                                GcmMessageResponse messageResponse = (GcmMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), GcmMessageResponse.class);
                                keyString = messageResponse.getMessage().getKeyString();
                                deviceKey = messageResponse.getMessage().getDeviceKeyString();
                            } catch (Exception e) {
                                try {
                                    InstantMessageResponse response = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                                    keyString = response.getMessage();
                                    Message message = new MessageDatabaseService(context).getMessage(keyString);
                                    Utils.printLog(context, TAG, "Message from db : " + message);
                                    if (message != null) {
                                        deviceKey = message.getDeviceKeyString();
                                    }
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                }
                            }
                            if (deviceKey != null && deviceKey.equals(MobiComUserPreference.getInstance(context).getDeviceKeyString())) {
                                return;
                            }
                            syncCallService.syncMessageMetadataUpdate(keyString, false);
                        }
                        if (NOTIFICATION_TYPE.USER_MUTE_NOTIFICATION.getValue().equals(mqttMessageResponse.getType())) {
                            try {
                                InstantMessageResponse response = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                                if (response.getMessage() != null) {
                                    String muteFlag = String.valueOf(response.getMessage().charAt(response.getMessage().length() - 1));
                                    if ("1".equals(muteFlag)) {
                                        syncCallService.syncMutedUserList(false, null);
                                    } else if ("0".equals(muteFlag)) {
                                        String userId = response.getMessage().substring(0, response.getMessage().length() - 2);
                                        syncCallService.syncMutedUserList(false, userId);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                thread.start();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Message(com.applozic.mobicomkit.api.conversation.Message) MqttMessageResponse(com.applozic.mobicomkit.feed.MqttMessageResponse) GcmMessageResponse(com.applozic.mobicomkit.feed.GcmMessageResponse) Channel(com.applozic.mobicommons.people.channel.Channel) InstantMessageResponse(com.applozic.mobicomkit.feed.InstantMessageResponse) Date(java.util.Date) MqttException(org.eclipse.paho.client.mqttv3.MqttException) SyncCallService(com.applozic.mobicomkit.api.conversation.SyncCallService) MessageDatabaseService(com.applozic.mobicomkit.api.conversation.database.MessageDatabaseService)

Example 3 with Message

use of com.applozic.mobicomkit.api.conversation.Message in project Applozic-Android-SDK by AppLozic.

the class MessageDatabaseService method getMessages.

public List<Message> getMessages(Long createdAt, String searchText) {
    String createdAtClause = "";
    if (createdAt != null && createdAt > 0) {
        createdAtClause = " and m1.createdAt < " + createdAt;
    }
    createdAtClause += " and m1.deleted = 0 ";
    String messageTypeClause = "";
    String messageTypeJoinClause = "";
    String searchCaluse = "";
    MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(context);
    if (!userPreferences.isDisplayCallRecordEnable()) {
        messageTypeClause = " and m1.type != " + Message.MessageType.CALL_INCOMING.getValue() + " and m1.type != " + Message.MessageType.CALL_OUTGOING.getValue();
        messageTypeJoinClause = " and m1.type = m2.type";
    }
    if (!TextUtils.isEmpty(searchText)) {
        searchCaluse += " and m1.message like '%" + searchText.replaceAll("'", "''") + "%' ";
    }
    String hiddenType = " and m1.messageContentType not in (" + Message.ContentType.HIDDEN.getValue() + "," + Message.ContentType.VIDEO_CALL_NOTIFICATION_MSG.getValue() + ") AND m1.hidden = 0 AND m1.replyMessage not in (" + Message.ReplyMessage.HIDE_MESSAGE.getValue() + ")";
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    /*final Cursor cursor = db.rawQuery("select * from sms where createdAt in " +
                "(select max(createdAt) from sms group by contactNumbers) order by createdAt desc", null);*/
    final Cursor cursor = db.rawQuery("select m1.* from sms m1 left outer join sms m2 on (m1.createdAt < m2.createdAt" + " and m1.channelKey = m2.channelKey and m1.contactNumbers = m2.contactNumbers and m1.deleted = m2.deleted and  m1.messageContentType = m2.messageContentType" + messageTypeJoinClause + " ) where m2.createdAt is null " + createdAtClause + searchCaluse + hiddenType + messageTypeClause + " order by m1.createdAt desc", null);
    /*final Cursor cursor = db.rawQuery("SELECT t1.* FROM sms t1" +
                "  JOIN (SELECT contactNumbers, MAX(createdAt) createdAt FROM sms GROUP BY contactNumbers) t2" +
                "  ON t1.contactNumbers = t2.contactNumbers AND t1.createdAt = t2.createdAt order by createdAt desc", null);*/
    List<Message> messageList = getLatestMessageList(cursor);
    dbHelper.close();
    return messageList;
}
Also used : Message(com.applozic.mobicomkit.api.conversation.Message) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MobiComUserPreference(com.applozic.mobicomkit.api.account.user.MobiComUserPreference) Cursor(android.database.Cursor)

Example 4 with Message

use of com.applozic.mobicomkit.api.conversation.Message in project Applozic-Android-SDK by AppLozic.

the class MessageDatabaseService method getMessage.

public Message getMessage(String keyString) {
    if (TextUtils.isEmpty(keyString)) {
        return null;
    }
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String structuredNameWhere = "";
    List<String> structuredNameParamsList = new ArrayList<String>();
    structuredNameWhere += "keyString = ?";
    structuredNameParamsList.add(keyString);
    Cursor cursor = db.query("sms", null, structuredNameWhere, structuredNameParamsList.toArray(new String[structuredNameParamsList.size()]), null, null, null);
    Message message = null;
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        message = getMessage(cursor);
    }
    cursor.close();
    dbHelper.close();
    return message;
}
Also used : Message(com.applozic.mobicomkit.api.conversation.Message) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 5 with Message

use of com.applozic.mobicomkit.api.conversation.Message in project Applozic-Android-SDK by AppLozic.

the class MessageDatabaseService method getLatestMessage.

public List<Message> getLatestMessage(String contactNumbers) {
    List<Message> messages = new ArrayList<Message>();
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    Cursor cursor = db.rawQuery("select * from sms where contactNumbers = " + "'" + contactNumbers + "'" + " order by createdAt desc limit 1", null);
    if (cursor.moveToFirst()) {
        messages = MessageDatabaseService.getMessageList(cursor);
    }
    dbHelper.close();
    return messages;
}
Also used : Message(com.applozic.mobicomkit.api.conversation.Message) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Aggregations

Message (com.applozic.mobicomkit.api.conversation.Message)55 ArrayList (java.util.ArrayList)14 Intent (android.content.Intent)13 Contact (com.applozic.mobicommons.people.contact.Contact)11 Cursor (android.database.Cursor)8 MobiComUserPreference (com.applozic.mobicomkit.api.account.user.MobiComUserPreference)8 Uri (android.net.Uri)7 FileMeta (com.applozic.mobicomkit.api.attachment.FileMeta)7 File (java.io.File)7 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)6 Context (android.content.Context)5 View (android.view.View)5 MessageDatabaseService (com.applozic.mobicomkit.api.conversation.database.MessageDatabaseService)5 Channel (com.applozic.mobicommons.people.channel.Channel)5 Configuration (android.content.res.Configuration)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 Collections.disjoint (java.util.Collections.disjoint)4 Bitmap (android.graphics.Bitmap)3 RecyclerView (android.support.v7.widget.RecyclerView)3