Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification signal settings. #305 #403

Merged
merged 7 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve design of NotificationSignals.
  • Loading branch information
Gabor Keszthelyi committed Sep 7, 2017
commit 4b373477f7de00a4215f50eb0cb43d4c3d2d9d08
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a decorator which evaluates lazily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not happy with this class. As mentioned before this should be a decorator which takes the "enabled" signal in the constructor. There could be a secondary constructor which then passes a SettingsSignal. So this becomes more like a "conditional signal" like so:

public final class Conditional implements NotificationSignal
{
   private final NotificationSignal mDelegate;
   private final boolean mCondition;

   public Conditional(Context context, boolean condition)
   {
      this(new SettingsSignal(mContext), condition);
   }

   public Conditional(NotificationSignal delegate, boolean condition)
   {
      mDelegate = delegate;
      mCondition = contition;
   }
  
   @Override
    public int value()
    {
        return (mWithoutSignal ? new NoSignal() : mDelegate).value();
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it's better that way, I missed the decoration the first time. Updated now.

{
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