Commit 93f11def authored by Grigory Fedorov's avatar Grigory Fedorov

New class MessageNotificationCreator. NotificationManager refactored....

New class MessageNotificationCreator. NotificationManager refactored. Notification builder used for all notifications.
New icons for notifications. Old removed.
parent ec790d5e
...@@ -14,18 +14,6 @@ ...@@ -14,18 +14,6 @@
*/ */
package com.xabber.android.data.account; package com.xabber.android.data.account;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.util.StringUtils;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.database.Cursor; import android.database.Cursor;
import android.os.Build; import android.os.Build;
...@@ -46,6 +34,18 @@ import com.xabber.android.data.roster.RosterManager; ...@@ -46,6 +34,18 @@ import com.xabber.android.data.roster.RosterManager;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
import com.xabber.xmpp.address.Jid; import com.xabber.xmpp.address.Jid;
import org.jivesoftware.smack.util.StringUtils;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
/** /**
* This class manage all operations with accounts. * This class manage all operations with accounts.
* <p/> * <p/>
...@@ -117,9 +117,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -117,9 +117,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
enabledAccounts = new HashSet<String>(); enabledAccounts = new HashSet<String>();
savedStatuses = new ArrayList<SavedStatus>(); savedStatuses = new ArrayList<SavedStatus>();
authorizationErrorProvider = new BaseAccountNotificationProvider<AccountAuthorizationError>( authorizationErrorProvider = new BaseAccountNotificationProvider<AccountAuthorizationError>(
R.drawable.ic_stat_auth_failed); R.drawable.ic_stat_error);
passwordRequestProvider = new BaseAccountNotificationProvider<PasswordRequest>( passwordRequestProvider = new BaseAccountNotificationProvider<PasswordRequest>(
R.drawable.ic_stat_request); R.drawable.ic_stat_ic_add_circle);
TypedArray accountAvatars = application.getResources() TypedArray accountAvatars = application.getResources()
.obtainTypedArray(R.array.account_avatars); .obtainTypedArray(R.array.account_avatars);
......
...@@ -14,6 +14,19 @@ ...@@ -14,6 +14,19 @@
*/ */
package com.xabber.android.data.connection; package com.xabber.android.data.connection;
import android.content.res.AssetManager;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.OnClearListener;
import com.xabber.android.data.OnLoadListener;
import com.xabber.android.data.notification.BaseNotificationProvider;
import com.xabber.android.data.notification.NotificationManager;
import com.xabber.androiddev.R;
import org.jivesoftware.smack.CertificateListener;
import org.jivesoftware.smack.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
...@@ -34,19 +47,6 @@ import java.util.HashMap; ...@@ -34,19 +47,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.CertificateListener;
import org.jivesoftware.smack.util.StringUtils;
import android.content.res.AssetManager;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.OnClearListener;
import com.xabber.android.data.OnLoadListener;
import com.xabber.android.data.notification.BaseNotificationProvider;
import com.xabber.android.data.notification.NotificationManager;
import com.xabber.androiddev.R;
/** /**
* Manage certificate exceptions. * Manage certificate exceptions.
* <p/> * <p/>
...@@ -94,7 +94,7 @@ public class CertificateManager implements OnLoadListener, OnClearListener { ...@@ -94,7 +94,7 @@ public class CertificateManager implements OnLoadListener, OnClearListener {
private final Map<CertificateInvalidReason, KeyStore> defaultStores; private final Map<CertificateInvalidReason, KeyStore> defaultStores;
private final BaseNotificationProvider<PendingCertificate> pendingCertificateProvider = new BaseNotificationProvider<PendingCertificate>( private final BaseNotificationProvider<PendingCertificate> pendingCertificateProvider = new BaseNotificationProvider<PendingCertificate>(
R.drawable.ic_stat_auth_failed) { R.drawable.ic_stat_error) {
@Override @Override
public void clearNotifications() { public void clearNotifications() {
......
...@@ -14,16 +14,6 @@ ...@@ -14,16 +14,6 @@
*/ */
package com.xabber.android.data.extension.archive; package com.xabber.android.data.extension.archive;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager; import com.xabber.android.data.LogManager;
import com.xabber.android.data.NetworkException; import com.xabber.android.data.NetworkException;
...@@ -68,6 +58,16 @@ import com.xabber.xmpp.archive.Session; ...@@ -68,6 +58,16 @@ import com.xabber.xmpp.archive.Session;
import com.xabber.xmpp.archive.SessionRemove; import com.xabber.xmpp.archive.SessionRemove;
import com.xabber.xmpp.rsm.Set; import com.xabber.xmpp.rsm.Set;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/** /**
* Manage server side archive. Replicate it with memory storage. * Manage server side archive. Replicate it with memory storage.
* <p/> * <p/>
...@@ -164,7 +164,7 @@ public class MessageArchiveManager implements OnPacketListener, ...@@ -164,7 +164,7 @@ public class MessageArchiveManager implements OnPacketListener,
modificationRequests = new NestedMap<Boolean>(); modificationRequests = new NestedMap<Boolean>();
chatStorages = new NestedNestedMaps<String, ChatStorage>(); chatStorages = new NestedNestedMaps<String, ChatStorage>();
availableArchiveRequestProvider = new BaseAccountNotificationProvider<AvailableArchiveRequest>( availableArchiveRequestProvider = new BaseAccountNotificationProvider<AvailableArchiveRequest>(
R.drawable.ic_stat_request); R.drawable.ic_stat_ic_help_black);
} }
@Override @Override
......
...@@ -14,16 +14,6 @@ ...@@ -14,16 +14,6 @@
*/ */
package com.xabber.android.data.extension.attention; package com.xabber.android.data.extension.attention;
import java.util.Iterator;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
...@@ -51,6 +41,16 @@ import com.xabber.androiddev.R; ...@@ -51,6 +41,16 @@ import com.xabber.androiddev.R;
import com.xabber.xmpp.address.Jid; import com.xabber.xmpp.address.Jid;
import com.xabber.xmpp.attention.Attention; import com.xabber.xmpp.attention.Attention;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import java.util.Iterator;
/** /**
* XEP-0224: Attention. * XEP-0224: Attention.
* *
...@@ -86,7 +86,7 @@ public class AttentionManager implements OnPacketListener, OnLoadListener { ...@@ -86,7 +86,7 @@ public class AttentionManager implements OnPacketListener, OnLoadListener {
} }
private final EntityNotificationProvider<AttentionRequest> attentionRequestProvider = new EntityNotificationProvider<AttentionRequest>( private final EntityNotificationProvider<AttentionRequest> attentionRequestProvider = new EntityNotificationProvider<AttentionRequest>(
R.drawable.ic_stat_attention) { R.drawable.ic_stat_error) {
@Override @Override
public Uri getSound() { public Uri getSound() {
......
...@@ -14,17 +14,6 @@ ...@@ -14,17 +14,6 @@
*/ */
package com.xabber.android.data.extension.muc; package com.xabber.android.data.extension.muc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.packet.MUCUser;
import android.database.Cursor; import android.database.Cursor;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
...@@ -46,6 +35,17 @@ import com.xabber.android.data.roster.RosterManager; ...@@ -46,6 +35,17 @@ import com.xabber.android.data.roster.RosterManager;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
import com.xabber.xmpp.muc.MUC; import com.xabber.xmpp.muc.MUC;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.packet.MUCUser;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/** /**
* Manage multi user chats. * Manage multi user chats.
* <p/> * <p/>
...@@ -72,9 +72,9 @@ public class MUCManager implements OnLoadListener, OnPacketListener { ...@@ -72,9 +72,9 @@ public class MUCManager implements OnLoadListener, OnPacketListener {
private MUCManager() { private MUCManager() {
inviteProvider = new EntityNotificationProvider<RoomInvite>( inviteProvider = new EntityNotificationProvider<RoomInvite>(
R.drawable.ic_stat_subscribe); R.drawable.ic_stat_ic_add_circle);
authorizationErrorProvider = new EntityNotificationProvider<RoomAuthorizationError>( authorizationErrorProvider = new EntityNotificationProvider<RoomAuthorizationError>(
R.drawable.ic_stat_auth_failed); R.drawable.ic_stat_error);
} }
@Override @Override
......
...@@ -14,29 +14,6 @@ ...@@ -14,29 +14,6 @@
*/ */
package com.xabber.android.data.extension.otr; package com.xabber.android.data.extension.otr;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import net.java.otr4j.OtrEngineHost;
import net.java.otr4j.OtrEngineListener;
import net.java.otr4j.OtrException;
import net.java.otr4j.OtrPolicy;
import net.java.otr4j.OtrPolicyImpl;
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
import net.java.otr4j.crypto.OtrCryptoException;
import net.java.otr4j.io.SerializationUtils;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus;
import android.database.Cursor; import android.database.Cursor;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
...@@ -66,6 +43,29 @@ import com.xabber.androiddev.R; ...@@ -66,6 +43,29 @@ import com.xabber.androiddev.R;
import com.xabber.xmpp.archive.OtrMode; import com.xabber.xmpp.archive.OtrMode;
import com.xabber.xmpp.archive.SaveMode; import com.xabber.xmpp.archive.SaveMode;
import net.java.otr4j.OtrEngineHost;
import net.java.otr4j.OtrEngineListener;
import net.java.otr4j.OtrException;
import net.java.otr4j.OtrPolicy;
import net.java.otr4j.OtrPolicyImpl;
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
import net.java.otr4j.crypto.OtrCryptoException;
import net.java.otr4j.io.SerializationUtils;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/** /**
* Manage off-the-record encryption. * Manage off-the-record encryption.
* <p/> * <p/>
...@@ -134,9 +134,9 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -134,9 +134,9 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
private OTRManager() { private OTRManager() {
smRequestProvider = new EntityNotificationProvider<SMRequest>( smRequestProvider = new EntityNotificationProvider<SMRequest>(
R.drawable.ic_stat_request); R.drawable.ic_stat_ic_help_black);
smProgressProvider = new EntityNotificationProvider<SMProgress>( smProgressProvider = new EntityNotificationProvider<SMProgress>(
R.drawable.ic_stat_play); R.drawable.ic_stat_ic_play_circle_fill);
smProgressProvider.setCanClearNotifications(false); smProgressProvider.setCanClearNotifications(false);
fingerprints = new NestedNestedMaps<String, Boolean>(); fingerprints = new NestedNestedMaps<String, Boolean>();
actives = new NestedMap<String>(); actives = new NestedMap<String>();
......
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 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.utils.StringUtils;
import com.xabber.androiddev.R;
import java.util.List;
public class MessageNotificationCreator {
private final Application application;
private List<MessageNotification> messageNotifications;
private NotificationCompat.Builder notificationBuilder;
public MessageNotificationCreator() {
application = Application.getInstance();
}
public android.app.Notification notifyMessageNotification(List<MessageNotification> messageNotifications,
MessageItem messageItem) {
this.messageNotifications = messageNotifications;
if (messageNotifications.isEmpty()) {
return null;
}
int messageCount = 0;
for (MessageNotification messageNotification : messageNotifications) {
messageCount += messageNotification.getCount();
}
MessageNotification message = messageNotifications.get(messageNotifications.size() - 1);
boolean showText = ChatManager.getInstance().isShowText(message.getAccount(), message.getUser());
notificationBuilder = new NotificationCompat.Builder(application);
notificationBuilder.setContentTitle(getTitle(message, messageCount));
notificationBuilder.setContentText(getText(message, showText));
if (showText) {
notificationBuilder.setTicker(message.getText());
}
notificationBuilder.setSmallIcon(getSmallIcon());
notificationBuilder.setLargeIcon(getLargeIcon(message));
notificationBuilder.setWhen(message.getTimestamp().getTime());
notificationBuilder.setColor(NotificationManager.COLOR_MATERIAL_RED_500);
notificationBuilder.setStyle(getStyle(message, messageCount));
notificationBuilder.setContentIntent(getIntent(message));
addEffects(messageItem);
return notificationBuilder.build();
}
private CharSequence getTitle(MessageNotification message, int messageCount) {
if (isFromOneContact()) {
return RosterManager.getInstance().getName(message.getAccount(), message.getUser());
} else {
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());
return application.getString(R.string.chat_status,
messageCount, messageText, messageNotifications.size(), contactText);
}
}
private CharSequence getText(MessageNotification message, boolean showText) {
if (showText && isFromOneContact()) {
return message.getText();
} else {
return "";
}
}
private int getSmallIcon() {
return R.drawable.ic_stat_chat;
}
private android.graphics.Bitmap getLargeIcon(MessageNotification message) {
if (isFromOneContact()) {
if (MUCManager.getInstance().hasRoom(message.getAccount(), message.getUser())) {
return AvatarManager.getInstance().getRoomBitmap(message.getUser());
} else {
return AvatarManager.getInstance().getUserBitmap(message.getUser());
}
}
return null;
}
private boolean isFromOneContact() {
return messageNotifications.size() == 1;
}
private NotificationCompat.BigTextStyle getStyle(MessageNotification message, int messageCount) {
if (messageCount == 1) {
String title = RosterManager.getInstance().getName(message.getAccount(), message.getUser());
String text = message.getText();
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(text);
return bigTextStyle;
}
return null;
}
private PendingIntent getIntent(MessageNotification message) {
Intent chatIntent
= ChatViewer.createClearTopIntent(application, message.getAccount(), message.getUser());
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,7 +19,6 @@ import android.app.PendingIntent; ...@@ -19,7 +19,6 @@ 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;
...@@ -38,18 +37,12 @@ import com.xabber.android.data.account.OnAccountArchiveModeChangedListener; ...@@ -38,18 +37,12 @@ import com.xabber.android.data.account.OnAccountArchiveModeChangedListener;
import com.xabber.android.data.account.OnAccountChangedListener; import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.account.OnAccountRemovedListener; 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.extension.avatar.AvatarManager;
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.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.ChatViewer;
import com.xabber.android.ui.ClearNotifications; import com.xabber.android.ui.ClearNotifications;
import com.xabber.android.ui.ContactList; import com.xabber.android.ui.ContactList;
import com.xabber.android.ui.ReconnectionActivity; import com.xabber.android.ui.ReconnectionActivity;
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;
...@@ -69,14 +62,13 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -69,14 +62,13 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
OnAccountArchiveModeChangedListener { OnAccountArchiveModeChangedListener {
public static final int PERSISTENT_NOTIFICATION_ID = 1; public static final int PERSISTENT_NOTIFICATION_ID = 1;
public static final int MESSAGE_NOTIFICATION_ID = 2;
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;
private static final int MAX_NOTIFICATION_TEXT = 80;
public static final int COLOR_MATERIAL_RED_500 = 0xF44336; public static final int COLOR_MATERIAL_RED_500 = 0xF44336;
private final long startTime;
private final Application application; private final Application application;
private final android.app.NotificationManager notificationManager; private final android.app.NotificationManager notificationManager;
private final PendingIntent clearNotifications; private final PendingIntent clearNotifications;
...@@ -84,6 +76,8 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -84,6 +76,8 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
private NotificationCompat.Builder persistentNotificationBuilder; private NotificationCompat.Builder persistentNotificationBuilder;
private MessageNotificationCreator messageNotificationCreator;
/** /**
* Runnable to start vibration. * Runnable to start vibration.
*/ */
...@@ -125,7 +119,6 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -125,7 +119,6 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
handler = new Handler(); handler = new Handler();
providers = new ArrayList<>(); providers = new ArrayList<>();
messageNotifications = new ArrayList<>(); messageNotifications = new ArrayList<>();
startTime = System.currentTimeMillis();
clearNotifications = PendingIntent.getActivity( clearNotifications = PendingIntent.getActivity(
application, 0, ClearNotifications.createIntent(application), 0); application, 0, ClearNotifications.createIntent(application), 0);
...@@ -154,6 +147,18 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -154,6 +147,18 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
}; };
persistentNotificationBuilder = new NotificationCompat.Builder(application); persistentNotificationBuilder = new NotificationCompat.Builder(application);
initPersistentNotification();
messageNotificationCreator = new MessageNotificationCreator();
}
private void initPersistentNotification() {
persistentNotificationBuilder.setContentTitle(application.getString(R.string.application_name));
persistentNotificationBuilder.setDeleteIntent(clearNotifications);
persistentNotificationBuilder.setOngoing(true);
persistentNotificationBuilder.setColor(COLOR_MATERIAL_RED_500);
persistentNotificationBuilder.setWhen(System.currentTimeMillis());
} }
@Override @Override
...@@ -245,38 +250,45 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -245,38 +250,45 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(application); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(application);
notificationBuilder.setSmallIcon(provider.getIcon()).setTicker(ticker); notificationBuilder.setSmallIcon(provider.getIcon());
notificationBuilder.setTicker(ticker);
if (!provider.canClearNotifications()) { if (!provider.canClearNotifications()) {
notificationBuilder.setOngoing(true); notificationBuilder.setOngoing(true);
} }
notificationBuilder.setContentTitle(top.getTitle()).setContentText(top.getText()); notificationBuilder.setContentTitle(top.getTitle());
notificationBuilder.setContentText(top.getText());
notificationBuilder.setContentIntent(PendingIntent.getActivity(application, 0, intent, notificationBuilder.setContentIntent(PendingIntent.getActivity(application, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)); PendingIntent.FLAG_UPDATE_CURRENT));
if (ticker != null) { if (ticker != null) {
setNotificationDefaults(SettingsManager.eventsVibro(), provider.getSound(), provider.getStreamType()); setNotificationDefaults(notificationBuilder, SettingsManager.eventsVibro(), provider.getSound(), provider.getStreamType());
} }
notificationBuilder.setDeleteIntent(clearNotifications); notificationBuilder.setDeleteIntent(clearNotifications);
notificationBuilder.setColor(COLOR_MATERIAL_RED_500);
notify(id, notificationBuilder.build()); notify(id, notificationBuilder.build());
} }
/** /**
* Sound, vibration and lightning flags. * Sound, vibration and lightning flags.
* *
* @param notificationBuilder
* @param streamType * @param streamType
*/ */
private void setNotificationDefaults(boolean vibration, Uri sound, int streamType) { public void setNotificationDefaults(NotificationCompat.Builder notificationBuilder, boolean vibration, Uri sound, int streamType) {
persistentNotificationBuilder.setSound(sound, streamType); notificationBuilder.setSound(sound, streamType);
persistentNotificationBuilder.setDefaults(0); notificationBuilder.setDefaults(0);
int defaults = 0; int defaults = 0;
if (vibration) { if (vibration) {
if (SettingsManager.eventsIgnoreSystemVibro()) { if (SettingsManager.eventsIgnoreSystemVibro()) {
handler.post(startVibration); startVibration();
} else { } else {
defaults |= Notification.DEFAULT_VIBRATE; defaults |= Notification.DEFAULT_VIBRATE;
...@@ -287,7 +299,11 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -287,7 +299,11 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
defaults |= Notification.DEFAULT_LIGHTS; defaults |= Notification.DEFAULT_LIGHTS;
} }
persistentNotificationBuilder.setDefaults(defaults); notificationBuilder.setDefaults(defaults);
}
public void startVibration() {
handler.post(startVibration);
} }
/** /**
...@@ -304,15 +320,23 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -304,15 +320,23 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
* @return * @return
*/ */
private void updateMessageNotification(MessageItem ticker) { private void updateMessageNotification(MessageItem ticker) {
Collection<String> accountList = AccountManager.getInstance().getAccounts(); updatePersistentNotification();
int accountCount = accountList.size(); Notification messageNotification = messageNotificationCreator.notifyMessageNotification(messageNotifications, ticker);
boolean started = application.isInitialized();
if (messageNotification != null) {
notify(MESSAGE_NOTIFICATION_ID, messageNotification);
} else {
notificationManager.cancel(MESSAGE_NOTIFICATION_ID);
}
}
private void updatePersistentNotification() {
int waiting = 0; int waiting = 0;
int connecting = 0; int connecting = 0;
int connected = 0; int connected = 0;
Collection<String> accountList = AccountManager.getInstance().getAccounts();
for (String account : accountList) { for (String account : accountList) {
ConnectionState state = AccountManager.getInstance().getAccount(account).getState(); ConnectionState state = AccountManager.getInstance().getAccount(account).getState();
...@@ -325,9 +349,27 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -325,9 +349,27 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
} }
} }
final Intent persistentIntent;
if (waiting > 0 && application.isInitialized()) {
persistentIntent = ReconnectionActivity.createIntent(application);
} else {
persistentIntent = ContactList.createPersistentIntent(application);
}
persistentNotificationBuilder.setSmallIcon(connected > 0 ? R.drawable.ic_stat_light_bulb_big
: R.drawable.ic_stat_light_bulb_big_off);
persistentNotificationBuilder.setContentText(getConnectionState(waiting, connecting, connected, accountList.size()));
persistentNotificationBuilder.setContentIntent(PendingIntent.getActivity(application, 0, persistentIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
notify(PERSISTENT_NOTIFICATION_ID, persistentNotificationBuilder.build());
}
private String getConnectionState(int waiting, int connecting, int connected, int accountCount) {
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, accountCount); application.getResources(), R.array.account_quantity, accountCount);
...@@ -347,7 +389,7 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -347,7 +389,7 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
connectionState = String.format(connectionFormat, connecting, accountCount, accountQuantity); connectionState = String.format(connectionFormat, connecting, accountCount, accountQuantity);
} else if (waiting > 0 && started) { } else if (waiting > 0 && application.isInitialized()) {
accountQuantity = StringUtils.getQuantityString( accountQuantity = StringUtils.getQuantityString(
application.getResources(), R.array.account_quantity, accountCount); application.getResources(), R.array.account_quantity, accountCount);
...@@ -363,145 +405,17 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -363,145 +405,17 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
connectionState = application.getString( connectionState = application.getString(
R.string.connection_state_offline, accountCount, accountQuantity); R.string.connection_state_offline, accountCount, accountQuantity);
} }
return connectionState;
final Intent persistentIntent;
if (waiting > 0 && started) {
persistentIntent = ReconnectionActivity.createIntent(application);
} else {
persistentIntent = ContactList.createPersistentIntent(application);
}
persistentNotificationBuilder.setSmallIcon(R.drawable.ic_stat_normal);
persistentNotificationBuilder.setContentTitle(application.getString(R.string.application_name));
persistentNotificationBuilder.setContentText(connectionState);
persistentNotificationBuilder.setContentIntent(PendingIntent.getActivity(application, 0, persistentIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
persistentNotificationBuilder.setOngoing(true);
persistentNotificationBuilder.setDefaults(0);
persistentNotificationBuilder.setSound(null);
persistentNotificationBuilder.setTicker(null);
persistentNotificationBuilder.setStyle(null);
persistentNotificationBuilder.setLargeIcon(null);
persistentNotificationBuilder.setColor(COLOR_MATERIAL_RED_500);
if (SettingsManager.eventsPersistent()) {
// Ongoing icons are in the left side, so always use it.
persistentNotificationBuilder.setWhen(startTime);
if (messageNotifications.isEmpty()) {
persistentNotificationBuilder.setSmallIcon(connected > 0 ? R.drawable.ic_stat_normal
: R.drawable.ic_stat_offline);
} else {
persistentNotificationBuilder.setSmallIcon(connected > 0 ? R.drawable.ic_stat_message
: R.drawable.ic_stat_message_offline);
}
updateNotification(ticker);
} else {
// Ongoing icons are in the right side, so hide it if necessary.
if (messageNotifications.isEmpty()) {
persistentNotificationBuilder.setSmallIcon(connected > 0 ? R.drawable.ic_stat_normal
: R.drawable.ic_stat_offline);
persistentNotificationBuilder.setWhen(startTime);
// Show ticker for the messages in active chat.
updateNotification(ticker);
} else {
persistentNotificationBuilder.setSmallIcon(R.drawable.ic_placeholder);
persistentNotificationBuilder.setWhen(-Long.MAX_VALUE);
}
}
if (SettingsManager.eventsPersistent()) {
notify(PERSISTENT_NOTIFICATION_ID, persistentNotificationBuilder.build());
} else {
notificationManager.cancel(PERSISTENT_NOTIFICATION_ID);
}
if (!messageNotifications.isEmpty()) {
notifyMessageNotification(ticker, connected);
}
} }
private void notifyMessageNotification(MessageItem ticker, int connected) {
int messageCount = 0;
for (MessageNotification messageNotification : messageNotifications) {
messageCount += messageNotification.getCount();
}
MessageNotification message = messageNotifications.get(messageNotifications.size() - 1);
Intent chatIntent
= ChatViewer.createClearTopIntent(application, message.getAccount(), message.getUser());
if (messageCount == 1) {
String title = RosterManager.getInstance().getName(message.getAccount(), message.getUser());
String text;
if (ChatManager.getInstance().isShowText(message.getAccount(), message.getUser())) {
text = trimText(message.getText());
} else {
text = "";
}
persistentNotificationBuilder.setContentTitle(title);
persistentNotificationBuilder.setContentText(Emoticons.getSmiledText(application, text));
if (MUCManager.getInstance().hasRoom(message.getAccount(), message.getUser())) {
persistentNotificationBuilder.setLargeIcon(AvatarManager.getInstance().getRoomBitmap(message.getUser()));
} else {
persistentNotificationBuilder.setLargeIcon(AvatarManager.getInstance().getUserBitmap(message.getUser()));
}
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(text);
persistentNotificationBuilder.setStyle(bigTextStyle);
} else {
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);
persistentNotificationBuilder.setContentTitle(status);
}
persistentNotificationBuilder.setWhen(message.getTimestamp().getTime());
updateNotification(ticker);
persistentNotificationBuilder.setSmallIcon(connected > 0 ? R.drawable.ic_stat_message
: R.drawable.ic_stat_message_offline);
persistentNotificationBuilder.setContentIntent(PendingIntent.getActivity(
application, 0, chatIntent, PendingIntent.FLAG_UPDATE_CURRENT));
persistentNotificationBuilder.setDeleteIntent(clearNotifications);
try {
notify(PERSISTENT_NOTIFICATION_ID, persistentNotificationBuilder.build());
} catch (RuntimeException e) {
LogManager.exception(this, e);
}
}
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
+ notification.tickerText + ", sound: " + notification.sound + ", ticker: " + notification.tickerText
+ ", vibro: " + ", sound: " + notification.sound
+ (notification.defaults & Notification.DEFAULT_VIBRATE) + ", vibro: " + (notification.defaults & Notification.DEFAULT_VIBRATE)
+ ", light: " + ", light: " + (notification.defaults & Notification.DEFAULT_LIGHTS));
+ (notification.defaults & Notification.DEFAULT_LIGHTS));
try { try {
notificationManager.notify(id, notification); notificationManager.notify(id, notification);
} catch (SecurityException e) { } catch (SecurityException e) {
...@@ -509,27 +423,6 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -509,27 +423,6 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
} }
} }
/**
* Update notification according to ticker.
*
* @param ticker
*/
private void updateNotification(MessageItem ticker) {
if (ticker == null) {
return;
}
if (ticker.getChat().getFirstNotification() || !SettingsManager.eventsFirstOnly()) {
setNotificationDefaults(ChatManager.getInstance().isMakeVibro(ticker.getChat().getAccount(),
ticker.getChat().getUser()),
PhraseManager.getInstance().getSound(ticker.getChat().getAccount(),
ticker.getChat().getUser(), ticker.getText()),
AudioManager.STREAM_NOTIFICATION);
}
if (ChatManager.getInstance().isShowText(ticker.getChat().getAccount(), ticker.getChat().getUser())) {
persistentNotificationBuilder.setTicker(ticker.getText());
}
}
private MessageNotification getMessageNotification(String account, String user) { private MessageNotification getMessageNotification(String account, String user) {
for (MessageNotification messageNotification : messageNotifications) { for (MessageNotification messageNotification : messageNotifications) {
if (messageNotification.equals(account, user)) { if (messageNotification.equals(account, user)) {
...@@ -667,18 +560,4 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan ...@@ -667,18 +560,4 @@ public class NotificationManager implements OnInitializedListener, OnAccountChan
public void onClose() { public void onClose() {
notificationManager.cancelAll(); notificationManager.cancelAll();
} }
/**
* @param text
* @return Trimmed text.
*/
private static String trimText(String text) {
if (text.length() > MAX_NOTIFICATION_TEXT) {
return text.substring(0, MAX_NOTIFICATION_TEXT - 3) + "...";
} else {
return text;
}
}
} }
...@@ -14,20 +14,6 @@ ...@@ -14,20 +14,6 @@
*/ */
package com.xabber.android.data.roster; package com.xabber.android.data.roster;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;
import org.jivesoftware.smack.util.StringUtils;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
import com.xabber.android.data.NetworkException; import com.xabber.android.data.NetworkException;
import com.xabber.android.data.OnLoadListener; import com.xabber.android.data.OnLoadListener;
...@@ -46,6 +32,20 @@ import com.xabber.android.data.notification.NotificationManager; ...@@ -46,6 +32,20 @@ import com.xabber.android.data.notification.NotificationManager;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
import com.xabber.xmpp.address.Jid; import com.xabber.xmpp.address.Jid;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;
import org.jivesoftware.smack.util.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
/** /**
* Process contact's presence information. * Process contact's presence information.
* *
...@@ -86,7 +86,7 @@ public class PresenceManager implements OnArchiveModificationsReceivedListener, ...@@ -86,7 +86,7 @@ public class PresenceManager implements OnArchiveModificationsReceivedListener,
private PresenceManager() { private PresenceManager() {
subscriptionRequestProvider = new EntityNotificationProvider<SubscriptionRequest>( subscriptionRequestProvider = new EntityNotificationProvider<SubscriptionRequest>(
R.drawable.ic_stat_subscribe); R.drawable.ic_stat_ic_add_circle);
requestedSubscriptions = new HashMap<String, HashSet<String>>(); requestedSubscriptions = new HashMap<String, HashSet<String>>();
presenceContainers = new NestedMap<ResourceContainer>(); presenceContainers = new NestedMap<ResourceContainer>();
readyAccounts = new ArrayList<String>(); readyAccounts = new ArrayList<String>();
......
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