Skip to content

Commit

Permalink
Improve design of NotificationSignals.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Keszthelyi committed Sep 7, 2017
1 parent 011b091 commit 4b37347
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public final class NoSignal implements NotificationSignal
{
@Override
public int defaultsValue()
public int value()
{
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public interface NotificationSignal
* <p>
* (<code>0</code> means no signal.)
*/
int defaultsValue();
int value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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}.
* <p>
* 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();
}
}
Loading

0 comments on commit 4b37347

Please sign in to comment.