Skip to content

Commit

Permalink
Notification signal settings. #305
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Keszthelyi committed Aug 10, 2017
1 parent 5c1a4d7 commit d9304f0
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);

}
}
Expand All @@ -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;
}

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}
Expand Down Expand Up @@ -176,23 +176,23 @@ 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:
case Intent.ACTION_TIME_CHANGED:
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;
}
}
Expand All @@ -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<ContentSet> tasksToPin, boolean isReboot, boolean withSound, boolean withHeadsUpNotification)
private void updatePinnedNotifications(ArrayList<ContentSet> tasksToPin, boolean isReboot, boolean noSignal, boolean withHeadsUpNotification)
{
ArrayList<Uri> pinnedTaskUris = TaskNotificationHandler.getPinnedTaskUris(this);

Expand All @@ -238,7 +226,7 @@ private void updatePinnedNotifications(ArrayList<ContentSet> 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
Expand Down Expand Up @@ -279,32 +267,6 @@ private boolean containsTask(ArrayList<ContentSet> 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<ContentSet> queryTasksToPin()
{
ArrayList<ContentSet> tasksToPin = new ArrayList<ContentSet>(20);
Expand Down Expand Up @@ -347,7 +309,7 @@ private ArrayList<ContentSet> 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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ static ArrayList<Uri> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit d9304f0

Please sign in to comment.