Search in sources :

Example 1 with ApplozicClient

use of com.applozic.mobicomkit.ApplozicClient in project Applozic-Android-SDK by AppLozic.

the class MessageDatabaseService method createSingleMessage.

/**
 * Adds the given message object to the database. If the message already exists (keystring), it is not replaced.
 */
public synchronized long createSingleMessage(final Message message) {
    SQLiteDatabase database = dbHelper.getWritableDatabase();
    ApplozicClient applozicClient = ApplozicClient.getInstance(context);
    long id = -1;
    boolean duplicateCheck = true;
    long minCreatedAt = applozicClient.getMinCreatedAtTime();
    long maxCreatedAt = applozicClient.getMaxCreatedAtTime();
    if (message.getCreatedAtTime() < minCreatedAt) {
        duplicateCheck = false;
        applozicClient.setMinCreatedAtTime(message.getCreatedAtTime());
    }
    if (message.getCreatedAtTime() > maxCreatedAt) {
        duplicateCheck = false;
        applozicClient.setMaxCreatedAtTime(message.getCreatedAtTime());
    }
    if (duplicateCheck) {
        Cursor cursor = null;
        try {
            String queryClause = "";
            if (message.getGroupId() != null) {
                queryClause = "channelKey ='" + String.valueOf(message.getGroupId()) + "'";
            } else {
                queryClause = "contactNumbers ='" + message.getContactIds() + "'";
            }
            if (message.isSentToServer() && !TextUtils.isEmpty(message.getKeyString())) {
                cursor = database.rawQuery("SELECT COUNT(*) FROM sms WHERE keyString = ? and " + queryClause, new String[] { message.getKeyString() });
            } else {
                cursor = database.rawQuery("SELECT COUNT(*) FROM sms WHERE sentToServer=0 and " + queryClause + " and message = ? and createdAt = " + message.getCreatedAtTime(), new String[] { message.getMessage() });
            }
            cursor.moveToFirst();
            if (cursor.getInt(0) > 0) {
                return -1;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            dbHelper.close();
        }
    }
    try {
        ContentValues values = new ContentValues();
        values.put("toNumbers", message.getTo());
        values.put("message", message.getMessage());
        values.put("createdAt", message.getCreatedAtTime());
        values.put("storeOnDevice", message.isStoreOnDevice());
        values.put("delivered", message.getDelivered());
        values.put("scheduledAt", message.getScheduledAt());
        values.put("type", message.getType());
        values.put("contactNumbers", message.getContactIds());
        values.put("sentToServer", message.isSentToServer());
        values.put("keyString", message.getKeyString());
        values.put("source", message.getSource());
        values.put("timeToLive", message.getTimeToLive());
        values.put("canceled", message.isCanceled());
        values.put("read", message.isRead() ? 1 : 0);
        values.put("applicationId", message.getApplicationId());
        values.put(MobiComDatabaseHelper.MESSAGE_CONTENT_TYPE, message.getContentType());
        values.put(MobiComDatabaseHelper.STATUS, message.getStatus());
        values.put(MobiComDatabaseHelper.CONVERSATION_ID, message.getConversationId());
        values.put(MobiComDatabaseHelper.TOPIC_ID, message.getTopicId());
        values.put(MobiComDatabaseHelper.HIDDEN, message.hasHideKey());
        boolean hidden = (hideActionMessages && message.isActionMessage()) || message.hasHideKey();
        values.put(MobiComDatabaseHelper.HIDDEN, hidden);
        if (message.getGroupId() != null) {
            values.put(MobiComDatabaseHelper.CHANNEL_KEY, message.getGroupId());
        }
        if (!TextUtils.isEmpty(message.getClientGroupId())) {
            values.put(MobiComDatabaseHelper.CLIENT_GROUP_ID, message.getClientGroupId());
        }
        if (message.getFileMetaKeyStrings() != null) {
            values.put("fileMetaKeyStrings", message.getFileMetaKeyStrings());
        }
        if (message.getFilePaths() != null && !message.getFilePaths().isEmpty()) {
            values.put("filePaths", TextUtils.join(",", message.getFilePaths()));
        }
        if (message.getMetadata() != null && !message.getMetadata().isEmpty()) {
            values.put(MobiComDatabaseHelper.MESSAGE_METADATA, GsonUtils.getJsonFromObject(message.getMetadata(), Map.class));
        }
        values.put(MobiComDatabaseHelper.REPLY_MESSAGE, message.isReplyMessage());
        // TODO:Right now we are supporting single image attachment...making entry in same table
        if (message.getFileMetas() != null) {
            FileMeta fileMeta = message.getFileMetas();
            if (fileMeta != null) {
                values.put("thumbnailUrl", fileMeta.getThumbnailUrl());
                values.put("size", fileMeta.getSize());
                values.put("name", fileMeta.getName());
                values.put("contentType", fileMeta.getContentType());
                values.put("metaFileKeyString", fileMeta.getKeyString());
                values.put("blobKeyString", fileMeta.getBlobKeyString());
                values.put("thumbnailBlobKey", fileMeta.getThumbnailBlobKey());
                values.put("url", fileMeta.getUrl());
            }
        }
        id = database.insertOrThrow("sms", null, values);
    } catch (Throwable ex) {
        ex.printStackTrace();
        Utils.printLog(context, TAG, " Ignore Duplicate entry in sms table, sms: " + message);
    } finally {
        dbHelper.close();
    }
    return id;
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ApplozicClient(com.applozic.mobicomkit.ApplozicClient) Cursor(android.database.Cursor) Map(java.util.Map) FileMeta(com.applozic.mobicomkit.api.attachment.FileMeta)

Example 2 with ApplozicClient

use of com.applozic.mobicomkit.ApplozicClient in project Applozic-Android-SDK by AppLozic.

the class MobiComConversationFragment method onCreateOptionsMenu.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    this.menu = menu;
    if (contact != null && contact.isDeleted()) {
        menu.findItem(R.id.dial).setVisible(false);
        menu.findItem(R.id.refresh).setVisible(false);
        menu.removeItem(R.id.conversations);
        menu.findItem(R.id.userBlock).setVisible(false);
        menu.findItem(R.id.userUnBlock).setVisible(false);
        menu.findItem(R.id.dial).setVisible(false);
        return;
    }
    String contactNumber = contact != null ? contact.getContactNumber() : null;
    ApplozicClient setting = ApplozicClient.getInstance(getActivity());
    if ((setting.isHandleDial() && !TextUtils.isEmpty(contactNumber) && contactNumber.length() > 2) || (setting.isIPCallEnabled())) {
        if (setting.isIPCallEnabled()) {
            menu.findItem(R.id.dial).setVisible(true);
            menu.findItem(R.id.video_call).setVisible(true);
        }
        if (setting.isHandleDial()) {
            menu.findItem(R.id.dial).setVisible(true);
        }
    } else {
        menu.findItem(R.id.video_call).setVisible(false);
        menu.findItem(R.id.dial).setVisible(false);
    }
    if (channel != null) {
        menu.findItem(R.id.dial).setVisible(false);
        menu.findItem(R.id.video_call).setVisible(false);
        if (Channel.GroupType.GROUPOFTWO.getValue().equals(channel.getType())) {
            String userId = ChannelService.getInstance(getActivity()).getGroupOfTwoReceiverUserId(channel.getKey());
            if (!TextUtils.isEmpty(userId) && alCustomizationSettings.isBlockOption()) {
                Contact withUserContact = appContactService.getContactById(userId);
                if (withUserContact.isBlocked()) {
                    menu.findItem(R.id.userUnBlock).setVisible(true);
                } else {
                    menu.findItem(R.id.userBlock).setVisible(true);
                }
            }
        } else {
            menu.findItem(R.id.userBlock).setVisible(false);
            menu.findItem(R.id.userUnBlock).setVisible(false);
            if (alCustomizationSettings.isMuteOption() && !Channel.GroupType.BROADCAST.getValue().equals(channel.getType())) {
                menu.findItem(R.id.unmuteGroup).setVisible(!Channel.GroupType.OPEN.getValue().equals(channel.getType()) && !channel.isDeleted() && channel.isNotificationMuted());
                menu.findItem(R.id.muteGroup).setVisible(!Channel.GroupType.OPEN.getValue().equals(channel.getType()) && !channel.isDeleted() && !channel.isNotificationMuted());
            }
        }
    } else if (contact != null) {
        if (alCustomizationSettings.isMuteUserChatOption()) {
            menu.findItem(R.id.unmuteGroup).setVisible(!contact.isDeleted() && contact.isNotificationMuted());
            menu.findItem(R.id.muteGroup).setVisible(!contact.isDeleted() && !contact.isNotificationMuted());
        }
        if (alCustomizationSettings.isBlockOption()) {
            if (contact.isBlocked()) {
                menu.findItem(R.id.userUnBlock).setVisible(true);
            } else {
                menu.findItem(R.id.userBlock).setVisible(true);
            }
        }
    }
    menu.removeItem(R.id.menu_search);
    menu.removeItem(R.id.start_new);
    if (channel != null && channel.isDeleted()) {
        menu.findItem(R.id.refresh).setVisible(false);
        menu.findItem(R.id.deleteConversation).setVisible(false);
    } else {
        menu.findItem(R.id.refresh).setVisible(alCustomizationSettings.isRefreshOption());
        if (channel != null && Channel.GroupType.OPEN.getValue().equals(channel.getType())) {
            menu.findItem(R.id.deleteConversation).setVisible(false);
        } else {
            menu.findItem(R.id.deleteConversation).setVisible(alCustomizationSettings.isDeleteOption() || ApplozicSetting.getInstance(getContext()).isDeleteConversationOption());
        }
    }
    menu.removeItem(R.id.conversations);
}
Also used : ApplozicClient(com.applozic.mobicomkit.ApplozicClient) Contact(com.applozic.mobicommons.people.contact.Contact)

Aggregations

ApplozicClient (com.applozic.mobicomkit.ApplozicClient)2 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 FileMeta (com.applozic.mobicomkit.api.attachment.FileMeta)1 Contact (com.applozic.mobicommons.people.contact.Contact)1 Map (java.util.Map)1