Make silent notification when quiet hours are on

Notify, just don't play a sound or vibrate
Also make the Java annoyingly verbose... I can't help myself... ahh!
This commit is contained in:
Sam Whited 2014-12-15 11:20:22 -05:00
parent a6d4b0aec5
commit af0f8e87bb
1 changed files with 64 additions and 68 deletions

View File

@ -54,10 +54,9 @@ public class NotificationService {
this.mXmppConnectionService = service; this.mXmppConnectionService = service;
} }
public boolean notify(Message message) { public boolean notify(final Message message) {
return (message.getStatus() == Message.STATUS_RECEIVED) return (message.getStatus() == Message.STATUS_RECEIVED)
&& notificationsEnabled() && notificationsEnabled()
&& !isQuietHours()
&& !message.getConversation().isMuted() && !message.getConversation().isMuted()
&& (message.getConversation().getMode() == Conversation.MODE_SINGLE && (message.getConversation().getMode() == Conversation.MODE_SINGLE
|| conferenceNotificationsEnabled() || conferenceNotificationsEnabled()
@ -93,28 +92,28 @@ public class NotificationService {
return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false); return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false);
} }
public void push(Message message) { public void push(final Message message) {
if (!notify(message)) { if (!notify(message)) {
return; return;
} }
PowerManager pm = (PowerManager) mXmppConnectionService final PowerManager pm = (PowerManager) mXmppConnectionService
.getSystemService(Context.POWER_SERVICE); .getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn(); final boolean isScreenOn = pm.isScreenOn();
if (this.mIsInForeground && isScreenOn if (this.mIsInForeground && isScreenOn
&& this.mOpenConversation == message.getConversation()) { && this.mOpenConversation == message.getConversation()) {
return; return;
} }
synchronized (notifications) { synchronized (notifications) {
String conversationUuid = message.getConversationUuid(); final String conversationUuid = message.getConversationUuid();
if (notifications.containsKey(conversationUuid)) { if (notifications.containsKey(conversationUuid)) {
notifications.get(conversationUuid).add(message); notifications.get(conversationUuid).add(message);
} else { } else {
ArrayList<Message> mList = new ArrayList<>(); final ArrayList<Message> mList = new ArrayList<>();
mList.add(message); mList.add(message);
notifications.put(conversationUuid, mList); notifications.put(conversationUuid, mList);
} }
Account account = message.getConversation().getAccount(); final Account account = message.getConversation().getAccount();
updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn) updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
&& !account.inGracePeriod() && !account.inGracePeriod()
&& !this.inMiniGracePeriod(account)); && !this.inMiniGracePeriod(account));
@ -129,21 +128,20 @@ public class NotificationService {
} }
} }
public void clear(Conversation conversation) { public void clear(final Conversation conversation) {
synchronized (notifications) { synchronized (notifications) {
notifications.remove(conversation.getUuid()); notifications.remove(conversation.getUuid());
updateNotification(false); updateNotification(false);
} }
} }
private void updateNotification(boolean notify) { private void updateNotification(final boolean notify) {
NotificationManager notificationManager = (NotificationManager) mXmppConnectionService final NotificationManager notificationManager = (NotificationManager) mXmppConnectionService
.getSystemService(Context.NOTIFICATION_SERVICE); .getSystemService(Context.NOTIFICATION_SERVICE);
SharedPreferences preferences = mXmppConnectionService.getPreferences(); final SharedPreferences preferences = mXmppConnectionService.getPreferences();
String ringtone = preferences.getString("notification_ringtone", null); final String ringtone = preferences.getString("notification_ringtone", null);
boolean vibrate = preferences.getBoolean("vibrate_on_notification", final boolean vibrate = preferences.getBoolean("vibrate_on_notification", true);
true);
if (notifications.size() == 0) { if (notifications.size() == 0) {
notificationManager.cancel(NOTIFICATION_ID); notificationManager.cancel(NOTIFICATION_ID);
@ -151,16 +149,16 @@ public class NotificationService {
if (notify) { if (notify) {
this.markLastNotification(); this.markLastNotification();
} }
Builder mBuilder; final Builder mBuilder;
if (notifications.size() == 1) { if (notifications.size() == 1) {
mBuilder = buildSingleConversations(notify); mBuilder = buildSingleConversations(notify);
} else { } else {
mBuilder = buildMultipleConversation(); mBuilder = buildMultipleConversation();
} }
if (notify) { if (notify && !isQuietHours()) {
if (vibrate) { if (vibrate) {
int dat = 70; final int dat = 70;
long[] pattern = {0, 3 * dat, dat, dat}; final long[] pattern = {0, 3 * dat, dat, dat};
mBuilder.setVibrate(pattern); mBuilder.setVibrate(pattern);
} }
if (ringtone != null) { if (ringtone != null) {
@ -170,20 +168,20 @@ public class NotificationService {
mBuilder.setSmallIcon(R.drawable.ic_notification); mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setDeleteIntent(createDeleteIntent()); mBuilder.setDeleteIntent(createDeleteIntent());
mBuilder.setLights(0xffffffff, 2000, 4000); mBuilder.setLights(0xffffffff, 2000, 4000);
Notification notification = mBuilder.build(); final Notification notification = mBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification); notificationManager.notify(NOTIFICATION_ID, notification);
} }
} }
private Builder buildMultipleConversation() { private Builder buildMultipleConversation() {
Builder mBuilder = new NotificationCompat.Builder( final Builder mBuilder = new NotificationCompat.Builder(
mXmppConnectionService); mXmppConnectionService);
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle(notifications.size() style.setBigContentTitle(notifications.size()
+ " " + " "
+ mXmppConnectionService + mXmppConnectionService
.getString(R.string.unread_conversations)); .getString(R.string.unread_conversations));
StringBuilder names = new StringBuilder(); final StringBuilder names = new StringBuilder();
Conversation conversation = null; Conversation conversation = null;
for (ArrayList<Message> messages : notifications.values()) { for (ArrayList<Message> messages : notifications.values()) {
if (messages.size() > 0) { if (messages.size() > 0) {
@ -211,16 +209,16 @@ public class NotificationService {
return mBuilder; return mBuilder;
} }
private Builder buildSingleConversations(boolean notify) { private Builder buildSingleConversations(final boolean notify) {
Builder mBuilder = new NotificationCompat.Builder( final Builder mBuilder = new NotificationCompat.Builder(
mXmppConnectionService); mXmppConnectionService);
ArrayList<Message> messages = notifications.values().iterator().next(); final ArrayList<Message> messages = notifications.values().iterator().next();
if (messages.size() >= 1) { if (messages.size() >= 1) {
Conversation conversation = messages.get(0).getConversation(); final Conversation conversation = messages.get(0).getConversation();
mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService() mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService()
.get(conversation, getPixel(64))); .get(conversation, getPixel(64)));
mBuilder.setContentTitle(conversation.getName()); mBuilder.setContentTitle(conversation.getName());
Message message; final Message message;
if ((message = getImage(messages)) != null) { if ((message = getImage(messages)) != null) {
modifyForImage(mBuilder, message, messages, notify); modifyForImage(mBuilder, message, messages, notify);
} else { } else {
@ -230,22 +228,21 @@ public class NotificationService {
.getUuid())); .getUuid()));
} }
return mBuilder; return mBuilder;
} }
private void modifyForImage(Builder builder, Message message, private void modifyForImage(final Builder builder, final Message message,
ArrayList<Message> messages, boolean notify) { final ArrayList<Message> messages, final boolean notify) {
try { try {
Bitmap bitmap = mXmppConnectionService.getFileBackend() final Bitmap bitmap = mXmppConnectionService.getFileBackend()
.getThumbnail(message, getPixel(288), false); .getThumbnail(message, getPixel(288), false);
ArrayList<Message> tmp = new ArrayList<>(); final ArrayList<Message> tmp = new ArrayList<>();
for (Message msg : messages) { for (final Message msg : messages) {
if (msg.getType() == Message.TYPE_TEXT if (msg.getType() == Message.TYPE_TEXT
&& msg.getDownloadable() == null) { && msg.getDownloadable() == null) {
tmp.add(msg); tmp.add(msg);
} }
} }
BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle(); final BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.bigPicture(bitmap); bigPictureStyle.bigPicture(bitmap);
if (tmp.size() > 0) { if (tmp.size() > 0) {
bigPictureStyle.setSummaryText(getMergedBodies(tmp)); bigPictureStyle.setSummaryText(getMergedBodies(tmp));
@ -254,13 +251,13 @@ public class NotificationService {
builder.setContentText(mXmppConnectionService.getString(R.string.image_file)); builder.setContentText(mXmppConnectionService.getString(R.string.image_file));
} }
builder.setStyle(bigPictureStyle); builder.setStyle(bigPictureStyle);
} catch (FileNotFoundException e) { } catch (final FileNotFoundException e) {
modifyForTextOnly(builder, messages, notify); modifyForTextOnly(builder, messages, notify);
} }
} }
private void modifyForTextOnly(Builder builder, private void modifyForTextOnly(final Builder builder,
ArrayList<Message> messages, boolean notify) { final ArrayList<Message> messages, final boolean notify) {
builder.setStyle(new NotificationCompat.BigTextStyle() builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getMergedBodies(messages))); .bigText(getMergedBodies(messages)));
builder.setContentText(getReadableBody(messages.get(0))); builder.setContentText(getReadableBody(messages.get(0)));
@ -269,8 +266,8 @@ public class NotificationService {
} }
} }
private Message getImage(ArrayList<Message> messages) { private Message getImage(final ArrayList<Message> messages) {
for (Message message : messages) { for (final Message message : messages) {
if (message.getType() == Message.TYPE_IMAGE if (message.getType() == Message.TYPE_IMAGE
&& message.getDownloadable() == null && message.getDownloadable() == null
&& message.getEncryption() != Message.ENCRYPTION_PGP) { && message.getEncryption() != Message.ENCRYPTION_PGP) {
@ -280,8 +277,8 @@ public class NotificationService {
return null; return null;
} }
private String getMergedBodies(ArrayList<Message> messages) { private String getMergedBodies(final ArrayList<Message> messages) {
StringBuilder text = new StringBuilder(); final StringBuilder text = new StringBuilder();
for (int i = 0; i < messages.size(); ++i) { for (int i = 0; i < messages.size(); ++i) {
text.append(getReadableBody(messages.get(i))); text.append(getReadableBody(messages.get(i)));
if (i != messages.size() - 1) { if (i != messages.size() - 1) {
@ -291,7 +288,7 @@ public class NotificationService {
return text.toString(); return text.toString();
} }
private String getReadableBody(Message message) { private String getReadableBody(final Message message) {
if (message.getDownloadable() != null if (message.getDownloadable() != null
&& (message.getDownloadable().getStatus() == Downloadable.STATUS_OFFER || message && (message.getDownloadable().getStatus() == Downloadable.STATUS_OFFER || message
.getDownloadable().getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE)) { .getDownloadable().getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE)) {
@ -318,15 +315,15 @@ public class NotificationService {
} }
} }
private PendingIntent createContentIntent(String conversationUuid) { private PendingIntent createContentIntent(final String conversationUuid) {
TaskStackBuilder stackBuilder = TaskStackBuilder final TaskStackBuilder stackBuilder = TaskStackBuilder
.create(mXmppConnectionService); .create(mXmppConnectionService);
stackBuilder.addParentStack(ConversationActivity.class); stackBuilder.addParentStack(ConversationActivity.class);
Intent viewConversationIntent = new Intent(mXmppConnectionService, final Intent viewConversationIntent = new Intent(mXmppConnectionService,
ConversationActivity.class); ConversationActivity.class);
viewConversationIntent.setAction(Intent.ACTION_VIEW); viewConversationIntent.setAction(Intent.ACTION_VIEW);
if (conversationUuid!=null) { if (conversationUuid != null) {
viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
conversationUuid); conversationUuid);
viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION); viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
@ -338,30 +335,30 @@ public class NotificationService {
} }
private PendingIntent createDeleteIntent() { private PendingIntent createDeleteIntent() {
Intent intent = new Intent(mXmppConnectionService, final Intent intent = new Intent(mXmppConnectionService,
XmppConnectionService.class); XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION); intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION);
return PendingIntent.getService(mXmppConnectionService, 0, intent, 0); return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
} }
private PendingIntent createDisableForeground() { private PendingIntent createDisableForeground() {
Intent intent = new Intent(mXmppConnectionService, final Intent intent = new Intent(mXmppConnectionService,
XmppConnectionService.class); XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_DISABLE_FOREGROUND); intent.setAction(XmppConnectionService.ACTION_DISABLE_FOREGROUND);
return PendingIntent.getService(mXmppConnectionService, 0, intent, 0); return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
} }
private boolean wasHighlightedOrPrivate(Message message) { private boolean wasHighlightedOrPrivate(final Message message) {
String nick = message.getConversation().getMucOptions().getActualNick(); final String nick = message.getConversation().getMucOptions().getActualNick();
Pattern highlight = generateNickHighlightPattern(nick); final Pattern highlight = generateNickHighlightPattern(nick);
if (message.getBody() == null || nick == null) { if (message.getBody() == null || nick == null) {
return false; return false;
} }
Matcher m = highlight.matcher(message.getBody()); final Matcher m = highlight.matcher(message.getBody());
return (m.find() || message.getType() == Message.TYPE_PRIVATE); return (m.find() || message.getType() == Message.TYPE_PRIVATE);
} }
private static Pattern generateNickHighlightPattern(String nick) { private static Pattern generateNickHighlightPattern(final String nick) {
// We expect a word boundary, i.e. space or start of string, followed by // We expect a word boundary, i.e. space or start of string, followed by
// the // the
// nick (matched in case-insensitive manner), followed by optional // nick (matched in case-insensitive manner), followed by optional
@ -371,16 +368,16 @@ public class NotificationService {
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
} }
public void setOpenConversation(Conversation conversation) { public void setOpenConversation(final Conversation conversation) {
this.mOpenConversation = conversation; this.mOpenConversation = conversation;
} }
public void setIsInForeground(boolean foreground) { public void setIsInForeground(final boolean foreground) {
this.mIsInForeground = foreground; this.mIsInForeground = foreground;
} }
private int getPixel(int dp) { private int getPixel(final int dp) {
DisplayMetrics metrics = mXmppConnectionService.getResources() final DisplayMetrics metrics = mXmppConnectionService.getResources()
.getDisplayMetrics(); .getDisplayMetrics();
return ((int) (dp * metrics.density)); return ((int) (dp * metrics.density));
} }
@ -389,14 +386,14 @@ public class NotificationService {
this.mLastNotification = SystemClock.elapsedRealtime(); this.mLastNotification = SystemClock.elapsedRealtime();
} }
private boolean inMiniGracePeriod(Account account) { private boolean inMiniGracePeriod(final Account account) {
int miniGrace = account.getStatus() == Account.State.ONLINE ? Config.MINI_GRACE_PERIOD final int miniGrace = account.getStatus() == Account.State.ONLINE ? Config.MINI_GRACE_PERIOD
: Config.MINI_GRACE_PERIOD * 2; : Config.MINI_GRACE_PERIOD * 2;
return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace); return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace);
} }
public Notification createForegroundNotification() { public Notification createForegroundNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
mBuilder.setSmallIcon(R.drawable.ic_stat_communication_import_export); mBuilder.setSmallIcon(R.drawable.ic_stat_communication_import_export);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service)); mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_disable)); mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_disable));
@ -407,14 +404,14 @@ public class NotificationService {
} }
public void updateErrorNotification() { public void updateErrorNotification() {
NotificationManager mNotificationManager = (NotificationManager) mXmppConnectionService.getSystemService(Context.NOTIFICATION_SERVICE); final NotificationManager mNotificationManager = (NotificationManager) mXmppConnectionService.getSystemService(Context.NOTIFICATION_SERVICE);
List<Account> errors = new ArrayList<>(); final List<Account> errors = new ArrayList<>();
for (Account account : mXmppConnectionService.getAccounts()) { for (final Account account : mXmppConnectionService.getAccounts()) {
if (account.hasErrorStatus()) { if (account.hasErrorStatus()) {
errors.add(account); errors.add(account);
} }
} }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
if (errors.size() == 0) { if (errors.size() == 0) {
mNotificationManager.cancel(ERROR_NOTIFICATION_ID); mNotificationManager.cancel(ERROR_NOTIFICATION_ID);
return; return;
@ -431,13 +428,12 @@ public class NotificationService {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mXmppConnectionService); TaskStackBuilder stackBuilder = TaskStackBuilder.create(mXmppConnectionService);
stackBuilder.addParentStack(ConversationActivity.class); stackBuilder.addParentStack(ConversationActivity.class);
Intent manageAccountsIntent = new Intent(mXmppConnectionService,ManageAccountActivity.class); final Intent manageAccountsIntent = new Intent(mXmppConnectionService,ManageAccountActivity.class);
stackBuilder.addNextIntent(manageAccountsIntent); stackBuilder.addNextIntent(manageAccountsIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); final PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent); mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build(); mNotificationManager.notify(ERROR_NOTIFICATION_ID, mBuilder.build());
mNotificationManager.notify(ERROR_NOTIFICATION_ID, notification);
} }
} }