use of com.applozic.mobicomkit.api.attachment.FileMeta in project Applozic-Android-SDK by AppLozic.
the class NotificationService method notifyUser.
public void notifyUser(Contact contact, Channel channel, Message message) {
if (ApplozicClient.getInstance(context).isNotificationDisabled()) {
Utils.printLog(context, TAG, "Notification is disabled !!");
return;
}
Bitmap notificationIconBitmap = null;
unReadMessageList = messageDatabaseService.getUnreadMessages();
int count = appContactService.getChatConversationCount() + appContactService.getGroupConversationCount();
int totalCount = messageDatabaseService.getTotalUnreadCount();
Class activity = null;
try {
activity = Class.forName(activityToOpen);
} catch (Exception e) {
e.printStackTrace();
}
if (message.getGroupId() != null) {
if (Channel.GroupType.GROUPOFTWO.getValue().equals(channel.getType())) {
String userId = ChannelService.getInstance(context).getGroupOfTwoReceiverUserId(channel.getKey());
if (!TextUtils.isEmpty(userId)) {
Contact newContact = appContactService.getContactById(userId);
notificationIconBitmap = appContactService.downloadContactImage(context, newContact);
}
} else {
notificationIconBitmap = appContactService.downloadGroupImage(context, channel);
}
} else {
notificationIconBitmap = appContactService.downloadContactImage(context, contact);
}
Integer smallIconResourceId = Utils.getMetaDataValueForResources(context, NOTIFICATION_SMALL_ICON_METADATA) != null ? Utils.getMetaDataValueForResources(context, NOTIFICATION_SMALL_ICON_METADATA) : iconResourceId;
Intent intent;
if (count < 2) {
intent = new Intent(context, activity);
intent.putExtra(MobiComKitConstants.MESSAGE_JSON_INTENT, GsonUtils.getJsonFromObject(message, Message.class));
} else {
intent = new Intent(context, activity);
intent.putExtra(MobiComKitConstants.QUICK_LIST, true);
}
if (applozicClient.isChatListOnNotificationIsHidden()) {
intent.putExtra("takeOrder", true);
}
if (applozicClient.isContextBasedChat()) {
intent.putExtra("contextBasedChat", true);
}
intent.putExtra("sms_body", "text");
intent.setType("vnd.android-dir/mms-sms");
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) (System.currentTimeMillis() & 0xfffffff), intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, MobiComKitConstants.AL_PUSH_NOTIFICATION).setSmallIcon(smallIconResourceId).setCategory(NotificationCompat.CATEGORY_MESSAGE).setPriority(NotificationCompat.PRIORITY_MAX).setWhen(System.currentTimeMillis());
mBuilder.setSound(TextUtils.isEmpty(MobiComUserPreference.getInstance(context).getNotificationSoundFilePath()) ? RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) : Uri.parse(MobiComUserPreference.getInstance(context).getNotificationSoundFilePath()));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
mBuilder.setGroup(GROUP_KEY);
mBuilder.setGroupSummary(true);
} else {
if (totalCount != 0) {
mBuilder.setNumber(totalCount);
}
}
mBuilder.setContentIntent(pendingIntent);
mBuilder.setAutoCancel(true);
if (ApplozicClient.getInstance(context).getVibrationOnNotification()) {
mBuilder.setVibrate(pattern);
}
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle(getNotificationTitle(count, contact, channel));
// Moves events into the expanded layout
try {
if (unReadMessageList != null) {
for (Message messageString : unReadMessageList) {
if (messageString.getGroupId() != null) {
Channel unreadChannel = ChannelService.getInstance(context).getChannelByChannelKey(messageString.getGroupId());
if (unreadChannel != null && unreadChannel.getUnreadCount() == 0) {
continue;
}
} else {
Contact unreadCount = appContactService.getContactById(messageString.getContactIds());
if (unreadCount != null && unreadCount.getUnreadCount() == 0) {
continue;
}
}
inboxStyle.addLine(getMessageBody(messageString, count, channel, contact));
}
}
// Moves the expanded layout object into the notification object.
} catch (Exception e) {
e.printStackTrace();
}
String summaryText = "";
if (count < 1) {
summaryText = "";
mBuilder.setLargeIcon(notificationIconBitmap != null ? notificationIconBitmap : BitmapFactory.decodeResource(context.getResources(), context.getResources().getIdentifier(message.getGroupId() != null ? applozicClient.getDefaultChannelImage() : applozicClient.getDefaultContactImage(), "drawable", context.getPackageName())));
mBuilder.setContentText(getMessageBody(message, count, channel, contact));
} else if (count >= 1 && count < 2) {
summaryText = totalCount < 2 ? totalCount + " new message " : totalCount + " new messages ";
mBuilder.setLargeIcon(notificationIconBitmap != null ? notificationIconBitmap : BitmapFactory.decodeResource(context.getResources(), context.getResources().getIdentifier(message.getGroupId() != null ? applozicClient.getDefaultChannelImage() : applozicClient.getDefaultContactImage(), "drawable", context.getPackageName())));
mBuilder.setContentText(summaryText);
} else {
summaryText = totalCount + " messages from " + count + " chats";
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), iconResourceId));
mBuilder.setContentText(summaryText);
}
inboxStyle.setSummaryText(summaryText);
mBuilder.setContentTitle(getNotificationTitle(count, contact, channel));
mBuilder.setStyle(inboxStyle);
if (message.hasAttachment()) {
try {
InputStream in;
FileMeta fileMeta = message.getFileMetas();
HttpURLConnection httpConn = null;
if (fileMeta.getThumbnailUrl() != null) {
httpConn = new MobiComKitClientService(context).openHttpConnection(fileMeta.getThumbnailUrl());
int response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
String imageName = FileUtils.getName(fileMeta.getName()) + message.getCreatedAtTime() + "." + FileUtils.getFileFormat(fileMeta.getName());
File file = FileClientService.getFilePath(imageName, context, "image", true);
ImageUtils.saveImageToInternalStorage(file, bitmap);
if (unReadMessageList != null && unReadMessageList.size() < 2) {
mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap));
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
WearableNotificationWithVoice notificationWithVoice = new WearableNotificationWithVoice(mBuilder, wearable_action_title, wearable_action_label, wearable_send_icon, NOTIFICATION_ID);
notificationWithVoice.setCurrentContext(context);
notificationWithVoice.setPendingIntent(pendingIntent);
try {
if (unReadMessageList != null && unReadMessageList.size() > 0) {
notificationWithVoice.sendNotification();
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.applozic.mobicomkit.api.attachment.FileMeta in project Applozic-Android-SDK by AppLozic.
the class MessageDatabaseService method updateMessageFileMetas.
public synchronized void updateMessageFileMetas(long messageId, final Message message) {
ContentValues values = new ContentValues();
values.put("keyString", message.getKeyString());
if (message.getFileMetaKeyStrings() != null) {
values.put("fileMetaKeyStrings", message.getFileMetaKeyStrings());
}
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("url", fileMeta.getUrl());
}
}
dbHelper.getWritableDatabase().update("sms", values, "id=" + messageId, null);
dbHelper.close();
}
use of com.applozic.mobicomkit.api.attachment.FileMeta in project Applozic-Android-SDK by AppLozic.
the class MessageDatabaseService method createSingleMessage.
public synchronized long createSingleMessage(final Message message) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
long id = -1;
boolean duplicateCheck = true;
SharedPreferences prefs = context.getSharedPreferences(MobiComKitClientService.getApplicationKey(context), Context.MODE_PRIVATE);
long minCreatedAt = prefs.getLong(MIN_CREATED_AT_KEY, 0);
long maxCreatedAt = prefs.getLong(MAX_CREATED_AT_KEY, Long.MAX_VALUE);
if (message.getCreatedAtTime() < minCreatedAt) {
duplicateCheck = false;
prefs.edit().putLong(MIN_CREATED_AT_KEY, message.getCreatedAtTime()).commit();
}
if (message.getCreatedAtTime() > maxCreatedAt) {
duplicateCheck = false;
prefs.edit().putLong(MAX_CREATED_AT_KEY, message.getCreatedAtTime()).commit();
}
if (duplicateCheck) {
Cursor cursor;
// Todo: add broadcastGroupId in the query if group is not null
/*if (message.isSentToServer() && !TextUtils.isEmpty(message.getMessageId())) {
} else */
/*if (message.isSentToServer() && !TextUtils.isEmpty(message.getKeyString())) {
cursor = database.rawQuery(
"SELECT COUNT(*) FROM sms WHERE keyString = ? and contactNumbers = ?",
new String[]{message.getKeyString(), message.getContactIds()});
} else {
cursor = database.rawQuery(
"SELECT COUNT(*) FROM sms WHERE sentToServer=0 and contactNumbers = ? and message = ? and createdAt between " + (message.getCreatedAtTime() - 120000) + " and " + (message.getCreatedAtTime() + 120000),
new String[]{message.getContactIds(), message.getMessage()});
}*/
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) {
cursor.close();
dbHelper.close();
return -1;
}
if (cursor != null) {
cursor.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.isHidden());
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) {
String thumbnailUrl = new FileClientService(context).getThumbnailUrl(fileMeta.getThumbnailUrl());
fileMeta.setThumbnailUrl(thumbnailUrl);
values.put("thumbnailUrl", thumbnailUrl);
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("url", fileMeta.getUrl());
}
}
id = database.insertOrThrow("sms", null, values);
} catch (SQLException ex) {
Utils.printLog(context, TAG, " Ignore Duplicate entry in sms table, sms: " + message);
} finally {
dbHelper.close();
}
return id;
}
use of com.applozic.mobicomkit.api.attachment.FileMeta in project Applozic-Android-SDK by AppLozic.
the class MessageDatabaseService method getMessage.
public static Message getMessage(Cursor cursor) {
Message message = new Message();
message.setMessageId(cursor.getLong(cursor.getColumnIndex("id")));
message.setKeyString(cursor.getString(cursor.getColumnIndex("keyString")));
message.setType(cursor.getShort(cursor.getColumnIndex("type")));
message.setSource(cursor.getShort(cursor.getColumnIndex("source")));
Long storeOnDevice = cursor.getLong(cursor.getColumnIndex("storeOnDevice"));
message.setStoreOnDevice(storeOnDevice != null && storeOnDevice.intValue() == 1);
String contactNumbers = cursor.getString(cursor.getColumnIndex("contactNumbers"));
message.setContactIds(contactNumbers);
message.setCreatedAtTime(cursor.getLong(cursor.getColumnIndex("createdAt")));
Long delivered = cursor.getLong(cursor.getColumnIndex("delivered"));
message.setDelivered(delivered != null && delivered.intValue() == 1);
Long canceled = cursor.getLong(cursor.getColumnIndex("canceled"));
message.setCanceled(canceled != null && canceled.intValue() == 1);
Long read = cursor.getLong(cursor.getColumnIndex("read"));
message.setRead(read != null && read.intValue() == 1);
message.setStatus(cursor.getShort(cursor.getColumnIndex(MobiComDatabaseHelper.STATUS)));
message.setClientGroupId(cursor.getString(cursor.getColumnIndex(MobiComDatabaseHelper.CLIENT_GROUP_ID)));
Long scheduledAt = cursor.getLong(cursor.getColumnIndex("scheduledAt"));
message.setScheduledAt(scheduledAt == null || scheduledAt.intValue() == 0 ? null : scheduledAt);
message.setMessage(cursor.getString(cursor.getColumnIndex("message")));
Long sentToServer = cursor.getLong(cursor.getColumnIndex("sentToServer"));
message.setSentToServer(sentToServer != null && sentToServer.intValue() == 1);
message.setTo(cursor.getString(cursor.getColumnIndex("toNumbers")));
int timeToLive = cursor.getInt(cursor.getColumnIndex("timeToLive"));
message.setReplyMessage(cursor.getInt(cursor.getColumnIndex("replyMessage")));
message.setTimeToLive(timeToLive != 0 ? timeToLive : null);
String fileMetaKeyStrings = cursor.getString(cursor.getColumnIndex("fileMetaKeyStrings"));
if (!TextUtils.isEmpty(fileMetaKeyStrings)) {
message.setFileMetaKeyStrings(fileMetaKeyStrings);
}
String filePaths = cursor.getString(cursor.getColumnIndex("filePaths"));
if (!TextUtils.isEmpty(filePaths)) {
message.setFilePaths(Arrays.asList(filePaths.split(",")));
}
message.setHidden(cursor.getInt(cursor.getColumnIndex(MobiComDatabaseHelper.HIDDEN)) == 1);
String metadata = cursor.getString(cursor.getColumnIndex(MobiComDatabaseHelper.MESSAGE_METADATA));
if (!TextUtils.isEmpty(metadata)) {
message.setMetadata(((Map<String, String>) GsonUtils.getObjectFromJson(metadata, Map.class)));
}
message.setApplicationId(cursor.getString(cursor.getColumnIndex("applicationId")));
message.setContentType(cursor.getShort(cursor.getColumnIndex(MobiComDatabaseHelper.MESSAGE_CONTENT_TYPE)));
int conversationId = cursor.getInt(cursor.getColumnIndex(MobiComDatabaseHelper.CONVERSATION_ID));
if (conversationId == 0) {
message.setConversationId(null);
} else {
message.setConversationId(conversationId);
}
message.setTopicId(cursor.getString(cursor.getColumnIndex(MobiComDatabaseHelper.TOPIC_ID)));
int channelKey = cursor.getInt(cursor.getColumnIndex(MobiComDatabaseHelper.CHANNEL_KEY));
if (channelKey == 0) {
message.setGroupId(null);
} else {
message.setGroupId(channelKey);
}
if (cursor.getString(cursor.getColumnIndex("blobKeyString")) == null) {
// file is not present... Don't set anything ...
} else {
FileMeta fileMeta = new FileMeta();
fileMeta.setKeyString(cursor.getString(cursor.getColumnIndex("metaFileKeyString")));
fileMeta.setBlobKeyString(cursor.getString(cursor.getColumnIndex("blobKeyString")));
fileMeta.setThumbnailUrl(cursor.getString(cursor.getColumnIndex("thumbnailUrl")));
fileMeta.setSize(cursor.getInt(cursor.getColumnIndex("size")));
fileMeta.setName(cursor.getString(cursor.getColumnIndex("name")));
fileMeta.setContentType(cursor.getString(cursor.getColumnIndex("contentType")));
fileMeta.setUrl(cursor.getString(cursor.getColumnIndex("url")));
message.setFileMetas(fileMeta);
}
return message;
}
use of com.applozic.mobicomkit.api.attachment.FileMeta in project Applozic-Android-SDK by AppLozic.
the class ConversationUIService method checkForStartNewConversation.
public void checkForStartNewConversation(Intent intent) {
Contact contact = null;
Channel channel = null;
Integer conversationId = null;
if (Intent.ACTION_SEND.equals(intent.getAction()) && intent.getType() != null) {
if ("text/plain".equals(intent.getType())) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
startContactActivityForResult(null, sharedText);
}
} else if (intent.getType().startsWith("image/")) {
// Todo: use this for image forwarding
}
}
final Uri uri = intent.getData();
if (uri != null) {
// Note: This is used only for the device contacts
Long contactId = intent.getLongExtra(CONTACT_ID, 0);
if (contactId == 0) {
// Todo: show warning that the user doesn't have any number stored.
return;
}
contact = baseContactService.getContactById(String.valueOf(contactId));
}
Integer channelKey = intent.getIntExtra(GROUP_ID, -1);
String clientGroupId = intent.getStringExtra(CLIENT_GROUP_ID);
String channelName = intent.getStringExtra(GROUP_NAME);
if (!TextUtils.isEmpty(clientGroupId)) {
channel = ChannelService.getInstance(fragmentActivity).getChannelByClientGroupId(clientGroupId);
if (channel == null) {
return;
}
} else if (channelKey != -1 && channelKey != null && channelKey != 0) {
channel = ChannelService.getInstance(fragmentActivity).getChannel(channelKey);
}
if (channel != null && !TextUtils.isEmpty(channelName) && TextUtils.isEmpty(channel.getName())) {
channel.setName(channelName);
ChannelService.getInstance(fragmentActivity).updateChannel(channel);
}
String contactNumber = intent.getStringExtra(CONTACT_NUMBER);
boolean firstTimeMTexterFriend = intent.getBooleanExtra(FIRST_TIME_MTEXTER_FRIEND, false);
if (!TextUtils.isEmpty(contactNumber)) {
contact = baseContactService.getContactById(contactNumber);
if (BroadcastService.isIndividual()) {
getConversationFragment().setFirstTimeMTexterFriend(firstTimeMTexterFriend);
}
}
String userId = intent.getStringExtra(USER_ID);
if (TextUtils.isEmpty(userId)) {
userId = intent.getStringExtra("contactId");
}
if (!TextUtils.isEmpty(userId)) {
contact = baseContactService.getContactById(userId);
}
String searchString = intent.getStringExtra(SEARCH_STRING);
String applicationId = intent.getStringExtra(APPLICATION_ID);
if (contact != null) {
contact.setApplicationId(applicationId);
baseContactService.upsert(contact);
}
String fullName = intent.getStringExtra(DISPLAY_NAME);
if (contact != null && TextUtils.isEmpty(contact.getFullName()) && !TextUtils.isEmpty(fullName)) {
contact.setFullName(fullName);
baseContactService.upsert(contact);
new UserClientService(fragmentActivity).updateUserDisplayName(userId, fullName);
}
String messageJson = intent.getStringExtra(MobiComKitConstants.MESSAGE_JSON_INTENT);
if (!TextUtils.isEmpty(messageJson)) {
Message message = (Message) GsonUtils.getObjectFromJson(messageJson, Message.class);
if (message.getGroupId() != null) {
channel = ChannelService.getInstance(fragmentActivity).getChannelByChannelKey(message.getGroupId());
} else {
contact = baseContactService.getContactById(message.getContactIds());
}
conversationId = message.getConversationId();
}
if (conversationId == null) {
conversationId = intent.getIntExtra(CONVERSATION_ID, 0);
}
if (conversationId != 0 && conversationId != null) {
getConversationFragment().setConversationId(conversationId);
} else {
conversationId = null;
}
boolean support = intent.getBooleanExtra(Support.SUPPORT_INTENT_KEY, false);
if (support) {
contact = new Support(fragmentActivity).getSupportContact();
}
String defaultText = intent.getStringExtra(ConversationUIService.DEFAULT_TEXT);
if (!TextUtils.isEmpty(defaultText)) {
getConversationFragment().setDefaultText(defaultText);
}
String forwardMessage = intent.getStringExtra(MobiComKitPeopleActivity.FORWARD_MESSAGE);
if (!TextUtils.isEmpty(forwardMessage)) {
Message messageToForward = (Message) GsonUtils.getObjectFromJson(forwardMessage, Message.class);
getConversationFragment().forwardMessage(messageToForward, contact, channel);
}
if (contact != null) {
openConversationFragment(contact, conversationId, searchString);
}
if (channel != null) {
openConversationFragment(channel, conversationId, searchString);
}
String productTopicId = intent.getStringExtra(ConversationUIService.PRODUCT_TOPIC_ID);
String productImageUrl = intent.getStringExtra(ConversationUIService.PRODUCT_IMAGE_URL);
if (!TextUtils.isEmpty(productTopicId) && !TextUtils.isEmpty(productImageUrl)) {
try {
FileMeta fileMeta = new FileMeta();
fileMeta.setContentType("image");
fileMeta.setBlobKeyString(productImageUrl);
getConversationFragment().sendProductMessage(productTopicId, fileMeta, contact, Message.ContentType.TEXT_URL.getValue());
} catch (Exception e) {
}
}
String sharedText = intent.getStringExtra(MobiComKitPeopleActivity.SHARED_TEXT);
if (!TextUtils.isEmpty(sharedText)) {
getConversationFragment().sendMessage(sharedText);
}
}
Aggregations