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 { ...@@ -394,14 +394,7 @@ public abstract class AbstractChat extends BaseEntity {
openChat(); openChat();
if (!incoming) if (!incoming)
notify = false; 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 MessageItem messageItem = new MessageItem(this, record ? null
: NO_RECORD_TAG, resource, text, action, timestamp, : NO_RECORD_TAG, resource, text, action, timestamp,
delayTimestamp, incoming, read, send, false, incoming, delayTimestamp, incoming, read, send, false, incoming,
...@@ -412,9 +405,17 @@ public abstract class AbstractChat extends BaseEntity { ...@@ -412,9 +405,17 @@ public abstract class AbstractChat extends BaseEntity {
if (save) if (save)
requestToWriteMessage(messageItem, resource, text, action, requestToWriteMessage(messageItem, resource, text, action,
timestamp, delayTimestamp, incoming, read, send); timestamp, delayTimestamp, incoming, read, send);
if (showTicker)
NotificationManager.getInstance().onMessageNotification( if (notify && notifyAboutMessage()) {
messageItem, notify); if (visible) {
if (ChatManager.getInstance().isNotifyVisible(account, user)) {
NotificationManager.getInstance().onCurrentChatMessageNotification(messageItem);
}
} else {
NotificationManager.getInstance().onMessageNotification(messageItem);
}
}
MessageManager.getInstance().onChatChanged(account, user, incoming); MessageManager.getInstance().onChatChanged(account, user, incoming);
return messageItem; return messageItem;
} }
......
...@@ -2,8 +2,6 @@ package com.xabber.android.data.notification; ...@@ -2,8 +2,6 @@ package com.xabber.android.data.notification;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Intent; import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
...@@ -11,12 +9,10 @@ import android.text.style.StyleSpan; ...@@ -11,12 +9,10 @@ import android.text.style.StyleSpan;
import com.xabber.android.R; import com.xabber.android.R;
import com.xabber.android.data.Application; 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.avatar.AvatarManager;
import com.xabber.android.data.extension.muc.MUCManager; import com.xabber.android.data.extension.muc.MUCManager;
import com.xabber.android.data.message.MessageItem; import com.xabber.android.data.message.MessageItem;
import com.xabber.android.data.message.chat.ChatManager; 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.data.roster.RosterManager;
import com.xabber.android.ui.ChatViewer; import com.xabber.android.ui.ChatViewer;
import com.xabber.android.ui.helper.AccountPainter; import com.xabber.android.ui.helper.AccountPainter;
...@@ -29,7 +25,6 @@ public class MessageNotificationCreator { ...@@ -29,7 +25,6 @@ public class MessageNotificationCreator {
private final Application application; private final Application application;
private final AccountPainter accountPainter; private final AccountPainter accountPainter;
private List<MessageNotification> messageNotifications; private List<MessageNotification> messageNotifications;
private NotificationCompat.Builder notificationBuilder;
public MessageNotificationCreator() { public MessageNotificationCreator() {
application = Application.getInstance(); application = Application.getInstance();
...@@ -56,7 +51,7 @@ public class MessageNotificationCreator { ...@@ -56,7 +51,7 @@ public class MessageNotificationCreator {
boolean showText = ChatManager.getInstance().isShowText(message.getAccount(), message.getUser()); 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.setContentTitle(getTitle(message, messageCount));
notificationBuilder.setContentText(getText(message, showText)); notificationBuilder.setContentText(getText(message, showText));
notificationBuilder.setSubText(message.getAccount()); notificationBuilder.setSubText(message.getAccount());
...@@ -75,7 +70,7 @@ public class MessageNotificationCreator { ...@@ -75,7 +70,7 @@ public class MessageNotificationCreator {
notificationBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE); notificationBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH); notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
addEffects(messageItem); NotificationManager.addEffects(notificationBuilder, messageItem);
return notificationBuilder.build(); return notificationBuilder.build();
} }
...@@ -205,18 +200,4 @@ public class MessageNotificationCreator { ...@@ -205,18 +200,4 @@ public class MessageNotificationCreator {
return PendingIntent.getActivity(application, 0, chatIntent, PendingIntent.FLAG_UPDATE_CURRENT); 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; ...@@ -19,6 +19,7 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.Vibrator; import android.os.Vibrator;
...@@ -40,6 +41,8 @@ import com.xabber.android.data.account.OnAccountRemovedListener; ...@@ -40,6 +41,8 @@ import com.xabber.android.data.account.OnAccountRemovedListener;
import com.xabber.android.data.connection.ConnectionState; import com.xabber.android.data.connection.ConnectionState;
import com.xabber.android.data.message.MessageItem; import com.xabber.android.data.message.MessageItem;
import com.xabber.android.data.message.MessageManager; 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.data.roster.RosterManager;
import com.xabber.android.ui.ClearNotifications; import com.xabber.android.ui.ClearNotifications;
import com.xabber.android.ui.ContactList; import com.xabber.android.ui.ContactList;
...@@ -64,6 +67,7 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -64,6 +67,7 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
public static final int PERSISTENT_NOTIFICATION_ID = 1; public static final int PERSISTENT_NOTIFICATION_ID = 1;
public static final int MESSAGE_NOTIFICATION_ID = 2; 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 int BASE_NOTIFICATION_PROVIDER_ID = 0x10;
private static final long VIBRATION_DURATION = 500; private static final long VIBRATION_DURATION = 500;
...@@ -153,6 +157,21 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -153,6 +157,21 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
return instance; 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() { private void initPersistentNotification() {
persistentNotificationBuilder.setContentTitle(application.getString(R.string.application_title_full)); persistentNotificationBuilder.setContentTitle(application.getString(R.string.application_title_full));
persistentNotificationBuilder.setDeleteIntent(clearNotifications); persistentNotificationBuilder.setDeleteIntent(clearNotifications);
...@@ -417,8 +436,6 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -417,8 +436,6 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
return connectionState; return connectionState;
} }
private void notify(int id, Notification notification) { private void notify(int id, Notification notification) {
LogManager.i(this, "Notification: " + id LogManager.i(this, "Notification: " + id
+ ", ticker: " + notification.tickerText + ", ticker: " + notification.tickerText
...@@ -441,14 +458,7 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -441,14 +458,7 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
return null; return null;
} }
/** public void onMessageNotification(MessageItem messageItem) {
* 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( MessageNotification messageNotification = getMessageNotification(
messageItem.getChat().getAccount(), messageItem.getChat().getUser()); messageItem.getChat().getAccount(), messageItem.getChat().getUser());
if (messageNotification == null) { if (messageNotification == null) {
...@@ -474,10 +484,16 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -474,10 +484,16 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
} }
}); });
} }
}
updateMessageNotification(messageItem); 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. * 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