Merge branch 'quiet-hours' of https://github.com/SamWhited/Conversations into SamWhited-quiet-hours

This commit is contained in:
iNPUTmice 2014-12-15 17:16:49 +01:00
commit 25d8546ae8
4 changed files with 176 additions and 29 deletions

View File

@ -19,6 +19,7 @@ import android.util.DisplayMetrics;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -33,12 +34,13 @@ import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.ui.ManageAccountActivity; import eu.siacs.conversations.ui.ManageAccountActivity;
import eu.siacs.conversations.ui.TimePreference;
public class NotificationService { public class NotificationService {
private XmppConnectionService mXmppConnectionService; private XmppConnectionService mXmppConnectionService;
private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>(); private final LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<>();
public static int NOTIFICATION_ID = 0x2342; public static int NOTIFICATION_ID = 0x2342;
public static int FOREGROUND_NOTIFICATION_ID = 0x8899; public static int FOREGROUND_NOTIFICATION_ID = 0x8899;
@ -54,18 +56,39 @@ public class NotificationService {
public boolean notify(Message message) { public boolean notify(Message message) {
return (message.getStatus() == Message.STATUS_RECEIVED) return (message.getStatus() == Message.STATUS_RECEIVED)
&& notificationsEnabled() && notificationsEnabled()
&& !message.getConversation().isMuted() && !isQuietHours()
&& (message.getConversation().getMode() == Conversation.MODE_SINGLE && !message.getConversation().isMuted()
&& (message.getConversation().getMode() == Conversation.MODE_SINGLE
|| conferenceNotificationsEnabled() || conferenceNotificationsEnabled()
|| wasHighlightedOrPrivate(message) || wasHighlightedOrPrivate(message)
); );
} }
public boolean notificationsEnabled() { public boolean notificationsEnabled() {
return mXmppConnectionService.getPreferences().getBoolean("show_notification", true); return mXmppConnectionService.getPreferences().getBoolean("show_notification", true);
} }
public boolean isQuietHours() {
if (!mXmppConnectionService.getPreferences().getBoolean("enable_quiet_hours", false)) {
return false;
}
final Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(mXmppConnectionService.getPreferences().getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE));
final Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(mXmppConnectionService.getPreferences().getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE));
final Calendar nowTime = Calendar.getInstance();
startTime.set(nowTime.get(Calendar.YEAR), nowTime.get(Calendar.MONTH), nowTime.get(Calendar.DATE));
endTime.set(nowTime.get(Calendar.YEAR), nowTime.get(Calendar.MONTH), nowTime.get(Calendar.DATE));
if (endTime.before(startTime)) {
endTime.add(Calendar.DATE, 1);
}
return nowTime.after(startTime) && nowTime.before(endTime);
}
public boolean conferenceNotificationsEnabled() { public boolean conferenceNotificationsEnabled() {
return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false); return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false);
} }
@ -75,19 +98,19 @@ public class NotificationService {
return; return;
} }
PowerManager pm = (PowerManager) mXmppConnectionService PowerManager pm = (PowerManager) mXmppConnectionService
.getSystemService(Context.POWER_SERVICE); .getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn(); 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(); 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<Message>(); ArrayList<Message> mList = new ArrayList<>();
mList.add(message); mList.add(message);
notifications.put(conversationUuid, mList); notifications.put(conversationUuid, mList);
} }
@ -115,7 +138,7 @@ public class NotificationService {
private void updateNotification(boolean notify) { private void updateNotification(boolean notify) {
NotificationManager notificationManager = (NotificationManager) mXmppConnectionService NotificationManager notificationManager = (NotificationManager) mXmppConnectionService
.getSystemService(Context.NOTIFICATION_SERVICE); .getSystemService(Context.NOTIFICATION_SERVICE);
SharedPreferences preferences = mXmppConnectionService.getPreferences(); SharedPreferences preferences = mXmppConnectionService.getPreferences();
String ringtone = preferences.getString("notification_ringtone", null); String ringtone = preferences.getString("notification_ringtone", null);
@ -167,7 +190,7 @@ public class NotificationService {
conversation = messages.get(0).getConversation(); conversation = messages.get(0).getConversation();
String name = conversation.getName(); String name = conversation.getName();
style.addLine(Html.fromHtml("<b>" + name + "</b> " style.addLine(Html.fromHtml("<b>" + name + "</b> "
+ getReadableBody(messages.get(0)))); + getReadableBody(messages.get(0))));
names.append(name); names.append(name);
names.append(", "); names.append(", ");
} }
@ -183,7 +206,7 @@ public class NotificationService {
mBuilder.setStyle(style); mBuilder.setStyle(style);
if (conversation != null) { if (conversation != null) {
mBuilder.setContentIntent(createContentIntent(conversation mBuilder.setContentIntent(createContentIntent(conversation
.getUuid())); .getUuid()));
} }
return mBuilder; return mBuilder;
} }
@ -204,23 +227,23 @@ public class NotificationService {
modifyForTextOnly(mBuilder, messages, notify); modifyForTextOnly(mBuilder, messages, notify);
} }
mBuilder.setContentIntent(createContentIntent(conversation mBuilder.setContentIntent(createContentIntent(conversation
.getUuid())); .getUuid()));
} }
return mBuilder; return mBuilder;
} }
private void modifyForImage(Builder builder, Message message, private void modifyForImage(Builder builder, Message message,
ArrayList<Message> messages, boolean notify) { ArrayList<Message> messages, boolean notify) {
try { try {
Bitmap bitmap = mXmppConnectionService.getFileBackend() Bitmap bitmap = mXmppConnectionService.getFileBackend()
.getThumbnail(message, getPixel(288), false); .getThumbnail(message, getPixel(288), false);
ArrayList<Message> tmp = new ArrayList<Message>(); ArrayList<Message> tmp = new ArrayList<>();
for (Message msg : messages) { for (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(); BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.bigPicture(bitmap); bigPictureStyle.bigPicture(bitmap);
@ -237,7 +260,7 @@ public class NotificationService {
} }
private void modifyForTextOnly(Builder builder, private void modifyForTextOnly(Builder builder,
ArrayList<Message> messages, boolean notify) { ArrayList<Message> messages, 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)));
@ -252,7 +275,7 @@ public class NotificationService {
&& message.getDownloadable() == null && message.getDownloadable() == null
&& message.getEncryption() != Message.ENCRYPTION_PGP) { && message.getEncryption() != Message.ENCRYPTION_PGP) {
return message; return message;
} }
} }
return null; return null;
} }
@ -271,7 +294,7 @@ public class NotificationService {
private String getReadableBody(Message message) { private String getReadableBody(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)) {
if (message.getType() == Message.TYPE_FILE) { if (message.getType() == Message.TYPE_FILE) {
return mXmppConnectionService.getString(R.string.file_offered_for_download); return mXmppConnectionService.getString(R.string.file_offered_for_download);
} else { } else {
@ -283,13 +306,13 @@ public class NotificationService {
R.string.encrypted_message_received).toString(); R.string.encrypted_message_received).toString();
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
return mXmppConnectionService.getText(R.string.decryption_failed) return mXmppConnectionService.getText(R.string.decryption_failed)
.toString(); .toString();
} else if (message.getType() == Message.TYPE_FILE) { } else if (message.getType() == Message.TYPE_FILE) {
DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message); DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
return mXmppConnectionService.getString(R.string.file,file.getMimeType()); return mXmppConnectionService.getString(R.string.file,file.getMimeType());
} else if (message.getType() == Message.TYPE_IMAGE) { } else if (message.getType() == Message.TYPE_IMAGE) {
return mXmppConnectionService.getText(R.string.image_file) return mXmppConnectionService.getText(R.string.image_file)
.toString(); .toString();
} else { } else {
return message.getBody().trim(); return message.getBody().trim();
} }
@ -297,7 +320,7 @@ public class NotificationService {
private PendingIntent createContentIntent(String conversationUuid) { private PendingIntent createContentIntent(String conversationUuid) {
TaskStackBuilder stackBuilder = TaskStackBuilder TaskStackBuilder stackBuilder = TaskStackBuilder
.create(mXmppConnectionService); .create(mXmppConnectionService);
stackBuilder.addParentStack(ConversationActivity.class); stackBuilder.addParentStack(ConversationActivity.class);
Intent viewConversationIntent = new Intent(mXmppConnectionService, Intent viewConversationIntent = new Intent(mXmppConnectionService,
@ -311,9 +334,7 @@ public class NotificationService {
stackBuilder.addNextIntent(viewConversationIntent); stackBuilder.addNextIntent(viewConversationIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent.FLAG_UPDATE_CURRENT);
return resultPendingIntent;
} }
private PendingIntent createDeleteIntent() { private PendingIntent createDeleteIntent() {
@ -360,7 +381,7 @@ public class NotificationService {
private int getPixel(int dp) { private int getPixel(int dp) {
DisplayMetrics metrics = mXmppConnectionService.getResources() DisplayMetrics metrics = mXmppConnectionService.getResources()
.getDisplayMetrics(); .getDisplayMetrics();
return ((int) (dp * metrics.density)); return ((int) (dp * metrics.density));
} }
@ -370,7 +391,7 @@ public class NotificationService {
private boolean inMiniGracePeriod(Account account) { private boolean inMiniGracePeriod(Account account) {
int miniGrace = account.getStatus() == Account.State.ONLINE ? Config.MINI_GRACE_PERIOD 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);
} }

View File

@ -0,0 +1,99 @@
package eu.siacs.conversations.ui;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimePreference extends DialogPreference {
private TimePicker picker = null;
public final static long DEFAULT_VALUE = 0;
public TimePreference(final Context context, final AttributeSet attrs) {
super(context, attrs, 0);
}
protected void setTime(final long time) {
persistLong(time);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
protected void updateSummary() {
final long time = getPersistedLong(DEFAULT_VALUE);
final DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getContext());
final Date date = new Date(time);
setSummary(dateFormat.format(date.getTime()));
}
@Override
protected View onCreateDialogView() {
picker = new TimePicker(getContext());
picker.setIs24HourView(android.text.format.DateFormat.is24HourFormat(getContext()));
return picker;
}
protected Calendar getPersistedTime() {
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(getPersistedLong(DEFAULT_VALUE));
return c;
}
@SuppressWarnings("NullableProblems")
@Override
protected void onBindDialogView(final View v) {
super.onBindDialogView(v);
final Calendar c = getPersistedTime();
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendar.MINUTE));
}
@Override
protected void onDialogClosed(final boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
final Calendar c = Calendar.getInstance();
c.set(Calendar.MINUTE, picker.getCurrentMinute());
c.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
if (!callChangeListener(c.getTimeInMillis())) {
return;
}
setTime(c.getTimeInMillis());
updateSummary();
}
}
@Override
protected Object onGetDefaultValue(final TypedArray a, final int index) {
return a.getInteger(index, 0);
}
@Override
protected void onSetInitialValue(final boolean restorePersistedValue, final Object defaultValue) {
long time;
if (defaultValue == null) {
time = restorePersistedValue ? getPersistedLong(DEFAULT_VALUE) : DEFAULT_VALUE;
} else if (defaultValue instanceof Long) {
time = restorePersistedValue ? getPersistedLong((Long) defaultValue) : (Long) defaultValue;
} else if (defaultValue instanceof Calendar) {
time = restorePersistedValue ? getPersistedLong(((Calendar)defaultValue).getTimeInMillis()) : ((Calendar)defaultValue).getTimeInMillis();
} else {
time = restorePersistedValue ? getPersistedLong(DEFAULT_VALUE) : DEFAULT_VALUE;
}
setTime(time);
updateSummary();
}
}

View File

@ -41,6 +41,7 @@
<string name="invite_contact">Invite Contact</string> <string name="invite_contact">Invite Contact</string>
<string name="contacts">Contacts</string> <string name="contacts">Contacts</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string name="set">Set</string>
<string name="add">Add</string> <string name="add">Add</string>
<string name="edit">Edit</string> <string name="edit">Edit</string>
<string name="delete">Delete</string> <string name="delete">Delete</string>
@ -282,6 +283,11 @@
\n\nhttps://developer.android.com/tools/support-library\n(Apache License, Version 2.0) \n\nhttps://developer.android.com/tools/support-library\n(Apache License, Version 2.0)
\n\nhttps://github.com/zxing/zxing\n(Apache License, Version 2.0) \n\nhttps://github.com/zxing/zxing\n(Apache License, Version 2.0)
</string> </string>
<string name="title_pref_quiet_hours">Quiet Hours</string>
<string name="title_pref_quiet_hours_start_time">Start time</string>
<string name="title_pref_quiet_hours_end_time">End time</string>
<string name="title_pref_enable_quiet_hours">Enable quiet hours</string>
<string name="pref_quiet_hours_summary">Notifications will be silenced during quiet hours</string>
<string name="pref_use_larger_font">Increase font size</string> <string name="pref_use_larger_font">Increase font size</string>
<string name="pref_use_larger_font_summary">Use larger font sizes across the entire app</string> <string name="pref_use_larger_font_summary">Use larger font sizes across the entire app</string>
<string name="pref_use_send_button_to_indicate_status">Send button indicates status</string> <string name="pref_use_send_button_to_indicate_status">Send button indicates status</string>

View File

@ -35,7 +35,29 @@
android:key="show_notification" android:key="show_notification"
android:summary="@string/pref_notifications_summary" android:summary="@string/pref_notifications_summary"
android:title="@string/pref_notifications" /> android:title="@string/pref_notifications" />
<CheckBoxPreference <PreferenceScreen
android:dependency="show_notification"
android:summary="@string/pref_quiet_hours_summary"
android:title="@string/title_pref_quiet_hours">
<CheckBoxPreference
android:defaultValue="false"
android:key="enable_quiet_hours"
android:summary="@string/pref_quiet_hours_summary"
android:title="@string/title_pref_enable_quiet_hours" />
<eu.siacs.conversations.ui.TimePreference
android:dependency="enable_quiet_hours"
android:key="quiet_hours_start"
android:negativeButtonText="@string/cancel"
android:positiveButtonText="@string/set"
android:title="@string/title_pref_quiet_hours_start_time" />
<eu.siacs.conversations.ui.TimePreference
android:dependency="enable_quiet_hours"
android:key="quiet_hours_end"
android:negativeButtonText="@string/cancel"
android:positiveButtonText="@string/set"
android:title="@string/title_pref_quiet_hours_end_time" />
</PreferenceScreen>
<CheckBoxPreference
android:defaultValue="true" android:defaultValue="true"
android:dependency="show_notification" android:dependency="show_notification"
android:key="vibrate_on_notification" android:key="vibrate_on_notification"
@ -123,5 +145,4 @@
<eu.siacs.conversations.ui.AboutPreference <eu.siacs.conversations.ui.AboutPreference
android:summary="@string/pref_about_conversations_summary" android:summary="@string/pref_about_conversations_summary"
android:title="@string/title_activity_about" /> android:title="@string/title_activity_about" />
</PreferenceScreen> </PreferenceScreen>