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)); + } +}