Commit 9eea63dd authored by Grigory Fedorov's avatar Grigory Fedorov

Current chat notifications fixed (sound and vibration effect for new message...

Current chat notifications fixed (sound and vibration effect for new message in  current chat if the option is turned on). #446
parent 5414d498
......@@ -394,14 +394,7 @@ public abstract class AbstractChat extends BaseEntity {
openChat();
if (!incoming)
notify = false;
if (notify && !notifyAboutMessage())
notify = false;
boolean showTicker = notify;
if (visible && showTicker) {
notify = false;
if (!ChatManager.getInstance().isNotifyVisible(account, user))
showTicker = false;
}
MessageItem messageItem = new MessageItem(this, record ? null
: NO_RECORD_TAG, resource, text, action, timestamp,
delayTimestamp, incoming, read, send, false, incoming,
......@@ -412,9 +405,17 @@ public abstract class AbstractChat extends BaseEntity {
if (save)
requestToWriteMessage(messageItem, resource, text, action,
timestamp, delayTimestamp, incoming, read, send);
if (showTicker)
NotificationManager.getInstance().onMessageNotification(
messageItem, notify);
if (notify && notifyAboutMessage()) {
if (visible) {
if (ChatManager.getInstance().isNotifyVisible(account, user)) {
NotificationManager.getInstance().onCurrentChatMessageNotification(messageItem);
}
} else {
NotificationManager.getInstance().onMessageNotification(messageItem);
}
}
MessageManager.getInstance().onChatChanged(account, user, incoming);
return messageItem;
}
......
......@@ -2,8 +2,6 @@ package com.xabber.android.data.notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.text.Spannable;
import android.text.SpannableString;
......@@ -11,12 +9,10 @@ import android.text.style.StyleSpan;
import com.xabber.android.R;
import com.xabber.android.data.Application;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.extension.avatar.AvatarManager;
import com.xabber.android.data.extension.muc.MUCManager;
import com.xabber.android.data.message.MessageItem;
import com.xabber.android.data.message.chat.ChatManager;
import com.xabber.android.data.message.phrase.PhraseManager;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.ChatViewer;
import com.xabber.android.ui.helper.AccountPainter;
......@@ -29,7 +25,6 @@ public class MessageNotificationCreator {
private final Application application;
private final AccountPainter accountPainter;
private List<MessageNotification> messageNotifications;
private NotificationCompat.Builder notificationBuilder;
public MessageNotificationCreator() {
application = Application.getInstance();
......@@ -56,7 +51,7 @@ public class MessageNotificationCreator {
boolean showText = ChatManager.getInstance().isShowText(message.getAccount(), message.getUser());
notificationBuilder = new NotificationCompat.Builder(application);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(application);
notificationBuilder.setContentTitle(getTitle(message, messageCount));
notificationBuilder.setContentText(getText(message, showText));
notificationBuilder.setSubText(message.getAccount());
......@@ -75,7 +70,7 @@ public class MessageNotificationCreator {
notificationBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
addEffects(messageItem);
NotificationManager.addEffects(notificationBuilder, messageItem);
return notificationBuilder.build();
}
......@@ -205,18 +200,4 @@ public class MessageNotificationCreator {
return PendingIntent.getActivity(application, 0, chatIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private void addEffects(MessageItem messageItem) {
if (messageItem == null) {
return;
}
if (messageItem.getChat().getFirstNotification() || !SettingsManager.eventsFirstOnly()) {
Uri sound = PhraseManager.getInstance().getSound(messageItem.getChat().getAccount(),
messageItem.getChat().getUser(), messageItem.getText());
boolean makeVibration = ChatManager.getInstance().isMakeVibro(messageItem.getChat().getAccount(),
messageItem.getChat().getUser());
NotificationManager.getInstance().setNotificationDefaults(notificationBuilder,
makeVibration, sound, AudioManager.STREAM_NOTIFICATION);
}
}
}
......@@ -19,6 +19,7 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Vibrator;
......@@ -40,6 +41,8 @@ import com.xabber.android.data.account.OnAccountRemovedListener;
import com.xabber.android.data.connection.ConnectionState;
import com.xabber.android.data.message.MessageItem;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.data.message.chat.ChatManager;
import com.xabber.android.data.message.phrase.PhraseManager;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.ClearNotifications;
import com.xabber.android.ui.ContactList;
......@@ -64,6 +67,7 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
public static final int PERSISTENT_NOTIFICATION_ID = 1;
public static final int MESSAGE_NOTIFICATION_ID = 2;
public static final int CURRENT_CHAT_MESSAGE_NOTIFICATION_ID = 3;
private static final int BASE_NOTIFICATION_PROVIDER_ID = 0x10;
private static final long VIBRATION_DURATION = 500;
......@@ -153,6 +157,21 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
return instance;
}
public static void addEffects(NotificationCompat.Builder notificationBuilder, MessageItem messageItem) {
if (messageItem == null) {
return;
}
if (messageItem.getChat().getFirstNotification() || !SettingsManager.eventsFirstOnly()) {
Uri sound = PhraseManager.getInstance().getSound(messageItem.getChat().getAccount(),
messageItem.getChat().getUser(), messageItem.getText());
boolean makeVibration = ChatManager.getInstance().isMakeVibro(messageItem.getChat().getAccount(),
messageItem.getChat().getUser());
NotificationManager.getInstance().setNotificationDefaults(notificationBuilder,
makeVibration, sound, AudioManager.STREAM_NOTIFICATION);
}
}
private void initPersistentNotification() {
persistentNotificationBuilder.setContentTitle(application.getString(R.string.application_title_full));
persistentNotificationBuilder.setDeleteIntent(clearNotifications);
......@@ -417,8 +436,6 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
return connectionState;
}
private void notify(int id, Notification notification) {
LogManager.i(this, "Notification: " + id
+ ", ticker: " + notification.tickerText
......@@ -441,43 +458,42 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
return null;
}
/**
* Shows ticker with the message and updates message notification.
*
* @param messageItem
* @param addNotification Whether notification should be stored.
*/
public void onMessageNotification(MessageItem messageItem, boolean addNotification) {
if (addNotification) {
MessageNotification messageNotification = getMessageNotification(
messageItem.getChat().getAccount(), messageItem.getChat().getUser());
if (messageNotification == null) {
messageNotification = new MessageNotification(
messageItem.getChat().getAccount(), messageItem.getChat().getUser(), null, null, 0);
} else {
messageNotifications.remove(messageNotification);
}
messageNotification.addMessage(messageItem.getText());
messageNotifications.add(messageNotification);
final String account = messageNotification.getAccount();
final String user = messageNotification.getUser();
final String text = messageNotification.getText();
final Date timestamp = messageNotification.getTimestamp();
final int count = messageNotification.getCount();
if (AccountManager.getInstance().getArchiveMode(account) != ArchiveMode.dontStore) {
Application.getInstance().runInBackground(new Runnable() {
@Override
public void run() {
NotificationTable.getInstance().write(account, user, text, timestamp, count);
}
});
}
public void onMessageNotification(MessageItem messageItem) {
MessageNotification messageNotification = getMessageNotification(
messageItem.getChat().getAccount(), messageItem.getChat().getUser());
if (messageNotification == null) {
messageNotification = new MessageNotification(
messageItem.getChat().getAccount(), messageItem.getChat().getUser(), null, null, 0);
} else {
messageNotifications.remove(messageNotification);
}
messageNotification.addMessage(messageItem.getText());
messageNotifications.add(messageNotification);
final String account = messageNotification.getAccount();
final String user = messageNotification.getUser();
final String text = messageNotification.getText();
final Date timestamp = messageNotification.getTimestamp();
final int count = messageNotification.getCount();
if (AccountManager.getInstance().getArchiveMode(account) != ArchiveMode.dontStore) {
Application.getInstance().runInBackground(new Runnable() {
@Override
public void run() {
NotificationTable.getInstance().write(account, user, text, timestamp, count);
}
});
}
updateMessageNotification(messageItem);
}
public void onCurrentChatMessageNotification(MessageItem messageItem) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(application);
addEffects(notificationBuilder, messageItem);
notify(CURRENT_CHAT_MESSAGE_NOTIFICATION_ID, notificationBuilder.build());
}
/**
* Updates message notification.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment