From 1e7cf564a7759d88d2fa676eec06f2ea28b8b82c Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Wed, 9 Aug 2017 19:30:16 +0200 Subject: [PATCH 1/7] Notification signal settings. #305 --- .../notification/AlarmBroadcastReceiver.java | 14 ++-- .../notification/NotificationActionUtils.java | 27 ++----- .../NotificationUpdaterService.java | 78 ++++--------------- .../notification/TaskNotificationHandler.java | 8 +- .../signals/CustomizableSignal.java | 62 +++++++++++++++ .../tasks/notification/signals/NoSignal.java | 31 ++++++++ .../signals/NotificationSignal.java | 36 +++++++++ .../notification/signals/SettingsSignal.java | 52 +++++++++++++ .../signals/SwitchableSignal.java | 43 ++++++++++ .../org/dmfs/tasks/utils/BitFlagUtils.java | 56 +++++++++++++ opentasks/src/main/res/values/prefs.xml | 13 +++- opentasks/src/main/res/values/strings.xml | 11 +++ .../src/main/res/xml/app_preferences.xml | 26 ++++++- 13 files changed, 360 insertions(+), 97 deletions(-) create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java b/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java index 341217904..3b01ef36e 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java @@ -49,11 +49,11 @@ public void onReceive(Context context, Intent intent) { Uri taskUri = intent.getData(); - boolean silent = intent.getBooleanExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, false); + boolean noSignal = intent.getBooleanExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, false); // check for pinned task if (TaskNotificationHandler.isTaskPinned(context, taskUri)) { - TaskNotificationHandler.sendPinnedTaskStartNotification(context, taskUri, silent); + TaskNotificationHandler.sendPinnedTaskStartNotification(context, taskUri, noSignal); return; } @@ -64,7 +64,7 @@ public void onReceive(Context context, Intent intent) String timezone = intent.getStringExtra(TaskContract.EXTRA_TASK_TIMEZONE); int notificationId = (int) ContentUris.parseId(taskUri); - NotificationActionUtils.sendStartNotification(context, title, taskUri, notificationId, startDate, startAllDay, timezone, silent); + NotificationActionUtils.sendStartNotification(context, title, taskUri, notificationId, startDate, startAllDay, timezone, noSignal); } } @@ -74,12 +74,12 @@ else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE)) { Uri taskUri = intent.getData(); - boolean silent = intent.getBooleanExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, false); + boolean noSignal = intent.getBooleanExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, false); // check for pinned task if (TaskNotificationHandler.isTaskPinned(context, taskUri)) { - TaskNotificationHandler.sendPinnedTaskDueNotification(context, taskUri, silent); + TaskNotificationHandler.sendPinnedTaskDueNotification(context, taskUri, noSignal); return; } @@ -91,7 +91,7 @@ else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE)) String timezone = intent.getStringExtra(TaskContract.EXTRA_TASK_TIMEZONE); int notificationId = (int) ContentUris.parseId(taskUri); - NotificationActionUtils.sendDueAlarmNotification(context, title, taskUri, notificationId, dueDate, dueAllDay, timezone, silent); + NotificationActionUtils.sendDueAlarmNotification(context, title, taskUri, notificationId, dueDate, dueAllDay, timezone, noSignal); } } @@ -101,7 +101,7 @@ else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE)) public boolean isNotificationEnabled(Context context) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); - return settings.getBoolean(context.getString(R.string.schedjoules_pref_notification_enabled), true); + return settings.getBoolean(context.getString(R.string.opentasks_pref_notification_enabled), true); } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java index 72e3f672f..6c0838816 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java @@ -37,6 +37,8 @@ import android.widget.RemoteViews; import org.dmfs.tasks.R; +import org.dmfs.tasks.notification.signals.NoSignal; +import org.dmfs.tasks.notification.signals.SwitchableSignal; import org.dmfs.tasks.utils.DateFormatter; import org.dmfs.tasks.utils.DateFormatter.DateFormatContext; @@ -60,9 +62,9 @@ public class NotificationActionUtils public final static String EXTRA_NOTIFICATION_ACTION = "org.dmfs.tasks.extra.notification.EXTRA_NOTIFICATION_ACTION"; /** - * Boolean extra to indicate that a notification is supposed to be silent, i.e. should not play a sound when it's fired. + * Boolean extra to indicate that a notification is not supposed to be signalling, i.e. should not play sound, vibrate or blink the lights when it's fired. */ - public final static String EXTRA_SILENT_NOTIFICATION = "org.dmfs.provider.tasks.extra.SILENT"; + public final static String EXTRA_NOTIFICATION_NO_SIGNAL = "org.dmfs.provider.tasks.extra.NO_SIGNAL"; private static long TIMEOUT_MILLIS = 10000; private static long sUndoTimeoutMillis = -1; @@ -110,14 +112,7 @@ public static void sendDueAlarmNotification(Context context, String title, Uri t mBuilder.setTicker(title); // enable light, sound and vibration - if (silent) - { - mBuilder.setDefaults(0); - } - else - { - mBuilder.setDefaults(Notification.DEFAULT_ALL); - } + mBuilder.setDefaults(new SwitchableSignal(context, silent).defaultsValue()); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(Intent.ACTION_VIEW); @@ -199,14 +194,7 @@ public static void sendStartNotification(Context context, String title, Uri task mBuilder.setTicker(title); // enable light, sound and vibration - if (silent) - { - mBuilder.setDefaults(0); - } - else - { - mBuilder.setDefaults(Notification.DEFAULT_ALL); - } + mBuilder.setDefaults(new SwitchableSignal(context, silent).defaultsValue()); // set notification time // set displayed time @@ -277,8 +265,7 @@ public static void createUndoNotification(final Context context, NotificationAct builder.setSmallIcon(R.drawable.ic_notification); builder.setWhen(action.getWhen()); - // disable sound & vibration - builder.setDefaults(0); + builder.setDefaults(new NoSignal().defaultsValue()); final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification); undoView.setTextViewText(R.id.description_text, context.getString(action.mActionTextResId)); diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java index 065e0326f..6533d95d7 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java @@ -16,7 +16,6 @@ package org.dmfs.tasks.notification; -import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.AlarmManager; import android.app.Notification; @@ -56,6 +55,7 @@ import org.dmfs.tasks.model.ContentSet; import org.dmfs.tasks.model.TaskFieldAdapters; import org.dmfs.tasks.notification.NotificationActionUtils.NotificationAction; +import org.dmfs.tasks.notification.signals.SwitchableSignal; import java.util.ArrayList; import java.util.TimeZone; @@ -131,7 +131,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { String intentAction = intent.getAction(); - boolean silent = intent.getBooleanExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, false); + boolean noSignal = intent.getBooleanExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, false); if (intentAction != null) { switch (intentAction) @@ -142,8 +142,8 @@ public int onStartCommand(Intent intent, int flags, int startId) case ACTION_PINNED_TASK_START: case ACTION_PINNED_TASK_DUE: - updateNotifications(true, !silent, !silent); - if (!silent) + updateNotifications(true, noSignal, !noSignal); + if (!noSignal) { delayedCancelHeadsUpNotification(); } @@ -176,7 +176,7 @@ public int onStartCommand(Intent intent, int flags, int startId) case Intent.ACTION_BOOT_COMPLETED: case Intent.ACTION_REBOOT: case TaskNotificationHandler.ACTION_FASTBOOT: - updateNotifications(true, false, false); + updateNotifications(true, true, false); break; case Intent.ACTION_DATE_CHANGED: @@ -184,15 +184,15 @@ public int onStartCommand(Intent intent, int flags, int startId) case Intent.ACTION_TIMEZONE_CHANGED: case ACTION_NEXT_DAY: updateNextDayAlarm(); - updateNotifications(false, false, false); + updateNotifications(false, true, false); break; case ACTION_CANCEL_HEADUP_NOTIFICATION: - updateNotifications(false, false, false); + updateNotifications(false, true, false); break; default: - updateNotifications(false, false, false); + updateNotifications(false, true, false); break; } } @@ -207,27 +207,15 @@ public int onStartCommand(Intent intent, int flags, int startId) } - @SuppressWarnings("unused") - private void pinNewTask(Intent intent) - { - // check for new task to pin - if (intent.hasExtra(EXTRA_NEW_PINNED_TASK)) - { - ContentSet newTaskToPin = intent.getParcelableExtra(EXTRA_NEW_PINNED_TASK); - makePinNotification(this, mBuilder, newTaskToPin, true, true, false); - } - } - - - private void updateNotifications(boolean isReboot, boolean withSound, boolean withHeadsUpNotification) + private void updateNotifications(boolean isReboot, boolean noSignal, boolean withHeadsUpNotification) { // update pinned tasks mTasksToPin = queryTasksToPin(); - updatePinnedNotifications(mTasksToPin, isReboot, withSound, withHeadsUpNotification); + updatePinnedNotifications(mTasksToPin, isReboot, noSignal, withHeadsUpNotification); } - private void updatePinnedNotifications(ArrayList tasksToPin, boolean isReboot, boolean withSound, boolean withHeadsUpNotification) + private void updatePinnedNotifications(ArrayList tasksToPin, boolean isReboot, boolean noSignal, boolean withHeadsUpNotification) { ArrayList pinnedTaskUris = TaskNotificationHandler.getPinnedTaskUris(this); @@ -238,7 +226,7 @@ private void updatePinnedNotifications(ArrayList tasksToPin, boolean boolean isAlreadyShown = pinnedTaskUris.contains(taskContentSet.getUri()); Integer taskId = TaskFieldAdapters.TASK_ID.get(taskContentSet); notificationManager.notify(taskId, - makePinNotification(this, mBuilder, taskContentSet, !isAlreadyShown || withSound, !isAlreadyShown || withSound, withHeadsUpNotification)); + makePinNotification(this, mBuilder, taskContentSet, isAlreadyShown && noSignal, isAlreadyShown && noSignal, withHeadsUpNotification)); } // remove old notifications @@ -279,32 +267,6 @@ private boolean containsTask(ArrayList tasks, Uri taskUri) } - @SuppressLint("InlinedApi") - public void resendPinNotification(Uri taskUri) - { - if (taskUri == null) - { - return; - } - long taskId = ContentUris.parseId(taskUri); - if (taskId < 0) - { - return; - } - Integer notificationId = Long.valueOf(taskId).intValue(); - mBuilder.setDefaults(Notification.DEFAULT_ALL); - mBuilder.setOngoing(true); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) - { - mBuilder.setPriority(Notification.PRIORITY_HIGH); - } - - NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); - notificationManager.notify(notificationId, mBuilder.build()); - } - - private ArrayList queryTasksToPin() { ArrayList tasksToPin = new ArrayList(20); @@ -347,7 +309,7 @@ private ArrayList queryTasksToPin() @TargetApi(Build.VERSION_CODES.JELLY_BEAN) - private static Notification makePinNotification(Context context, Builder builder, ContentSet task, boolean withSound, boolean withTickerText, + private static Notification makePinNotification(Context context, Builder builder, ContentSet task, boolean noSignal, boolean withTickerText, boolean withHeadsUpNotification) { Resources resources = context.getResources(); @@ -430,15 +392,7 @@ private static Notification makePinNotification(Context context, Builder builder // unpin action builder.addAction(NotificationUpdaterService.getUnpinAction(context, TaskFieldAdapters.TASK_ID.get(task), task.getUri())); - // sound - if (withSound) - { - builder.setDefaults(Notification.DEFAULT_ALL); - } - else - { - builder.setDefaults(Notification.DEFAULT_LIGHTS); - } + builder.setDefaults(new SwitchableSignal(context, noSignal).defaultsValue()); return builder.build(); } @@ -525,7 +479,7 @@ private void resendNotification(NotificationAction notificationAction) startIntent.setPackage(getApplicationContext().getPackageName()); startIntent.putExtra(TaskContract.EXTRA_TASK_TITLE, notificationAction.title()); startIntent.putExtra(TaskContract.EXTRA_TASK_TIMESTAMP, notificationAction.getWhen()); - startIntent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, true); + startIntent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, true); sendBroadcast(startIntent); // Due broadcast @@ -534,7 +488,7 @@ private void resendNotification(NotificationAction notificationAction) dueIntent.setPackage(getApplicationContext().getPackageName()); dueIntent.putExtra(TaskContract.EXTRA_TASK_TITLE, notificationAction.title()); dueIntent.putExtra(TaskContract.EXTRA_TASK_TIMESTAMP, notificationAction.getWhen()); - dueIntent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, true); + dueIntent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, true); sendBroadcast(dueIntent); } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/TaskNotificationHandler.java b/opentasks/src/main/java/org/dmfs/tasks/notification/TaskNotificationHandler.java index c23f1a348..8748ae407 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/TaskNotificationHandler.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/TaskNotificationHandler.java @@ -185,21 +185,21 @@ static ArrayList getPinnedTaskUris(Context context) } - public static void sendPinnedTaskStartNotification(Context context, Uri taskUri, boolean silent) + public static void sendPinnedTaskStartNotification(Context context, Uri taskUri, boolean noSignal) { Intent intent = new Intent(context, NotificationUpdaterService.class); intent.setAction(NotificationUpdaterService.ACTION_PINNED_TASK_START); - intent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, silent); + intent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, noSignal); intent.setData(taskUri); context.startService(intent); } - public static void sendPinnedTaskDueNotification(Context context, Uri taskUri, boolean silent) + public static void sendPinnedTaskDueNotification(Context context, Uri taskUri, boolean noSignal) { Intent intent = new Intent(context, NotificationUpdaterService.class); intent.setAction(NotificationUpdaterService.ACTION_PINNED_TASK_DUE); - intent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, silent); + intent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, noSignal); intent.setData(taskUri); context.startService(intent); } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java new file mode 100644 index 000000000..78930da2a --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.app.Notification; + +import org.dmfs.tasks.utils.BitFlagUtils; + + +/** + * {@link NotificationSignal} which receives all the relevant signal booleans in the constructor. + * + * @author Gabor Keszthelyi + */ +public final class CustomizableSignal implements NotificationSignal +{ + private final boolean mSound; + private final boolean mVibrate; + private final boolean mLight; + + + public CustomizableSignal(boolean sound, boolean vibrate, boolean light) + { + mSound = sound; + mVibrate = vibrate; + mLight = light; + } + + + @Override + public int defaultsValue() + { + int result = 0; + if (mSound) + { + result = BitFlagUtils.addFlag(result, Notification.DEFAULT_SOUND); + } + if (mVibrate) + { + result = BitFlagUtils.addFlag(result, Notification.DEFAULT_VIBRATE); + } + if (mLight) + { + result = BitFlagUtils.addFlag(result, Notification.DEFAULT_LIGHTS); + } + return result; + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java new file mode 100644 index 000000000..d1bab271f --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +/** + * {@link NotificationSignal} for no signal, i.e. sound, vibration, lights disabled. + * + * @author Gabor Keszthelyi + */ +public final class NoSignal implements NotificationSignal +{ + @Override + public int defaultsValue() + { + return 0; + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java new file mode 100644 index 000000000..73dbe340b --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java @@ -0,0 +1,36 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.support.v4.app.NotificationCompat; + + +/** + * Represents a notification signal setting (signal meaning sound, vibration, lights) that can be set for a {@link NotificationCompat} + * with {@link NotificationCompat.Builder#setDefaults(int)}. + * + * @author Gabor Keszthelyi + */ +public interface NotificationSignal +{ + /** + * Returns the value that can be used for {@link NotificationCompat.Builder#setDefaults(int)}. + *

+ * (0 means no signal.) + */ + int defaultsValue(); +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java new file mode 100644 index 000000000..11c61c085 --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java @@ -0,0 +1,52 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import org.dmfs.tasks.R; + + +/** + * {@link NotificationSignal} that corresponds to the user defined settings in the app's Settings screen. + * + * @author Gabor Keszthelyi + */ +public final class SettingsSignal implements NotificationSignal +{ + private final Context mContext; + + + public SettingsSignal(Context context) + { + mContext = context.getApplicationContext(); + } + + + @Override + public int defaultsValue() + { + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext); + boolean sound = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_sound), true); + boolean vibrate = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_vibrate), true); + boolean lights = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_lights), true); + + return new CustomizableSignal(sound, vibrate, lights).defaultsValue(); + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java new file mode 100644 index 000000000..0ed7faa43 --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java @@ -0,0 +1,43 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.content.Context; + + +/** + * {@link NotificationSignal} that delegates to either {@link NoSignal} or {@link SettingsSignal} based on the received boolean. + * + * @author Gabor Keszthelyi + */ +public final class SwitchableSignal implements NotificationSignal +{ + private final NotificationSignal mDelegate; + + + public SwitchableSignal(Context context, boolean withoutSignal) + { + mDelegate = withoutSignal ? new NoSignal() : new SettingsSignal(context); + } + + + @Override + public int defaultsValue() + { + return mDelegate.defaultsValue(); + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java b/opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java new file mode 100644 index 000000000..dc0a02cac --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java @@ -0,0 +1,56 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.utils; + +/** + * Convenience methods for manipulating bit flags. + *

+ * Code from Flag Attributes in Android — How to Use Them + * + * @author Gabor Keszthelyi + */ +public final class BitFlagUtils +{ + + public static boolean containsFlag(int flagSet, int flag) + { + return (flagSet | flag) == flagSet; + } + + + public static int addFlag(int flagSet, int flag) + { + return flagSet | flag; + } + + + public static int toggleFlag(int flagSet, int flag) + { + return flagSet ^ flag; + } + + + public static int removeFlag(int flagSet, int flag) + { + return flagSet & (~flag); + } + + + private BitFlagUtils() + { + } +} diff --git a/opentasks/src/main/res/values/prefs.xml b/opentasks/src/main/res/values/prefs.xml index d2e1f7e09..e918575ba 100644 --- a/opentasks/src/main/res/values/prefs.xml +++ b/opentasks/src/main/res/values/prefs.xml @@ -1,7 +1,16 @@ - schedjoules_pref_notification_enabled + opentasks_pref_notification_enabled + + opentasks_pref_notification_sound + + opentasks_pref_notification_vibrate + + opentasks_pref_notification_lights \ No newline at end of file diff --git a/opentasks/src/main/res/values/strings.xml b/opentasks/src/main/res/values/strings.xml index 1fdcad9f3..829ca226a 100644 --- a/opentasks/src/main/res/values/strings.xml +++ b/opentasks/src/main/res/values/strings.xml @@ -250,4 +250,15 @@ Create Task + + + Notification signals + + Sound + + Vibrate + + Pulse light + + diff --git a/opentasks/src/main/res/xml/app_preferences.xml b/opentasks/src/main/res/xml/app_preferences.xml index 8c8b8c119..2b79da83e 100644 --- a/opentasks/src/main/res/xml/app_preferences.xml +++ b/opentasks/src/main/res/xml/app_preferences.xml @@ -1,8 +1,30 @@ - + + + + + + + + + + + \ No newline at end of file From 011b0914d7f02d0771aca9edd26fe12dd914d55a Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Thu, 7 Sep 2017 14:15:39 +0200 Subject: [PATCH 2/7] Update notification settings label string resource name. --- opentasks/src/main/res/values-cs/strings.xml | 2 +- opentasks/src/main/res/values-de/strings.xml | 2 +- opentasks/src/main/res/values-es/strings.xml | 2 +- opentasks/src/main/res/values-fr/strings.xml | 2 +- opentasks/src/main/res/values-hu/strings.xml | 2 +- opentasks/src/main/res/values-ja/strings.xml | 2 +- opentasks/src/main/res/values-nl/strings.xml | 2 +- opentasks/src/main/res/values-pl/strings.xml | 2 +- opentasks/src/main/res/values-pt-rPT/strings.xml | 2 +- opentasks/src/main/res/values-pt-rbr/strings.xml | 2 +- opentasks/src/main/res/values-ru/strings.xml | 2 +- opentasks/src/main/res/values-sk/strings.xml | 2 +- opentasks/src/main/res/values-sr/strings.xml | 2 +- opentasks/src/main/res/values-uk/strings.xml | 2 +- opentasks/src/main/res/values/strings.xml | 3 ++- opentasks/src/main/res/xml/app_preferences.xml | 2 +- 16 files changed, 17 insertions(+), 16 deletions(-) diff --git a/opentasks/src/main/res/values-cs/strings.xml b/opentasks/src/main/res/values-cs/strings.xml index 6c66e726a..191b27cec 100644 --- a/opentasks/src/main/res/values-cs/strings.xml +++ b/opentasks/src/main/res/values-cs/strings.xml @@ -151,7 +151,7 @@ Nastavení Přidat seznam Zobrazené seznamy - Upozornění + Upozornění Synchronizované seznamy diff --git a/opentasks/src/main/res/values-de/strings.xml b/opentasks/src/main/res/values-de/strings.xml index 6f7765ad6..b453aedc7 100644 --- a/opentasks/src/main/res/values-de/strings.xml +++ b/opentasks/src/main/res/values-de/strings.xml @@ -147,7 +147,7 @@ Einstellungen Liste anlegen Sichtbare Listen - Benachrichtigungen + Benachrichtigungen Synchronisierte Listen diff --git a/opentasks/src/main/res/values-es/strings.xml b/opentasks/src/main/res/values-es/strings.xml index 193d2ae1a..c68fd7bf1 100644 --- a/opentasks/src/main/res/values-es/strings.xml +++ b/opentasks/src/main/res/values-es/strings.xml @@ -152,7 +152,7 @@ Ajustes Añadir Lista Listas Mostradas - Notificaciones + Notificaciones Listas Sincronizadas diff --git a/opentasks/src/main/res/values-fr/strings.xml b/opentasks/src/main/res/values-fr/strings.xml index 0cc32d2d9..802bd128e 100644 --- a/opentasks/src/main/res/values-fr/strings.xml +++ b/opentasks/src/main/res/values-fr/strings.xml @@ -150,7 +150,7 @@ Préférences Ajouter une liste Listes montrées - Notifications + Notifications Listes synchronisées diff --git a/opentasks/src/main/res/values-hu/strings.xml b/opentasks/src/main/res/values-hu/strings.xml index c51de9765..2a25d3518 100644 --- a/opentasks/src/main/res/values-hu/strings.xml +++ b/opentasks/src/main/res/values-hu/strings.xml @@ -150,7 +150,7 @@ Beállítások Lista létrehozása Megjelenített listák - Értesítések + Értesítések Szinkronizált listák diff --git a/opentasks/src/main/res/values-ja/strings.xml b/opentasks/src/main/res/values-ja/strings.xml index 7c146c14b..074ee129e 100644 --- a/opentasks/src/main/res/values-ja/strings.xml +++ b/opentasks/src/main/res/values-ja/strings.xml @@ -150,7 +150,7 @@ 設定 リストを追加 表示したリスト - 通知 + 通知 同期したリスト diff --git a/opentasks/src/main/res/values-nl/strings.xml b/opentasks/src/main/res/values-nl/strings.xml index 6a0a5d06e..e9e0cf733 100644 --- a/opentasks/src/main/res/values-nl/strings.xml +++ b/opentasks/src/main/res/values-nl/strings.xml @@ -148,7 +148,7 @@ Instellingen Lijst toevoegen Getoonde Lijsten - Notificaties + Notificaties Gesynchroniseerde Lijst diff --git a/opentasks/src/main/res/values-pl/strings.xml b/opentasks/src/main/res/values-pl/strings.xml index d01cb1449..df0f5d439 100644 --- a/opentasks/src/main/res/values-pl/strings.xml +++ b/opentasks/src/main/res/values-pl/strings.xml @@ -142,7 +142,7 @@ Ustawienia Dodaj listę Wyświetlane listy - Powiadomienia + Powiadomienia Synchronizowane listy diff --git a/opentasks/src/main/res/values-pt-rPT/strings.xml b/opentasks/src/main/res/values-pt-rPT/strings.xml index 414d705ac..c2cfff7fd 100644 --- a/opentasks/src/main/res/values-pt-rPT/strings.xml +++ b/opentasks/src/main/res/values-pt-rPT/strings.xml @@ -150,7 +150,7 @@ Definições Adicionar lista Listas visíveis - Notificações + Notificações Listas sincronizadas diff --git a/opentasks/src/main/res/values-pt-rbr/strings.xml b/opentasks/src/main/res/values-pt-rbr/strings.xml index 3f671d98f..a73720ce5 100644 --- a/opentasks/src/main/res/values-pt-rbr/strings.xml +++ b/opentasks/src/main/res/values-pt-rbr/strings.xml @@ -140,7 +140,7 @@ Configurações Adicionar lista Listas mostradas - Notificações + Notificações Listas sincronizadas diff --git a/opentasks/src/main/res/values-ru/strings.xml b/opentasks/src/main/res/values-ru/strings.xml index 6afdb2023..8d3456027 100644 --- a/opentasks/src/main/res/values-ru/strings.xml +++ b/opentasks/src/main/res/values-ru/strings.xml @@ -147,7 +147,7 @@ Настройки Настройки Используемые списки - Напоминания + Напоминания Все доступные списки diff --git a/opentasks/src/main/res/values-sk/strings.xml b/opentasks/src/main/res/values-sk/strings.xml index 162e3120b..47db62f10 100644 --- a/opentasks/src/main/res/values-sk/strings.xml +++ b/opentasks/src/main/res/values-sk/strings.xml @@ -152,7 +152,7 @@ Nastavenia Pridať zoznam Zobrazené zoznamy - Upozornenia + Upozornenia Synchronizované zoznamy diff --git a/opentasks/src/main/res/values-sr/strings.xml b/opentasks/src/main/res/values-sr/strings.xml index ca5bb10af..31a2f51a5 100644 --- a/opentasks/src/main/res/values-sr/strings.xml +++ b/opentasks/src/main/res/values-sr/strings.xml @@ -141,7 +141,7 @@ Поставке Додај листу Приказане листе - Обавештења + Обавештења Синхронизоване листе diff --git a/opentasks/src/main/res/values-uk/strings.xml b/opentasks/src/main/res/values-uk/strings.xml index f9d5c1192..afb62a424 100644 --- a/opentasks/src/main/res/values-uk/strings.xml +++ b/opentasks/src/main/res/values-uk/strings.xml @@ -143,7 +143,7 @@ Налаштування Додати список Списки, що використовуються - Нагадування + Нагадування Всі доступні списки diff --git a/opentasks/src/main/res/values/strings.xml b/opentasks/src/main/res/values/strings.xml index 829ca226a..e7029de4a 100644 --- a/opentasks/src/main/res/values/strings.xml +++ b/opentasks/src/main/res/values/strings.xml @@ -151,7 +151,6 @@ Settings Add List Displayed Lists - Notifications Synchronized Lists @@ -251,6 +250,8 @@ Create Task + + Notifications Notification signals diff --git a/opentasks/src/main/res/xml/app_preferences.xml b/opentasks/src/main/res/xml/app_preferences.xml index 2b79da83e..e93d27276 100644 --- a/opentasks/src/main/res/xml/app_preferences.xml +++ b/opentasks/src/main/res/xml/app_preferences.xml @@ -3,7 +3,7 @@ Date: Thu, 7 Sep 2017 15:05:08 +0200 Subject: [PATCH 3/7] Improve design of NotificationSignals. --- .../notification/NotificationActionUtils.java | 6 +- .../NotificationUpdaterService.java | 2 +- .../tasks/notification/signals/AllSignal.java | 34 ++++++++++ .../signals/CustomizableSignal.java | 62 ------------------- .../signals/DelegatingNotificationSignal.java | 40 ++++++++++++ .../tasks/notification/signals/Lights.java | 47 ++++++++++++++ .../tasks/notification/signals/NoSignal.java | 2 +- .../signals/NotificationSignal.java | 2 +- .../notification/signals/SettingsSignal.java | 4 +- .../tasks/notification/signals/Sound.java | 47 ++++++++++++++ .../signals/SwitchableSignal.java | 12 ++-- .../tasks/notification/signals/Toggled.java | 60 ++++++++++++++++++ .../tasks/notification/signals/Vibrate.java | 47 ++++++++++++++ .../notification/signals/ToggledTest.java | 62 +++++++++++++++++++ 14 files changed, 352 insertions(+), 75 deletions(-) create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/AllSignal.java delete mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/DelegatingNotificationSignal.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/Lights.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/Sound.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibrate.java create mode 100644 opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java index 6c0838816..d83c634d5 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java @@ -112,7 +112,7 @@ public static void sendDueAlarmNotification(Context context, String title, Uri t mBuilder.setTicker(title); // enable light, sound and vibration - mBuilder.setDefaults(new SwitchableSignal(context, silent).defaultsValue()); + mBuilder.setDefaults(new SwitchableSignal(context, silent).value()); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(Intent.ACTION_VIEW); @@ -194,7 +194,7 @@ public static void sendStartNotification(Context context, String title, Uri task mBuilder.setTicker(title); // enable light, sound and vibration - mBuilder.setDefaults(new SwitchableSignal(context, silent).defaultsValue()); + mBuilder.setDefaults(new SwitchableSignal(context, silent).value()); // set notification time // set displayed time @@ -265,7 +265,7 @@ public static void createUndoNotification(final Context context, NotificationAct builder.setSmallIcon(R.drawable.ic_notification); builder.setWhen(action.getWhen()); - builder.setDefaults(new NoSignal().defaultsValue()); + builder.setDefaults(new NoSignal().value()); final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification); undoView.setTextViewText(R.id.description_text, context.getString(action.mActionTextResId)); diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java index 6533d95d7..1682cde2c 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java @@ -392,7 +392,7 @@ private static Notification makePinNotification(Context context, Builder builder // unpin action builder.addAction(NotificationUpdaterService.getUnpinAction(context, TaskFieldAdapters.TASK_ID.get(task), task.getUri())); - builder.setDefaults(new SwitchableSignal(context, noSignal).defaultsValue()); + builder.setDefaults(new SwitchableSignal(context, noSignal).value()); return builder.build(); } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/AllSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/AllSignal.java new file mode 100644 index 000000000..3d5ca4c51 --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/AllSignal.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.app.Notification; + + +/** + * Represents {@link NotificationSignal} for {@link Notification#DEFAULT_ALL}. + * + * @author Gabor Keszthelyi + */ +public final class AllSignal implements NotificationSignal +{ + @Override + public int value() + { + return Notification.DEFAULT_ALL; + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java deleted file mode 100644 index 78930da2a..000000000 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/CustomizableSignal.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2017 dmfs GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.dmfs.tasks.notification.signals; - -import android.app.Notification; - -import org.dmfs.tasks.utils.BitFlagUtils; - - -/** - * {@link NotificationSignal} which receives all the relevant signal booleans in the constructor. - * - * @author Gabor Keszthelyi - */ -public final class CustomizableSignal implements NotificationSignal -{ - private final boolean mSound; - private final boolean mVibrate; - private final boolean mLight; - - - public CustomizableSignal(boolean sound, boolean vibrate, boolean light) - { - mSound = sound; - mVibrate = vibrate; - mLight = light; - } - - - @Override - public int defaultsValue() - { - int result = 0; - if (mSound) - { - result = BitFlagUtils.addFlag(result, Notification.DEFAULT_SOUND); - } - if (mVibrate) - { - result = BitFlagUtils.addFlag(result, Notification.DEFAULT_VIBRATE); - } - if (mLight) - { - result = BitFlagUtils.addFlag(result, Notification.DEFAULT_LIGHTS); - } - return result; - } -} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/DelegatingNotificationSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/DelegatingNotificationSignal.java new file mode 100644 index 000000000..1f4c9f9d5 --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/DelegatingNotificationSignal.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +/** + * Abstract base class for {@link NotificationSignal}s that simply delegate to another instance, composed in the constructor. + * + * @author Gabor Keszthelyi + */ +public abstract class DelegatingNotificationSignal implements NotificationSignal +{ + private final NotificationSignal mDelegate; + + + public DelegatingNotificationSignal(NotificationSignal delegate) + { + mDelegate = delegate; + } + + + @Override + public final int value() + { + return mDelegate.value(); + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Lights.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Lights.java new file mode 100644 index 000000000..60168d64a --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Lights.java @@ -0,0 +1,47 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.app.Notification; + + +/** + * {@link NotificationSignal} for representing, adding, or toggling {@link Notification#DEFAULT_LIGHTS} + * value. + * + * @author Gabor Keszthelyi + */ +public final class Lights extends DelegatingNotificationSignal +{ + public Lights(boolean enable, NotificationSignal original) + { + super(new Toggled(Notification.DEFAULT_LIGHTS, enable, original)); + } + + + public Lights(NotificationSignal original) + { + super(new Toggled(Notification.DEFAULT_LIGHTS, true, original)); + } + + + public Lights() + { + super(new Toggled(Notification.DEFAULT_LIGHTS, true, new NoSignal())); + } + +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java index d1bab271f..25313f633 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NoSignal.java @@ -24,7 +24,7 @@ public final class NoSignal implements NotificationSignal { @Override - public int defaultsValue() + public int value() { return 0; } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java index 73dbe340b..76bb23c02 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/NotificationSignal.java @@ -32,5 +32,5 @@ public interface NotificationSignal *

* (0 means no signal.) */ - int defaultsValue(); + int value(); } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java index 11c61c085..aa3bdfb0b 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java @@ -40,13 +40,13 @@ public SettingsSignal(Context context) @Override - public int defaultsValue() + public int value() { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext); boolean sound = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_sound), true); boolean vibrate = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_vibrate), true); boolean lights = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_lights), true); - return new CustomizableSignal(sound, vibrate, lights).defaultsValue(); + return new Lights(lights, new Vibrate(vibrate, new Sound(sound, new AllSignal()))).value(); } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Sound.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Sound.java new file mode 100644 index 000000000..83f2ef819 --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Sound.java @@ -0,0 +1,47 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.app.Notification; + + +/** + * {@link NotificationSignal} for representing, adding, or toggling {@link Notification#DEFAULT_SOUND} + * value. + * + * @author Gabor Keszthelyi + */ +public final class Sound extends DelegatingNotificationSignal +{ + public Sound(boolean enable, NotificationSignal original) + { + super(new Toggled(Notification.DEFAULT_SOUND, enable, original)); + } + + + public Sound(NotificationSignal original) + { + super(new Toggled(Notification.DEFAULT_SOUND, true, original)); + } + + + public Sound() + { + super(new Toggled(Notification.DEFAULT_SOUND, true, new NoSignal())); + } + +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java index 0ed7faa43..b199f1b5c 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java @@ -20,24 +20,26 @@ /** - * {@link NotificationSignal} that delegates to either {@link NoSignal} or {@link SettingsSignal} based on the received boolean. + * {@link NotificationSignal} that evaluates to either {@link NoSignal} or {@link SettingsSignal} based on the received boolean. * * @author Gabor Keszthelyi */ public final class SwitchableSignal implements NotificationSignal { - private final NotificationSignal mDelegate; + private final Context mContext; + private final boolean mWithoutSignal; public SwitchableSignal(Context context, boolean withoutSignal) { - mDelegate = withoutSignal ? new NoSignal() : new SettingsSignal(context); + mContext = context; + mWithoutSignal = withoutSignal; } @Override - public int defaultsValue() + public int value() { - return mDelegate.defaultsValue(); + return (mWithoutSignal ? new NoSignal() : new SettingsSignal(mContext)).value(); } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java new file mode 100644 index 000000000..ae8d9fd0b --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.app.Notification; + +import org.dmfs.tasks.utils.BitFlagUtils; + + +/** + * Decorator for {@link NotificationSignal} that toggles a flag value. Flag must be one of + * {@link Notification#DEFAULT_LIGHTS}, {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE}. + *

+ * Intended for internal use in the package only since it exposes the bit flag mechanism. + * + * @author Gabor Keszthelyi + */ +final class Toggled implements NotificationSignal +{ + private final int mFlag; + private final boolean mEnable; + private final NotificationSignal mOriginal; + + + /** + * @param flag + * must be one of {@link Notification#DEFAULT_LIGHTS}, {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE} + */ + Toggled(int flag, boolean enable, NotificationSignal original) + { + if (flag != Notification.DEFAULT_VIBRATE && flag != Notification.DEFAULT_SOUND && flag != Notification.DEFAULT_LIGHTS) + { + throw new IllegalArgumentException("Notification signal flag is not valid: " + flag); + } + mFlag = flag; + mEnable = enable; + mOriginal = original; + } + + + @Override + public int value() + { + return mEnable ? BitFlagUtils.addFlag(mOriginal.value(), mFlag) : mOriginal.value(); + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibrate.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibrate.java new file mode 100644 index 000000000..c163d41cf --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibrate.java @@ -0,0 +1,47 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.app.Notification; + + +/** + * {@link NotificationSignal} for representing, adding, or toggling {@link Notification#DEFAULT_VIBRATE} + * value. + * + * @author Gabor Keszthelyi + */ +public final class Vibrate extends DelegatingNotificationSignal +{ + public Vibrate(boolean enable, NotificationSignal original) + { + super(new Toggled(Notification.DEFAULT_VIBRATE, enable, original)); + } + + + public Vibrate(NotificationSignal original) + { + super(new Toggled(Notification.DEFAULT_VIBRATE, true, original)); + } + + + public Vibrate() + { + super(new Toggled(Notification.DEFAULT_VIBRATE, true, new NoSignal())); + } + +} diff --git a/opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java b/opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java new file mode 100644 index 000000000..70ac4d04a --- /dev/null +++ b/opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.notification.signals; + +import android.app.Notification; + +import org.junit.Test; + +import static org.dmfs.tasks.utils.BitFlagUtils.containsFlag; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + +/** + * Unit test for {@link Toggled}. + * + * @author Gabor Keszthelyi + */ +public final class ToggledTest +{ + @Test + public void testValidFlags() + { + new Toggled(Notification.DEFAULT_SOUND, true, new NoSignal()); + new Toggled(Notification.DEFAULT_VIBRATE, true, new NoSignal()); + new Toggled(Notification.DEFAULT_LIGHTS, true, new NoSignal()); + } + + + @Test(expected = IllegalArgumentException.class) + public void testInValidFlag() + { + new Toggled(15, true, new NoSignal()); + } + + + @Test + public void testAddingFlag() + { + assertTrue(containsFlag( + new Toggled(Notification.DEFAULT_SOUND, true, new NoSignal()).value(), + Notification.DEFAULT_SOUND)); + + assertFalse(containsFlag( + new Toggled(Notification.DEFAULT_SOUND, false, new NoSignal()).value(), + Notification.DEFAULT_SOUND)); + } +} From 5635239256eca608fa58160c8ca95d8bf2f6b376 Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Thu, 7 Sep 2017 16:14:11 +0200 Subject: [PATCH 4/7] Add German translations. --- opentasks/src/main/res/values-de/strings.xml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/opentasks/src/main/res/values-de/strings.xml b/opentasks/src/main/res/values-de/strings.xml index b453aedc7..70f975ac8 100644 --- a/opentasks/src/main/res/values-de/strings.xml +++ b/opentasks/src/main/res/values-de/strings.xml @@ -147,7 +147,6 @@ Einstellungen Liste anlegen Sichtbare Listen - Benachrichtigungen Synchronisierte Listen @@ -246,4 +245,10 @@ Aufgabe erstellen + Benachrichtigungen + Benachrichtungseinstellungen + Ton + Vibration + LED + From 562d08b42b1e96003db660b08e7c4b5fe6e85b3c Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Thu, 7 Sep 2017 16:29:43 +0200 Subject: [PATCH 5/7] Fix flag disable in Toggled. Rename Vibrate to Vibration. Use NoSignal instead of AllSignal in Toggled (doesn't matter). --- .../dmfs/tasks/notification/signals/SettingsSignal.java | 2 +- .../java/org/dmfs/tasks/notification/signals/Toggled.java | 2 +- .../notification/signals/{Vibrate.java => Vibration.java} | 8 ++++---- opentasks/src/main/res/values/strings.xml | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) rename opentasks/src/main/java/org/dmfs/tasks/notification/signals/{Vibrate.java => Vibration.java} (84%) diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java index aa3bdfb0b..d7d94b726 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SettingsSignal.java @@ -47,6 +47,6 @@ public int value() boolean vibrate = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_vibrate), true); boolean lights = settings.getBoolean(mContext.getString(R.string.opentasks_pref_notification_lights), true); - return new Lights(lights, new Vibrate(vibrate, new Sound(sound, new AllSignal()))).value(); + return new Lights(lights, new Vibration(vibrate, new Sound(sound, new NoSignal()))).value(); } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java index ae8d9fd0b..b148e272c 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java @@ -55,6 +55,6 @@ final class Toggled implements NotificationSignal @Override public int value() { - return mEnable ? BitFlagUtils.addFlag(mOriginal.value(), mFlag) : mOriginal.value(); + return mEnable ? BitFlagUtils.addFlag(mOriginal.value(), mFlag) : BitFlagUtils.removeFlag(mOriginal.value(), mFlag); } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibrate.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibration.java similarity index 84% rename from opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibrate.java rename to opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibration.java index c163d41cf..b743904a6 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibrate.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Vibration.java @@ -25,21 +25,21 @@ * * @author Gabor Keszthelyi */ -public final class Vibrate extends DelegatingNotificationSignal +public final class Vibration extends DelegatingNotificationSignal { - public Vibrate(boolean enable, NotificationSignal original) + public Vibration(boolean enable, NotificationSignal original) { super(new Toggled(Notification.DEFAULT_VIBRATE, enable, original)); } - public Vibrate(NotificationSignal original) + public Vibration(NotificationSignal original) { super(new Toggled(Notification.DEFAULT_VIBRATE, true, original)); } - public Vibrate() + public Vibration() { super(new Toggled(Notification.DEFAULT_VIBRATE, true, new NoSignal())); } diff --git a/opentasks/src/main/res/values/strings.xml b/opentasks/src/main/res/values/strings.xml index e7029de4a..c1db2bd9f 100644 --- a/opentasks/src/main/res/values/strings.xml +++ b/opentasks/src/main/res/values/strings.xml @@ -256,7 +256,7 @@ Notification signals Sound - + Vibrate Pulse light From c4281cef8295b8a82efa31167798e4c65f83644c Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Fri, 8 Sep 2017 14:07:50 +0200 Subject: [PATCH 6/7] Improve former SwitchableSignal, renamed to Conditional. --- .../notification/NotificationActionUtils.java | 6 ++--- .../NotificationUpdaterService.java | 4 ++-- ...SwitchableSignal.java => Conditional.java} | 23 ++++++++++++------- 3 files changed, 20 insertions(+), 13 deletions(-) rename opentasks/src/main/java/org/dmfs/tasks/notification/signals/{SwitchableSignal.java => Conditional.java} (55%) diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java index d83c634d5..0aa398065 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationActionUtils.java @@ -37,8 +37,8 @@ import android.widget.RemoteViews; import org.dmfs.tasks.R; +import org.dmfs.tasks.notification.signals.Conditional; import org.dmfs.tasks.notification.signals.NoSignal; -import org.dmfs.tasks.notification.signals.SwitchableSignal; import org.dmfs.tasks.utils.DateFormatter; import org.dmfs.tasks.utils.DateFormatter.DateFormatContext; @@ -112,7 +112,7 @@ public static void sendDueAlarmNotification(Context context, String title, Uri t mBuilder.setTicker(title); // enable light, sound and vibration - mBuilder.setDefaults(new SwitchableSignal(context, silent).value()); + mBuilder.setDefaults(new Conditional(!silent, context).value()); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(Intent.ACTION_VIEW); @@ -194,7 +194,7 @@ public static void sendStartNotification(Context context, String title, Uri task mBuilder.setTicker(title); // enable light, sound and vibration - mBuilder.setDefaults(new SwitchableSignal(context, silent).value()); + mBuilder.setDefaults(new Conditional(!silent, context).value()); // set notification time // set displayed time diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java index 1682cde2c..34d79f02e 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/NotificationUpdaterService.java @@ -55,7 +55,7 @@ import org.dmfs.tasks.model.ContentSet; import org.dmfs.tasks.model.TaskFieldAdapters; import org.dmfs.tasks.notification.NotificationActionUtils.NotificationAction; -import org.dmfs.tasks.notification.signals.SwitchableSignal; +import org.dmfs.tasks.notification.signals.Conditional; import java.util.ArrayList; import java.util.TimeZone; @@ -392,7 +392,7 @@ private static Notification makePinNotification(Context context, Builder builder // unpin action builder.addAction(NotificationUpdaterService.getUnpinAction(context, TaskFieldAdapters.TASK_ID.get(task), task.getUri())); - builder.setDefaults(new SwitchableSignal(context, noSignal).value()); + builder.setDefaults(new Conditional(!noSignal, context).value()); return builder.build(); } diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Conditional.java similarity index 55% rename from opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java rename to opentasks/src/main/java/org/dmfs/tasks/notification/signals/Conditional.java index b199f1b5c..96089a6d2 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/SwitchableSignal.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Conditional.java @@ -20,26 +20,33 @@ /** - * {@link NotificationSignal} that evaluates to either {@link NoSignal} or {@link SettingsSignal} based on the received boolean. + * {@link NotificationSignal} that conditionally delegates to either {@link NoSignal} or to the given {@link NotificationSignal} + * based on the provided boolean. * * @author Gabor Keszthelyi */ -public final class SwitchableSignal implements NotificationSignal +public final class Conditional implements NotificationSignal { - private final Context mContext; - private final boolean mWithoutSignal; + private final boolean mUseSignal; + private final NotificationSignal mDelegate; - public SwitchableSignal(Context context, boolean withoutSignal) + public Conditional(boolean useSignal, NotificationSignal delegate) { - mContext = context; - mWithoutSignal = withoutSignal; + mUseSignal = useSignal; + mDelegate = delegate; + } + + + public Conditional(boolean useSignal, Context context) + { + this(useSignal, new SettingsSignal(context)); } @Override public int value() { - return (mWithoutSignal ? new NoSignal() : new SettingsSignal(mContext)).value(); + return (mUseSignal ? mDelegate : new NoSignal()).value(); } } From 0abbd2a624759f847590dcc291955bc2a8015bba Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Fri, 8 Sep 2017 14:17:40 +0200 Subject: [PATCH 7/7] Remove BitFlagUtils. Move validation in Toggled from ctor to method. --- .../tasks/notification/signals/Toggled.java | 24 +++++--- .../org/dmfs/tasks/utils/BitFlagUtils.java | 56 ------------------- .../notification/signals/ToggledTest.java | 26 ++++----- 3 files changed, 27 insertions(+), 79 deletions(-) delete mode 100644 opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java index b148e272c..bcf81f9d7 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/signals/Toggled.java @@ -18,8 +18,6 @@ import android.app.Notification; -import org.dmfs.tasks.utils.BitFlagUtils; - /** * Decorator for {@link NotificationSignal} that toggles a flag value. Flag must be one of @@ -42,10 +40,6 @@ final class Toggled implements NotificationSignal */ Toggled(int flag, boolean enable, NotificationSignal original) { - if (flag != Notification.DEFAULT_VIBRATE && flag != Notification.DEFAULT_SOUND && flag != Notification.DEFAULT_LIGHTS) - { - throw new IllegalArgumentException("Notification signal flag is not valid: " + flag); - } mFlag = flag; mEnable = enable; mOriginal = original; @@ -55,6 +49,22 @@ final class Toggled implements NotificationSignal @Override public int value() { - return mEnable ? BitFlagUtils.addFlag(mOriginal.value(), mFlag) : BitFlagUtils.removeFlag(mOriginal.value(), mFlag); + if (mFlag != Notification.DEFAULT_VIBRATE && mFlag != Notification.DEFAULT_SOUND && mFlag != Notification.DEFAULT_LIGHTS) + { + throw new IllegalArgumentException("Notification signal flag is not valid: " + mFlag); + } + return mEnable ? addFlag(mOriginal.value(), mFlag) : removeFlag(mOriginal.value(), mFlag); + } + + + private int addFlag(int flagSet, int flag) + { + return flagSet | flag; + } + + + private int removeFlag(int flagSet, int flag) + { + return flagSet & (~flag); } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java b/opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java deleted file mode 100644 index dc0a02cac..000000000 --- a/opentasks/src/main/java/org/dmfs/tasks/utils/BitFlagUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2017 dmfs GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.dmfs.tasks.utils; - -/** - * Convenience methods for manipulating bit flags. - *

- * Code from Flag Attributes in Android — How to Use Them - * - * @author Gabor Keszthelyi - */ -public final class BitFlagUtils -{ - - public static boolean containsFlag(int flagSet, int flag) - { - return (flagSet | flag) == flagSet; - } - - - public static int addFlag(int flagSet, int flag) - { - return flagSet | flag; - } - - - public static int toggleFlag(int flagSet, int flag) - { - return flagSet ^ flag; - } - - - public static int removeFlag(int flagSet, int flag) - { - return flagSet & (~flag); - } - - - private BitFlagUtils() - { - } -} diff --git a/opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java b/opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java index 70ac4d04a..68467b786 100644 --- a/opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java +++ b/opentasks/src/test/java/org/dmfs/tasks/notification/signals/ToggledTest.java @@ -20,9 +20,8 @@ import org.junit.Test; -import static org.dmfs.tasks.utils.BitFlagUtils.containsFlag; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; /** @@ -33,30 +32,25 @@ public final class ToggledTest { @Test - public void testValidFlags() + public void testValidFlags_dontThrowException() { - new Toggled(Notification.DEFAULT_SOUND, true, new NoSignal()); - new Toggled(Notification.DEFAULT_VIBRATE, true, new NoSignal()); - new Toggled(Notification.DEFAULT_LIGHTS, true, new NoSignal()); + new Toggled(Notification.DEFAULT_SOUND, true, new NoSignal()).value(); + new Toggled(Notification.DEFAULT_VIBRATE, true, new NoSignal()).value(); + new Toggled(Notification.DEFAULT_LIGHTS, true, new NoSignal()).value(); } @Test(expected = IllegalArgumentException.class) - public void testInValidFlag() + public void testInValidFlag_throwsException() { - new Toggled(15, true, new NoSignal()); + new Toggled(15, true, new NoSignal()).value(); } @Test public void testAddingFlag() { - assertTrue(containsFlag( - new Toggled(Notification.DEFAULT_SOUND, true, new NoSignal()).value(), - Notification.DEFAULT_SOUND)); - - assertFalse(containsFlag( - new Toggled(Notification.DEFAULT_SOUND, false, new NoSignal()).value(), - Notification.DEFAULT_SOUND)); + assertThat(new Toggled(Notification.DEFAULT_SOUND, true, new NoSignal()).value(), is(new NoSignal().value() | Notification.DEFAULT_SOUND)); + assertThat(new Toggled(Notification.DEFAULT_SOUND, false, new NoSignal()).value(), is(new NoSignal().value())); } }