Commit e643f083 authored by Grigory Fedorov's avatar Grigory Fedorov

NotificationManager: light refactoring and code formatting.

parent 1c332697
...@@ -14,12 +14,6 @@ ...@@ -14,12 +14,6 @@
*/ */
package com.xabber.android.data.notification; package com.xabber.android.data.notification;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import android.app.Notification; import android.app.Notification;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
...@@ -27,7 +21,6 @@ import android.content.Intent; ...@@ -27,7 +21,6 @@ import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Vibrator; import android.os.Vibrator;
import android.widget.RemoteViews; import android.widget.RemoteViews;
...@@ -60,14 +53,20 @@ import com.xabber.android.utils.Emoticons; ...@@ -60,14 +53,20 @@ import com.xabber.android.utils.Emoticons;
import com.xabber.android.utils.StringUtils; import com.xabber.android.utils.StringUtils;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/** /**
* Manage notifications about message, subscription and authentication. * Manage notifications about message, subscription and authentication.
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public class NotificationManager implements OnInitializedListener, public class NotificationManager implements OnInitializedListener, OnAccountChangedListener,
OnAccountChangedListener, OnCloseListener, OnLoadListener, Runnable, OnCloseListener, OnLoadListener, Runnable, OnAccountRemovedListener,
OnAccountRemovedListener, OnAccountArchiveModeChangedListener { OnAccountArchiveModeChangedListener {
public static final int PERSISTENT_NOTIFICATION_ID = 1; public static final int PERSISTENT_NOTIFICATION_ID = 1;
private static final int CHAT_NOTIFICATION_ID = 2; private static final int CHAT_NOTIFICATION_ID = 2;
...@@ -85,12 +84,12 @@ public class NotificationManager implements OnInitializedListener, ...@@ -85,12 +84,12 @@ public class NotificationManager implements OnInitializedListener,
/** /**
* Runnable to start vibration. * Runnable to start vibration.
*/ */
private final Runnable startVibro; private final Runnable startVibration;
/** /**
* Runnable to force stop vibration. * Runnable to force stop vibration.
*/ */
private final Runnable stopVibro; private final Runnable stopVibration;
/** /**
* List of providers for notifications. * List of providers for notifications.
...@@ -115,42 +114,47 @@ public class NotificationManager implements OnInitializedListener, ...@@ -115,42 +114,47 @@ public class NotificationManager implements OnInitializedListener,
private NotificationManager() { private NotificationManager() {
this.application = Application.getInstance(); this.application = Application.getInstance();
notificationManager = (android.app.NotificationManager) application
.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager = (android.app.NotificationManager)
application.getSystemService(Context.NOTIFICATION_SERVICE);
persistentNotification = new Notification(); persistentNotification = new Notification();
handler = new Handler(); handler = new Handler();
providers = new ArrayList<NotificationProvider<? extends NotificationItem>>(); providers = new ArrayList<>();
messageNotifications = new ArrayList<MessageNotification>(); messageNotifications = new ArrayList<>();
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
clearNotifications = PendingIntent.getActivity(application, 0, clearNotifications = PendingIntent.getActivity(
ClearNotifications.createIntent(application), 0); application, 0, ClearNotifications.createIntent(application), 0);
stopVibro = new Runnable() {
stopVibration = new Runnable() {
@Override @Override
public void run() { public void run() {
handler.removeCallbacks(startVibro); handler.removeCallbacks(startVibration);
handler.removeCallbacks(stopVibro); handler.removeCallbacks(stopVibration);
((Vibrator) NotificationManager.this.application ((Vibrator) NotificationManager.this.application.
.getSystemService(Context.VIBRATOR_SERVICE)).cancel(); getSystemService(Context.VIBRATOR_SERVICE)).cancel();
} }
}; };
startVibro = new Runnable() {
startVibration = new Runnable() {
@Override @Override
public void run() { public void run() {
handler.removeCallbacks(startVibro); handler.removeCallbacks(startVibration);
handler.removeCallbacks(stopVibro); handler.removeCallbacks(stopVibration);
((Vibrator) NotificationManager.this.application ((Vibrator) NotificationManager.this.application
.getSystemService(Context.VIBRATOR_SERVICE)).cancel(); .getSystemService(Context.VIBRATOR_SERVICE)).cancel();
((Vibrator) NotificationManager.this.application ((Vibrator) NotificationManager.this.application
.getSystemService(Context.VIBRATOR_SERVICE)) .getSystemService(Context.VIBRATOR_SERVICE))
.vibrate(VIBRATION_DURATION); .vibrate(VIBRATION_DURATION);
handler.postDelayed(stopVibro, VIBRATION_DURATION); handler.postDelayed(stopVibration, VIBRATION_DURATION);
} }
}; };
} }
@Override @Override
public void onLoad() { public void onLoad() {
final Collection<MessageNotification> messageNotifications = new ArrayList<MessageNotification>(); final Collection<MessageNotification> messageNotifications = new ArrayList<>();
Cursor cursor = NotificationTable.getInstance().list(); Cursor cursor = NotificationTable.getInstance().list();
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
...@@ -176,10 +180,11 @@ public class NotificationManager implements OnInitializedListener, ...@@ -176,10 +180,11 @@ public class NotificationManager implements OnInitializedListener,
private void onLoaded(Collection<MessageNotification> messageNotifications) { private void onLoaded(Collection<MessageNotification> messageNotifications) {
this.messageNotifications.addAll(messageNotifications); this.messageNotifications.addAll(messageNotifications);
for (MessageNotification messageNotification : messageNotifications) for (MessageNotification messageNotification : messageNotifications) {
MessageManager.getInstance().openChat( MessageManager.getInstance().openChat(
messageNotification.getAccount(), messageNotification.getAccount(),
messageNotification.getUser()); messageNotification.getUser());
}
} }
@Override @Override
...@@ -208,40 +213,48 @@ public class NotificationManager implements OnInitializedListener, ...@@ -208,40 +213,48 @@ public class NotificationManager implements OnInitializedListener,
public <T extends NotificationItem> void updateNotifications( public <T extends NotificationItem> void updateNotifications(
NotificationProvider<T> provider, T notify) { NotificationProvider<T> provider, T notify) {
int id = providers.indexOf(provider); int id = providers.indexOf(provider);
if (id == -1)
if (id == -1) {
throw new IllegalStateException( throw new IllegalStateException(
"registerNotificationProvider() must be called from onLoaded() method."); "registerNotificationProvider() must be called from onLoaded() method.");
else }
id += BASE_NOTIFICATION_PROVIDER_ID;
Iterator<? extends NotificationItem> iterator = provider id += BASE_NOTIFICATION_PROVIDER_ID;
.getNotifications().iterator(); Iterator<? extends NotificationItem> iterator = provider.getNotifications().iterator();
if (!iterator.hasNext()) { if (!iterator.hasNext()) {
notificationManager.cancel(id); notificationManager.cancel(id);
return;
}
NotificationItem top;
String ticker;
if (notify == null) {
top = iterator.next();
ticker = null;
} else { } else {
NotificationItem top; top = notify;
String ticker; ticker = top.getTitle();
if (notify == null) {
top = iterator.next();
ticker = null;
} else {
top = notify;
ticker = top.getTitle();
}
Intent intent = top.getIntent();
Notification notification = new Notification(provider.getIcon(),
ticker, System.currentTimeMillis());
if (!provider.canClearNotifications())
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.setLatestEventInfo(application, top.getTitle(), top
.getText(), PendingIntent.getActivity(application, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT));
if (ticker != null)
setNotificationDefaults(notification,
SettingsManager.eventsVibro(), provider.getSound(),
provider.getStreamType());
notification.deleteIntent = clearNotifications;
notify(id, notification);
} }
Intent intent = top.getIntent();
Notification notification = new Notification(provider.getIcon(),
ticker, System.currentTimeMillis());
if (!provider.canClearNotifications()) {
notification.flags |= Notification.FLAG_NO_CLEAR;
}
notification.setLatestEventInfo(application, top.getTitle(), top.getText(),
PendingIntent.getActivity(application, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
if (ticker != null) {
setNotificationDefaults(notification,
SettingsManager.eventsVibro(), provider.getSound(), provider.getStreamType());
}
notification.deleteIntent = clearNotifications;
notify(id, notification);
} }
/** /**
...@@ -250,17 +263,20 @@ public class NotificationManager implements OnInitializedListener, ...@@ -250,17 +263,20 @@ public class NotificationManager implements OnInitializedListener,
* @param notification * @param notification
* @param streamType * @param streamType
*/ */
private void setNotificationDefaults(Notification notification, private void setNotificationDefaults(
boolean vibro, Uri sound, int streamType) { Notification notification, boolean vibration, Uri sound, int streamType) {
notification.audioStreamType = streamType; notification.audioStreamType = streamType;
notification.defaults = 0; notification.defaults = 0;
notification.sound = sound; notification.sound = sound;
if (vibro) {
if (SettingsManager.eventsIgnoreSystemVibro()) if (vibration) {
handler.post(startVibro); if (SettingsManager.eventsIgnoreSystemVibro()) {
else handler.post(startVibration);
} else {
notification.defaults |= Notification.DEFAULT_VIBRATE; notification.defaults |= Notification.DEFAULT_VIBRATE;
}
} }
if (SettingsManager.eventsLightning()) { if (SettingsManager.eventsLightning()) {
notification.defaults |= Notification.DEFAULT_LIGHTS; notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS; notification.flags |= Notification.FLAG_SHOW_LIGHTS;
...@@ -281,142 +297,78 @@ public class NotificationManager implements OnInitializedListener, ...@@ -281,142 +297,78 @@ public class NotificationManager implements OnInitializedListener,
* @return * @return
*/ */
private void updateMessageNotification(MessageItem ticker) { private void updateMessageNotification(MessageItem ticker) {
Collection<String> accountList = AccountManager.getInstance() Collection<String> accountList = AccountManager.getInstance().getAccounts();
.getAccounts();
int accountCount = accountList.size(); int accountCount = accountList.size();
boolean started = application.isInitialized(); boolean started = application.isInitialized();
int waiting = 0; int waiting = 0;
int connecting = 0; int connecting = 0;
int connected = 0; int connected = 0;
for (String account : accountList) { for (String account : accountList) {
ConnectionState state = AccountManager.getInstance() ConnectionState state = AccountManager.getInstance().getAccount(account).getState();
.getAccount(account).getState();
if (RosterManager.getInstance().isRosterReceived(account)) if (RosterManager.getInstance().isRosterReceived(account)) {
connected++; connected++;
else if (state == ConnectionState.connecting } else if (state == ConnectionState.connecting || state == ConnectionState.authentication) {
|| state == ConnectionState.authentication)
connecting++; connecting++;
else if (state == ConnectionState.waiting) } else if (state == ConnectionState.waiting) {
waiting++; waiting++;
}
} }
String accountQuantity; String accountQuantity;
String connectionState; String connectionState;
if (connected > 0) { if (connected > 0) {
accountQuantity = StringUtils.getQuantityString( accountQuantity = StringUtils.getQuantityString(
application.getResources(), R.array.account_quantity, application.getResources(), R.array.account_quantity, accountCount);
accountCount);
String connectionFormat = StringUtils.getQuantityString( String connectionFormat = StringUtils.getQuantityString(
application.getResources(), application.getResources(), R.array.connection_state_connected, connected);
R.array.connection_state_connected, connected);
connectionState = String.format(connectionFormat, connected, connectionState = String.format(connectionFormat, connected, accountCount, accountQuantity);
accountCount, accountQuantity);
} else if (connecting > 0) { } else if (connecting > 0) {
accountQuantity = StringUtils.getQuantityString( accountQuantity = StringUtils.getQuantityString(
application.getResources(), R.array.account_quantity, application.getResources(), R.array.account_quantity, accountCount);
accountCount);
String connectionFormat = StringUtils.getQuantityString( String connectionFormat = StringUtils.getQuantityString(
application.getResources(), application.getResources(), R.array.connection_state_connecting, connecting);
R.array.connection_state_connecting, connecting);
connectionState = String.format(connectionFormat, connecting, connectionState = String.format(connectionFormat, connecting, accountCount, accountQuantity);
accountCount, accountQuantity);
} else if (waiting > 0 && started) { } else if (waiting > 0 && started) {
accountQuantity = StringUtils.getQuantityString( accountQuantity = StringUtils.getQuantityString(
application.getResources(), R.array.account_quantity, application.getResources(), R.array.account_quantity, accountCount);
accountCount);
String connectionFormat = StringUtils.getQuantityString( String connectionFormat = StringUtils.getQuantityString(
application.getResources(), application.getResources(), R.array.connection_state_waiting, waiting);
R.array.connection_state_waiting, waiting);
connectionState = String.format(connectionFormat, waiting, connectionState = String.format(connectionFormat, waiting, accountCount, accountQuantity);
accountCount, accountQuantity);
} else { } else {
accountQuantity = StringUtils.getQuantityString( accountQuantity = StringUtils.getQuantityString(
application.getResources(), application.getResources(), R.array.account_quantity_offline, accountCount);
R.array.account_quantity_offline, accountCount);
connectionState = application.getString( connectionState = application.getString(
R.string.connection_state_offline, accountCount, R.string.connection_state_offline, accountCount, accountQuantity);
accountQuantity);
} }
final Intent persistentIntent; final Intent persistentIntent;
if (waiting > 0 && started)
if (waiting > 0 && started) {
persistentIntent = ReconnectionActivity.createIntent(application); persistentIntent = ReconnectionActivity.createIntent(application);
else } else {
persistentIntent = ContactList.createPersistentIntent(application); persistentIntent = ContactList.createPersistentIntent(application);
}
if (messageNotifications.isEmpty()) { if (messageNotifications.isEmpty()) {
notificationManager.cancel(CHAT_NOTIFICATION_ID); notificationManager.cancel(CHAT_NOTIFICATION_ID);
} else { } else {
int messageCount = 0; notifyMessageNotification(ticker, connected);
for (MessageNotification messageNotification : messageNotifications)
messageCount += messageNotification.getCount();
MessageNotification message = messageNotifications
.get(messageNotifications.size() - 1);
RemoteViews chatViews = new RemoteViews(
application.getPackageName(), R.layout.chat_notification);
Intent chatIntent = ChatViewer.createClearTopIntent(application,
message.getAccount(), message.getUser());
if (MUCManager.getInstance().hasRoom(message.getAccount(),
message.getUser()))
chatViews.setImageViewBitmap(R.id.icon, AvatarManager
.getInstance().getRoomBitmap(message.getUser()));
else
chatViews.setImageViewBitmap(R.id.icon, AvatarManager
.getInstance().getUserBitmap(message.getUser()));
chatViews.setTextViewText(R.id.title, RosterManager.getInstance()
.getName(message.getAccount(), message.getUser()));
String text;
if (ChatManager.getInstance().isShowText(message.getAccount(),
message.getUser()))
text = trimText(message.getText());
else
text = "";
chatViews.setTextViewText(R.id.text2,
Emoticons.getSmiledText(application, text));
chatViews.setTextViewText(R.id.time,
StringUtils.getSmartTimeText(message.getTimestamp()));
String messageText = StringUtils.getQuantityString(
application.getResources(), R.array.chat_message_quantity,
messageCount);
String contactText = StringUtils.getQuantityString(
application.getResources(), R.array.chat_contact_quantity,
messageNotifications.size());
String status = application.getString(R.string.chat_status,
messageCount, messageText, messageNotifications.size(),
contactText);
chatViews.setTextViewText(R.id.text, status);
Notification notification = new Notification();
if (Build.VERSION.SDK_INT >= 14
&& SettingsManager.eventsPersistent()) {
// Ongoing icons are in the left side, so hide this one.
notification.icon = R.drawable.ic_placeholder;
notification.when = Long.MIN_VALUE;
} else {
// Ongoing icons are in the right side, so show this one.
updateNotification(notification, ticker);
notification.icon = connected > 0 ? R.drawable.ic_stat_message
: R.drawable.ic_stat_message_offline;
notification.when = System.currentTimeMillis();
}
notification.contentView = chatViews;
notification.contentIntent = PendingIntent.getActivity(application,
0, chatIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.deleteIntent = clearNotifications;
try {
notify(CHAT_NOTIFICATION_ID, notification);
} catch (RuntimeException e) {
LogManager.exception(this, e);
// Try to remove avatar in order to avoid
// OutOfMemoryError on the Android system side.
chatViews.setImageViewResource(R.id.icon,
R.drawable.ic_placeholder);
notify(CHAT_NOTIFICATION_ID, notification);
}
} }
persistentNotification.icon = R.drawable.ic_stat_normal; persistentNotification.icon = R.drawable.ic_stat_normal;
...@@ -429,7 +381,7 @@ public class NotificationManager implements OnInitializedListener, ...@@ -429,7 +381,7 @@ public class NotificationManager implements OnInitializedListener,
persistentNotification.defaults = 0; persistentNotification.defaults = 0;
persistentNotification.sound = null; persistentNotification.sound = null;
persistentNotification.tickerText = null; persistentNotification.tickerText = null;
if (Build.VERSION.SDK_INT >= 14 && SettingsManager.eventsPersistent()) { if (SettingsManager.eventsPersistent()) {
// Ongoing icons are in the left side, so always use it. // Ongoing icons are in the left side, so always use it.
persistentNotification.when = startTime; persistentNotification.when = startTime;
if (messageNotifications.isEmpty()) { if (messageNotifications.isEmpty()) {
...@@ -450,8 +402,7 @@ public class NotificationManager implements OnInitializedListener, ...@@ -450,8 +402,7 @@ public class NotificationManager implements OnInitializedListener,
updateNotification(persistentNotification, ticker); updateNotification(persistentNotification, ticker);
} else { } else {
persistentNotification.icon = R.drawable.ic_placeholder; persistentNotification.icon = R.drawable.ic_placeholder;
persistentNotification.when = Build.VERSION.SDK_INT >= 9 ? -Long.MAX_VALUE persistentNotification.when = -Long.MAX_VALUE;
: Long.MAX_VALUE;
} }
} }
...@@ -462,6 +413,82 @@ public class NotificationManager implements OnInitializedListener, ...@@ -462,6 +413,82 @@ public class NotificationManager implements OnInitializedListener,
} }
} }
private void notifyMessageNotification(MessageItem ticker, int connected) {
int messageCount = 0;
for (MessageNotification messageNotification : messageNotifications) {
messageCount += messageNotification.getCount();
}
MessageNotification message = messageNotifications.get(messageNotifications.size() - 1);
RemoteViews chatViews
= new RemoteViews(application.getPackageName(), R.layout.chat_notification);
Intent chatIntent
= ChatViewer.createClearTopIntent(application, message.getAccount(), message.getUser());
if (MUCManager.getInstance().hasRoom(message.getAccount(), message.getUser())) {
chatViews.setImageViewBitmap(
R.id.icon, AvatarManager.getInstance().getRoomBitmap(message.getUser()));
} else {
chatViews.setImageViewBitmap(
R.id.icon, AvatarManager.getInstance().getUserBitmap(message.getUser()));
}
chatViews.setTextViewText(R.id.title,
RosterManager.getInstance().getName(message.getAccount(), message.getUser()));
String text;
if (ChatManager.getInstance().isShowText(message.getAccount(), message.getUser())) {
text = trimText(message.getText());
} else {
text = "";
}
chatViews.setTextViewText(R.id.text2, Emoticons.getSmiledText(application, text));
chatViews.setTextViewText(R.id.time, StringUtils.getSmartTimeText(message.getTimestamp()));
String messageText = StringUtils.getQuantityString(
application.getResources(), R.array.chat_message_quantity, messageCount);
String contactText = StringUtils.getQuantityString(application.getResources(),
R.array.chat_contact_quantity, messageNotifications.size());
String status = application.getString(R.string.chat_status,
messageCount, messageText, messageNotifications.size(), contactText);
chatViews.setTextViewText(R.id.text, status);
Notification notification = new Notification();
if (SettingsManager.eventsPersistent()) {
// Ongoing icons are in the left side, so hide this one.
notification.icon = R.drawable.ic_placeholder;
notification.when = Long.MIN_VALUE;
} else {
// Ongoing icons are in the right side, so show this one.
updateNotification(notification, ticker);
notification.icon
= connected > 0 ? R.drawable.ic_stat_message : R.drawable.ic_stat_message_offline;
notification.when = System.currentTimeMillis();
}
notification.contentView = chatViews;
notification.contentIntent = PendingIntent.getActivity(
application, 0, chatIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.deleteIntent = clearNotifications;
try {
notify(CHAT_NOTIFICATION_ID, notification);
} catch (RuntimeException e) {
LogManager.exception(this, e);
// Try to remove avatar in order to avoid
// OutOfMemoryError on the Android system side.
chatViews.setImageViewResource(R.id.icon, R.drawable.ic_placeholder);
notify(CHAT_NOTIFICATION_ID, notification);
}
}
private void notify(int id, Notification notification) { private void notify(int id, Notification notification) {
LogManager.i(this, "Notification: " + id + ", ticker: " LogManager.i(this, "Notification: " + id + ", ticker: "
+ notification.tickerText + ", sound: " + notification.sound + notification.tickerText + ", sound: " + notification.sound
...@@ -483,29 +510,28 @@ public class NotificationManager implements OnInitializedListener, ...@@ -483,29 +510,28 @@ public class NotificationManager implements OnInitializedListener,
*/ */
private void updateNotification(Notification notification, private void updateNotification(Notification notification,
MessageItem ticker) { MessageItem ticker) {
if (ticker == null) if (ticker == null) {
return; return;
if (ticker.getChat().getFirstNotification() }
|| !SettingsManager.eventsFirstOnly()) if (ticker.getChat().getFirstNotification() || !SettingsManager.eventsFirstOnly()) {
setNotificationDefaults( setNotificationDefaults(notification,
notification, ChatManager.getInstance().isMakeVibro(ticker.getChat().getAccount(),
ChatManager.getInstance().isMakeVibro(
ticker.getChat().getAccount(),
ticker.getChat().getUser()), ticker.getChat().getUser()),
PhraseManager.getInstance().getSound( PhraseManager.getInstance().getSound(ticker.getChat().getAccount(),
ticker.getChat().getAccount(),
ticker.getChat().getUser(), ticker.getText()), ticker.getChat().getUser(), ticker.getText()),
AudioManager.STREAM_NOTIFICATION); AudioManager.STREAM_NOTIFICATION);
if (ChatManager.getInstance().isShowText(ticker.getChat().getAccount(), }
ticker.getChat().getUser())) if (ChatManager.getInstance().isShowText(ticker.getChat().getAccount(), ticker.getChat().getUser())) {
notification.tickerText = trimText(ticker.getText()); notification.tickerText = trimText(ticker.getText());
}
} }
private MessageNotification getMessageNotification(String account, private MessageNotification getMessageNotification(String account, String user) {
String user) { for (MessageNotification messageNotification : messageNotifications) {
for (MessageNotification messageNotification : messageNotifications) if (messageNotification.equals(account, user)) {
if (messageNotification.equals(account, user))
return messageNotification; return messageNotification;
}
}
return null; return null;
} }
...@@ -515,33 +541,33 @@ public class NotificationManager implements OnInitializedListener, ...@@ -515,33 +541,33 @@ public class NotificationManager implements OnInitializedListener,
* @param messageItem * @param messageItem
* @param addNotification Whether notification should be stored. * @param addNotification Whether notification should be stored.
*/ */
public void onMessageNotification(MessageItem messageItem, public void onMessageNotification(MessageItem messageItem, boolean addNotification) {
boolean addNotification) {
if (addNotification) { if (addNotification) {
MessageNotification messageNotification = getMessageNotification( MessageNotification messageNotification = getMessageNotification(
messageItem.getChat().getAccount(), messageItem.getChat() messageItem.getChat().getAccount(), messageItem.getChat().getUser());
.getUser()); if (messageNotification == null) {
if (messageNotification == null) messageNotification = new MessageNotification(
messageNotification = new MessageNotification(messageItem messageItem.getChat().getAccount(), messageItem.getChat().getUser(), null, null, 0);
.getChat().getAccount(), messageItem.getChat() } else {
.getUser(), null, null, 0);
else
messageNotifications.remove(messageNotification); messageNotifications.remove(messageNotification);
}
messageNotification.addMessage(messageItem.getText()); messageNotification.addMessage(messageItem.getText());
messageNotifications.add(messageNotification); messageNotifications.add(messageNotification);
final String account = messageNotification.getAccount(); final String account = messageNotification.getAccount();
final String user = messageNotification.getUser(); final String user = messageNotification.getUser();
final String text = messageNotification.getText(); final String text = messageNotification.getText();
final Date timestamp = messageNotification.getTimestamp(); final Date timestamp = messageNotification.getTimestamp();
final int count = messageNotification.getCount(); final int count = messageNotification.getCount();
if (AccountManager.getInstance().getArchiveMode(account) != ArchiveMode.dontStore)
if (AccountManager.getInstance().getArchiveMode(account) != ArchiveMode.dontStore) {
Application.getInstance().runInBackground(new Runnable() { Application.getInstance().runInBackground(new Runnable() {
@Override @Override
public void run() { public void run() {
NotificationTable.getInstance().write(account, user, NotificationTable.getInstance().write(account, user, text, timestamp, count);
text, timestamp, count);
} }
}); });
}
} }
updateMessageNotification(messageItem); updateMessageNotification(messageItem);
} }
...@@ -614,12 +640,13 @@ public class NotificationManager implements OnInitializedListener, ...@@ -614,12 +640,13 @@ public class NotificationManager implements OnInitializedListener,
@Override @Override
public void onAccountRemoved(AccountItem accountItem) { public void onAccountRemoved(AccountItem accountItem) {
for (NotificationProvider<? extends NotificationItem> notificationProvider : providers) for (NotificationProvider<? extends NotificationItem> notificationProvider : providers) {
if (notificationProvider instanceof AccountNotificationProvider) { if (notificationProvider instanceof AccountNotificationProvider) {
((AccountNotificationProvider<? extends NotificationItem>) notificationProvider) ((AccountNotificationProvider) notificationProvider)
.clearAccountNotifications(accountItem.getAccount()); .clearAccountNotifications(accountItem.getAccount());
updateNotifications(notificationProvider, null); updateNotifications(notificationProvider, null);
} }
}
} }
@Override @Override
...@@ -642,10 +669,11 @@ public class NotificationManager implements OnInitializedListener, ...@@ -642,10 +669,11 @@ public class NotificationManager implements OnInitializedListener,
* @return Trimmed text. * @return Trimmed text.
*/ */
private static String trimText(String text) { private static String trimText(String text) {
if (text.length() > MAX_NOTIFICATION_TEXT) if (text.length() > MAX_NOTIFICATION_TEXT) {
return text.substring(0, MAX_NOTIFICATION_TEXT - 3) + "..."; return text.substring(0, MAX_NOTIFICATION_TEXT - 3) + "...";
else } else {
return text; return text;
}
} }
......
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