Add quiet hours feature

This commit is contained in:
Sam Whited 2014-12-14 02:02:17 -05:00
parent 50410dad33
commit a6d4b0aec5
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;
@ -55,6 +57,7 @@ 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()
&& !isQuietHours()
&& !message.getConversation().isMuted() && !message.getConversation().isMuted()
&& (message.getConversation().getMode() == Conversation.MODE_SINGLE && (message.getConversation().getMode() == Conversation.MODE_SINGLE
|| conferenceNotificationsEnabled() || conferenceNotificationsEnabled()
@ -66,6 +69,26 @@ public class NotificationService {
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);
} }
@ -87,7 +110,7 @@ public class NotificationService {
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);
} }
@ -215,7 +238,7 @@ public class NotificationService {
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) {
@ -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() {

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,6 +35,28 @@
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" />
<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 <CheckBoxPreference
android:defaultValue="true" android:defaultValue="true"
android:dependency="show_notification" android:dependency="show_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>